blob: fc69c90d9d7cd794a15d3b5ca7a0468582a9e0fe [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 Moolenaarcb5ff342019-07-20 16:51:19 +0200743 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
744 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200745 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
746 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200747
Bram Moolenaar790498b2019-06-01 22:15:29 +0200748 di = dict_find(dict, (char_u *)"borderhighlight", -1);
749 if (di != NULL)
750 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200751 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200752 emsg(_(e_listreq));
753 else
754 {
755 list_T *list = di->di_tv.vval.v_list;
756 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200757 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200758
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200759 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
761 ++i, li = li->li_next)
762 {
763 str = tv_get_string(&li->li_tv);
764 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100765 {
766 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100768 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200769 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200770 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
771 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100772 {
773 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200774 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200775 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100776 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200777 }
778 }
779
Bram Moolenaar790498b2019-06-01 22:15:29 +0200780 di = dict_find(dict, (char_u *)"borderchars", -1);
781 if (di != NULL)
782 {
783 if (di->di_tv.v_type != VAR_LIST)
784 emsg(_(e_listreq));
785 else
786 {
787 list_T *list = di->di_tv.vval.v_list;
788 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200789 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200790
791 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100792 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200793 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200794 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
795 ++i, li = li->li_next)
796 {
797 str = tv_get_string(&li->li_tv);
798 if (*str != NUL)
799 wp->w_border_char[i] = mb_ptr2char(str);
800 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200801 if (list->lv_len == 1)
802 for (i = 1; i < 8; ++i)
803 wp->w_border_char[i] = wp->w_border_char[0];
804 if (list->lv_len == 2)
805 {
806 for (i = 4; i < 8; ++i)
807 wp->w_border_char[i] = wp->w_border_char[1];
808 for (i = 1; i < 4; ++i)
809 wp->w_border_char[i] = wp->w_border_char[0];
810 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200811 }
812 }
813 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200814
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200815 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
816 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
817
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 di = dict_find(dict, (char_u *)"zindex", -1);
819 if (di != NULL)
820 {
821 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
822 if (wp->w_zindex < 1)
823 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
824 if (wp->w_zindex > 32000)
825 wp->w_zindex = 32000;
826 }
827
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200828 di = dict_find(dict, (char_u *)"mask", -1);
829 if (di != NULL)
830 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200831 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200833 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200834 {
835 listitem_T *li;
836
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200837 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200838 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200839 {
840 if (li->li_tv.v_type != VAR_LIST
841 || li->li_tv.vval.v_list == NULL
842 || li->li_tv.vval.v_list->lv_len != 4)
843 {
844 ok = FALSE;
845 break;
846 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100847 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200848 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200849 }
850 }
851 if (ok)
852 {
853 wp->w_popup_mask = di->di_tv.vval.v_list;
854 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200855 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200856 }
857 else
858 semsg(_(e_invargval), "mask");
859 }
860
Bram Moolenaarae943152019-06-16 22:54:14 +0200861#if defined(FEAT_TIMERS)
862 // Add timer to close the popup after some time.
863 nr = dict_get_number(dict, (char_u *)"time");
864 if (nr > 0)
865 popup_add_timeout(wp, nr);
866#endif
867
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868 di = dict_find(dict, (char_u *)"moved", -1);
869 if (di != NULL)
870 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200871 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200872 handle_moved_argument(wp, di, FALSE);
873 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200874
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200875 di = dict_find(dict, (char_u *)"mousemoved", -1);
876 if (di != NULL)
877 {
878 set_mousemoved_values(wp);
879 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200880 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200881
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200882 di = dict_find(dict, (char_u *)"cursorline", -1);
883 if (di != NULL)
884 {
885 if (di->di_tv.v_type == VAR_NUMBER)
886 {
887 if (di->di_tv.vval.v_number != 0)
888 wp->w_popup_flags |= POPF_CURSORLINE;
889 else
890 wp->w_popup_flags &= ~POPF_CURSORLINE;
891 }
892 else
893 semsg(_(e_invargval), "cursorline");
894 }
895
Bram Moolenaarae943152019-06-16 22:54:14 +0200896 di = dict_find(dict, (char_u *)"filter", -1);
897 if (di != NULL)
898 {
899 callback_T callback = get_callback(&di->di_tv);
900
901 if (callback.cb_name != NULL)
902 {
903 free_callback(&wp->w_filter_cb);
904 set_callback(&wp->w_filter_cb, &callback);
905 }
906 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200907 di = dict_find(dict, (char_u *)"mapping", -1);
908 if (di != NULL)
909 {
910 nr = dict_get_number(dict, (char_u *)"mapping");
911 if (nr)
912 wp->w_popup_flags |= POPF_MAPPING;
913 else
914 wp->w_popup_flags &= ~POPF_MAPPING;
915 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200916
Bram Moolenaar581ba392019-09-03 22:08:33 +0200917 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
918 if (str != NULL)
919 {
920 if (STRCMP(str, "a") == 0)
921 wp->w_filter_mode = MODE_ALL;
922 else
923 wp->w_filter_mode = mode_str2flags(str);
924 }
925
Bram Moolenaarae943152019-06-16 22:54:14 +0200926 di = dict_find(dict, (char_u *)"callback", -1);
927 if (di != NULL)
928 {
929 callback_T callback = get_callback(&di->di_tv);
930
931 if (callback.cb_name != NULL)
932 {
933 free_callback(&wp->w_close_cb);
934 set_callback(&wp->w_close_cb, &callback);
935 }
936 }
937}
938
939/*
940 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200941 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200942 */
943 static void
944apply_options(win_T *wp, dict_T *dict)
945{
946 int nr;
947
948 apply_move_options(wp, dict);
949 apply_general_options(wp, dict);
950
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200951 nr = dict_get_number(dict, (char_u *)"hidden");
952 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200953 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200954
Bram Moolenaar33796b32019-06-08 16:01:13 +0200955 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200956 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200957}
958
959/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200960 * Add lines to the popup from a list of strings.
961 */
962 static void
963add_popup_strings(buf_T *buf, list_T *l)
964{
965 listitem_T *li;
966 linenr_T lnum = 0;
967 char_u *p;
968
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200969 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200970 if (li->li_tv.v_type == VAR_STRING)
971 {
972 p = li->li_tv.vval.v_string;
973 ml_append_buf(buf, lnum++,
974 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
975 }
976}
977
978/*
979 * Add lines to the popup from a list of dictionaries.
980 */
981 static void
982add_popup_dicts(buf_T *buf, list_T *l)
983{
984 listitem_T *li;
985 listitem_T *pli;
986 linenr_T lnum = 0;
987 char_u *p;
988 dict_T *dict;
989
990 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200991 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200992 {
993 if (li->li_tv.v_type != VAR_DICT)
994 {
995 emsg(_(e_dictreq));
996 return;
997 }
998 dict = li->li_tv.vval.v_dict;
999 p = dict == NULL ? NULL
1000 : dict_get_string(dict, (char_u *)"text", FALSE);
1001 ml_append_buf(buf, lnum++,
1002 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1003 }
1004
1005 // add the text properties
1006 lnum = 1;
1007 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1008 {
1009 dictitem_T *di;
1010 list_T *plist;
1011
1012 dict = li->li_tv.vval.v_dict;
1013 di = dict_find(dict, (char_u *)"props", -1);
1014 if (di != NULL)
1015 {
1016 if (di->di_tv.v_type != VAR_LIST)
1017 {
1018 emsg(_(e_listreq));
1019 return;
1020 }
1021 plist = di->di_tv.vval.v_list;
1022 if (plist != NULL)
1023 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001024 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001025 {
1026 if (pli->li_tv.v_type != VAR_DICT)
1027 {
1028 emsg(_(e_dictreq));
1029 return;
1030 }
1031 dict = pli->li_tv.vval.v_dict;
1032 if (dict != NULL)
1033 {
1034 int col = dict_get_number(dict, (char_u *)"col");
1035
1036 prop_add_common( lnum, col, dict, buf, NULL);
1037 }
1038 }
1039 }
1040 }
1041 }
1042}
1043
1044/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001045 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1046 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001047 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001048popup_top_extra(win_T *wp)
1049{
1050 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1051
1052 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1053 return 1;
1054 return extra;
1055}
1056
1057/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001058 * Get the padding plus border at the left.
1059 */
1060 int
1061popup_left_extra(win_T *wp)
1062{
1063 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1064}
1065
1066/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001067 * Return the height of popup window "wp", including border and padding.
1068 */
1069 int
1070popup_height(win_T *wp)
1071{
1072 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001073 + popup_top_extra(wp)
1074 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001075}
1076
1077/*
1078 * Return the width of popup window "wp", including border, padding and
1079 * scrollbar.
1080 */
1081 int
1082popup_width(win_T *wp)
1083{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001084 // w_leftcol is how many columns of the core are left of the screen
1085 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001086 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001087 + popup_extra_width(wp)
1088 + wp->w_popup_rightoff;
1089}
1090
1091/*
1092 * Return the extra width of popup window "wp": border, padding and scrollbar.
1093 */
1094 int
1095popup_extra_width(win_T *wp)
1096{
1097 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1098 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1099 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001100}
1101
1102/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001103 * Adjust the position and size of the popup to fit on the screen.
1104 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001105 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001106popup_adjust_position(win_T *wp)
1107{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001108 linenr_T lnum;
1109 int wrapped = 0;
1110 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001111 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001112 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001113 int center_vert = FALSE;
1114 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001115 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001116 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001117 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1118 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1119 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1120 int extra_height = top_extra + bot_extra;
1121 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001122 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001123 int org_winrow = wp->w_winrow;
1124 int org_wincol = wp->w_wincol;
1125 int org_width = wp->w_width;
1126 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001127 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001128 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001129 int minwidth, minheight;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001130 int wantline = wp->w_wantline; // adjusted for textprop
1131 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001132 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001133
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001134 wp->w_winrow = 0;
1135 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001136 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001137 wp->w_popup_leftoff = 0;
1138 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001139
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001140 // May need to update the "cursorline" highlighting, which may also change
1141 // "topline"
1142 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1143 popup_highlight_curline(wp);
1144
Bram Moolenaar12034e22019-08-25 22:25:02 +02001145 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1146 {
1147 win_T *prop_win = wp->w_popup_prop_win;
1148 textprop_T prop;
1149 linenr_T prop_lnum;
1150 pos_T pos;
1151 int screen_row;
1152 int screen_scol;
1153 int screen_ccol;
1154 int screen_ecol;
1155
1156 // Popup window is positioned relative to a text property.
1157 if (find_visible_prop(prop_win,
1158 wp->w_popup_prop_type, wp->w_popup_prop_id,
1159 &prop, &prop_lnum) == FAIL)
1160 {
1161 // Text property is no longer visible, hide the popup.
1162 // Unhiding the popup is done in check_popup_unhidden().
1163 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1164 {
1165 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001166 if (win_valid(wp->w_popup_prop_win))
1167 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1168 }
1169 return;
1170 }
1171
1172 // Compute the desired position from the position of the text
1173 // property. Use "wantline" and "wantcol" as offsets.
1174 pos.lnum = prop_lnum;
1175 pos.col = prop.tp_col;
1176 if (wp->w_popup_pos == POPPOS_TOPLEFT
1177 || wp->w_popup_pos == POPPOS_BOTLEFT)
1178 pos.col += prop.tp_len - 1;
1179 textpos2screenpos(prop_win, &pos, &screen_row,
1180 &screen_scol, &screen_ccol, &screen_ecol);
1181
1182 if (wp->w_popup_pos == POPPOS_TOPLEFT
1183 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1184 // below the text
1185 wantline = screen_row + wantline + 1;
1186 else
1187 // above the text
1188 wantline = screen_row + wantline - 1;
1189 center_vert = FALSE;
1190 if (wp->w_popup_pos == POPPOS_TOPLEFT
1191 || wp->w_popup_pos == POPPOS_BOTLEFT)
1192 // right of the text
1193 wantcol = screen_ecol + wantcol;
1194 else
1195 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001196 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001197 use_wantcol = TRUE;
1198 }
1199 else
1200 {
1201 // If no line was specified default to vertical centering.
1202 if (wantline == 0)
1203 center_vert = TRUE;
1204 else if (wantline < 0)
1205 // If "wantline" is negative it actually means zero.
1206 wantline = 0;
1207 if (wantcol < 0)
1208 // If "wantcol" is negative it actually means zero.
1209 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001210 }
1211
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001212 if (wp->w_popup_pos == POPPOS_CENTER)
1213 {
1214 // center after computing the size
1215 center_vert = TRUE;
1216 center_hor = TRUE;
1217 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001218 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001219 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001220 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1221 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001222 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001223 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001224 if (wp->w_winrow >= Rows)
1225 wp->w_winrow = Rows - 1;
1226 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001227
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001228 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001229 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001230 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1231 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001232 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001233 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001234 // Need to see at least one character after the decoration.
1235 if (wp->w_wincol > Columns - left_extra - 1)
1236 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001237 }
1238 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001239
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001240 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001241 // When left aligned use the space available, but shift to the left when we
1242 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001243 maxspace = Columns - wp->w_wincol - left_extra;
1244 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001245 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001246 {
1247 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001248 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001249 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001250 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001251 minheight = wp->w_minheight;
1252#ifdef FEAT_TERMINAL
1253 // A terminal popup initially does not have content, use a default minimal
1254 // width of 20 characters and height of 5 lines.
1255 if (wp->w_buffer->b_term != NULL)
1256 {
1257 if (minwidth == 0)
1258 minwidth = 20;
1259 if (minheight == 0)
1260 minheight = 5;
1261 }
1262#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001263
Bram Moolenaar8d241042019-06-12 23:40:01 +02001264 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001265 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001266 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001267 if (wp->w_topline < 1)
1268 wp->w_topline = 1;
1269 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001270 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1271
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001272 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001273 // Use a minimum width of one, so that something shows when there is no
1274 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001275 // When "firstline" is -1 then start with the last buffer line and go
1276 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001277 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001278 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001279 if (wp->w_firstline < 0)
1280 lnum = wp->w_buffer->b_ml.ml_line_count;
1281 else
1282 lnum = wp->w_topline;
1283 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001284 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001285 int len;
1286 int w_width = wp->w_width;
1287
1288 // Count Tabs for what they are worth and compute the length based on
1289 // the maximum width (matters when 'showbreak' is set).
1290 if (wp->w_width < maxwidth)
1291 wp->w_width = maxwidth;
1292 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001293 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001294 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001295
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001296 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001298 while (len > maxwidth)
1299 {
1300 ++wrapped;
1301 len -= maxwidth;
1302 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001303 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001304 }
1305 }
1306 else if (len > maxwidth
1307 && allow_adjust_left
1308 && (wp->w_popup_pos == POPPOS_TOPLEFT
1309 || wp->w_popup_pos == POPPOS_BOTLEFT))
1310 {
1311 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001312 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001313
Bram Moolenaar51c31312019-06-15 22:27:23 +02001314 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315 {
1316 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001317
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001318 len -= truncate_shift;
1319 shift_by -= truncate_shift;
1320 }
1321
1322 wp->w_wincol -= shift_by;
1323 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001324 wp->w_width = maxwidth;
1325 }
1326 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001327 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001328 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001329 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1330 wp->w_width = wp->w_maxwidth;
1331 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001332
1333 if (wp->w_firstline < 0)
1334 --lnum;
1335 else
1336 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001337
1338 // do not use the width of lines we're not going to show
1339 if (wp->w_maxheight > 0
1340 && (wp->w_firstline >= 0
1341 ? lnum - wp->w_topline
1342 : wp->w_buffer->b_ml.ml_line_count - lnum)
1343 + wrapped >= wp->w_maxheight)
1344 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001345 }
1346
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001347 if (wp->w_firstline < 0)
1348 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1349
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001350 wp->w_has_scrollbar = wp->w_want_scrollbar
1351 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001352#ifdef FEAT_TERMINAL
1353 if (wp->w_buffer->b_term != NULL)
1354 // Terminal window never has a scrollbar, adjusts to window height.
1355 wp->w_has_scrollbar = FALSE;
1356#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001357 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001358 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001359 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001360 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001361 // make space for the scrollbar if needed, when lines wrap and when
1362 // applying minwidth
1363 if (maxwidth + right_extra >= maxspace
1364 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1365 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001366 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001367
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001368 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1369 {
1370 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1371
1372 if (minwidth < title_len)
1373 minwidth = title_len;
1374 }
1375
1376 if (minwidth > 0 && wp->w_width < minwidth)
1377 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001378 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001379 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001380 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001381 // some columns cut off on the right
1382 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001383 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001384 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001385 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001386 {
1387 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1388 if (wp->w_wincol < 0)
1389 wp->w_wincol = 0;
1390 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001391 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1392 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1393 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001394 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001395
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001396 // Right aligned: move to the right if needed.
1397 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001398 if (leftoff >= 0)
1399 wp->w_wincol = leftoff;
1400 else if (wp->w_popup_fixed)
1401 {
1402 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001403 // columns when displaying the window, minus left border and
1404 // padding.
1405 if (-leftoff > left_extra)
1406 wp->w_leftcol = -leftoff - left_extra;
1407 wp->w_width -= wp->w_leftcol;
1408 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001409 if (wp->w_width < 0)
1410 wp->w_width = 0;
1411 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001412 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001413
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001414 if (wp->w_p_wrap || (!wp->w_popup_fixed
1415 && (wp->w_popup_pos == POPPOS_TOPLEFT
1416 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001417 {
1418 int want_col = 0;
1419
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001420 // try to show the right border and any scrollbar
1421 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001422 if (want_col > 0 && wp->w_wincol > 0
1423 && wp->w_wincol + want_col >= Columns)
1424 {
1425 wp->w_wincol = Columns - want_col;
1426 if (wp->w_wincol < 0)
1427 wp->w_wincol = 0;
1428 }
1429 }
1430
Bram Moolenaar8d241042019-06-12 23:40:01 +02001431 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1432 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001433 if (minheight > 0 && wp->w_height < minheight)
1434 wp->w_height = minheight;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001435 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1436 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001437 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001438 if (wp->w_height > Rows - wp->w_winrow)
1439 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001440 if (wp->w_height != org_height)
1441 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001442
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001443 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001444 {
1445 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1446 if (wp->w_winrow < 0)
1447 wp->w_winrow = 0;
1448 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001449 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001450 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001451 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001452 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001453 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001454 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001455 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1456 {
1457 // Bottom aligned but does not fit, and less space on the other
1458 // side or "posinvert" is off: reduce height.
1459 wp->w_winrow = 0;
1460 wp->w_height = wantline - extra_height;
1461 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001462 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001463 // Not enough space and more space on the other side: make top
1464 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001465 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001466 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001467 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1468 || wp->w_popup_pos == POPPOS_TOPLEFT)
1469 {
1470 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1471 && wantline * 2 > Rows
1472 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001473 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001474 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001475 // make bottom aligned and recompute the height
1476 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001477 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001478 if (wp->w_winrow < 0)
1479 {
1480 wp->w_height += wp->w_winrow;
1481 wp->w_winrow = 0;
1482 }
1483 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001484 else
1485 wp->w_winrow = wantline - 1;
1486 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001487 if (wp->w_winrow >= Rows)
1488 wp->w_winrow = Rows - 1;
1489 else if (wp->w_winrow < 0)
1490 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001491
Bram Moolenaar17146962019-05-30 00:12:11 +02001492 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001493 if (win_valid(wp->w_popup_prop_win))
1494 {
1495 wp->w_popup_prop_changedtick =
1496 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1497 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1498 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001499
1500 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001501 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001502 if (org_winrow != wp->w_winrow
1503 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001504 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001505 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001506 || org_width != wp->w_width
1507 || org_height != wp->w_height)
1508 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001509 redraw_win_later(wp, NOT_VALID);
1510 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1511 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001512 popup_mask_refresh = TRUE;
1513 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001514}
1515
Bram Moolenaar17627312019-06-02 19:53:44 +02001516typedef enum
1517{
1518 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001519 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001520 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001521 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001522 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001523 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001524 TYPE_PREVIEW, // preview window
1525 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001526} create_type_T;
1527
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001528/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001529 * Make "buf" empty and set the contents to "text".
1530 * Used by popup_create() and popup_settext().
1531 */
1532 static void
1533popup_set_buffer_text(buf_T *buf, typval_T text)
1534{
1535 int lnum;
1536
1537 // Clear the buffer, then replace the lines.
1538 curbuf = buf;
1539 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001540 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001541 curbuf = curwin->w_buffer;
1542
1543 // Add text to the buffer.
1544 if (text.v_type == VAR_STRING)
1545 {
1546 // just a string
1547 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1548 }
1549 else
1550 {
1551 list_T *l = text.vval.v_list;
1552
1553 if (l->lv_len > 0)
1554 {
1555 if (l->lv_first->li_tv.v_type == VAR_STRING)
1556 // list of strings
1557 add_popup_strings(buf, l);
1558 else
1559 // list of dictionaries
1560 add_popup_dicts(buf, l);
1561 }
1562 }
1563
1564 // delete the line that was in the empty buffer
1565 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001566 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001567 curbuf = curwin->w_buffer;
1568}
1569
1570/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001571 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1572 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001573 * Return FAIL if the parsing fails.
1574 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001575 static int
1576parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001577{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001578 char_u *p =
1579#ifdef FEAT_QUICKFIX
1580 !is_preview ? p_cpp :
1581#endif
1582 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001583
Bram Moolenaar258cef52019-08-21 17:29:29 +02001584 if (wp != NULL)
1585 wp->w_popup_flags &= ~POPF_INFO_MENU;
1586
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001587 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001588 {
1589 char_u *e, *dig;
1590 char_u *s = p;
1591 int x;
1592
1593 e = vim_strchr(p, ':');
1594 if (e == NULL || e[1] == NUL)
1595 return FAIL;
1596
1597 p = vim_strchr(e, ',');
1598 if (p == NULL)
1599 p = e + STRLEN(e);
1600 dig = e + 1;
1601 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001602
1603 if (STRNCMP(s, "height:", 7) == 0)
1604 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001605 if (dig != p)
1606 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001607 if (wp != NULL)
1608 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001609 if (is_preview)
1610 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001611 wp->w_maxheight = x;
1612 }
1613 }
1614 else if (STRNCMP(s, "width:", 6) == 0)
1615 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001616 if (dig != p)
1617 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001618 if (wp != NULL)
1619 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001620 if (is_preview)
1621 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001622 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001623 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001624 }
1625 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001626 else if (STRNCMP(s, "highlight:", 10) == 0)
1627 {
1628 if (wp != NULL)
1629 {
1630 int c = *p;
1631
1632 *p = NUL;
1633 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1634 s + 10, OPT_FREE|OPT_LOCAL, 0);
1635 *p = c;
1636 }
1637 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001638 else if (STRNCMP(s, "border:", 7) == 0)
1639 {
1640 char_u *arg = s + 7;
1641 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1642 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1643 int i;
1644
1645 if (!on && !off)
1646 return FAIL;
1647 if (wp != NULL)
1648 {
1649 for (i = 0; i < 4; ++i)
1650 wp->w_popup_border[i] = on ? 1 : 0;
1651 if (off)
1652 // only show the X for close when there is a border
1653 wp->w_popup_close = POPCLOSE_NONE;
1654 }
1655 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001656 else if (STRNCMP(s, "align:", 6) == 0)
1657 {
1658 char_u *arg = s + 6;
1659 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1660 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1661
1662 if (!menu && !item)
1663 return FAIL;
1664 if (wp != NULL && menu)
1665 wp->w_popup_flags |= POPF_INFO_MENU;
1666 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001667 else
1668 return FAIL;
1669 }
1670 return OK;
1671}
1672
1673/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001674 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1675 * is not NULL.
1676 * Return FAIL if the parsing fails.
1677 */
1678 int
1679parse_previewpopup(win_T *wp)
1680{
1681 return parse_popup_option(wp, TRUE);
1682}
1683
1684/*
1685 * Parse the 'completepopup' option and apply the values to window "wp" if it
1686 * is not NULL.
1687 * Return FAIL if the parsing fails.
1688 */
1689 int
1690parse_completepopup(win_T *wp)
1691{
1692 return parse_popup_option(wp, FALSE);
1693}
1694
1695/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001696 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001697 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001698 */
1699 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001700popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001701{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001702 poppos_T ppt = POPPOS_NONE;
1703
1704 if (d != NULL)
1705 ppt = get_pos_entry(d, FALSE);
1706
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001708 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001709 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001710 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1711 }
1712 else
1713 {
1714 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1715 if (wp->w_wantline == 0) // cursor in first line
1716 {
1717 wp->w_wantline = 2;
1718 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1719 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1720 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001721 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001722
Bram Moolenaar79648732019-07-18 21:43:07 +02001723 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001724 if (wp->w_wantcol > Columns - width)
1725 {
1726 wp->w_wantcol = Columns - width;
1727 if (wp->w_wantcol < 1)
1728 wp->w_wantcol = 1;
1729 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001730
Bram Moolenaar79648732019-07-18 21:43:07 +02001731 popup_adjust_position(wp);
1732}
1733
1734/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001735 * Set w_wantline and w_wantcol for the a given screen position.
1736 * Caller must take care of running into the window border.
1737 */
1738 void
1739popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1740{
1741 wp->w_wantline = row;
1742 wp->w_wantcol = col;
1743 popup_adjust_position(wp);
1744}
1745
1746/*
1747 * Add a border and lef&right padding.
1748 */
1749 static void
1750add_border_left_right_padding(win_T *wp)
1751{
1752 int i;
1753
1754 for (i = 0; i < 4; ++i)
1755 {
1756 wp->w_popup_border[i] = 1;
1757 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1758 }
1759}
1760
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001761#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001762/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001763 * Return TRUE if there is any popup window with a terminal buffer.
1764 */
1765 static int
1766popup_terminal_exists(void)
1767{
1768 win_T *wp;
1769 tabpage_T *tp;
1770
1771 FOR_ALL_POPUPWINS(wp)
1772 if (wp->w_buffer->b_term != NULL)
1773 return TRUE;
1774 FOR_ALL_TABPAGES(tp)
1775 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1776 if (wp->w_buffer->b_term != NULL)
1777 return TRUE;
1778 return FALSE;
1779}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001780#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001781
1782/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001783 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001784 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001785 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001786 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001787 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001788 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001789popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001790{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001791 win_T *wp;
1792 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001793 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001794 int new_buffer;
1795 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001796 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001797 int nr;
1798 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001799
Bram Moolenaar79648732019-07-18 21:43:07 +02001800 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001801 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001802 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001803 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001804 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001805 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001806 if (buf == NULL)
1807 {
1808 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1809 return NULL;
1810 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001811#ifdef FEAT_TERMINAL
1812 if (buf->b_term != NULL && popup_terminal_exists())
1813 {
1814 emsg(_("E861: Cannot open a second popup with a terminal"));
1815 return NULL;
1816 }
1817#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001818 }
1819 else if (!(argvars[0].v_type == VAR_STRING
1820 && argvars[0].vval.v_string != NULL)
1821 && !(argvars[0].v_type == VAR_LIST
1822 && argvars[0].vval.v_list != NULL))
1823 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001824 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001825 return NULL;
1826 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001827 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001828 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001829 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001830 return NULL;
1831 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001832 d = argvars[1].vval.v_dict;
1833 }
1834
1835 if (d != NULL)
1836 {
1837 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1838 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1839 else if (type == TYPE_NOTIFICATION)
1840 tabnr = -1; // notifications are global by default
1841 else
1842 tabnr = 0;
1843 if (tabnr > 0)
1844 {
1845 tp = find_tabpage(tabnr);
1846 if (tp == NULL)
1847 {
1848 semsg(_("E997: Tabpage not found: %d"), tabnr);
1849 return NULL;
1850 }
1851 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001852 }
1853
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001854 // Create the window and buffer.
1855 wp = win_alloc_popup_win();
1856 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001857 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001858 if (rettv != NULL)
1859 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001860 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001861 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001862
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001863 if (buf != NULL)
1864 {
1865 // use existing buffer
1866 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001867 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001868 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001869 buffer_ensure_loaded(buf);
1870 }
1871 else
1872 {
1873 // create a new buffer associated with the popup
1874 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001875 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001876 if (buf == NULL)
1877 return NULL;
1878 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001879
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001880 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001881
Bram Moolenaar46451042019-08-24 15:50:46 +02001882 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001883 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001884 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001885 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001886 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001887 buf->b_p_ul = -1; // no undo
1888 buf->b_p_swf = FALSE; // no swap file
1889 buf->b_p_bl = FALSE; // unlisted buffer
1890 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001891
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001892 // Avoid that 'buftype' is reset when this buffer is entered.
1893 buf->b_p_initialized = TRUE;
1894 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001895 wp->w_p_wrap = TRUE; // 'wrap' is default on
1896 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001897
Bram Moolenaara3fce622019-06-20 02:31:49 +02001898 if (tp != NULL)
1899 {
1900 // popup on specified tab page
1901 wp->w_next = tp->tp_first_popupwin;
1902 tp->tp_first_popupwin = wp;
1903 }
1904 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001905 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001906 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001907 wp->w_next = curtab->tp_first_popupwin;
1908 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001909 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001910 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001911 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001912 win_T *prev = first_popupwin;
1913
1914 // Global popup: add at the end, so that it gets displayed on top of
1915 // older ones with the same zindex. Matters for notifications.
1916 if (first_popupwin == NULL)
1917 first_popupwin = wp;
1918 else
1919 {
1920 while (prev->w_next != NULL)
1921 prev = prev->w_next;
1922 prev->w_next = wp;
1923 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001924 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001925
Bram Moolenaar79648732019-07-18 21:43:07 +02001926 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001927 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001928
Bram Moolenaar79648732019-07-18 21:43:07 +02001929 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001930 {
1931 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001932 }
1933 if (type == TYPE_ATCURSOR)
1934 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001935 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001936 set_moved_values(wp);
1937 set_moved_columns(wp, FIND_STRING);
1938 }
1939
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001940 if (type == TYPE_BEVAL)
1941 {
1942 wp->w_popup_pos = POPPOS_BOTLEFT;
1943
1944 // by default use the mouse position
1945 wp->w_wantline = mouse_row;
1946 if (wp->w_wantline <= 0) // mouse on first line
1947 {
1948 wp->w_wantline = 2;
1949 wp->w_popup_pos = POPPOS_TOPLEFT;
1950 }
1951 wp->w_wantcol = mouse_col + 1;
1952 set_mousemoved_values(wp);
1953 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1954 }
1955
Bram Moolenaar33796b32019-06-08 16:01:13 +02001956 // set default values
1957 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001958 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001959
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001960 if (type == TYPE_NOTIFICATION)
1961 {
1962 win_T *twp, *nextwin;
1963 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001964
1965 // Try to not overlap with another global popup. Guess we need 3
1966 // more screen lines than buffer lines.
1967 wp->w_wantline = 1;
1968 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1969 {
1970 nextwin = twp->w_next;
1971 if (twp != wp
1972 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1973 && twp->w_winrow <= wp->w_wantline - 1 + height
1974 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1975 {
1976 // move to below this popup and restart the loop to check for
1977 // overlap with other popups
1978 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1979 nextwin = first_popupwin;
1980 }
1981 }
1982 if (wp->w_wantline + height > Rows)
1983 {
1984 // can't avoid overlap, put on top in the hope that message goes
1985 // away soon.
1986 wp->w_wantline = 1;
1987 }
1988
1989 wp->w_wantcol = 10;
1990 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001991 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001992 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001993 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001994 for (i = 0; i < 4; ++i)
1995 wp->w_popup_border[i] = 1;
1996 wp->w_popup_padding[1] = 1;
1997 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001998
1999 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002000 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002001 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2002 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002003 }
2004
Bram Moolenaara730e552019-06-16 19:05:31 +02002005 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002006 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002007 wp->w_popup_pos = POPPOS_CENTER;
2008 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002009 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002010 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002011 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002012 }
2013
Bram Moolenaara730e552019-06-16 19:05:31 +02002014 if (type == TYPE_MENU)
2015 {
2016 typval_T tv;
2017 callback_T callback;
2018
2019 tv.v_type = VAR_STRING;
2020 tv.vval.v_string = (char_u *)"popup_filter_menu";
2021 callback = get_callback(&tv);
2022 if (callback.cb_name != NULL)
2023 set_callback(&wp->w_filter_cb, &callback);
2024
2025 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002026 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002027 }
2028
Bram Moolenaar79648732019-07-18 21:43:07 +02002029 if (type == TYPE_PREVIEW)
2030 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002031 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002032 wp->w_popup_close = POPCLOSE_BUTTON;
2033 for (i = 0; i < 4; ++i)
2034 wp->w_popup_border[i] = 1;
2035 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002036 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002037 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002038# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002039 if (type == TYPE_INFO)
2040 {
2041 wp->w_popup_pos = POPPOS_TOPLEFT;
2042 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2043 wp->w_popup_close = POPCLOSE_BUTTON;
2044 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002045 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002046 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002047# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002048
Bram Moolenaarae943152019-06-16 22:54:14 +02002049 for (i = 0; i < 4; ++i)
2050 VIM_CLEAR(wp->w_border_highlight[i]);
2051 for (i = 0; i < 8; ++i)
2052 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002053 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002054 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002055 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002056
Bram Moolenaar79648732019-07-18 21:43:07 +02002057 if (d != NULL)
2058 // Deal with options.
2059 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002060
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002061#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002062 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2063 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002064#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002065
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002066 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002067
2068 wp->w_vsep_width = 0;
2069
2070 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002071 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002072
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002073#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002074 // When running a terminal in the popup it becomes the current window.
2075 if (buf->b_term != NULL)
2076 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002077#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002078
Bram Moolenaara730e552019-06-16 19:05:31 +02002079 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002080}
2081
2082/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002083 * popup_clear()
2084 */
2085 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002086f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002087{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002088 int force = FALSE;
2089
2090 if (argvars[0].v_type != VAR_UNKNOWN)
2091 force = (int)tv_get_number(&argvars[0]);
2092 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002093}
2094
2095/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002096 * popup_create({text}, {options})
2097 */
2098 void
2099f_popup_create(typval_T *argvars, typval_T *rettv)
2100{
Bram Moolenaar17627312019-06-02 19:53:44 +02002101 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002102}
2103
2104/*
2105 * popup_atcursor({text}, {options})
2106 */
2107 void
2108f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2109{
Bram Moolenaar17627312019-06-02 19:53:44 +02002110 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002111}
2112
2113/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002114 * popup_beval({text}, {options})
2115 */
2116 void
2117f_popup_beval(typval_T *argvars, typval_T *rettv)
2118{
2119 popup_create(argvars, rettv, TYPE_BEVAL);
2120}
2121
2122/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002123 * Invoke the close callback for window "wp" with value "result".
2124 * Careful: The callback may make "wp" invalid!
2125 */
2126 static void
2127invoke_popup_callback(win_T *wp, typval_T *result)
2128{
2129 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002130 typval_T argv[3];
2131
2132 argv[0].v_type = VAR_NUMBER;
2133 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2134
2135 if (result != NULL && result->v_type != VAR_UNKNOWN)
2136 copy_tv(result, &argv[1]);
2137 else
2138 {
2139 argv[1].v_type = VAR_NUMBER;
2140 argv[1].vval.v_number = 0;
2141 }
2142
2143 argv[2].v_type = VAR_UNKNOWN;
2144
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002145 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002146 if (result != NULL)
2147 clear_tv(&argv[1]);
2148 clear_tv(&rettv);
2149}
2150
2151/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002152 * Make "prevwin" the current window, unless it's equal to "wp".
2153 * Otherwise make "firstwin" the current window.
2154 */
2155 static void
2156back_to_prevwin(win_T *wp)
2157{
2158 if (win_valid(prevwin) && wp != prevwin)
2159 win_enter(prevwin, FALSE);
2160 else
2161 win_enter(firstwin, FALSE);
2162}
2163
2164/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002165 * Close popup "wp" and invoke any close callback for it.
2166 */
2167 static void
2168popup_close_and_callback(win_T *wp, typval_T *arg)
2169{
2170 int id = wp->w_id;
2171
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002172#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002173 if (wp == curwin && curbuf->b_term != NULL)
2174 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002175 win_T *owp;
2176
2177 // Closing popup window with a terminal: put focus back on the first
2178 // that works:
2179 // - another popup window with a terminal
2180 // - the previous window
2181 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002182 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002183 if (owp != curwin && owp->w_buffer->b_term != NULL)
2184 break;
2185 if (owp != NULL)
2186 win_enter(owp, FALSE);
2187 else
2188 {
2189 for (owp = curtab->tp_first_popupwin; owp != NULL;
2190 owp = owp->w_next)
2191 if (owp != curwin && owp->w_buffer->b_term != NULL)
2192 break;
2193 if (owp != NULL)
2194 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002195 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002196 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002197 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002198 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002199#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002200
Bram Moolenaar49540192019-12-11 19:34:54 +01002201 // Just in case a check higher up is missing.
2202 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2203 return;
2204
Bram Moolenaarcee52202020-03-11 14:19:58 +01002205 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002206 if (wp->w_close_cb.cb_name != NULL)
2207 // Careful: This may make "wp" invalid.
2208 invoke_popup_callback(wp, arg);
2209
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002210 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002211 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002212}
2213
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002214 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002215popup_close_with_retval(win_T *wp, int retval)
2216{
2217 typval_T res;
2218
2219 res.v_type = VAR_NUMBER;
2220 res.vval.v_number = retval;
2221 popup_close_and_callback(wp, &res);
2222}
2223
Bram Moolenaar3397f742019-06-02 18:40:06 +02002224/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002225 * Close popup "wp" because of a mouse click.
2226 */
2227 void
2228popup_close_for_mouse_click(win_T *wp)
2229{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002230 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002231}
2232
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002233 static void
2234check_mouse_moved(win_T *wp, win_T *mouse_wp)
2235{
2236 // Close the popup when all if these are true:
2237 // - the mouse is not on this popup
2238 // - "mousemoved" was used
2239 // - the mouse is no longer on the same screen row or the mouse column is
2240 // outside of the relevant text
2241 if (wp != mouse_wp
2242 && wp->w_popup_mouse_row != 0
2243 && (wp->w_popup_mouse_row != mouse_row
2244 || mouse_col < wp->w_popup_mouse_mincol
2245 || mouse_col > wp->w_popup_mouse_maxcol))
2246 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002247 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002248 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002249 }
2250}
2251
2252/*
2253 * Called when the mouse moved: may close a popup with "mousemoved".
2254 */
2255 void
2256popup_handle_mouse_moved(void)
2257{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002258 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002259 win_T *mouse_wp;
2260 int row = mouse_row;
2261 int col = mouse_col;
2262
2263 // find the window where the mouse is in
2264 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2265
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002266 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2267 {
2268 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002269 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002270 }
2271 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2272 {
2273 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002274 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002275 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002276}
2277
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002278/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002279 * In a filter: check if the typed key is a mouse event that is used for
2280 * dragging the popup.
2281 */
2282 static void
2283filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2284{
2285 int row = mouse_row;
2286 int col = mouse_col;
2287
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002288 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002289 && is_mouse_key(c)
2290 && (wp == popup_dragwin
2291 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2292 // do not consume the key, allow for dragging the popup
2293 rettv->vval.v_number = 0;
2294}
2295
Bram Moolenaara730e552019-06-16 19:05:31 +02002296/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002297 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002298 */
2299 void
2300f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2301{
2302 int id = tv_get_number(&argvars[0]);
2303 win_T *wp = win_id2wp(id);
2304 char_u *key = tv_get_string(&argvars[1]);
2305 typval_T res;
2306 int c;
2307 linenr_T old_lnum;
2308
2309 // If the popup has been closed do not consume the key.
2310 if (wp == NULL)
2311 return;
2312
2313 c = *key;
2314 if (c == K_SPECIAL && key[1] != NUL)
2315 c = TO_SPECIAL(key[1], key[2]);
2316
2317 // consume all keys until done
2318 rettv->vval.v_number = 1;
2319 res.v_type = VAR_NUMBER;
2320
2321 old_lnum = wp->w_cursor.lnum;
2322 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2323 --wp->w_cursor.lnum;
2324 if ((c == 'j' || c == 'J' || c == K_DOWN)
2325 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2326 ++wp->w_cursor.lnum;
2327 if (old_lnum != wp->w_cursor.lnum)
2328 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002329 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002330 return;
2331 }
2332
2333 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2334 {
2335 // Cancelled, invoke callback with -1
2336 res.vval.v_number = -1;
2337 popup_close_and_callback(wp, &res);
2338 return;
2339 }
2340 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2341 {
2342 // Invoke callback with current index.
2343 res.vval.v_number = wp->w_cursor.lnum;
2344 popup_close_and_callback(wp, &res);
2345 return;
2346 }
2347
2348 filter_handle_drag(wp, c, rettv);
2349}
2350
2351/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002352 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002353 */
2354 void
2355f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2356{
2357 int id = tv_get_number(&argvars[0]);
2358 win_T *wp = win_id2wp(id);
2359 char_u *key = tv_get_string(&argvars[1]);
2360 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002361 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002362
2363 // If the popup has been closed don't consume the key.
2364 if (wp == NULL)
2365 return;
2366
Bram Moolenaara730e552019-06-16 19:05:31 +02002367 c = *key;
2368 if (c == K_SPECIAL && key[1] != NUL)
2369 c = TO_SPECIAL(key[1], key[2]);
2370
Bram Moolenaara42d9452019-06-15 21:46:30 +02002371 // consume all keys until done
2372 rettv->vval.v_number = 1;
2373
Bram Moolenaara730e552019-06-16 19:05:31 +02002374 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002375 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002376 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002377 res.vval.v_number = 0;
2378 else
2379 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002380 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002381 return;
2382 }
2383
2384 // Invoke callback
2385 res.v_type = VAR_NUMBER;
2386 popup_close_and_callback(wp, &res);
2387}
2388
2389/*
2390 * popup_dialog({text}, {options})
2391 */
2392 void
2393f_popup_dialog(typval_T *argvars, typval_T *rettv)
2394{
2395 popup_create(argvars, rettv, TYPE_DIALOG);
2396}
2397
2398/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002399 * popup_menu({text}, {options})
2400 */
2401 void
2402f_popup_menu(typval_T *argvars, typval_T *rettv)
2403{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002404 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002405}
2406
2407/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002408 * popup_notification({text}, {options})
2409 */
2410 void
2411f_popup_notification(typval_T *argvars, typval_T *rettv)
2412{
2413 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2414}
2415
2416/*
2417 * Find the popup window with window-ID "id".
2418 * If the popup window does not exist NULL is returned.
2419 * If the window is not a popup window, and error message is given.
2420 */
2421 static win_T *
2422find_popup_win(int id)
2423{
2424 win_T *wp = win_id2wp(id);
2425
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002426 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002427 {
2428 semsg(_("E993: window %d is not a popup window"), id);
2429 return NULL;
2430 }
2431 return wp;
2432}
2433
2434/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002435 * popup_close({id})
2436 */
2437 void
2438f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2439{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002440 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002441 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002442
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002443 if (
2444# ifdef FEAT_TERMINAL
2445 // if the popup contains a terminal it will become hidden
2446 curbuf->b_term == NULL &&
2447# endif
2448 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002449 return;
2450
2451 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002452 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002453 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002454}
2455
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002456 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002457popup_hide(win_T *wp)
2458{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002459#ifdef FEAT_TERMINAL
2460 if (error_if_term_popup_window())
2461 return;
2462#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002463 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2464 {
2465 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002466 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002467 redraw_all_later(NOT_VALID);
2468 popup_mask_refresh = TRUE;
2469 }
2470}
2471
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002472/*
2473 * popup_hide({id})
2474 */
2475 void
2476f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2477{
2478 int id = (int)tv_get_number(argvars);
2479 win_T *wp = find_popup_win(id);
2480
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002481 if (wp != NULL)
2482 popup_hide(wp);
2483}
2484
2485 void
2486popup_show(win_T *wp)
2487{
2488 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002489 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002490 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002491 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002492 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002493 }
2494}
2495
2496/*
2497 * popup_show({id})
2498 */
2499 void
2500f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2501{
2502 int id = (int)tv_get_number(argvars);
2503 win_T *wp = find_popup_win(id);
2504
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002505 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002506 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002507 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002508#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002509 if (wp->w_popup_flags & POPF_INFO)
2510 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002511#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002512 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002513}
2514
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002515/*
2516 * popup_settext({id}, {text})
2517 */
2518 void
2519f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2520{
2521 int id = (int)tv_get_number(&argvars[0]);
2522 win_T *wp = find_popup_win(id);
2523
2524 if (wp != NULL)
2525 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002526 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2527 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2528 else
2529 {
2530 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002531 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002532 popup_adjust_position(wp);
2533 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002534 }
2535}
2536
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002537 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002538popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002539{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002540 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002541 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002542 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002543 clear_cmdline = TRUE;
2544 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002545
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002546 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002547 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002548}
2549
Bram Moolenaar82106932020-03-13 14:34:38 +01002550 static void
2551error_for_popup_window(void)
2552{
2553 emsg(_("E994: Not allowed in a popup window"));
2554}
2555
2556 int
2557error_if_popup_window(int also_with_term UNUSED)
2558{
2559 // win_execute() may set "curwin" to a popup window temporarily, but many
2560 // commands are disallowed then. When a terminal runs in the popup most
2561 // things are allowed. When a terminal is finished it can be closed.
2562 if (WIN_IS_POPUP(curwin)
2563# ifdef FEAT_TERMINAL
2564 && (also_with_term || curbuf->b_term == NULL)
2565 && !term_is_finished(curbuf)
2566# endif
2567 )
2568 {
2569 error_for_popup_window();
2570 return TRUE;
2571 }
2572 return FALSE;
2573}
2574
Bram Moolenaarec583842019-05-26 14:11:23 +02002575/*
2576 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002577 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002578 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002579 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002580 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002581popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002582{
2583 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002584 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002585 win_T *prev = NULL;
2586
Bram Moolenaarec583842019-05-26 14:11:23 +02002587 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002588 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002589 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002590 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002591 if (wp == curwin)
2592 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002593 if (!force)
2594 {
2595 error_for_popup_window();
2596 return FAIL;
2597 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002598 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002599 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002600 if (prev == NULL)
2601 first_popupwin = wp->w_next;
2602 else
2603 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002604 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002605 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002606 }
2607
Bram Moolenaarec583842019-05-26 14:11:23 +02002608 // go through tab-local popups
2609 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002610 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002611 return OK;
2612 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002613}
2614
2615/*
2616 * Close a popup window with Window-id "id" in tabpage "tp".
2617 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002618 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002619popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002620{
2621 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002622 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002623 win_T *prev = NULL;
2624
Bram Moolenaarec583842019-05-26 14:11:23 +02002625 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2626 if (wp->w_id == id)
2627 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002628 if (wp == curwin)
2629 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002630 if (!force)
2631 {
2632 error_for_popup_window();
2633 return FAIL;
2634 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002635 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002636 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002637 if (prev == NULL)
2638 *root = wp->w_next;
2639 else
2640 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002641 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002642 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002643 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002644 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002645}
2646
2647 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002648close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002649{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002650 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002651 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002652 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002653 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002654 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002655 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002656 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002657 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002658}
2659
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002660/*
2661 * popup_move({id}, {options})
2662 */
2663 void
2664f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2665{
Bram Moolenaarae943152019-06-16 22:54:14 +02002666 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002667 int id = (int)tv_get_number(argvars);
2668 win_T *wp = find_popup_win(id);
2669
2670 if (wp == NULL)
2671 return; // invalid {id}
2672
2673 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2674 {
2675 emsg(_(e_dictreq));
2676 return;
2677 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002678 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002679
Bram Moolenaarae943152019-06-16 22:54:14 +02002680 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002681
2682 if (wp->w_winrow + wp->w_height >= cmdline_row)
2683 clear_cmdline = TRUE;
2684 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002685}
2686
Bram Moolenaarbc133542019-05-29 20:26:46 +02002687/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002688 * popup_setoptions({id}, {options})
2689 */
2690 void
2691f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2692{
2693 dict_T *dict;
2694 int id = (int)tv_get_number(argvars);
2695 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002696 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002697
2698 if (wp == NULL)
2699 return; // invalid {id}
2700
2701 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2702 {
2703 emsg(_(e_dictreq));
2704 return;
2705 }
2706 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002707 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002708
2709 apply_move_options(wp, dict);
2710 apply_general_options(wp, dict);
2711
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002712 if (old_firstline != wp->w_firstline)
2713 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002714 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002715 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002716 popup_adjust_position(wp);
2717}
2718
2719/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002720 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002721 */
2722 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002723f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002724{
2725 dict_T *dict;
2726 int id = (int)tv_get_number(argvars);
2727 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002728 int top_extra;
2729 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002730
2731 if (rettv_dict_alloc(rettv) == OK)
2732 {
2733 if (wp == NULL)
2734 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002735 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002736 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2737
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002738 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002739 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002740 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002741
Bram Moolenaarbc133542019-05-29 20:26:46 +02002742 dict_add_number(dict, "line", wp->w_winrow + 1);
2743 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002744 dict_add_number(dict, "width", wp->w_width + left_extra
2745 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2746 dict_add_number(dict, "height", wp->w_height + top_extra
2747 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002748
2749 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2750 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2751 dict_add_number(dict, "core_width", wp->w_width);
2752 dict_add_number(dict, "core_height", wp->w_height);
2753
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002754 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002755 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002756 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002757 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002758 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002759
2760 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002761 }
2762}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002763
2764/*
2765 * popup_list()
2766 */
2767 void
2768f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2769{
2770 win_T *wp;
2771 tabpage_T *tp;
2772
2773 if (rettv_list_alloc(rettv) != OK)
2774 return;
2775 FOR_ALL_POPUPWINS(wp)
2776 list_append_number(rettv->vval.v_list, wp->w_id);
2777 FOR_ALL_TABPAGES(tp)
2778 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2779 list_append_number(rettv->vval.v_list, wp->w_id);
2780}
2781
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002782/*
2783 * popup_locate({row}, {col})
2784 */
2785 void
2786f_popup_locate(typval_T *argvars, typval_T *rettv)
2787{
2788 int row = tv_get_number(&argvars[0]) - 1;
2789 int col = tv_get_number(&argvars[1]) - 1;
2790 win_T *wp;
2791
2792 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002793 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002794 rettv->vval.v_number = wp->w_id;
2795}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002796
2797/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002798 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2799 */
2800 static void
2801get_padding_border(dict_T *dict, int *array, char *name)
2802{
2803 list_T *list;
2804 int i;
2805
2806 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2807 return;
2808
2809 list = list_alloc();
2810 if (list != NULL)
2811 {
2812 dict_add_list(dict, name, list);
2813 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2814 for (i = 0; i < 4; ++i)
2815 list_append_number(list, array[i]);
2816 }
2817}
2818
2819/*
2820 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2821 */
2822 static void
2823get_borderhighlight(dict_T *dict, win_T *wp)
2824{
2825 list_T *list;
2826 int i;
2827
2828 for (i = 0; i < 4; ++i)
2829 if (wp->w_border_highlight[i] != NULL)
2830 break;
2831 if (i == 4)
2832 return;
2833
2834 list = list_alloc();
2835 if (list != NULL)
2836 {
2837 dict_add_list(dict, "borderhighlight", list);
2838 for (i = 0; i < 4; ++i)
2839 list_append_string(list, wp->w_border_highlight[i], -1);
2840 }
2841}
2842
2843/*
2844 * For popup_getoptions(): add a "borderchars" entry to "dict".
2845 */
2846 static void
2847get_borderchars(dict_T *dict, win_T *wp)
2848{
2849 list_T *list;
2850 int i;
2851 char_u buf[NUMBUFLEN];
2852 int len;
2853
2854 for (i = 0; i < 8; ++i)
2855 if (wp->w_border_char[i] != 0)
2856 break;
2857 if (i == 8)
2858 return;
2859
2860 list = list_alloc();
2861 if (list != NULL)
2862 {
2863 dict_add_list(dict, "borderchars", list);
2864 for (i = 0; i < 8; ++i)
2865 {
2866 len = mb_char2bytes(wp->w_border_char[i], buf);
2867 list_append_string(list, buf, len);
2868 }
2869 }
2870}
2871
2872/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002873 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002874 */
2875 static void
2876get_moved_list(dict_T *dict, win_T *wp)
2877{
2878 list_T *list;
2879
2880 list = list_alloc();
2881 if (list != NULL)
2882 {
2883 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002884 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002885 list_append_number(list, wp->w_popup_mincol);
2886 list_append_number(list, wp->w_popup_maxcol);
2887 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002888 list = list_alloc();
2889 if (list != NULL)
2890 {
2891 dict_add_list(dict, "mousemoved", list);
2892 list_append_number(list, wp->w_popup_mouse_row);
2893 list_append_number(list, wp->w_popup_mouse_mincol);
2894 list_append_number(list, wp->w_popup_mouse_maxcol);
2895 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002896}
2897
2898/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002899 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002900 */
2901 void
2902f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2903{
2904 dict_T *dict;
2905 int id = (int)tv_get_number(argvars);
2906 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002907 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002908 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002909
2910 if (rettv_dict_alloc(rettv) == OK)
2911 {
2912 if (wp == NULL)
2913 return;
2914
2915 dict = rettv->vval.v_dict;
2916 dict_add_number(dict, "line", wp->w_wantline);
2917 dict_add_number(dict, "col", wp->w_wantcol);
2918 dict_add_number(dict, "minwidth", wp->w_minwidth);
2919 dict_add_number(dict, "minheight", wp->w_minheight);
2920 dict_add_number(dict, "maxheight", wp->w_maxheight);
2921 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002922 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002923 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002924 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002925 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002926 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2927 {
2928 proptype_T *pt = text_prop_type_by_id(
2929 wp->w_popup_prop_win->w_buffer,
2930 wp->w_popup_prop_type);
2931
2932 if (pt != NULL)
2933 dict_add_string(dict, "textprop", pt->pt_name);
2934 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2935 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2936 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002937 dict_add_string(dict, "title", wp->w_popup_title);
2938 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002939 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002940 dict_add_number(dict, "mapping",
2941 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002942 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002943 dict_add_number(dict, "posinvert",
2944 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002945 dict_add_number(dict, "cursorline",
2946 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002947 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002948 if (wp->w_scrollbar_highlight != NULL)
2949 dict_add_string(dict, "scrollbarhighlight",
2950 wp->w_scrollbar_highlight);
2951 if (wp->w_thumb_highlight != NULL)
2952 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002953
Bram Moolenaara3fce622019-06-20 02:31:49 +02002954 // find the tabpage that holds this popup
2955 i = 1;
2956 FOR_ALL_TABPAGES(tp)
2957 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002958 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002959
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002960 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
2961 if (twp->w_id == id)
2962 break;
2963 if (twp != NULL)
2964 break;
2965 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002966 }
2967 if (tp == NULL)
2968 i = -1; // must be global
2969 else if (tp == curtab)
2970 i = 0;
2971 dict_add_number(dict, "tabpage", i);
2972
Bram Moolenaarae943152019-06-16 22:54:14 +02002973 get_padding_border(dict, wp->w_popup_padding, "padding");
2974 get_padding_border(dict, wp->w_popup_border, "border");
2975 get_borderhighlight(dict, wp);
2976 get_borderchars(dict, wp);
2977 get_moved_list(dict, wp);
2978
2979 if (wp->w_filter_cb.cb_name != NULL)
2980 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2981 if (wp->w_close_cb.cb_name != NULL)
2982 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002983
2984 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2985 ++i)
2986 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2987 {
2988 dict_add_string(dict, "pos",
2989 (char_u *)poppos_entries[i].pp_name);
2990 break;
2991 }
2992
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002993 dict_add_string(dict, "close", (char_u *)(
2994 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2995 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2996
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002997# if defined(FEAT_TIMERS)
2998 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2999 ? (long)wp->w_popup_timer->tr_interval : 0L);
3000# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003001 }
3002}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003003
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003004# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003005/*
3006 * Return TRUE if the current window is running a terminal in a popup window.
3007 * Return FALSE when the job has ended.
3008 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003009 int
3010error_if_term_popup_window()
3011{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003012 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3013 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003014 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003015 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003016 return TRUE;
3017 }
3018 return FALSE;
3019}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003020# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003021
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003022/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003023 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003024 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003025 * Each calling function should use a different flag, see the list at
3026 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003027 */
3028 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003029popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003030{
3031 win_T *wp;
3032
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003033 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003034 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003035 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003036 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003037}
3038
3039/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003040 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003041 * Must have called popup_reset_handled() first.
3042 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3043 * popup with the highest zindex.
3044 */
3045 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003046find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003047{
3048 win_T *wp;
3049 win_T *found_wp;
3050 int found_zindex;
3051
3052 found_zindex = lowest ? INT_MAX : 0;
3053 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003054 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003055 if ((wp->w_popup_handled & handled_flag) == 0
3056 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003057 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003058 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003059 {
3060 found_zindex = wp->w_zindex;
3061 found_wp = wp;
3062 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003063 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003064 if ((wp->w_popup_handled & handled_flag) == 0
3065 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003066 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003067 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003068 {
3069 found_zindex = wp->w_zindex;
3070 found_wp = wp;
3071 }
3072
3073 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003074 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003075 return found_wp;
3076}
3077
3078/*
3079 * Invoke the filter callback for window "wp" with typed character "c".
3080 * Uses the global "mod_mask" for modifiers.
3081 * Returns the return value of the filter.
3082 * Careful: The filter may make "wp" invalid!
3083 */
3084 static int
3085invoke_popup_filter(win_T *wp, int c)
3086{
3087 int res;
3088 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003089 typval_T argv[3];
3090 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003091 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003092
Bram Moolenaara42d9452019-06-15 21:46:30 +02003093 // Emergency exit: CTRL-C closes the popup.
3094 if (c == Ctrl_C)
3095 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003096 int save_got_int = got_int;
3097
3098 // Reset got_int to avoid the callback isn't called.
3099 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003100 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003101 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003102 return 1;
3103 }
3104
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003105 argv[0].v_type = VAR_NUMBER;
3106 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3107
3108 // Convert the number to a string, so that the function can use:
3109 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003110 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003111 argv[1].v_type = VAR_STRING;
3112 argv[1].vval.v_string = vim_strsave(buf);
3113
3114 argv[2].v_type = VAR_UNKNOWN;
3115
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003116 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003117 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003118 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003119 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003120 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003121
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003122 vim_free(argv[1].vval.v_string);
3123 clear_tv(&rettv);
3124 return res;
3125}
3126
3127/*
3128 * Called when "c" was typed: invoke popup filter callbacks.
3129 * Returns TRUE when the character was consumed,
3130 */
3131 int
3132popup_do_filter(int c)
3133{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003134 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003135 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003136 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003137 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003138 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003139 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003140
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003141#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003142 // Popup window with terminal always gets focus.
3143 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3144 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003145#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003146
Bram Moolenaar934470e2019-09-01 23:27:05 +02003147 if (recursive)
3148 return FALSE;
3149 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003150
Bram Moolenaarf6396232019-08-24 19:36:00 +02003151 if (c == K_LEFTMOUSE)
3152 {
3153 int row = mouse_row;
3154 int col = mouse_col;
3155
3156 wp = mouse_find_win(&row, &col, FIND_POPUP);
3157 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003158 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003159 }
3160
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003161 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003162 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003163 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003164 if (wp->w_filter_cb.cb_name != NULL
3165 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003166 res = invoke_popup_filter(wp, c);
3167
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003168 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003169 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02003170 recursive = FALSE;
3171 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003172 return res;
3173}
3174
Bram Moolenaar3397f742019-06-02 18:40:06 +02003175/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003176 * Return TRUE if there is a popup visible with a filter callback and the
3177 * "mapping" property off.
3178 */
3179 int
3180popup_no_mapping(void)
3181{
3182 int round;
3183 win_T *wp;
3184
3185 for (round = 1; round <= 2; ++round)
3186 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3187 wp != NULL; wp = wp->w_next)
3188 if (wp->w_filter_cb.cb_name != NULL
3189 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3190 return TRUE;
3191 return FALSE;
3192}
3193
3194/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003195 * Called when the cursor moved: check if any popup needs to be closed if the
3196 * cursor moved far enough.
3197 */
3198 void
3199popup_check_cursor_pos()
3200{
3201 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003202
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003203 popup_reset_handled(POPUP_HANDLED_3);
3204 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003205 if (wp->w_popup_curwin != NULL
3206 && (curwin != wp->w_popup_curwin
3207 || curwin->w_cursor.lnum != wp->w_popup_lnum
3208 || curwin->w_cursor.col < wp->w_popup_mincol
3209 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003210 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003211}
3212
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003213/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003214 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003215 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003216 static void
3217popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003218{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003219 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003220 char_u *cells;
3221 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003222
3223 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003224 return;
3225 if (wp->w_popup_mask_cells != NULL
3226 && wp->w_popup_mask_height == height
3227 && wp->w_popup_mask_width == width)
3228 return; // cache is still valid
3229
3230 vim_free(wp->w_popup_mask_cells);
3231 wp->w_popup_mask_cells = alloc_clear(width * height);
3232 if (wp->w_popup_mask_cells == NULL)
3233 return;
3234 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003235
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003236 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003237 {
3238 int cols, cole;
3239 int lines, linee;
3240
3241 li = lio->li_tv.vval.v_list->lv_first;
3242 cols = tv_get_number(&li->li_tv);
3243 if (cols < 0)
3244 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003245 li = li->li_next;
3246 cole = tv_get_number(&li->li_tv);
3247 if (cole < 0)
3248 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003249 li = li->li_next;
3250 lines = tv_get_number(&li->li_tv);
3251 if (lines < 0)
3252 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003253 li = li->li_next;
3254 linee = tv_get_number(&li->li_tv);
3255 if (linee < 0)
3256 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003257
3258 for (row = lines - 1; row < linee && row < height; ++row)
3259 for (col = cols - 1; col < cole && col < width; ++col)
3260 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003261 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003262}
3263
3264/*
3265 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3266 * "col" and "line" are screen coordinates.
3267 */
3268 static int
3269popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3270{
3271 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3272 int line = screenline - wp->w_winrow;
3273
3274 return col >= 0 && col < width
3275 && line >= 0 && line < height
3276 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003277}
3278
3279/*
3280 * Set flags in popup_transparent[] for window "wp" to "val".
3281 */
3282 static void
3283update_popup_transparent(win_T *wp, int val)
3284{
3285 if (wp->w_popup_mask != NULL)
3286 {
3287 int width = popup_width(wp);
3288 int height = popup_height(wp);
3289 listitem_T *lio, *li;
3290 int cols, cole;
3291 int lines, linee;
3292 int col, line;
3293
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003294 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003295 {
3296 li = lio->li_tv.vval.v_list->lv_first;
3297 cols = tv_get_number(&li->li_tv);
3298 if (cols < 0)
3299 cols = width + cols + 1;
3300 li = li->li_next;
3301 cole = tv_get_number(&li->li_tv);
3302 if (cole < 0)
3303 cole = width + cole + 1;
3304 li = li->li_next;
3305 lines = tv_get_number(&li->li_tv);
3306 if (lines < 0)
3307 lines = height + lines + 1;
3308 li = li->li_next;
3309 linee = tv_get_number(&li->li_tv);
3310 if (linee < 0)
3311 linee = height + linee + 1;
3312
3313 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003314 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003315 if (cols < 0)
3316 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003317 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003318 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003319 if (lines < 0)
3320 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003321 for (line = lines; line < linee
3322 && line + wp->w_winrow < screen_Rows; ++line)
3323 for (col = cols; col < cole
3324 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003325 popup_transparent[(line + wp->w_winrow) * screen_Columns
3326 + col + wp->w_wincol] = val;
3327 }
3328 }
3329}
3330
3331/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003332 * Only called when popup window "wp" is hidden: If the window is positioned
3333 * next to a text property, and it is now visible, then unhide the popup.
3334 * We don't check if visible popups become hidden, that is done in
3335 * popup_adjust_position().
3336 * Return TRUE if the popup became unhidden.
3337 */
3338 static int
3339check_popup_unhidden(win_T *wp)
3340{
3341 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3342 {
3343 textprop_T prop;
3344 linenr_T lnum;
3345
3346 if (find_visible_prop(wp->w_popup_prop_win,
3347 wp->w_popup_prop_type, wp->w_popup_prop_id,
3348 &prop, &lnum) == OK)
3349 {
3350 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003351 wp->w_popup_prop_topline = 0; // force repositioning
3352 return TRUE;
3353 }
3354 }
3355 return FALSE;
3356}
3357
3358/*
3359 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3360 * That is when the buffer in the popup was changed, or the popup is following
3361 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003362 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003363 */
3364 static int
3365popup_need_position_adjust(win_T *wp)
3366{
3367 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3368 return TRUE;
3369 if (win_valid(wp->w_popup_prop_win))
3370 return wp->w_popup_prop_changedtick
3371 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003372 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3373 || ((wp->w_popup_flags & POPF_CURSORLINE)
3374 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003375 return FALSE;
3376}
3377
3378/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003379 * Update "popup_mask" if needed.
3380 * Also recomputes the popup size and positions.
3381 * Also updates "popup_visible".
3382 * Also marks window lines for redrawing.
3383 */
3384 void
3385may_update_popup_mask(int type)
3386{
3387 win_T *wp;
3388 short *mask;
3389 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003390 int redraw_all_popups = FALSE;
3391 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003392
3393 // Need to recompute when switching tabs.
3394 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3395 // (such as the screen size) must have changed.
3396 if (popup_mask_tab != curtab || type >= NOT_VALID)
3397 {
3398 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003399 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003400 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003401
3402 // Check if any popup window buffer has changed and if any popup connected
3403 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003404 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003405 if (wp->w_popup_flags & POPF_HIDDEN)
3406 popup_mask_refresh |= check_popup_unhidden(wp);
3407 else if (popup_need_position_adjust(wp))
3408 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003409 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003410 if (wp->w_popup_flags & POPF_HIDDEN)
3411 popup_mask_refresh |= check_popup_unhidden(wp);
3412 else if (popup_need_position_adjust(wp))
3413 popup_mask_refresh = TRUE;
3414
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003415 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003416 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003417
3418 // Need to update the mask, something has changed.
3419 popup_mask_refresh = FALSE;
3420 popup_mask_tab = curtab;
3421 popup_visible = FALSE;
3422
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003423 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003424 // If redrawing only what is needed, update "popup_mask_next" and then
3425 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003426 redrawing_all_win = TRUE;
3427 FOR_ALL_WINDOWS(wp)
3428 if (wp->w_redr_type < SOME_VALID)
3429 redrawing_all_win = FALSE;
3430 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003431 mask = popup_mask;
3432 else
3433 mask = popup_mask_next;
3434 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3435
3436 // Find the window with the lowest zindex that hasn't been handled yet,
3437 // so that the window with a higher zindex overwrites the value in
3438 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003439 popup_reset_handled(POPUP_HANDLED_4);
3440 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003441 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003442 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003443 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003444
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003445 popup_visible = TRUE;
3446
Bram Moolenaar12034e22019-08-25 22:25:02 +02003447 // Recompute the position if the text changed. It may make the popup
3448 // hidden if it's attach to a text property that is no longer visible.
3449 if (redraw_all_popups || popup_need_position_adjust(wp))
3450 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003451 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003452 if (wp->w_popup_flags & POPF_HIDDEN)
3453 continue;
3454 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003455
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003456 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003457 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003458 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003459 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003460 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003461 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003462 col < wp->w_wincol + width - wp->w_popup_leftoff
3463 && col < screen_Columns; ++col)
3464 if (wp->w_popup_mask_cells == NULL
3465 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003466 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003467 }
3468
3469 // Only check which lines are to be updated if not already
3470 // updating all lines.
3471 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003472 {
3473 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3474 win_T *prev_wp = NULL;
3475
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003476 for (line = 0; line < screen_Rows; ++line)
3477 {
3478 int col_done = 0;
3479
3480 for (col = 0; col < screen_Columns; ++col)
3481 {
3482 int off = line * screen_Columns + col;
3483
3484 if (popup_mask[off] != popup_mask_next[off])
3485 {
3486 popup_mask[off] = popup_mask_next[off];
3487
3488 if (line >= cmdline_row)
3489 {
3490 // the command line needs to be cleared if text below
3491 // the popup is now visible.
3492 if (!msg_scrolled && popup_mask_next[off] == 0)
3493 clear_cmdline = TRUE;
3494 }
3495 else if (col >= col_done)
3496 {
3497 linenr_T lnum;
3498 int line_cp = line;
3499 int col_cp = col;
3500
3501 // The screen position "line" / "col" needs to be
3502 // redrawn. Figure out what window that is and update
3503 // w_redraw_top and w_redr_bot. Only needs to be done
3504 // once for each window line.
3505 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3506 if (wp != NULL)
3507 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003508#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003509 // A terminal window needs to be redrawn.
3510 if (bt_terminal(wp->w_buffer))
3511 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003512 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003513#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003514 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003515 if (wp != prev_wp)
3516 {
3517 vim_memset(plines_cache, 0,
3518 sizeof(int) * Rows);
3519 prev_wp = wp;
3520 }
3521
3522 if (line_cp >= wp->w_height)
3523 // In (or below) status line
3524 wp->w_redr_status = TRUE;
3525 else
3526 {
3527 // compute the position in the buffer line
3528 // from the position in the window
3529 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003530 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003531 redrawWinline(wp, lnum);
3532 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003533 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003534
3535 // This line is going to be redrawn, no need to
3536 // check until the right side of the window.
3537 col_done = wp->w_wincol + wp->w_width - 1;
3538 }
3539 }
3540 }
3541 }
3542 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003543
3544 vim_free(plines_cache);
3545 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003546}
3547
3548/*
3549 * Return a string of "len" spaces in IObuff.
3550 */
3551 static char_u *
3552get_spaces(int len)
3553{
3554 vim_memset(IObuff, ' ', (size_t)len);
3555 IObuff[len] = NUL;
3556 return IObuff;
3557}
3558
3559/*
3560 * Update popup windows. They are drawn on top of normal windows.
3561 * "win_update" is called for each popup window, lowest zindex first.
3562 */
3563 void
3564update_popups(void (*win_update)(win_T *wp))
3565{
3566 win_T *wp;
3567 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003568 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003569 int total_width;
3570 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003571 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003572 int popup_attr;
3573 int border_attr[4];
3574 int border_char[8];
3575 char_u buf[MB_MAXBYTES];
3576 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003577 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003578 int padcol = 0;
3579 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003580 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003581 int sb_thumb_top = 0;
3582 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003583 int attr_scroll = 0;
3584 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003585
3586 // Find the window with the lowest zindex that hasn't been updated yet,
3587 // so that the window with a higher zindex is drawn later, thus goes on
3588 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003589 popup_reset_handled(POPUP_HANDLED_5);
3590 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003591 {
3592 // This drawing uses the zindex of the popup window, so that it's on
3593 // top of the text but doesn't draw when another popup with higher
3594 // zindex is on top of the character.
3595 screen_zindex = wp->w_zindex;
3596
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003597 // Set flags in popup_transparent[] for masked cells.
3598 update_popup_transparent(wp, 1);
3599
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003600 // adjust w_winrow and w_wincol for border and padding, since
3601 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003602 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003603 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3604 - wp->w_popup_leftoff;
3605 if (wp->w_wincol + left_extra < 0)
3606 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003607 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003608 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003609
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003610 // Draw the popup text, unless it's off screen.
3611 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003612 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003613 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003614
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003615 // move the cursor into the visible lines, otherwise executing
3616 // commands with win_execute() may cause the text to jump.
3617 if (wp->w_cursor.lnum < wp->w_topline)
3618 wp->w_cursor.lnum = wp->w_topline;
3619 else if (wp->w_cursor.lnum >= wp->w_botline)
3620 wp->w_cursor.lnum = wp->w_botline - 1;
3621 }
3622
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003624 wp->w_wincol -= left_extra;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003625 // cursor position matters in terminal in job mode
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003626#ifdef FEAT_TERMINAL
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003627 if (wp != curwin || !term_in_normal_mode())
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003628#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003629 {
3630 wp->w_wrow += top_off;
3631 wp->w_wcol += left_extra;
3632 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003633
Bram Moolenaard529ba52019-07-02 23:13:53 +02003634 total_width = popup_width(wp);
3635 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003636 popup_attr = get_wcr_attr(wp);
3637
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003638 if (wp->w_winrow + total_height > cmdline_row)
3639 wp->w_popup_flags |= POPF_ON_CMDLINE;
3640 else
3641 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3642
3643
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003644 // We can only use these line drawing characters when 'encoding' is
3645 // "utf-8" and 'ambiwidth' is "single".
3646 if (enc_utf8 && *p_ambw == 's')
3647 {
3648 border_char[0] = border_char[2] = 0x2550;
3649 border_char[1] = border_char[3] = 0x2551;
3650 border_char[4] = 0x2554;
3651 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003652 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3653 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003654 border_char[7] = 0x255a;
3655 }
3656 else
3657 {
3658 border_char[0] = border_char[2] = '-';
3659 border_char[1] = border_char[3] = '|';
3660 for (i = 4; i < 8; ++i)
3661 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003662 if (wp->w_popup_flags & POPF_RESIZE)
3663 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003664 }
3665 for (i = 0; i < 8; ++i)
3666 if (wp->w_border_char[i] != 0)
3667 border_char[i] = wp->w_border_char[i];
3668
3669 for (i = 0; i < 4; ++i)
3670 {
3671 border_attr[i] = popup_attr;
3672 if (wp->w_border_highlight[i] != NULL)
3673 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3674 }
3675
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003676 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003677 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003678 if (wp->w_popup_border[0] > 0)
3679 {
3680 // top border
3681 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003682 wincol < 0 ? 0 : wincol, wincol + total_width,
3683 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003684 ? border_char[4] : border_char[0],
3685 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003686 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003687 {
3688 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3689 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003690 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003691 }
3692 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003693 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3694 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003695
Bram Moolenaard529ba52019-07-02 23:13:53 +02003696 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3697 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003698 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003699 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3700 - wp->w_has_scrollbar;
3701 if (padcol < 0)
3702 {
3703 padwidth += padcol;
3704 padcol = 0;
3705 }
3706 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003707 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003708 {
3709 // top padding
3710 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003711 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003712 ' ', ' ', popup_attr);
3713 }
3714
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003715 // Title goes on top of border or padding.
3716 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003717 {
3718 int len = (int)STRLEN(wp->w_popup_title) + 1;
3719 char_u *title = alloc(len);
3720
3721 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3722 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003723 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003724 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003725 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003726
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003727 // Compute scrollbar thumb position and size.
3728 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003730 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3731 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003732
Bram Moolenaar076d9882019-09-14 22:23:29 +02003733 sb_thumb_height = (height * height + linecount / 2) / linecount;
3734 if (wp->w_topline > 1 && sb_thumb_height == height)
3735 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003736 if (sb_thumb_height == 0)
3737 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003738 if (linecount <= wp->w_height)
3739 // it just fits, avoid divide by zero
3740 sb_thumb_top = 0;
3741 else
3742 sb_thumb_top = (wp->w_topline - 1
3743 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003744 * (wp->w_height - sb_thumb_height)
3745 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003746 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3747 sb_thumb_top = 1; // show it's scrolled
3748
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003749 if (wp->w_scrollbar_highlight != NULL)
3750 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3751 else
3752 attr_scroll = highlight_attr[HLF_PSB];
3753 if (wp->w_thumb_highlight != NULL)
3754 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3755 else
3756 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003757 }
3758
3759 for (i = wp->w_popup_border[0];
3760 i < total_height - wp->w_popup_border[2]; ++i)
3761 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003762 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003763 // left and right padding only needed next to the body
3764 int do_padding =
3765 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3766 && i < total_height - wp->w_popup_border[2]
3767 - wp->w_popup_padding[2];
3768
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003769 row = wp->w_winrow + i;
3770
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003772 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773 {
3774 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003775 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003776 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003777 if (do_padding && wp->w_popup_padding[3] > 0)
3778 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003779 int col = wincol + wp->w_popup_border[3];
3780
Bram Moolenaard529ba52019-07-02 23:13:53 +02003781 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003782 pad_left = wp->w_popup_padding[3];
3783 if (col < 0)
3784 {
3785 pad_left += col;
3786 col = 0;
3787 }
3788 if (pad_left > 0)
3789 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3790 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003791 // scrollbar
3792 if (wp->w_has_scrollbar)
3793 {
3794 int line = i - top_off;
3795 int scroll_col = wp->w_wincol + total_width - 1
3796 - wp->w_popup_border[1];
3797
3798 if (line >= 0 && line < wp->w_height)
3799 screen_putchar(' ', row, scroll_col,
3800 line >= sb_thumb_top
3801 && line < sb_thumb_top + sb_thumb_height
3802 ? attr_thumb : attr_scroll);
3803 else
3804 screen_putchar(' ', row, scroll_col, popup_attr);
3805 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003806 // right border
3807 if (wp->w_popup_border[1] > 0)
3808 {
3809 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003810 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003811 }
3812 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003813 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003814 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003815 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003816 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3817 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003818 }
3819
3820 if (wp->w_popup_padding[2] > 0)
3821 {
3822 // bottom padding
3823 row = wp->w_winrow + wp->w_popup_border[0]
3824 + wp->w_popup_padding[0] + wp->w_height;
3825 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003826 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003827 }
3828
3829 if (wp->w_popup_border[2] > 0)
3830 {
3831 // bottom border
3832 row = wp->w_winrow + total_height - 1;
3833 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003834 wincol < 0 ? 0 : wincol,
3835 wincol + total_width,
3836 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003837 ? border_char[7] : border_char[2],
3838 border_char[2], border_attr[2]);
3839 if (wp->w_popup_border[1] > 0)
3840 {
3841 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003842 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003843 }
3844 }
3845
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003846 if (wp->w_popup_close == POPCLOSE_BUTTON)
3847 {
3848 // close button goes on top of anything at the top-right corner
3849 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003850 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003851 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3852 }
3853
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003854 update_popup_transparent(wp, 0);
3855
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003856 // Back to the normal zindex.
3857 screen_zindex = 0;
3858 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02003859
3860#if defined(FEAT_SEARCH_EXTRA)
3861 // In case win_update() called start_search_hl().
3862 end_search_hl();
3863#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003864}
3865
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003866/*
3867 * Mark references in callbacks of one popup window.
3868 */
3869 static int
3870set_ref_in_one_popup(win_T *wp, int copyID)
3871{
3872 int abort = FALSE;
3873 typval_T tv;
3874
3875 if (wp->w_close_cb.cb_partial != NULL)
3876 {
3877 tv.v_type = VAR_PARTIAL;
3878 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3879 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3880 }
3881 if (wp->w_filter_cb.cb_partial != NULL)
3882 {
3883 tv.v_type = VAR_PARTIAL;
3884 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3885 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3886 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003887 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003888 return abort;
3889}
3890
3891/*
3892 * Set reference in callbacks of popup windows.
3893 */
3894 int
3895set_ref_in_popups(int copyID)
3896{
3897 int abort = FALSE;
3898 win_T *wp;
3899 tabpage_T *tp;
3900
3901 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3902 abort = abort || set_ref_in_one_popup(wp, copyID);
3903
3904 FOR_ALL_TABPAGES(tp)
3905 {
3906 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3907 abort = abort || set_ref_in_one_popup(wp, copyID);
3908 if (abort)
3909 break;
3910 }
3911 return abort;
3912}
Bram Moolenaar79648732019-07-18 21:43:07 +02003913
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003914 int
3915popup_is_popup(win_T *wp)
3916{
3917 return wp->w_popup_flags != 0;
3918}
3919
3920#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003921/*
3922 * Find an existing popup used as the preview window, in the current tab page.
3923 * Return NULL if not found.
3924 */
3925 win_T *
3926popup_find_preview_window(void)
3927{
3928 win_T *wp;
3929
3930 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003931 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02003932 if (wp->w_p_pvw)
3933 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003934 return NULL;
3935}
3936
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003937/*
3938 * Find an existing popup used as the info window, in the current tab page.
3939 * Return NULL if not found.
3940 */
3941 win_T *
3942popup_find_info_window(void)
3943{
3944 win_T *wp;
3945
3946 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003947 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003948 if (wp->w_popup_flags & POPF_INFO)
3949 return wp;
3950 return NULL;
3951}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003952#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003953
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003954 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003955f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3956{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003957#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003958 win_T *wp = popup_find_info_window();
3959
3960 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003961#else
3962 rettv->vval.v_number = 0;
3963#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003964}
3965
3966 void
3967f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003968{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003969#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003970 win_T *wp = popup_find_preview_window();
3971
3972 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003973#else
3974 rettv->vval.v_number = 0;
3975#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003976}
3977
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003978#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003979/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003980 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003981 * NOTE: this makes the popup the current window, so that the file can be
3982 * edited. However, it must not remain to be the current window, the caller
3983 * must make sure of that.
3984 */
3985 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003986popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003987{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003988 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003989
3990 if (wp == NULL)
3991 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003992 if (info)
3993 wp->w_popup_flags |= POPF_INFO;
3994 else
3995 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003996
3997 // Set the width to a reasonable value, so that w_topline can be computed.
3998 if (wp->w_minwidth > 0)
3999 wp->w_width = wp->w_minwidth;
4000 else if (wp->w_maxwidth > 0)
4001 wp->w_width = wp->w_maxwidth;
4002 else
4003 wp->w_width = curwin->w_width;
4004
4005 // Will switch to another buffer soon, dummy one can be wiped.
4006 wp->w_buffer->b_locked = FALSE;
4007
4008 win_enter(wp, FALSE);
4009 return OK;
4010}
4011
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004012/*
4013 * Close any preview popup.
4014 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004015 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004016popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004017{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004018 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004019
4020 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004021 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004022}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004023
4024/*
4025 * Hide the info popup.
4026 */
4027 void
4028popup_hide_info(void)
4029{
4030 win_T *wp = popup_find_info_window();
4031
4032 if (wp != NULL)
4033 popup_hide(wp);
4034}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004035
4036/*
4037 * Close any info popup.
4038 */
4039 void
4040popup_close_info(void)
4041{
4042 win_T *wp = popup_find_info_window();
4043
4044 if (wp != NULL)
4045 popup_close_with_retval(wp, -1);
4046}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004047#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004048
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004049/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004050 * Close any popup for a text property associated with "win".
4051 * Return TRUE if a popup was closed.
4052 */
4053 int
4054popup_win_closed(win_T *win)
4055{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004056 int round;
4057 win_T *wp;
4058 win_T *next;
4059 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004060
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004061 for (round = 1; round <= 2; ++round)
4062 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4063 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004064 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004065 next = wp->w_next;
4066 if (wp->w_popup_prop_win == win)
4067 {
4068 popup_close_with_retval(wp, -1);
4069 ret = TRUE;
4070 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004071 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004072 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004073}
4074
4075/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004076 * Set the title of the popup window to the file name.
4077 */
4078 void
4079popup_set_title(win_T *wp)
4080{
4081 if (wp->w_buffer->b_fname != NULL)
4082 {
4083 char_u dirname[MAXPATHL];
4084 size_t len;
4085
4086 mch_dirname(dirname, MAXPATHL);
4087 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4088
4089 vim_free(wp->w_popup_title);
4090 len = STRLEN(wp->w_buffer->b_fname) + 3;
4091 wp->w_popup_title = alloc((int)len);
4092 if (wp->w_popup_title != NULL)
4093 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4094 wp->w_buffer->b_fname);
4095 redraw_win_later(wp, VALID);
4096 }
4097}
4098
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004099# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004100/*
4101 * If there is a preview window, update the title.
4102 * Used after changing directory.
4103 */
4104 void
4105popup_update_preview_title(void)
4106{
4107 win_T *wp = popup_find_preview_window();
4108
4109 if (wp != NULL)
4110 popup_set_title(wp);
4111}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004112# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004113
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004114#endif // FEAT_PROP_POPUP