blob: 947dd91bc1c1c75926d2419e122063465fd47d71 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
101 range_list_materialize(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
300 if (!(wp->w_popup_flags & POPF_DRAG))
301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
386 "{_ -> popup_close(%d)}", wp->w_id);
387 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
405 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
406 ++nr)
407 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
408 return poppos_entries[nr].pp_val;
409
410 if (give_error)
411 semsg(_(e_invarg2), str);
412 return POPPOS_NONE;
413}
414
Bram Moolenaar17627312019-06-02 19:53:44 +0200415/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200416 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200417 */
418 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200419apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200420{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200421 int nr;
422 char_u *str;
423 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200424
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200425 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200426 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200427 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200428 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200429 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200430 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200431 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200432 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433
434 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantline = nr;
437 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200438 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200439 wp->w_wantcol = nr;
440
441 di = dict_find(d, (char_u *)"fixed", -1);
442 if (di != NULL)
443 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
444
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);
463 if (!win_valid(wp->w_popup_prop_win))
464 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 Moolenaarb0992022020-01-30 14:55:42 +0100519 range_list_materialize(l);
520 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
581 && (curwin->w_valid & VALID_BOTLINE))
582 {
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
595 // Don't use "firstline" now.
596 wp->w_firstline = 0;
597}
598
599/*
600 * Get the sign group name for window "wp".
601 * Returns a pointer to a static buffer, overwritten on the next call.
602 */
603 static char_u *
604popup_get_sign_name(win_T *wp)
605{
606 static char buf[30];
607
608 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
609 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200610}
611
612/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200613 * Highlight the line with the cursor.
614 * Also scrolls the text to put the cursor line in view.
615 */
616 static void
617popup_highlight_curline(win_T *wp)
618{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200619 int sign_id = 0;
620 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200621
Bram Moolenaar72570732019-11-30 14:21:53 +0100622 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200623
624 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
625 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200626 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200627
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200628 if (!sign_exists_by_name(sign_name))
629 {
630 char *linehl = "PopupSelected";
631
632 if (syn_name2id((char_u *)linehl) == 0)
633 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200634 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200635 }
636
Bram Moolenaar72570732019-11-30 14:21:53 +0100637 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200638 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
639 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200640 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200641 else
642 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200643 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200644}
645
646/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200647 * Shared between popup_create() and f_popup_setoptions().
648 */
649 static void
650apply_general_options(win_T *wp, dict_T *dict)
651{
652 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200653 int nr;
654 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200655
Bram Moolenaarae943152019-06-16 22:54:14 +0200656 // TODO: flip
657
658 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200659 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200660 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200661 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200662 if (wp->w_firstline < 0)
663 wp->w_firstline = -1;
664 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200665
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200666 di = dict_find(dict, (char_u *)"scrollbar", -1);
667 if (di != NULL)
668 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
669
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200670 str = dict_get_string(dict, (char_u *)"title", FALSE);
671 if (str != NULL)
672 {
673 vim_free(wp->w_popup_title);
674 wp->w_popup_title = vim_strsave(str);
675 }
676
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 di = dict_find(dict, (char_u *)"wrap", -1);
678 if (di != NULL)
679 {
680 nr = dict_get_number(dict, (char_u *)"wrap");
681 wp->w_p_wrap = nr != 0;
682 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200683
Bram Moolenaara42d9452019-06-15 21:46:30 +0200684 di = dict_find(dict, (char_u *)"drag", -1);
685 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200686 {
687 nr = dict_get_number(dict, (char_u *)"drag");
688 if (nr)
689 wp->w_popup_flags |= POPF_DRAG;
690 else
691 wp->w_popup_flags &= ~POPF_DRAG;
692 }
693
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 di = dict_find(dict, (char_u *)"posinvert", -1);
695 if (di != NULL)
696 {
697 nr = dict_get_number(dict, (char_u *)"posinvert");
698 if (nr)
699 wp->w_popup_flags |= POPF_POSINVERT;
700 else
701 wp->w_popup_flags &= ~POPF_POSINVERT;
702 }
703
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200704 di = dict_find(dict, (char_u *)"resize", -1);
705 if (di != NULL)
706 {
707 nr = dict_get_number(dict, (char_u *)"resize");
708 if (nr)
709 wp->w_popup_flags |= POPF_RESIZE;
710 else
711 wp->w_popup_flags &= ~POPF_RESIZE;
712 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200713
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200714 di = dict_find(dict, (char_u *)"close", -1);
715 if (di != NULL)
716 {
717 int ok = TRUE;
718
719 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
720 {
721 char_u *s = di->di_tv.vval.v_string;
722
723 if (STRCMP(s, "none") == 0)
724 wp->w_popup_close = POPCLOSE_NONE;
725 else if (STRCMP(s, "button") == 0)
726 wp->w_popup_close = POPCLOSE_BUTTON;
727 else if (STRCMP(s, "click") == 0)
728 wp->w_popup_close = POPCLOSE_CLICK;
729 else
730 ok = FALSE;
731 }
732 else
733 ok = FALSE;
734 if (!ok)
735 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
736 }
737
Bram Moolenaarae943152019-06-16 22:54:14 +0200738 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
739 if (str != NULL)
740 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
741 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200742
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200743 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
744 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200745 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
746 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200747
Bram Moolenaar790498b2019-06-01 22:15:29 +0200748 di = dict_find(dict, (char_u *)"borderhighlight", -1);
749 if (di != NULL)
750 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200751 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200752 emsg(_(e_listreq));
753 else
754 {
755 list_T *list = di->di_tv.vval.v_list;
756 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200757 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200758
Bram Moolenaarb0992022020-01-30 14:55:42 +0100759 range_list_materialize(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
761 ++i, li = li->li_next)
762 {
763 str = tv_get_string(&li->li_tv);
764 if (*str != NUL)
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] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100768 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200769 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200770 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
771 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100772 {
773 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200774 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200775 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100776 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200777 }
778 }
779
Bram Moolenaar790498b2019-06-01 22:15:29 +0200780 di = dict_find(dict, (char_u *)"borderchars", -1);
781 if (di != NULL)
782 {
783 if (di->di_tv.v_type != VAR_LIST)
784 emsg(_(e_listreq));
785 else
786 {
787 list_T *list = di->di_tv.vval.v_list;
788 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200789 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200790
791 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100792 {
793 range_list_materialize(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200794 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
795 ++i, li = li->li_next)
796 {
797 str = tv_get_string(&li->li_tv);
798 if (*str != NUL)
799 wp->w_border_char[i] = mb_ptr2char(str);
800 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100801 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 if (list->lv_len == 1)
803 for (i = 1; i < 8; ++i)
804 wp->w_border_char[i] = wp->w_border_char[0];
805 if (list->lv_len == 2)
806 {
807 for (i = 4; i < 8; ++i)
808 wp->w_border_char[i] = wp->w_border_char[1];
809 for (i = 1; i < 4; ++i)
810 wp->w_border_char[i] = wp->w_border_char[0];
811 }
812 }
813 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200814
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200815 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
816 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
817
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 di = dict_find(dict, (char_u *)"zindex", -1);
819 if (di != NULL)
820 {
821 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
822 if (wp->w_zindex < 1)
823 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
824 if (wp->w_zindex > 32000)
825 wp->w_zindex = 32000;
826 }
827
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200828 di = dict_find(dict, (char_u *)"mask", -1);
829 if (di != NULL)
830 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200831 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200833 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200834 {
835 listitem_T *li;
836
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200837 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200838 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
839 li = li->li_next)
840 {
841 if (li->li_tv.v_type != VAR_LIST
842 || li->li_tv.vval.v_list == NULL
843 || li->li_tv.vval.v_list->lv_len != 4)
844 {
845 ok = FALSE;
846 break;
847 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100848 else
849 range_list_materialize(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200850 }
851 }
852 if (ok)
853 {
854 wp->w_popup_mask = di->di_tv.vval.v_list;
855 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200856 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200857 }
858 else
859 semsg(_(e_invargval), "mask");
860 }
861
Bram Moolenaarae943152019-06-16 22:54:14 +0200862#if defined(FEAT_TIMERS)
863 // Add timer to close the popup after some time.
864 nr = dict_get_number(dict, (char_u *)"time");
865 if (nr > 0)
866 popup_add_timeout(wp, nr);
867#endif
868
Bram Moolenaar3397f742019-06-02 18:40:06 +0200869 di = dict_find(dict, (char_u *)"moved", -1);
870 if (di != NULL)
871 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200872 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200873 handle_moved_argument(wp, di, FALSE);
874 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200875
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200876 di = dict_find(dict, (char_u *)"mousemoved", -1);
877 if (di != NULL)
878 {
879 set_mousemoved_values(wp);
880 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200881 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200882
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200883 di = dict_find(dict, (char_u *)"cursorline", -1);
884 if (di != NULL)
885 {
886 if (di->di_tv.v_type == VAR_NUMBER)
887 {
888 if (di->di_tv.vval.v_number != 0)
889 wp->w_popup_flags |= POPF_CURSORLINE;
890 else
891 wp->w_popup_flags &= ~POPF_CURSORLINE;
892 }
893 else
894 semsg(_(e_invargval), "cursorline");
895 }
896
Bram Moolenaarae943152019-06-16 22:54:14 +0200897 di = dict_find(dict, (char_u *)"filter", -1);
898 if (di != NULL)
899 {
900 callback_T callback = get_callback(&di->di_tv);
901
902 if (callback.cb_name != NULL)
903 {
904 free_callback(&wp->w_filter_cb);
905 set_callback(&wp->w_filter_cb, &callback);
906 }
907 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200908 di = dict_find(dict, (char_u *)"mapping", -1);
909 if (di != NULL)
910 {
911 nr = dict_get_number(dict, (char_u *)"mapping");
912 if (nr)
913 wp->w_popup_flags |= POPF_MAPPING;
914 else
915 wp->w_popup_flags &= ~POPF_MAPPING;
916 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200917
Bram Moolenaar581ba392019-09-03 22:08:33 +0200918 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
919 if (str != NULL)
920 {
921 if (STRCMP(str, "a") == 0)
922 wp->w_filter_mode = MODE_ALL;
923 else
924 wp->w_filter_mode = mode_str2flags(str);
925 }
926
Bram Moolenaarae943152019-06-16 22:54:14 +0200927 di = dict_find(dict, (char_u *)"callback", -1);
928 if (di != NULL)
929 {
930 callback_T callback = get_callback(&di->di_tv);
931
932 if (callback.cb_name != NULL)
933 {
934 free_callback(&wp->w_close_cb);
935 set_callback(&wp->w_close_cb, &callback);
936 }
937 }
938}
939
940/*
941 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200942 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200943 */
944 static void
945apply_options(win_T *wp, dict_T *dict)
946{
947 int nr;
948
949 apply_move_options(wp, dict);
950 apply_general_options(wp, dict);
951
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200952 nr = dict_get_number(dict, (char_u *)"hidden");
953 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200954 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200955
Bram Moolenaar33796b32019-06-08 16:01:13 +0200956 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200957 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200958}
959
960/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200961 * Add lines to the popup from a list of strings.
962 */
963 static void
964add_popup_strings(buf_T *buf, list_T *l)
965{
966 listitem_T *li;
967 linenr_T lnum = 0;
968 char_u *p;
969
970 for (li = l->lv_first; li != NULL; li = li->li_next)
971 if (li->li_tv.v_type == VAR_STRING)
972 {
973 p = li->li_tv.vval.v_string;
974 ml_append_buf(buf, lnum++,
975 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
976 }
977}
978
979/*
980 * Add lines to the popup from a list of dictionaries.
981 */
982 static void
983add_popup_dicts(buf_T *buf, list_T *l)
984{
985 listitem_T *li;
986 listitem_T *pli;
987 linenr_T lnum = 0;
988 char_u *p;
989 dict_T *dict;
990
991 // first add the text lines
992 for (li = l->lv_first; li != NULL; li = li->li_next)
993 {
994 if (li->li_tv.v_type != VAR_DICT)
995 {
996 emsg(_(e_dictreq));
997 return;
998 }
999 dict = li->li_tv.vval.v_dict;
1000 p = dict == NULL ? NULL
1001 : dict_get_string(dict, (char_u *)"text", FALSE);
1002 ml_append_buf(buf, lnum++,
1003 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1004 }
1005
1006 // add the text properties
1007 lnum = 1;
1008 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1009 {
1010 dictitem_T *di;
1011 list_T *plist;
1012
1013 dict = li->li_tv.vval.v_dict;
1014 di = dict_find(dict, (char_u *)"props", -1);
1015 if (di != NULL)
1016 {
1017 if (di->di_tv.v_type != VAR_LIST)
1018 {
1019 emsg(_(e_listreq));
1020 return;
1021 }
1022 plist = di->di_tv.vval.v_list;
1023 if (plist != NULL)
1024 {
1025 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
1026 {
1027 if (pli->li_tv.v_type != VAR_DICT)
1028 {
1029 emsg(_(e_dictreq));
1030 return;
1031 }
1032 dict = pli->li_tv.vval.v_dict;
1033 if (dict != NULL)
1034 {
1035 int col = dict_get_number(dict, (char_u *)"col");
1036
1037 prop_add_common( lnum, col, dict, buf, NULL);
1038 }
1039 }
1040 }
1041 }
1042 }
1043}
1044
1045/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001046 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1047 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001048 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001049popup_top_extra(win_T *wp)
1050{
1051 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1052
1053 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1054 return 1;
1055 return extra;
1056}
1057
1058/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001059 * Get the padding plus border at the left.
1060 */
1061 int
1062popup_left_extra(win_T *wp)
1063{
1064 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1065}
1066
1067/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001068 * Return the height of popup window "wp", including border and padding.
1069 */
1070 int
1071popup_height(win_T *wp)
1072{
1073 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001074 + popup_top_extra(wp)
1075 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001076}
1077
1078/*
1079 * Return the width of popup window "wp", including border, padding and
1080 * scrollbar.
1081 */
1082 int
1083popup_width(win_T *wp)
1084{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001085 // w_leftcol is how many columns of the core are left of the screen
1086 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001087 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001088 + popup_extra_width(wp)
1089 + wp->w_popup_rightoff;
1090}
1091
1092/*
1093 * Return the extra width of popup window "wp": border, padding and scrollbar.
1094 */
1095 int
1096popup_extra_width(win_T *wp)
1097{
1098 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1099 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1100 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001101}
1102
1103/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001104 * Adjust the position and size of the popup to fit on the screen.
1105 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001106 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001107popup_adjust_position(win_T *wp)
1108{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001109 linenr_T lnum;
1110 int wrapped = 0;
1111 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001112 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001113 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001114 int center_vert = FALSE;
1115 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001116 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001117 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001118 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1119 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1120 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1121 int extra_height = top_extra + bot_extra;
1122 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001123 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001124 int org_winrow = wp->w_winrow;
1125 int org_wincol = wp->w_wincol;
1126 int org_width = wp->w_width;
1127 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001128 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001129 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001130 int minwidth, minheight;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001131 int wantline = wp->w_wantline; // adjusted for textprop
1132 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001133 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001134
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001135 wp->w_winrow = 0;
1136 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001137 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001138 wp->w_popup_leftoff = 0;
1139 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001140
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001141 // May need to update the "cursorline" highlighting, which may also change
1142 // "topline"
1143 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1144 popup_highlight_curline(wp);
1145
Bram Moolenaar12034e22019-08-25 22:25:02 +02001146 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1147 {
1148 win_T *prop_win = wp->w_popup_prop_win;
1149 textprop_T prop;
1150 linenr_T prop_lnum;
1151 pos_T pos;
1152 int screen_row;
1153 int screen_scol;
1154 int screen_ccol;
1155 int screen_ecol;
1156
1157 // Popup window is positioned relative to a text property.
1158 if (find_visible_prop(prop_win,
1159 wp->w_popup_prop_type, wp->w_popup_prop_id,
1160 &prop, &prop_lnum) == FAIL)
1161 {
1162 // Text property is no longer visible, hide the popup.
1163 // Unhiding the popup is done in check_popup_unhidden().
1164 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1165 {
1166 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001167 if (win_valid(wp->w_popup_prop_win))
1168 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1169 }
1170 return;
1171 }
1172
1173 // Compute the desired position from the position of the text
1174 // property. Use "wantline" and "wantcol" as offsets.
1175 pos.lnum = prop_lnum;
1176 pos.col = prop.tp_col;
1177 if (wp->w_popup_pos == POPPOS_TOPLEFT
1178 || wp->w_popup_pos == POPPOS_BOTLEFT)
1179 pos.col += prop.tp_len - 1;
1180 textpos2screenpos(prop_win, &pos, &screen_row,
1181 &screen_scol, &screen_ccol, &screen_ecol);
1182
1183 if (wp->w_popup_pos == POPPOS_TOPLEFT
1184 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1185 // below the text
1186 wantline = screen_row + wantline + 1;
1187 else
1188 // above the text
1189 wantline = screen_row + wantline - 1;
1190 center_vert = FALSE;
1191 if (wp->w_popup_pos == POPPOS_TOPLEFT
1192 || wp->w_popup_pos == POPPOS_BOTLEFT)
1193 // right of the text
1194 wantcol = screen_ecol + wantcol;
1195 else
1196 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001197 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001198 use_wantcol = TRUE;
1199 }
1200 else
1201 {
1202 // If no line was specified default to vertical centering.
1203 if (wantline == 0)
1204 center_vert = TRUE;
1205 else if (wantline < 0)
1206 // If "wantline" is negative it actually means zero.
1207 wantline = 0;
1208 if (wantcol < 0)
1209 // If "wantcol" is negative it actually means zero.
1210 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001211 }
1212
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001213 if (wp->w_popup_pos == POPPOS_CENTER)
1214 {
1215 // center after computing the size
1216 center_vert = TRUE;
1217 center_hor = TRUE;
1218 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001219 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001220 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001221 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1222 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001223 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001224 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001225 if (wp->w_winrow >= Rows)
1226 wp->w_winrow = Rows - 1;
1227 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001228
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001229 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001230 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001231 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1232 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001233 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001234 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001235 // Need to see at least one character after the decoration.
1236 if (wp->w_wincol > Columns - left_extra - 1)
1237 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001238 }
1239 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001240
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001241 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001242 // When left aligned use the space available, but shift to the left when we
1243 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001244 maxspace = Columns - wp->w_wincol - left_extra;
1245 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001246 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001247 {
1248 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001249 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001250 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001251 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001252 minheight = wp->w_minheight;
1253#ifdef FEAT_TERMINAL
1254 // A terminal popup initially does not have content, use a default minimal
1255 // width of 20 characters and height of 5 lines.
1256 if (wp->w_buffer->b_term != NULL)
1257 {
1258 if (minwidth == 0)
1259 minwidth = 20;
1260 if (minheight == 0)
1261 minheight = 5;
1262 }
1263#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001264
Bram Moolenaar8d241042019-06-12 23:40:01 +02001265 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001266 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001267 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001268 if (wp->w_topline < 1)
1269 wp->w_topline = 1;
1270 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001271 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1272
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001273 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001274 // Use a minimum width of one, so that something shows when there is no
1275 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001276 // When "firstline" is -1 then start with the last buffer line and go
1277 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001278 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001279 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001280 if (wp->w_firstline < 0)
1281 lnum = wp->w_buffer->b_ml.ml_line_count;
1282 else
1283 lnum = wp->w_topline;
1284 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001285 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001286 int len;
1287 int w_width = wp->w_width;
1288
1289 // Count Tabs for what they are worth and compute the length based on
1290 // the maximum width (matters when 'showbreak' is set).
1291 if (wp->w_width < maxwidth)
1292 wp->w_width = maxwidth;
1293 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001294 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001295 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001296
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001297 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001298 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001299 while (len > maxwidth)
1300 {
1301 ++wrapped;
1302 len -= maxwidth;
1303 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001304 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001305 }
1306 }
1307 else if (len > maxwidth
1308 && allow_adjust_left
1309 && (wp->w_popup_pos == POPPOS_TOPLEFT
1310 || wp->w_popup_pos == POPPOS_BOTLEFT))
1311 {
1312 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001313 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001314
Bram Moolenaar51c31312019-06-15 22:27:23 +02001315 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001316 {
1317 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001318
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001319 len -= truncate_shift;
1320 shift_by -= truncate_shift;
1321 }
1322
1323 wp->w_wincol -= shift_by;
1324 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001325 wp->w_width = maxwidth;
1326 }
1327 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001328 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001329 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001330 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1331 wp->w_width = wp->w_maxwidth;
1332 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001333
1334 if (wp->w_firstline < 0)
1335 --lnum;
1336 else
1337 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001338
1339 // do not use the width of lines we're not going to show
1340 if (wp->w_maxheight > 0
1341 && (wp->w_firstline >= 0
1342 ? lnum - wp->w_topline
1343 : wp->w_buffer->b_ml.ml_line_count - lnum)
1344 + wrapped >= wp->w_maxheight)
1345 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001346 }
1347
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001348 if (wp->w_firstline < 0)
1349 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1350
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001351 wp->w_has_scrollbar = wp->w_want_scrollbar
1352 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001353#ifdef FEAT_TERMINAL
1354 if (wp->w_buffer->b_term != NULL)
1355 // Terminal window never has a scrollbar, adjusts to window height.
1356 wp->w_has_scrollbar = FALSE;
1357#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001358 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001359 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001360 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001361 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001362 // make space for the scrollbar if needed, when lines wrap and when
1363 // applying minwidth
1364 if (maxwidth + right_extra >= maxspace
1365 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1366 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001367 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001368
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001369 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1370 {
1371 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1372
1373 if (minwidth < title_len)
1374 minwidth = title_len;
1375 }
1376
1377 if (minwidth > 0 && wp->w_width < minwidth)
1378 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001379 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001380 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001381 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001382 // some columns cut off on the right
1383 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001384 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001385 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001386 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001387 {
1388 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1389 if (wp->w_wincol < 0)
1390 wp->w_wincol = 0;
1391 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001392 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1393 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1394 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001395 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001396
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001397 // Right aligned: move to the right if needed.
1398 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001399 if (leftoff >= 0)
1400 wp->w_wincol = leftoff;
1401 else if (wp->w_popup_fixed)
1402 {
1403 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001404 // columns when displaying the window, minus left border and
1405 // padding.
1406 if (-leftoff > left_extra)
1407 wp->w_leftcol = -leftoff - left_extra;
1408 wp->w_width -= wp->w_leftcol;
1409 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001410 if (wp->w_width < 0)
1411 wp->w_width = 0;
1412 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001413 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001414
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001415 if (wp->w_p_wrap || (!wp->w_popup_fixed
1416 && (wp->w_popup_pos == POPPOS_TOPLEFT
1417 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001418 {
1419 int want_col = 0;
1420
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001421 // try to show the right border and any scrollbar
1422 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001423 if (want_col > 0 && wp->w_wincol > 0
1424 && wp->w_wincol + want_col >= Columns)
1425 {
1426 wp->w_wincol = Columns - want_col;
1427 if (wp->w_wincol < 0)
1428 wp->w_wincol = 0;
1429 }
1430 }
1431
Bram Moolenaar8d241042019-06-12 23:40:01 +02001432 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1433 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001434 if (minheight > 0 && wp->w_height < minheight)
1435 wp->w_height = minheight;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001436 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1437 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001438 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001439 if (wp->w_height > Rows - wp->w_winrow)
1440 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001441 if (wp->w_height != org_height)
1442 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001443
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001444 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001445 {
1446 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1447 if (wp->w_winrow < 0)
1448 wp->w_winrow = 0;
1449 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001450 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001451 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001452 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001453 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001454 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001455 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001456 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1457 {
1458 // Bottom aligned but does not fit, and less space on the other
1459 // side or "posinvert" is off: reduce height.
1460 wp->w_winrow = 0;
1461 wp->w_height = wantline - extra_height;
1462 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001463 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001464 // Not enough space and more space on the other side: make top
1465 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001466 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001467 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001468 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1469 || wp->w_popup_pos == POPPOS_TOPLEFT)
1470 {
1471 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1472 && wantline * 2 > Rows
1473 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001474 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001475 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001476 // make bottom aligned and recompute the height
1477 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001478 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001479 if (wp->w_winrow < 0)
1480 {
1481 wp->w_height += wp->w_winrow;
1482 wp->w_winrow = 0;
1483 }
1484 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001485 else
1486 wp->w_winrow = wantline - 1;
1487 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001488 if (wp->w_winrow >= Rows)
1489 wp->w_winrow = Rows - 1;
1490 else if (wp->w_winrow < 0)
1491 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001492
Bram Moolenaar17146962019-05-30 00:12:11 +02001493 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001494 if (win_valid(wp->w_popup_prop_win))
1495 {
1496 wp->w_popup_prop_changedtick =
1497 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1498 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1499 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001500
1501 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001502 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001503 if (org_winrow != wp->w_winrow
1504 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001505 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001506 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001507 || org_width != wp->w_width
1508 || org_height != wp->w_height)
1509 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001510 redraw_win_later(wp, NOT_VALID);
1511 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1512 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001513 popup_mask_refresh = TRUE;
1514 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001515}
1516
Bram Moolenaar17627312019-06-02 19:53:44 +02001517typedef enum
1518{
1519 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001520 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001521 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001522 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001523 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001524 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001525 TYPE_PREVIEW, // preview window
1526 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001527} create_type_T;
1528
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001529/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001530 * Make "buf" empty and set the contents to "text".
1531 * Used by popup_create() and popup_settext().
1532 */
1533 static void
1534popup_set_buffer_text(buf_T *buf, typval_T text)
1535{
1536 int lnum;
1537
1538 // Clear the buffer, then replace the lines.
1539 curbuf = buf;
1540 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1541 ml_delete(lnum, FALSE);
1542 curbuf = curwin->w_buffer;
1543
1544 // Add text to the buffer.
1545 if (text.v_type == VAR_STRING)
1546 {
1547 // just a string
1548 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1549 }
1550 else
1551 {
1552 list_T *l = text.vval.v_list;
1553
1554 if (l->lv_len > 0)
1555 {
1556 if (l->lv_first->li_tv.v_type == VAR_STRING)
1557 // list of strings
1558 add_popup_strings(buf, l);
1559 else
1560 // list of dictionaries
1561 add_popup_dicts(buf, l);
1562 }
1563 }
1564
1565 // delete the line that was in the empty buffer
1566 curbuf = buf;
1567 ml_delete(buf->b_ml.ml_line_count, FALSE);
1568 curbuf = curwin->w_buffer;
1569}
1570
1571/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001572 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1573 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001574 * Return FAIL if the parsing fails.
1575 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001576 static int
1577parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001578{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001579 char_u *p =
1580#ifdef FEAT_QUICKFIX
1581 !is_preview ? p_cpp :
1582#endif
1583 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001584
Bram Moolenaar258cef52019-08-21 17:29:29 +02001585 if (wp != NULL)
1586 wp->w_popup_flags &= ~POPF_INFO_MENU;
1587
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001588 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001589 {
1590 char_u *e, *dig;
1591 char_u *s = p;
1592 int x;
1593
1594 e = vim_strchr(p, ':');
1595 if (e == NULL || e[1] == NUL)
1596 return FAIL;
1597
1598 p = vim_strchr(e, ',');
1599 if (p == NULL)
1600 p = e + STRLEN(e);
1601 dig = e + 1;
1602 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001603
1604 if (STRNCMP(s, "height:", 7) == 0)
1605 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001606 if (dig != p)
1607 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001608 if (wp != NULL)
1609 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001610 if (is_preview)
1611 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001612 wp->w_maxheight = x;
1613 }
1614 }
1615 else if (STRNCMP(s, "width:", 6) == 0)
1616 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001617 if (dig != p)
1618 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001619 if (wp != NULL)
1620 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001621 if (is_preview)
1622 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001623 wp->w_maxwidth = x;
1624 }
1625 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001626 else if (STRNCMP(s, "highlight:", 10) == 0)
1627 {
1628 if (wp != NULL)
1629 {
1630 int c = *p;
1631
1632 *p = NUL;
1633 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1634 s + 10, OPT_FREE|OPT_LOCAL, 0);
1635 *p = c;
1636 }
1637 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001638 else if (STRNCMP(s, "border:", 7) == 0)
1639 {
1640 char_u *arg = s + 7;
1641 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1642 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1643 int i;
1644
1645 if (!on && !off)
1646 return FAIL;
1647 if (wp != NULL)
1648 {
1649 for (i = 0; i < 4; ++i)
1650 wp->w_popup_border[i] = on ? 1 : 0;
1651 if (off)
1652 // only show the X for close when there is a border
1653 wp->w_popup_close = POPCLOSE_NONE;
1654 }
1655 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001656 else if (STRNCMP(s, "align:", 6) == 0)
1657 {
1658 char_u *arg = s + 6;
1659 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1660 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1661
1662 if (!menu && !item)
1663 return FAIL;
1664 if (wp != NULL && menu)
1665 wp->w_popup_flags |= POPF_INFO_MENU;
1666 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001667 else
1668 return FAIL;
1669 }
1670 return OK;
1671}
1672
1673/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001674 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1675 * is not NULL.
1676 * Return FAIL if the parsing fails.
1677 */
1678 int
1679parse_previewpopup(win_T *wp)
1680{
1681 return parse_popup_option(wp, TRUE);
1682}
1683
1684/*
1685 * Parse the 'completepopup' option and apply the values to window "wp" if it
1686 * is not NULL.
1687 * Return FAIL if the parsing fails.
1688 */
1689 int
1690parse_completepopup(win_T *wp)
1691{
1692 return parse_popup_option(wp, FALSE);
1693}
1694
1695/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001696 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001697 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001698 */
1699 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001700popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001701{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001702 poppos_T ppt = POPPOS_NONE;
1703
1704 if (d != NULL)
1705 ppt = get_pos_entry(d, FALSE);
1706
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001708 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001709 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001710 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1711 }
1712 else
1713 {
1714 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1715 if (wp->w_wantline == 0) // cursor in first line
1716 {
1717 wp->w_wantline = 2;
1718 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1719 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1720 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001721 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001722
Bram Moolenaar79648732019-07-18 21:43:07 +02001723 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001724 if (wp->w_wantcol > Columns - width)
1725 {
1726 wp->w_wantcol = Columns - width;
1727 if (wp->w_wantcol < 1)
1728 wp->w_wantcol = 1;
1729 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001730
Bram Moolenaar79648732019-07-18 21:43:07 +02001731 popup_adjust_position(wp);
1732}
1733
1734/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001735 * Set w_wantline and w_wantcol for the a given screen position.
1736 * Caller must take care of running into the window border.
1737 */
1738 void
1739popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1740{
1741 wp->w_wantline = row;
1742 wp->w_wantcol = col;
1743 popup_adjust_position(wp);
1744}
1745
1746/*
1747 * Add a border and lef&right padding.
1748 */
1749 static void
1750add_border_left_right_padding(win_T *wp)
1751{
1752 int i;
1753
1754 for (i = 0; i < 4; ++i)
1755 {
1756 wp->w_popup_border[i] = 1;
1757 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1758 }
1759}
1760
1761/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001762 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001763 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001764 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001765 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001766 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001767 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001768popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001769{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001770 win_T *wp;
1771 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001772 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001773 int new_buffer;
1774 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001775 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001776 int nr;
1777 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001778
Bram Moolenaar79648732019-07-18 21:43:07 +02001779 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001780 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001781 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001782 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001783 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001784 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 if (buf == NULL)
1786 {
1787 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1788 return NULL;
1789 }
1790 }
1791 else if (!(argvars[0].v_type == VAR_STRING
1792 && argvars[0].vval.v_string != NULL)
1793 && !(argvars[0].v_type == VAR_LIST
1794 && argvars[0].vval.v_list != NULL))
1795 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001796 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001797 return NULL;
1798 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001799 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001800 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001801 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001802 return NULL;
1803 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001804 d = argvars[1].vval.v_dict;
1805 }
1806
1807 if (d != NULL)
1808 {
1809 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1810 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1811 else if (type == TYPE_NOTIFICATION)
1812 tabnr = -1; // notifications are global by default
1813 else
1814 tabnr = 0;
1815 if (tabnr > 0)
1816 {
1817 tp = find_tabpage(tabnr);
1818 if (tp == NULL)
1819 {
1820 semsg(_("E997: Tabpage not found: %d"), tabnr);
1821 return NULL;
1822 }
1823 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001824 }
1825
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001826 // Create the window and buffer.
1827 wp = win_alloc_popup_win();
1828 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001829 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001830 if (rettv != NULL)
1831 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001832 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001833 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001834
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001835 if (buf != NULL)
1836 {
1837 // use existing buffer
1838 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001839 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001840 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001841 buffer_ensure_loaded(buf);
1842 }
1843 else
1844 {
1845 // create a new buffer associated with the popup
1846 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001847 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848 if (buf == NULL)
1849 return NULL;
1850 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001851
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001852 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001853
Bram Moolenaar46451042019-08-24 15:50:46 +02001854 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001855 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001856 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001857 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001858 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001859 buf->b_p_ul = -1; // no undo
1860 buf->b_p_swf = FALSE; // no swap file
1861 buf->b_p_bl = FALSE; // unlisted buffer
1862 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001863
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001864 // Avoid that 'buftype' is reset when this buffer is entered.
1865 buf->b_p_initialized = TRUE;
1866 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001867 wp->w_p_wrap = TRUE; // 'wrap' is default on
1868 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001869
Bram Moolenaara3fce622019-06-20 02:31:49 +02001870 if (tp != NULL)
1871 {
1872 // popup on specified tab page
1873 wp->w_next = tp->tp_first_popupwin;
1874 tp->tp_first_popupwin = wp;
1875 }
1876 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001877 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001878 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001879 wp->w_next = curtab->tp_first_popupwin;
1880 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001881 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001882 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001883 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001884 win_T *prev = first_popupwin;
1885
1886 // Global popup: add at the end, so that it gets displayed on top of
1887 // older ones with the same zindex. Matters for notifications.
1888 if (first_popupwin == NULL)
1889 first_popupwin = wp;
1890 else
1891 {
1892 while (prev->w_next != NULL)
1893 prev = prev->w_next;
1894 prev->w_next = wp;
1895 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001896 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001897
Bram Moolenaar79648732019-07-18 21:43:07 +02001898 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001899 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001900
Bram Moolenaar79648732019-07-18 21:43:07 +02001901 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001902 {
1903 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001904 }
1905 if (type == TYPE_ATCURSOR)
1906 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001907 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001908 set_moved_values(wp);
1909 set_moved_columns(wp, FIND_STRING);
1910 }
1911
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001912 if (type == TYPE_BEVAL)
1913 {
1914 wp->w_popup_pos = POPPOS_BOTLEFT;
1915
1916 // by default use the mouse position
1917 wp->w_wantline = mouse_row;
1918 if (wp->w_wantline <= 0) // mouse on first line
1919 {
1920 wp->w_wantline = 2;
1921 wp->w_popup_pos = POPPOS_TOPLEFT;
1922 }
1923 wp->w_wantcol = mouse_col + 1;
1924 set_mousemoved_values(wp);
1925 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1926 }
1927
Bram Moolenaar33796b32019-06-08 16:01:13 +02001928 // set default values
1929 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001930 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001931
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001932 if (type == TYPE_NOTIFICATION)
1933 {
1934 win_T *twp, *nextwin;
1935 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001936
1937 // Try to not overlap with another global popup. Guess we need 3
1938 // more screen lines than buffer lines.
1939 wp->w_wantline = 1;
1940 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1941 {
1942 nextwin = twp->w_next;
1943 if (twp != wp
1944 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1945 && twp->w_winrow <= wp->w_wantline - 1 + height
1946 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1947 {
1948 // move to below this popup and restart the loop to check for
1949 // overlap with other popups
1950 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1951 nextwin = first_popupwin;
1952 }
1953 }
1954 if (wp->w_wantline + height > Rows)
1955 {
1956 // can't avoid overlap, put on top in the hope that message goes
1957 // away soon.
1958 wp->w_wantline = 1;
1959 }
1960
1961 wp->w_wantcol = 10;
1962 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001963 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001964 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001965 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001966 for (i = 0; i < 4; ++i)
1967 wp->w_popup_border[i] = 1;
1968 wp->w_popup_padding[1] = 1;
1969 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001970
1971 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001972 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001973 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1974 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001975 }
1976
Bram Moolenaara730e552019-06-16 19:05:31 +02001977 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001978 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001979 wp->w_popup_pos = POPPOS_CENTER;
1980 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001981 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001982 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001983 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001984 }
1985
Bram Moolenaara730e552019-06-16 19:05:31 +02001986 if (type == TYPE_MENU)
1987 {
1988 typval_T tv;
1989 callback_T callback;
1990
1991 tv.v_type = VAR_STRING;
1992 tv.vval.v_string = (char_u *)"popup_filter_menu";
1993 callback = get_callback(&tv);
1994 if (callback.cb_name != NULL)
1995 set_callback(&wp->w_filter_cb, &callback);
1996
1997 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001998 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001999 }
2000
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 if (type == TYPE_PREVIEW)
2002 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002003 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 wp->w_popup_close = POPCLOSE_BUTTON;
2005 for (i = 0; i < 4; ++i)
2006 wp->w_popup_border[i] = 1;
2007 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002008 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002009 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002010# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002011 if (type == TYPE_INFO)
2012 {
2013 wp->w_popup_pos = POPPOS_TOPLEFT;
2014 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2015 wp->w_popup_close = POPCLOSE_BUTTON;
2016 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002017 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002018 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002019# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002020
Bram Moolenaarae943152019-06-16 22:54:14 +02002021 for (i = 0; i < 4; ++i)
2022 VIM_CLEAR(wp->w_border_highlight[i]);
2023 for (i = 0; i < 8; ++i)
2024 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002025 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002026 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002027 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002028
Bram Moolenaar79648732019-07-18 21:43:07 +02002029 if (d != NULL)
2030 // Deal with options.
2031 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002032
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002033#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002034 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2035 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002036#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002037
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002038 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002039
2040 wp->w_vsep_width = 0;
2041
2042 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002043 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002044
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002045#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002046 // When running a terminal in the popup it becomes the current window.
2047 if (buf->b_term != NULL)
2048 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002049#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002050
Bram Moolenaara730e552019-06-16 19:05:31 +02002051 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002052}
2053
2054/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002055 * popup_clear()
2056 */
2057 void
2058f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2059{
2060 close_all_popups();
2061}
2062
2063/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002064 * popup_create({text}, {options})
2065 */
2066 void
2067f_popup_create(typval_T *argvars, typval_T *rettv)
2068{
Bram Moolenaar17627312019-06-02 19:53:44 +02002069 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002070}
2071
2072/*
2073 * popup_atcursor({text}, {options})
2074 */
2075 void
2076f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2077{
Bram Moolenaar17627312019-06-02 19:53:44 +02002078 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002079}
2080
2081/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002082 * popup_beval({text}, {options})
2083 */
2084 void
2085f_popup_beval(typval_T *argvars, typval_T *rettv)
2086{
2087 popup_create(argvars, rettv, TYPE_BEVAL);
2088}
2089
2090/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002091 * Invoke the close callback for window "wp" with value "result".
2092 * Careful: The callback may make "wp" invalid!
2093 */
2094 static void
2095invoke_popup_callback(win_T *wp, typval_T *result)
2096{
2097 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002098 typval_T argv[3];
2099
2100 argv[0].v_type = VAR_NUMBER;
2101 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2102
2103 if (result != NULL && result->v_type != VAR_UNKNOWN)
2104 copy_tv(result, &argv[1]);
2105 else
2106 {
2107 argv[1].v_type = VAR_NUMBER;
2108 argv[1].vval.v_number = 0;
2109 }
2110
2111 argv[2].v_type = VAR_UNKNOWN;
2112
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002113 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002114 if (result != NULL)
2115 clear_tv(&argv[1]);
2116 clear_tv(&rettv);
2117}
2118
2119/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002120 * Close popup "wp" and invoke any close callback for it.
2121 */
2122 static void
2123popup_close_and_callback(win_T *wp, typval_T *arg)
2124{
2125 int id = wp->w_id;
2126
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002127#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002128 if (wp == curwin && curbuf->b_term != NULL)
2129 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002130 win_T *owp;
2131
2132 // Closing popup window with a terminal: put focus back on the first
2133 // that works:
2134 // - another popup window with a terminal
2135 // - the previous window
2136 // - the first one.
2137 for (owp = first_popupwin; owp != NULL; owp = owp->w_next)
2138 if (owp != curwin && owp->w_buffer->b_term != NULL)
2139 break;
2140 if (owp != NULL)
2141 win_enter(owp, FALSE);
2142 else
2143 {
2144 for (owp = curtab->tp_first_popupwin; owp != NULL;
2145 owp = owp->w_next)
2146 if (owp != curwin && owp->w_buffer->b_term != NULL)
2147 break;
2148 if (owp != NULL)
2149 win_enter(owp, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002150 else if (win_valid(prevwin) && wp != prevwin)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002151 win_enter(prevwin, FALSE);
2152 else
2153 win_enter(firstwin, FALSE);
2154 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002155 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002156#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002157
Bram Moolenaar49540192019-12-11 19:34:54 +01002158 // Just in case a check higher up is missing.
2159 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2160 return;
2161
Bram Moolenaarcee52202020-03-11 14:19:58 +01002162 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002163 if (wp->w_close_cb.cb_name != NULL)
2164 // Careful: This may make "wp" invalid.
2165 invoke_popup_callback(wp, arg);
2166
2167 popup_close(id);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002168 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002169}
2170
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002171 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002172popup_close_with_retval(win_T *wp, int retval)
2173{
2174 typval_T res;
2175
2176 res.v_type = VAR_NUMBER;
2177 res.vval.v_number = retval;
2178 popup_close_and_callback(wp, &res);
2179}
2180
Bram Moolenaar3397f742019-06-02 18:40:06 +02002181/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002182 * Close popup "wp" because of a mouse click.
2183 */
2184 void
2185popup_close_for_mouse_click(win_T *wp)
2186{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002187 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002188}
2189
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002190 static void
2191check_mouse_moved(win_T *wp, win_T *mouse_wp)
2192{
2193 // Close the popup when all if these are true:
2194 // - the mouse is not on this popup
2195 // - "mousemoved" was used
2196 // - the mouse is no longer on the same screen row or the mouse column is
2197 // outside of the relevant text
2198 if (wp != mouse_wp
2199 && wp->w_popup_mouse_row != 0
2200 && (wp->w_popup_mouse_row != mouse_row
2201 || mouse_col < wp->w_popup_mouse_mincol
2202 || mouse_col > wp->w_popup_mouse_maxcol))
2203 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002204 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002205 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002206 }
2207}
2208
2209/*
2210 * Called when the mouse moved: may close a popup with "mousemoved".
2211 */
2212 void
2213popup_handle_mouse_moved(void)
2214{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002215 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002216 win_T *mouse_wp;
2217 int row = mouse_row;
2218 int col = mouse_col;
2219
2220 // find the window where the mouse is in
2221 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2222
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002223 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2224 {
2225 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002226 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002227 }
2228 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2229 {
2230 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002231 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002232 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002233}
2234
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002235/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002236 * In a filter: check if the typed key is a mouse event that is used for
2237 * dragging the popup.
2238 */
2239 static void
2240filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2241{
2242 int row = mouse_row;
2243 int col = mouse_col;
2244
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002245 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002246 && is_mouse_key(c)
2247 && (wp == popup_dragwin
2248 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2249 // do not consume the key, allow for dragging the popup
2250 rettv->vval.v_number = 0;
2251}
2252
Bram Moolenaara730e552019-06-16 19:05:31 +02002253/*
2254 * popup_filter_menu({text}, {options})
2255 */
2256 void
2257f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2258{
2259 int id = tv_get_number(&argvars[0]);
2260 win_T *wp = win_id2wp(id);
2261 char_u *key = tv_get_string(&argvars[1]);
2262 typval_T res;
2263 int c;
2264 linenr_T old_lnum;
2265
2266 // If the popup has been closed do not consume the key.
2267 if (wp == NULL)
2268 return;
2269
2270 c = *key;
2271 if (c == K_SPECIAL && key[1] != NUL)
2272 c = TO_SPECIAL(key[1], key[2]);
2273
2274 // consume all keys until done
2275 rettv->vval.v_number = 1;
2276 res.v_type = VAR_NUMBER;
2277
2278 old_lnum = wp->w_cursor.lnum;
2279 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2280 --wp->w_cursor.lnum;
2281 if ((c == 'j' || c == 'J' || c == K_DOWN)
2282 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2283 ++wp->w_cursor.lnum;
2284 if (old_lnum != wp->w_cursor.lnum)
2285 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002286 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002287 return;
2288 }
2289
2290 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2291 {
2292 // Cancelled, invoke callback with -1
2293 res.vval.v_number = -1;
2294 popup_close_and_callback(wp, &res);
2295 return;
2296 }
2297 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2298 {
2299 // Invoke callback with current index.
2300 res.vval.v_number = wp->w_cursor.lnum;
2301 popup_close_and_callback(wp, &res);
2302 return;
2303 }
2304
2305 filter_handle_drag(wp, c, rettv);
2306}
2307
2308/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002309 * popup_filter_yesno({text}, {options})
2310 */
2311 void
2312f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2313{
2314 int id = tv_get_number(&argvars[0]);
2315 win_T *wp = win_id2wp(id);
2316 char_u *key = tv_get_string(&argvars[1]);
2317 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002318 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002319
2320 // If the popup has been closed don't consume the key.
2321 if (wp == NULL)
2322 return;
2323
Bram Moolenaara730e552019-06-16 19:05:31 +02002324 c = *key;
2325 if (c == K_SPECIAL && key[1] != NUL)
2326 c = TO_SPECIAL(key[1], key[2]);
2327
Bram Moolenaara42d9452019-06-15 21:46:30 +02002328 // consume all keys until done
2329 rettv->vval.v_number = 1;
2330
Bram Moolenaara730e552019-06-16 19:05:31 +02002331 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002332 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002333 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002334 res.vval.v_number = 0;
2335 else
2336 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002337 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002338 return;
2339 }
2340
2341 // Invoke callback
2342 res.v_type = VAR_NUMBER;
2343 popup_close_and_callback(wp, &res);
2344}
2345
2346/*
2347 * popup_dialog({text}, {options})
2348 */
2349 void
2350f_popup_dialog(typval_T *argvars, typval_T *rettv)
2351{
2352 popup_create(argvars, rettv, TYPE_DIALOG);
2353}
2354
2355/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002356 * popup_menu({text}, {options})
2357 */
2358 void
2359f_popup_menu(typval_T *argvars, typval_T *rettv)
2360{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002361 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002362}
2363
2364/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002365 * popup_notification({text}, {options})
2366 */
2367 void
2368f_popup_notification(typval_T *argvars, typval_T *rettv)
2369{
2370 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2371}
2372
2373/*
2374 * Find the popup window with window-ID "id".
2375 * If the popup window does not exist NULL is returned.
2376 * If the window is not a popup window, and error message is given.
2377 */
2378 static win_T *
2379find_popup_win(int id)
2380{
2381 win_T *wp = win_id2wp(id);
2382
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002383 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002384 {
2385 semsg(_("E993: window %d is not a popup window"), id);
2386 return NULL;
2387 }
2388 return wp;
2389}
2390
2391/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002392 * popup_close({id})
2393 */
2394 void
2395f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2396{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002397 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002398 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002399
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002400 if (
2401# ifdef FEAT_TERMINAL
2402 // if the popup contains a terminal it will become hidden
2403 curbuf->b_term == NULL &&
2404# endif
2405 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002406 return;
2407
2408 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002409 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002410 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002411}
2412
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002413 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002414popup_hide(win_T *wp)
2415{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002416#ifdef FEAT_TERMINAL
2417 if (error_if_term_popup_window())
2418 return;
2419#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002420 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2421 {
2422 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002423 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002424 redraw_all_later(NOT_VALID);
2425 popup_mask_refresh = TRUE;
2426 }
2427}
2428
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002429/*
2430 * popup_hide({id})
2431 */
2432 void
2433f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2434{
2435 int id = (int)tv_get_number(argvars);
2436 win_T *wp = find_popup_win(id);
2437
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002438 if (wp != NULL)
2439 popup_hide(wp);
2440}
2441
2442 void
2443popup_show(win_T *wp)
2444{
2445 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002446 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002447 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002448 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002449 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002450 }
2451}
2452
2453/*
2454 * popup_show({id})
2455 */
2456 void
2457f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2458{
2459 int id = (int)tv_get_number(argvars);
2460 win_T *wp = find_popup_win(id);
2461
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002462 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002463 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002464 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002465#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002466 if (wp->w_popup_flags & POPF_INFO)
2467 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002468#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002469 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002470}
2471
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002472/*
2473 * popup_settext({id}, {text})
2474 */
2475 void
2476f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2477{
2478 int id = (int)tv_get_number(&argvars[0]);
2479 win_T *wp = find_popup_win(id);
2480
2481 if (wp != NULL)
2482 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002483 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2484 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2485 else
2486 {
2487 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002488 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002489 popup_adjust_position(wp);
2490 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002491 }
2492}
2493
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002494 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002495popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002496{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002497 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002498 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002499 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002500 clear_cmdline = TRUE;
2501 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002502
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002503 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002504 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002505}
2506
Bram Moolenaar82106932020-03-13 14:34:38 +01002507 static void
2508error_for_popup_window(void)
2509{
2510 emsg(_("E994: Not allowed in a popup window"));
2511}
2512
2513 int
2514error_if_popup_window(int also_with_term UNUSED)
2515{
2516 // win_execute() may set "curwin" to a popup window temporarily, but many
2517 // commands are disallowed then. When a terminal runs in the popup most
2518 // things are allowed. When a terminal is finished it can be closed.
2519 if (WIN_IS_POPUP(curwin)
2520# ifdef FEAT_TERMINAL
2521 && (also_with_term || curbuf->b_term == NULL)
2522 && !term_is_finished(curbuf)
2523# endif
2524 )
2525 {
2526 error_for_popup_window();
2527 return TRUE;
2528 }
2529 return FALSE;
2530}
2531
Bram Moolenaarec583842019-05-26 14:11:23 +02002532/*
2533 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002534 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002535 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002536 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002537popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002538{
2539 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002540 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002541 win_T *prev = NULL;
2542
Bram Moolenaarec583842019-05-26 14:11:23 +02002543 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002544 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002545 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002546 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002547 if (wp == curwin)
2548 {
Bram Moolenaar82106932020-03-13 14:34:38 +01002549 error_for_popup_window();
Bram Moolenaarcee52202020-03-11 14:19:58 +01002550 return;
2551 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002552 if (prev == NULL)
2553 first_popupwin = wp->w_next;
2554 else
2555 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002556 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002557 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002558 }
2559
Bram Moolenaarec583842019-05-26 14:11:23 +02002560 // go through tab-local popups
2561 FOR_ALL_TABPAGES(tp)
2562 popup_close_tabpage(tp, id);
2563}
2564
2565/*
2566 * Close a popup window with Window-id "id" in tabpage "tp".
2567 */
2568 void
2569popup_close_tabpage(tabpage_T *tp, int id)
2570{
2571 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002572 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002573 win_T *prev = NULL;
2574
Bram Moolenaarec583842019-05-26 14:11:23 +02002575 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2576 if (wp->w_id == id)
2577 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002578 if (wp == curwin)
2579 {
Bram Moolenaar82106932020-03-13 14:34:38 +01002580 error_for_popup_window();
Bram Moolenaarcee52202020-03-11 14:19:58 +01002581 return;
2582 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002583 if (prev == NULL)
2584 *root = wp->w_next;
2585 else
2586 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002587 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002588 return;
2589 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002590}
2591
2592 void
2593close_all_popups(void)
2594{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01002595 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002596 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002597 while (first_popupwin != NULL)
2598 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002599 while (curtab->tp_first_popupwin != NULL)
2600 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002601}
2602
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002603/*
2604 * popup_move({id}, {options})
2605 */
2606 void
2607f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2608{
Bram Moolenaarae943152019-06-16 22:54:14 +02002609 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002610 int id = (int)tv_get_number(argvars);
2611 win_T *wp = find_popup_win(id);
2612
2613 if (wp == NULL)
2614 return; // invalid {id}
2615
2616 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2617 {
2618 emsg(_(e_dictreq));
2619 return;
2620 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002621 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002622
Bram Moolenaarae943152019-06-16 22:54:14 +02002623 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002624
2625 if (wp->w_winrow + wp->w_height >= cmdline_row)
2626 clear_cmdline = TRUE;
2627 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002628}
2629
Bram Moolenaarbc133542019-05-29 20:26:46 +02002630/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002631 * popup_setoptions({id}, {options})
2632 */
2633 void
2634f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2635{
2636 dict_T *dict;
2637 int id = (int)tv_get_number(argvars);
2638 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002639 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002640
2641 if (wp == NULL)
2642 return; // invalid {id}
2643
2644 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2645 {
2646 emsg(_(e_dictreq));
2647 return;
2648 }
2649 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002650 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002651
2652 apply_move_options(wp, dict);
2653 apply_general_options(wp, dict);
2654
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002655 if (old_firstline != wp->w_firstline)
2656 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002657 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002658 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002659 popup_adjust_position(wp);
2660}
2661
2662/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002663 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002664 */
2665 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002666f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002667{
2668 dict_T *dict;
2669 int id = (int)tv_get_number(argvars);
2670 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002671 int top_extra;
2672 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002673
2674 if (rettv_dict_alloc(rettv) == OK)
2675 {
2676 if (wp == NULL)
2677 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002678 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002679 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2680
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002681 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002682 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002683 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002684
Bram Moolenaarbc133542019-05-29 20:26:46 +02002685 dict_add_number(dict, "line", wp->w_winrow + 1);
2686 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002687 dict_add_number(dict, "width", wp->w_width + left_extra
2688 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2689 dict_add_number(dict, "height", wp->w_height + top_extra
2690 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002691
2692 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2693 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2694 dict_add_number(dict, "core_width", wp->w_width);
2695 dict_add_number(dict, "core_height", wp->w_height);
2696
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002697 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002698 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002699 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002700 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002701 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002702
2703 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002704 }
2705}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002706/*
2707 * popup_locate({row}, {col})
2708 */
2709 void
2710f_popup_locate(typval_T *argvars, typval_T *rettv)
2711{
2712 int row = tv_get_number(&argvars[0]) - 1;
2713 int col = tv_get_number(&argvars[1]) - 1;
2714 win_T *wp;
2715
2716 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002717 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002718 rettv->vval.v_number = wp->w_id;
2719}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002720
2721/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002722 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2723 */
2724 static void
2725get_padding_border(dict_T *dict, int *array, char *name)
2726{
2727 list_T *list;
2728 int i;
2729
2730 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2731 return;
2732
2733 list = list_alloc();
2734 if (list != NULL)
2735 {
2736 dict_add_list(dict, name, list);
2737 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2738 for (i = 0; i < 4; ++i)
2739 list_append_number(list, array[i]);
2740 }
2741}
2742
2743/*
2744 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2745 */
2746 static void
2747get_borderhighlight(dict_T *dict, win_T *wp)
2748{
2749 list_T *list;
2750 int i;
2751
2752 for (i = 0; i < 4; ++i)
2753 if (wp->w_border_highlight[i] != NULL)
2754 break;
2755 if (i == 4)
2756 return;
2757
2758 list = list_alloc();
2759 if (list != NULL)
2760 {
2761 dict_add_list(dict, "borderhighlight", list);
2762 for (i = 0; i < 4; ++i)
2763 list_append_string(list, wp->w_border_highlight[i], -1);
2764 }
2765}
2766
2767/*
2768 * For popup_getoptions(): add a "borderchars" entry to "dict".
2769 */
2770 static void
2771get_borderchars(dict_T *dict, win_T *wp)
2772{
2773 list_T *list;
2774 int i;
2775 char_u buf[NUMBUFLEN];
2776 int len;
2777
2778 for (i = 0; i < 8; ++i)
2779 if (wp->w_border_char[i] != 0)
2780 break;
2781 if (i == 8)
2782 return;
2783
2784 list = list_alloc();
2785 if (list != NULL)
2786 {
2787 dict_add_list(dict, "borderchars", list);
2788 for (i = 0; i < 8; ++i)
2789 {
2790 len = mb_char2bytes(wp->w_border_char[i], buf);
2791 list_append_string(list, buf, len);
2792 }
2793 }
2794}
2795
2796/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002797 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002798 */
2799 static void
2800get_moved_list(dict_T *dict, win_T *wp)
2801{
2802 list_T *list;
2803
2804 list = list_alloc();
2805 if (list != NULL)
2806 {
2807 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002808 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002809 list_append_number(list, wp->w_popup_mincol);
2810 list_append_number(list, wp->w_popup_maxcol);
2811 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002812 list = list_alloc();
2813 if (list != NULL)
2814 {
2815 dict_add_list(dict, "mousemoved", list);
2816 list_append_number(list, wp->w_popup_mouse_row);
2817 list_append_number(list, wp->w_popup_mouse_mincol);
2818 list_append_number(list, wp->w_popup_mouse_maxcol);
2819 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002820}
2821
2822/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002823 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002824 */
2825 void
2826f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2827{
2828 dict_T *dict;
2829 int id = (int)tv_get_number(argvars);
2830 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002831 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002832 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002833
2834 if (rettv_dict_alloc(rettv) == OK)
2835 {
2836 if (wp == NULL)
2837 return;
2838
2839 dict = rettv->vval.v_dict;
2840 dict_add_number(dict, "line", wp->w_wantline);
2841 dict_add_number(dict, "col", wp->w_wantcol);
2842 dict_add_number(dict, "minwidth", wp->w_minwidth);
2843 dict_add_number(dict, "minheight", wp->w_minheight);
2844 dict_add_number(dict, "maxheight", wp->w_maxheight);
2845 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002846 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002847 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002848 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002849 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002850 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2851 {
2852 proptype_T *pt = text_prop_type_by_id(
2853 wp->w_popup_prop_win->w_buffer,
2854 wp->w_popup_prop_type);
2855
2856 if (pt != NULL)
2857 dict_add_string(dict, "textprop", pt->pt_name);
2858 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2859 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2860 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002861 dict_add_string(dict, "title", wp->w_popup_title);
2862 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002863 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002864 dict_add_number(dict, "mapping",
2865 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002866 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002867 dict_add_number(dict, "posinvert",
2868 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002869 dict_add_number(dict, "cursorline",
2870 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002871 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002872 if (wp->w_scrollbar_highlight != NULL)
2873 dict_add_string(dict, "scrollbarhighlight",
2874 wp->w_scrollbar_highlight);
2875 if (wp->w_thumb_highlight != NULL)
2876 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002877
Bram Moolenaara3fce622019-06-20 02:31:49 +02002878 // find the tabpage that holds this popup
2879 i = 1;
2880 FOR_ALL_TABPAGES(tp)
2881 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002882 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002883
Bram Moolenaar1824f452019-10-02 23:06:46 +02002884 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2885 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002886 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002887 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002888 break;
2889 ++i;
2890 }
2891 if (tp == NULL)
2892 i = -1; // must be global
2893 else if (tp == curtab)
2894 i = 0;
2895 dict_add_number(dict, "tabpage", i);
2896
Bram Moolenaarae943152019-06-16 22:54:14 +02002897 get_padding_border(dict, wp->w_popup_padding, "padding");
2898 get_padding_border(dict, wp->w_popup_border, "border");
2899 get_borderhighlight(dict, wp);
2900 get_borderchars(dict, wp);
2901 get_moved_list(dict, wp);
2902
2903 if (wp->w_filter_cb.cb_name != NULL)
2904 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2905 if (wp->w_close_cb.cb_name != NULL)
2906 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002907
2908 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2909 ++i)
2910 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2911 {
2912 dict_add_string(dict, "pos",
2913 (char_u *)poppos_entries[i].pp_name);
2914 break;
2915 }
2916
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002917 dict_add_string(dict, "close", (char_u *)(
2918 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2919 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2920
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002921# if defined(FEAT_TIMERS)
2922 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2923 ? (long)wp->w_popup_timer->tr_interval : 0L);
2924# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002925 }
2926}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002927
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002928# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002929/*
2930 * Return TRUE if the current window is running a terminal in a popup window.
2931 * Return FALSE when the job has ended.
2932 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002933 int
2934error_if_term_popup_window()
2935{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002936 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
2937 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002938 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01002939 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002940 return TRUE;
2941 }
2942 return FALSE;
2943}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002944# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002945
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002946/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002947 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002948 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002949 * Each calling function should use a different flag, see the list at
2950 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002951 */
2952 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002953popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002954{
2955 win_T *wp;
2956
2957 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002958 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002959 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002960 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002961}
2962
2963/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002964 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002965 * Must have called popup_reset_handled() first.
2966 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2967 * popup with the highest zindex.
2968 */
2969 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002970find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002971{
2972 win_T *wp;
2973 win_T *found_wp;
2974 int found_zindex;
2975
2976 found_zindex = lowest ? INT_MAX : 0;
2977 found_wp = NULL;
2978 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002979 if ((wp->w_popup_handled & handled_flag) == 0
2980 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002981 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002982 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002983 {
2984 found_zindex = wp->w_zindex;
2985 found_wp = wp;
2986 }
2987 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002988 if ((wp->w_popup_handled & handled_flag) == 0
2989 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002990 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002991 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002992 {
2993 found_zindex = wp->w_zindex;
2994 found_wp = wp;
2995 }
2996
2997 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002998 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002999 return found_wp;
3000}
3001
3002/*
3003 * Invoke the filter callback for window "wp" with typed character "c".
3004 * Uses the global "mod_mask" for modifiers.
3005 * Returns the return value of the filter.
3006 * Careful: The filter may make "wp" invalid!
3007 */
3008 static int
3009invoke_popup_filter(win_T *wp, int c)
3010{
3011 int res;
3012 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003013 typval_T argv[3];
3014 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003015 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003016
Bram Moolenaara42d9452019-06-15 21:46:30 +02003017 // Emergency exit: CTRL-C closes the popup.
3018 if (c == Ctrl_C)
3019 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003020 int save_got_int = got_int;
3021
3022 // Reset got_int to avoid the callback isn't called.
3023 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003024 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003025 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003026 return 1;
3027 }
3028
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003029 argv[0].v_type = VAR_NUMBER;
3030 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3031
3032 // Convert the number to a string, so that the function can use:
3033 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003034 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003035 argv[1].v_type = VAR_STRING;
3036 argv[1].vval.v_string = vim_strsave(buf);
3037
3038 argv[2].v_type = VAR_UNKNOWN;
3039
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003040 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003041 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003042 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003043 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003044 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003045
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003046 vim_free(argv[1].vval.v_string);
3047 clear_tv(&rettv);
3048 return res;
3049}
3050
3051/*
3052 * Called when "c" was typed: invoke popup filter callbacks.
3053 * Returns TRUE when the character was consumed,
3054 */
3055 int
3056popup_do_filter(int c)
3057{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003058 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003059 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003060 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003061 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003062 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003063 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003064
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003065#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003066 // Popup window with terminal always gets focus.
3067 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3068 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003069#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003070
Bram Moolenaar934470e2019-09-01 23:27:05 +02003071 if (recursive)
3072 return FALSE;
3073 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003074
Bram Moolenaarf6396232019-08-24 19:36:00 +02003075 if (c == K_LEFTMOUSE)
3076 {
3077 int row = mouse_row;
3078 int col = mouse_col;
3079
3080 wp = mouse_find_win(&row, &col, FIND_POPUP);
3081 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003082 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003083 }
3084
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003085 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003086 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003087 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003088 if (wp->w_filter_cb.cb_name != NULL
3089 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003090 res = invoke_popup_filter(wp, c);
3091
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003092 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003093 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02003094 recursive = FALSE;
3095 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003096 return res;
3097}
3098
Bram Moolenaar3397f742019-06-02 18:40:06 +02003099/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003100 * Return TRUE if there is a popup visible with a filter callback and the
3101 * "mapping" property off.
3102 */
3103 int
3104popup_no_mapping(void)
3105{
3106 int round;
3107 win_T *wp;
3108
3109 for (round = 1; round <= 2; ++round)
3110 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3111 wp != NULL; wp = wp->w_next)
3112 if (wp->w_filter_cb.cb_name != NULL
3113 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3114 return TRUE;
3115 return FALSE;
3116}
3117
3118/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003119 * Called when the cursor moved: check if any popup needs to be closed if the
3120 * cursor moved far enough.
3121 */
3122 void
3123popup_check_cursor_pos()
3124{
3125 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003126
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003127 popup_reset_handled(POPUP_HANDLED_3);
3128 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003129 if (wp->w_popup_curwin != NULL
3130 && (curwin != wp->w_popup_curwin
3131 || curwin->w_cursor.lnum != wp->w_popup_lnum
3132 || curwin->w_cursor.col < wp->w_popup_mincol
3133 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003134 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003135}
3136
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003137/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003138 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003139 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003140 static void
3141popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003142{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003143 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003144 char_u *cells;
3145 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003146
3147 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003148 return;
3149 if (wp->w_popup_mask_cells != NULL
3150 && wp->w_popup_mask_height == height
3151 && wp->w_popup_mask_width == width)
3152 return; // cache is still valid
3153
3154 vim_free(wp->w_popup_mask_cells);
3155 wp->w_popup_mask_cells = alloc_clear(width * height);
3156 if (wp->w_popup_mask_cells == NULL)
3157 return;
3158 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003159
3160 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3161 {
3162 int cols, cole;
3163 int lines, linee;
3164
3165 li = lio->li_tv.vval.v_list->lv_first;
3166 cols = tv_get_number(&li->li_tv);
3167 if (cols < 0)
3168 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003169 li = li->li_next;
3170 cole = tv_get_number(&li->li_tv);
3171 if (cole < 0)
3172 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003173 li = li->li_next;
3174 lines = tv_get_number(&li->li_tv);
3175 if (lines < 0)
3176 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003177 li = li->li_next;
3178 linee = tv_get_number(&li->li_tv);
3179 if (linee < 0)
3180 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003181
3182 for (row = lines - 1; row < linee && row < height; ++row)
3183 for (col = cols - 1; col < cole && col < width; ++col)
3184 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003185 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003186}
3187
3188/*
3189 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3190 * "col" and "line" are screen coordinates.
3191 */
3192 static int
3193popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3194{
3195 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3196 int line = screenline - wp->w_winrow;
3197
3198 return col >= 0 && col < width
3199 && line >= 0 && line < height
3200 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003201}
3202
3203/*
3204 * Set flags in popup_transparent[] for window "wp" to "val".
3205 */
3206 static void
3207update_popup_transparent(win_T *wp, int val)
3208{
3209 if (wp->w_popup_mask != NULL)
3210 {
3211 int width = popup_width(wp);
3212 int height = popup_height(wp);
3213 listitem_T *lio, *li;
3214 int cols, cole;
3215 int lines, linee;
3216 int col, line;
3217
3218 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3219 {
3220 li = lio->li_tv.vval.v_list->lv_first;
3221 cols = tv_get_number(&li->li_tv);
3222 if (cols < 0)
3223 cols = width + cols + 1;
3224 li = li->li_next;
3225 cole = tv_get_number(&li->li_tv);
3226 if (cole < 0)
3227 cole = width + cole + 1;
3228 li = li->li_next;
3229 lines = tv_get_number(&li->li_tv);
3230 if (lines < 0)
3231 lines = height + lines + 1;
3232 li = li->li_next;
3233 linee = tv_get_number(&li->li_tv);
3234 if (linee < 0)
3235 linee = height + linee + 1;
3236
3237 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003238 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003239 if (cols < 0)
3240 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003241 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003242 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003243 if (lines < 0)
3244 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003245 for (line = lines; line < linee
3246 && line + wp->w_winrow < screen_Rows; ++line)
3247 for (col = cols; col < cole
3248 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003249 popup_transparent[(line + wp->w_winrow) * screen_Columns
3250 + col + wp->w_wincol] = val;
3251 }
3252 }
3253}
3254
3255/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003256 * Only called when popup window "wp" is hidden: If the window is positioned
3257 * next to a text property, and it is now visible, then unhide the popup.
3258 * We don't check if visible popups become hidden, that is done in
3259 * popup_adjust_position().
3260 * Return TRUE if the popup became unhidden.
3261 */
3262 static int
3263check_popup_unhidden(win_T *wp)
3264{
3265 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3266 {
3267 textprop_T prop;
3268 linenr_T lnum;
3269
3270 if (find_visible_prop(wp->w_popup_prop_win,
3271 wp->w_popup_prop_type, wp->w_popup_prop_id,
3272 &prop, &lnum) == OK)
3273 {
3274 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003275 wp->w_popup_prop_topline = 0; // force repositioning
3276 return TRUE;
3277 }
3278 }
3279 return FALSE;
3280}
3281
3282/*
3283 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3284 * That is when the buffer in the popup was changed, or the popup is following
3285 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003286 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003287 */
3288 static int
3289popup_need_position_adjust(win_T *wp)
3290{
3291 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3292 return TRUE;
3293 if (win_valid(wp->w_popup_prop_win))
3294 return wp->w_popup_prop_changedtick
3295 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003296 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3297 || ((wp->w_popup_flags & POPF_CURSORLINE)
3298 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003299 return FALSE;
3300}
3301
3302/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003303 * Update "popup_mask" if needed.
3304 * Also recomputes the popup size and positions.
3305 * Also updates "popup_visible".
3306 * Also marks window lines for redrawing.
3307 */
3308 void
3309may_update_popup_mask(int type)
3310{
3311 win_T *wp;
3312 short *mask;
3313 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003314 int redraw_all_popups = FALSE;
3315 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003316
3317 // Need to recompute when switching tabs.
3318 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3319 // (such as the screen size) must have changed.
3320 if (popup_mask_tab != curtab || type >= NOT_VALID)
3321 {
3322 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003323 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003324 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003325
3326 // Check if any popup window buffer has changed and if any popup connected
3327 // to a text property has become visible.
3328 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3329 if (wp->w_popup_flags & POPF_HIDDEN)
3330 popup_mask_refresh |= check_popup_unhidden(wp);
3331 else if (popup_need_position_adjust(wp))
3332 popup_mask_refresh = TRUE;
3333 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3334 if (wp->w_popup_flags & POPF_HIDDEN)
3335 popup_mask_refresh |= check_popup_unhidden(wp);
3336 else if (popup_need_position_adjust(wp))
3337 popup_mask_refresh = TRUE;
3338
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003339 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003340 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003341
3342 // Need to update the mask, something has changed.
3343 popup_mask_refresh = FALSE;
3344 popup_mask_tab = curtab;
3345 popup_visible = FALSE;
3346
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003347 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003348 // If redrawing only what is needed, update "popup_mask_next" and then
3349 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003350 redrawing_all_win = TRUE;
3351 FOR_ALL_WINDOWS(wp)
3352 if (wp->w_redr_type < SOME_VALID)
3353 redrawing_all_win = FALSE;
3354 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003355 mask = popup_mask;
3356 else
3357 mask = popup_mask_next;
3358 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3359
3360 // Find the window with the lowest zindex that hasn't been handled yet,
3361 // so that the window with a higher zindex overwrites the value in
3362 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003363 popup_reset_handled(POPUP_HANDLED_4);
3364 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003365 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003366 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003367 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003368
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003369 popup_visible = TRUE;
3370
Bram Moolenaar12034e22019-08-25 22:25:02 +02003371 // Recompute the position if the text changed. It may make the popup
3372 // hidden if it's attach to a text property that is no longer visible.
3373 if (redraw_all_popups || popup_need_position_adjust(wp))
3374 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003375 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003376 if (wp->w_popup_flags & POPF_HIDDEN)
3377 continue;
3378 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003379
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003380 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003381 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003382 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003383 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003384 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003385 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003386 col < wp->w_wincol + width - wp->w_popup_leftoff
3387 && col < screen_Columns; ++col)
3388 if (wp->w_popup_mask_cells == NULL
3389 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003390 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003391 }
3392
3393 // Only check which lines are to be updated if not already
3394 // updating all lines.
3395 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003396 {
3397 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3398 win_T *prev_wp = NULL;
3399
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003400 for (line = 0; line < screen_Rows; ++line)
3401 {
3402 int col_done = 0;
3403
3404 for (col = 0; col < screen_Columns; ++col)
3405 {
3406 int off = line * screen_Columns + col;
3407
3408 if (popup_mask[off] != popup_mask_next[off])
3409 {
3410 popup_mask[off] = popup_mask_next[off];
3411
3412 if (line >= cmdline_row)
3413 {
3414 // the command line needs to be cleared if text below
3415 // the popup is now visible.
3416 if (!msg_scrolled && popup_mask_next[off] == 0)
3417 clear_cmdline = TRUE;
3418 }
3419 else if (col >= col_done)
3420 {
3421 linenr_T lnum;
3422 int line_cp = line;
3423 int col_cp = col;
3424
3425 // The screen position "line" / "col" needs to be
3426 // redrawn. Figure out what window that is and update
3427 // w_redraw_top and w_redr_bot. Only needs to be done
3428 // once for each window line.
3429 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3430 if (wp != NULL)
3431 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003432 if (wp != prev_wp)
3433 {
3434 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3435 prev_wp = wp;
3436 }
3437
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003438 if (line_cp >= wp->w_height)
3439 // In (or below) status line
3440 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003441 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003442 {
3443 // compute the position in the buffer line from
3444 // the position in the window
3445 mouse_comp_pos(wp, &line_cp, &col_cp,
3446 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003447 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003448 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003449
3450 // This line is going to be redrawn, no need to
3451 // check until the right side of the window.
3452 col_done = wp->w_wincol + wp->w_width - 1;
3453 }
3454 }
3455 }
3456 }
3457 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003458
3459 vim_free(plines_cache);
3460 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003461}
3462
3463/*
3464 * Return a string of "len" spaces in IObuff.
3465 */
3466 static char_u *
3467get_spaces(int len)
3468{
3469 vim_memset(IObuff, ' ', (size_t)len);
3470 IObuff[len] = NUL;
3471 return IObuff;
3472}
3473
3474/*
3475 * Update popup windows. They are drawn on top of normal windows.
3476 * "win_update" is called for each popup window, lowest zindex first.
3477 */
3478 void
3479update_popups(void (*win_update)(win_T *wp))
3480{
3481 win_T *wp;
3482 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003483 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003484 int total_width;
3485 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003486 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003487 int popup_attr;
3488 int border_attr[4];
3489 int border_char[8];
3490 char_u buf[MB_MAXBYTES];
3491 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003492 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003493 int padcol = 0;
3494 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003495 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003496 int sb_thumb_top = 0;
3497 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003498 int attr_scroll = 0;
3499 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003500
3501 // Find the window with the lowest zindex that hasn't been updated yet,
3502 // so that the window with a higher zindex is drawn later, thus goes on
3503 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003504 popup_reset_handled(POPUP_HANDLED_5);
3505 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003506 {
3507 // This drawing uses the zindex of the popup window, so that it's on
3508 // top of the text but doesn't draw when another popup with higher
3509 // zindex is on top of the character.
3510 screen_zindex = wp->w_zindex;
3511
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003512 // Set flags in popup_transparent[] for masked cells.
3513 update_popup_transparent(wp, 1);
3514
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003515 // adjust w_winrow and w_wincol for border and padding, since
3516 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003517 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003518 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3519 - wp->w_popup_leftoff;
3520 if (wp->w_wincol + left_extra < 0)
3521 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003522 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003523 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003524
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003525 // Draw the popup text, unless it's off screen.
3526 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003527 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003528 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003529
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003530 // move the cursor into the visible lines, otherwise executing
3531 // commands with win_execute() may cause the text to jump.
3532 if (wp->w_cursor.lnum < wp->w_topline)
3533 wp->w_cursor.lnum = wp->w_topline;
3534 else if (wp->w_cursor.lnum >= wp->w_botline)
3535 wp->w_cursor.lnum = wp->w_botline - 1;
3536 }
3537
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003538 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003539 wp->w_wincol -= left_extra;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003540 // cursor position matters in terminal in job mode
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003541#ifdef FEAT_TERMINAL
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003542 if (wp != curwin || !term_in_normal_mode())
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003543#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003544 {
3545 wp->w_wrow += top_off;
3546 wp->w_wcol += left_extra;
3547 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003548
Bram Moolenaard529ba52019-07-02 23:13:53 +02003549 total_width = popup_width(wp);
3550 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003551 popup_attr = get_wcr_attr(wp);
3552
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003553 if (wp->w_winrow + total_height > cmdline_row)
3554 wp->w_popup_flags |= POPF_ON_CMDLINE;
3555 else
3556 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3557
3558
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003559 // We can only use these line drawing characters when 'encoding' is
3560 // "utf-8" and 'ambiwidth' is "single".
3561 if (enc_utf8 && *p_ambw == 's')
3562 {
3563 border_char[0] = border_char[2] = 0x2550;
3564 border_char[1] = border_char[3] = 0x2551;
3565 border_char[4] = 0x2554;
3566 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003567 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3568 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003569 border_char[7] = 0x255a;
3570 }
3571 else
3572 {
3573 border_char[0] = border_char[2] = '-';
3574 border_char[1] = border_char[3] = '|';
3575 for (i = 4; i < 8; ++i)
3576 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003577 if (wp->w_popup_flags & POPF_RESIZE)
3578 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003579 }
3580 for (i = 0; i < 8; ++i)
3581 if (wp->w_border_char[i] != 0)
3582 border_char[i] = wp->w_border_char[i];
3583
3584 for (i = 0; i < 4; ++i)
3585 {
3586 border_attr[i] = popup_attr;
3587 if (wp->w_border_highlight[i] != NULL)
3588 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3589 }
3590
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003591 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003592 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003593 if (wp->w_popup_border[0] > 0)
3594 {
3595 // top border
3596 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003597 wincol < 0 ? 0 : wincol, wincol + total_width,
3598 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003599 ? border_char[4] : border_char[0],
3600 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003601 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003602 {
3603 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3604 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003605 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003606 }
3607 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003608 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3609 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003610
Bram Moolenaard529ba52019-07-02 23:13:53 +02003611 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3612 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003613 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003614 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3615 - wp->w_has_scrollbar;
3616 if (padcol < 0)
3617 {
3618 padwidth += padcol;
3619 padcol = 0;
3620 }
3621 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003622 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623 {
3624 // top padding
3625 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003626 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003627 ' ', ' ', popup_attr);
3628 }
3629
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003630 // Title goes on top of border or padding.
3631 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003632 {
3633 int len = (int)STRLEN(wp->w_popup_title) + 1;
3634 char_u *title = alloc(len);
3635
3636 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3637 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003638 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003639 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003640 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003641
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003642 // Compute scrollbar thumb position and size.
3643 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003644 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003645 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3646 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003647
Bram Moolenaar076d9882019-09-14 22:23:29 +02003648 sb_thumb_height = (height * height + linecount / 2) / linecount;
3649 if (wp->w_topline > 1 && sb_thumb_height == height)
3650 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003651 if (sb_thumb_height == 0)
3652 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003653 if (linecount <= wp->w_height)
3654 // it just fits, avoid divide by zero
3655 sb_thumb_top = 0;
3656 else
3657 sb_thumb_top = (wp->w_topline - 1
3658 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003659 * (wp->w_height - sb_thumb_height)
3660 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003661 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3662 sb_thumb_top = 1; // show it's scrolled
3663
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003664 if (wp->w_scrollbar_highlight != NULL)
3665 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3666 else
3667 attr_scroll = highlight_attr[HLF_PSB];
3668 if (wp->w_thumb_highlight != NULL)
3669 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3670 else
3671 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003672 }
3673
3674 for (i = wp->w_popup_border[0];
3675 i < total_height - wp->w_popup_border[2]; ++i)
3676 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003677 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003678 // left and right padding only needed next to the body
3679 int do_padding =
3680 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3681 && i < total_height - wp->w_popup_border[2]
3682 - wp->w_popup_padding[2];
3683
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003684 row = wp->w_winrow + i;
3685
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003686 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003687 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003688 {
3689 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003690 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003691 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003692 if (do_padding && wp->w_popup_padding[3] > 0)
3693 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003694 int col = wincol + wp->w_popup_border[3];
3695
Bram Moolenaard529ba52019-07-02 23:13:53 +02003696 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003697 pad_left = wp->w_popup_padding[3];
3698 if (col < 0)
3699 {
3700 pad_left += col;
3701 col = 0;
3702 }
3703 if (pad_left > 0)
3704 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3705 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003706 // scrollbar
3707 if (wp->w_has_scrollbar)
3708 {
3709 int line = i - top_off;
3710 int scroll_col = wp->w_wincol + total_width - 1
3711 - wp->w_popup_border[1];
3712
3713 if (line >= 0 && line < wp->w_height)
3714 screen_putchar(' ', row, scroll_col,
3715 line >= sb_thumb_top
3716 && line < sb_thumb_top + sb_thumb_height
3717 ? attr_thumb : attr_scroll);
3718 else
3719 screen_putchar(' ', row, scroll_col, popup_attr);
3720 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003721 // right border
3722 if (wp->w_popup_border[1] > 0)
3723 {
3724 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003725 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003726 }
3727 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003728 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003730 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003731 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3732 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003733 }
3734
3735 if (wp->w_popup_padding[2] > 0)
3736 {
3737 // bottom padding
3738 row = wp->w_winrow + wp->w_popup_border[0]
3739 + wp->w_popup_padding[0] + wp->w_height;
3740 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003741 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003742 }
3743
3744 if (wp->w_popup_border[2] > 0)
3745 {
3746 // bottom border
3747 row = wp->w_winrow + total_height - 1;
3748 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003749 wincol < 0 ? 0 : wincol,
3750 wincol + total_width,
3751 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003752 ? border_char[7] : border_char[2],
3753 border_char[2], border_attr[2]);
3754 if (wp->w_popup_border[1] > 0)
3755 {
3756 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003757 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003758 }
3759 }
3760
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003761 if (wp->w_popup_close == POPCLOSE_BUTTON)
3762 {
3763 // close button goes on top of anything at the top-right corner
3764 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003765 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003766 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3767 }
3768
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003769 update_popup_transparent(wp, 0);
3770
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 // Back to the normal zindex.
3772 screen_zindex = 0;
3773 }
3774}
3775
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003776/*
3777 * Mark references in callbacks of one popup window.
3778 */
3779 static int
3780set_ref_in_one_popup(win_T *wp, int copyID)
3781{
3782 int abort = FALSE;
3783 typval_T tv;
3784
3785 if (wp->w_close_cb.cb_partial != NULL)
3786 {
3787 tv.v_type = VAR_PARTIAL;
3788 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3789 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3790 }
3791 if (wp->w_filter_cb.cb_partial != NULL)
3792 {
3793 tv.v_type = VAR_PARTIAL;
3794 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3795 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3796 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003797 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003798 return abort;
3799}
3800
3801/*
3802 * Set reference in callbacks of popup windows.
3803 */
3804 int
3805set_ref_in_popups(int copyID)
3806{
3807 int abort = FALSE;
3808 win_T *wp;
3809 tabpage_T *tp;
3810
3811 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3812 abort = abort || set_ref_in_one_popup(wp, copyID);
3813
3814 FOR_ALL_TABPAGES(tp)
3815 {
3816 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3817 abort = abort || set_ref_in_one_popup(wp, copyID);
3818 if (abort)
3819 break;
3820 }
3821 return abort;
3822}
Bram Moolenaar79648732019-07-18 21:43:07 +02003823
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003824 int
3825popup_is_popup(win_T *wp)
3826{
3827 return wp->w_popup_flags != 0;
3828}
3829
3830#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003831/*
3832 * Find an existing popup used as the preview window, in the current tab page.
3833 * Return NULL if not found.
3834 */
3835 win_T *
3836popup_find_preview_window(void)
3837{
3838 win_T *wp;
3839
3840 // Preview window popup is always local to tab page.
3841 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3842 if (wp->w_p_pvw)
3843 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003844 return NULL;
3845}
3846
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003847/*
3848 * Find an existing popup used as the info window, in the current tab page.
3849 * Return NULL if not found.
3850 */
3851 win_T *
3852popup_find_info_window(void)
3853{
3854 win_T *wp;
3855
3856 // info window popup is always local to tab page.
3857 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3858 if (wp->w_popup_flags & POPF_INFO)
3859 return wp;
3860 return NULL;
3861}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003862#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003863
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003864 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003865f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3866{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003867#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003868 win_T *wp = popup_find_info_window();
3869
3870 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003871#else
3872 rettv->vval.v_number = 0;
3873#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003874}
3875
3876 void
3877f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003878{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003879#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003880 win_T *wp = popup_find_preview_window();
3881
3882 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003883#else
3884 rettv->vval.v_number = 0;
3885#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003886}
3887
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003888#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003889/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003890 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003891 * NOTE: this makes the popup the current window, so that the file can be
3892 * edited. However, it must not remain to be the current window, the caller
3893 * must make sure of that.
3894 */
3895 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003896popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003897{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003898 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003899
3900 if (wp == NULL)
3901 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003902 if (info)
3903 wp->w_popup_flags |= POPF_INFO;
3904 else
3905 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003906
3907 // Set the width to a reasonable value, so that w_topline can be computed.
3908 if (wp->w_minwidth > 0)
3909 wp->w_width = wp->w_minwidth;
3910 else if (wp->w_maxwidth > 0)
3911 wp->w_width = wp->w_maxwidth;
3912 else
3913 wp->w_width = curwin->w_width;
3914
3915 // Will switch to another buffer soon, dummy one can be wiped.
3916 wp->w_buffer->b_locked = FALSE;
3917
3918 win_enter(wp, FALSE);
3919 return OK;
3920}
3921
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003922/*
3923 * Close any preview popup.
3924 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003925 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003926popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003927{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003928 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003929
3930 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003931 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003932}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003933
3934/*
3935 * Hide the info popup.
3936 */
3937 void
3938popup_hide_info(void)
3939{
3940 win_T *wp = popup_find_info_window();
3941
3942 if (wp != NULL)
3943 popup_hide(wp);
3944}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003945#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003946
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003947/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003948 * Close any popup for a text property associated with "win".
3949 * Return TRUE if a popup was closed.
3950 */
3951 int
3952popup_win_closed(win_T *win)
3953{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003954 int round;
3955 win_T *wp;
3956 win_T *next;
3957 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003958
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003959 for (round = 1; round <= 2; ++round)
3960 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3961 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003962 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003963 next = wp->w_next;
3964 if (wp->w_popup_prop_win == win)
3965 {
3966 popup_close_with_retval(wp, -1);
3967 ret = TRUE;
3968 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003969 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003970 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003971}
3972
3973/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003974 * Set the title of the popup window to the file name.
3975 */
3976 void
3977popup_set_title(win_T *wp)
3978{
3979 if (wp->w_buffer->b_fname != NULL)
3980 {
3981 char_u dirname[MAXPATHL];
3982 size_t len;
3983
3984 mch_dirname(dirname, MAXPATHL);
3985 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3986
3987 vim_free(wp->w_popup_title);
3988 len = STRLEN(wp->w_buffer->b_fname) + 3;
3989 wp->w_popup_title = alloc((int)len);
3990 if (wp->w_popup_title != NULL)
3991 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3992 wp->w_buffer->b_fname);
3993 redraw_win_later(wp, VALID);
3994 }
3995}
3996
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003997# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003998/*
3999 * If there is a preview window, update the title.
4000 * Used after changing directory.
4001 */
4002 void
4003popup_update_preview_title(void)
4004{
4005 win_T *wp = popup_find_preview_window();
4006
4007 if (wp != NULL)
4008 popup_set_title(wp);
4009}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004010# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004011
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004012#endif // FEAT_PROP_POPUP