blob: d2b7f22f0b91a1849e012e635b894b902a2b0d25 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
300 if (!(wp->w_popup_flags & POPF_DRAG))
301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
386 "{_ -> popup_close(%d)}", wp->w_id);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200387 if (get_lambda_tv(&ptr, &tv, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
405 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
406 ++nr)
407 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
408 return poppos_entries[nr].pp_val;
409
410 if (give_error)
411 semsg(_(e_invarg2), str);
412 return POPPOS_NONE;
413}
414
Bram Moolenaar17627312019-06-02 19:53:44 +0200415/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200416 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200417 */
418 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200419apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200420{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200421 int nr;
422 char_u *str;
423 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200424
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200425 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200426 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200427 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200428 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200429 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200430 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200431 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200432 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433
434 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantline = nr;
437 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200438 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200439 wp->w_wantcol = nr;
440
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 Moolenaar7e9f3512020-05-13 22:44:22 +0200519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100520 li = l->lv_first;
521 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200522 {
523 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
524
525 // Three numbers, might be from popup_getoptions().
526 if (mousemoved)
527 wp->w_popup_mouse_row = nr;
528 else
529 wp->w_popup_lnum = nr;
530 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100531 if (nr == 0)
532 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200533 }
534
535 mincol = tv_get_number(&li->li_tv);
536 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200537 if (mousemoved)
538 {
539 wp->w_popup_mouse_mincol = mincol;
540 wp->w_popup_mouse_maxcol = maxcol;
541 }
542 else
543 {
544 wp->w_popup_mincol = mincol;
545 wp->w_popup_maxcol = maxcol;
546 }
547 }
548 else
549 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
550}
551
552 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200553check_highlight(dict_T *dict, char *name, char_u **pval)
554{
555 dictitem_T *di;
556 char_u *str;
557
558 di = dict_find(dict, (char_u *)name, -1);
559 if (di != NULL)
560 {
561 if (di->di_tv.v_type != VAR_STRING)
562 semsg(_(e_invargval), name);
563 else
564 {
565 str = tv_get_string(&di->di_tv);
566 if (*str != NUL)
567 *pval = vim_strsave(str);
568 }
569 }
570}
571
Bram Moolenaarae943152019-06-16 22:54:14 +0200572/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200573 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200574 */
575 static void
576popup_show_curline(win_T *wp)
577{
578 if (wp->w_cursor.lnum < wp->w_topline)
579 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200580 else if (wp->w_cursor.lnum >= wp->w_botline
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 Moolenaarae943152019-06-16 22:54:14 +0200743 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
744 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200745
Bram Moolenaar790498b2019-06-01 22:15:29 +0200746 di = dict_find(dict, (char_u *)"borderhighlight", -1);
747 if (di != NULL)
748 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200749 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200750 emsg(_(e_listreq));
751 else
752 {
753 list_T *list = di->di_tv.vval.v_list;
754 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200755 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200756
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200757 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200758 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
759 ++i, li = li->li_next)
760 {
761 str = tv_get_string(&li->li_tv);
762 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100763 {
764 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200765 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100766 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
769 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100770 {
771 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200772 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200773 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100774 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200775 }
776 }
777
Bram Moolenaar790498b2019-06-01 22:15:29 +0200778 di = dict_find(dict, (char_u *)"borderchars", -1);
779 if (di != NULL)
780 {
781 if (di->di_tv.v_type != VAR_LIST)
782 emsg(_(e_listreq));
783 else
784 {
785 list_T *list = di->di_tv.vval.v_list;
786 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200787 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200788
789 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100790 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200791 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200792 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
793 ++i, li = li->li_next)
794 {
795 str = tv_get_string(&li->li_tv);
796 if (*str != NUL)
797 wp->w_border_char[i] = mb_ptr2char(str);
798 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200799 if (list->lv_len == 1)
800 for (i = 1; i < 8; ++i)
801 wp->w_border_char[i] = wp->w_border_char[0];
802 if (list->lv_len == 2)
803 {
804 for (i = 4; i < 8; ++i)
805 wp->w_border_char[i] = wp->w_border_char[1];
806 for (i = 1; i < 4; ++i)
807 wp->w_border_char[i] = wp->w_border_char[0];
808 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 }
810 }
811 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200812
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200813 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
814 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
815
Bram Moolenaarae943152019-06-16 22:54:14 +0200816 di = dict_find(dict, (char_u *)"zindex", -1);
817 if (di != NULL)
818 {
819 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
820 if (wp->w_zindex < 1)
821 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
822 if (wp->w_zindex > 32000)
823 wp->w_zindex = 32000;
824 }
825
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200826 di = dict_find(dict, (char_u *)"mask", -1);
827 if (di != NULL)
828 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200829 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200830
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200831 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832 {
833 listitem_T *li;
834
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200835 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200836 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200837 {
838 if (li->li_tv.v_type != VAR_LIST
839 || li->li_tv.vval.v_list == NULL
840 || li->li_tv.vval.v_list->lv_len != 4)
841 {
842 ok = FALSE;
843 break;
844 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100845 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200846 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200847 }
848 }
849 if (ok)
850 {
851 wp->w_popup_mask = di->di_tv.vval.v_list;
852 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200853 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200854 }
855 else
856 semsg(_(e_invargval), "mask");
857 }
858
Bram Moolenaarae943152019-06-16 22:54:14 +0200859#if defined(FEAT_TIMERS)
860 // Add timer to close the popup after some time.
861 nr = dict_get_number(dict, (char_u *)"time");
862 if (nr > 0)
863 popup_add_timeout(wp, nr);
864#endif
865
Bram Moolenaar3397f742019-06-02 18:40:06 +0200866 di = dict_find(dict, (char_u *)"moved", -1);
867 if (di != NULL)
868 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200869 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200870 handle_moved_argument(wp, di, FALSE);
871 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200872
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200873 di = dict_find(dict, (char_u *)"mousemoved", -1);
874 if (di != NULL)
875 {
876 set_mousemoved_values(wp);
877 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200878 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200879
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200880 di = dict_find(dict, (char_u *)"cursorline", -1);
881 if (di != NULL)
882 {
883 if (di->di_tv.v_type == VAR_NUMBER)
884 {
885 if (di->di_tv.vval.v_number != 0)
886 wp->w_popup_flags |= POPF_CURSORLINE;
887 else
888 wp->w_popup_flags &= ~POPF_CURSORLINE;
889 }
890 else
891 semsg(_(e_invargval), "cursorline");
892 }
893
Bram Moolenaarae943152019-06-16 22:54:14 +0200894 di = dict_find(dict, (char_u *)"filter", -1);
895 if (di != NULL)
896 {
897 callback_T callback = get_callback(&di->di_tv);
898
899 if (callback.cb_name != NULL)
900 {
901 free_callback(&wp->w_filter_cb);
902 set_callback(&wp->w_filter_cb, &callback);
903 }
904 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200905 di = dict_find(dict, (char_u *)"mapping", -1);
906 if (di != NULL)
907 {
908 nr = dict_get_number(dict, (char_u *)"mapping");
909 if (nr)
910 wp->w_popup_flags |= POPF_MAPPING;
911 else
912 wp->w_popup_flags &= ~POPF_MAPPING;
913 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200914
Bram Moolenaar581ba392019-09-03 22:08:33 +0200915 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
916 if (str != NULL)
917 {
918 if (STRCMP(str, "a") == 0)
919 wp->w_filter_mode = MODE_ALL;
920 else
921 wp->w_filter_mode = mode_str2flags(str);
922 }
923
Bram Moolenaarae943152019-06-16 22:54:14 +0200924 di = dict_find(dict, (char_u *)"callback", -1);
925 if (di != NULL)
926 {
927 callback_T callback = get_callback(&di->di_tv);
928
929 if (callback.cb_name != NULL)
930 {
931 free_callback(&wp->w_close_cb);
932 set_callback(&wp->w_close_cb, &callback);
933 }
934 }
935}
936
937/*
938 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200939 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200940 */
941 static void
942apply_options(win_T *wp, dict_T *dict)
943{
944 int nr;
945
946 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200947
948 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
949 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
950
Bram Moolenaarae943152019-06-16 22:54:14 +0200951 apply_general_options(wp, dict);
952
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200953 nr = dict_get_number(dict, (char_u *)"hidden");
954 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200955 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200956
Bram Moolenaar33796b32019-06-08 16:01:13 +0200957 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200958 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200959}
960
961/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200962 * Add lines to the popup from a list of strings.
963 */
964 static void
965add_popup_strings(buf_T *buf, list_T *l)
966{
967 listitem_T *li;
968 linenr_T lnum = 0;
969 char_u *p;
970
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200971 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200972 if (li->li_tv.v_type == VAR_STRING)
973 {
974 p = li->li_tv.vval.v_string;
975 ml_append_buf(buf, lnum++,
976 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
977 }
978}
979
980/*
981 * Add lines to the popup from a list of dictionaries.
982 */
983 static void
984add_popup_dicts(buf_T *buf, list_T *l)
985{
986 listitem_T *li;
987 listitem_T *pli;
988 linenr_T lnum = 0;
989 char_u *p;
990 dict_T *dict;
991
992 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200993 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200994 {
995 if (li->li_tv.v_type != VAR_DICT)
996 {
997 emsg(_(e_dictreq));
998 return;
999 }
1000 dict = li->li_tv.vval.v_dict;
1001 p = dict == NULL ? NULL
1002 : dict_get_string(dict, (char_u *)"text", FALSE);
1003 ml_append_buf(buf, lnum++,
1004 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1005 }
1006
1007 // add the text properties
1008 lnum = 1;
1009 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1010 {
1011 dictitem_T *di;
1012 list_T *plist;
1013
1014 dict = li->li_tv.vval.v_dict;
1015 di = dict_find(dict, (char_u *)"props", -1);
1016 if (di != NULL)
1017 {
1018 if (di->di_tv.v_type != VAR_LIST)
1019 {
1020 emsg(_(e_listreq));
1021 return;
1022 }
1023 plist = di->di_tv.vval.v_list;
1024 if (plist != NULL)
1025 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001026 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001027 {
1028 if (pli->li_tv.v_type != VAR_DICT)
1029 {
1030 emsg(_(e_dictreq));
1031 return;
1032 }
1033 dict = pli->li_tv.vval.v_dict;
1034 if (dict != NULL)
1035 {
1036 int col = dict_get_number(dict, (char_u *)"col");
1037
1038 prop_add_common( lnum, col, dict, buf, NULL);
1039 }
1040 }
1041 }
1042 }
1043 }
1044}
1045
1046/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001047 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1048 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001049 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001050popup_top_extra(win_T *wp)
1051{
1052 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1053
1054 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1055 return 1;
1056 return extra;
1057}
1058
1059/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001060 * Get the padding plus border at the left.
1061 */
1062 int
1063popup_left_extra(win_T *wp)
1064{
1065 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1066}
1067
1068/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001069 * Return the height of popup window "wp", including border and padding.
1070 */
1071 int
1072popup_height(win_T *wp)
1073{
1074 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001075 + popup_top_extra(wp)
1076 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001077}
1078
1079/*
1080 * Return the width of popup window "wp", including border, padding and
1081 * scrollbar.
1082 */
1083 int
1084popup_width(win_T *wp)
1085{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001086 // w_leftcol is how many columns of the core are left of the screen
1087 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001088 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001089 + popup_extra_width(wp)
1090 + wp->w_popup_rightoff;
1091}
1092
1093/*
1094 * Return the extra width of popup window "wp": border, padding and scrollbar.
1095 */
1096 int
1097popup_extra_width(win_T *wp)
1098{
1099 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1100 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1101 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001102}
1103
1104/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001105 * Adjust the position and size of the popup to fit on the screen.
1106 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001107 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001108popup_adjust_position(win_T *wp)
1109{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001110 linenr_T lnum;
1111 int wrapped = 0;
1112 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001113 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001114 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001115 int center_vert = FALSE;
1116 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001117 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001118 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001119 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1120 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1121 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1122 int extra_height = top_extra + bot_extra;
1123 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001124 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001125 int org_winrow = wp->w_winrow;
1126 int org_wincol = wp->w_wincol;
1127 int org_width = wp->w_width;
1128 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001129 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001130 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001131 int minwidth, minheight;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001132 int wantline = wp->w_wantline; // adjusted for textprop
1133 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001134 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001135
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001136 wp->w_winrow = 0;
1137 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001138 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001139 wp->w_popup_leftoff = 0;
1140 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001141
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001142 // May need to update the "cursorline" highlighting, which may also change
1143 // "topline"
1144 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1145 popup_highlight_curline(wp);
1146
Bram Moolenaar12034e22019-08-25 22:25:02 +02001147 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1148 {
1149 win_T *prop_win = wp->w_popup_prop_win;
1150 textprop_T prop;
1151 linenr_T prop_lnum;
1152 pos_T pos;
1153 int screen_row;
1154 int screen_scol;
1155 int screen_ccol;
1156 int screen_ecol;
1157
1158 // Popup window is positioned relative to a text property.
1159 if (find_visible_prop(prop_win,
1160 wp->w_popup_prop_type, wp->w_popup_prop_id,
1161 &prop, &prop_lnum) == FAIL)
1162 {
1163 // Text property is no longer visible, hide the popup.
1164 // Unhiding the popup is done in check_popup_unhidden().
1165 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1166 {
1167 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001168 if (win_valid(wp->w_popup_prop_win))
1169 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1170 }
1171 return;
1172 }
1173
1174 // Compute the desired position from the position of the text
1175 // property. Use "wantline" and "wantcol" as offsets.
1176 pos.lnum = prop_lnum;
1177 pos.col = prop.tp_col;
1178 if (wp->w_popup_pos == POPPOS_TOPLEFT
1179 || wp->w_popup_pos == POPPOS_BOTLEFT)
1180 pos.col += prop.tp_len - 1;
1181 textpos2screenpos(prop_win, &pos, &screen_row,
1182 &screen_scol, &screen_ccol, &screen_ecol);
1183
1184 if (wp->w_popup_pos == POPPOS_TOPLEFT
1185 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1186 // below the text
1187 wantline = screen_row + wantline + 1;
1188 else
1189 // above the text
1190 wantline = screen_row + wantline - 1;
1191 center_vert = FALSE;
1192 if (wp->w_popup_pos == POPPOS_TOPLEFT
1193 || wp->w_popup_pos == POPPOS_BOTLEFT)
1194 // right of the text
1195 wantcol = screen_ecol + wantcol;
1196 else
1197 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001198 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001199 use_wantcol = TRUE;
1200 }
1201 else
1202 {
1203 // If no line was specified default to vertical centering.
1204 if (wantline == 0)
1205 center_vert = TRUE;
1206 else if (wantline < 0)
1207 // If "wantline" is negative it actually means zero.
1208 wantline = 0;
1209 if (wantcol < 0)
1210 // If "wantcol" is negative it actually means zero.
1211 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001212 }
1213
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001214 if (wp->w_popup_pos == POPPOS_CENTER)
1215 {
1216 // center after computing the size
1217 center_vert = TRUE;
1218 center_hor = TRUE;
1219 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001220 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001221 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001222 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1223 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001224 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001225 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001226 if (wp->w_winrow >= Rows)
1227 wp->w_winrow = Rows - 1;
1228 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001229
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001230 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001231 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001232 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1233 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001234 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001235 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001236 // Need to see at least one character after the decoration.
1237 if (wp->w_wincol > Columns - left_extra - 1)
1238 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001239 }
1240 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001241
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001242 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001243 // When left aligned use the space available, but shift to the left when we
1244 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001245 maxspace = Columns - wp->w_wincol - left_extra;
1246 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001247 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001248 {
1249 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001250 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001251 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001252 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001253 minheight = wp->w_minheight;
1254#ifdef FEAT_TERMINAL
1255 // A terminal popup initially does not have content, use a default minimal
1256 // width of 20 characters and height of 5 lines.
1257 if (wp->w_buffer->b_term != NULL)
1258 {
1259 if (minwidth == 0)
1260 minwidth = 20;
1261 if (minheight == 0)
1262 minheight = 5;
1263 }
1264#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001265
Bram Moolenaar8d241042019-06-12 23:40:01 +02001266 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001267 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001268 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001269 if (wp->w_topline < 1)
1270 wp->w_topline = 1;
1271 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001272 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1273
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001274 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001275 // Use a minimum width of one, so that something shows when there is no
1276 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001277 // When "firstline" is -1 then start with the last buffer line and go
1278 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001279 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001280 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001281 if (wp->w_firstline < 0)
1282 lnum = wp->w_buffer->b_ml.ml_line_count;
1283 else
1284 lnum = wp->w_topline;
1285 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001286 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001287 int len;
1288 int w_width = wp->w_width;
1289
1290 // Count Tabs for what they are worth and compute the length based on
1291 // the maximum width (matters when 'showbreak' is set).
1292 if (wp->w_width < maxwidth)
1293 wp->w_width = maxwidth;
1294 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001295 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001296 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001298 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001299 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001300 while (len > maxwidth)
1301 {
1302 ++wrapped;
1303 len -= maxwidth;
1304 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001305 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001306 }
1307 }
1308 else if (len > maxwidth
1309 && allow_adjust_left
1310 && (wp->w_popup_pos == POPPOS_TOPLEFT
1311 || wp->w_popup_pos == POPPOS_BOTLEFT))
1312 {
1313 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001314 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315
Bram Moolenaar51c31312019-06-15 22:27:23 +02001316 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001317 {
1318 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001319
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 len -= truncate_shift;
1321 shift_by -= truncate_shift;
1322 }
1323
1324 wp->w_wincol -= shift_by;
1325 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001326 wp->w_width = maxwidth;
1327 }
1328 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001329 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001330 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001331 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1332 wp->w_width = wp->w_maxwidth;
1333 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001334
1335 if (wp->w_firstline < 0)
1336 --lnum;
1337 else
1338 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001339
1340 // do not use the width of lines we're not going to show
1341 if (wp->w_maxheight > 0
1342 && (wp->w_firstline >= 0
1343 ? lnum - wp->w_topline
1344 : wp->w_buffer->b_ml.ml_line_count - lnum)
1345 + wrapped >= wp->w_maxheight)
1346 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001347 }
1348
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001349 if (wp->w_firstline < 0)
1350 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1351
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001352 wp->w_has_scrollbar = wp->w_want_scrollbar
1353 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001354#ifdef FEAT_TERMINAL
1355 if (wp->w_buffer->b_term != NULL)
1356 // Terminal window never has a scrollbar, adjusts to window height.
1357 wp->w_has_scrollbar = FALSE;
1358#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001359 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001360 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001361 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001362 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001363 // make space for the scrollbar if needed, when lines wrap and when
1364 // applying minwidth
1365 if (maxwidth + right_extra >= maxspace
1366 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1367 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001368 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001369
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001370 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1371 {
1372 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1373
1374 if (minwidth < title_len)
1375 minwidth = title_len;
1376 }
1377
1378 if (minwidth > 0 && wp->w_width < minwidth)
1379 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001380 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001381 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001382 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001383 // some columns cut off on the right
1384 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001385 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001386 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001387 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001388 {
1389 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1390 if (wp->w_wincol < 0)
1391 wp->w_wincol = 0;
1392 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001393 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1394 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1395 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001396 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001397
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001398 // Right aligned: move to the right if needed.
1399 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001400 if (leftoff >= 0)
1401 wp->w_wincol = leftoff;
1402 else if (wp->w_popup_fixed)
1403 {
1404 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001405 // columns when displaying the window, minus left border and
1406 // padding.
1407 if (-leftoff > left_extra)
1408 wp->w_leftcol = -leftoff - left_extra;
1409 wp->w_width -= wp->w_leftcol;
1410 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001411 if (wp->w_width < 0)
1412 wp->w_width = 0;
1413 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001414 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001415
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001416 if (wp->w_p_wrap || (!wp->w_popup_fixed
1417 && (wp->w_popup_pos == POPPOS_TOPLEFT
1418 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001419 {
1420 int want_col = 0;
1421
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001422 // try to show the right border and any scrollbar
1423 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001424 if (want_col > 0 && wp->w_wincol > 0
1425 && wp->w_wincol + want_col >= Columns)
1426 {
1427 wp->w_wincol = Columns - want_col;
1428 if (wp->w_wincol < 0)
1429 wp->w_wincol = 0;
1430 }
1431 }
1432
Bram Moolenaar8d241042019-06-12 23:40:01 +02001433 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1434 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001435 if (minheight > 0 && wp->w_height < minheight)
1436 wp->w_height = minheight;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001437 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1438 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001439 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001440 if (wp->w_height > Rows - wp->w_winrow)
1441 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001442 if (wp->w_height != org_height)
1443 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001444
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001445 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001446 {
1447 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1448 if (wp->w_winrow < 0)
1449 wp->w_winrow = 0;
1450 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001451 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001452 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001453 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001454 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001455 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001456 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001457 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1458 {
1459 // Bottom aligned but does not fit, and less space on the other
1460 // side or "posinvert" is off: reduce height.
1461 wp->w_winrow = 0;
1462 wp->w_height = wantline - extra_height;
1463 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001464 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001465 // Not enough space and more space on the other side: make top
1466 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001467 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001468 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001469 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1470 || wp->w_popup_pos == POPPOS_TOPLEFT)
1471 {
1472 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1473 && wantline * 2 > Rows
1474 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001475 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001476 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001477 // make bottom aligned and recompute the height
1478 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001479 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001480 if (wp->w_winrow < 0)
1481 {
1482 wp->w_height += wp->w_winrow;
1483 wp->w_winrow = 0;
1484 }
1485 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001486 else
1487 wp->w_winrow = wantline - 1;
1488 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001489 if (wp->w_winrow >= Rows)
1490 wp->w_winrow = Rows - 1;
1491 else if (wp->w_winrow < 0)
1492 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001493
Bram Moolenaar17146962019-05-30 00:12:11 +02001494 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001495 if (win_valid(wp->w_popup_prop_win))
1496 {
1497 wp->w_popup_prop_changedtick =
1498 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1499 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1500 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001501
1502 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001503 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001504 if (org_winrow != wp->w_winrow
1505 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001506 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001507 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001508 || org_width != wp->w_width
1509 || org_height != wp->w_height)
1510 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001511 redraw_win_later(wp, NOT_VALID);
1512 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1513 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001514 popup_mask_refresh = TRUE;
1515 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001516}
1517
Bram Moolenaar17627312019-06-02 19:53:44 +02001518typedef enum
1519{
1520 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001521 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001522 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001523 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001524 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001525 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001526 TYPE_PREVIEW, // preview window
1527 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001528} create_type_T;
1529
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001530/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001531 * Make "buf" empty and set the contents to "text".
1532 * Used by popup_create() and popup_settext().
1533 */
1534 static void
1535popup_set_buffer_text(buf_T *buf, typval_T text)
1536{
1537 int lnum;
1538
1539 // Clear the buffer, then replace the lines.
1540 curbuf = buf;
1541 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001542 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001543 curbuf = curwin->w_buffer;
1544
1545 // Add text to the buffer.
1546 if (text.v_type == VAR_STRING)
1547 {
1548 // just a string
1549 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1550 }
1551 else
1552 {
1553 list_T *l = text.vval.v_list;
1554
1555 if (l->lv_len > 0)
1556 {
1557 if (l->lv_first->li_tv.v_type == VAR_STRING)
1558 // list of strings
1559 add_popup_strings(buf, l);
1560 else
1561 // list of dictionaries
1562 add_popup_dicts(buf, l);
1563 }
1564 }
1565
1566 // delete the line that was in the empty buffer
1567 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001568 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001569 curbuf = curwin->w_buffer;
1570}
1571
1572/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001573 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1574 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001575 * Return FAIL if the parsing fails.
1576 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001577 static int
1578parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001579{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001580 char_u *p =
1581#ifdef FEAT_QUICKFIX
1582 !is_preview ? p_cpp :
1583#endif
1584 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001585
Bram Moolenaar258cef52019-08-21 17:29:29 +02001586 if (wp != NULL)
1587 wp->w_popup_flags &= ~POPF_INFO_MENU;
1588
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001589 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001590 {
1591 char_u *e, *dig;
1592 char_u *s = p;
1593 int x;
1594
1595 e = vim_strchr(p, ':');
1596 if (e == NULL || e[1] == NUL)
1597 return FAIL;
1598
1599 p = vim_strchr(e, ',');
1600 if (p == NULL)
1601 p = e + STRLEN(e);
1602 dig = e + 1;
1603 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001604
1605 if (STRNCMP(s, "height:", 7) == 0)
1606 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001607 if (dig != p)
1608 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001609 if (wp != NULL)
1610 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001611 if (is_preview)
1612 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001613 wp->w_maxheight = x;
1614 }
1615 }
1616 else if (STRNCMP(s, "width:", 6) == 0)
1617 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001618 if (dig != p)
1619 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001620 if (wp != NULL)
1621 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001622 if (is_preview)
1623 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001624 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001625 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001626 }
1627 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001628 else if (STRNCMP(s, "highlight:", 10) == 0)
1629 {
1630 if (wp != NULL)
1631 {
1632 int c = *p;
1633
1634 *p = NUL;
1635 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1636 s + 10, OPT_FREE|OPT_LOCAL, 0);
1637 *p = c;
1638 }
1639 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001640 else if (STRNCMP(s, "border:", 7) == 0)
1641 {
1642 char_u *arg = s + 7;
1643 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1644 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1645 int i;
1646
1647 if (!on && !off)
1648 return FAIL;
1649 if (wp != NULL)
1650 {
1651 for (i = 0; i < 4; ++i)
1652 wp->w_popup_border[i] = on ? 1 : 0;
1653 if (off)
1654 // only show the X for close when there is a border
1655 wp->w_popup_close = POPCLOSE_NONE;
1656 }
1657 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001658 else if (STRNCMP(s, "align:", 6) == 0)
1659 {
1660 char_u *arg = s + 6;
1661 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1662 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1663
1664 if (!menu && !item)
1665 return FAIL;
1666 if (wp != NULL && menu)
1667 wp->w_popup_flags |= POPF_INFO_MENU;
1668 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001669 else
1670 return FAIL;
1671 }
1672 return OK;
1673}
1674
1675/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001676 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1677 * is not NULL.
1678 * Return FAIL if the parsing fails.
1679 */
1680 int
1681parse_previewpopup(win_T *wp)
1682{
1683 return parse_popup_option(wp, TRUE);
1684}
1685
1686/*
1687 * Parse the 'completepopup' option and apply the values to window "wp" if it
1688 * is not NULL.
1689 * Return FAIL if the parsing fails.
1690 */
1691 int
1692parse_completepopup(win_T *wp)
1693{
1694 return parse_popup_option(wp, FALSE);
1695}
1696
1697/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001698 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001699 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001700 */
1701 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001702popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001703{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001704 poppos_T ppt = POPPOS_NONE;
1705
1706 if (d != NULL)
1707 ppt = get_pos_entry(d, FALSE);
1708
Bram Moolenaar79648732019-07-18 21:43:07 +02001709 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001710 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001711 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001712 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1713 }
1714 else
1715 {
1716 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1717 if (wp->w_wantline == 0) // cursor in first line
1718 {
1719 wp->w_wantline = 2;
1720 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1721 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1722 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001723 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001724
Bram Moolenaar79648732019-07-18 21:43:07 +02001725 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001726 if (wp->w_wantcol > Columns - width)
1727 {
1728 wp->w_wantcol = Columns - width;
1729 if (wp->w_wantcol < 1)
1730 wp->w_wantcol = 1;
1731 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001732
Bram Moolenaar79648732019-07-18 21:43:07 +02001733 popup_adjust_position(wp);
1734}
1735
1736/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001737 * Set w_wantline and w_wantcol for the a given screen position.
1738 * Caller must take care of running into the window border.
1739 */
1740 void
1741popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1742{
1743 wp->w_wantline = row;
1744 wp->w_wantcol = col;
1745 popup_adjust_position(wp);
1746}
1747
1748/*
1749 * Add a border and lef&right padding.
1750 */
1751 static void
1752add_border_left_right_padding(win_T *wp)
1753{
1754 int i;
1755
1756 for (i = 0; i < 4; ++i)
1757 {
1758 wp->w_popup_border[i] = 1;
1759 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1760 }
1761}
1762
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001763#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001764/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001765 * Return TRUE if there is any popup window with a terminal buffer.
1766 */
1767 static int
1768popup_terminal_exists(void)
1769{
1770 win_T *wp;
1771 tabpage_T *tp;
1772
1773 FOR_ALL_POPUPWINS(wp)
1774 if (wp->w_buffer->b_term != NULL)
1775 return TRUE;
1776 FOR_ALL_TABPAGES(tp)
1777 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1778 if (wp->w_buffer->b_term != NULL)
1779 return TRUE;
1780 return FALSE;
1781}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001782#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001783
1784/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001785 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001786 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001787 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001788 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001789 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001790 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001791popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001792{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001793 win_T *wp;
1794 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001795 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001796 int new_buffer;
1797 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001798 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001799 int nr;
1800 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001801
Bram Moolenaar79648732019-07-18 21:43:07 +02001802 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001803 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001804 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001805 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001806 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001807 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001808 if (buf == NULL)
1809 {
1810 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1811 return NULL;
1812 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001813#ifdef FEAT_TERMINAL
1814 if (buf->b_term != NULL && popup_terminal_exists())
1815 {
1816 emsg(_("E861: Cannot open a second popup with a terminal"));
1817 return NULL;
1818 }
1819#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001820 }
1821 else if (!(argvars[0].v_type == VAR_STRING
1822 && argvars[0].vval.v_string != NULL)
1823 && !(argvars[0].v_type == VAR_LIST
1824 && argvars[0].vval.v_list != NULL))
1825 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001826 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001827 return NULL;
1828 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001829 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001830 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001831 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001832 return NULL;
1833 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001834 d = argvars[1].vval.v_dict;
1835 }
1836
1837 if (d != NULL)
1838 {
1839 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1840 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1841 else if (type == TYPE_NOTIFICATION)
1842 tabnr = -1; // notifications are global by default
1843 else
1844 tabnr = 0;
1845 if (tabnr > 0)
1846 {
1847 tp = find_tabpage(tabnr);
1848 if (tp == NULL)
1849 {
1850 semsg(_("E997: Tabpage not found: %d"), tabnr);
1851 return NULL;
1852 }
1853 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001854 }
1855
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001856 // Create the window and buffer.
1857 wp = win_alloc_popup_win();
1858 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001859 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001860 if (rettv != NULL)
1861 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001862 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001863 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001864
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001865 if (buf != NULL)
1866 {
1867 // use existing buffer
1868 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001869 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001870 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001871 buffer_ensure_loaded(buf);
1872 }
1873 else
1874 {
1875 // create a new buffer associated with the popup
1876 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001877 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001878 if (buf == NULL)
1879 return NULL;
1880 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001881
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001882 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001883
Bram Moolenaar46451042019-08-24 15:50:46 +02001884 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001885 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001886 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001887 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001888 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001889 buf->b_p_ul = -1; // no undo
1890 buf->b_p_swf = FALSE; // no swap file
1891 buf->b_p_bl = FALSE; // unlisted buffer
1892 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001893
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001894 // Avoid that 'buftype' is reset when this buffer is entered.
1895 buf->b_p_initialized = TRUE;
1896 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001897 wp->w_p_wrap = TRUE; // 'wrap' is default on
1898 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001899
Bram Moolenaara3fce622019-06-20 02:31:49 +02001900 if (tp != NULL)
1901 {
1902 // popup on specified tab page
1903 wp->w_next = tp->tp_first_popupwin;
1904 tp->tp_first_popupwin = wp;
1905 }
1906 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001907 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001908 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001909 wp->w_next = curtab->tp_first_popupwin;
1910 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001911 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001912 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001913 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001914 win_T *prev = first_popupwin;
1915
1916 // Global popup: add at the end, so that it gets displayed on top of
1917 // older ones with the same zindex. Matters for notifications.
1918 if (first_popupwin == NULL)
1919 first_popupwin = wp;
1920 else
1921 {
1922 while (prev->w_next != NULL)
1923 prev = prev->w_next;
1924 prev->w_next = wp;
1925 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001926 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001927
Bram Moolenaar79648732019-07-18 21:43:07 +02001928 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001929 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001930
Bram Moolenaar79648732019-07-18 21:43:07 +02001931 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001932 {
1933 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001934 }
1935 if (type == TYPE_ATCURSOR)
1936 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001937 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001938 set_moved_values(wp);
1939 set_moved_columns(wp, FIND_STRING);
1940 }
1941
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001942 if (type == TYPE_BEVAL)
1943 {
1944 wp->w_popup_pos = POPPOS_BOTLEFT;
1945
1946 // by default use the mouse position
1947 wp->w_wantline = mouse_row;
1948 if (wp->w_wantline <= 0) // mouse on first line
1949 {
1950 wp->w_wantline = 2;
1951 wp->w_popup_pos = POPPOS_TOPLEFT;
1952 }
1953 wp->w_wantcol = mouse_col + 1;
1954 set_mousemoved_values(wp);
1955 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1956 }
1957
Bram Moolenaar33796b32019-06-08 16:01:13 +02001958 // set default values
1959 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001960 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001961
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001962 if (type == TYPE_NOTIFICATION)
1963 {
1964 win_T *twp, *nextwin;
1965 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001966
1967 // Try to not overlap with another global popup. Guess we need 3
1968 // more screen lines than buffer lines.
1969 wp->w_wantline = 1;
1970 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1971 {
1972 nextwin = twp->w_next;
1973 if (twp != wp
1974 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1975 && twp->w_winrow <= wp->w_wantline - 1 + height
1976 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1977 {
1978 // move to below this popup and restart the loop to check for
1979 // overlap with other popups
1980 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1981 nextwin = first_popupwin;
1982 }
1983 }
1984 if (wp->w_wantline + height > Rows)
1985 {
1986 // can't avoid overlap, put on top in the hope that message goes
1987 // away soon.
1988 wp->w_wantline = 1;
1989 }
1990
1991 wp->w_wantcol = 10;
1992 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001993 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001994 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001995 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001996 for (i = 0; i < 4; ++i)
1997 wp->w_popup_border[i] = 1;
1998 wp->w_popup_padding[1] = 1;
1999 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002000
2001 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002002 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002003 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2004 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002005 }
2006
Bram Moolenaara730e552019-06-16 19:05:31 +02002007 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002008 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002009 wp->w_popup_pos = POPPOS_CENTER;
2010 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002011 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002012 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002013 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002014 }
2015
Bram Moolenaara730e552019-06-16 19:05:31 +02002016 if (type == TYPE_MENU)
2017 {
2018 typval_T tv;
2019 callback_T callback;
2020
2021 tv.v_type = VAR_STRING;
2022 tv.vval.v_string = (char_u *)"popup_filter_menu";
2023 callback = get_callback(&tv);
2024 if (callback.cb_name != NULL)
2025 set_callback(&wp->w_filter_cb, &callback);
2026
2027 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002028 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002029 }
2030
Bram Moolenaar79648732019-07-18 21:43:07 +02002031 if (type == TYPE_PREVIEW)
2032 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002033 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002034 wp->w_popup_close = POPCLOSE_BUTTON;
2035 for (i = 0; i < 4; ++i)
2036 wp->w_popup_border[i] = 1;
2037 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002038 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002039 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002040# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002041 if (type == TYPE_INFO)
2042 {
2043 wp->w_popup_pos = POPPOS_TOPLEFT;
2044 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2045 wp->w_popup_close = POPCLOSE_BUTTON;
2046 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002047 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002048 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002049# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002050
Bram Moolenaarae943152019-06-16 22:54:14 +02002051 for (i = 0; i < 4; ++i)
2052 VIM_CLEAR(wp->w_border_highlight[i]);
2053 for (i = 0; i < 8; ++i)
2054 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002055 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002056 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002057 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002058
Bram Moolenaar79648732019-07-18 21:43:07 +02002059 if (d != NULL)
2060 // Deal with options.
2061 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002062
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002063#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002064 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2065 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002066#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002067
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002068 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002069
2070 wp->w_vsep_width = 0;
2071
2072 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002073 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002074
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002075#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002076 // When running a terminal in the popup it becomes the current window.
2077 if (buf->b_term != NULL)
2078 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002079#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002080
Bram Moolenaara730e552019-06-16 19:05:31 +02002081 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002082}
2083
2084/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002085 * popup_clear()
2086 */
2087 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002088f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002089{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002090 int force = FALSE;
2091
2092 if (argvars[0].v_type != VAR_UNKNOWN)
2093 force = (int)tv_get_number(&argvars[0]);
2094 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002095}
2096
2097/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002098 * popup_create({text}, {options})
2099 */
2100 void
2101f_popup_create(typval_T *argvars, typval_T *rettv)
2102{
Bram Moolenaar17627312019-06-02 19:53:44 +02002103 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002104}
2105
2106/*
2107 * popup_atcursor({text}, {options})
2108 */
2109 void
2110f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2111{
Bram Moolenaar17627312019-06-02 19:53:44 +02002112 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002113}
2114
2115/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002116 * popup_beval({text}, {options})
2117 */
2118 void
2119f_popup_beval(typval_T *argvars, typval_T *rettv)
2120{
2121 popup_create(argvars, rettv, TYPE_BEVAL);
2122}
2123
2124/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002125 * Invoke the close callback for window "wp" with value "result".
2126 * Careful: The callback may make "wp" invalid!
2127 */
2128 static void
2129invoke_popup_callback(win_T *wp, typval_T *result)
2130{
2131 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002132 typval_T argv[3];
2133
2134 argv[0].v_type = VAR_NUMBER;
2135 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2136
2137 if (result != NULL && result->v_type != VAR_UNKNOWN)
2138 copy_tv(result, &argv[1]);
2139 else
2140 {
2141 argv[1].v_type = VAR_NUMBER;
2142 argv[1].vval.v_number = 0;
2143 }
2144
2145 argv[2].v_type = VAR_UNKNOWN;
2146
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002147 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002148 if (result != NULL)
2149 clear_tv(&argv[1]);
2150 clear_tv(&rettv);
2151}
2152
2153/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002154 * Make "prevwin" the current window, unless it's equal to "wp".
2155 * Otherwise make "firstwin" the current window.
2156 */
2157 static void
2158back_to_prevwin(win_T *wp)
2159{
2160 if (win_valid(prevwin) && wp != prevwin)
2161 win_enter(prevwin, FALSE);
2162 else
2163 win_enter(firstwin, FALSE);
2164}
2165
2166/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002167 * Close popup "wp" and invoke any close callback for it.
2168 */
2169 static void
2170popup_close_and_callback(win_T *wp, typval_T *arg)
2171{
2172 int id = wp->w_id;
2173
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002174#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002175 if (wp == curwin && curbuf->b_term != NULL)
2176 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002177 win_T *owp;
2178
2179 // Closing popup window with a terminal: put focus back on the first
2180 // that works:
2181 // - another popup window with a terminal
2182 // - the previous window
2183 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002184 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002185 if (owp != curwin && owp->w_buffer->b_term != NULL)
2186 break;
2187 if (owp != NULL)
2188 win_enter(owp, FALSE);
2189 else
2190 {
2191 for (owp = curtab->tp_first_popupwin; owp != NULL;
2192 owp = owp->w_next)
2193 if (owp != curwin && owp->w_buffer->b_term != NULL)
2194 break;
2195 if (owp != NULL)
2196 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002197 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002198 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002199 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002200 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002201#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002202
Bram Moolenaar49540192019-12-11 19:34:54 +01002203 // Just in case a check higher up is missing.
2204 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2205 return;
2206
Bram Moolenaarcee52202020-03-11 14:19:58 +01002207 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002208 if (wp->w_close_cb.cb_name != NULL)
2209 // Careful: This may make "wp" invalid.
2210 invoke_popup_callback(wp, arg);
2211
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002212 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002213 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002214}
2215
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002216 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002217popup_close_with_retval(win_T *wp, int retval)
2218{
2219 typval_T res;
2220
2221 res.v_type = VAR_NUMBER;
2222 res.vval.v_number = retval;
2223 popup_close_and_callback(wp, &res);
2224}
2225
Bram Moolenaar3397f742019-06-02 18:40:06 +02002226/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002227 * Close popup "wp" because of a mouse click.
2228 */
2229 void
2230popup_close_for_mouse_click(win_T *wp)
2231{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002232 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002233}
2234
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002235 static void
2236check_mouse_moved(win_T *wp, win_T *mouse_wp)
2237{
2238 // Close the popup when all if these are true:
2239 // - the mouse is not on this popup
2240 // - "mousemoved" was used
2241 // - the mouse is no longer on the same screen row or the mouse column is
2242 // outside of the relevant text
2243 if (wp != mouse_wp
2244 && wp->w_popup_mouse_row != 0
2245 && (wp->w_popup_mouse_row != mouse_row
2246 || mouse_col < wp->w_popup_mouse_mincol
2247 || mouse_col > wp->w_popup_mouse_maxcol))
2248 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002249 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002250 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002251 }
2252}
2253
2254/*
2255 * Called when the mouse moved: may close a popup with "mousemoved".
2256 */
2257 void
2258popup_handle_mouse_moved(void)
2259{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002260 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002261 win_T *mouse_wp;
2262 int row = mouse_row;
2263 int col = mouse_col;
2264
2265 // find the window where the mouse is in
2266 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2267
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002268 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2269 {
2270 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002271 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002272 }
2273 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2274 {
2275 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002276 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002277 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002278}
2279
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002280/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002281 * In a filter: check if the typed key is a mouse event that is used for
2282 * dragging the popup.
2283 */
2284 static void
2285filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2286{
2287 int row = mouse_row;
2288 int col = mouse_col;
2289
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002290 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002291 && is_mouse_key(c)
2292 && (wp == popup_dragwin
2293 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2294 // do not consume the key, allow for dragging the popup
2295 rettv->vval.v_number = 0;
2296}
2297
Bram Moolenaara730e552019-06-16 19:05:31 +02002298/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002299 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002300 */
2301 void
2302f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2303{
2304 int id = tv_get_number(&argvars[0]);
2305 win_T *wp = win_id2wp(id);
2306 char_u *key = tv_get_string(&argvars[1]);
2307 typval_T res;
2308 int c;
2309 linenr_T old_lnum;
2310
2311 // If the popup has been closed do not consume the key.
2312 if (wp == NULL)
2313 return;
2314
2315 c = *key;
2316 if (c == K_SPECIAL && key[1] != NUL)
2317 c = TO_SPECIAL(key[1], key[2]);
2318
2319 // consume all keys until done
2320 rettv->vval.v_number = 1;
2321 res.v_type = VAR_NUMBER;
2322
2323 old_lnum = wp->w_cursor.lnum;
2324 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2325 --wp->w_cursor.lnum;
2326 if ((c == 'j' || c == 'J' || c == K_DOWN)
2327 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2328 ++wp->w_cursor.lnum;
2329 if (old_lnum != wp->w_cursor.lnum)
2330 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002331 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002332 return;
2333 }
2334
2335 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2336 {
2337 // Cancelled, invoke callback with -1
2338 res.vval.v_number = -1;
2339 popup_close_and_callback(wp, &res);
2340 return;
2341 }
2342 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2343 {
2344 // Invoke callback with current index.
2345 res.vval.v_number = wp->w_cursor.lnum;
2346 popup_close_and_callback(wp, &res);
2347 return;
2348 }
2349
2350 filter_handle_drag(wp, c, rettv);
2351}
2352
2353/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002354 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002355 */
2356 void
2357f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2358{
2359 int id = tv_get_number(&argvars[0]);
2360 win_T *wp = win_id2wp(id);
2361 char_u *key = tv_get_string(&argvars[1]);
2362 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002363 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002364
2365 // If the popup has been closed don't consume the key.
2366 if (wp == NULL)
2367 return;
2368
Bram Moolenaara730e552019-06-16 19:05:31 +02002369 c = *key;
2370 if (c == K_SPECIAL && key[1] != NUL)
2371 c = TO_SPECIAL(key[1], key[2]);
2372
Bram Moolenaara42d9452019-06-15 21:46:30 +02002373 // consume all keys until done
2374 rettv->vval.v_number = 1;
2375
Bram Moolenaara730e552019-06-16 19:05:31 +02002376 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002377 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002378 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002379 res.vval.v_number = 0;
2380 else
2381 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002382 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002383 return;
2384 }
2385
2386 // Invoke callback
2387 res.v_type = VAR_NUMBER;
2388 popup_close_and_callback(wp, &res);
2389}
2390
2391/*
2392 * popup_dialog({text}, {options})
2393 */
2394 void
2395f_popup_dialog(typval_T *argvars, typval_T *rettv)
2396{
2397 popup_create(argvars, rettv, TYPE_DIALOG);
2398}
2399
2400/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002401 * popup_menu({text}, {options})
2402 */
2403 void
2404f_popup_menu(typval_T *argvars, typval_T *rettv)
2405{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002406 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002407}
2408
2409/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002410 * popup_notification({text}, {options})
2411 */
2412 void
2413f_popup_notification(typval_T *argvars, typval_T *rettv)
2414{
2415 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2416}
2417
2418/*
2419 * Find the popup window with window-ID "id".
2420 * If the popup window does not exist NULL is returned.
2421 * If the window is not a popup window, and error message is given.
2422 */
2423 static win_T *
2424find_popup_win(int id)
2425{
2426 win_T *wp = win_id2wp(id);
2427
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002428 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002429 {
2430 semsg(_("E993: window %d is not a popup window"), id);
2431 return NULL;
2432 }
2433 return wp;
2434}
2435
2436/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002437 * popup_close({id})
2438 */
2439 void
2440f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2441{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002442 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002443 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002444
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002445 if (
2446# ifdef FEAT_TERMINAL
2447 // if the popup contains a terminal it will become hidden
2448 curbuf->b_term == NULL &&
2449# endif
2450 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002451 return;
2452
2453 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002454 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002455 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002456}
2457
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002458 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002459popup_hide(win_T *wp)
2460{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002461#ifdef FEAT_TERMINAL
2462 if (error_if_term_popup_window())
2463 return;
2464#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002465 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2466 {
2467 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002468 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002469 redraw_all_later(NOT_VALID);
2470 popup_mask_refresh = TRUE;
2471 }
2472}
2473
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002474/*
2475 * popup_hide({id})
2476 */
2477 void
2478f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2479{
2480 int id = (int)tv_get_number(argvars);
2481 win_T *wp = find_popup_win(id);
2482
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002483 if (wp != NULL)
2484 popup_hide(wp);
2485}
2486
2487 void
2488popup_show(win_T *wp)
2489{
2490 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002491 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002492 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002493 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002494 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002495 }
2496}
2497
2498/*
2499 * popup_show({id})
2500 */
2501 void
2502f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2503{
2504 int id = (int)tv_get_number(argvars);
2505 win_T *wp = find_popup_win(id);
2506
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002507 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002508 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002509 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002510#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002511 if (wp->w_popup_flags & POPF_INFO)
2512 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002513#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002514 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002515}
2516
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002517/*
2518 * popup_settext({id}, {text})
2519 */
2520 void
2521f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2522{
2523 int id = (int)tv_get_number(&argvars[0]);
2524 win_T *wp = find_popup_win(id);
2525
2526 if (wp != NULL)
2527 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002528 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2529 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2530 else
2531 {
2532 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002533 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002534 popup_adjust_position(wp);
2535 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002536 }
2537}
2538
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002539 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002540popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002541{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002542 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002543 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002544 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002545 clear_cmdline = TRUE;
2546 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002547
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002548 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002549 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002550}
2551
Bram Moolenaar82106932020-03-13 14:34:38 +01002552 static void
2553error_for_popup_window(void)
2554{
2555 emsg(_("E994: Not allowed in a popup window"));
2556}
2557
2558 int
2559error_if_popup_window(int also_with_term UNUSED)
2560{
2561 // win_execute() may set "curwin" to a popup window temporarily, but many
2562 // commands are disallowed then. When a terminal runs in the popup most
2563 // things are allowed. When a terminal is finished it can be closed.
2564 if (WIN_IS_POPUP(curwin)
2565# ifdef FEAT_TERMINAL
2566 && (also_with_term || curbuf->b_term == NULL)
2567 && !term_is_finished(curbuf)
2568# endif
2569 )
2570 {
2571 error_for_popup_window();
2572 return TRUE;
2573 }
2574 return FALSE;
2575}
2576
Bram Moolenaarec583842019-05-26 14:11:23 +02002577/*
2578 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002579 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002580 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002581 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002582 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002583popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002584{
2585 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002586 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002587 win_T *prev = NULL;
2588
Bram Moolenaarec583842019-05-26 14:11:23 +02002589 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002590 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002591 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002592 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002593 if (wp == curwin)
2594 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002595 if (!force)
2596 {
2597 error_for_popup_window();
2598 return FAIL;
2599 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002600 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002601 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002602 if (prev == NULL)
2603 first_popupwin = wp->w_next;
2604 else
2605 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002606 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002607 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002608 }
2609
Bram Moolenaarec583842019-05-26 14:11:23 +02002610 // go through tab-local popups
2611 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002612 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002613 return OK;
2614 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002615}
2616
2617/*
2618 * Close a popup window with Window-id "id" in tabpage "tp".
2619 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002620 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002621popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002622{
2623 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002624 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002625 win_T *prev = NULL;
2626
Bram Moolenaarec583842019-05-26 14:11:23 +02002627 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2628 if (wp->w_id == id)
2629 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002630 if (wp == curwin)
2631 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002632 if (!force)
2633 {
2634 error_for_popup_window();
2635 return FAIL;
2636 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002637 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002638 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002639 if (prev == NULL)
2640 *root = wp->w_next;
2641 else
2642 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002643 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002644 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002645 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002646 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002647}
2648
2649 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002650close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002651{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002652 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002653 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002654 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002655 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002656 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002657 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002658 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002659 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002660}
2661
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002662/*
2663 * popup_move({id}, {options})
2664 */
2665 void
2666f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2667{
Bram Moolenaarae943152019-06-16 22:54:14 +02002668 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002669 int id = (int)tv_get_number(argvars);
2670 win_T *wp = find_popup_win(id);
2671
2672 if (wp == NULL)
2673 return; // invalid {id}
2674
2675 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2676 {
2677 emsg(_(e_dictreq));
2678 return;
2679 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002680 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002681
Bram Moolenaarae943152019-06-16 22:54:14 +02002682 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002683
2684 if (wp->w_winrow + wp->w_height >= cmdline_row)
2685 clear_cmdline = TRUE;
2686 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002687}
2688
Bram Moolenaarbc133542019-05-29 20:26:46 +02002689/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002690 * popup_setoptions({id}, {options})
2691 */
2692 void
2693f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2694{
2695 dict_T *dict;
2696 int id = (int)tv_get_number(argvars);
2697 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002698 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002699
2700 if (wp == NULL)
2701 return; // invalid {id}
2702
2703 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2704 {
2705 emsg(_(e_dictreq));
2706 return;
2707 }
2708 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002709 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002710
2711 apply_move_options(wp, dict);
2712 apply_general_options(wp, dict);
2713
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002714 if (old_firstline != wp->w_firstline)
2715 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002716 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002717 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002718 popup_adjust_position(wp);
2719}
2720
2721/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002722 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002723 */
2724 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002725f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002726{
2727 dict_T *dict;
2728 int id = (int)tv_get_number(argvars);
2729 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002730 int top_extra;
2731 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002732
2733 if (rettv_dict_alloc(rettv) == OK)
2734 {
2735 if (wp == NULL)
2736 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002737 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002738 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2739
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002740 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002741 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002742 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002743
Bram Moolenaarbc133542019-05-29 20:26:46 +02002744 dict_add_number(dict, "line", wp->w_winrow + 1);
2745 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002746 dict_add_number(dict, "width", wp->w_width + left_extra
2747 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2748 dict_add_number(dict, "height", wp->w_height + top_extra
2749 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002750
2751 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2752 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2753 dict_add_number(dict, "core_width", wp->w_width);
2754 dict_add_number(dict, "core_height", wp->w_height);
2755
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002756 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002757 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002758 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002759 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002760 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002761
2762 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002763 }
2764}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002765
2766/*
2767 * popup_list()
2768 */
2769 void
2770f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2771{
2772 win_T *wp;
2773 tabpage_T *tp;
2774
2775 if (rettv_list_alloc(rettv) != OK)
2776 return;
2777 FOR_ALL_POPUPWINS(wp)
2778 list_append_number(rettv->vval.v_list, wp->w_id);
2779 FOR_ALL_TABPAGES(tp)
2780 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2781 list_append_number(rettv->vval.v_list, wp->w_id);
2782}
2783
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002784/*
2785 * popup_locate({row}, {col})
2786 */
2787 void
2788f_popup_locate(typval_T *argvars, typval_T *rettv)
2789{
2790 int row = tv_get_number(&argvars[0]) - 1;
2791 int col = tv_get_number(&argvars[1]) - 1;
2792 win_T *wp;
2793
2794 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002795 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002796 rettv->vval.v_number = wp->w_id;
2797}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002798
2799/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002800 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2801 */
2802 static void
2803get_padding_border(dict_T *dict, int *array, char *name)
2804{
2805 list_T *list;
2806 int i;
2807
2808 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2809 return;
2810
2811 list = list_alloc();
2812 if (list != NULL)
2813 {
2814 dict_add_list(dict, name, list);
2815 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2816 for (i = 0; i < 4; ++i)
2817 list_append_number(list, array[i]);
2818 }
2819}
2820
2821/*
2822 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2823 */
2824 static void
2825get_borderhighlight(dict_T *dict, win_T *wp)
2826{
2827 list_T *list;
2828 int i;
2829
2830 for (i = 0; i < 4; ++i)
2831 if (wp->w_border_highlight[i] != NULL)
2832 break;
2833 if (i == 4)
2834 return;
2835
2836 list = list_alloc();
2837 if (list != NULL)
2838 {
2839 dict_add_list(dict, "borderhighlight", list);
2840 for (i = 0; i < 4; ++i)
2841 list_append_string(list, wp->w_border_highlight[i], -1);
2842 }
2843}
2844
2845/*
2846 * For popup_getoptions(): add a "borderchars" entry to "dict".
2847 */
2848 static void
2849get_borderchars(dict_T *dict, win_T *wp)
2850{
2851 list_T *list;
2852 int i;
2853 char_u buf[NUMBUFLEN];
2854 int len;
2855
2856 for (i = 0; i < 8; ++i)
2857 if (wp->w_border_char[i] != 0)
2858 break;
2859 if (i == 8)
2860 return;
2861
2862 list = list_alloc();
2863 if (list != NULL)
2864 {
2865 dict_add_list(dict, "borderchars", list);
2866 for (i = 0; i < 8; ++i)
2867 {
2868 len = mb_char2bytes(wp->w_border_char[i], buf);
2869 list_append_string(list, buf, len);
2870 }
2871 }
2872}
2873
2874/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002875 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002876 */
2877 static void
2878get_moved_list(dict_T *dict, win_T *wp)
2879{
2880 list_T *list;
2881
2882 list = list_alloc();
2883 if (list != NULL)
2884 {
2885 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002886 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002887 list_append_number(list, wp->w_popup_mincol);
2888 list_append_number(list, wp->w_popup_maxcol);
2889 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002890 list = list_alloc();
2891 if (list != NULL)
2892 {
2893 dict_add_list(dict, "mousemoved", list);
2894 list_append_number(list, wp->w_popup_mouse_row);
2895 list_append_number(list, wp->w_popup_mouse_mincol);
2896 list_append_number(list, wp->w_popup_mouse_maxcol);
2897 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002898}
2899
2900/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002901 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002902 */
2903 void
2904f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2905{
2906 dict_T *dict;
2907 int id = (int)tv_get_number(argvars);
2908 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002909 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002910 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002911
2912 if (rettv_dict_alloc(rettv) == OK)
2913 {
2914 if (wp == NULL)
2915 return;
2916
2917 dict = rettv->vval.v_dict;
2918 dict_add_number(dict, "line", wp->w_wantline);
2919 dict_add_number(dict, "col", wp->w_wantcol);
2920 dict_add_number(dict, "minwidth", wp->w_minwidth);
2921 dict_add_number(dict, "minheight", wp->w_minheight);
2922 dict_add_number(dict, "maxheight", wp->w_maxheight);
2923 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002924 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002925 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002926 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002927 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002928 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2929 {
2930 proptype_T *pt = text_prop_type_by_id(
2931 wp->w_popup_prop_win->w_buffer,
2932 wp->w_popup_prop_type);
2933
2934 if (pt != NULL)
2935 dict_add_string(dict, "textprop", pt->pt_name);
2936 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2937 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2938 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002939 dict_add_string(dict, "title", wp->w_popup_title);
2940 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002941 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002942 dict_add_number(dict, "mapping",
2943 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002944 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002945 dict_add_number(dict, "posinvert",
2946 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002947 dict_add_number(dict, "cursorline",
2948 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002949 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002950 if (wp->w_scrollbar_highlight != NULL)
2951 dict_add_string(dict, "scrollbarhighlight",
2952 wp->w_scrollbar_highlight);
2953 if (wp->w_thumb_highlight != NULL)
2954 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002955
Bram Moolenaara3fce622019-06-20 02:31:49 +02002956 // find the tabpage that holds this popup
2957 i = 1;
2958 FOR_ALL_TABPAGES(tp)
2959 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002960 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002961
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002962 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
2963 if (twp->w_id == id)
2964 break;
2965 if (twp != NULL)
2966 break;
2967 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002968 }
2969 if (tp == NULL)
2970 i = -1; // must be global
2971 else if (tp == curtab)
2972 i = 0;
2973 dict_add_number(dict, "tabpage", i);
2974
Bram Moolenaarae943152019-06-16 22:54:14 +02002975 get_padding_border(dict, wp->w_popup_padding, "padding");
2976 get_padding_border(dict, wp->w_popup_border, "border");
2977 get_borderhighlight(dict, wp);
2978 get_borderchars(dict, wp);
2979 get_moved_list(dict, wp);
2980
2981 if (wp->w_filter_cb.cb_name != NULL)
2982 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2983 if (wp->w_close_cb.cb_name != NULL)
2984 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002985
2986 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2987 ++i)
2988 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2989 {
2990 dict_add_string(dict, "pos",
2991 (char_u *)poppos_entries[i].pp_name);
2992 break;
2993 }
2994
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002995 dict_add_string(dict, "close", (char_u *)(
2996 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2997 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2998
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002999# if defined(FEAT_TIMERS)
3000 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3001 ? (long)wp->w_popup_timer->tr_interval : 0L);
3002# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003003 }
3004}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003005
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003006# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003007/*
3008 * Return TRUE if the current window is running a terminal in a popup window.
3009 * Return FALSE when the job has ended.
3010 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003011 int
3012error_if_term_popup_window()
3013{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003014 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3015 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003016 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003017 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003018 return TRUE;
3019 }
3020 return FALSE;
3021}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003022# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003023
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003024/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003025 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003026 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003027 * Each calling function should use a different flag, see the list at
3028 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003029 */
3030 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003031popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003032{
3033 win_T *wp;
3034
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003035 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003036 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003037 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003038 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003039}
3040
3041/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003042 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003043 * Must have called popup_reset_handled() first.
3044 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3045 * popup with the highest zindex.
3046 */
3047 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003048find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003049{
3050 win_T *wp;
3051 win_T *found_wp;
3052 int found_zindex;
3053
3054 found_zindex = lowest ? INT_MAX : 0;
3055 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003056 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003057 if ((wp->w_popup_handled & handled_flag) == 0
3058 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003059 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003060 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003061 {
3062 found_zindex = wp->w_zindex;
3063 found_wp = wp;
3064 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003065 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003066 if ((wp->w_popup_handled & handled_flag) == 0
3067 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003068 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003069 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003070 {
3071 found_zindex = wp->w_zindex;
3072 found_wp = wp;
3073 }
3074
3075 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003076 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003077 return found_wp;
3078}
3079
3080/*
3081 * Invoke the filter callback for window "wp" with typed character "c".
3082 * Uses the global "mod_mask" for modifiers.
3083 * Returns the return value of the filter.
3084 * Careful: The filter may make "wp" invalid!
3085 */
3086 static int
3087invoke_popup_filter(win_T *wp, int c)
3088{
3089 int res;
3090 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003091 typval_T argv[3];
3092 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003093 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003094
Bram Moolenaara42d9452019-06-15 21:46:30 +02003095 // Emergency exit: CTRL-C closes the popup.
3096 if (c == Ctrl_C)
3097 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003098 int save_got_int = got_int;
3099
3100 // Reset got_int to avoid the callback isn't called.
3101 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003102 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003103 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003104 return 1;
3105 }
3106
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003107 argv[0].v_type = VAR_NUMBER;
3108 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3109
3110 // Convert the number to a string, so that the function can use:
3111 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003112 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003113 argv[1].v_type = VAR_STRING;
3114 argv[1].vval.v_string = vim_strsave(buf);
3115
3116 argv[2].v_type = VAR_UNKNOWN;
3117
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003118 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003119 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003120 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003121 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003122 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003123
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003124 vim_free(argv[1].vval.v_string);
3125 clear_tv(&rettv);
3126 return res;
3127}
3128
3129/*
3130 * Called when "c" was typed: invoke popup filter callbacks.
3131 * Returns TRUE when the character was consumed,
3132 */
3133 int
3134popup_do_filter(int c)
3135{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003136 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003137 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003138 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003139 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003140 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003141 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003142
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003143#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003144 // Popup window with terminal always gets focus.
3145 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3146 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003147#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003148
Bram Moolenaar934470e2019-09-01 23:27:05 +02003149 if (recursive)
3150 return FALSE;
3151 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003152
Bram Moolenaarf6396232019-08-24 19:36:00 +02003153 if (c == K_LEFTMOUSE)
3154 {
3155 int row = mouse_row;
3156 int col = mouse_col;
3157
3158 wp = mouse_find_win(&row, &col, FIND_POPUP);
3159 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003160 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003161 }
3162
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003163 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003164 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003165 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003166 if (wp->w_filter_cb.cb_name != NULL
3167 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003168 res = invoke_popup_filter(wp, c);
3169
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003170 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003171 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02003172 recursive = FALSE;
3173 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003174 return res;
3175}
3176
Bram Moolenaar3397f742019-06-02 18:40:06 +02003177/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003178 * Return TRUE if there is a popup visible with a filter callback and the
3179 * "mapping" property off.
3180 */
3181 int
3182popup_no_mapping(void)
3183{
3184 int round;
3185 win_T *wp;
3186
3187 for (round = 1; round <= 2; ++round)
3188 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3189 wp != NULL; wp = wp->w_next)
3190 if (wp->w_filter_cb.cb_name != NULL
3191 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3192 return TRUE;
3193 return FALSE;
3194}
3195
3196/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003197 * Called when the cursor moved: check if any popup needs to be closed if the
3198 * cursor moved far enough.
3199 */
3200 void
3201popup_check_cursor_pos()
3202{
3203 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003204
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003205 popup_reset_handled(POPUP_HANDLED_3);
3206 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003207 if (wp->w_popup_curwin != NULL
3208 && (curwin != wp->w_popup_curwin
3209 || curwin->w_cursor.lnum != wp->w_popup_lnum
3210 || curwin->w_cursor.col < wp->w_popup_mincol
3211 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003212 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003213}
3214
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003215/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003216 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003217 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003218 static void
3219popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003220{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003221 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003222 char_u *cells;
3223 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003224
3225 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003226 return;
3227 if (wp->w_popup_mask_cells != NULL
3228 && wp->w_popup_mask_height == height
3229 && wp->w_popup_mask_width == width)
3230 return; // cache is still valid
3231
3232 vim_free(wp->w_popup_mask_cells);
3233 wp->w_popup_mask_cells = alloc_clear(width * height);
3234 if (wp->w_popup_mask_cells == NULL)
3235 return;
3236 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003237
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003238 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003239 {
3240 int cols, cole;
3241 int lines, linee;
3242
3243 li = lio->li_tv.vval.v_list->lv_first;
3244 cols = tv_get_number(&li->li_tv);
3245 if (cols < 0)
3246 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003247 li = li->li_next;
3248 cole = tv_get_number(&li->li_tv);
3249 if (cole < 0)
3250 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003251 li = li->li_next;
3252 lines = tv_get_number(&li->li_tv);
3253 if (lines < 0)
3254 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003255 li = li->li_next;
3256 linee = tv_get_number(&li->li_tv);
3257 if (linee < 0)
3258 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003259
3260 for (row = lines - 1; row < linee && row < height; ++row)
3261 for (col = cols - 1; col < cole && col < width; ++col)
3262 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003263 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003264}
3265
3266/*
3267 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3268 * "col" and "line" are screen coordinates.
3269 */
3270 static int
3271popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3272{
3273 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3274 int line = screenline - wp->w_winrow;
3275
3276 return col >= 0 && col < width
3277 && line >= 0 && line < height
3278 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003279}
3280
3281/*
3282 * Set flags in popup_transparent[] for window "wp" to "val".
3283 */
3284 static void
3285update_popup_transparent(win_T *wp, int val)
3286{
3287 if (wp->w_popup_mask != NULL)
3288 {
3289 int width = popup_width(wp);
3290 int height = popup_height(wp);
3291 listitem_T *lio, *li;
3292 int cols, cole;
3293 int lines, linee;
3294 int col, line;
3295
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003296 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003297 {
3298 li = lio->li_tv.vval.v_list->lv_first;
3299 cols = tv_get_number(&li->li_tv);
3300 if (cols < 0)
3301 cols = width + cols + 1;
3302 li = li->li_next;
3303 cole = tv_get_number(&li->li_tv);
3304 if (cole < 0)
3305 cole = width + cole + 1;
3306 li = li->li_next;
3307 lines = tv_get_number(&li->li_tv);
3308 if (lines < 0)
3309 lines = height + lines + 1;
3310 li = li->li_next;
3311 linee = tv_get_number(&li->li_tv);
3312 if (linee < 0)
3313 linee = height + linee + 1;
3314
3315 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003316 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003317 if (cols < 0)
3318 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003319 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003320 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003321 if (lines < 0)
3322 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003323 for (line = lines; line < linee
3324 && line + wp->w_winrow < screen_Rows; ++line)
3325 for (col = cols; col < cole
3326 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003327 popup_transparent[(line + wp->w_winrow) * screen_Columns
3328 + col + wp->w_wincol] = val;
3329 }
3330 }
3331}
3332
3333/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003334 * Only called when popup window "wp" is hidden: If the window is positioned
3335 * next to a text property, and it is now visible, then unhide the popup.
3336 * We don't check if visible popups become hidden, that is done in
3337 * popup_adjust_position().
3338 * Return TRUE if the popup became unhidden.
3339 */
3340 static int
3341check_popup_unhidden(win_T *wp)
3342{
3343 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3344 {
3345 textprop_T prop;
3346 linenr_T lnum;
3347
3348 if (find_visible_prop(wp->w_popup_prop_win,
3349 wp->w_popup_prop_type, wp->w_popup_prop_id,
3350 &prop, &lnum) == OK)
3351 {
3352 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003353 wp->w_popup_prop_topline = 0; // force repositioning
3354 return TRUE;
3355 }
3356 }
3357 return FALSE;
3358}
3359
3360/*
3361 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3362 * That is when the buffer in the popup was changed, or the popup is following
3363 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003364 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003365 */
3366 static int
3367popup_need_position_adjust(win_T *wp)
3368{
3369 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3370 return TRUE;
3371 if (win_valid(wp->w_popup_prop_win))
3372 return wp->w_popup_prop_changedtick
3373 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003374 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3375 || ((wp->w_popup_flags & POPF_CURSORLINE)
3376 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003377 return FALSE;
3378}
3379
3380/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003381 * Update "popup_mask" if needed.
3382 * Also recomputes the popup size and positions.
3383 * Also updates "popup_visible".
3384 * Also marks window lines for redrawing.
3385 */
3386 void
3387may_update_popup_mask(int type)
3388{
3389 win_T *wp;
3390 short *mask;
3391 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003392 int redraw_all_popups = FALSE;
3393 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003394
3395 // Need to recompute when switching tabs.
3396 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3397 // (such as the screen size) must have changed.
3398 if (popup_mask_tab != curtab || type >= NOT_VALID)
3399 {
3400 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003401 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003402 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003403
3404 // Check if any popup window buffer has changed and if any popup connected
3405 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003406 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003407 if (wp->w_popup_flags & POPF_HIDDEN)
3408 popup_mask_refresh |= check_popup_unhidden(wp);
3409 else if (popup_need_position_adjust(wp))
3410 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003411 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003412 if (wp->w_popup_flags & POPF_HIDDEN)
3413 popup_mask_refresh |= check_popup_unhidden(wp);
3414 else if (popup_need_position_adjust(wp))
3415 popup_mask_refresh = TRUE;
3416
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003417 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003418 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003419
3420 // Need to update the mask, something has changed.
3421 popup_mask_refresh = FALSE;
3422 popup_mask_tab = curtab;
3423 popup_visible = FALSE;
3424
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003425 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003426 // If redrawing only what is needed, update "popup_mask_next" and then
3427 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003428 redrawing_all_win = TRUE;
3429 FOR_ALL_WINDOWS(wp)
3430 if (wp->w_redr_type < SOME_VALID)
3431 redrawing_all_win = FALSE;
3432 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003433 mask = popup_mask;
3434 else
3435 mask = popup_mask_next;
3436 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3437
3438 // Find the window with the lowest zindex that hasn't been handled yet,
3439 // so that the window with a higher zindex overwrites the value in
3440 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003441 popup_reset_handled(POPUP_HANDLED_4);
3442 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003443 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003444 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003445 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003446
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003447 popup_visible = TRUE;
3448
Bram Moolenaar12034e22019-08-25 22:25:02 +02003449 // Recompute the position if the text changed. It may make the popup
3450 // hidden if it's attach to a text property that is no longer visible.
3451 if (redraw_all_popups || popup_need_position_adjust(wp))
3452 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003453 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003454 if (wp->w_popup_flags & POPF_HIDDEN)
3455 continue;
3456 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003457
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003458 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003459 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003460 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003461 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003462 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003463 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003464 col < wp->w_wincol + width - wp->w_popup_leftoff
3465 && col < screen_Columns; ++col)
3466 if (wp->w_popup_mask_cells == NULL
3467 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003468 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003469 }
3470
3471 // Only check which lines are to be updated if not already
3472 // updating all lines.
3473 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003474 {
3475 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3476 win_T *prev_wp = NULL;
3477
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003478 for (line = 0; line < screen_Rows; ++line)
3479 {
3480 int col_done = 0;
3481
3482 for (col = 0; col < screen_Columns; ++col)
3483 {
3484 int off = line * screen_Columns + col;
3485
3486 if (popup_mask[off] != popup_mask_next[off])
3487 {
3488 popup_mask[off] = popup_mask_next[off];
3489
3490 if (line >= cmdline_row)
3491 {
3492 // the command line needs to be cleared if text below
3493 // the popup is now visible.
3494 if (!msg_scrolled && popup_mask_next[off] == 0)
3495 clear_cmdline = TRUE;
3496 }
3497 else if (col >= col_done)
3498 {
3499 linenr_T lnum;
3500 int line_cp = line;
3501 int col_cp = col;
3502
3503 // The screen position "line" / "col" needs to be
3504 // redrawn. Figure out what window that is and update
3505 // w_redraw_top and w_redr_bot. Only needs to be done
3506 // once for each window line.
3507 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3508 if (wp != NULL)
3509 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003510#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003511 // A terminal window needs to be redrawn.
3512 if (bt_terminal(wp->w_buffer))
3513 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003514 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003515#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003516 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003517 if (wp != prev_wp)
3518 {
3519 vim_memset(plines_cache, 0,
3520 sizeof(int) * Rows);
3521 prev_wp = wp;
3522 }
3523
3524 if (line_cp >= wp->w_height)
3525 // In (or below) status line
3526 wp->w_redr_status = TRUE;
3527 else
3528 {
3529 // compute the position in the buffer line
3530 // from the position in the window
3531 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003532 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003533 redrawWinline(wp, lnum);
3534 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003535 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003536
3537 // This line is going to be redrawn, no need to
3538 // check until the right side of the window.
3539 col_done = wp->w_wincol + wp->w_width - 1;
3540 }
3541 }
3542 }
3543 }
3544 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003545
3546 vim_free(plines_cache);
3547 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003548}
3549
3550/*
3551 * Return a string of "len" spaces in IObuff.
3552 */
3553 static char_u *
3554get_spaces(int len)
3555{
3556 vim_memset(IObuff, ' ', (size_t)len);
3557 IObuff[len] = NUL;
3558 return IObuff;
3559}
3560
3561/*
3562 * Update popup windows. They are drawn on top of normal windows.
3563 * "win_update" is called for each popup window, lowest zindex first.
3564 */
3565 void
3566update_popups(void (*win_update)(win_T *wp))
3567{
3568 win_T *wp;
3569 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003570 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003571 int total_width;
3572 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003573 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003574 int popup_attr;
3575 int border_attr[4];
3576 int border_char[8];
3577 char_u buf[MB_MAXBYTES];
3578 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003579 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003580 int padcol = 0;
3581 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003582 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003583 int sb_thumb_top = 0;
3584 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003585 int attr_scroll = 0;
3586 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003587
3588 // Find the window with the lowest zindex that hasn't been updated yet,
3589 // so that the window with a higher zindex is drawn later, thus goes on
3590 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003591 popup_reset_handled(POPUP_HANDLED_5);
3592 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003593 {
3594 // This drawing uses the zindex of the popup window, so that it's on
3595 // top of the text but doesn't draw when another popup with higher
3596 // zindex is on top of the character.
3597 screen_zindex = wp->w_zindex;
3598
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003599 // Set flags in popup_transparent[] for masked cells.
3600 update_popup_transparent(wp, 1);
3601
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003602 // adjust w_winrow and w_wincol for border and padding, since
3603 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003604 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003605 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3606 - wp->w_popup_leftoff;
3607 if (wp->w_wincol + left_extra < 0)
3608 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003609 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003610 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003611
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003612 // Draw the popup text, unless it's off screen.
3613 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003614 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003615 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003616
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003617 // move the cursor into the visible lines, otherwise executing
3618 // commands with win_execute() may cause the text to jump.
3619 if (wp->w_cursor.lnum < wp->w_topline)
3620 wp->w_cursor.lnum = wp->w_topline;
3621 else if (wp->w_cursor.lnum >= wp->w_botline)
3622 wp->w_cursor.lnum = wp->w_botline - 1;
3623 }
3624
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003625 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003626 wp->w_wincol -= left_extra;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003627 // cursor position matters in terminal in job mode
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003628#ifdef FEAT_TERMINAL
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003629 if (wp != curwin || !term_in_normal_mode())
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003630#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003631 {
3632 wp->w_wrow += top_off;
3633 wp->w_wcol += left_extra;
3634 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003635
Bram Moolenaard529ba52019-07-02 23:13:53 +02003636 total_width = popup_width(wp);
3637 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003638 popup_attr = get_wcr_attr(wp);
3639
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003640 if (wp->w_winrow + total_height > cmdline_row)
3641 wp->w_popup_flags |= POPF_ON_CMDLINE;
3642 else
3643 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3644
3645
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003646 // We can only use these line drawing characters when 'encoding' is
3647 // "utf-8" and 'ambiwidth' is "single".
3648 if (enc_utf8 && *p_ambw == 's')
3649 {
3650 border_char[0] = border_char[2] = 0x2550;
3651 border_char[1] = border_char[3] = 0x2551;
3652 border_char[4] = 0x2554;
3653 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003654 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3655 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003656 border_char[7] = 0x255a;
3657 }
3658 else
3659 {
3660 border_char[0] = border_char[2] = '-';
3661 border_char[1] = border_char[3] = '|';
3662 for (i = 4; i < 8; ++i)
3663 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003664 if (wp->w_popup_flags & POPF_RESIZE)
3665 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003666 }
3667 for (i = 0; i < 8; ++i)
3668 if (wp->w_border_char[i] != 0)
3669 border_char[i] = wp->w_border_char[i];
3670
3671 for (i = 0; i < 4; ++i)
3672 {
3673 border_attr[i] = popup_attr;
3674 if (wp->w_border_highlight[i] != NULL)
3675 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3676 }
3677
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003678 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003679 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003680 if (wp->w_popup_border[0] > 0)
3681 {
3682 // top border
3683 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003684 wincol < 0 ? 0 : wincol, wincol + total_width,
3685 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003686 ? border_char[4] : border_char[0],
3687 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003688 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003689 {
3690 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3691 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003692 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003693 }
3694 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003695 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3696 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003697
Bram Moolenaard529ba52019-07-02 23:13:53 +02003698 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3699 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003700 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003701 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3702 - wp->w_has_scrollbar;
3703 if (padcol < 0)
3704 {
3705 padwidth += padcol;
3706 padcol = 0;
3707 }
3708 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003709 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003710 {
3711 // top padding
3712 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003713 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003714 ' ', ' ', popup_attr);
3715 }
3716
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003717 // Title goes on top of border or padding.
3718 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003719 {
3720 int len = (int)STRLEN(wp->w_popup_title) + 1;
3721 char_u *title = alloc(len);
3722
3723 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3724 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003725 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003726 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003727 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003728
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003729 // Compute scrollbar thumb position and size.
3730 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003731 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003732 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3733 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003734
Bram Moolenaar076d9882019-09-14 22:23:29 +02003735 sb_thumb_height = (height * height + linecount / 2) / linecount;
3736 if (wp->w_topline > 1 && sb_thumb_height == height)
3737 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003738 if (sb_thumb_height == 0)
3739 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003740 if (linecount <= wp->w_height)
3741 // it just fits, avoid divide by zero
3742 sb_thumb_top = 0;
3743 else
3744 sb_thumb_top = (wp->w_topline - 1
3745 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003746 * (wp->w_height - sb_thumb_height)
3747 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003748 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3749 sb_thumb_top = 1; // show it's scrolled
3750
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003751 if (wp->w_scrollbar_highlight != NULL)
3752 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3753 else
3754 attr_scroll = highlight_attr[HLF_PSB];
3755 if (wp->w_thumb_highlight != NULL)
3756 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3757 else
3758 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003759 }
3760
3761 for (i = wp->w_popup_border[0];
3762 i < total_height - wp->w_popup_border[2]; ++i)
3763 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003764 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003765 // left and right padding only needed next to the body
3766 int do_padding =
3767 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3768 && i < total_height - wp->w_popup_border[2]
3769 - wp->w_popup_padding[2];
3770
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003771 row = wp->w_winrow + i;
3772
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003774 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 {
3776 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003777 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003778 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003779 if (do_padding && wp->w_popup_padding[3] > 0)
3780 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003781 int col = wincol + wp->w_popup_border[3];
3782
Bram Moolenaard529ba52019-07-02 23:13:53 +02003783 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003784 pad_left = wp->w_popup_padding[3];
3785 if (col < 0)
3786 {
3787 pad_left += col;
3788 col = 0;
3789 }
3790 if (pad_left > 0)
3791 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3792 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003793 // scrollbar
3794 if (wp->w_has_scrollbar)
3795 {
3796 int line = i - top_off;
3797 int scroll_col = wp->w_wincol + total_width - 1
3798 - wp->w_popup_border[1];
3799
3800 if (line >= 0 && line < wp->w_height)
3801 screen_putchar(' ', row, scroll_col,
3802 line >= sb_thumb_top
3803 && line < sb_thumb_top + sb_thumb_height
3804 ? attr_thumb : attr_scroll);
3805 else
3806 screen_putchar(' ', row, scroll_col, popup_attr);
3807 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003808 // right border
3809 if (wp->w_popup_border[1] > 0)
3810 {
3811 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003812 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003813 }
3814 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003815 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003816 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003817 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003818 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3819 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003820 }
3821
3822 if (wp->w_popup_padding[2] > 0)
3823 {
3824 // bottom padding
3825 row = wp->w_winrow + wp->w_popup_border[0]
3826 + wp->w_popup_padding[0] + wp->w_height;
3827 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003828 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003829 }
3830
3831 if (wp->w_popup_border[2] > 0)
3832 {
3833 // bottom border
3834 row = wp->w_winrow + total_height - 1;
3835 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003836 wincol < 0 ? 0 : wincol,
3837 wincol + total_width,
3838 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003839 ? border_char[7] : border_char[2],
3840 border_char[2], border_attr[2]);
3841 if (wp->w_popup_border[1] > 0)
3842 {
3843 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003844 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003845 }
3846 }
3847
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003848 if (wp->w_popup_close == POPCLOSE_BUTTON)
3849 {
3850 // close button goes on top of anything at the top-right corner
3851 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003852 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003853 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3854 }
3855
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003856 update_popup_transparent(wp, 0);
3857
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003858 // Back to the normal zindex.
3859 screen_zindex = 0;
3860 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02003861
3862#if defined(FEAT_SEARCH_EXTRA)
3863 // In case win_update() called start_search_hl().
3864 end_search_hl();
3865#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003866}
3867
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003868/*
3869 * Mark references in callbacks of one popup window.
3870 */
3871 static int
3872set_ref_in_one_popup(win_T *wp, int copyID)
3873{
3874 int abort = FALSE;
3875 typval_T tv;
3876
3877 if (wp->w_close_cb.cb_partial != NULL)
3878 {
3879 tv.v_type = VAR_PARTIAL;
3880 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3881 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3882 }
3883 if (wp->w_filter_cb.cb_partial != NULL)
3884 {
3885 tv.v_type = VAR_PARTIAL;
3886 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3887 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3888 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003889 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003890 return abort;
3891}
3892
3893/*
3894 * Set reference in callbacks of popup windows.
3895 */
3896 int
3897set_ref_in_popups(int copyID)
3898{
3899 int abort = FALSE;
3900 win_T *wp;
3901 tabpage_T *tp;
3902
3903 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3904 abort = abort || set_ref_in_one_popup(wp, copyID);
3905
3906 FOR_ALL_TABPAGES(tp)
3907 {
3908 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3909 abort = abort || set_ref_in_one_popup(wp, copyID);
3910 if (abort)
3911 break;
3912 }
3913 return abort;
3914}
Bram Moolenaar79648732019-07-18 21:43:07 +02003915
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003916 int
3917popup_is_popup(win_T *wp)
3918{
3919 return wp->w_popup_flags != 0;
3920}
3921
3922#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003923/*
3924 * Find an existing popup used as the preview window, in the current tab page.
3925 * Return NULL if not found.
3926 */
3927 win_T *
3928popup_find_preview_window(void)
3929{
3930 win_T *wp;
3931
3932 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003933 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02003934 if (wp->w_p_pvw)
3935 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003936 return NULL;
3937}
3938
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003939/*
3940 * Find an existing popup used as the info window, in the current tab page.
3941 * Return NULL if not found.
3942 */
3943 win_T *
3944popup_find_info_window(void)
3945{
3946 win_T *wp;
3947
3948 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003949 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003950 if (wp->w_popup_flags & POPF_INFO)
3951 return wp;
3952 return NULL;
3953}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003954#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003955
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003956 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003957f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3958{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003959#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003960 win_T *wp = popup_find_info_window();
3961
3962 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003963#else
3964 rettv->vval.v_number = 0;
3965#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003966}
3967
3968 void
3969f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003970{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003971#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003972 win_T *wp = popup_find_preview_window();
3973
3974 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003975#else
3976 rettv->vval.v_number = 0;
3977#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003978}
3979
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003980#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003981/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003982 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003983 * NOTE: this makes the popup the current window, so that the file can be
3984 * edited. However, it must not remain to be the current window, the caller
3985 * must make sure of that.
3986 */
3987 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003988popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003989{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003990 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003991
3992 if (wp == NULL)
3993 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003994 if (info)
3995 wp->w_popup_flags |= POPF_INFO;
3996 else
3997 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003998
3999 // Set the width to a reasonable value, so that w_topline can be computed.
4000 if (wp->w_minwidth > 0)
4001 wp->w_width = wp->w_minwidth;
4002 else if (wp->w_maxwidth > 0)
4003 wp->w_width = wp->w_maxwidth;
4004 else
4005 wp->w_width = curwin->w_width;
4006
4007 // Will switch to another buffer soon, dummy one can be wiped.
4008 wp->w_buffer->b_locked = FALSE;
4009
4010 win_enter(wp, FALSE);
4011 return OK;
4012}
4013
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004014/*
4015 * Close any preview popup.
4016 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004017 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004018popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004019{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004020 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004021
4022 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004023 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004024}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004025
4026/*
4027 * Hide the info popup.
4028 */
4029 void
4030popup_hide_info(void)
4031{
4032 win_T *wp = popup_find_info_window();
4033
4034 if (wp != NULL)
4035 popup_hide(wp);
4036}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004037
4038/*
4039 * Close any info popup.
4040 */
4041 void
4042popup_close_info(void)
4043{
4044 win_T *wp = popup_find_info_window();
4045
4046 if (wp != NULL)
4047 popup_close_with_retval(wp, -1);
4048}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004049#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004050
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004051/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004052 * Close any popup for a text property associated with "win".
4053 * Return TRUE if a popup was closed.
4054 */
4055 int
4056popup_win_closed(win_T *win)
4057{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004058 int round;
4059 win_T *wp;
4060 win_T *next;
4061 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004062
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004063 for (round = 1; round <= 2; ++round)
4064 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4065 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004066 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004067 next = wp->w_next;
4068 if (wp->w_popup_prop_win == win)
4069 {
4070 popup_close_with_retval(wp, -1);
4071 ret = TRUE;
4072 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004073 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004074 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004075}
4076
4077/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004078 * Set the title of the popup window to the file name.
4079 */
4080 void
4081popup_set_title(win_T *wp)
4082{
4083 if (wp->w_buffer->b_fname != NULL)
4084 {
4085 char_u dirname[MAXPATHL];
4086 size_t len;
4087
4088 mch_dirname(dirname, MAXPATHL);
4089 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4090
4091 vim_free(wp->w_popup_title);
4092 len = STRLEN(wp->w_buffer->b_fname) + 3;
4093 wp->w_popup_title = alloc((int)len);
4094 if (wp->w_popup_title != NULL)
4095 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4096 wp->w_buffer->b_fname);
4097 redraw_win_later(wp, VALID);
4098 }
4099}
4100
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004101# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004102/*
4103 * If there is a preview window, update the title.
4104 * Used after changing directory.
4105 */
4106 void
4107popup_update_preview_title(void)
4108{
4109 win_T *wp = popup_find_preview_window();
4110
4111 if (wp != NULL)
4112 popup_set_title(wp);
4113}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004114# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004115
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004116#endif // FEAT_PROP_POPUP