blob: 747852735d10e91469c7aa2e74e88a8cd8db9cf9 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020064 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
300 if (!(wp->w_popup_flags & POPF_DRAG))
301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200386 "(_) => popup_close(%d)", wp->w_id);
387 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
K.Takataeeec2542021-06-02 13:28:16 +0200405 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100406 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
407 return poppos_entries[nr].pp_val;
408
409 if (give_error)
410 semsg(_(e_invarg2), str);
411 return POPPOS_NONE;
412}
413
Bram Moolenaar17627312019-06-02 19:53:44 +0200414/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200415 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200416 */
417 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200418apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200419{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200420 int nr;
421 char_u *str;
422 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200423
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200426 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200429 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200430 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200431 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200432
433 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200434 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200435 wp->w_wantline = nr;
436 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200437 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200438 wp->w_wantcol = nr;
439
Bram Moolenaar55881332020-08-18 13:04:15 +0200440
441 nr = dict_get_bool(d, (char_u *)"fixed", -1);
442 if (nr != -1)
443 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200444
Bram Moolenaar12034e22019-08-25 22:25:02 +0200445 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100446 poppos_T ppt = get_pos_entry(d, TRUE);
447
448 if (ppt != POPPOS_NONE)
449 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200450 }
451
452 str = dict_get_string(d, (char_u *)"textprop", FALSE);
453 if (str != NULL)
454 {
455 wp->w_popup_prop_type = 0;
456 if (*str != NUL)
457 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100458 wp->w_popup_prop_win = curwin;
459 di = dict_find(d, (char_u *)"textpropwin", -1);
460 if (di != NULL)
461 {
462 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100463 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100464 wp->w_popup_prop_win = curwin;
465 }
466
467 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200468 if (nr <= 0)
469 nr = find_prop_type_id(str, NULL);
470 if (nr <= 0)
471 semsg(_(e_invarg2), str);
472 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200473 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 }
475 }
476
477 di = dict_find(d, (char_u *)"textpropid", -1);
478 if (di != NULL)
479 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200480}
481
Bram Moolenaarb0992022020-01-30 14:55:42 +0100482/*
483 * Handle "moved" and "mousemoved" arguments.
484 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200485 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200486handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
487{
488 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
489 {
490 char_u *s = di->di_tv.vval.v_string;
491 int flags = 0;
492
493 if (STRCMP(s, "word") == 0)
494 flags = FIND_IDENT | FIND_STRING;
495 else if (STRCMP(s, "WORD") == 0)
496 flags = FIND_STRING;
497 else if (STRCMP(s, "expr") == 0)
498 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
499 else if (STRCMP(s, "any") != 0)
500 semsg(_(e_invarg2), s);
501 if (flags != 0)
502 {
503 if (mousemoved)
504 set_mousemoved_columns(wp, flags);
505 else
506 set_moved_columns(wp, flags);
507 }
508 }
509 else if (di->di_tv.v_type == VAR_LIST
510 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200511 && (di->di_tv.vval.v_list->lv_len == 2
512 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200513 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200514 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100515 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200516 int mincol;
517 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200518
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100520 li = l->lv_first;
521 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200522 {
523 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
524
525 // Three numbers, might be from popup_getoptions().
526 if (mousemoved)
527 wp->w_popup_mouse_row = nr;
528 else
529 wp->w_popup_lnum = nr;
530 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100531 if (nr == 0)
532 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200533 }
534
535 mincol = tv_get_number(&li->li_tv);
536 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200537 if (mousemoved)
538 {
539 wp->w_popup_mouse_mincol = mincol;
540 wp->w_popup_mouse_maxcol = maxcol;
541 }
542 else
543 {
544 wp->w_popup_mincol = mincol;
545 wp->w_popup_maxcol = maxcol;
546 }
547 }
548 else
549 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
550}
551
552 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200553check_highlight(dict_T *dict, char *name, char_u **pval)
554{
555 dictitem_T *di;
556 char_u *str;
557
558 di = dict_find(dict, (char_u *)name, -1);
559 if (di != NULL)
560 {
561 if (di->di_tv.v_type != VAR_STRING)
562 semsg(_(e_invargval), name);
563 else
564 {
565 str = tv_get_string(&di->di_tv);
566 if (*str != NUL)
567 *pval = vim_strsave(str);
568 }
569 }
570}
571
Bram Moolenaarae943152019-06-16 22:54:14 +0200572/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200573 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200574 */
575 static void
576popup_show_curline(win_T *wp)
577{
578 if (wp->w_cursor.lnum < wp->w_topline)
579 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200580 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200581 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200582 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200583 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200584 if (wp->w_topline < 1)
585 wp->w_topline = 1;
586 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
587 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200588 while (wp->w_topline < wp->w_cursor.lnum
589 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
590 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
591 > wp->w_height)
592 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200593 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200594
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200595 // Don't let "firstline" cause a scroll.
596 if (wp->w_firstline > 0)
597 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200598}
599
600/*
601 * Get the sign group name for window "wp".
602 * Returns a pointer to a static buffer, overwritten on the next call.
603 */
604 static char_u *
605popup_get_sign_name(win_T *wp)
606{
607 static char buf[30];
608
609 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
610 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200611}
612
613/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200614 * Highlight the line with the cursor.
615 * Also scrolls the text to put the cursor line in view.
616 */
617 static void
618popup_highlight_curline(win_T *wp)
619{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200620 int sign_id = 0;
621 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622
Bram Moolenaar72570732019-11-30 14:21:53 +0100623 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200624
625 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
626 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200627 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200628
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200629 if (!sign_exists_by_name(sign_name))
630 {
631 char *linehl = "PopupSelected";
632
633 if (syn_name2id((char_u *)linehl) == 0)
634 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200635 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200636 }
637
Bram Moolenaar72570732019-11-30 14:21:53 +0100638 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200639 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
640 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200641 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200642 else
643 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200644 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645}
646
647/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200648 * Shared between popup_create() and f_popup_setoptions().
649 */
650 static void
651apply_general_options(win_T *wp, dict_T *dict)
652{
653 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200654 int nr;
655 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200656
Bram Moolenaarae943152019-06-16 22:54:14 +0200657 // TODO: flip
658
659 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200660 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200661 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200662 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200663 if (wp->w_firstline < 0)
664 wp->w_firstline = -1;
665 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200666
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200667 nr = dict_get_bool(dict, (char_u *)"scrollbar", -1);
668 if (nr != -1)
669 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200670
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200671 str = dict_get_string(dict, (char_u *)"title", FALSE);
672 if (str != NULL)
673 {
674 vim_free(wp->w_popup_title);
675 wp->w_popup_title = vim_strsave(str);
676 }
677
Bram Moolenaar55881332020-08-18 13:04:15 +0200678 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
679 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200680 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200681
Bram Moolenaar55881332020-08-18 13:04:15 +0200682 nr = dict_get_bool(dict, (char_u *)"drag", -1);
683 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200684 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200685 if (nr)
686 wp->w_popup_flags |= POPF_DRAG;
687 else
688 wp->w_popup_flags &= ~POPF_DRAG;
689 }
690
Bram Moolenaar55881332020-08-18 13:04:15 +0200691 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
692 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100693 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 if (nr)
695 wp->w_popup_flags |= POPF_POSINVERT;
696 else
697 wp->w_popup_flags &= ~POPF_POSINVERT;
698 }
699
Bram Moolenaar55881332020-08-18 13:04:15 +0200700 nr = dict_get_bool(dict, (char_u *)"resize", -1);
701 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200702 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200703 if (nr)
704 wp->w_popup_flags |= POPF_RESIZE;
705 else
706 wp->w_popup_flags &= ~POPF_RESIZE;
707 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200708
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200709 di = dict_find(dict, (char_u *)"close", -1);
710 if (di != NULL)
711 {
712 int ok = TRUE;
713
714 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
715 {
716 char_u *s = di->di_tv.vval.v_string;
717
718 if (STRCMP(s, "none") == 0)
719 wp->w_popup_close = POPCLOSE_NONE;
720 else if (STRCMP(s, "button") == 0)
721 wp->w_popup_close = POPCLOSE_BUTTON;
722 else if (STRCMP(s, "click") == 0)
723 wp->w_popup_close = POPCLOSE_CLICK;
724 else
725 ok = FALSE;
726 }
727 else
728 ok = FALSE;
729 if (!ok)
730 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
731 }
732
Bram Moolenaarae943152019-06-16 22:54:14 +0200733 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
734 if (str != NULL)
735 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
736 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200737
Bram Moolenaarae943152019-06-16 22:54:14 +0200738 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
739 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200740
Bram Moolenaar790498b2019-06-01 22:15:29 +0200741 di = dict_find(dict, (char_u *)"borderhighlight", -1);
742 if (di != NULL)
743 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200744 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200745 emsg(_(e_listreq));
746 else
747 {
748 list_T *list = di->di_tv.vval.v_list;
749 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200750 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200751
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200752 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200753 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
754 ++i, li = li->li_next)
755 {
756 str = tv_get_string(&li->li_tv);
757 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100758 {
759 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100761 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200762 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200763 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
764 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100765 {
766 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100769 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200770 }
771 }
772
Bram Moolenaar790498b2019-06-01 22:15:29 +0200773 di = dict_find(dict, (char_u *)"borderchars", -1);
774 if (di != NULL)
775 {
776 if (di->di_tv.v_type != VAR_LIST)
777 emsg(_(e_listreq));
778 else
779 {
780 list_T *list = di->di_tv.vval.v_list;
781 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200782 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200783
784 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100785 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200786 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200787 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
788 ++i, li = li->li_next)
789 {
790 str = tv_get_string(&li->li_tv);
791 if (*str != NUL)
792 wp->w_border_char[i] = mb_ptr2char(str);
793 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200794 if (list->lv_len == 1)
795 for (i = 1; i < 8; ++i)
796 wp->w_border_char[i] = wp->w_border_char[0];
797 if (list->lv_len == 2)
798 {
799 for (i = 4; i < 8; ++i)
800 wp->w_border_char[i] = wp->w_border_char[1];
801 for (i = 1; i < 4; ++i)
802 wp->w_border_char[i] = wp->w_border_char[0];
803 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200804 }
805 }
806 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200807
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200808 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
809 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
810
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 di = dict_find(dict, (char_u *)"zindex", -1);
812 if (di != NULL)
813 {
814 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
815 if (wp->w_zindex < 1)
816 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
817 if (wp->w_zindex > 32000)
818 wp->w_zindex = 32000;
819 }
820
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200821 di = dict_find(dict, (char_u *)"mask", -1);
822 if (di != NULL)
823 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200824 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200825
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200826 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200827 {
828 listitem_T *li;
829
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200830 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200831 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832 {
833 if (li->li_tv.v_type != VAR_LIST
834 || li->li_tv.vval.v_list == NULL
835 || li->li_tv.vval.v_list->lv_len != 4)
836 {
837 ok = FALSE;
838 break;
839 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100840 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200841 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200842 }
843 }
844 if (ok)
845 {
846 wp->w_popup_mask = di->di_tv.vval.v_list;
847 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200848 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200849 }
850 else
851 semsg(_(e_invargval), "mask");
852 }
853
Bram Moolenaarae943152019-06-16 22:54:14 +0200854#if defined(FEAT_TIMERS)
855 // Add timer to close the popup after some time.
856 nr = dict_get_number(dict, (char_u *)"time");
857 if (nr > 0)
858 popup_add_timeout(wp, nr);
859#endif
860
Bram Moolenaar3397f742019-06-02 18:40:06 +0200861 di = dict_find(dict, (char_u *)"moved", -1);
862 if (di != NULL)
863 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200864 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200865 handle_moved_argument(wp, di, FALSE);
866 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200867
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200868 di = dict_find(dict, (char_u *)"mousemoved", -1);
869 if (di != NULL)
870 {
871 set_mousemoved_values(wp);
872 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200873 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200874
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100875 nr = dict_get_bool(dict, (char_u *)"cursorline", -1);
876 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200877 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100878 if (nr != 0)
879 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200880 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100881 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200882 }
883
Bram Moolenaarae943152019-06-16 22:54:14 +0200884 di = dict_find(dict, (char_u *)"filter", -1);
885 if (di != NULL)
886 {
887 callback_T callback = get_callback(&di->di_tv);
888
889 if (callback.cb_name != NULL)
890 {
891 free_callback(&wp->w_filter_cb);
892 set_callback(&wp->w_filter_cb, &callback);
893 }
894 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200895 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
896 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200897 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200898 if (nr)
899 wp->w_popup_flags |= POPF_MAPPING;
900 else
901 wp->w_popup_flags &= ~POPF_MAPPING;
902 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200903
Bram Moolenaar581ba392019-09-03 22:08:33 +0200904 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
905 if (str != NULL)
906 {
907 if (STRCMP(str, "a") == 0)
908 wp->w_filter_mode = MODE_ALL;
909 else
910 wp->w_filter_mode = mode_str2flags(str);
911 }
912
Bram Moolenaarae943152019-06-16 22:54:14 +0200913 di = dict_find(dict, (char_u *)"callback", -1);
914 if (di != NULL)
915 {
916 callback_T callback = get_callback(&di->di_tv);
917
918 if (callback.cb_name != NULL)
919 {
920 free_callback(&wp->w_close_cb);
921 set_callback(&wp->w_close_cb, &callback);
922 }
923 }
924}
925
926/*
927 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200928 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200929 */
930 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200931apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200932{
933 int nr;
934
935 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200936
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200937 if (create)
938 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200939 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
940
Bram Moolenaarae943152019-06-16 22:54:14 +0200941 apply_general_options(wp, dict);
942
Bram Moolenaar55881332020-08-18 13:04:15 +0200943 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200944 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200945 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200946
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200947 // when "firstline" and "cursorline" are both set and the cursor would be
948 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200949 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
950 {
951 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
952 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200953 else if (wp->w_cursor.lnum < wp->w_firstline
954 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200955 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200956 wp->w_topline = wp->w_firstline;
957 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200958 }
959
Bram Moolenaar33796b32019-06-08 16:01:13 +0200960 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200961 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200962}
963
964/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200965 * Add lines to the popup from a list of strings.
966 */
967 static void
968add_popup_strings(buf_T *buf, list_T *l)
969{
970 listitem_T *li;
971 linenr_T lnum = 0;
972 char_u *p;
973
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200974 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200975 if (li->li_tv.v_type == VAR_STRING)
976 {
977 p = li->li_tv.vval.v_string;
978 ml_append_buf(buf, lnum++,
979 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
980 }
981}
982
983/*
984 * Add lines to the popup from a list of dictionaries.
985 */
986 static void
987add_popup_dicts(buf_T *buf, list_T *l)
988{
989 listitem_T *li;
990 listitem_T *pli;
991 linenr_T lnum = 0;
992 char_u *p;
993 dict_T *dict;
994
995 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200996 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200997 {
998 if (li->li_tv.v_type != VAR_DICT)
999 {
1000 emsg(_(e_dictreq));
1001 return;
1002 }
1003 dict = li->li_tv.vval.v_dict;
1004 p = dict == NULL ? NULL
1005 : dict_get_string(dict, (char_u *)"text", FALSE);
1006 ml_append_buf(buf, lnum++,
1007 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1008 }
1009
1010 // add the text properties
1011 lnum = 1;
1012 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1013 {
1014 dictitem_T *di;
1015 list_T *plist;
1016
1017 dict = li->li_tv.vval.v_dict;
1018 di = dict_find(dict, (char_u *)"props", -1);
1019 if (di != NULL)
1020 {
1021 if (di->di_tv.v_type != VAR_LIST)
1022 {
1023 emsg(_(e_listreq));
1024 return;
1025 }
1026 plist = di->di_tv.vval.v_list;
1027 if (plist != NULL)
1028 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001029 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001030 {
1031 if (pli->li_tv.v_type != VAR_DICT)
1032 {
1033 emsg(_(e_dictreq));
1034 return;
1035 }
1036 dict = pli->li_tv.vval.v_dict;
1037 if (dict != NULL)
1038 {
1039 int col = dict_get_number(dict, (char_u *)"col");
1040
1041 prop_add_common( lnum, col, dict, buf, NULL);
1042 }
1043 }
1044 }
1045 }
1046 }
1047}
1048
1049/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001050 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1051 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001052 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001053popup_top_extra(win_T *wp)
1054{
1055 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1056
1057 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1058 return 1;
1059 return extra;
1060}
1061
1062/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001063 * Get the padding plus border at the left.
1064 */
1065 int
1066popup_left_extra(win_T *wp)
1067{
1068 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1069}
1070
1071/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001072 * Return the height of popup window "wp", including border and padding.
1073 */
1074 int
1075popup_height(win_T *wp)
1076{
1077 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001078 + popup_top_extra(wp)
1079 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001080}
1081
1082/*
1083 * Return the width of popup window "wp", including border, padding and
1084 * scrollbar.
1085 */
1086 int
1087popup_width(win_T *wp)
1088{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001089 // w_leftcol is how many columns of the core are left of the screen
1090 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001091 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001092 + popup_extra_width(wp)
1093 + wp->w_popup_rightoff;
1094}
1095
1096/*
1097 * Return the extra width of popup window "wp": border, padding and scrollbar.
1098 */
1099 int
1100popup_extra_width(win_T *wp)
1101{
1102 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1103 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1104 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001105}
1106
1107/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001108 * Adjust the position and size of the popup to fit on the screen.
1109 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001110 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001111popup_adjust_position(win_T *wp)
1112{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001113 linenr_T lnum;
1114 int wrapped = 0;
1115 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001116 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001117 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001118 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001119 int center_vert = FALSE;
1120 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001121 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001122 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001123 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1124 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1125 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1126 int extra_height = top_extra + bot_extra;
1127 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001128 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001129 int org_winrow = wp->w_winrow;
1130 int org_wincol = wp->w_wincol;
1131 int org_width = wp->w_width;
1132 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001133 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001134 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001135 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001136 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001137 int wantline = wp->w_wantline; // adjusted for textprop
1138 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001139 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001140 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001141
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001142 wp->w_winrow = 0;
1143 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001144 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001145 wp->w_popup_leftoff = 0;
1146 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001147
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001148 // May need to update the "cursorline" highlighting, which may also change
1149 // "topline"
1150 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1151 popup_highlight_curline(wp);
1152
Bram Moolenaar12034e22019-08-25 22:25:02 +02001153 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1154 {
1155 win_T *prop_win = wp->w_popup_prop_win;
1156 textprop_T prop;
1157 linenr_T prop_lnum;
1158 pos_T pos;
1159 int screen_row;
1160 int screen_scol;
1161 int screen_ccol;
1162 int screen_ecol;
1163
1164 // Popup window is positioned relative to a text property.
1165 if (find_visible_prop(prop_win,
1166 wp->w_popup_prop_type, wp->w_popup_prop_id,
1167 &prop, &prop_lnum) == FAIL)
1168 {
1169 // Text property is no longer visible, hide the popup.
1170 // Unhiding the popup is done in check_popup_unhidden().
1171 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1172 {
1173 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001174 if (win_valid(wp->w_popup_prop_win))
1175 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1176 }
1177 return;
1178 }
1179
1180 // Compute the desired position from the position of the text
1181 // property. Use "wantline" and "wantcol" as offsets.
1182 pos.lnum = prop_lnum;
1183 pos.col = prop.tp_col;
1184 if (wp->w_popup_pos == POPPOS_TOPLEFT
1185 || wp->w_popup_pos == POPPOS_BOTLEFT)
1186 pos.col += prop.tp_len - 1;
1187 textpos2screenpos(prop_win, &pos, &screen_row,
1188 &screen_scol, &screen_ccol, &screen_ecol);
1189
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001190 if (screen_scol == 0)
1191 {
1192 // position is off screen, make the width zero to hide it.
1193 wp->w_width = 0;
1194 return;
1195 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001196 if (wp->w_popup_pos == POPPOS_TOPLEFT
1197 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1198 // below the text
1199 wantline = screen_row + wantline + 1;
1200 else
1201 // above the text
1202 wantline = screen_row + wantline - 1;
1203 center_vert = FALSE;
1204 if (wp->w_popup_pos == POPPOS_TOPLEFT
1205 || wp->w_popup_pos == POPPOS_BOTLEFT)
1206 // right of the text
1207 wantcol = screen_ecol + wantcol;
1208 else
1209 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001210 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001211 use_wantcol = TRUE;
1212 }
1213 else
1214 {
1215 // If no line was specified default to vertical centering.
1216 if (wantline == 0)
1217 center_vert = TRUE;
1218 else if (wantline < 0)
1219 // If "wantline" is negative it actually means zero.
1220 wantline = 0;
1221 if (wantcol < 0)
1222 // If "wantcol" is negative it actually means zero.
1223 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001224 }
1225
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001226 if (wp->w_popup_pos == POPPOS_CENTER)
1227 {
1228 // center after computing the size
1229 center_vert = TRUE;
1230 center_hor = TRUE;
1231 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001232 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001233 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001234 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1235 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001236 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001237 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001238 if (wp->w_winrow >= Rows)
1239 wp->w_winrow = Rows - 1;
1240 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001241
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001242 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001243 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001244 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1245 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001246 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001247 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001248 // Need to see at least one character after the decoration.
1249 if (wp->w_wincol > Columns - left_extra - 1)
1250 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001251 }
1252 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001253
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001254 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001255 // When left aligned use the space available, but shift to the left when we
1256 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001257 maxspace = Columns - wp->w_wincol - left_extra;
1258 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001259 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001260 {
1261 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001263 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001264
1265 if (wp->w_p_nu || wp->w_p_rnu)
1266 margin_width = number_width(wp) + 1;
1267#ifdef FEAT_FOLDING
1268 margin_width += wp->w_p_fdc;
1269#endif
1270#ifdef FEAT_SIGNS
1271 if (signcolumn_on(wp))
1272 margin_width += 2;
1273#endif
1274 if (margin_width >= maxwidth)
1275 margin_width = maxwidth - 1;
1276
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001277 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001278 minheight = wp->w_minheight;
1279#ifdef FEAT_TERMINAL
1280 // A terminal popup initially does not have content, use a default minimal
1281 // width of 20 characters and height of 5 lines.
1282 if (wp->w_buffer->b_term != NULL)
1283 {
1284 if (minwidth == 0)
1285 minwidth = 20;
1286 if (minheight == 0)
1287 minheight = 5;
1288 }
1289#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001290
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001291 if (wp->w_maxheight > 0)
1292 maxheight = wp->w_maxheight;
1293
Bram Moolenaar8d241042019-06-12 23:40:01 +02001294 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001295 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001296 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001297 if (wp->w_topline < 1)
1298 wp->w_topline = 1;
1299 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001300 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1301
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001302 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001303 // Use a minimum width of one, so that something shows when there is no
1304 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001305 // When "firstline" is -1 then start with the last buffer line and go
1306 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001307 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001308 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001309 if (wp->w_firstline < 0)
1310 lnum = wp->w_buffer->b_ml.ml_line_count;
1311 else
1312 lnum = wp->w_topline;
1313 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001314 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001315 int len;
1316 int w_width = wp->w_width;
1317
1318 // Count Tabs for what they are worth and compute the length based on
1319 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001320 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001321 if (wp->w_width < maxwidth)
1322 wp->w_width = maxwidth;
1323 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001324 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001325 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001326
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001327 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001329 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001330 {
1331 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001332 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001333 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001334 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001335 }
1336 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001337 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 && allow_adjust_left
1339 && (wp->w_popup_pos == POPPOS_TOPLEFT
1340 || wp->w_popup_pos == POPPOS_BOTLEFT))
1341 {
1342 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001343 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001344
Bram Moolenaar51c31312019-06-15 22:27:23 +02001345 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001346 {
1347 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001348
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001349 len -= truncate_shift;
1350 shift_by -= truncate_shift;
1351 }
1352
1353 wp->w_wincol -= shift_by;
1354 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001355 wp->w_width = maxwidth;
1356 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001357 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001358 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001359 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001360 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1361 wp->w_width = wp->w_maxwidth;
1362 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001363
1364 if (wp->w_firstline < 0)
1365 --lnum;
1366 else
1367 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001368
1369 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001370 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001371 && (wp->w_firstline >= 0
1372 ? lnum - wp->w_topline
1373 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001374 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001375 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001376 }
1377
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001378 if (wp->w_firstline < 0)
1379 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1380
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001381 wp->w_has_scrollbar = wp->w_want_scrollbar
1382 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001383#ifdef FEAT_TERMINAL
1384 if (wp->w_buffer->b_term != NULL)
1385 // Terminal window never has a scrollbar, adjusts to window height.
1386 wp->w_has_scrollbar = FALSE;
1387#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001388 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001389 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001390 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001391 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001392 // make space for the scrollbar if needed, when lines wrap and when
1393 // applying minwidth
1394 if (maxwidth + right_extra >= maxspace
1395 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1396 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001397 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001398
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001399 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1400 {
1401 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1402
1403 if (minwidth < title_len)
1404 minwidth = title_len;
1405 }
1406
1407 if (minwidth > 0 && wp->w_width < minwidth)
1408 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001409 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001410 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001411 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001412 // some columns cut off on the right
1413 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001414 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001415 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001416 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001417 {
1418 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1419 if (wp->w_wincol < 0)
1420 wp->w_wincol = 0;
1421 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001422 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1423 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1424 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001425 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001426
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001427 // Right aligned: move to the right if needed.
1428 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001429 if (leftoff >= 0)
1430 wp->w_wincol = leftoff;
1431 else if (wp->w_popup_fixed)
1432 {
1433 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001434 // columns when displaying the window, minus left border and
1435 // padding.
1436 if (-leftoff > left_extra)
1437 wp->w_leftcol = -leftoff - left_extra;
1438 wp->w_width -= wp->w_leftcol;
1439 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001440 if (wp->w_width < 0)
1441 wp->w_width = 0;
1442 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001443 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001444
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001445 if (wp->w_p_wrap || (!wp->w_popup_fixed
1446 && (wp->w_popup_pos == POPPOS_TOPLEFT
1447 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001448 {
1449 int want_col = 0;
1450
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001451 // try to show the right border and any scrollbar
1452 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001453 if (want_col > 0 && wp->w_wincol > 0
1454 && wp->w_wincol + want_col >= Columns)
1455 {
1456 wp->w_wincol = Columns - want_col;
1457 if (wp->w_wincol < 0)
1458 wp->w_wincol = 0;
1459 }
1460 }
1461
Bram Moolenaar8d241042019-06-12 23:40:01 +02001462 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1463 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001464 if (minheight > 0 && wp->w_height < minheight)
1465 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001466 if (maxheight > 0 && wp->w_height > maxheight)
1467 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001468 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001469 if (wp->w_height > Rows - wp->w_winrow)
1470 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001471
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001472 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001473 {
1474 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1475 if (wp->w_winrow < 0)
1476 wp->w_winrow = 0;
1477 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001478 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001479 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001480 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001481 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001482 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001483 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001484 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1485 {
1486 // Bottom aligned but does not fit, and less space on the other
1487 // side or "posinvert" is off: reduce height.
1488 wp->w_winrow = 0;
1489 wp->w_height = wantline - extra_height;
1490 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001491 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001492 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001493 // Not enough space and more space on the other side: make top
1494 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001495 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001496 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001497 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001498 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001499 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1500 || wp->w_popup_pos == POPPOS_TOPLEFT)
1501 {
1502 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1503 && wantline * 2 > Rows
1504 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001505 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001506 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001507 // make bottom aligned and recompute the height
1508 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001509 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001510 if (wp->w_winrow < 0)
1511 {
1512 wp->w_height += wp->w_winrow;
1513 wp->w_winrow = 0;
1514 }
1515 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001516 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001517 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001518 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001519 adjust_height_for_top_aligned = TRUE;
1520 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001521 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001522
1523 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1524 && wp->w_winrow + wp->w_height + extra_height > Rows)
1525 {
1526 // Bottom of the popup goes below the last line, reduce the height and
1527 // add a scrollbar.
1528 wp->w_height = Rows - wp->w_winrow - extra_height;
1529#ifdef FEAT_TERMINAL
1530 if (wp->w_buffer->b_term == NULL)
1531#endif
1532 wp->w_has_scrollbar = TRUE;
1533 }
1534
1535 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001536 if (wp->w_winrow >= Rows)
1537 wp->w_winrow = Rows - 1;
1538 else if (wp->w_winrow < 0)
1539 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001540
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001541 if (wp->w_height != org_height)
1542 win_comp_scroll(wp);
1543
Bram Moolenaar17146962019-05-30 00:12:11 +02001544 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001545 if (win_valid(wp->w_popup_prop_win))
1546 {
1547 wp->w_popup_prop_changedtick =
1548 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1549 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1550 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001551
1552 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001553 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001554 if (org_winrow != wp->w_winrow
1555 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001556 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001557 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001558 || org_width != wp->w_width
1559 || org_height != wp->w_height)
1560 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001561 redraw_win_later(wp, NOT_VALID);
1562 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1563 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001564 popup_mask_refresh = TRUE;
1565 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001566}
1567
Bram Moolenaar17627312019-06-02 19:53:44 +02001568typedef enum
1569{
1570 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001571 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001572 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001573 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001574 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001575 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001576 TYPE_PREVIEW, // preview window
1577 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001578} create_type_T;
1579
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001580/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001581 * Make "buf" empty and set the contents to "text".
1582 * Used by popup_create() and popup_settext().
1583 */
1584 static void
1585popup_set_buffer_text(buf_T *buf, typval_T text)
1586{
1587 int lnum;
1588
1589 // Clear the buffer, then replace the lines.
1590 curbuf = buf;
1591 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001592 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001593 curbuf = curwin->w_buffer;
1594
1595 // Add text to the buffer.
1596 if (text.v_type == VAR_STRING)
1597 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001598 char_u *s = text.vval.v_string;
1599
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001600 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001601 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001602 }
1603 else
1604 {
1605 list_T *l = text.vval.v_list;
1606
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001607 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001608 {
1609 if (l->lv_first->li_tv.v_type == VAR_STRING)
1610 // list of strings
1611 add_popup_strings(buf, l);
1612 else
1613 // list of dictionaries
1614 add_popup_dicts(buf, l);
1615 }
1616 }
1617
1618 // delete the line that was in the empty buffer
1619 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001620 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001621 curbuf = curwin->w_buffer;
1622}
1623
1624/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001625 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1626 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001627 * Return FAIL if the parsing fails.
1628 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001629 static int
1630parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001631{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001632 char_u *p =
1633#ifdef FEAT_QUICKFIX
1634 !is_preview ? p_cpp :
1635#endif
1636 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001637
Bram Moolenaar258cef52019-08-21 17:29:29 +02001638 if (wp != NULL)
1639 wp->w_popup_flags &= ~POPF_INFO_MENU;
1640
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001641 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001642 {
1643 char_u *e, *dig;
1644 char_u *s = p;
1645 int x;
1646
1647 e = vim_strchr(p, ':');
1648 if (e == NULL || e[1] == NUL)
1649 return FAIL;
1650
1651 p = vim_strchr(e, ',');
1652 if (p == NULL)
1653 p = e + STRLEN(e);
1654 dig = e + 1;
1655 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001656
1657 if (STRNCMP(s, "height:", 7) == 0)
1658 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001659 if (dig != p)
1660 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001661 if (wp != NULL)
1662 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001663 if (is_preview)
1664 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001665 wp->w_maxheight = x;
1666 }
1667 }
1668 else if (STRNCMP(s, "width:", 6) == 0)
1669 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001670 if (dig != p)
1671 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001672 if (wp != NULL)
1673 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001674 if (is_preview)
1675 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001676 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001677 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001678 }
1679 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001680 else if (STRNCMP(s, "highlight:", 10) == 0)
1681 {
1682 if (wp != NULL)
1683 {
1684 int c = *p;
1685
1686 *p = NUL;
1687 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1688 s + 10, OPT_FREE|OPT_LOCAL, 0);
1689 *p = c;
1690 }
1691 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001692 else if (STRNCMP(s, "border:", 7) == 0)
1693 {
1694 char_u *arg = s + 7;
1695 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1696 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1697 int i;
1698
1699 if (!on && !off)
1700 return FAIL;
1701 if (wp != NULL)
1702 {
1703 for (i = 0; i < 4; ++i)
1704 wp->w_popup_border[i] = on ? 1 : 0;
1705 if (off)
1706 // only show the X for close when there is a border
1707 wp->w_popup_close = POPCLOSE_NONE;
1708 }
1709 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001710 else if (STRNCMP(s, "align:", 6) == 0)
1711 {
1712 char_u *arg = s + 6;
1713 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1714 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1715
1716 if (!menu && !item)
1717 return FAIL;
1718 if (wp != NULL && menu)
1719 wp->w_popup_flags |= POPF_INFO_MENU;
1720 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001721 else
1722 return FAIL;
1723 }
1724 return OK;
1725}
1726
1727/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001728 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1729 * is not NULL.
1730 * Return FAIL if the parsing fails.
1731 */
1732 int
1733parse_previewpopup(win_T *wp)
1734{
1735 return parse_popup_option(wp, TRUE);
1736}
1737
1738/*
1739 * Parse the 'completepopup' option and apply the values to window "wp" if it
1740 * is not NULL.
1741 * Return FAIL if the parsing fails.
1742 */
1743 int
1744parse_completepopup(win_T *wp)
1745{
1746 return parse_popup_option(wp, FALSE);
1747}
1748
1749/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001750 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001751 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001752 */
1753 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001754popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001755{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001756 poppos_T ppt = POPPOS_NONE;
1757
1758 if (d != NULL)
1759 ppt = get_pos_entry(d, FALSE);
1760
Bram Moolenaar79648732019-07-18 21:43:07 +02001761 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001762 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001763 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001764 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1765 }
1766 else
1767 {
1768 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1769 if (wp->w_wantline == 0) // cursor in first line
1770 {
1771 wp->w_wantline = 2;
1772 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1773 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1774 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001775 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001776
Bram Moolenaar79648732019-07-18 21:43:07 +02001777 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001778 if (wp->w_wantcol > Columns - width)
1779 {
1780 wp->w_wantcol = Columns - width;
1781 if (wp->w_wantcol < 1)
1782 wp->w_wantcol = 1;
1783 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001784
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 popup_adjust_position(wp);
1786}
1787
1788/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001789 * Set w_wantline and w_wantcol for the a given screen position.
1790 * Caller must take care of running into the window border.
1791 */
1792 void
1793popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1794{
1795 wp->w_wantline = row;
1796 wp->w_wantcol = col;
1797 popup_adjust_position(wp);
1798}
1799
1800/*
1801 * Add a border and lef&right padding.
1802 */
1803 static void
1804add_border_left_right_padding(win_T *wp)
1805{
1806 int i;
1807
1808 for (i = 0; i < 4; ++i)
1809 {
1810 wp->w_popup_border[i] = 1;
1811 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1812 }
1813}
1814
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001815#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001816/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001817 * Return TRUE if there is any popup window with a terminal buffer.
1818 */
1819 static int
1820popup_terminal_exists(void)
1821{
1822 win_T *wp;
1823 tabpage_T *tp;
1824
1825 FOR_ALL_POPUPWINS(wp)
1826 if (wp->w_buffer->b_term != NULL)
1827 return TRUE;
1828 FOR_ALL_TABPAGES(tp)
1829 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1830 if (wp->w_buffer->b_term != NULL)
1831 return TRUE;
1832 return FALSE;
1833}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001834#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001835
1836/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001837 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001838 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001839 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001840 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001841 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001842 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001843popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001844{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001845 win_T *wp;
1846 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001847 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848 int new_buffer;
1849 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001850 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001851 int nr;
1852 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001853
Bram Moolenaar79648732019-07-18 21:43:07 +02001854 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001855 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001856 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001857 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001858 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001859 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001860 if (buf == NULL)
1861 {
1862 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1863 return NULL;
1864 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001865#ifdef FEAT_TERMINAL
1866 if (buf->b_term != NULL && popup_terminal_exists())
1867 {
1868 emsg(_("E861: Cannot open a second popup with a terminal"));
1869 return NULL;
1870 }
1871#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001872 }
1873 else if (!(argvars[0].v_type == VAR_STRING
1874 && argvars[0].vval.v_string != NULL)
1875 && !(argvars[0].v_type == VAR_LIST
1876 && argvars[0].vval.v_list != NULL))
1877 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001878 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001879 return NULL;
1880 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001881 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001882 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001883 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001884 return NULL;
1885 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001886 d = argvars[1].vval.v_dict;
1887 }
1888
1889 if (d != NULL)
1890 {
1891 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1892 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1893 else if (type == TYPE_NOTIFICATION)
1894 tabnr = -1; // notifications are global by default
1895 else
1896 tabnr = 0;
1897 if (tabnr > 0)
1898 {
1899 tp = find_tabpage(tabnr);
1900 if (tp == NULL)
1901 {
1902 semsg(_("E997: Tabpage not found: %d"), tabnr);
1903 return NULL;
1904 }
1905 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001906 }
1907
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001908 // Create the window and buffer.
1909 wp = win_alloc_popup_win();
1910 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001911 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001912 if (rettv != NULL)
1913 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001914 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001915 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001916
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001917 if (buf != NULL)
1918 {
1919 // use existing buffer
1920 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001921 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001922 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001923 buffer_ensure_loaded(buf);
1924 }
1925 else
1926 {
1927 // create a new buffer associated with the popup
1928 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001929 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001930 if (buf == NULL)
1931 return NULL;
1932 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001933
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001934 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001935
Bram Moolenaar46451042019-08-24 15:50:46 +02001936 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001937 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001938 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001939 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001940 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001941 buf->b_p_ul = -1; // no undo
1942 buf->b_p_swf = FALSE; // no swap file
1943 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001944 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001945
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001946 // Avoid that 'buftype' is reset when this buffer is entered.
1947 buf->b_p_initialized = TRUE;
1948 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001949 wp->w_p_wrap = TRUE; // 'wrap' is default on
1950 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001951
Bram Moolenaara3fce622019-06-20 02:31:49 +02001952 if (tp != NULL)
1953 {
1954 // popup on specified tab page
1955 wp->w_next = tp->tp_first_popupwin;
1956 tp->tp_first_popupwin = wp;
1957 }
1958 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001959 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001960 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001961 wp->w_next = curtab->tp_first_popupwin;
1962 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001963 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001964 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001965 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001966 win_T *prev = first_popupwin;
1967
1968 // Global popup: add at the end, so that it gets displayed on top of
1969 // older ones with the same zindex. Matters for notifications.
1970 if (first_popupwin == NULL)
1971 first_popupwin = wp;
1972 else
1973 {
1974 while (prev->w_next != NULL)
1975 prev = prev->w_next;
1976 prev->w_next = wp;
1977 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001978 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001979
Bram Moolenaar79648732019-07-18 21:43:07 +02001980 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001981 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001982
Bram Moolenaar79648732019-07-18 21:43:07 +02001983 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001984 {
1985 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001986 }
1987 if (type == TYPE_ATCURSOR)
1988 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001989 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001990 set_moved_values(wp);
1991 set_moved_columns(wp, FIND_STRING);
1992 }
1993
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001994 if (type == TYPE_BEVAL)
1995 {
1996 wp->w_popup_pos = POPPOS_BOTLEFT;
1997
1998 // by default use the mouse position
1999 wp->w_wantline = mouse_row;
2000 if (wp->w_wantline <= 0) // mouse on first line
2001 {
2002 wp->w_wantline = 2;
2003 wp->w_popup_pos = POPPOS_TOPLEFT;
2004 }
2005 wp->w_wantcol = mouse_col + 1;
2006 set_mousemoved_values(wp);
2007 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2008 }
2009
Bram Moolenaar33796b32019-06-08 16:01:13 +02002010 // set default values
2011 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002012 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002013
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002014 if (type == TYPE_NOTIFICATION)
2015 {
2016 win_T *twp, *nextwin;
2017 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002018
2019 // Try to not overlap with another global popup. Guess we need 3
2020 // more screen lines than buffer lines.
2021 wp->w_wantline = 1;
2022 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2023 {
2024 nextwin = twp->w_next;
2025 if (twp != wp
2026 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2027 && twp->w_winrow <= wp->w_wantline - 1 + height
2028 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2029 {
2030 // move to below this popup and restart the loop to check for
2031 // overlap with other popups
2032 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2033 nextwin = first_popupwin;
2034 }
2035 }
2036 if (wp->w_wantline + height > Rows)
2037 {
2038 // can't avoid overlap, put on top in the hope that message goes
2039 // away soon.
2040 wp->w_wantline = 1;
2041 }
2042
2043 wp->w_wantcol = 10;
2044 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002045 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002046 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002047 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002048 for (i = 0; i < 4; ++i)
2049 wp->w_popup_border[i] = 1;
2050 wp->w_popup_padding[1] = 1;
2051 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002052
2053 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002054 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002055 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2056 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002057 }
2058
Bram Moolenaara730e552019-06-16 19:05:31 +02002059 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002060 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002061 wp->w_popup_pos = POPPOS_CENTER;
2062 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002063 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002064 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002065 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002066 }
2067
Bram Moolenaara730e552019-06-16 19:05:31 +02002068 if (type == TYPE_MENU)
2069 {
2070 typval_T tv;
2071 callback_T callback;
2072
2073 tv.v_type = VAR_STRING;
2074 tv.vval.v_string = (char_u *)"popup_filter_menu";
2075 callback = get_callback(&tv);
2076 if (callback.cb_name != NULL)
2077 set_callback(&wp->w_filter_cb, &callback);
2078
2079 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002080 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002081 }
2082
Bram Moolenaar79648732019-07-18 21:43:07 +02002083 if (type == TYPE_PREVIEW)
2084 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002085 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002086 wp->w_popup_close = POPCLOSE_BUTTON;
2087 for (i = 0; i < 4; ++i)
2088 wp->w_popup_border[i] = 1;
2089 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002090 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002091 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002092# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002093 if (type == TYPE_INFO)
2094 {
2095 wp->w_popup_pos = POPPOS_TOPLEFT;
2096 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2097 wp->w_popup_close = POPCLOSE_BUTTON;
2098 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002099 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002100 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002101# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002102
Bram Moolenaarae943152019-06-16 22:54:14 +02002103 for (i = 0; i < 4; ++i)
2104 VIM_CLEAR(wp->w_border_highlight[i]);
2105 for (i = 0; i < 8; ++i)
2106 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002107 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002108 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002109 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002110
Bram Moolenaar79648732019-07-18 21:43:07 +02002111 if (d != NULL)
2112 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002113 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002114
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002115#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002116 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2117 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002118#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002119
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002120 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121
2122 wp->w_vsep_width = 0;
2123
2124 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002125 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002126
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002127#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002128 // When running a terminal in the popup it becomes the current window.
2129 if (buf->b_term != NULL)
2130 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002131#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002132
Bram Moolenaara730e552019-06-16 19:05:31 +02002133 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002134}
2135
2136/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002137 * popup_clear()
2138 */
2139 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002140f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002141{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002142 int force = FALSE;
2143
2144 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002145 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002146 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002147}
2148
2149/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002150 * popup_create({text}, {options})
2151 */
2152 void
2153f_popup_create(typval_T *argvars, typval_T *rettv)
2154{
Bram Moolenaar17627312019-06-02 19:53:44 +02002155 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002156}
2157
2158/*
2159 * popup_atcursor({text}, {options})
2160 */
2161 void
2162f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2163{
Bram Moolenaar17627312019-06-02 19:53:44 +02002164 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002165}
2166
2167/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002168 * popup_beval({text}, {options})
2169 */
2170 void
2171f_popup_beval(typval_T *argvars, typval_T *rettv)
2172{
2173 popup_create(argvars, rettv, TYPE_BEVAL);
2174}
2175
2176/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002177 * Invoke the close callback for window "wp" with value "result".
2178 * Careful: The callback may make "wp" invalid!
2179 */
2180 static void
2181invoke_popup_callback(win_T *wp, typval_T *result)
2182{
2183 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002184 typval_T argv[3];
2185
2186 argv[0].v_type = VAR_NUMBER;
2187 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2188
2189 if (result != NULL && result->v_type != VAR_UNKNOWN)
2190 copy_tv(result, &argv[1]);
2191 else
2192 {
2193 argv[1].v_type = VAR_NUMBER;
2194 argv[1].vval.v_number = 0;
2195 }
2196
2197 argv[2].v_type = VAR_UNKNOWN;
2198
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002199 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002200 if (result != NULL)
2201 clear_tv(&argv[1]);
2202 clear_tv(&rettv);
2203}
2204
2205/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002206 * Make "prevwin" the current window, unless it's equal to "wp".
2207 * Otherwise make "firstwin" the current window.
2208 */
2209 static void
2210back_to_prevwin(win_T *wp)
2211{
2212 if (win_valid(prevwin) && wp != prevwin)
2213 win_enter(prevwin, FALSE);
2214 else
2215 win_enter(firstwin, FALSE);
2216}
2217
2218/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002219 * Close popup "wp" and invoke any close callback for it.
2220 */
2221 static void
2222popup_close_and_callback(win_T *wp, typval_T *arg)
2223{
2224 int id = wp->w_id;
2225
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002226#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002227 if (wp == curwin && curbuf->b_term != NULL)
2228 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002229 win_T *owp;
2230
2231 // Closing popup window with a terminal: put focus back on the first
2232 // that works:
2233 // - another popup window with a terminal
2234 // - the previous window
2235 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002236 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002237 if (owp != curwin && owp->w_buffer->b_term != NULL)
2238 break;
2239 if (owp != NULL)
2240 win_enter(owp, FALSE);
2241 else
2242 {
2243 for (owp = curtab->tp_first_popupwin; owp != NULL;
2244 owp = owp->w_next)
2245 if (owp != curwin && owp->w_buffer->b_term != NULL)
2246 break;
2247 if (owp != NULL)
2248 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002249 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002250 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002251 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002252 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002253#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002254
Bram Moolenaar49540192019-12-11 19:34:54 +01002255 // Just in case a check higher up is missing.
2256 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002257 {
2258 // To avoid getting stuck when win_execute() does something that causes
2259 // an error, stop calling the filter callback.
2260 free_callback(&wp->w_filter_cb);
2261
Bram Moolenaar49540192019-12-11 19:34:54 +01002262 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002263 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002264
Bram Moolenaarcee52202020-03-11 14:19:58 +01002265 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002266 if (wp->w_close_cb.cb_name != NULL)
2267 // Careful: This may make "wp" invalid.
2268 invoke_popup_callback(wp, arg);
2269
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002270 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002271 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002272}
2273
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002274 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002275popup_close_with_retval(win_T *wp, int retval)
2276{
2277 typval_T res;
2278
2279 res.v_type = VAR_NUMBER;
2280 res.vval.v_number = retval;
2281 popup_close_and_callback(wp, &res);
2282}
2283
Bram Moolenaar3397f742019-06-02 18:40:06 +02002284/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002285 * Close popup "wp" because of a mouse click.
2286 */
2287 void
2288popup_close_for_mouse_click(win_T *wp)
2289{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002290 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002291}
2292
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002293 static void
2294check_mouse_moved(win_T *wp, win_T *mouse_wp)
2295{
2296 // Close the popup when all if these are true:
2297 // - the mouse is not on this popup
2298 // - "mousemoved" was used
2299 // - the mouse is no longer on the same screen row or the mouse column is
2300 // outside of the relevant text
2301 if (wp != mouse_wp
2302 && wp->w_popup_mouse_row != 0
2303 && (wp->w_popup_mouse_row != mouse_row
2304 || mouse_col < wp->w_popup_mouse_mincol
2305 || mouse_col > wp->w_popup_mouse_maxcol))
2306 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002307 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002308 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002309 }
2310}
2311
2312/*
2313 * Called when the mouse moved: may close a popup with "mousemoved".
2314 */
2315 void
2316popup_handle_mouse_moved(void)
2317{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002318 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002319 win_T *mouse_wp;
2320 int row = mouse_row;
2321 int col = mouse_col;
2322
2323 // find the window where the mouse is in
2324 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2325
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002326 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2327 {
2328 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002329 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002330 }
2331 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2332 {
2333 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002334 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002335 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002336}
2337
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002338/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002339 * In a filter: check if the typed key is a mouse event that is used for
2340 * dragging the popup.
2341 */
2342 static void
2343filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2344{
2345 int row = mouse_row;
2346 int col = mouse_col;
2347
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002348 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002349 && is_mouse_key(c)
2350 && (wp == popup_dragwin
2351 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2352 // do not consume the key, allow for dragging the popup
2353 rettv->vval.v_number = 0;
2354}
2355
Bram Moolenaara730e552019-06-16 19:05:31 +02002356/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002357 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002358 */
2359 void
2360f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2361{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002362 int id;
2363 win_T *wp;
2364 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002365 typval_T res;
2366 int c;
2367 linenr_T old_lnum;
2368
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002369 if (in_vim9script()
2370 && (check_for_number_arg(argvars, 0) == FAIL
2371 || check_for_string_arg(argvars, 1) == FAIL))
2372 return;
2373
2374 id = tv_get_number(&argvars[0]);
2375 wp = win_id2wp(id);
2376 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002377 // If the popup has been closed do not consume the key.
2378 if (wp == NULL)
2379 return;
2380
2381 c = *key;
2382 if (c == K_SPECIAL && key[1] != NUL)
2383 c = TO_SPECIAL(key[1], key[2]);
2384
2385 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002386 rettv->v_type = VAR_BOOL;
2387 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002388 res.v_type = VAR_NUMBER;
2389
2390 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002391 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2392 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002393 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002394 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002395 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2396 ++wp->w_cursor.lnum;
2397 if (old_lnum != wp->w_cursor.lnum)
2398 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002399 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002400 return;
2401 }
2402
2403 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2404 {
2405 // Cancelled, invoke callback with -1
2406 res.vval.v_number = -1;
2407 popup_close_and_callback(wp, &res);
2408 return;
2409 }
2410 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2411 {
2412 // Invoke callback with current index.
2413 res.vval.v_number = wp->w_cursor.lnum;
2414 popup_close_and_callback(wp, &res);
2415 return;
2416 }
2417
2418 filter_handle_drag(wp, c, rettv);
2419}
2420
2421/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002422 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002423 */
2424 void
2425f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2426{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002427 int id;
2428 win_T *wp;
2429 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002430 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002431 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002432
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002433 if (in_vim9script()
2434 && (check_for_number_arg(argvars, 0) == FAIL
2435 || check_for_string_arg(argvars, 1) == FAIL))
2436 return;
2437
2438 id = tv_get_number(&argvars[0]);
2439 wp = win_id2wp(id);
2440 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002441 // If the popup has been closed don't consume the key.
2442 if (wp == NULL)
2443 return;
2444
Bram Moolenaara730e552019-06-16 19:05:31 +02002445 c = *key;
2446 if (c == K_SPECIAL && key[1] != NUL)
2447 c = TO_SPECIAL(key[1], key[2]);
2448
Bram Moolenaara42d9452019-06-15 21:46:30 +02002449 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002450 rettv->v_type = VAR_BOOL;
2451 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002452
Bram Moolenaara730e552019-06-16 19:05:31 +02002453 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002454 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002455 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002456 res.vval.v_number = 0;
2457 else
2458 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002459 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002460 return;
2461 }
2462
2463 // Invoke callback
2464 res.v_type = VAR_NUMBER;
2465 popup_close_and_callback(wp, &res);
2466}
2467
2468/*
2469 * popup_dialog({text}, {options})
2470 */
2471 void
2472f_popup_dialog(typval_T *argvars, typval_T *rettv)
2473{
2474 popup_create(argvars, rettv, TYPE_DIALOG);
2475}
2476
2477/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002478 * popup_menu({text}, {options})
2479 */
2480 void
2481f_popup_menu(typval_T *argvars, typval_T *rettv)
2482{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002483 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002484}
2485
2486/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002487 * popup_notification({text}, {options})
2488 */
2489 void
2490f_popup_notification(typval_T *argvars, typval_T *rettv)
2491{
2492 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2493}
2494
2495/*
2496 * Find the popup window with window-ID "id".
2497 * If the popup window does not exist NULL is returned.
2498 * If the window is not a popup window, and error message is given.
2499 */
2500 static win_T *
2501find_popup_win(int id)
2502{
2503 win_T *wp = win_id2wp(id);
2504
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002505 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002506 {
2507 semsg(_("E993: window %d is not a popup window"), id);
2508 return NULL;
2509 }
2510 return wp;
2511}
2512
2513/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002514 * popup_close({id})
2515 */
2516 void
2517f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2518{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002519 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002520 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002521
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002522 if (
2523# ifdef FEAT_TERMINAL
2524 // if the popup contains a terminal it will become hidden
2525 curbuf->b_term == NULL &&
2526# endif
2527 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002528 return;
2529
2530 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002531 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002532 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002533}
2534
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002535 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002536popup_hide(win_T *wp)
2537{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002538#ifdef FEAT_TERMINAL
2539 if (error_if_term_popup_window())
2540 return;
2541#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002542 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2543 {
2544 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002545 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002546 redraw_all_later(NOT_VALID);
2547 popup_mask_refresh = TRUE;
2548 }
2549}
2550
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002551/*
2552 * popup_hide({id})
2553 */
2554 void
2555f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2556{
2557 int id = (int)tv_get_number(argvars);
2558 win_T *wp = find_popup_win(id);
2559
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002560 if (wp != NULL)
2561 popup_hide(wp);
2562}
2563
2564 void
2565popup_show(win_T *wp)
2566{
2567 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002568 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002569 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002570 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002571 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002572 }
2573}
2574
2575/*
2576 * popup_show({id})
2577 */
2578 void
2579f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2580{
2581 int id = (int)tv_get_number(argvars);
2582 win_T *wp = find_popup_win(id);
2583
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002584 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002585 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002586 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002587#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002588 if (wp->w_popup_flags & POPF_INFO)
2589 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002590#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002591 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002592}
2593
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002594/*
2595 * popup_settext({id}, {text})
2596 */
2597 void
2598f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2599{
2600 int id = (int)tv_get_number(&argvars[0]);
2601 win_T *wp = find_popup_win(id);
2602
2603 if (wp != NULL)
2604 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002605 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2606 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2607 else
2608 {
2609 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002610 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002611 popup_adjust_position(wp);
2612 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002613 }
2614}
2615
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002616 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002617popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002618{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002619 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002620 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002621 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002622 clear_cmdline = TRUE;
2623 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002624
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002625 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002626 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002627}
2628
Bram Moolenaar82106932020-03-13 14:34:38 +01002629 static void
2630error_for_popup_window(void)
2631{
2632 emsg(_("E994: Not allowed in a popup window"));
2633}
2634
2635 int
2636error_if_popup_window(int also_with_term UNUSED)
2637{
2638 // win_execute() may set "curwin" to a popup window temporarily, but many
2639 // commands are disallowed then. When a terminal runs in the popup most
2640 // things are allowed. When a terminal is finished it can be closed.
2641 if (WIN_IS_POPUP(curwin)
2642# ifdef FEAT_TERMINAL
2643 && (also_with_term || curbuf->b_term == NULL)
2644 && !term_is_finished(curbuf)
2645# endif
2646 )
2647 {
2648 error_for_popup_window();
2649 return TRUE;
2650 }
2651 return FALSE;
2652}
2653
Bram Moolenaarec583842019-05-26 14:11:23 +02002654/*
2655 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002656 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002657 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002658 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002659 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002660popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002661{
2662 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002663 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002664 win_T *prev = NULL;
2665
Bram Moolenaarec583842019-05-26 14:11:23 +02002666 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002667 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002668 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002669 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002670 if (wp == curwin)
2671 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002672 if (!force)
2673 {
2674 error_for_popup_window();
2675 return FAIL;
2676 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002677 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002678 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002679 if (prev == NULL)
2680 first_popupwin = wp->w_next;
2681 else
2682 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002683 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002684 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002685 }
2686
Bram Moolenaarec583842019-05-26 14:11:23 +02002687 // go through tab-local popups
2688 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002689 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002690 return OK;
2691 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002692}
2693
2694/*
2695 * Close a popup window with Window-id "id" in tabpage "tp".
2696 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002697 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002698popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002699{
2700 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002701 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002702 win_T *prev = NULL;
2703
Bram Moolenaarec583842019-05-26 14:11:23 +02002704 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2705 if (wp->w_id == id)
2706 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002707 if (wp == curwin)
2708 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002709 if (!force)
2710 {
2711 error_for_popup_window();
2712 return FAIL;
2713 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002714 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002715 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002716 if (prev == NULL)
2717 *root = wp->w_next;
2718 else
2719 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002720 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002721 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002722 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002723 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002724}
2725
2726 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002727close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002728{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002729 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002730 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002731 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002732 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002733 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002734 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002735 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002736 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002737}
2738
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002739/*
2740 * popup_move({id}, {options})
2741 */
2742 void
2743f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2744{
Bram Moolenaarae943152019-06-16 22:54:14 +02002745 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002746 int id;
2747 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002748
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002749 if (in_vim9script()
2750 && (check_for_number_arg(argvars, 0) == FAIL
2751 || check_for_dict_arg(argvars, 1) == FAIL))
2752 return;
2753
2754 id = (int)tv_get_number(argvars);
2755 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002756 if (wp == NULL)
2757 return; // invalid {id}
2758
2759 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2760 {
2761 emsg(_(e_dictreq));
2762 return;
2763 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002764 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002765
Bram Moolenaarae943152019-06-16 22:54:14 +02002766 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002767
2768 if (wp->w_winrow + wp->w_height >= cmdline_row)
2769 clear_cmdline = TRUE;
2770 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002771}
2772
Bram Moolenaarbc133542019-05-29 20:26:46 +02002773/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002774 * popup_setoptions({id}, {options})
2775 */
2776 void
2777f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2778{
2779 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002780 int id;
2781 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002782 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002783
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002784 if (in_vim9script()
2785 && (check_for_number_arg(argvars, 0) == FAIL
2786 || check_for_dict_arg(argvars, 1) == FAIL))
2787 return;
2788
2789 id = (int)tv_get_number(argvars);
2790 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002791 if (wp == NULL)
2792 return; // invalid {id}
2793
2794 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2795 {
2796 emsg(_(e_dictreq));
2797 return;
2798 }
2799 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002800 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002801
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002802 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002803
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002804 if (old_firstline != wp->w_firstline)
2805 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002806 popup_adjust_position(wp);
2807}
2808
2809/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002810 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002811 */
2812 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002813f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002814{
2815 dict_T *dict;
2816 int id = (int)tv_get_number(argvars);
2817 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002818 int top_extra;
2819 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002820
2821 if (rettv_dict_alloc(rettv) == OK)
2822 {
2823 if (wp == NULL)
2824 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002825 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002826 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2827
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002828 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002829 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002830 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002831
Bram Moolenaarbc133542019-05-29 20:26:46 +02002832 dict_add_number(dict, "line", wp->w_winrow + 1);
2833 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002834 dict_add_number(dict, "width", wp->w_width + left_extra
2835 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2836 dict_add_number(dict, "height", wp->w_height + top_extra
2837 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002838
2839 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2840 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2841 dict_add_number(dict, "core_width", wp->w_width);
2842 dict_add_number(dict, "core_height", wp->w_height);
2843
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002844 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002845 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002846 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002847 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002848 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002849
2850 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002851 }
2852}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002853
2854/*
2855 * popup_list()
2856 */
2857 void
2858f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2859{
2860 win_T *wp;
2861 tabpage_T *tp;
2862
2863 if (rettv_list_alloc(rettv) != OK)
2864 return;
2865 FOR_ALL_POPUPWINS(wp)
2866 list_append_number(rettv->vval.v_list, wp->w_id);
2867 FOR_ALL_TABPAGES(tp)
2868 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2869 list_append_number(rettv->vval.v_list, wp->w_id);
2870}
2871
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002872/*
2873 * popup_locate({row}, {col})
2874 */
2875 void
2876f_popup_locate(typval_T *argvars, typval_T *rettv)
2877{
2878 int row = tv_get_number(&argvars[0]) - 1;
2879 int col = tv_get_number(&argvars[1]) - 1;
2880 win_T *wp;
2881
2882 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002883 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002884 rettv->vval.v_number = wp->w_id;
2885}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002886
2887/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002888 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2889 */
2890 static void
2891get_padding_border(dict_T *dict, int *array, char *name)
2892{
2893 list_T *list;
2894 int i;
2895
2896 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2897 return;
2898
2899 list = list_alloc();
2900 if (list != NULL)
2901 {
2902 dict_add_list(dict, name, list);
2903 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2904 for (i = 0; i < 4; ++i)
2905 list_append_number(list, array[i]);
2906 }
2907}
2908
2909/*
2910 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2911 */
2912 static void
2913get_borderhighlight(dict_T *dict, win_T *wp)
2914{
2915 list_T *list;
2916 int i;
2917
2918 for (i = 0; i < 4; ++i)
2919 if (wp->w_border_highlight[i] != NULL)
2920 break;
2921 if (i == 4)
2922 return;
2923
2924 list = list_alloc();
2925 if (list != NULL)
2926 {
2927 dict_add_list(dict, "borderhighlight", list);
2928 for (i = 0; i < 4; ++i)
2929 list_append_string(list, wp->w_border_highlight[i], -1);
2930 }
2931}
2932
2933/*
2934 * For popup_getoptions(): add a "borderchars" entry to "dict".
2935 */
2936 static void
2937get_borderchars(dict_T *dict, win_T *wp)
2938{
2939 list_T *list;
2940 int i;
2941 char_u buf[NUMBUFLEN];
2942 int len;
2943
2944 for (i = 0; i < 8; ++i)
2945 if (wp->w_border_char[i] != 0)
2946 break;
2947 if (i == 8)
2948 return;
2949
2950 list = list_alloc();
2951 if (list != NULL)
2952 {
2953 dict_add_list(dict, "borderchars", list);
2954 for (i = 0; i < 8; ++i)
2955 {
2956 len = mb_char2bytes(wp->w_border_char[i], buf);
2957 list_append_string(list, buf, len);
2958 }
2959 }
2960}
2961
2962/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002963 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002964 */
2965 static void
2966get_moved_list(dict_T *dict, win_T *wp)
2967{
2968 list_T *list;
2969
2970 list = list_alloc();
2971 if (list != NULL)
2972 {
2973 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002974 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002975 list_append_number(list, wp->w_popup_mincol);
2976 list_append_number(list, wp->w_popup_maxcol);
2977 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002978 list = list_alloc();
2979 if (list != NULL)
2980 {
2981 dict_add_list(dict, "mousemoved", list);
2982 list_append_number(list, wp->w_popup_mouse_row);
2983 list_append_number(list, wp->w_popup_mouse_mincol);
2984 list_append_number(list, wp->w_popup_mouse_maxcol);
2985 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002986}
2987
2988/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002989 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002990 */
2991 void
2992f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2993{
2994 dict_T *dict;
2995 int id = (int)tv_get_number(argvars);
2996 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002997 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002998 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002999
3000 if (rettv_dict_alloc(rettv) == OK)
3001 {
3002 if (wp == NULL)
3003 return;
3004
3005 dict = rettv->vval.v_dict;
3006 dict_add_number(dict, "line", wp->w_wantline);
3007 dict_add_number(dict, "col", wp->w_wantcol);
3008 dict_add_number(dict, "minwidth", wp->w_minwidth);
3009 dict_add_number(dict, "minheight", wp->w_minheight);
3010 dict_add_number(dict, "maxheight", wp->w_maxheight);
3011 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003012 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003013 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003014 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003015 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003016 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003017 {
3018 proptype_T *pt = text_prop_type_by_id(
3019 wp->w_popup_prop_win->w_buffer,
3020 wp->w_popup_prop_type);
3021
3022 if (pt != NULL)
3023 dict_add_string(dict, "textprop", pt->pt_name);
3024 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3025 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3026 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003027 dict_add_string(dict, "title", wp->w_popup_title);
3028 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003029 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003030 dict_add_number(dict, "mapping",
3031 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003032 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003033 dict_add_number(dict, "posinvert",
3034 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003035 dict_add_number(dict, "cursorline",
3036 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003037 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003038 if (wp->w_scrollbar_highlight != NULL)
3039 dict_add_string(dict, "scrollbarhighlight",
3040 wp->w_scrollbar_highlight);
3041 if (wp->w_thumb_highlight != NULL)
3042 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003043
Bram Moolenaara3fce622019-06-20 02:31:49 +02003044 // find the tabpage that holds this popup
3045 i = 1;
3046 FOR_ALL_TABPAGES(tp)
3047 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003048 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003049
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003050 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3051 if (twp->w_id == id)
3052 break;
3053 if (twp != NULL)
3054 break;
3055 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003056 }
3057 if (tp == NULL)
3058 i = -1; // must be global
3059 else if (tp == curtab)
3060 i = 0;
3061 dict_add_number(dict, "tabpage", i);
3062
Bram Moolenaarae943152019-06-16 22:54:14 +02003063 get_padding_border(dict, wp->w_popup_padding, "padding");
3064 get_padding_border(dict, wp->w_popup_border, "border");
3065 get_borderhighlight(dict, wp);
3066 get_borderchars(dict, wp);
3067 get_moved_list(dict, wp);
3068
3069 if (wp->w_filter_cb.cb_name != NULL)
3070 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3071 if (wp->w_close_cb.cb_name != NULL)
3072 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003073
K.Takataeeec2542021-06-02 13:28:16 +02003074 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003075 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3076 {
3077 dict_add_string(dict, "pos",
3078 (char_u *)poppos_entries[i].pp_name);
3079 break;
3080 }
3081
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003082 dict_add_string(dict, "close", (char_u *)(
3083 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3084 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3085
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003086# if defined(FEAT_TIMERS)
3087 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3088 ? (long)wp->w_popup_timer->tr_interval : 0L);
3089# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003090 }
3091}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003092
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003093# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003094/*
3095 * Return TRUE if the current window is running a terminal in a popup window.
3096 * Return FALSE when the job has ended.
3097 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003098 int
3099error_if_term_popup_window()
3100{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003101 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3102 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003103 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003104 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003105 return TRUE;
3106 }
3107 return FALSE;
3108}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003109# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003110
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003111/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003112 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003113 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003114 * Each calling function should use a different flag, see the list at
3115 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003116 */
3117 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003118popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003119{
3120 win_T *wp;
3121
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003122 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003123 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003124 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003125 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003126}
3127
3128/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003129 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003130 * Must have called popup_reset_handled() first.
3131 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3132 * popup with the highest zindex.
3133 */
3134 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003135find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003136{
3137 win_T *wp;
3138 win_T *found_wp;
3139 int found_zindex;
3140
3141 found_zindex = lowest ? INT_MAX : 0;
3142 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003143 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003144 if ((wp->w_popup_handled & handled_flag) == 0
3145 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003146 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003147 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003148 {
3149 found_zindex = wp->w_zindex;
3150 found_wp = wp;
3151 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003152 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003153 if ((wp->w_popup_handled & handled_flag) == 0
3154 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003155 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003156 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003157 {
3158 found_zindex = wp->w_zindex;
3159 found_wp = wp;
3160 }
3161
3162 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003163 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003164 return found_wp;
3165}
3166
3167/*
3168 * Invoke the filter callback for window "wp" with typed character "c".
3169 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003170 * Returns the return value of the filter or -1 for CTRL-C in the current
3171 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003172 * Careful: The filter may make "wp" invalid!
3173 */
3174 static int
3175invoke_popup_filter(win_T *wp, int c)
3176{
3177 int res;
3178 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003179 typval_T argv[3];
3180 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003181 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003182 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003183
Bram Moolenaara42d9452019-06-15 21:46:30 +02003184 // Emergency exit: CTRL-C closes the popup.
3185 if (c == Ctrl_C)
3186 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003187 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003188 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003189
3190 // Reset got_int to avoid the callback isn't called.
3191 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003192 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003193 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003194
3195 // If the popup is the current window it probably fails to close. Then
3196 // do not consume the key.
3197 if (was_curwin && wp == curwin)
3198 return -1;
3199 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003200 }
3201
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003202 argv[0].v_type = VAR_NUMBER;
3203 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3204
3205 // Convert the number to a string, so that the function can use:
3206 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003207 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003208 argv[1].v_type = VAR_STRING;
3209 argv[1].vval.v_string = vim_strsave(buf);
3210
3211 argv[2].v_type = VAR_UNKNOWN;
3212
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003213 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003214 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3215 {
3216 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003217 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003218 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003219 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003220 }
3221 else
3222 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003223 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3224 popup_highlight_curline(wp);
3225
Bram Moolenaar371806e2020-10-22 13:44:54 +02003226 // If an error message was given always return FALSE, so that keys are
3227 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003228 // If we get three errors in a row then close the popup. Decrement the
3229 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3230 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003231 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003232 {
3233 wp->w_filter_errors += 10;
3234 if (wp->w_filter_errors >= 30)
3235 popup_close_with_retval(wp, -1);
3236 res = FALSE;
3237 }
3238 else
3239 {
3240 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3241 --wp->w_filter_errors;
3242 res = tv_get_bool(&rettv);
3243 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003244 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003245
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003246 vim_free(argv[1].vval.v_string);
3247 clear_tv(&rettv);
3248 return res;
3249}
3250
3251/*
3252 * Called when "c" was typed: invoke popup filter callbacks.
3253 * Returns TRUE when the character was consumed,
3254 */
3255 int
3256popup_do_filter(int c)
3257{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003258 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003259 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003260 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003261 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003262 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003263 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003264
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003265#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003266 // Popup window with terminal always gets focus.
3267 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3268 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003269#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003270
Bram Moolenaar934470e2019-09-01 23:27:05 +02003271 if (recursive)
3272 return FALSE;
3273 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003274
Bram Moolenaarf6396232019-08-24 19:36:00 +02003275 if (c == K_LEFTMOUSE)
3276 {
3277 int row = mouse_row;
3278 int col = mouse_col;
3279
3280 wp = mouse_find_win(&row, &col, FIND_POPUP);
3281 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003282 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003283 }
3284
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003285 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003286 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003287 while (res == FALSE
3288 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003289 if (wp->w_filter_cb.cb_name != NULL
3290 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003291 res = invoke_popup_filter(wp, c);
3292
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003293 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003294 {
3295 int save_got_int = got_int;
3296
3297 // Reset got_int to avoid a function used in the statusline aborts.
3298 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003299 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003300 got_int |= save_got_int;
3301 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003302 recursive = FALSE;
3303 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003304
3305 // When interrupted return FALSE to avoid looping.
3306 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003307}
3308
Bram Moolenaar3397f742019-06-02 18:40:06 +02003309/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003310 * Return TRUE if there is a popup visible with a filter callback and the
3311 * "mapping" property off.
3312 */
3313 int
3314popup_no_mapping(void)
3315{
3316 int round;
3317 win_T *wp;
3318
3319 for (round = 1; round <= 2; ++round)
3320 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3321 wp != NULL; wp = wp->w_next)
3322 if (wp->w_filter_cb.cb_name != NULL
3323 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3324 return TRUE;
3325 return FALSE;
3326}
3327
3328/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003329 * Called when the cursor moved: check if any popup needs to be closed if the
3330 * cursor moved far enough.
3331 */
3332 void
3333popup_check_cursor_pos()
3334{
3335 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003336
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003337 popup_reset_handled(POPUP_HANDLED_3);
3338 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003339 if (wp->w_popup_curwin != NULL
3340 && (curwin != wp->w_popup_curwin
3341 || curwin->w_cursor.lnum != wp->w_popup_lnum
3342 || curwin->w_cursor.col < wp->w_popup_mincol
3343 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003344 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003345}
3346
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003347/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003348 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003349 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003350 static void
3351popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003352{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003353 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003354 char_u *cells;
3355 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003356
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003357 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3358 {
3359 vim_free(wp->w_popup_mask_cells);
3360 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003361 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003362 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003363 if (wp->w_popup_mask_cells != NULL
3364 && wp->w_popup_mask_height == height
3365 && wp->w_popup_mask_width == width)
3366 return; // cache is still valid
3367
3368 vim_free(wp->w_popup_mask_cells);
3369 wp->w_popup_mask_cells = alloc_clear(width * height);
3370 if (wp->w_popup_mask_cells == NULL)
3371 return;
3372 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003373
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003374 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003375 {
3376 int cols, cole;
3377 int lines, linee;
3378
3379 li = lio->li_tv.vval.v_list->lv_first;
3380 cols = tv_get_number(&li->li_tv);
3381 if (cols < 0)
3382 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003383 if (cols <= 0)
3384 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003385 li = li->li_next;
3386 cole = tv_get_number(&li->li_tv);
3387 if (cole < 0)
3388 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003389 if (cole > width)
3390 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003391 li = li->li_next;
3392 lines = tv_get_number(&li->li_tv);
3393 if (lines < 0)
3394 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003395 if (lines <= 0)
3396 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003397 li = li->li_next;
3398 linee = tv_get_number(&li->li_tv);
3399 if (linee < 0)
3400 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003401 if (linee > height)
3402 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003403
Bram Moolenaar4012d262020-12-29 11:57:46 +01003404 for (row = lines - 1; row < linee; ++row)
3405 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003406 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003407 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003408}
3409
3410/*
3411 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3412 * "col" and "line" are screen coordinates.
3413 */
3414 static int
3415popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3416{
3417 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3418 int line = screenline - wp->w_winrow;
3419
3420 return col >= 0 && col < width
3421 && line >= 0 && line < height
3422 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003423}
3424
3425/*
3426 * Set flags in popup_transparent[] for window "wp" to "val".
3427 */
3428 static void
3429update_popup_transparent(win_T *wp, int val)
3430{
3431 if (wp->w_popup_mask != NULL)
3432 {
3433 int width = popup_width(wp);
3434 int height = popup_height(wp);
3435 listitem_T *lio, *li;
3436 int cols, cole;
3437 int lines, linee;
3438 int col, line;
3439
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003440 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003441 {
3442 li = lio->li_tv.vval.v_list->lv_first;
3443 cols = tv_get_number(&li->li_tv);
3444 if (cols < 0)
3445 cols = width + cols + 1;
3446 li = li->li_next;
3447 cole = tv_get_number(&li->li_tv);
3448 if (cole < 0)
3449 cole = width + cole + 1;
3450 li = li->li_next;
3451 lines = tv_get_number(&li->li_tv);
3452 if (lines < 0)
3453 lines = height + lines + 1;
3454 li = li->li_next;
3455 linee = tv_get_number(&li->li_tv);
3456 if (linee < 0)
3457 linee = height + linee + 1;
3458
3459 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003460 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003461 if (cols < 0)
3462 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003463 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003464 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003465 if (lines < 0)
3466 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003467 for (line = lines; line < linee
3468 && line + wp->w_winrow < screen_Rows; ++line)
3469 for (col = cols; col < cole
3470 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003471 popup_transparent[(line + wp->w_winrow) * screen_Columns
3472 + col + wp->w_wincol] = val;
3473 }
3474 }
3475}
3476
3477/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003478 * Only called when popup window "wp" is hidden: If the window is positioned
3479 * next to a text property, and it is now visible, then unhide the popup.
3480 * We don't check if visible popups become hidden, that is done in
3481 * popup_adjust_position().
3482 * Return TRUE if the popup became unhidden.
3483 */
3484 static int
3485check_popup_unhidden(win_T *wp)
3486{
3487 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3488 {
3489 textprop_T prop;
3490 linenr_T lnum;
3491
3492 if (find_visible_prop(wp->w_popup_prop_win,
3493 wp->w_popup_prop_type, wp->w_popup_prop_id,
3494 &prop, &lnum) == OK)
3495 {
3496 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003497 wp->w_popup_prop_topline = 0; // force repositioning
3498 return TRUE;
3499 }
3500 }
3501 return FALSE;
3502}
3503
3504/*
3505 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3506 * That is when the buffer in the popup was changed, or the popup is following
3507 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003508 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003509 */
3510 static int
3511popup_need_position_adjust(win_T *wp)
3512{
3513 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3514 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003515 if (win_valid(wp->w_popup_prop_win)
3516 && (wp->w_popup_prop_changedtick
3517 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3518 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3519 return TRUE;
3520
3521 // May need to adjust the width if the cursor moved.
3522 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003523}
3524
3525/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003526 * Update "popup_mask" if needed.
3527 * Also recomputes the popup size and positions.
3528 * Also updates "popup_visible".
3529 * Also marks window lines for redrawing.
3530 */
3531 void
3532may_update_popup_mask(int type)
3533{
3534 win_T *wp;
3535 short *mask;
3536 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003537 int redraw_all_popups = FALSE;
3538 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003539
3540 // Need to recompute when switching tabs.
3541 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3542 // (such as the screen size) must have changed.
3543 if (popup_mask_tab != curtab || type >= NOT_VALID)
3544 {
3545 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003546 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003547 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003548
3549 // Check if any popup window buffer has changed and if any popup connected
3550 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003551 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003552 if (wp->w_popup_flags & POPF_HIDDEN)
3553 popup_mask_refresh |= check_popup_unhidden(wp);
3554 else if (popup_need_position_adjust(wp))
3555 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003556 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003557 if (wp->w_popup_flags & POPF_HIDDEN)
3558 popup_mask_refresh |= check_popup_unhidden(wp);
3559 else if (popup_need_position_adjust(wp))
3560 popup_mask_refresh = TRUE;
3561
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003562 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003563 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003564
3565 // Need to update the mask, something has changed.
3566 popup_mask_refresh = FALSE;
3567 popup_mask_tab = curtab;
3568 popup_visible = FALSE;
3569
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003570 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003571 // If redrawing only what is needed, update "popup_mask_next" and then
3572 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003573 redrawing_all_win = TRUE;
3574 FOR_ALL_WINDOWS(wp)
3575 if (wp->w_redr_type < SOME_VALID)
3576 redrawing_all_win = FALSE;
3577 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003578 mask = popup_mask;
3579 else
3580 mask = popup_mask_next;
3581 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3582
3583 // Find the window with the lowest zindex that hasn't been handled yet,
3584 // so that the window with a higher zindex overwrites the value in
3585 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003586 popup_reset_handled(POPUP_HANDLED_4);
3587 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003588 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003589 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003590 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003591
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003592 popup_visible = TRUE;
3593
Bram Moolenaar12034e22019-08-25 22:25:02 +02003594 // Recompute the position if the text changed. It may make the popup
3595 // hidden if it's attach to a text property that is no longer visible.
3596 if (redraw_all_popups || popup_need_position_adjust(wp))
3597 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003598 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003599 if (wp->w_popup_flags & POPF_HIDDEN)
3600 continue;
3601 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003602
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003603 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003604 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003605 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003606 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003607 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003608 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003609 col < wp->w_wincol + width - wp->w_popup_leftoff
3610 && col < screen_Columns; ++col)
3611 if (wp->w_popup_mask_cells == NULL
3612 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003613 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003614 }
3615
3616 // Only check which lines are to be updated if not already
3617 // updating all lines.
3618 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003619 {
3620 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3621 win_T *prev_wp = NULL;
3622
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623 for (line = 0; line < screen_Rows; ++line)
3624 {
3625 int col_done = 0;
3626
3627 for (col = 0; col < screen_Columns; ++col)
3628 {
3629 int off = line * screen_Columns + col;
3630
3631 if (popup_mask[off] != popup_mask_next[off])
3632 {
3633 popup_mask[off] = popup_mask_next[off];
3634
3635 if (line >= cmdline_row)
3636 {
3637 // the command line needs to be cleared if text below
3638 // the popup is now visible.
3639 if (!msg_scrolled && popup_mask_next[off] == 0)
3640 clear_cmdline = TRUE;
3641 }
3642 else if (col >= col_done)
3643 {
3644 linenr_T lnum;
3645 int line_cp = line;
3646 int col_cp = col;
3647
3648 // The screen position "line" / "col" needs to be
3649 // redrawn. Figure out what window that is and update
3650 // w_redraw_top and w_redr_bot. Only needs to be done
3651 // once for each window line.
3652 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3653 if (wp != NULL)
3654 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003655#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003656 // A terminal window needs to be redrawn.
3657 if (bt_terminal(wp->w_buffer))
3658 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003659 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003660#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003661 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003662 if (wp != prev_wp)
3663 {
3664 vim_memset(plines_cache, 0,
3665 sizeof(int) * Rows);
3666 prev_wp = wp;
3667 }
3668
3669 if (line_cp >= wp->w_height)
3670 // In (or below) status line
3671 wp->w_redr_status = TRUE;
3672 else
3673 {
3674 // compute the position in the buffer line
3675 // from the position in the window
3676 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003677 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003678 redrawWinline(wp, lnum);
3679 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003680 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003681
3682 // This line is going to be redrawn, no need to
3683 // check until the right side of the window.
3684 col_done = wp->w_wincol + wp->w_width - 1;
3685 }
3686 }
3687 }
3688 }
3689 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003690
3691 vim_free(plines_cache);
3692 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003693}
3694
3695/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003696 * If the current window is a popup and something relevant changed, recompute
3697 * the position and size.
3698 */
3699 void
3700may_update_popup_position(void)
3701{
3702 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3703 popup_adjust_position(curwin);
3704}
3705
3706/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003707 * Return a string of "len" spaces in IObuff.
3708 */
3709 static char_u *
3710get_spaces(int len)
3711{
3712 vim_memset(IObuff, ' ', (size_t)len);
3713 IObuff[len] = NUL;
3714 return IObuff;
3715}
3716
3717/*
3718 * Update popup windows. They are drawn on top of normal windows.
3719 * "win_update" is called for each popup window, lowest zindex first.
3720 */
3721 void
3722update_popups(void (*win_update)(win_T *wp))
3723{
3724 win_T *wp;
3725 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003726 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003727 int total_width;
3728 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003729 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003730 int popup_attr;
3731 int border_attr[4];
3732 int border_char[8];
3733 char_u buf[MB_MAXBYTES];
3734 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003735 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003736 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003737 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003738 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003739 int sb_thumb_top = 0;
3740 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003741 int attr_scroll = 0;
3742 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003743
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003744 // hide the cursor until redrawing is done.
3745 cursor_off();
3746
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003747 // Find the window with the lowest zindex that hasn't been updated yet,
3748 // so that the window with a higher zindex is drawn later, thus goes on
3749 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003750 popup_reset_handled(POPUP_HANDLED_5);
3751 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003752 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003753 int title_len = 0;
3754 int title_wincol;
3755
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003756 // This drawing uses the zindex of the popup window, so that it's on
3757 // top of the text but doesn't draw when another popup with higher
3758 // zindex is on top of the character.
3759 screen_zindex = wp->w_zindex;
3760
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003761 // Set flags in popup_transparent[] for masked cells.
3762 update_popup_transparent(wp, 1);
3763
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003764 // adjust w_winrow and w_wincol for border and padding, since
3765 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003766 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003767 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3768 - wp->w_popup_leftoff;
3769 if (wp->w_wincol + left_extra < 0)
3770 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003772 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003774 // Draw the popup text, unless it's off screen.
3775 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003776 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003777 // May need to update the "cursorline" highlighting, which may also
3778 // change "topline"
3779 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3780 popup_highlight_curline(wp);
3781
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003782 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003783
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003784 // move the cursor into the visible lines, otherwise executing
3785 // commands with win_execute() may cause the text to jump.
3786 if (wp->w_cursor.lnum < wp->w_topline)
3787 wp->w_cursor.lnum = wp->w_topline;
3788 else if (wp->w_cursor.lnum >= wp->w_botline)
3789 wp->w_cursor.lnum = wp->w_botline - 1;
3790 }
3791
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003792 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003793 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003794
3795 // Add offset for border and padding if not done already.
3796 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3797 {
3798 wp->w_wcol += left_extra;
3799 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3800 }
3801 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003802 {
3803 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003804 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003805 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003806
Bram Moolenaard529ba52019-07-02 23:13:53 +02003807 total_width = popup_width(wp);
3808 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003809 popup_attr = get_wcr_attr(wp);
3810
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003811 if (wp->w_winrow + total_height > cmdline_row)
3812 wp->w_popup_flags |= POPF_ON_CMDLINE;
3813 else
3814 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3815
3816
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003817 // We can only use these line drawing characters when 'encoding' is
3818 // "utf-8" and 'ambiwidth' is "single".
3819 if (enc_utf8 && *p_ambw == 's')
3820 {
3821 border_char[0] = border_char[2] = 0x2550;
3822 border_char[1] = border_char[3] = 0x2551;
3823 border_char[4] = 0x2554;
3824 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003825 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3826 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003827 border_char[7] = 0x255a;
3828 }
3829 else
3830 {
3831 border_char[0] = border_char[2] = '-';
3832 border_char[1] = border_char[3] = '|';
3833 for (i = 4; i < 8; ++i)
3834 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003835 if (wp->w_popup_flags & POPF_RESIZE)
3836 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003837 }
3838 for (i = 0; i < 8; ++i)
3839 if (wp->w_border_char[i] != 0)
3840 border_char[i] = wp->w_border_char[i];
3841
3842 for (i = 0; i < 4; ++i)
3843 {
3844 border_attr[i] = popup_attr;
3845 if (wp->w_border_highlight[i] != NULL)
3846 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3847 }
3848
Bram Moolenaard91467f2020-11-21 12:42:09 +01003849 // Title goes on top of border or padding.
3850 title_wincol = wp->w_wincol + 1;
3851 if (wp->w_popup_title != NULL)
3852 {
Ralf Schandlbc869872021-05-28 14:12:14 +02003853 title_len = (int)MB_CHARLEN(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003854
Ralf Schandlbc869872021-05-28 14:12:14 +02003855 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01003856 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02003857 {
3858 int title_byte_len = (int)STRLEN(wp->w_popup_title);
3859 char_u *title_text = alloc(title_byte_len + 1);
3860
3861 if (title_text != NULL)
3862 {
3863 trunc_string(wp->w_popup_title, title_text,
3864 total_width - 2, title_byte_len + 1);
3865 screen_puts(title_text, wp->w_winrow, title_wincol,
3866 wp->w_popup_border[0] > 0
3867 ? border_attr[0] : popup_attr);
3868 vim_free(title_text);
3869 }
3870
Bram Moolenaard91467f2020-11-21 12:42:09 +01003871 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02003872 }
3873 else
3874 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
3875 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01003876 }
3877
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003878 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003879 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003880 if (wp->w_popup_border[0] > 0)
3881 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003882 // top border; do not draw over the title
3883 if (title_len > 0)
3884 {
3885 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3886 wincol < 0 ? 0 : wincol, title_wincol,
3887 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003888 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01003889 border_char[0], border_attr[0]);
3890 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3891 title_wincol + title_len, wincol + total_width,
3892 border_char[0], border_char[0], border_attr[0]);
3893 }
3894 else
3895 {
3896 screen_fill(wp->w_winrow, wp->w_winrow + 1,
3897 wincol < 0 ? 0 : wincol, wincol + total_width,
3898 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
3899 ? border_char[4] : border_char[0],
3900 border_char[0], border_attr[0]);
3901 }
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003902 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003903 {
3904 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3905 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003906 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003907 }
3908 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003909 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3910 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003911
Bram Moolenaard529ba52019-07-02 23:13:53 +02003912 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3913 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003914 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01003915 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003916 - wp->w_has_scrollbar;
3917 if (padcol < 0)
3918 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003919 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003920 padcol = 0;
3921 }
3922 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003923 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003924 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003925 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003926 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01003927 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003928 // top padding and no border; do not draw over the title
3929 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003930 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003931 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003932 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003933 row += 1;
3934 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003935 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01003936 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01003937 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003938 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003939
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003940 // Compute scrollbar thumb position and size.
3941 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003942 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003943 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3944 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003945
Bram Moolenaar076d9882019-09-14 22:23:29 +02003946 sb_thumb_height = (height * height + linecount / 2) / linecount;
3947 if (wp->w_topline > 1 && sb_thumb_height == height)
3948 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003949 if (sb_thumb_height == 0)
3950 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003951 if (linecount <= wp->w_height)
3952 // it just fits, avoid divide by zero
3953 sb_thumb_top = 0;
3954 else
3955 sb_thumb_top = (wp->w_topline - 1
3956 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003957 * (wp->w_height - sb_thumb_height)
3958 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003959 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3960 sb_thumb_top = 1; // show it's scrolled
3961
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003962 if (wp->w_scrollbar_highlight != NULL)
3963 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3964 else
3965 attr_scroll = highlight_attr[HLF_PSB];
3966 if (wp->w_thumb_highlight != NULL)
3967 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3968 else
3969 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003970 }
3971
3972 for (i = wp->w_popup_border[0];
3973 i < total_height - wp->w_popup_border[2]; ++i)
3974 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003975 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003976 // left and right padding only needed next to the body
3977 int do_padding =
3978 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3979 && i < total_height - wp->w_popup_border[2]
3980 - wp->w_popup_padding[2];
3981
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003982 row = wp->w_winrow + i;
3983
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003984 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003985 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003986 {
3987 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003988 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003989 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003990 if (do_padding && wp->w_popup_padding[3] > 0)
3991 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003992 int col = wincol + wp->w_popup_border[3];
3993
Bram Moolenaard529ba52019-07-02 23:13:53 +02003994 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003995 pad_left = wp->w_popup_padding[3];
3996 if (col < 0)
3997 {
3998 pad_left += col;
3999 col = 0;
4000 }
4001 if (pad_left > 0)
4002 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4003 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004004 // scrollbar
4005 if (wp->w_has_scrollbar)
4006 {
4007 int line = i - top_off;
4008 int scroll_col = wp->w_wincol + total_width - 1
4009 - wp->w_popup_border[1];
4010
4011 if (line >= 0 && line < wp->w_height)
4012 screen_putchar(' ', row, scroll_col,
4013 line >= sb_thumb_top
4014 && line < sb_thumb_top + sb_thumb_height
4015 ? attr_thumb : attr_scroll);
4016 else
4017 screen_putchar(' ', row, scroll_col, popup_attr);
4018 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004019 // right border
4020 if (wp->w_popup_border[1] > 0)
4021 {
4022 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004023 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004024 }
4025 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004026 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004027 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004028 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004029 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4030 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004031 }
4032
4033 if (wp->w_popup_padding[2] > 0)
4034 {
4035 // bottom padding
4036 row = wp->w_winrow + wp->w_popup_border[0]
4037 + wp->w_popup_padding[0] + wp->w_height;
4038 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004039 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004040 }
4041
4042 if (wp->w_popup_border[2] > 0)
4043 {
4044 // bottom border
4045 row = wp->w_winrow + total_height - 1;
4046 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004047 wincol < 0 ? 0 : wincol,
4048 wincol + total_width,
4049 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004050 ? border_char[7] : border_char[2],
4051 border_char[2], border_attr[2]);
4052 if (wp->w_popup_border[1] > 0)
4053 {
4054 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004055 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004056 }
4057 }
4058
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004059 if (wp->w_popup_close == POPCLOSE_BUTTON)
4060 {
4061 // close button goes on top of anything at the top-right corner
4062 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004063 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004064 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4065 }
4066
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004067 update_popup_transparent(wp, 0);
4068
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004069 // Back to the normal zindex.
4070 screen_zindex = 0;
4071 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004072
4073#if defined(FEAT_SEARCH_EXTRA)
4074 // In case win_update() called start_search_hl().
4075 end_search_hl();
4076#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004077}
4078
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004079/*
4080 * Mark references in callbacks of one popup window.
4081 */
4082 static int
4083set_ref_in_one_popup(win_T *wp, int copyID)
4084{
4085 int abort = FALSE;
4086 typval_T tv;
4087
4088 if (wp->w_close_cb.cb_partial != NULL)
4089 {
4090 tv.v_type = VAR_PARTIAL;
4091 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4092 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4093 }
4094 if (wp->w_filter_cb.cb_partial != NULL)
4095 {
4096 tv.v_type = VAR_PARTIAL;
4097 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4098 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4099 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004100 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004101 return abort;
4102}
4103
4104/*
4105 * Set reference in callbacks of popup windows.
4106 */
4107 int
4108set_ref_in_popups(int copyID)
4109{
4110 int abort = FALSE;
4111 win_T *wp;
4112 tabpage_T *tp;
4113
4114 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4115 abort = abort || set_ref_in_one_popup(wp, copyID);
4116
4117 FOR_ALL_TABPAGES(tp)
4118 {
4119 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4120 abort = abort || set_ref_in_one_popup(wp, copyID);
4121 if (abort)
4122 break;
4123 }
4124 return abort;
4125}
Bram Moolenaar79648732019-07-18 21:43:07 +02004126
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004127 int
4128popup_is_popup(win_T *wp)
4129{
4130 return wp->w_popup_flags != 0;
4131}
4132
4133#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004134/*
4135 * Find an existing popup used as the preview window, in the current tab page.
4136 * Return NULL if not found.
4137 */
4138 win_T *
4139popup_find_preview_window(void)
4140{
4141 win_T *wp;
4142
4143 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004144 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004145 if (wp->w_p_pvw)
4146 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004147 return NULL;
4148}
4149
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004150/*
4151 * Find an existing popup used as the info window, in the current tab page.
4152 * Return NULL if not found.
4153 */
4154 win_T *
4155popup_find_info_window(void)
4156{
4157 win_T *wp;
4158
4159 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004160 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004161 if (wp->w_popup_flags & POPF_INFO)
4162 return wp;
4163 return NULL;
4164}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004165#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004166
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004167 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004168f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4169{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004170#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004171 win_T *wp = popup_find_info_window();
4172
4173 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004174#else
4175 rettv->vval.v_number = 0;
4176#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004177}
4178
4179 void
4180f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004181{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004182#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004183 win_T *wp = popup_find_preview_window();
4184
4185 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004186#else
4187 rettv->vval.v_number = 0;
4188#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004189}
4190
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004191#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004192/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004193 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004194 * NOTE: this makes the popup the current window, so that the file can be
4195 * edited. However, it must not remain to be the current window, the caller
4196 * must make sure of that.
4197 */
4198 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004199popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004200{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004201 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004202
4203 if (wp == NULL)
4204 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004205 if (info)
4206 wp->w_popup_flags |= POPF_INFO;
4207 else
4208 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004209
4210 // Set the width to a reasonable value, so that w_topline can be computed.
4211 if (wp->w_minwidth > 0)
4212 wp->w_width = wp->w_minwidth;
4213 else if (wp->w_maxwidth > 0)
4214 wp->w_width = wp->w_maxwidth;
4215 else
4216 wp->w_width = curwin->w_width;
4217
4218 // Will switch to another buffer soon, dummy one can be wiped.
4219 wp->w_buffer->b_locked = FALSE;
4220
4221 win_enter(wp, FALSE);
4222 return OK;
4223}
4224
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004225/*
4226 * Close any preview popup.
4227 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004228 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004229popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004230{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004231 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004232
4233 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004234 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004235}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004236
4237/*
4238 * Hide the info popup.
4239 */
4240 void
4241popup_hide_info(void)
4242{
4243 win_T *wp = popup_find_info_window();
4244
4245 if (wp != NULL)
4246 popup_hide(wp);
4247}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004248
4249/*
4250 * Close any info popup.
4251 */
4252 void
4253popup_close_info(void)
4254{
4255 win_T *wp = popup_find_info_window();
4256
4257 if (wp != NULL)
4258 popup_close_with_retval(wp, -1);
4259}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004260#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004261
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004262/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004263 * Close any popup for a text property associated with "win".
4264 * Return TRUE if a popup was closed.
4265 */
4266 int
4267popup_win_closed(win_T *win)
4268{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004269 int round;
4270 win_T *wp;
4271 win_T *next;
4272 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004273
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004274 for (round = 1; round <= 2; ++round)
4275 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4276 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004277 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004278 next = wp->w_next;
4279 if (wp->w_popup_prop_win == win)
4280 {
4281 popup_close_with_retval(wp, -1);
4282 ret = TRUE;
4283 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004284 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004285 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004286}
4287
4288/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004289 * Set the title of the popup window to the file name.
4290 */
4291 void
4292popup_set_title(win_T *wp)
4293{
4294 if (wp->w_buffer->b_fname != NULL)
4295 {
4296 char_u dirname[MAXPATHL];
4297 size_t len;
4298
4299 mch_dirname(dirname, MAXPATHL);
4300 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4301
4302 vim_free(wp->w_popup_title);
4303 len = STRLEN(wp->w_buffer->b_fname) + 3;
4304 wp->w_popup_title = alloc((int)len);
4305 if (wp->w_popup_title != NULL)
4306 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4307 wp->w_buffer->b_fname);
4308 redraw_win_later(wp, VALID);
4309 }
4310}
4311
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004312# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004313/*
4314 * If there is a preview window, update the title.
4315 * Used after changing directory.
4316 */
4317 void
4318popup_update_preview_title(void)
4319{
4320 win_T *wp = popup_find_preview_window();
4321
4322 if (wp != NULL)
4323 popup_set_title(wp);
4324}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004325# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004326
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004327#endif // FEAT_PROP_POPUP