blob: 8a3f500d211fa13e9d194b6b2aa6adc462a87610 [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 Moolenaareb2310d2019-06-16 20:09:10 +02001130 int minwidth;
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 Moolenaarfbcdf672020-01-06 23:07:48 +01001235 if (wp->w_wincol >= Columns - 1)
1236 wp->w_wincol = Columns - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001237 }
1238 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001239
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001240 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001241 // When left aligned use the space available, but shift to the left when we
1242 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001243 maxspace = Columns - wp->w_wincol - left_extra;
1244 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001245 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001246 {
1247 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001248 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001249 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001250 minwidth = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001251
Bram Moolenaar8d241042019-06-12 23:40:01 +02001252 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001253 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001254 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001255 if (wp->w_topline < 1)
1256 wp->w_topline = 1;
1257 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001258 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1259
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001260 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001261 // Use a minimum width of one, so that something shows when there is no
1262 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001263 // When "firstline" is -1 then start with the last buffer line and go
1264 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001265 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001266 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001267 if (wp->w_firstline < 0)
1268 lnum = wp->w_buffer->b_ml.ml_line_count;
1269 else
1270 lnum = wp->w_topline;
1271 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001272 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001273 int len;
1274 int w_width = wp->w_width;
1275
1276 // Count Tabs for what they are worth and compute the length based on
1277 // the maximum width (matters when 'showbreak' is set).
1278 if (wp->w_width < maxwidth)
1279 wp->w_width = maxwidth;
1280 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001281 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001282 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001283
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001284 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001285 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001286 while (len > maxwidth)
1287 {
1288 ++wrapped;
1289 len -= maxwidth;
1290 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001291 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001292 }
1293 }
1294 else if (len > maxwidth
1295 && allow_adjust_left
1296 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_BOTLEFT))
1298 {
1299 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001300 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001301
Bram Moolenaar51c31312019-06-15 22:27:23 +02001302 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001303 {
1304 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001305
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001306 len -= truncate_shift;
1307 shift_by -= truncate_shift;
1308 }
1309
1310 wp->w_wincol -= shift_by;
1311 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001312 wp->w_width = maxwidth;
1313 }
1314 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001315 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001316 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001317 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1318 wp->w_width = wp->w_maxwidth;
1319 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001320
1321 if (wp->w_firstline < 0)
1322 --lnum;
1323 else
1324 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001325
1326 // do not use the width of lines we're not going to show
1327 if (wp->w_maxheight > 0
1328 && (wp->w_firstline >= 0
1329 ? lnum - wp->w_topline
1330 : wp->w_buffer->b_ml.ml_line_count - lnum)
1331 + wrapped >= wp->w_maxheight)
1332 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001333 }
1334
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001335 if (wp->w_firstline < 0)
1336 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1337
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001338 wp->w_has_scrollbar = wp->w_want_scrollbar
1339 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001340 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001341 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001342 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001343 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001344 // make space for the scrollbar if needed, when lines wrap and when
1345 // applying minwidth
1346 if (maxwidth + right_extra >= maxspace
1347 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1348 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001349 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001350
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001351 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1352 {
1353 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1354
1355 if (minwidth < title_len)
1356 minwidth = title_len;
1357 }
1358
1359 if (minwidth > 0 && wp->w_width < minwidth)
1360 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001361 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001362 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001363 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001364 // some columns cut off on the right
1365 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001366 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001367 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001368 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001369 {
1370 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1371 if (wp->w_wincol < 0)
1372 wp->w_wincol = 0;
1373 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001374 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1375 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1376 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001377 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001378
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001379 // Right aligned: move to the right if needed.
1380 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001381 if (leftoff >= 0)
1382 wp->w_wincol = leftoff;
1383 else if (wp->w_popup_fixed)
1384 {
1385 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001386 // columns when displaying the window, minus left border and
1387 // padding.
1388 if (-leftoff > left_extra)
1389 wp->w_leftcol = -leftoff - left_extra;
1390 wp->w_width -= wp->w_leftcol;
1391 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001392 if (wp->w_width < 0)
1393 wp->w_width = 0;
1394 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001395 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001396
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001397 if (wp->w_p_wrap || (!wp->w_popup_fixed
1398 && (wp->w_popup_pos == POPPOS_TOPLEFT
1399 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001400 {
1401 int want_col = 0;
1402
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001403 // try to show the right border and any scrollbar
1404 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001405 if (want_col > 0 && wp->w_wincol > 0
1406 && wp->w_wincol + want_col >= Columns)
1407 {
1408 wp->w_wincol = Columns - want_col;
1409 if (wp->w_wincol < 0)
1410 wp->w_wincol = 0;
1411 }
1412 }
1413
Bram Moolenaar8d241042019-06-12 23:40:01 +02001414 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1415 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001416 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1417 wp->w_height = wp->w_minheight;
1418 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1419 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001420 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001421 if (wp->w_height > Rows - wp->w_winrow)
1422 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001423 if (wp->w_height != org_height)
1424 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001425
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001426 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001427 {
1428 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1429 if (wp->w_winrow < 0)
1430 wp->w_winrow = 0;
1431 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001432 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001433 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001434 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001435 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001436 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001437 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001438 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1439 {
1440 // Bottom aligned but does not fit, and less space on the other
1441 // side or "posinvert" is off: reduce height.
1442 wp->w_winrow = 0;
1443 wp->w_height = wantline - extra_height;
1444 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001445 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001446 // Not enough space and more space on the other side: make top
1447 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001448 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001449 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001450 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1451 || wp->w_popup_pos == POPPOS_TOPLEFT)
1452 {
1453 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1454 && wantline * 2 > Rows
1455 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001456 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001457 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001458 // make bottom aligned and recompute the height
1459 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001460 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001461 if (wp->w_winrow < 0)
1462 {
1463 wp->w_height += wp->w_winrow;
1464 wp->w_winrow = 0;
1465 }
1466 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001467 else
1468 wp->w_winrow = wantline - 1;
1469 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001470 if (wp->w_winrow >= Rows)
1471 wp->w_winrow = Rows - 1;
1472 else if (wp->w_winrow < 0)
1473 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001474
Bram Moolenaar17146962019-05-30 00:12:11 +02001475 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001476 if (win_valid(wp->w_popup_prop_win))
1477 {
1478 wp->w_popup_prop_changedtick =
1479 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1480 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1481 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001482
1483 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001484 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001485 if (org_winrow != wp->w_winrow
1486 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001487 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001488 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001489 || org_width != wp->w_width
1490 || org_height != wp->w_height)
1491 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001492 redraw_win_later(wp, NOT_VALID);
1493 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1494 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001495 popup_mask_refresh = TRUE;
1496 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001497}
1498
Bram Moolenaar17627312019-06-02 19:53:44 +02001499typedef enum
1500{
1501 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001502 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001504 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001506 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001507 TYPE_PREVIEW, // preview window
1508 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001509} create_type_T;
1510
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001511/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001512 * Make "buf" empty and set the contents to "text".
1513 * Used by popup_create() and popup_settext().
1514 */
1515 static void
1516popup_set_buffer_text(buf_T *buf, typval_T text)
1517{
1518 int lnum;
1519
1520 // Clear the buffer, then replace the lines.
1521 curbuf = buf;
1522 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1523 ml_delete(lnum, FALSE);
1524 curbuf = curwin->w_buffer;
1525
1526 // Add text to the buffer.
1527 if (text.v_type == VAR_STRING)
1528 {
1529 // just a string
1530 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1531 }
1532 else
1533 {
1534 list_T *l = text.vval.v_list;
1535
1536 if (l->lv_len > 0)
1537 {
1538 if (l->lv_first->li_tv.v_type == VAR_STRING)
1539 // list of strings
1540 add_popup_strings(buf, l);
1541 else
1542 // list of dictionaries
1543 add_popup_dicts(buf, l);
1544 }
1545 }
1546
1547 // delete the line that was in the empty buffer
1548 curbuf = buf;
1549 ml_delete(buf->b_ml.ml_line_count, FALSE);
1550 curbuf = curwin->w_buffer;
1551}
1552
1553/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001554 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1555 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001556 * Return FAIL if the parsing fails.
1557 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001558 static int
1559parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001560{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001561 char_u *p =
1562#ifdef FEAT_QUICKFIX
1563 !is_preview ? p_cpp :
1564#endif
1565 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001566
Bram Moolenaar258cef52019-08-21 17:29:29 +02001567 if (wp != NULL)
1568 wp->w_popup_flags &= ~POPF_INFO_MENU;
1569
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001570 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001571 {
1572 char_u *e, *dig;
1573 char_u *s = p;
1574 int x;
1575
1576 e = vim_strchr(p, ':');
1577 if (e == NULL || e[1] == NUL)
1578 return FAIL;
1579
1580 p = vim_strchr(e, ',');
1581 if (p == NULL)
1582 p = e + STRLEN(e);
1583 dig = e + 1;
1584 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001585
1586 if (STRNCMP(s, "height:", 7) == 0)
1587 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001588 if (dig != p)
1589 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001590 if (wp != NULL)
1591 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001592 if (is_preview)
1593 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001594 wp->w_maxheight = x;
1595 }
1596 }
1597 else if (STRNCMP(s, "width:", 6) == 0)
1598 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001599 if (dig != p)
1600 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001601 if (wp != NULL)
1602 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001603 if (is_preview)
1604 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001605 wp->w_maxwidth = x;
1606 }
1607 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001608 else if (STRNCMP(s, "highlight:", 10) == 0)
1609 {
1610 if (wp != NULL)
1611 {
1612 int c = *p;
1613
1614 *p = NUL;
1615 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1616 s + 10, OPT_FREE|OPT_LOCAL, 0);
1617 *p = c;
1618 }
1619 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001620 else if (STRNCMP(s, "border:", 7) == 0)
1621 {
1622 char_u *arg = s + 7;
1623 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1624 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1625 int i;
1626
1627 if (!on && !off)
1628 return FAIL;
1629 if (wp != NULL)
1630 {
1631 for (i = 0; i < 4; ++i)
1632 wp->w_popup_border[i] = on ? 1 : 0;
1633 if (off)
1634 // only show the X for close when there is a border
1635 wp->w_popup_close = POPCLOSE_NONE;
1636 }
1637 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001638 else if (STRNCMP(s, "align:", 6) == 0)
1639 {
1640 char_u *arg = s + 6;
1641 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1642 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1643
1644 if (!menu && !item)
1645 return FAIL;
1646 if (wp != NULL && menu)
1647 wp->w_popup_flags |= POPF_INFO_MENU;
1648 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001649 else
1650 return FAIL;
1651 }
1652 return OK;
1653}
1654
1655/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001656 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1657 * is not NULL.
1658 * Return FAIL if the parsing fails.
1659 */
1660 int
1661parse_previewpopup(win_T *wp)
1662{
1663 return parse_popup_option(wp, TRUE);
1664}
1665
1666/*
1667 * Parse the 'completepopup' option and apply the values to window "wp" if it
1668 * is not NULL.
1669 * Return FAIL if the parsing fails.
1670 */
1671 int
1672parse_completepopup(win_T *wp)
1673{
1674 return parse_popup_option(wp, FALSE);
1675}
1676
1677/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001678 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001679 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001680 */
1681 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001682popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001683{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001684 poppos_T ppt = POPPOS_NONE;
1685
1686 if (d != NULL)
1687 ppt = get_pos_entry(d, FALSE);
1688
Bram Moolenaar79648732019-07-18 21:43:07 +02001689 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001690 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001692 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1693 }
1694 else
1695 {
1696 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1697 if (wp->w_wantline == 0) // cursor in first line
1698 {
1699 wp->w_wantline = 2;
1700 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1701 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1702 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001703 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001704
Bram Moolenaar79648732019-07-18 21:43:07 +02001705 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001706 if (wp->w_wantcol > Columns - width)
1707 {
1708 wp->w_wantcol = Columns - width;
1709 if (wp->w_wantcol < 1)
1710 wp->w_wantcol = 1;
1711 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001712
Bram Moolenaar79648732019-07-18 21:43:07 +02001713 popup_adjust_position(wp);
1714}
1715
1716/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001717 * Set w_wantline and w_wantcol for the a given screen position.
1718 * Caller must take care of running into the window border.
1719 */
1720 void
1721popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1722{
1723 wp->w_wantline = row;
1724 wp->w_wantcol = col;
1725 popup_adjust_position(wp);
1726}
1727
1728/*
1729 * Add a border and lef&right padding.
1730 */
1731 static void
1732add_border_left_right_padding(win_T *wp)
1733{
1734 int i;
1735
1736 for (i = 0; i < 4; ++i)
1737 {
1738 wp->w_popup_border[i] = 1;
1739 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1740 }
1741}
1742
1743/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001744 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001745 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001746 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001747 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001748 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001749 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001750popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001751{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001752 win_T *wp;
1753 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001754 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001755 int new_buffer;
1756 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001758 int nr;
1759 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001760
Bram Moolenaar79648732019-07-18 21:43:07 +02001761 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001762 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001763 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001764 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001765 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001766 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001767 if (buf == NULL)
1768 {
1769 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1770 return NULL;
1771 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001772#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001773 if (buf->b_term != NULL)
1774 {
1775 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1776 return NULL;
1777 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001778#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001779 }
1780 else if (!(argvars[0].v_type == VAR_STRING
1781 && argvars[0].vval.v_string != NULL)
1782 && !(argvars[0].v_type == VAR_LIST
1783 && argvars[0].vval.v_list != NULL))
1784 {
1785 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001786 return NULL;
1787 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001788 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001789 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001790 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001791 return NULL;
1792 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001793 d = argvars[1].vval.v_dict;
1794 }
1795
1796 if (d != NULL)
1797 {
1798 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1799 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1800 else if (type == TYPE_NOTIFICATION)
1801 tabnr = -1; // notifications are global by default
1802 else
1803 tabnr = 0;
1804 if (tabnr > 0)
1805 {
1806 tp = find_tabpage(tabnr);
1807 if (tp == NULL)
1808 {
1809 semsg(_("E997: Tabpage not found: %d"), tabnr);
1810 return NULL;
1811 }
1812 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001813 }
1814
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001815 // Create the window and buffer.
1816 wp = win_alloc_popup_win();
1817 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001818 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001819 if (rettv != NULL)
1820 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001821 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001822 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001823
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001824 if (buf != NULL)
1825 {
1826 // use existing buffer
1827 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001828 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001829 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001830 buffer_ensure_loaded(buf);
1831 }
1832 else
1833 {
1834 // create a new buffer associated with the popup
1835 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001836 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001837 if (buf == NULL)
1838 return NULL;
1839 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001840
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001841 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001842
Bram Moolenaar46451042019-08-24 15:50:46 +02001843 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001844 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001845 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001846 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001847 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848 buf->b_p_ul = -1; // no undo
1849 buf->b_p_swf = FALSE; // no swap file
1850 buf->b_p_bl = FALSE; // unlisted buffer
1851 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001852
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001853 // Avoid that 'buftype' is reset when this buffer is entered.
1854 buf->b_p_initialized = TRUE;
1855 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001856 wp->w_p_wrap = TRUE; // 'wrap' is default on
1857 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001858
Bram Moolenaara3fce622019-06-20 02:31:49 +02001859 if (tp != NULL)
1860 {
1861 // popup on specified tab page
1862 wp->w_next = tp->tp_first_popupwin;
1863 tp->tp_first_popupwin = wp;
1864 }
1865 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001866 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001867 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001868 wp->w_next = curtab->tp_first_popupwin;
1869 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001870 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001871 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001872 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001873 win_T *prev = first_popupwin;
1874
1875 // Global popup: add at the end, so that it gets displayed on top of
1876 // older ones with the same zindex. Matters for notifications.
1877 if (first_popupwin == NULL)
1878 first_popupwin = wp;
1879 else
1880 {
1881 while (prev->w_next != NULL)
1882 prev = prev->w_next;
1883 prev->w_next = wp;
1884 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001885 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001886
Bram Moolenaar79648732019-07-18 21:43:07 +02001887 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001888 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001889
Bram Moolenaar79648732019-07-18 21:43:07 +02001890 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001891 {
1892 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001893 }
1894 if (type == TYPE_ATCURSOR)
1895 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001896 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001897 set_moved_values(wp);
1898 set_moved_columns(wp, FIND_STRING);
1899 }
1900
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001901 if (type == TYPE_BEVAL)
1902 {
1903 wp->w_popup_pos = POPPOS_BOTLEFT;
1904
1905 // by default use the mouse position
1906 wp->w_wantline = mouse_row;
1907 if (wp->w_wantline <= 0) // mouse on first line
1908 {
1909 wp->w_wantline = 2;
1910 wp->w_popup_pos = POPPOS_TOPLEFT;
1911 }
1912 wp->w_wantcol = mouse_col + 1;
1913 set_mousemoved_values(wp);
1914 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1915 }
1916
Bram Moolenaar33796b32019-06-08 16:01:13 +02001917 // set default values
1918 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001919 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001920
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001921 if (type == TYPE_NOTIFICATION)
1922 {
1923 win_T *twp, *nextwin;
1924 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001925
1926 // Try to not overlap with another global popup. Guess we need 3
1927 // more screen lines than buffer lines.
1928 wp->w_wantline = 1;
1929 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1930 {
1931 nextwin = twp->w_next;
1932 if (twp != wp
1933 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1934 && twp->w_winrow <= wp->w_wantline - 1 + height
1935 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1936 {
1937 // move to below this popup and restart the loop to check for
1938 // overlap with other popups
1939 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1940 nextwin = first_popupwin;
1941 }
1942 }
1943 if (wp->w_wantline + height > Rows)
1944 {
1945 // can't avoid overlap, put on top in the hope that message goes
1946 // away soon.
1947 wp->w_wantline = 1;
1948 }
1949
1950 wp->w_wantcol = 10;
1951 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001952 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001953 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001954 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001955 for (i = 0; i < 4; ++i)
1956 wp->w_popup_border[i] = 1;
1957 wp->w_popup_padding[1] = 1;
1958 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001959
1960 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001961 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001962 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1963 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001964 }
1965
Bram Moolenaara730e552019-06-16 19:05:31 +02001966 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001967 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001968 wp->w_popup_pos = POPPOS_CENTER;
1969 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001970 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001971 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001972 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001973 }
1974
Bram Moolenaara730e552019-06-16 19:05:31 +02001975 if (type == TYPE_MENU)
1976 {
1977 typval_T tv;
1978 callback_T callback;
1979
1980 tv.v_type = VAR_STRING;
1981 tv.vval.v_string = (char_u *)"popup_filter_menu";
1982 callback = get_callback(&tv);
1983 if (callback.cb_name != NULL)
1984 set_callback(&wp->w_filter_cb, &callback);
1985
1986 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001987 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001988 }
1989
Bram Moolenaar79648732019-07-18 21:43:07 +02001990 if (type == TYPE_PREVIEW)
1991 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001992 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001993 wp->w_popup_close = POPCLOSE_BUTTON;
1994 for (i = 0; i < 4; ++i)
1995 wp->w_popup_border[i] = 1;
1996 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001997 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001998 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001999# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002000 if (type == TYPE_INFO)
2001 {
2002 wp->w_popup_pos = POPPOS_TOPLEFT;
2003 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2004 wp->w_popup_close = POPCLOSE_BUTTON;
2005 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002006 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002007 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002008# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002009
Bram Moolenaarae943152019-06-16 22:54:14 +02002010 for (i = 0; i < 4; ++i)
2011 VIM_CLEAR(wp->w_border_highlight[i]);
2012 for (i = 0; i < 8; ++i)
2013 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002014 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002015 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002016 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002017
Bram Moolenaar79648732019-07-18 21:43:07 +02002018 if (d != NULL)
2019 // Deal with options.
2020 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002021
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002022#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002023 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2024 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002025#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002026
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002027 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002028
2029 wp->w_vsep_width = 0;
2030
2031 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002032 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002033
2034 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002035}
2036
2037/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002038 * popup_clear()
2039 */
2040 void
2041f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2042{
2043 close_all_popups();
2044}
2045
2046/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002047 * popup_create({text}, {options})
2048 */
2049 void
2050f_popup_create(typval_T *argvars, typval_T *rettv)
2051{
Bram Moolenaar17627312019-06-02 19:53:44 +02002052 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002053}
2054
2055/*
2056 * popup_atcursor({text}, {options})
2057 */
2058 void
2059f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2060{
Bram Moolenaar17627312019-06-02 19:53:44 +02002061 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002062}
2063
2064/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002065 * popup_beval({text}, {options})
2066 */
2067 void
2068f_popup_beval(typval_T *argvars, typval_T *rettv)
2069{
2070 popup_create(argvars, rettv, TYPE_BEVAL);
2071}
2072
2073/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002074 * Invoke the close callback for window "wp" with value "result".
2075 * Careful: The callback may make "wp" invalid!
2076 */
2077 static void
2078invoke_popup_callback(win_T *wp, typval_T *result)
2079{
2080 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002081 typval_T argv[3];
2082
2083 argv[0].v_type = VAR_NUMBER;
2084 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2085
2086 if (result != NULL && result->v_type != VAR_UNKNOWN)
2087 copy_tv(result, &argv[1]);
2088 else
2089 {
2090 argv[1].v_type = VAR_NUMBER;
2091 argv[1].vval.v_number = 0;
2092 }
2093
2094 argv[2].v_type = VAR_UNKNOWN;
2095
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002096 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002097 if (result != NULL)
2098 clear_tv(&argv[1]);
2099 clear_tv(&rettv);
2100}
2101
2102/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002103 * Close popup "wp" and invoke any close callback for it.
2104 */
2105 static void
2106popup_close_and_callback(win_T *wp, typval_T *arg)
2107{
2108 int id = wp->w_id;
2109
Bram Moolenaar49540192019-12-11 19:34:54 +01002110 // Just in case a check higher up is missing.
2111 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2112 return;
2113
Bram Moolenaar3397f742019-06-02 18:40:06 +02002114 if (wp->w_close_cb.cb_name != NULL)
2115 // Careful: This may make "wp" invalid.
2116 invoke_popup_callback(wp, arg);
2117
2118 popup_close(id);
2119}
2120
Bram Moolenaar12034e22019-08-25 22:25:02 +02002121 static void
2122popup_close_with_retval(win_T *wp, int retval)
2123{
2124 typval_T res;
2125
2126 res.v_type = VAR_NUMBER;
2127 res.vval.v_number = retval;
2128 popup_close_and_callback(wp, &res);
2129}
2130
Bram Moolenaar3397f742019-06-02 18:40:06 +02002131/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002132 * Close popup "wp" because of a mouse click.
2133 */
2134 void
2135popup_close_for_mouse_click(win_T *wp)
2136{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002137 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002138}
2139
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002140 static void
2141check_mouse_moved(win_T *wp, win_T *mouse_wp)
2142{
2143 // Close the popup when all if these are true:
2144 // - the mouse is not on this popup
2145 // - "mousemoved" was used
2146 // - the mouse is no longer on the same screen row or the mouse column is
2147 // outside of the relevant text
2148 if (wp != mouse_wp
2149 && wp->w_popup_mouse_row != 0
2150 && (wp->w_popup_mouse_row != mouse_row
2151 || mouse_col < wp->w_popup_mouse_mincol
2152 || mouse_col > wp->w_popup_mouse_maxcol))
2153 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002154 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002155 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002156 }
2157}
2158
2159/*
2160 * Called when the mouse moved: may close a popup with "mousemoved".
2161 */
2162 void
2163popup_handle_mouse_moved(void)
2164{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002165 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002166 win_T *mouse_wp;
2167 int row = mouse_row;
2168 int col = mouse_col;
2169
2170 // find the window where the mouse is in
2171 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2172
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002173 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2174 {
2175 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002176 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002177 }
2178 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2179 {
2180 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002181 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002182 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002183}
2184
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002185/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002186 * In a filter: check if the typed key is a mouse event that is used for
2187 * dragging the popup.
2188 */
2189 static void
2190filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2191{
2192 int row = mouse_row;
2193 int col = mouse_col;
2194
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002195 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002196 && is_mouse_key(c)
2197 && (wp == popup_dragwin
2198 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2199 // do not consume the key, allow for dragging the popup
2200 rettv->vval.v_number = 0;
2201}
2202
Bram Moolenaara730e552019-06-16 19:05:31 +02002203/*
2204 * popup_filter_menu({text}, {options})
2205 */
2206 void
2207f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2208{
2209 int id = tv_get_number(&argvars[0]);
2210 win_T *wp = win_id2wp(id);
2211 char_u *key = tv_get_string(&argvars[1]);
2212 typval_T res;
2213 int c;
2214 linenr_T old_lnum;
2215
2216 // If the popup has been closed do not consume the key.
2217 if (wp == NULL)
2218 return;
2219
2220 c = *key;
2221 if (c == K_SPECIAL && key[1] != NUL)
2222 c = TO_SPECIAL(key[1], key[2]);
2223
2224 // consume all keys until done
2225 rettv->vval.v_number = 1;
2226 res.v_type = VAR_NUMBER;
2227
2228 old_lnum = wp->w_cursor.lnum;
2229 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2230 --wp->w_cursor.lnum;
2231 if ((c == 'j' || c == 'J' || c == K_DOWN)
2232 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2233 ++wp->w_cursor.lnum;
2234 if (old_lnum != wp->w_cursor.lnum)
2235 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002236 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002237 return;
2238 }
2239
2240 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2241 {
2242 // Cancelled, invoke callback with -1
2243 res.vval.v_number = -1;
2244 popup_close_and_callback(wp, &res);
2245 return;
2246 }
2247 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2248 {
2249 // Invoke callback with current index.
2250 res.vval.v_number = wp->w_cursor.lnum;
2251 popup_close_and_callback(wp, &res);
2252 return;
2253 }
2254
2255 filter_handle_drag(wp, c, rettv);
2256}
2257
2258/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002259 * popup_filter_yesno({text}, {options})
2260 */
2261 void
2262f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2263{
2264 int id = tv_get_number(&argvars[0]);
2265 win_T *wp = win_id2wp(id);
2266 char_u *key = tv_get_string(&argvars[1]);
2267 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002268 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002269
2270 // If the popup has been closed don't consume the key.
2271 if (wp == NULL)
2272 return;
2273
Bram Moolenaara730e552019-06-16 19:05:31 +02002274 c = *key;
2275 if (c == K_SPECIAL && key[1] != NUL)
2276 c = TO_SPECIAL(key[1], key[2]);
2277
Bram Moolenaara42d9452019-06-15 21:46:30 +02002278 // consume all keys until done
2279 rettv->vval.v_number = 1;
2280
Bram Moolenaara730e552019-06-16 19:05:31 +02002281 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002282 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002283 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002284 res.vval.v_number = 0;
2285 else
2286 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002287 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002288 return;
2289 }
2290
2291 // Invoke callback
2292 res.v_type = VAR_NUMBER;
2293 popup_close_and_callback(wp, &res);
2294}
2295
2296/*
2297 * popup_dialog({text}, {options})
2298 */
2299 void
2300f_popup_dialog(typval_T *argvars, typval_T *rettv)
2301{
2302 popup_create(argvars, rettv, TYPE_DIALOG);
2303}
2304
2305/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002306 * popup_menu({text}, {options})
2307 */
2308 void
2309f_popup_menu(typval_T *argvars, typval_T *rettv)
2310{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002311 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002312}
2313
2314/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002315 * popup_notification({text}, {options})
2316 */
2317 void
2318f_popup_notification(typval_T *argvars, typval_T *rettv)
2319{
2320 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2321}
2322
2323/*
2324 * Find the popup window with window-ID "id".
2325 * If the popup window does not exist NULL is returned.
2326 * If the window is not a popup window, and error message is given.
2327 */
2328 static win_T *
2329find_popup_win(int id)
2330{
2331 win_T *wp = win_id2wp(id);
2332
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002333 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002334 {
2335 semsg(_("E993: window %d is not a popup window"), id);
2336 return NULL;
2337 }
2338 return wp;
2339}
2340
2341/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002342 * popup_close({id})
2343 */
2344 void
2345f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2346{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002347 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002348 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002349
Bram Moolenaar49540192019-12-11 19:34:54 +01002350 if (ERROR_IF_POPUP_WINDOW)
2351 return;
2352
2353 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002354 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002355 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002356}
2357
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002358 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002359popup_hide(win_T *wp)
2360{
2361 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2362 {
2363 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002364 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002365 redraw_all_later(NOT_VALID);
2366 popup_mask_refresh = TRUE;
2367 }
2368}
2369
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002370/*
2371 * popup_hide({id})
2372 */
2373 void
2374f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2375{
2376 int id = (int)tv_get_number(argvars);
2377 win_T *wp = find_popup_win(id);
2378
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002379 if (wp != NULL)
2380 popup_hide(wp);
2381}
2382
2383 void
2384popup_show(win_T *wp)
2385{
2386 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002387 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002388 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002389 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002390 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002391 }
2392}
2393
2394/*
2395 * popup_show({id})
2396 */
2397 void
2398f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2399{
2400 int id = (int)tv_get_number(argvars);
2401 win_T *wp = find_popup_win(id);
2402
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002403 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002404 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002405 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002406#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002407 if (wp->w_popup_flags & POPF_INFO)
2408 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002409#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002410 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002411}
2412
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002413/*
2414 * popup_settext({id}, {text})
2415 */
2416 void
2417f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2418{
2419 int id = (int)tv_get_number(&argvars[0]);
2420 win_T *wp = find_popup_win(id);
2421
2422 if (wp != NULL)
2423 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002424 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2425 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2426 else
2427 {
2428 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002429 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002430 popup_adjust_position(wp);
2431 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002432 }
2433}
2434
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002435 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002436popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002437{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002438 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002439 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002440 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002441 clear_cmdline = TRUE;
2442 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002443
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002444 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002445 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002446}
2447
Bram Moolenaarec583842019-05-26 14:11:23 +02002448/*
2449 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002450 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002451 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002452 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002453popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002454{
2455 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002456 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002457 win_T *prev = NULL;
2458
Bram Moolenaarec583842019-05-26 14:11:23 +02002459 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002460 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002461 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002462 {
2463 if (prev == NULL)
2464 first_popupwin = wp->w_next;
2465 else
2466 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002467 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002468 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002469 }
2470
Bram Moolenaarec583842019-05-26 14:11:23 +02002471 // go through tab-local popups
2472 FOR_ALL_TABPAGES(tp)
2473 popup_close_tabpage(tp, id);
2474}
2475
2476/*
2477 * Close a popup window with Window-id "id" in tabpage "tp".
2478 */
2479 void
2480popup_close_tabpage(tabpage_T *tp, int id)
2481{
2482 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002483 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002484 win_T *prev = NULL;
2485
Bram Moolenaarec583842019-05-26 14:11:23 +02002486 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2487 if (wp->w_id == id)
2488 {
2489 if (prev == NULL)
2490 *root = wp->w_next;
2491 else
2492 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002493 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002494 return;
2495 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002496}
2497
2498 void
2499close_all_popups(void)
2500{
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002501 if (ERROR_IF_POPUP_WINDOW)
2502 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002503 while (first_popupwin != NULL)
2504 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002505 while (curtab->tp_first_popupwin != NULL)
2506 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002507}
2508
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002509/*
2510 * popup_move({id}, {options})
2511 */
2512 void
2513f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2514{
Bram Moolenaarae943152019-06-16 22:54:14 +02002515 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002516 int id = (int)tv_get_number(argvars);
2517 win_T *wp = find_popup_win(id);
2518
2519 if (wp == NULL)
2520 return; // invalid {id}
2521
2522 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2523 {
2524 emsg(_(e_dictreq));
2525 return;
2526 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002527 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002528
Bram Moolenaarae943152019-06-16 22:54:14 +02002529 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002530
2531 if (wp->w_winrow + wp->w_height >= cmdline_row)
2532 clear_cmdline = TRUE;
2533 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002534}
2535
Bram Moolenaarbc133542019-05-29 20:26:46 +02002536/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002537 * popup_setoptions({id}, {options})
2538 */
2539 void
2540f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2541{
2542 dict_T *dict;
2543 int id = (int)tv_get_number(argvars);
2544 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002545 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002546
2547 if (wp == NULL)
2548 return; // invalid {id}
2549
2550 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2551 {
2552 emsg(_(e_dictreq));
2553 return;
2554 }
2555 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002556 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002557
2558 apply_move_options(wp, dict);
2559 apply_general_options(wp, dict);
2560
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002561 if (old_firstline != wp->w_firstline)
2562 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002563 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002564 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002565 popup_adjust_position(wp);
2566}
2567
2568/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002569 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002570 */
2571 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002572f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002573{
2574 dict_T *dict;
2575 int id = (int)tv_get_number(argvars);
2576 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002577 int top_extra;
2578 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002579
2580 if (rettv_dict_alloc(rettv) == OK)
2581 {
2582 if (wp == NULL)
2583 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002584 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002585 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2586
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002587 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002588 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002589 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002590
Bram Moolenaarbc133542019-05-29 20:26:46 +02002591 dict_add_number(dict, "line", wp->w_winrow + 1);
2592 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002593 dict_add_number(dict, "width", wp->w_width + left_extra
2594 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2595 dict_add_number(dict, "height", wp->w_height + top_extra
2596 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002597
2598 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2599 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2600 dict_add_number(dict, "core_width", wp->w_width);
2601 dict_add_number(dict, "core_height", wp->w_height);
2602
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002603 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002604 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002605 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002606 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002607 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002608
2609 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002610 }
2611}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002612/*
2613 * popup_locate({row}, {col})
2614 */
2615 void
2616f_popup_locate(typval_T *argvars, typval_T *rettv)
2617{
2618 int row = tv_get_number(&argvars[0]) - 1;
2619 int col = tv_get_number(&argvars[1]) - 1;
2620 win_T *wp;
2621
2622 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002623 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002624 rettv->vval.v_number = wp->w_id;
2625}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002626
2627/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002628 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2629 */
2630 static void
2631get_padding_border(dict_T *dict, int *array, char *name)
2632{
2633 list_T *list;
2634 int i;
2635
2636 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2637 return;
2638
2639 list = list_alloc();
2640 if (list != NULL)
2641 {
2642 dict_add_list(dict, name, list);
2643 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2644 for (i = 0; i < 4; ++i)
2645 list_append_number(list, array[i]);
2646 }
2647}
2648
2649/*
2650 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2651 */
2652 static void
2653get_borderhighlight(dict_T *dict, win_T *wp)
2654{
2655 list_T *list;
2656 int i;
2657
2658 for (i = 0; i < 4; ++i)
2659 if (wp->w_border_highlight[i] != NULL)
2660 break;
2661 if (i == 4)
2662 return;
2663
2664 list = list_alloc();
2665 if (list != NULL)
2666 {
2667 dict_add_list(dict, "borderhighlight", list);
2668 for (i = 0; i < 4; ++i)
2669 list_append_string(list, wp->w_border_highlight[i], -1);
2670 }
2671}
2672
2673/*
2674 * For popup_getoptions(): add a "borderchars" entry to "dict".
2675 */
2676 static void
2677get_borderchars(dict_T *dict, win_T *wp)
2678{
2679 list_T *list;
2680 int i;
2681 char_u buf[NUMBUFLEN];
2682 int len;
2683
2684 for (i = 0; i < 8; ++i)
2685 if (wp->w_border_char[i] != 0)
2686 break;
2687 if (i == 8)
2688 return;
2689
2690 list = list_alloc();
2691 if (list != NULL)
2692 {
2693 dict_add_list(dict, "borderchars", list);
2694 for (i = 0; i < 8; ++i)
2695 {
2696 len = mb_char2bytes(wp->w_border_char[i], buf);
2697 list_append_string(list, buf, len);
2698 }
2699 }
2700}
2701
2702/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002703 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002704 */
2705 static void
2706get_moved_list(dict_T *dict, win_T *wp)
2707{
2708 list_T *list;
2709
2710 list = list_alloc();
2711 if (list != NULL)
2712 {
2713 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002714 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002715 list_append_number(list, wp->w_popup_mincol);
2716 list_append_number(list, wp->w_popup_maxcol);
2717 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002718 list = list_alloc();
2719 if (list != NULL)
2720 {
2721 dict_add_list(dict, "mousemoved", list);
2722 list_append_number(list, wp->w_popup_mouse_row);
2723 list_append_number(list, wp->w_popup_mouse_mincol);
2724 list_append_number(list, wp->w_popup_mouse_maxcol);
2725 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002726}
2727
2728/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002729 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002730 */
2731 void
2732f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2733{
2734 dict_T *dict;
2735 int id = (int)tv_get_number(argvars);
2736 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002737 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002738 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002739
2740 if (rettv_dict_alloc(rettv) == OK)
2741 {
2742 if (wp == NULL)
2743 return;
2744
2745 dict = rettv->vval.v_dict;
2746 dict_add_number(dict, "line", wp->w_wantline);
2747 dict_add_number(dict, "col", wp->w_wantcol);
2748 dict_add_number(dict, "minwidth", wp->w_minwidth);
2749 dict_add_number(dict, "minheight", wp->w_minheight);
2750 dict_add_number(dict, "maxheight", wp->w_maxheight);
2751 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002752 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002753 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002754 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002755 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002756 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2757 {
2758 proptype_T *pt = text_prop_type_by_id(
2759 wp->w_popup_prop_win->w_buffer,
2760 wp->w_popup_prop_type);
2761
2762 if (pt != NULL)
2763 dict_add_string(dict, "textprop", pt->pt_name);
2764 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2765 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2766 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002767 dict_add_string(dict, "title", wp->w_popup_title);
2768 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002769 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002770 dict_add_number(dict, "mapping",
2771 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002772 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002773 dict_add_number(dict, "posinvert",
2774 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002775 dict_add_number(dict, "cursorline",
2776 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002777 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002778 if (wp->w_scrollbar_highlight != NULL)
2779 dict_add_string(dict, "scrollbarhighlight",
2780 wp->w_scrollbar_highlight);
2781 if (wp->w_thumb_highlight != NULL)
2782 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002783
Bram Moolenaara3fce622019-06-20 02:31:49 +02002784 // find the tabpage that holds this popup
2785 i = 1;
2786 FOR_ALL_TABPAGES(tp)
2787 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002788 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002789
Bram Moolenaar1824f452019-10-02 23:06:46 +02002790 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2791 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002792 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002793 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002794 break;
2795 ++i;
2796 }
2797 if (tp == NULL)
2798 i = -1; // must be global
2799 else if (tp == curtab)
2800 i = 0;
2801 dict_add_number(dict, "tabpage", i);
2802
Bram Moolenaarae943152019-06-16 22:54:14 +02002803 get_padding_border(dict, wp->w_popup_padding, "padding");
2804 get_padding_border(dict, wp->w_popup_border, "border");
2805 get_borderhighlight(dict, wp);
2806 get_borderchars(dict, wp);
2807 get_moved_list(dict, wp);
2808
2809 if (wp->w_filter_cb.cb_name != NULL)
2810 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2811 if (wp->w_close_cb.cb_name != NULL)
2812 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002813
2814 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2815 ++i)
2816 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2817 {
2818 dict_add_string(dict, "pos",
2819 (char_u *)poppos_entries[i].pp_name);
2820 break;
2821 }
2822
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002823 dict_add_string(dict, "close", (char_u *)(
2824 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2825 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2826
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002827# if defined(FEAT_TIMERS)
2828 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2829 ? (long)wp->w_popup_timer->tr_interval : 0L);
2830# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002831 }
2832}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002833
2834 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002835error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002836{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002837 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002838 {
2839 emsg(_("E994: Not allowed in a popup window"));
2840 return TRUE;
2841 }
2842 return FALSE;
2843}
2844
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002845/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002846 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002847 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002848 * Each calling function should use a different flag, see the list at
2849 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002850 */
2851 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002852popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002853{
2854 win_T *wp;
2855
2856 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002857 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002858 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002859 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002860}
2861
2862/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002863 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002864 * Must have called popup_reset_handled() first.
2865 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2866 * popup with the highest zindex.
2867 */
2868 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002869find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002870{
2871 win_T *wp;
2872 win_T *found_wp;
2873 int found_zindex;
2874
2875 found_zindex = lowest ? INT_MAX : 0;
2876 found_wp = NULL;
2877 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002878 if ((wp->w_popup_handled & handled_flag) == 0
2879 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002880 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002881 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002882 {
2883 found_zindex = wp->w_zindex;
2884 found_wp = wp;
2885 }
2886 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002887 if ((wp->w_popup_handled & handled_flag) == 0
2888 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002889 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002890 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002891 {
2892 found_zindex = wp->w_zindex;
2893 found_wp = wp;
2894 }
2895
2896 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002897 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002898 return found_wp;
2899}
2900
2901/*
2902 * Invoke the filter callback for window "wp" with typed character "c".
2903 * Uses the global "mod_mask" for modifiers.
2904 * Returns the return value of the filter.
2905 * Careful: The filter may make "wp" invalid!
2906 */
2907 static int
2908invoke_popup_filter(win_T *wp, int c)
2909{
2910 int res;
2911 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002912 typval_T argv[3];
2913 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002914 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002915
Bram Moolenaara42d9452019-06-15 21:46:30 +02002916 // Emergency exit: CTRL-C closes the popup.
2917 if (c == Ctrl_C)
2918 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002919 int save_got_int = got_int;
2920
2921 // Reset got_int to avoid the callback isn't called.
2922 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002923 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002924 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002925 return 1;
2926 }
2927
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002928 argv[0].v_type = VAR_NUMBER;
2929 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2930
2931 // Convert the number to a string, so that the function can use:
2932 // if a:c == "\<F2>"
2933 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2934 argv[1].v_type = VAR_STRING;
2935 argv[1].vval.v_string = vim_strsave(buf);
2936
2937 argv[2].v_type = VAR_UNKNOWN;
2938
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002939 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002940 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002941 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002942 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002943 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002944
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002945 vim_free(argv[1].vval.v_string);
2946 clear_tv(&rettv);
2947 return res;
2948}
2949
2950/*
2951 * Called when "c" was typed: invoke popup filter callbacks.
2952 * Returns TRUE when the character was consumed,
2953 */
2954 int
2955popup_do_filter(int c)
2956{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002957 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002958 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002959 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002960 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002961 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002962 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002963
2964 if (recursive)
2965 return FALSE;
2966 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002967
Bram Moolenaarf6396232019-08-24 19:36:00 +02002968 if (c == K_LEFTMOUSE)
2969 {
2970 int row = mouse_row;
2971 int col = mouse_col;
2972
2973 wp = mouse_find_win(&row, &col, FIND_POPUP);
2974 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002975 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002976 }
2977
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002978 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02002979 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002980 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002981 if (wp->w_filter_cb.cb_name != NULL
2982 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002983 res = invoke_popup_filter(wp, c);
2984
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002985 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002986 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002987 recursive = FALSE;
2988 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002989 return res;
2990}
2991
Bram Moolenaar3397f742019-06-02 18:40:06 +02002992/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002993 * Return TRUE if there is a popup visible with a filter callback and the
2994 * "mapping" property off.
2995 */
2996 int
2997popup_no_mapping(void)
2998{
2999 int round;
3000 win_T *wp;
3001
3002 for (round = 1; round <= 2; ++round)
3003 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3004 wp != NULL; wp = wp->w_next)
3005 if (wp->w_filter_cb.cb_name != NULL
3006 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3007 return TRUE;
3008 return FALSE;
3009}
3010
3011/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003012 * Called when the cursor moved: check if any popup needs to be closed if the
3013 * cursor moved far enough.
3014 */
3015 void
3016popup_check_cursor_pos()
3017{
3018 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003019
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003020 popup_reset_handled(POPUP_HANDLED_3);
3021 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003022 if (wp->w_popup_curwin != NULL
3023 && (curwin != wp->w_popup_curwin
3024 || curwin->w_cursor.lnum != wp->w_popup_lnum
3025 || curwin->w_cursor.col < wp->w_popup_mincol
3026 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003027 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003028}
3029
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003030/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003031 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003032 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003033 static void
3034popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003035{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003036 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003037 char_u *cells;
3038 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003039
3040 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003041 return;
3042 if (wp->w_popup_mask_cells != NULL
3043 && wp->w_popup_mask_height == height
3044 && wp->w_popup_mask_width == width)
3045 return; // cache is still valid
3046
3047 vim_free(wp->w_popup_mask_cells);
3048 wp->w_popup_mask_cells = alloc_clear(width * height);
3049 if (wp->w_popup_mask_cells == NULL)
3050 return;
3051 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003052
3053 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3054 {
3055 int cols, cole;
3056 int lines, linee;
3057
3058 li = lio->li_tv.vval.v_list->lv_first;
3059 cols = tv_get_number(&li->li_tv);
3060 if (cols < 0)
3061 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003062 li = li->li_next;
3063 cole = tv_get_number(&li->li_tv);
3064 if (cole < 0)
3065 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003066 li = li->li_next;
3067 lines = tv_get_number(&li->li_tv);
3068 if (lines < 0)
3069 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003070 li = li->li_next;
3071 linee = tv_get_number(&li->li_tv);
3072 if (linee < 0)
3073 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003074
3075 for (row = lines - 1; row < linee && row < height; ++row)
3076 for (col = cols - 1; col < cole && col < width; ++col)
3077 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003078 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003079}
3080
3081/*
3082 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3083 * "col" and "line" are screen coordinates.
3084 */
3085 static int
3086popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3087{
3088 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3089 int line = screenline - wp->w_winrow;
3090
3091 return col >= 0 && col < width
3092 && line >= 0 && line < height
3093 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003094}
3095
3096/*
3097 * Set flags in popup_transparent[] for window "wp" to "val".
3098 */
3099 static void
3100update_popup_transparent(win_T *wp, int val)
3101{
3102 if (wp->w_popup_mask != NULL)
3103 {
3104 int width = popup_width(wp);
3105 int height = popup_height(wp);
3106 listitem_T *lio, *li;
3107 int cols, cole;
3108 int lines, linee;
3109 int col, line;
3110
3111 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3112 {
3113 li = lio->li_tv.vval.v_list->lv_first;
3114 cols = tv_get_number(&li->li_tv);
3115 if (cols < 0)
3116 cols = width + cols + 1;
3117 li = li->li_next;
3118 cole = tv_get_number(&li->li_tv);
3119 if (cole < 0)
3120 cole = width + cole + 1;
3121 li = li->li_next;
3122 lines = tv_get_number(&li->li_tv);
3123 if (lines < 0)
3124 lines = height + lines + 1;
3125 li = li->li_next;
3126 linee = tv_get_number(&li->li_tv);
3127 if (linee < 0)
3128 linee = height + linee + 1;
3129
3130 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003131 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003132 if (cols < 0)
3133 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003134 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003135 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003136 if (lines < 0)
3137 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003138 for (line = lines; line < linee
3139 && line + wp->w_winrow < screen_Rows; ++line)
3140 for (col = cols; col < cole
3141 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003142 popup_transparent[(line + wp->w_winrow) * screen_Columns
3143 + col + wp->w_wincol] = val;
3144 }
3145 }
3146}
3147
3148/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003149 * Only called when popup window "wp" is hidden: If the window is positioned
3150 * next to a text property, and it is now visible, then unhide the popup.
3151 * We don't check if visible popups become hidden, that is done in
3152 * popup_adjust_position().
3153 * Return TRUE if the popup became unhidden.
3154 */
3155 static int
3156check_popup_unhidden(win_T *wp)
3157{
3158 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3159 {
3160 textprop_T prop;
3161 linenr_T lnum;
3162
3163 if (find_visible_prop(wp->w_popup_prop_win,
3164 wp->w_popup_prop_type, wp->w_popup_prop_id,
3165 &prop, &lnum) == OK)
3166 {
3167 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003168 wp->w_popup_prop_topline = 0; // force repositioning
3169 return TRUE;
3170 }
3171 }
3172 return FALSE;
3173}
3174
3175/*
3176 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3177 * That is when the buffer in the popup was changed, or the popup is following
3178 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003179 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003180 */
3181 static int
3182popup_need_position_adjust(win_T *wp)
3183{
3184 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3185 return TRUE;
3186 if (win_valid(wp->w_popup_prop_win))
3187 return wp->w_popup_prop_changedtick
3188 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003189 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3190 || ((wp->w_popup_flags & POPF_CURSORLINE)
3191 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003192 return FALSE;
3193}
3194
3195/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003196 * Update "popup_mask" if needed.
3197 * Also recomputes the popup size and positions.
3198 * Also updates "popup_visible".
3199 * Also marks window lines for redrawing.
3200 */
3201 void
3202may_update_popup_mask(int type)
3203{
3204 win_T *wp;
3205 short *mask;
3206 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003207 int redraw_all_popups = FALSE;
3208 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003209
3210 // Need to recompute when switching tabs.
3211 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3212 // (such as the screen size) must have changed.
3213 if (popup_mask_tab != curtab || type >= NOT_VALID)
3214 {
3215 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003216 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003217 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003218
3219 // Check if any popup window buffer has changed and if any popup connected
3220 // to a text property has become visible.
3221 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3222 if (wp->w_popup_flags & POPF_HIDDEN)
3223 popup_mask_refresh |= check_popup_unhidden(wp);
3224 else if (popup_need_position_adjust(wp))
3225 popup_mask_refresh = TRUE;
3226 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3227 if (wp->w_popup_flags & POPF_HIDDEN)
3228 popup_mask_refresh |= check_popup_unhidden(wp);
3229 else if (popup_need_position_adjust(wp))
3230 popup_mask_refresh = TRUE;
3231
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003232 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003233 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003234
3235 // Need to update the mask, something has changed.
3236 popup_mask_refresh = FALSE;
3237 popup_mask_tab = curtab;
3238 popup_visible = FALSE;
3239
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003240 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003241 // If redrawing only what is needed, update "popup_mask_next" and then
3242 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003243 redrawing_all_win = TRUE;
3244 FOR_ALL_WINDOWS(wp)
3245 if (wp->w_redr_type < SOME_VALID)
3246 redrawing_all_win = FALSE;
3247 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003248 mask = popup_mask;
3249 else
3250 mask = popup_mask_next;
3251 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3252
3253 // Find the window with the lowest zindex that hasn't been handled yet,
3254 // so that the window with a higher zindex overwrites the value in
3255 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003256 popup_reset_handled(POPUP_HANDLED_4);
3257 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003258 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003259 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003260 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003261
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003262 popup_visible = TRUE;
3263
Bram Moolenaar12034e22019-08-25 22:25:02 +02003264 // Recompute the position if the text changed. It may make the popup
3265 // hidden if it's attach to a text property that is no longer visible.
3266 if (redraw_all_popups || popup_need_position_adjust(wp))
3267 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003268 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003269 if (wp->w_popup_flags & POPF_HIDDEN)
3270 continue;
3271 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003272
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003273 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003274 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003275 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003276 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003277 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003278 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003279 col < wp->w_wincol + width - wp->w_popup_leftoff
3280 && col < screen_Columns; ++col)
3281 if (wp->w_popup_mask_cells == NULL
3282 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003283 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003284 }
3285
3286 // Only check which lines are to be updated if not already
3287 // updating all lines.
3288 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003289 {
3290 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3291 win_T *prev_wp = NULL;
3292
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003293 for (line = 0; line < screen_Rows; ++line)
3294 {
3295 int col_done = 0;
3296
3297 for (col = 0; col < screen_Columns; ++col)
3298 {
3299 int off = line * screen_Columns + col;
3300
3301 if (popup_mask[off] != popup_mask_next[off])
3302 {
3303 popup_mask[off] = popup_mask_next[off];
3304
3305 if (line >= cmdline_row)
3306 {
3307 // the command line needs to be cleared if text below
3308 // the popup is now visible.
3309 if (!msg_scrolled && popup_mask_next[off] == 0)
3310 clear_cmdline = TRUE;
3311 }
3312 else if (col >= col_done)
3313 {
3314 linenr_T lnum;
3315 int line_cp = line;
3316 int col_cp = col;
3317
3318 // The screen position "line" / "col" needs to be
3319 // redrawn. Figure out what window that is and update
3320 // w_redraw_top and w_redr_bot. Only needs to be done
3321 // once for each window line.
3322 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3323 if (wp != NULL)
3324 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003325 if (wp != prev_wp)
3326 {
3327 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3328 prev_wp = wp;
3329 }
3330
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003331 if (line_cp >= wp->w_height)
3332 // In (or below) status line
3333 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003334 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003335 {
3336 // compute the position in the buffer line from
3337 // the position in the window
3338 mouse_comp_pos(wp, &line_cp, &col_cp,
3339 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003340 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003341 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003342
3343 // This line is going to be redrawn, no need to
3344 // check until the right side of the window.
3345 col_done = wp->w_wincol + wp->w_width - 1;
3346 }
3347 }
3348 }
3349 }
3350 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003351
3352 vim_free(plines_cache);
3353 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003354}
3355
3356/*
3357 * Return a string of "len" spaces in IObuff.
3358 */
3359 static char_u *
3360get_spaces(int len)
3361{
3362 vim_memset(IObuff, ' ', (size_t)len);
3363 IObuff[len] = NUL;
3364 return IObuff;
3365}
3366
3367/*
3368 * Update popup windows. They are drawn on top of normal windows.
3369 * "win_update" is called for each popup window, lowest zindex first.
3370 */
3371 void
3372update_popups(void (*win_update)(win_T *wp))
3373{
3374 win_T *wp;
3375 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003376 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003377 int total_width;
3378 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003379 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003380 int popup_attr;
3381 int border_attr[4];
3382 int border_char[8];
3383 char_u buf[MB_MAXBYTES];
3384 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003385 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003386 int padcol = 0;
3387 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003388 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003389 int sb_thumb_top = 0;
3390 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003391 int attr_scroll = 0;
3392 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003393
3394 // Find the window with the lowest zindex that hasn't been updated yet,
3395 // so that the window with a higher zindex is drawn later, thus goes on
3396 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003397 popup_reset_handled(POPUP_HANDLED_5);
3398 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003399 {
3400 // This drawing uses the zindex of the popup window, so that it's on
3401 // top of the text but doesn't draw when another popup with higher
3402 // zindex is on top of the character.
3403 screen_zindex = wp->w_zindex;
3404
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003405 // Set flags in popup_transparent[] for masked cells.
3406 update_popup_transparent(wp, 1);
3407
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003408 // adjust w_winrow and w_wincol for border and padding, since
3409 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003410 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003411 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3412 - wp->w_popup_leftoff;
3413 if (wp->w_wincol + left_extra < 0)
3414 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003415 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003416 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003417
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003418 // Draw the popup text, unless it's off screen.
3419 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003420 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003421 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003422
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003423 // move the cursor into the visible lines, otherwise executing
3424 // commands with win_execute() may cause the text to jump.
3425 if (wp->w_cursor.lnum < wp->w_topline)
3426 wp->w_cursor.lnum = wp->w_topline;
3427 else if (wp->w_cursor.lnum >= wp->w_botline)
3428 wp->w_cursor.lnum = wp->w_botline - 1;
3429 }
3430
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003431 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003432 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003433
Bram Moolenaard529ba52019-07-02 23:13:53 +02003434 total_width = popup_width(wp);
3435 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003436 popup_attr = get_wcr_attr(wp);
3437
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003438 if (wp->w_winrow + total_height > cmdline_row)
3439 wp->w_popup_flags |= POPF_ON_CMDLINE;
3440 else
3441 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3442
3443
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003444 // We can only use these line drawing characters when 'encoding' is
3445 // "utf-8" and 'ambiwidth' is "single".
3446 if (enc_utf8 && *p_ambw == 's')
3447 {
3448 border_char[0] = border_char[2] = 0x2550;
3449 border_char[1] = border_char[3] = 0x2551;
3450 border_char[4] = 0x2554;
3451 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003452 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3453 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003454 border_char[7] = 0x255a;
3455 }
3456 else
3457 {
3458 border_char[0] = border_char[2] = '-';
3459 border_char[1] = border_char[3] = '|';
3460 for (i = 4; i < 8; ++i)
3461 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003462 if (wp->w_popup_flags & POPF_RESIZE)
3463 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003464 }
3465 for (i = 0; i < 8; ++i)
3466 if (wp->w_border_char[i] != 0)
3467 border_char[i] = wp->w_border_char[i];
3468
3469 for (i = 0; i < 4; ++i)
3470 {
3471 border_attr[i] = popup_attr;
3472 if (wp->w_border_highlight[i] != NULL)
3473 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3474 }
3475
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003476 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003477 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003478 if (wp->w_popup_border[0] > 0)
3479 {
3480 // top border
3481 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003482 wincol < 0 ? 0 : wincol, wincol + total_width,
3483 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003484 ? border_char[4] : border_char[0],
3485 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003486 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003487 {
3488 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3489 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003490 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003491 }
3492 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003493 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3494 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003495
Bram Moolenaard529ba52019-07-02 23:13:53 +02003496 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3497 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003498 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003499 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3500 - wp->w_has_scrollbar;
3501 if (padcol < 0)
3502 {
3503 padwidth += padcol;
3504 padcol = 0;
3505 }
3506 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003507 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003508 {
3509 // top padding
3510 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003511 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003512 ' ', ' ', popup_attr);
3513 }
3514
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003515 // Title goes on top of border or padding.
3516 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003517 {
3518 int len = (int)STRLEN(wp->w_popup_title) + 1;
3519 char_u *title = alloc(len);
3520
3521 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3522 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003523 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003524 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003525 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003526
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003527 // Compute scrollbar thumb position and size.
3528 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003529 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003530 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3531 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003532
Bram Moolenaar076d9882019-09-14 22:23:29 +02003533 sb_thumb_height = (height * height + linecount / 2) / linecount;
3534 if (wp->w_topline > 1 && sb_thumb_height == height)
3535 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003536 if (sb_thumb_height == 0)
3537 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003538 if (linecount <= wp->w_height)
3539 // it just fits, avoid divide by zero
3540 sb_thumb_top = 0;
3541 else
3542 sb_thumb_top = (wp->w_topline - 1
3543 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003544 * (wp->w_height - sb_thumb_height)
3545 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003546 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3547 sb_thumb_top = 1; // show it's scrolled
3548
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003549 if (wp->w_scrollbar_highlight != NULL)
3550 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3551 else
3552 attr_scroll = highlight_attr[HLF_PSB];
3553 if (wp->w_thumb_highlight != NULL)
3554 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3555 else
3556 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003557 }
3558
3559 for (i = wp->w_popup_border[0];
3560 i < total_height - wp->w_popup_border[2]; ++i)
3561 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003562 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003563 // left and right padding only needed next to the body
3564 int do_padding =
3565 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3566 && i < total_height - wp->w_popup_border[2]
3567 - wp->w_popup_padding[2];
3568
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003569 row = wp->w_winrow + i;
3570
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003571 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003572 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003573 {
3574 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003575 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003576 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003577 if (do_padding && wp->w_popup_padding[3] > 0)
3578 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003579 int col = wincol + wp->w_popup_border[3];
3580
Bram Moolenaard529ba52019-07-02 23:13:53 +02003581 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003582 pad_left = wp->w_popup_padding[3];
3583 if (col < 0)
3584 {
3585 pad_left += col;
3586 col = 0;
3587 }
3588 if (pad_left > 0)
3589 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3590 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003591 // scrollbar
3592 if (wp->w_has_scrollbar)
3593 {
3594 int line = i - top_off;
3595 int scroll_col = wp->w_wincol + total_width - 1
3596 - wp->w_popup_border[1];
3597
3598 if (line >= 0 && line < wp->w_height)
3599 screen_putchar(' ', row, scroll_col,
3600 line >= sb_thumb_top
3601 && line < sb_thumb_top + sb_thumb_height
3602 ? attr_thumb : attr_scroll);
3603 else
3604 screen_putchar(' ', row, scroll_col, popup_attr);
3605 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003606 // right border
3607 if (wp->w_popup_border[1] > 0)
3608 {
3609 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003610 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003611 }
3612 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003613 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003614 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003615 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003616 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3617 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003618 }
3619
3620 if (wp->w_popup_padding[2] > 0)
3621 {
3622 // bottom padding
3623 row = wp->w_winrow + wp->w_popup_border[0]
3624 + wp->w_popup_padding[0] + wp->w_height;
3625 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003626 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003627 }
3628
3629 if (wp->w_popup_border[2] > 0)
3630 {
3631 // bottom border
3632 row = wp->w_winrow + total_height - 1;
3633 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003634 wincol < 0 ? 0 : wincol,
3635 wincol + total_width,
3636 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003637 ? border_char[7] : border_char[2],
3638 border_char[2], border_attr[2]);
3639 if (wp->w_popup_border[1] > 0)
3640 {
3641 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003642 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003643 }
3644 }
3645
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003646 if (wp->w_popup_close == POPCLOSE_BUTTON)
3647 {
3648 // close button goes on top of anything at the top-right corner
3649 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003650 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003651 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3652 }
3653
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003654 update_popup_transparent(wp, 0);
3655
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003656 // Back to the normal zindex.
3657 screen_zindex = 0;
3658 }
3659}
3660
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003661/*
3662 * Mark references in callbacks of one popup window.
3663 */
3664 static int
3665set_ref_in_one_popup(win_T *wp, int copyID)
3666{
3667 int abort = FALSE;
3668 typval_T tv;
3669
3670 if (wp->w_close_cb.cb_partial != NULL)
3671 {
3672 tv.v_type = VAR_PARTIAL;
3673 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3674 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3675 }
3676 if (wp->w_filter_cb.cb_partial != NULL)
3677 {
3678 tv.v_type = VAR_PARTIAL;
3679 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3680 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3681 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003682 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003683 return abort;
3684}
3685
3686/*
3687 * Set reference in callbacks of popup windows.
3688 */
3689 int
3690set_ref_in_popups(int copyID)
3691{
3692 int abort = FALSE;
3693 win_T *wp;
3694 tabpage_T *tp;
3695
3696 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3697 abort = abort || set_ref_in_one_popup(wp, copyID);
3698
3699 FOR_ALL_TABPAGES(tp)
3700 {
3701 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3702 abort = abort || set_ref_in_one_popup(wp, copyID);
3703 if (abort)
3704 break;
3705 }
3706 return abort;
3707}
Bram Moolenaar79648732019-07-18 21:43:07 +02003708
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003709 int
3710popup_is_popup(win_T *wp)
3711{
3712 return wp->w_popup_flags != 0;
3713}
3714
3715#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003716/*
3717 * Find an existing popup used as the preview window, in the current tab page.
3718 * Return NULL if not found.
3719 */
3720 win_T *
3721popup_find_preview_window(void)
3722{
3723 win_T *wp;
3724
3725 // Preview window popup is always local to tab page.
3726 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3727 if (wp->w_p_pvw)
3728 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003729 return NULL;
3730}
3731
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003732/*
3733 * Find an existing popup used as the info window, in the current tab page.
3734 * Return NULL if not found.
3735 */
3736 win_T *
3737popup_find_info_window(void)
3738{
3739 win_T *wp;
3740
3741 // info window popup is always local to tab page.
3742 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3743 if (wp->w_popup_flags & POPF_INFO)
3744 return wp;
3745 return NULL;
3746}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003747#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003748
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003749 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003750f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3751{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003752#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003753 win_T *wp = popup_find_info_window();
3754
3755 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003756#else
3757 rettv->vval.v_number = 0;
3758#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003759}
3760
3761 void
3762f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003763{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003764#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003765 win_T *wp = popup_find_preview_window();
3766
3767 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003768#else
3769 rettv->vval.v_number = 0;
3770#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003771}
3772
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003773#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003774/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003775 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003776 * NOTE: this makes the popup the current window, so that the file can be
3777 * edited. However, it must not remain to be the current window, the caller
3778 * must make sure of that.
3779 */
3780 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003781popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003782{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003783 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003784
3785 if (wp == NULL)
3786 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003787 if (info)
3788 wp->w_popup_flags |= POPF_INFO;
3789 else
3790 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003791
3792 // Set the width to a reasonable value, so that w_topline can be computed.
3793 if (wp->w_minwidth > 0)
3794 wp->w_width = wp->w_minwidth;
3795 else if (wp->w_maxwidth > 0)
3796 wp->w_width = wp->w_maxwidth;
3797 else
3798 wp->w_width = curwin->w_width;
3799
3800 // Will switch to another buffer soon, dummy one can be wiped.
3801 wp->w_buffer->b_locked = FALSE;
3802
3803 win_enter(wp, FALSE);
3804 return OK;
3805}
3806
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003807/*
3808 * Close any preview popup.
3809 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003810 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003811popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003812{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003813 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003814
3815 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003816 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003817}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003818
3819/*
3820 * Hide the info popup.
3821 */
3822 void
3823popup_hide_info(void)
3824{
3825 win_T *wp = popup_find_info_window();
3826
3827 if (wp != NULL)
3828 popup_hide(wp);
3829}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003830#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003831
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003832/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003833 * Close any popup for a text property associated with "win".
3834 * Return TRUE if a popup was closed.
3835 */
3836 int
3837popup_win_closed(win_T *win)
3838{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003839 int round;
3840 win_T *wp;
3841 win_T *next;
3842 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003843
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003844 for (round = 1; round <= 2; ++round)
3845 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3846 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003847 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003848 next = wp->w_next;
3849 if (wp->w_popup_prop_win == win)
3850 {
3851 popup_close_with_retval(wp, -1);
3852 ret = TRUE;
3853 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003854 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003855 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003856}
3857
3858/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003859 * Set the title of the popup window to the file name.
3860 */
3861 void
3862popup_set_title(win_T *wp)
3863{
3864 if (wp->w_buffer->b_fname != NULL)
3865 {
3866 char_u dirname[MAXPATHL];
3867 size_t len;
3868
3869 mch_dirname(dirname, MAXPATHL);
3870 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3871
3872 vim_free(wp->w_popup_title);
3873 len = STRLEN(wp->w_buffer->b_fname) + 3;
3874 wp->w_popup_title = alloc((int)len);
3875 if (wp->w_popup_title != NULL)
3876 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3877 wp->w_buffer->b_fname);
3878 redraw_win_later(wp, VALID);
3879 }
3880}
3881
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003882# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003883/*
3884 * If there is a preview window, update the title.
3885 * Used after changing directory.
3886 */
3887 void
3888popup_update_preview_title(void)
3889{
3890 win_T *wp = popup_find_preview_window();
3891
3892 if (wp != NULL)
3893 popup_set_title(wp);
3894}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003895# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003896
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003897#endif // FEAT_PROP_POPUP