blob: e65b4bf4404044d5c5f39fdde6c31733e07c1d13 [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)
100 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
101 ++i, li = li->li_next)
102 {
103 nr = (int)tv_get_number(&li->li_tv);
104 if (nr >= 0)
105 array[i] = nr > max_val ? max_val : nr;
106 }
107 }
108 }
109}
110
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200111/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200112 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200113 */
114 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200115set_moved_values(win_T *wp)
116{
117 wp->w_popup_curwin = curwin;
118 wp->w_popup_lnum = curwin->w_cursor.lnum;
119 wp->w_popup_mincol = curwin->w_cursor.col;
120 wp->w_popup_maxcol = curwin->w_cursor.col;
121}
122
123/*
124 * Used when popup options contain "moved" with "word" or "WORD".
125 */
126 static void
127set_moved_columns(win_T *wp, int flags)
128{
129 char_u *ptr;
130 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
131
132 if (len > 0)
133 {
134 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
135 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
136 }
137}
138
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200139/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200140 * Used when popup options contain "mousemoved": set default moved values.
141 */
142 static void
143set_mousemoved_values(win_T *wp)
144{
145 wp->w_popup_mouse_row = mouse_row;
146 wp->w_popup_mouse_mincol = mouse_col;
147 wp->w_popup_mouse_maxcol = mouse_col;
148}
149
150/*
151 * Used when popup options contain "moved" with "word" or "WORD".
152 */
153 static void
154set_mousemoved_columns(win_T *wp, int flags)
155{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200156 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200157 char_u *text;
158 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 pos_T pos;
160 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200161
162 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200163 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200165 // convert text column to mouse column
166 pos.col = col;
167 pos.coladd = 0;
168 getvcol(textwp, &pos, &mcol, NULL, NULL);
169 wp->w_popup_mouse_mincol = mcol;
170
Bram Moolenaar10727682019-07-12 13:59:20 +0200171 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200172 getvcol(textwp, &pos, NULL, NULL, &mcol);
173 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200174 vim_free(text);
175 }
176}
177
178/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200179 * Return TRUE if "row"/"col" is on the border of the popup.
180 * The values are relative to the top-left corner.
181 */
182 int
183popup_on_border(win_T *wp, int row, int col)
184{
185 return (row == 0 && wp->w_popup_border[0] > 0)
186 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
187 || (col == 0 && wp->w_popup_border[3] > 0)
188 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
189}
190
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200191/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200192 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
193 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Caller should check the left mouse button was clicked.
196 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 */
198 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200199popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200201 if (wp->w_popup_close == POPCLOSE_BUTTON
202 && row == 0 && col == popup_width(wp) - 1)
203 {
204 popup_close_for_mouse_click(wp);
205 return TRUE;
206 }
207 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200208}
209
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210// Values set when dragging a popup window starts.
211static int drag_start_row;
212static int drag_start_col;
213static int drag_start_wantline;
214static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200215static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200216
217/*
218 * Mouse down on border of popup window: start dragging it.
219 * Uses mouse_col and mouse_row.
220 */
221 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200222popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200223{
224 drag_start_row = mouse_row;
225 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200226 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200227 drag_start_wantline = wp->w_winrow + 1;
228 else
229 drag_start_wantline = wp->w_wantline;
230 if (wp->w_wantcol == 0)
231 drag_start_wantcol = wp->w_wincol + 1;
232 else
233 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200234
235 // Stop centering the popup
236 if (wp->w_popup_pos == POPPOS_CENTER)
237 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200238
239 drag_on_resize_handle = wp->w_popup_border[1] > 0
240 && wp->w_popup_border[2] > 0
241 && row == popup_height(wp) - 1
242 && col == popup_width(wp) - 1;
243
244 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
245 {
246 if (wp->w_popup_pos == POPPOS_TOPRIGHT
247 || wp->w_popup_pos == POPPOS_BOTRIGHT)
248 wp->w_wantcol = wp->w_wincol + 1;
249 if (wp->w_popup_pos == POPPOS_BOTLEFT)
250 wp->w_wantline = wp->w_winrow + 1;
251 wp->w_popup_pos = POPPOS_TOPLEFT;
252 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253}
254
255/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200256 * Mouse moved while dragging a popup window: adjust the window popup position
257 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 */
259 void
260popup_drag(win_T *wp)
261{
262 // The popup may be closed before dragging stops.
263 if (!win_valid_popup(wp))
264 return;
265
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200266 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
267 {
268 int width_inc = mouse_col - drag_start_col;
269 int height_inc = mouse_row - drag_start_row;
270
271 if (width_inc != 0)
272 {
273 int width = wp->w_width + width_inc;
274
275 if (width < 1)
276 width = 1;
277 wp->w_minwidth = width;
278 wp->w_maxwidth = width;
279 drag_start_col = mouse_col;
280 }
281
282 if (height_inc != 0)
283 {
284 int height = wp->w_height + height_inc;
285
286 if (height < 1)
287 height = 1;
288 wp->w_minheight = height;
289 wp->w_maxheight = height;
290 drag_start_row = mouse_row;
291 }
292
293 popup_adjust_position(wp);
294 return;
295 }
296
297 if (!(wp->w_popup_flags & POPF_DRAG))
298 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200299 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
300 if (wp->w_wantline < 1)
301 wp->w_wantline = 1;
302 if (wp->w_wantline > Rows)
303 wp->w_wantline = Rows;
304 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
305 if (wp->w_wantcol < 1)
306 wp->w_wantcol = 1;
307 if (wp->w_wantcol > Columns)
308 wp->w_wantcol = Columns;
309
310 popup_adjust_position(wp);
311}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200312
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200313/*
314 * Set w_firstline to match the current "wp->w_topline".
315 */
316 void
317popup_set_firstline(win_T *wp)
318{
319 int height = wp->w_height;
320
321 wp->w_firstline = wp->w_topline;
322 popup_adjust_position(wp);
323
324 // we don't want the popup to get smaller, decrement the first line
325 // until it doesn't
326 while (wp->w_firstline > 1 && wp->w_height < height)
327 {
328 --wp->w_firstline;
329 popup_adjust_position(wp);
330 }
331}
332
333/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200334 * Return TRUE if the position is in the popup window scrollbar.
335 */
336 int
337popup_is_in_scrollbar(win_T *wp, int row, int col)
338{
339 return wp->w_has_scrollbar
340 && row >= wp->w_popup_border[0]
341 && row < popup_height(wp) - wp->w_popup_border[2]
342 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
343}
344
345
346/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200347 * Handle a click in a popup window, if it is in the scrollbar.
348 */
349 void
350popup_handle_scrollbar_click(win_T *wp, int row, int col)
351{
352 int height = popup_height(wp);
353 int old_topline = wp->w_topline;
354
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200355 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200356 {
357 if (row >= height / 2)
358 {
359 // Click in lower half, scroll down.
360 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
361 ++wp->w_topline;
362 }
363 else if (wp->w_topline > 1)
364 // click on upper half, scroll up.
365 --wp->w_topline;
366 if (wp->w_topline != old_topline)
367 {
368 popup_set_firstline(wp);
369 redraw_win_later(wp, NOT_VALID);
370 }
371 }
372}
373
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200374#if defined(FEAT_TIMERS)
375 static void
376popup_add_timeout(win_T *wp, int time)
377{
378 char_u cbbuf[50];
379 char_u *ptr = cbbuf;
380 typval_T tv;
381
382 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
383 "{_ -> popup_close(%d)}", wp->w_id);
384 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
385 {
386 wp->w_popup_timer = create_timer(time, 0);
387 wp->w_popup_timer->tr_callback = get_callback(&tv);
388 clear_tv(&tv);
389 }
390}
391#endif
392
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100393 static poppos_T
394get_pos_entry(dict_T *d, int give_error)
395{
396 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
397 int nr;
398
399 if (str == NULL)
400 return POPPOS_NONE;
401
402 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
403 ++nr)
404 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
405 return poppos_entries[nr].pp_val;
406
407 if (give_error)
408 semsg(_(e_invarg2), str);
409 return POPPOS_NONE;
410}
411
Bram Moolenaar17627312019-06-02 19:53:44 +0200412/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200413 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200414 */
415 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200416apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200417{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200418 int nr;
419 char_u *str;
420 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200421
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200422 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200423 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200426 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200429 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200430
431 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200432 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433 wp->w_wantline = nr;
434 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantcol = nr;
437
438 di = dict_find(d, (char_u *)"fixed", -1);
439 if (di != NULL)
440 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
441
Bram Moolenaar12034e22019-08-25 22:25:02 +0200442 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 poppos_T ppt = get_pos_entry(d, TRUE);
444
445 if (ppt != POPPOS_NONE)
446 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200447 }
448
449 str = dict_get_string(d, (char_u *)"textprop", FALSE);
450 if (str != NULL)
451 {
452 wp->w_popup_prop_type = 0;
453 if (*str != NUL)
454 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100455 wp->w_popup_prop_win = curwin;
456 di = dict_find(d, (char_u *)"textpropwin", -1);
457 if (di != NULL)
458 {
459 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
460 if (!win_valid(wp->w_popup_prop_win))
461 wp->w_popup_prop_win = curwin;
462 }
463
464 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200465 if (nr <= 0)
466 nr = find_prop_type_id(str, NULL);
467 if (nr <= 0)
468 semsg(_(e_invarg2), str);
469 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200470 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200471 }
472 }
473
474 di = dict_find(d, (char_u *)"textpropid", -1);
475 if (di != NULL)
476 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200477}
478
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200479 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200480handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
481{
482 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
483 {
484 char_u *s = di->di_tv.vval.v_string;
485 int flags = 0;
486
487 if (STRCMP(s, "word") == 0)
488 flags = FIND_IDENT | FIND_STRING;
489 else if (STRCMP(s, "WORD") == 0)
490 flags = FIND_STRING;
491 else if (STRCMP(s, "expr") == 0)
492 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
493 else if (STRCMP(s, "any") != 0)
494 semsg(_(e_invarg2), s);
495 if (flags != 0)
496 {
497 if (mousemoved)
498 set_mousemoved_columns(wp, flags);
499 else
500 set_moved_columns(wp, flags);
501 }
502 }
503 else if (di->di_tv.v_type == VAR_LIST
504 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200505 && (di->di_tv.vval.v_list->lv_len == 2
506 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200507 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200508 list_T *l = di->di_tv.vval.v_list;
509 listitem_T *li = l->lv_first;
510 int mincol;
511 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200512
Bram Moolenaara1396152019-10-20 18:46:05 +0200513 if (di->di_tv.vval.v_list->lv_len == 3)
514 {
515 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
516
517 // Three numbers, might be from popup_getoptions().
518 if (mousemoved)
519 wp->w_popup_mouse_row = nr;
520 else
521 wp->w_popup_lnum = nr;
522 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100523 if (nr == 0)
524 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200525 }
526
527 mincol = tv_get_number(&li->li_tv);
528 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200529 if (mousemoved)
530 {
531 wp->w_popup_mouse_mincol = mincol;
532 wp->w_popup_mouse_maxcol = maxcol;
533 }
534 else
535 {
536 wp->w_popup_mincol = mincol;
537 wp->w_popup_maxcol = maxcol;
538 }
539 }
540 else
541 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
542}
543
544 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200545check_highlight(dict_T *dict, char *name, char_u **pval)
546{
547 dictitem_T *di;
548 char_u *str;
549
550 di = dict_find(dict, (char_u *)name, -1);
551 if (di != NULL)
552 {
553 if (di->di_tv.v_type != VAR_STRING)
554 semsg(_(e_invargval), name);
555 else
556 {
557 str = tv_get_string(&di->di_tv);
558 if (*str != NUL)
559 *pval = vim_strsave(str);
560 }
561 }
562}
563
Bram Moolenaarae943152019-06-16 22:54:14 +0200564/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200565 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200566 */
567 static void
568popup_show_curline(win_T *wp)
569{
570 if (wp->w_cursor.lnum < wp->w_topline)
571 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200572 else if (wp->w_cursor.lnum >= wp->w_botline
573 && (curwin->w_valid & VALID_BOTLINE))
574 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200575 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200576 if (wp->w_topline < 1)
577 wp->w_topline = 1;
578 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
579 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200580 while (wp->w_topline < wp->w_cursor.lnum
581 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
582 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
583 > wp->w_height)
584 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200585 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200586
587 // Don't use "firstline" now.
588 wp->w_firstline = 0;
589}
590
591/*
592 * Get the sign group name for window "wp".
593 * Returns a pointer to a static buffer, overwritten on the next call.
594 */
595 static char_u *
596popup_get_sign_name(win_T *wp)
597{
598 static char buf[30];
599
600 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
601 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200602}
603
604/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200605 * Highlight the line with the cursor.
606 * Also scrolls the text to put the cursor line in view.
607 */
608 static void
609popup_highlight_curline(win_T *wp)
610{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200611 int sign_id = 0;
612 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200613
Bram Moolenaar72570732019-11-30 14:21:53 +0100614 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200615
616 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
617 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200618 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200619
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200620 if (!sign_exists_by_name(sign_name))
621 {
622 char *linehl = "PopupSelected";
623
624 if (syn_name2id((char_u *)linehl) == 0)
625 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200626 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200627 }
628
Bram Moolenaar72570732019-11-30 14:21:53 +0100629 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200630 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
631 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200632 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200633 else
634 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200635 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200636}
637
638/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200639 * Shared between popup_create() and f_popup_setoptions().
640 */
641 static void
642apply_general_options(win_T *wp, dict_T *dict)
643{
644 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200645 int nr;
646 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200647
Bram Moolenaarae943152019-06-16 22:54:14 +0200648 // TODO: flip
649
650 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200651 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200652 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200653 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200654 if (wp->w_firstline < 0)
655 wp->w_firstline = -1;
656 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200657
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200658 di = dict_find(dict, (char_u *)"scrollbar", -1);
659 if (di != NULL)
660 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
661
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200662 str = dict_get_string(dict, (char_u *)"title", FALSE);
663 if (str != NULL)
664 {
665 vim_free(wp->w_popup_title);
666 wp->w_popup_title = vim_strsave(str);
667 }
668
Bram Moolenaar402502d2019-05-30 22:07:36 +0200669 di = dict_find(dict, (char_u *)"wrap", -1);
670 if (di != NULL)
671 {
672 nr = dict_get_number(dict, (char_u *)"wrap");
673 wp->w_p_wrap = nr != 0;
674 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200675
Bram Moolenaara42d9452019-06-15 21:46:30 +0200676 di = dict_find(dict, (char_u *)"drag", -1);
677 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200678 {
679 nr = dict_get_number(dict, (char_u *)"drag");
680 if (nr)
681 wp->w_popup_flags |= POPF_DRAG;
682 else
683 wp->w_popup_flags &= ~POPF_DRAG;
684 }
685
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100686 di = dict_find(dict, (char_u *)"posinvert", -1);
687 if (di != NULL)
688 {
689 nr = dict_get_number(dict, (char_u *)"posinvert");
690 if (nr)
691 wp->w_popup_flags |= POPF_POSINVERT;
692 else
693 wp->w_popup_flags &= ~POPF_POSINVERT;
694 }
695
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200696 di = dict_find(dict, (char_u *)"resize", -1);
697 if (di != NULL)
698 {
699 nr = dict_get_number(dict, (char_u *)"resize");
700 if (nr)
701 wp->w_popup_flags |= POPF_RESIZE;
702 else
703 wp->w_popup_flags &= ~POPF_RESIZE;
704 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200705
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200706 di = dict_find(dict, (char_u *)"close", -1);
707 if (di != NULL)
708 {
709 int ok = TRUE;
710
711 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
712 {
713 char_u *s = di->di_tv.vval.v_string;
714
715 if (STRCMP(s, "none") == 0)
716 wp->w_popup_close = POPCLOSE_NONE;
717 else if (STRCMP(s, "button") == 0)
718 wp->w_popup_close = POPCLOSE_BUTTON;
719 else if (STRCMP(s, "click") == 0)
720 wp->w_popup_close = POPCLOSE_CLICK;
721 else
722 ok = FALSE;
723 }
724 else
725 ok = FALSE;
726 if (!ok)
727 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
728 }
729
Bram Moolenaarae943152019-06-16 22:54:14 +0200730 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
731 if (str != NULL)
732 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
733 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200734
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200735 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
736 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200737 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
738 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200739
Bram Moolenaar790498b2019-06-01 22:15:29 +0200740 di = dict_find(dict, (char_u *)"borderhighlight", -1);
741 if (di != NULL)
742 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200743 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200744 emsg(_(e_listreq));
745 else
746 {
747 list_T *list = di->di_tv.vval.v_list;
748 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200749 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200750
Bram Moolenaar403d0902019-07-17 21:37:32 +0200751 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
752 ++i, li = li->li_next)
753 {
754 str = tv_get_string(&li->li_tv);
755 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100756 {
757 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200758 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100759 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200761 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
762 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100763 {
764 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200765 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200766 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100767 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 }
769 }
770
Bram Moolenaar790498b2019-06-01 22:15:29 +0200771 di = dict_find(dict, (char_u *)"borderchars", -1);
772 if (di != NULL)
773 {
774 if (di->di_tv.v_type != VAR_LIST)
775 emsg(_(e_listreq));
776 else
777 {
778 list_T *list = di->di_tv.vval.v_list;
779 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200780 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200781
782 if (list != NULL)
783 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
784 ++i, li = li->li_next)
785 {
786 str = tv_get_string(&li->li_tv);
787 if (*str != NUL)
788 wp->w_border_char[i] = mb_ptr2char(str);
789 }
790 if (list->lv_len == 1)
791 for (i = 1; i < 8; ++i)
792 wp->w_border_char[i] = wp->w_border_char[0];
793 if (list->lv_len == 2)
794 {
795 for (i = 4; i < 8; ++i)
796 wp->w_border_char[i] = wp->w_border_char[1];
797 for (i = 1; i < 4; ++i)
798 wp->w_border_char[i] = wp->w_border_char[0];
799 }
800 }
801 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200802
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200803 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
804 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
805
Bram Moolenaarae943152019-06-16 22:54:14 +0200806 di = dict_find(dict, (char_u *)"zindex", -1);
807 if (di != NULL)
808 {
809 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
810 if (wp->w_zindex < 1)
811 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
812 if (wp->w_zindex > 32000)
813 wp->w_zindex = 32000;
814 }
815
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200816 di = dict_find(dict, (char_u *)"mask", -1);
817 if (di != NULL)
818 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200819 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200820
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200821 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200822 {
823 listitem_T *li;
824
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200825 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200826 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
827 li = li->li_next)
828 {
829 if (li->li_tv.v_type != VAR_LIST
830 || li->li_tv.vval.v_list == NULL
831 || li->li_tv.vval.v_list->lv_len != 4)
832 {
833 ok = FALSE;
834 break;
835 }
836 }
837 }
838 if (ok)
839 {
840 wp->w_popup_mask = di->di_tv.vval.v_list;
841 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200842 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200843 }
844 else
845 semsg(_(e_invargval), "mask");
846 }
847
Bram Moolenaarae943152019-06-16 22:54:14 +0200848#if defined(FEAT_TIMERS)
849 // Add timer to close the popup after some time.
850 nr = dict_get_number(dict, (char_u *)"time");
851 if (nr > 0)
852 popup_add_timeout(wp, nr);
853#endif
854
Bram Moolenaar3397f742019-06-02 18:40:06 +0200855 di = dict_find(dict, (char_u *)"moved", -1);
856 if (di != NULL)
857 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200858 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200859 handle_moved_argument(wp, di, FALSE);
860 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200861
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200862 di = dict_find(dict, (char_u *)"mousemoved", -1);
863 if (di != NULL)
864 {
865 set_mousemoved_values(wp);
866 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200867 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200868
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200869 di = dict_find(dict, (char_u *)"cursorline", -1);
870 if (di != NULL)
871 {
872 if (di->di_tv.v_type == VAR_NUMBER)
873 {
874 if (di->di_tv.vval.v_number != 0)
875 wp->w_popup_flags |= POPF_CURSORLINE;
876 else
877 wp->w_popup_flags &= ~POPF_CURSORLINE;
878 }
879 else
880 semsg(_(e_invargval), "cursorline");
881 }
882
Bram Moolenaarae943152019-06-16 22:54:14 +0200883 di = dict_find(dict, (char_u *)"filter", -1);
884 if (di != NULL)
885 {
886 callback_T callback = get_callback(&di->di_tv);
887
888 if (callback.cb_name != NULL)
889 {
890 free_callback(&wp->w_filter_cb);
891 set_callback(&wp->w_filter_cb, &callback);
892 }
893 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200894 di = dict_find(dict, (char_u *)"mapping", -1);
895 if (di != NULL)
896 {
897 nr = dict_get_number(dict, (char_u *)"mapping");
898 if (nr)
899 wp->w_popup_flags |= POPF_MAPPING;
900 else
901 wp->w_popup_flags &= ~POPF_MAPPING;
902 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200903
Bram Moolenaar581ba392019-09-03 22:08:33 +0200904 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
905 if (str != NULL)
906 {
907 if (STRCMP(str, "a") == 0)
908 wp->w_filter_mode = MODE_ALL;
909 else
910 wp->w_filter_mode = mode_str2flags(str);
911 }
912
Bram Moolenaarae943152019-06-16 22:54:14 +0200913 di = dict_find(dict, (char_u *)"callback", -1);
914 if (di != NULL)
915 {
916 callback_T callback = get_callback(&di->di_tv);
917
918 if (callback.cb_name != NULL)
919 {
920 free_callback(&wp->w_close_cb);
921 set_callback(&wp->w_close_cb, &callback);
922 }
923 }
924}
925
926/*
927 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200928 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200929 */
930 static void
931apply_options(win_T *wp, dict_T *dict)
932{
933 int nr;
934
935 apply_move_options(wp, dict);
936 apply_general_options(wp, dict);
937
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200938 nr = dict_get_number(dict, (char_u *)"hidden");
939 if (nr > 0)
940 {
941 wp->w_popup_flags |= POPF_HIDDEN;
942 --wp->w_buffer->b_nwindows;
943 }
944
Bram Moolenaar33796b32019-06-08 16:01:13 +0200945 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200946 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200947}
948
949/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200950 * Add lines to the popup from a list of strings.
951 */
952 static void
953add_popup_strings(buf_T *buf, list_T *l)
954{
955 listitem_T *li;
956 linenr_T lnum = 0;
957 char_u *p;
958
959 for (li = l->lv_first; li != NULL; li = li->li_next)
960 if (li->li_tv.v_type == VAR_STRING)
961 {
962 p = li->li_tv.vval.v_string;
963 ml_append_buf(buf, lnum++,
964 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
965 }
966}
967
968/*
969 * Add lines to the popup from a list of dictionaries.
970 */
971 static void
972add_popup_dicts(buf_T *buf, list_T *l)
973{
974 listitem_T *li;
975 listitem_T *pli;
976 linenr_T lnum = 0;
977 char_u *p;
978 dict_T *dict;
979
980 // first add the text lines
981 for (li = l->lv_first; li != NULL; li = li->li_next)
982 {
983 if (li->li_tv.v_type != VAR_DICT)
984 {
985 emsg(_(e_dictreq));
986 return;
987 }
988 dict = li->li_tv.vval.v_dict;
989 p = dict == NULL ? NULL
990 : dict_get_string(dict, (char_u *)"text", FALSE);
991 ml_append_buf(buf, lnum++,
992 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
993 }
994
995 // add the text properties
996 lnum = 1;
997 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
998 {
999 dictitem_T *di;
1000 list_T *plist;
1001
1002 dict = li->li_tv.vval.v_dict;
1003 di = dict_find(dict, (char_u *)"props", -1);
1004 if (di != NULL)
1005 {
1006 if (di->di_tv.v_type != VAR_LIST)
1007 {
1008 emsg(_(e_listreq));
1009 return;
1010 }
1011 plist = di->di_tv.vval.v_list;
1012 if (plist != NULL)
1013 {
1014 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
1015 {
1016 if (pli->li_tv.v_type != VAR_DICT)
1017 {
1018 emsg(_(e_dictreq));
1019 return;
1020 }
1021 dict = pli->li_tv.vval.v_dict;
1022 if (dict != NULL)
1023 {
1024 int col = dict_get_number(dict, (char_u *)"col");
1025
1026 prop_add_common( lnum, col, dict, buf, NULL);
1027 }
1028 }
1029 }
1030 }
1031 }
1032}
1033
1034/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001035 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1036 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001037 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001038popup_top_extra(win_T *wp)
1039{
1040 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1041
1042 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1043 return 1;
1044 return extra;
1045}
1046
1047/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001048 * Get the padding plus border at the left.
1049 */
1050 int
1051popup_left_extra(win_T *wp)
1052{
1053 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1054}
1055
1056/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001057 * Return the height of popup window "wp", including border and padding.
1058 */
1059 int
1060popup_height(win_T *wp)
1061{
1062 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001063 + popup_top_extra(wp)
1064 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001065}
1066
1067/*
1068 * Return the width of popup window "wp", including border, padding and
1069 * scrollbar.
1070 */
1071 int
1072popup_width(win_T *wp)
1073{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001074 // w_leftcol is how many columns of the core are left of the screen
1075 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001076 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001077 + popup_extra_width(wp)
1078 + wp->w_popup_rightoff;
1079}
1080
1081/*
1082 * Return the extra width of popup window "wp": border, padding and scrollbar.
1083 */
1084 int
1085popup_extra_width(win_T *wp)
1086{
1087 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1088 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1089 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001090}
1091
1092/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001093 * Adjust the position and size of the popup to fit on the screen.
1094 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001095 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001096popup_adjust_position(win_T *wp)
1097{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001098 linenr_T lnum;
1099 int wrapped = 0;
1100 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001101 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001102 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001103 int center_vert = FALSE;
1104 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001105 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001106 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001107 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1108 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1109 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1110 int extra_height = top_extra + bot_extra;
1111 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001112 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001113 int org_winrow = wp->w_winrow;
1114 int org_wincol = wp->w_wincol;
1115 int org_width = wp->w_width;
1116 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001117 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001118 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001119 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001120 int wantline = wp->w_wantline; // adjusted for textprop
1121 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001122 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001123
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001124 wp->w_winrow = 0;
1125 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001126 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001127 wp->w_popup_leftoff = 0;
1128 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001129
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001130 // May need to update the "cursorline" highlighting, which may also change
1131 // "topline"
1132 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1133 popup_highlight_curline(wp);
1134
Bram Moolenaar12034e22019-08-25 22:25:02 +02001135 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1136 {
1137 win_T *prop_win = wp->w_popup_prop_win;
1138 textprop_T prop;
1139 linenr_T prop_lnum;
1140 pos_T pos;
1141 int screen_row;
1142 int screen_scol;
1143 int screen_ccol;
1144 int screen_ecol;
1145
1146 // Popup window is positioned relative to a text property.
1147 if (find_visible_prop(prop_win,
1148 wp->w_popup_prop_type, wp->w_popup_prop_id,
1149 &prop, &prop_lnum) == FAIL)
1150 {
1151 // Text property is no longer visible, hide the popup.
1152 // Unhiding the popup is done in check_popup_unhidden().
1153 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1154 {
1155 wp->w_popup_flags |= POPF_HIDDEN;
1156 --wp->w_buffer->b_nwindows;
1157 if (win_valid(wp->w_popup_prop_win))
1158 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1159 }
1160 return;
1161 }
1162
1163 // Compute the desired position from the position of the text
1164 // property. Use "wantline" and "wantcol" as offsets.
1165 pos.lnum = prop_lnum;
1166 pos.col = prop.tp_col;
1167 if (wp->w_popup_pos == POPPOS_TOPLEFT
1168 || wp->w_popup_pos == POPPOS_BOTLEFT)
1169 pos.col += prop.tp_len - 1;
1170 textpos2screenpos(prop_win, &pos, &screen_row,
1171 &screen_scol, &screen_ccol, &screen_ecol);
1172
1173 if (wp->w_popup_pos == POPPOS_TOPLEFT
1174 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1175 // below the text
1176 wantline = screen_row + wantline + 1;
1177 else
1178 // above the text
1179 wantline = screen_row + wantline - 1;
1180 center_vert = FALSE;
1181 if (wp->w_popup_pos == POPPOS_TOPLEFT
1182 || wp->w_popup_pos == POPPOS_BOTLEFT)
1183 // right of the text
1184 wantcol = screen_ecol + wantcol;
1185 else
1186 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001187 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001188 use_wantcol = TRUE;
1189 }
1190 else
1191 {
1192 // If no line was specified default to vertical centering.
1193 if (wantline == 0)
1194 center_vert = TRUE;
1195 else if (wantline < 0)
1196 // If "wantline" is negative it actually means zero.
1197 wantline = 0;
1198 if (wantcol < 0)
1199 // If "wantcol" is negative it actually means zero.
1200 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001201 }
1202
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001203 if (wp->w_popup_pos == POPPOS_CENTER)
1204 {
1205 // center after computing the size
1206 center_vert = TRUE;
1207 center_hor = TRUE;
1208 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001209 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001210 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001211 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1212 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001213 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001214 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001215 if (wp->w_winrow >= Rows)
1216 wp->w_winrow = Rows - 1;
1217 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001218
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001219 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001220 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001221 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1222 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001223 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001224 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001225 if (wp->w_wincol >= Columns - 3)
1226 wp->w_wincol = Columns - 3;
1227 }
1228 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001229
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001230 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001231 // When left aligned use the space available, but shift to the left when we
1232 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001233 maxspace = Columns - wp->w_wincol - left_extra;
1234 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001235 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001236 {
1237 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001238 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001239 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001240 minwidth = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001241
Bram Moolenaar8d241042019-06-12 23:40:01 +02001242 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001243 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001244 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001245 if (wp->w_topline < 1)
1246 wp->w_topline = 1;
1247 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001248 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1249
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001250 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001251 // Use a minimum width of one, so that something shows when there is no
1252 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001253 // When "firstline" is -1 then start with the last buffer line and go
1254 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001255 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001256 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001257 if (wp->w_firstline < 0)
1258 lnum = wp->w_buffer->b_ml.ml_line_count;
1259 else
1260 lnum = wp->w_topline;
1261 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001263 int len;
1264 int w_width = wp->w_width;
1265
1266 // Count Tabs for what they are worth and compute the length based on
1267 // the maximum width (matters when 'showbreak' is set).
1268 if (wp->w_width < maxwidth)
1269 wp->w_width = maxwidth;
1270 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001271 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001272 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001273
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001274 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001275 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001276 while (len > maxwidth)
1277 {
1278 ++wrapped;
1279 len -= maxwidth;
1280 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001281 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001282 }
1283 }
1284 else if (len > maxwidth
1285 && allow_adjust_left
1286 && (wp->w_popup_pos == POPPOS_TOPLEFT
1287 || wp->w_popup_pos == POPPOS_BOTLEFT))
1288 {
1289 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001290 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001291
Bram Moolenaar51c31312019-06-15 22:27:23 +02001292 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001293 {
1294 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001295
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001296 len -= truncate_shift;
1297 shift_by -= truncate_shift;
1298 }
1299
1300 wp->w_wincol -= shift_by;
1301 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001302 wp->w_width = maxwidth;
1303 }
1304 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001305 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001306 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001307 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1308 wp->w_width = wp->w_maxwidth;
1309 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001310
1311 if (wp->w_firstline < 0)
1312 --lnum;
1313 else
1314 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001315
1316 // do not use the width of lines we're not going to show
1317 if (wp->w_maxheight > 0
1318 && (wp->w_firstline >= 0
1319 ? lnum - wp->w_topline
1320 : wp->w_buffer->b_ml.ml_line_count - lnum)
1321 + wrapped >= wp->w_maxheight)
1322 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001323 }
1324
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001325 if (wp->w_firstline < 0)
1326 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1327
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001328 wp->w_has_scrollbar = wp->w_want_scrollbar
1329 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001330 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001331 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001332 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001333 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001334 // make space for the scrollbar if needed, when lines wrap and when
1335 // applying minwidth
1336 if (maxwidth + right_extra >= maxspace
1337 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1338 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001339 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001340
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001341 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1342 {
1343 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1344
1345 if (minwidth < title_len)
1346 minwidth = title_len;
1347 }
1348
1349 if (minwidth > 0 && wp->w_width < minwidth)
1350 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001351 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001352 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001353 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001354 // some columns cut off on the right
1355 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001357 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001358 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001359 {
1360 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1361 if (wp->w_wincol < 0)
1362 wp->w_wincol = 0;
1363 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001364 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1365 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1366 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001367 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001368
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001369 // Right aligned: move to the right if needed.
1370 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001371 if (leftoff >= 0)
1372 wp->w_wincol = leftoff;
1373 else if (wp->w_popup_fixed)
1374 {
1375 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001376 // columns when displaying the window, minus left border and
1377 // padding.
1378 if (-leftoff > left_extra)
1379 wp->w_leftcol = -leftoff - left_extra;
1380 wp->w_width -= wp->w_leftcol;
1381 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001382 if (wp->w_width < 0)
1383 wp->w_width = 0;
1384 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001385 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001386
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001387 if (wp->w_p_wrap || (!wp->w_popup_fixed
1388 && (wp->w_popup_pos == POPPOS_TOPLEFT
1389 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001390 {
1391 int want_col = 0;
1392
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001393 // try to show the right border and any scrollbar
1394 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001395 if (want_col > 0 && wp->w_wincol > 0
1396 && wp->w_wincol + want_col >= Columns)
1397 {
1398 wp->w_wincol = Columns - want_col;
1399 if (wp->w_wincol < 0)
1400 wp->w_wincol = 0;
1401 }
1402 }
1403
Bram Moolenaar8d241042019-06-12 23:40:01 +02001404 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1405 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001406 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1407 wp->w_height = wp->w_minheight;
1408 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1409 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001410 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001411 if (wp->w_height > Rows - wp->w_winrow)
1412 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001413 if (wp->w_height != org_height)
1414 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001415
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001416 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001417 {
1418 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1419 if (wp->w_winrow < 0)
1420 wp->w_winrow = 0;
1421 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001422 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001423 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001424 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001425 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001426 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001427 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001428 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1429 {
1430 // Bottom aligned but does not fit, and less space on the other
1431 // side or "posinvert" is off: reduce height.
1432 wp->w_winrow = 0;
1433 wp->w_height = wantline - extra_height;
1434 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001435 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001436 // Not enough space and more space on the other side: make top
1437 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001438 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001439 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001440 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1441 || wp->w_popup_pos == POPPOS_TOPLEFT)
1442 {
1443 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1444 && wantline * 2 > Rows
1445 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001446 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001447 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001448 // make bottom aligned and recompute the height
1449 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001450 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001451 if (wp->w_winrow < 0)
1452 {
1453 wp->w_height += wp->w_winrow;
1454 wp->w_winrow = 0;
1455 }
1456 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001457 else
1458 wp->w_winrow = wantline - 1;
1459 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001460 if (wp->w_winrow >= Rows)
1461 wp->w_winrow = Rows - 1;
1462 else if (wp->w_winrow < 0)
1463 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001464
Bram Moolenaar17146962019-05-30 00:12:11 +02001465 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001466 if (win_valid(wp->w_popup_prop_win))
1467 {
1468 wp->w_popup_prop_changedtick =
1469 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1470 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1471 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001472
1473 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001474 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001475 if (org_winrow != wp->w_winrow
1476 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001477 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001478 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001479 || org_width != wp->w_width
1480 || org_height != wp->w_height)
1481 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001482 redraw_win_later(wp, NOT_VALID);
1483 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1484 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001485 popup_mask_refresh = TRUE;
1486 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001487}
1488
Bram Moolenaar17627312019-06-02 19:53:44 +02001489typedef enum
1490{
1491 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001492 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001494 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001495 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001496 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001497 TYPE_PREVIEW, // preview window
1498 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001499} create_type_T;
1500
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001501/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001502 * Make "buf" empty and set the contents to "text".
1503 * Used by popup_create() and popup_settext().
1504 */
1505 static void
1506popup_set_buffer_text(buf_T *buf, typval_T text)
1507{
1508 int lnum;
1509
1510 // Clear the buffer, then replace the lines.
1511 curbuf = buf;
1512 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1513 ml_delete(lnum, FALSE);
1514 curbuf = curwin->w_buffer;
1515
1516 // Add text to the buffer.
1517 if (text.v_type == VAR_STRING)
1518 {
1519 // just a string
1520 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1521 }
1522 else
1523 {
1524 list_T *l = text.vval.v_list;
1525
1526 if (l->lv_len > 0)
1527 {
1528 if (l->lv_first->li_tv.v_type == VAR_STRING)
1529 // list of strings
1530 add_popup_strings(buf, l);
1531 else
1532 // list of dictionaries
1533 add_popup_dicts(buf, l);
1534 }
1535 }
1536
1537 // delete the line that was in the empty buffer
1538 curbuf = buf;
1539 ml_delete(buf->b_ml.ml_line_count, FALSE);
1540 curbuf = curwin->w_buffer;
1541}
1542
1543/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001544 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1545 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001546 * Return FAIL if the parsing fails.
1547 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001548 static int
1549parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001550{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001551 char_u *p =
1552#ifdef FEAT_QUICKFIX
1553 !is_preview ? p_cpp :
1554#endif
1555 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001556
Bram Moolenaar258cef52019-08-21 17:29:29 +02001557 if (wp != NULL)
1558 wp->w_popup_flags &= ~POPF_INFO_MENU;
1559
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001560 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001561 {
1562 char_u *e, *dig;
1563 char_u *s = p;
1564 int x;
1565
1566 e = vim_strchr(p, ':');
1567 if (e == NULL || e[1] == NUL)
1568 return FAIL;
1569
1570 p = vim_strchr(e, ',');
1571 if (p == NULL)
1572 p = e + STRLEN(e);
1573 dig = e + 1;
1574 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001575
1576 if (STRNCMP(s, "height:", 7) == 0)
1577 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001578 if (dig != p)
1579 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001580 if (wp != NULL)
1581 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001582 if (is_preview)
1583 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001584 wp->w_maxheight = x;
1585 }
1586 }
1587 else if (STRNCMP(s, "width:", 6) == 0)
1588 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001589 if (dig != p)
1590 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001591 if (wp != NULL)
1592 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001593 if (is_preview)
1594 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001595 wp->w_maxwidth = x;
1596 }
1597 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001598 else if (STRNCMP(s, "highlight:", 10) == 0)
1599 {
1600 if (wp != NULL)
1601 {
1602 int c = *p;
1603
1604 *p = NUL;
1605 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1606 s + 10, OPT_FREE|OPT_LOCAL, 0);
1607 *p = c;
1608 }
1609 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001610 else if (STRNCMP(s, "border:", 7) == 0)
1611 {
1612 char_u *arg = s + 7;
1613 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1614 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1615 int i;
1616
1617 if (!on && !off)
1618 return FAIL;
1619 if (wp != NULL)
1620 {
1621 for (i = 0; i < 4; ++i)
1622 wp->w_popup_border[i] = on ? 1 : 0;
1623 if (off)
1624 // only show the X for close when there is a border
1625 wp->w_popup_close = POPCLOSE_NONE;
1626 }
1627 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001628 else if (STRNCMP(s, "align:", 6) == 0)
1629 {
1630 char_u *arg = s + 6;
1631 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1632 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1633
1634 if (!menu && !item)
1635 return FAIL;
1636 if (wp != NULL && menu)
1637 wp->w_popup_flags |= POPF_INFO_MENU;
1638 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001639 else
1640 return FAIL;
1641 }
1642 return OK;
1643}
1644
1645/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001646 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1647 * is not NULL.
1648 * Return FAIL if the parsing fails.
1649 */
1650 int
1651parse_previewpopup(win_T *wp)
1652{
1653 return parse_popup_option(wp, TRUE);
1654}
1655
1656/*
1657 * Parse the 'completepopup' option and apply the values to window "wp" if it
1658 * is not NULL.
1659 * Return FAIL if the parsing fails.
1660 */
1661 int
1662parse_completepopup(win_T *wp)
1663{
1664 return parse_popup_option(wp, FALSE);
1665}
1666
1667/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001668 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001669 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001670 */
1671 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001672popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001673{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001674 poppos_T ppt = POPPOS_NONE;
1675
1676 if (d != NULL)
1677 ppt = get_pos_entry(d, FALSE);
1678
Bram Moolenaar79648732019-07-18 21:43:07 +02001679 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001680 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001681 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001682 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1683 }
1684 else
1685 {
1686 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1687 if (wp->w_wantline == 0) // cursor in first line
1688 {
1689 wp->w_wantline = 2;
1690 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1691 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1692 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001693 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001694
Bram Moolenaar79648732019-07-18 21:43:07 +02001695 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001696 if (wp->w_wantcol > Columns - width)
1697 {
1698 wp->w_wantcol = Columns - width;
1699 if (wp->w_wantcol < 1)
1700 wp->w_wantcol = 1;
1701 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001702
Bram Moolenaar79648732019-07-18 21:43:07 +02001703 popup_adjust_position(wp);
1704}
1705
1706/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001707 * Set w_wantline and w_wantcol for the a given screen position.
1708 * Caller must take care of running into the window border.
1709 */
1710 void
1711popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1712{
1713 wp->w_wantline = row;
1714 wp->w_wantcol = col;
1715 popup_adjust_position(wp);
1716}
1717
1718/*
1719 * Add a border and lef&right padding.
1720 */
1721 static void
1722add_border_left_right_padding(win_T *wp)
1723{
1724 int i;
1725
1726 for (i = 0; i < 4; ++i)
1727 {
1728 wp->w_popup_border[i] = 1;
1729 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1730 }
1731}
1732
1733/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001735 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001736 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001737 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001738 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001739 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001740popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001741{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001742 win_T *wp;
1743 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001744 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001745 int new_buffer;
1746 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001747 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001748 int nr;
1749 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001750
Bram Moolenaar79648732019-07-18 21:43:07 +02001751 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001752 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001753 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001754 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001755 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001756 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001757 if (buf == NULL)
1758 {
1759 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1760 return NULL;
1761 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001762#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001763 if (buf->b_term != NULL)
1764 {
1765 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1766 return NULL;
1767 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001768#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001769 }
1770 else if (!(argvars[0].v_type == VAR_STRING
1771 && argvars[0].vval.v_string != NULL)
1772 && !(argvars[0].v_type == VAR_LIST
1773 && argvars[0].vval.v_list != NULL))
1774 {
1775 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001776 return NULL;
1777 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001778 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001779 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001780 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001781 return NULL;
1782 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 d = argvars[1].vval.v_dict;
1784 }
1785
1786 if (d != NULL)
1787 {
1788 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1789 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1790 else if (type == TYPE_NOTIFICATION)
1791 tabnr = -1; // notifications are global by default
1792 else
1793 tabnr = 0;
1794 if (tabnr > 0)
1795 {
1796 tp = find_tabpage(tabnr);
1797 if (tp == NULL)
1798 {
1799 semsg(_("E997: Tabpage not found: %d"), tabnr);
1800 return NULL;
1801 }
1802 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001803 }
1804
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001805 // Create the window and buffer.
1806 wp = win_alloc_popup_win();
1807 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001808 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001809 if (rettv != NULL)
1810 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001811 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001812 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001813
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001814 if (buf != NULL)
1815 {
1816 // use existing buffer
1817 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001818 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001819 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001820 buffer_ensure_loaded(buf);
1821 }
1822 else
1823 {
1824 // create a new buffer associated with the popup
1825 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001826 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001827 if (buf == NULL)
1828 return NULL;
1829 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001830
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001831 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001832
Bram Moolenaar46451042019-08-24 15:50:46 +02001833 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001834 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001835 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001836 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001837 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001838 buf->b_p_ul = -1; // no undo
1839 buf->b_p_swf = FALSE; // no swap file
1840 buf->b_p_bl = FALSE; // unlisted buffer
1841 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001842
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001843 // Avoid that 'buftype' is reset when this buffer is entered.
1844 buf->b_p_initialized = TRUE;
1845 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001846 wp->w_p_wrap = TRUE; // 'wrap' is default on
1847 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001848
Bram Moolenaara3fce622019-06-20 02:31:49 +02001849 if (tp != NULL)
1850 {
1851 // popup on specified tab page
1852 wp->w_next = tp->tp_first_popupwin;
1853 tp->tp_first_popupwin = wp;
1854 }
1855 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001856 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001857 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001858 wp->w_next = curtab->tp_first_popupwin;
1859 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001860 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001861 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001862 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001863 win_T *prev = first_popupwin;
1864
1865 // Global popup: add at the end, so that it gets displayed on top of
1866 // older ones with the same zindex. Matters for notifications.
1867 if (first_popupwin == NULL)
1868 first_popupwin = wp;
1869 else
1870 {
1871 while (prev->w_next != NULL)
1872 prev = prev->w_next;
1873 prev->w_next = wp;
1874 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001875 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001876
Bram Moolenaar79648732019-07-18 21:43:07 +02001877 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001878 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001879
Bram Moolenaar79648732019-07-18 21:43:07 +02001880 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001881 {
1882 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001883 }
1884 if (type == TYPE_ATCURSOR)
1885 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001886 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001887 set_moved_values(wp);
1888 set_moved_columns(wp, FIND_STRING);
1889 }
1890
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001891 if (type == TYPE_BEVAL)
1892 {
1893 wp->w_popup_pos = POPPOS_BOTLEFT;
1894
1895 // by default use the mouse position
1896 wp->w_wantline = mouse_row;
1897 if (wp->w_wantline <= 0) // mouse on first line
1898 {
1899 wp->w_wantline = 2;
1900 wp->w_popup_pos = POPPOS_TOPLEFT;
1901 }
1902 wp->w_wantcol = mouse_col + 1;
1903 set_mousemoved_values(wp);
1904 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1905 }
1906
Bram Moolenaar33796b32019-06-08 16:01:13 +02001907 // set default values
1908 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001909 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001910
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001911 if (type == TYPE_NOTIFICATION)
1912 {
1913 win_T *twp, *nextwin;
1914 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001915
1916 // Try to not overlap with another global popup. Guess we need 3
1917 // more screen lines than buffer lines.
1918 wp->w_wantline = 1;
1919 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1920 {
1921 nextwin = twp->w_next;
1922 if (twp != wp
1923 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1924 && twp->w_winrow <= wp->w_wantline - 1 + height
1925 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1926 {
1927 // move to below this popup and restart the loop to check for
1928 // overlap with other popups
1929 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1930 nextwin = first_popupwin;
1931 }
1932 }
1933 if (wp->w_wantline + height > Rows)
1934 {
1935 // can't avoid overlap, put on top in the hope that message goes
1936 // away soon.
1937 wp->w_wantline = 1;
1938 }
1939
1940 wp->w_wantcol = 10;
1941 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001942 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001943 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001944 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001945 for (i = 0; i < 4; ++i)
1946 wp->w_popup_border[i] = 1;
1947 wp->w_popup_padding[1] = 1;
1948 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001949
1950 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001951 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001952 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1953 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001954 }
1955
Bram Moolenaara730e552019-06-16 19:05:31 +02001956 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001957 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001958 wp->w_popup_pos = POPPOS_CENTER;
1959 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001960 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001961 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001962 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001963 }
1964
Bram Moolenaara730e552019-06-16 19:05:31 +02001965 if (type == TYPE_MENU)
1966 {
1967 typval_T tv;
1968 callback_T callback;
1969
1970 tv.v_type = VAR_STRING;
1971 tv.vval.v_string = (char_u *)"popup_filter_menu";
1972 callback = get_callback(&tv);
1973 if (callback.cb_name != NULL)
1974 set_callback(&wp->w_filter_cb, &callback);
1975
1976 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001977 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001978 }
1979
Bram Moolenaar79648732019-07-18 21:43:07 +02001980 if (type == TYPE_PREVIEW)
1981 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001982 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001983 wp->w_popup_close = POPCLOSE_BUTTON;
1984 for (i = 0; i < 4; ++i)
1985 wp->w_popup_border[i] = 1;
1986 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001987 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001988 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001989# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001990 if (type == TYPE_INFO)
1991 {
1992 wp->w_popup_pos = POPPOS_TOPLEFT;
1993 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1994 wp->w_popup_close = POPCLOSE_BUTTON;
1995 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001996 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001997 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001998# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001999
Bram Moolenaarae943152019-06-16 22:54:14 +02002000 for (i = 0; i < 4; ++i)
2001 VIM_CLEAR(wp->w_border_highlight[i]);
2002 for (i = 0; i < 8; ++i)
2003 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002004 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002005 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002006 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002007
Bram Moolenaar79648732019-07-18 21:43:07 +02002008 if (d != NULL)
2009 // Deal with options.
2010 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002011
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002012#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002013 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2014 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002015#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002016
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002017 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018
2019 wp->w_vsep_width = 0;
2020
2021 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002022 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002023
2024 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002025}
2026
2027/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002028 * popup_clear()
2029 */
2030 void
2031f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2032{
2033 close_all_popups();
2034}
2035
2036/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002037 * popup_create({text}, {options})
2038 */
2039 void
2040f_popup_create(typval_T *argvars, typval_T *rettv)
2041{
Bram Moolenaar17627312019-06-02 19:53:44 +02002042 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002043}
2044
2045/*
2046 * popup_atcursor({text}, {options})
2047 */
2048 void
2049f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2050{
Bram Moolenaar17627312019-06-02 19:53:44 +02002051 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002052}
2053
2054/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002055 * popup_beval({text}, {options})
2056 */
2057 void
2058f_popup_beval(typval_T *argvars, typval_T *rettv)
2059{
2060 popup_create(argvars, rettv, TYPE_BEVAL);
2061}
2062
2063/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002064 * Invoke the close callback for window "wp" with value "result".
2065 * Careful: The callback may make "wp" invalid!
2066 */
2067 static void
2068invoke_popup_callback(win_T *wp, typval_T *result)
2069{
2070 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002071 typval_T argv[3];
2072
2073 argv[0].v_type = VAR_NUMBER;
2074 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2075
2076 if (result != NULL && result->v_type != VAR_UNKNOWN)
2077 copy_tv(result, &argv[1]);
2078 else
2079 {
2080 argv[1].v_type = VAR_NUMBER;
2081 argv[1].vval.v_number = 0;
2082 }
2083
2084 argv[2].v_type = VAR_UNKNOWN;
2085
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002086 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002087 if (result != NULL)
2088 clear_tv(&argv[1]);
2089 clear_tv(&rettv);
2090}
2091
2092/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002093 * Close popup "wp" and invoke any close callback for it.
2094 */
2095 static void
2096popup_close_and_callback(win_T *wp, typval_T *arg)
2097{
2098 int id = wp->w_id;
2099
Bram Moolenaar49540192019-12-11 19:34:54 +01002100 // Just in case a check higher up is missing.
2101 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2102 return;
2103
Bram Moolenaar3397f742019-06-02 18:40:06 +02002104 if (wp->w_close_cb.cb_name != NULL)
2105 // Careful: This may make "wp" invalid.
2106 invoke_popup_callback(wp, arg);
2107
2108 popup_close(id);
2109}
2110
Bram Moolenaar12034e22019-08-25 22:25:02 +02002111 static void
2112popup_close_with_retval(win_T *wp, int retval)
2113{
2114 typval_T res;
2115
2116 res.v_type = VAR_NUMBER;
2117 res.vval.v_number = retval;
2118 popup_close_and_callback(wp, &res);
2119}
2120
Bram Moolenaar3397f742019-06-02 18:40:06 +02002121/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002122 * Close popup "wp" because of a mouse click.
2123 */
2124 void
2125popup_close_for_mouse_click(win_T *wp)
2126{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002127 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002128}
2129
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002130 static void
2131check_mouse_moved(win_T *wp, win_T *mouse_wp)
2132{
2133 // Close the popup when all if these are true:
2134 // - the mouse is not on this popup
2135 // - "mousemoved" was used
2136 // - the mouse is no longer on the same screen row or the mouse column is
2137 // outside of the relevant text
2138 if (wp != mouse_wp
2139 && wp->w_popup_mouse_row != 0
2140 && (wp->w_popup_mouse_row != mouse_row
2141 || mouse_col < wp->w_popup_mouse_mincol
2142 || mouse_col > wp->w_popup_mouse_maxcol))
2143 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002144 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002145 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002146 }
2147}
2148
2149/*
2150 * Called when the mouse moved: may close a popup with "mousemoved".
2151 */
2152 void
2153popup_handle_mouse_moved(void)
2154{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002155 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002156 win_T *mouse_wp;
2157 int row = mouse_row;
2158 int col = mouse_col;
2159
2160 // find the window where the mouse is in
2161 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2162
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002163 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2164 {
2165 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002166 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002167 }
2168 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2169 {
2170 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002171 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002172 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002173}
2174
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002175/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002176 * In a filter: check if the typed key is a mouse event that is used for
2177 * dragging the popup.
2178 */
2179 static void
2180filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2181{
2182 int row = mouse_row;
2183 int col = mouse_col;
2184
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002185 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002186 && is_mouse_key(c)
2187 && (wp == popup_dragwin
2188 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2189 // do not consume the key, allow for dragging the popup
2190 rettv->vval.v_number = 0;
2191}
2192
Bram Moolenaara730e552019-06-16 19:05:31 +02002193/*
2194 * popup_filter_menu({text}, {options})
2195 */
2196 void
2197f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2198{
2199 int id = tv_get_number(&argvars[0]);
2200 win_T *wp = win_id2wp(id);
2201 char_u *key = tv_get_string(&argvars[1]);
2202 typval_T res;
2203 int c;
2204 linenr_T old_lnum;
2205
2206 // If the popup has been closed do not consume the key.
2207 if (wp == NULL)
2208 return;
2209
2210 c = *key;
2211 if (c == K_SPECIAL && key[1] != NUL)
2212 c = TO_SPECIAL(key[1], key[2]);
2213
2214 // consume all keys until done
2215 rettv->vval.v_number = 1;
2216 res.v_type = VAR_NUMBER;
2217
2218 old_lnum = wp->w_cursor.lnum;
2219 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2220 --wp->w_cursor.lnum;
2221 if ((c == 'j' || c == 'J' || c == K_DOWN)
2222 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2223 ++wp->w_cursor.lnum;
2224 if (old_lnum != wp->w_cursor.lnum)
2225 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002226 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002227 return;
2228 }
2229
2230 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2231 {
2232 // Cancelled, invoke callback with -1
2233 res.vval.v_number = -1;
2234 popup_close_and_callback(wp, &res);
2235 return;
2236 }
2237 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2238 {
2239 // Invoke callback with current index.
2240 res.vval.v_number = wp->w_cursor.lnum;
2241 popup_close_and_callback(wp, &res);
2242 return;
2243 }
2244
2245 filter_handle_drag(wp, c, rettv);
2246}
2247
2248/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002249 * popup_filter_yesno({text}, {options})
2250 */
2251 void
2252f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2253{
2254 int id = tv_get_number(&argvars[0]);
2255 win_T *wp = win_id2wp(id);
2256 char_u *key = tv_get_string(&argvars[1]);
2257 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002258 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002259
2260 // If the popup has been closed don't consume the key.
2261 if (wp == NULL)
2262 return;
2263
Bram Moolenaara730e552019-06-16 19:05:31 +02002264 c = *key;
2265 if (c == K_SPECIAL && key[1] != NUL)
2266 c = TO_SPECIAL(key[1], key[2]);
2267
Bram Moolenaara42d9452019-06-15 21:46:30 +02002268 // consume all keys until done
2269 rettv->vval.v_number = 1;
2270
Bram Moolenaara730e552019-06-16 19:05:31 +02002271 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002272 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002273 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002274 res.vval.v_number = 0;
2275 else
2276 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002277 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002278 return;
2279 }
2280
2281 // Invoke callback
2282 res.v_type = VAR_NUMBER;
2283 popup_close_and_callback(wp, &res);
2284}
2285
2286/*
2287 * popup_dialog({text}, {options})
2288 */
2289 void
2290f_popup_dialog(typval_T *argvars, typval_T *rettv)
2291{
2292 popup_create(argvars, rettv, TYPE_DIALOG);
2293}
2294
2295/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002296 * popup_menu({text}, {options})
2297 */
2298 void
2299f_popup_menu(typval_T *argvars, typval_T *rettv)
2300{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002301 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002302}
2303
2304/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002305 * popup_notification({text}, {options})
2306 */
2307 void
2308f_popup_notification(typval_T *argvars, typval_T *rettv)
2309{
2310 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2311}
2312
2313/*
2314 * Find the popup window with window-ID "id".
2315 * If the popup window does not exist NULL is returned.
2316 * If the window is not a popup window, and error message is given.
2317 */
2318 static win_T *
2319find_popup_win(int id)
2320{
2321 win_T *wp = win_id2wp(id);
2322
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002323 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002324 {
2325 semsg(_("E993: window %d is not a popup window"), id);
2326 return NULL;
2327 }
2328 return wp;
2329}
2330
2331/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002332 * popup_close({id})
2333 */
2334 void
2335f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2336{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002337 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002338 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002339
Bram Moolenaar49540192019-12-11 19:34:54 +01002340 if (ERROR_IF_POPUP_WINDOW)
2341 return;
2342
2343 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002344 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002345 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002346}
2347
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002348 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002349popup_hide(win_T *wp)
2350{
2351 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2352 {
2353 wp->w_popup_flags |= POPF_HIDDEN;
2354 --wp->w_buffer->b_nwindows;
2355 redraw_all_later(NOT_VALID);
2356 popup_mask_refresh = TRUE;
2357 }
2358}
2359
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002360/*
2361 * popup_hide({id})
2362 */
2363 void
2364f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2365{
2366 int id = (int)tv_get_number(argvars);
2367 win_T *wp = find_popup_win(id);
2368
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002369 if (wp != NULL)
2370 popup_hide(wp);
2371}
2372
2373 void
2374popup_show(win_T *wp)
2375{
2376 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002377 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002378 wp->w_popup_flags &= ~POPF_HIDDEN;
2379 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002380 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002381 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002382 }
2383}
2384
2385/*
2386 * popup_show({id})
2387 */
2388 void
2389f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2390{
2391 int id = (int)tv_get_number(argvars);
2392 win_T *wp = find_popup_win(id);
2393
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002394 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002395 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002396 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002397#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002398 if (wp->w_popup_flags & POPF_INFO)
2399 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002400#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002401 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002402}
2403
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002404/*
2405 * popup_settext({id}, {text})
2406 */
2407 void
2408f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2409{
2410 int id = (int)tv_get_number(&argvars[0]);
2411 win_T *wp = find_popup_win(id);
2412
2413 if (wp != NULL)
2414 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002415 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2416 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2417 else
2418 {
2419 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002420 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002421 popup_adjust_position(wp);
2422 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002423 }
2424}
2425
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002426 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002427popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002428{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002429 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002430 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002431 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002432 clear_cmdline = TRUE;
2433 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002434
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002435 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002436 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002437}
2438
Bram Moolenaarec583842019-05-26 14:11:23 +02002439/*
2440 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002441 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002442 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002443 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002444popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002445{
2446 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002447 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002448 win_T *prev = NULL;
2449
Bram Moolenaarec583842019-05-26 14:11:23 +02002450 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002451 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002452 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002453 {
2454 if (prev == NULL)
2455 first_popupwin = wp->w_next;
2456 else
2457 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002458 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002459 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002460 }
2461
Bram Moolenaarec583842019-05-26 14:11:23 +02002462 // go through tab-local popups
2463 FOR_ALL_TABPAGES(tp)
2464 popup_close_tabpage(tp, id);
2465}
2466
2467/*
2468 * Close a popup window with Window-id "id" in tabpage "tp".
2469 */
2470 void
2471popup_close_tabpage(tabpage_T *tp, int id)
2472{
2473 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002474 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002475 win_T *prev = NULL;
2476
Bram Moolenaarec583842019-05-26 14:11:23 +02002477 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2478 if (wp->w_id == id)
2479 {
2480 if (prev == NULL)
2481 *root = wp->w_next;
2482 else
2483 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002484 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002485 return;
2486 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002487}
2488
2489 void
2490close_all_popups(void)
2491{
2492 while (first_popupwin != NULL)
2493 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002494 while (curtab->tp_first_popupwin != NULL)
2495 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002496}
2497
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002498/*
2499 * popup_move({id}, {options})
2500 */
2501 void
2502f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2503{
Bram Moolenaarae943152019-06-16 22:54:14 +02002504 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002505 int id = (int)tv_get_number(argvars);
2506 win_T *wp = find_popup_win(id);
2507
2508 if (wp == NULL)
2509 return; // invalid {id}
2510
2511 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2512 {
2513 emsg(_(e_dictreq));
2514 return;
2515 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002516 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002517
Bram Moolenaarae943152019-06-16 22:54:14 +02002518 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002519
2520 if (wp->w_winrow + wp->w_height >= cmdline_row)
2521 clear_cmdline = TRUE;
2522 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002523}
2524
Bram Moolenaarbc133542019-05-29 20:26:46 +02002525/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002526 * popup_setoptions({id}, {options})
2527 */
2528 void
2529f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2530{
2531 dict_T *dict;
2532 int id = (int)tv_get_number(argvars);
2533 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002534 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002535
2536 if (wp == NULL)
2537 return; // invalid {id}
2538
2539 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2540 {
2541 emsg(_(e_dictreq));
2542 return;
2543 }
2544 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002545 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002546
2547 apply_move_options(wp, dict);
2548 apply_general_options(wp, dict);
2549
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002550 if (old_firstline != wp->w_firstline)
2551 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002552 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002553 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002554 popup_adjust_position(wp);
2555}
2556
2557/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002558 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002559 */
2560 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002561f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002562{
2563 dict_T *dict;
2564 int id = (int)tv_get_number(argvars);
2565 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002566 int top_extra;
2567 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002568
2569 if (rettv_dict_alloc(rettv) == OK)
2570 {
2571 if (wp == NULL)
2572 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002573 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002574 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2575
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002576 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002577 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002578 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002579
Bram Moolenaarbc133542019-05-29 20:26:46 +02002580 dict_add_number(dict, "line", wp->w_winrow + 1);
2581 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002582 dict_add_number(dict, "width", wp->w_width + left_extra
2583 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2584 dict_add_number(dict, "height", wp->w_height + top_extra
2585 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002586
2587 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2588 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2589 dict_add_number(dict, "core_width", wp->w_width);
2590 dict_add_number(dict, "core_height", wp->w_height);
2591
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002592 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002593 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002594 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002595 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002596 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002597
2598 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002599 }
2600}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002601/*
2602 * popup_locate({row}, {col})
2603 */
2604 void
2605f_popup_locate(typval_T *argvars, typval_T *rettv)
2606{
2607 int row = tv_get_number(&argvars[0]) - 1;
2608 int col = tv_get_number(&argvars[1]) - 1;
2609 win_T *wp;
2610
2611 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002612 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002613 rettv->vval.v_number = wp->w_id;
2614}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002615
2616/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002617 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2618 */
2619 static void
2620get_padding_border(dict_T *dict, int *array, char *name)
2621{
2622 list_T *list;
2623 int i;
2624
2625 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2626 return;
2627
2628 list = list_alloc();
2629 if (list != NULL)
2630 {
2631 dict_add_list(dict, name, list);
2632 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2633 for (i = 0; i < 4; ++i)
2634 list_append_number(list, array[i]);
2635 }
2636}
2637
2638/*
2639 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2640 */
2641 static void
2642get_borderhighlight(dict_T *dict, win_T *wp)
2643{
2644 list_T *list;
2645 int i;
2646
2647 for (i = 0; i < 4; ++i)
2648 if (wp->w_border_highlight[i] != NULL)
2649 break;
2650 if (i == 4)
2651 return;
2652
2653 list = list_alloc();
2654 if (list != NULL)
2655 {
2656 dict_add_list(dict, "borderhighlight", list);
2657 for (i = 0; i < 4; ++i)
2658 list_append_string(list, wp->w_border_highlight[i], -1);
2659 }
2660}
2661
2662/*
2663 * For popup_getoptions(): add a "borderchars" entry to "dict".
2664 */
2665 static void
2666get_borderchars(dict_T *dict, win_T *wp)
2667{
2668 list_T *list;
2669 int i;
2670 char_u buf[NUMBUFLEN];
2671 int len;
2672
2673 for (i = 0; i < 8; ++i)
2674 if (wp->w_border_char[i] != 0)
2675 break;
2676 if (i == 8)
2677 return;
2678
2679 list = list_alloc();
2680 if (list != NULL)
2681 {
2682 dict_add_list(dict, "borderchars", list);
2683 for (i = 0; i < 8; ++i)
2684 {
2685 len = mb_char2bytes(wp->w_border_char[i], buf);
2686 list_append_string(list, buf, len);
2687 }
2688 }
2689}
2690
2691/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002692 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002693 */
2694 static void
2695get_moved_list(dict_T *dict, win_T *wp)
2696{
2697 list_T *list;
2698
2699 list = list_alloc();
2700 if (list != NULL)
2701 {
2702 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002703 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002704 list_append_number(list, wp->w_popup_mincol);
2705 list_append_number(list, wp->w_popup_maxcol);
2706 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002707 list = list_alloc();
2708 if (list != NULL)
2709 {
2710 dict_add_list(dict, "mousemoved", list);
2711 list_append_number(list, wp->w_popup_mouse_row);
2712 list_append_number(list, wp->w_popup_mouse_mincol);
2713 list_append_number(list, wp->w_popup_mouse_maxcol);
2714 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002715}
2716
2717/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002718 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002719 */
2720 void
2721f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2722{
2723 dict_T *dict;
2724 int id = (int)tv_get_number(argvars);
2725 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002726 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002727 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002728
2729 if (rettv_dict_alloc(rettv) == OK)
2730 {
2731 if (wp == NULL)
2732 return;
2733
2734 dict = rettv->vval.v_dict;
2735 dict_add_number(dict, "line", wp->w_wantline);
2736 dict_add_number(dict, "col", wp->w_wantcol);
2737 dict_add_number(dict, "minwidth", wp->w_minwidth);
2738 dict_add_number(dict, "minheight", wp->w_minheight);
2739 dict_add_number(dict, "maxheight", wp->w_maxheight);
2740 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002741 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002742 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002743 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002744 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002745 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2746 {
2747 proptype_T *pt = text_prop_type_by_id(
2748 wp->w_popup_prop_win->w_buffer,
2749 wp->w_popup_prop_type);
2750
2751 if (pt != NULL)
2752 dict_add_string(dict, "textprop", pt->pt_name);
2753 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2754 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2755 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002756 dict_add_string(dict, "title", wp->w_popup_title);
2757 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002758 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002759 dict_add_number(dict, "mapping",
2760 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002761 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002762 dict_add_number(dict, "posinvert",
2763 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002764 dict_add_number(dict, "cursorline",
2765 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002766 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002767 if (wp->w_scrollbar_highlight != NULL)
2768 dict_add_string(dict, "scrollbarhighlight",
2769 wp->w_scrollbar_highlight);
2770 if (wp->w_thumb_highlight != NULL)
2771 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002772
Bram Moolenaara3fce622019-06-20 02:31:49 +02002773 // find the tabpage that holds this popup
2774 i = 1;
2775 FOR_ALL_TABPAGES(tp)
2776 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002777 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002778
Bram Moolenaar1824f452019-10-02 23:06:46 +02002779 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2780 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002781 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002782 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002783 break;
2784 ++i;
2785 }
2786 if (tp == NULL)
2787 i = -1; // must be global
2788 else if (tp == curtab)
2789 i = 0;
2790 dict_add_number(dict, "tabpage", i);
2791
Bram Moolenaarae943152019-06-16 22:54:14 +02002792 get_padding_border(dict, wp->w_popup_padding, "padding");
2793 get_padding_border(dict, wp->w_popup_border, "border");
2794 get_borderhighlight(dict, wp);
2795 get_borderchars(dict, wp);
2796 get_moved_list(dict, wp);
2797
2798 if (wp->w_filter_cb.cb_name != NULL)
2799 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2800 if (wp->w_close_cb.cb_name != NULL)
2801 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002802
2803 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2804 ++i)
2805 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2806 {
2807 dict_add_string(dict, "pos",
2808 (char_u *)poppos_entries[i].pp_name);
2809 break;
2810 }
2811
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002812 dict_add_string(dict, "close", (char_u *)(
2813 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2814 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2815
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002816# if defined(FEAT_TIMERS)
2817 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2818 ? (long)wp->w_popup_timer->tr_interval : 0L);
2819# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002820 }
2821}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002822
2823 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002824error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002825{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002826 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002827 {
2828 emsg(_("E994: Not allowed in a popup window"));
2829 return TRUE;
2830 }
2831 return FALSE;
2832}
2833
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002834/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002835 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002836 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002837 * Each calling function should use a different flag, see the list at
2838 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002839 */
2840 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002841popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002842{
2843 win_T *wp;
2844
2845 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002846 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002847 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002848 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002849}
2850
2851/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002852 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002853 * Must have called popup_reset_handled() first.
2854 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2855 * popup with the highest zindex.
2856 */
2857 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002858find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002859{
2860 win_T *wp;
2861 win_T *found_wp;
2862 int found_zindex;
2863
2864 found_zindex = lowest ? INT_MAX : 0;
2865 found_wp = NULL;
2866 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002867 if ((wp->w_popup_handled & handled_flag) == 0
2868 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002869 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002870 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002871 {
2872 found_zindex = wp->w_zindex;
2873 found_wp = wp;
2874 }
2875 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002876 if ((wp->w_popup_handled & handled_flag) == 0
2877 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002878 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002879 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002880 {
2881 found_zindex = wp->w_zindex;
2882 found_wp = wp;
2883 }
2884
2885 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002886 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002887 return found_wp;
2888}
2889
2890/*
2891 * Invoke the filter callback for window "wp" with typed character "c".
2892 * Uses the global "mod_mask" for modifiers.
2893 * Returns the return value of the filter.
2894 * Careful: The filter may make "wp" invalid!
2895 */
2896 static int
2897invoke_popup_filter(win_T *wp, int c)
2898{
2899 int res;
2900 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002901 typval_T argv[3];
2902 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002903 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002904
Bram Moolenaara42d9452019-06-15 21:46:30 +02002905 // Emergency exit: CTRL-C closes the popup.
2906 if (c == Ctrl_C)
2907 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002908 int save_got_int = got_int;
2909
2910 // Reset got_int to avoid the callback isn't called.
2911 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002912 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002913 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002914 return 1;
2915 }
2916
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002917 argv[0].v_type = VAR_NUMBER;
2918 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2919
2920 // Convert the number to a string, so that the function can use:
2921 // if a:c == "\<F2>"
2922 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2923 argv[1].v_type = VAR_STRING;
2924 argv[1].vval.v_string = vim_strsave(buf);
2925
2926 argv[2].v_type = VAR_UNKNOWN;
2927
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002928 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002929 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002930 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002931 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002932 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002933
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002934 vim_free(argv[1].vval.v_string);
2935 clear_tv(&rettv);
2936 return res;
2937}
2938
2939/*
2940 * Called when "c" was typed: invoke popup filter callbacks.
2941 * Returns TRUE when the character was consumed,
2942 */
2943 int
2944popup_do_filter(int c)
2945{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002946 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002947 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002948 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002949 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002950 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002951 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002952
2953 if (recursive)
2954 return FALSE;
2955 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002956
Bram Moolenaarf6396232019-08-24 19:36:00 +02002957 if (c == K_LEFTMOUSE)
2958 {
2959 int row = mouse_row;
2960 int col = mouse_col;
2961
2962 wp = mouse_find_win(&row, &col, FIND_POPUP);
2963 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002964 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002965 }
2966
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002967 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02002968 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002969 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002970 if (wp->w_filter_cb.cb_name != NULL
2971 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002972 res = invoke_popup_filter(wp, c);
2973
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002974 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002975 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002976 recursive = FALSE;
2977 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002978 return res;
2979}
2980
Bram Moolenaar3397f742019-06-02 18:40:06 +02002981/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002982 * Return TRUE if there is a popup visible with a filter callback and the
2983 * "mapping" property off.
2984 */
2985 int
2986popup_no_mapping(void)
2987{
2988 int round;
2989 win_T *wp;
2990
2991 for (round = 1; round <= 2; ++round)
2992 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2993 wp != NULL; wp = wp->w_next)
2994 if (wp->w_filter_cb.cb_name != NULL
2995 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2996 return TRUE;
2997 return FALSE;
2998}
2999
3000/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003001 * Called when the cursor moved: check if any popup needs to be closed if the
3002 * cursor moved far enough.
3003 */
3004 void
3005popup_check_cursor_pos()
3006{
3007 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003008
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003009 popup_reset_handled(POPUP_HANDLED_3);
3010 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003011 if (wp->w_popup_curwin != NULL
3012 && (curwin != wp->w_popup_curwin
3013 || curwin->w_cursor.lnum != wp->w_popup_lnum
3014 || curwin->w_cursor.col < wp->w_popup_mincol
3015 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003016 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003017}
3018
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003019/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003020 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003021 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003022 static void
3023popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003024{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003025 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003026 char_u *cells;
3027 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003028
3029 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003030 return;
3031 if (wp->w_popup_mask_cells != NULL
3032 && wp->w_popup_mask_height == height
3033 && wp->w_popup_mask_width == width)
3034 return; // cache is still valid
3035
3036 vim_free(wp->w_popup_mask_cells);
3037 wp->w_popup_mask_cells = alloc_clear(width * height);
3038 if (wp->w_popup_mask_cells == NULL)
3039 return;
3040 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003041
3042 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3043 {
3044 int cols, cole;
3045 int lines, linee;
3046
3047 li = lio->li_tv.vval.v_list->lv_first;
3048 cols = tv_get_number(&li->li_tv);
3049 if (cols < 0)
3050 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003051 li = li->li_next;
3052 cole = tv_get_number(&li->li_tv);
3053 if (cole < 0)
3054 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003055 li = li->li_next;
3056 lines = tv_get_number(&li->li_tv);
3057 if (lines < 0)
3058 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003059 li = li->li_next;
3060 linee = tv_get_number(&li->li_tv);
3061 if (linee < 0)
3062 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003063
3064 for (row = lines - 1; row < linee && row < height; ++row)
3065 for (col = cols - 1; col < cole && col < width; ++col)
3066 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003067 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003068}
3069
3070/*
3071 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3072 * "col" and "line" are screen coordinates.
3073 */
3074 static int
3075popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3076{
3077 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3078 int line = screenline - wp->w_winrow;
3079
3080 return col >= 0 && col < width
3081 && line >= 0 && line < height
3082 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003083}
3084
3085/*
3086 * Set flags in popup_transparent[] for window "wp" to "val".
3087 */
3088 static void
3089update_popup_transparent(win_T *wp, int val)
3090{
3091 if (wp->w_popup_mask != NULL)
3092 {
3093 int width = popup_width(wp);
3094 int height = popup_height(wp);
3095 listitem_T *lio, *li;
3096 int cols, cole;
3097 int lines, linee;
3098 int col, line;
3099
3100 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3101 {
3102 li = lio->li_tv.vval.v_list->lv_first;
3103 cols = tv_get_number(&li->li_tv);
3104 if (cols < 0)
3105 cols = width + cols + 1;
3106 li = li->li_next;
3107 cole = tv_get_number(&li->li_tv);
3108 if (cole < 0)
3109 cole = width + cole + 1;
3110 li = li->li_next;
3111 lines = tv_get_number(&li->li_tv);
3112 if (lines < 0)
3113 lines = height + lines + 1;
3114 li = li->li_next;
3115 linee = tv_get_number(&li->li_tv);
3116 if (linee < 0)
3117 linee = height + linee + 1;
3118
3119 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003120 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003121 if (cols < 0)
3122 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003123 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003124 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003125 if (lines < 0)
3126 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003127 for (line = lines; line < linee
3128 && line + wp->w_winrow < screen_Rows; ++line)
3129 for (col = cols; col < cole
3130 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003131 popup_transparent[(line + wp->w_winrow) * screen_Columns
3132 + col + wp->w_wincol] = val;
3133 }
3134 }
3135}
3136
3137/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003138 * Only called when popup window "wp" is hidden: If the window is positioned
3139 * next to a text property, and it is now visible, then unhide the popup.
3140 * We don't check if visible popups become hidden, that is done in
3141 * popup_adjust_position().
3142 * Return TRUE if the popup became unhidden.
3143 */
3144 static int
3145check_popup_unhidden(win_T *wp)
3146{
3147 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3148 {
3149 textprop_T prop;
3150 linenr_T lnum;
3151
3152 if (find_visible_prop(wp->w_popup_prop_win,
3153 wp->w_popup_prop_type, wp->w_popup_prop_id,
3154 &prop, &lnum) == OK)
3155 {
3156 wp->w_popup_flags &= ~POPF_HIDDEN;
3157 ++wp->w_buffer->b_nwindows;
3158 wp->w_popup_prop_topline = 0; // force repositioning
3159 return TRUE;
3160 }
3161 }
3162 return FALSE;
3163}
3164
3165/*
3166 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3167 * That is when the buffer in the popup was changed, or the popup is following
3168 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003169 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003170 */
3171 static int
3172popup_need_position_adjust(win_T *wp)
3173{
3174 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3175 return TRUE;
3176 if (win_valid(wp->w_popup_prop_win))
3177 return wp->w_popup_prop_changedtick
3178 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003179 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3180 || ((wp->w_popup_flags & POPF_CURSORLINE)
3181 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003182 return FALSE;
3183}
3184
3185/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003186 * Update "popup_mask" if needed.
3187 * Also recomputes the popup size and positions.
3188 * Also updates "popup_visible".
3189 * Also marks window lines for redrawing.
3190 */
3191 void
3192may_update_popup_mask(int type)
3193{
3194 win_T *wp;
3195 short *mask;
3196 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003197 int redraw_all_popups = FALSE;
3198 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003199
3200 // Need to recompute when switching tabs.
3201 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3202 // (such as the screen size) must have changed.
3203 if (popup_mask_tab != curtab || type >= NOT_VALID)
3204 {
3205 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003206 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003207 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003208
3209 // Check if any popup window buffer has changed and if any popup connected
3210 // to a text property has become visible.
3211 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3212 if (wp->w_popup_flags & POPF_HIDDEN)
3213 popup_mask_refresh |= check_popup_unhidden(wp);
3214 else if (popup_need_position_adjust(wp))
3215 popup_mask_refresh = TRUE;
3216 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3217 if (wp->w_popup_flags & POPF_HIDDEN)
3218 popup_mask_refresh |= check_popup_unhidden(wp);
3219 else if (popup_need_position_adjust(wp))
3220 popup_mask_refresh = TRUE;
3221
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003222 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003223 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003224
3225 // Need to update the mask, something has changed.
3226 popup_mask_refresh = FALSE;
3227 popup_mask_tab = curtab;
3228 popup_visible = FALSE;
3229
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003230 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003231 // If redrawing only what is needed, update "popup_mask_next" and then
3232 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003233 redrawing_all_win = TRUE;
3234 FOR_ALL_WINDOWS(wp)
3235 if (wp->w_redr_type < SOME_VALID)
3236 redrawing_all_win = FALSE;
3237 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003238 mask = popup_mask;
3239 else
3240 mask = popup_mask_next;
3241 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3242
3243 // Find the window with the lowest zindex that hasn't been handled yet,
3244 // so that the window with a higher zindex overwrites the value in
3245 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003246 popup_reset_handled(POPUP_HANDLED_4);
3247 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003248 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003249 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003250 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003251
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003252 popup_visible = TRUE;
3253
Bram Moolenaar12034e22019-08-25 22:25:02 +02003254 // Recompute the position if the text changed. It may make the popup
3255 // hidden if it's attach to a text property that is no longer visible.
3256 if (redraw_all_popups || popup_need_position_adjust(wp))
3257 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003258 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003259 if (wp->w_popup_flags & POPF_HIDDEN)
3260 continue;
3261 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003262
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003263 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003264 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003265 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003266 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003267 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003268 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003269 col < wp->w_wincol + width - wp->w_popup_leftoff
3270 && col < screen_Columns; ++col)
3271 if (wp->w_popup_mask_cells == NULL
3272 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003273 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003274 }
3275
3276 // Only check which lines are to be updated if not already
3277 // updating all lines.
3278 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003279 {
3280 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3281 win_T *prev_wp = NULL;
3282
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003283 for (line = 0; line < screen_Rows; ++line)
3284 {
3285 int col_done = 0;
3286
3287 for (col = 0; col < screen_Columns; ++col)
3288 {
3289 int off = line * screen_Columns + col;
3290
3291 if (popup_mask[off] != popup_mask_next[off])
3292 {
3293 popup_mask[off] = popup_mask_next[off];
3294
3295 if (line >= cmdline_row)
3296 {
3297 // the command line needs to be cleared if text below
3298 // the popup is now visible.
3299 if (!msg_scrolled && popup_mask_next[off] == 0)
3300 clear_cmdline = TRUE;
3301 }
3302 else if (col >= col_done)
3303 {
3304 linenr_T lnum;
3305 int line_cp = line;
3306 int col_cp = col;
3307
3308 // The screen position "line" / "col" needs to be
3309 // redrawn. Figure out what window that is and update
3310 // w_redraw_top and w_redr_bot. Only needs to be done
3311 // once for each window line.
3312 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3313 if (wp != NULL)
3314 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003315 if (wp != prev_wp)
3316 {
3317 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3318 prev_wp = wp;
3319 }
3320
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003321 if (line_cp >= wp->w_height)
3322 // In (or below) status line
3323 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003324 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003325 {
3326 // compute the position in the buffer line from
3327 // the position in the window
3328 mouse_comp_pos(wp, &line_cp, &col_cp,
3329 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003330 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003331 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003332
3333 // This line is going to be redrawn, no need to
3334 // check until the right side of the window.
3335 col_done = wp->w_wincol + wp->w_width - 1;
3336 }
3337 }
3338 }
3339 }
3340 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003341
3342 vim_free(plines_cache);
3343 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003344}
3345
3346/*
3347 * Return a string of "len" spaces in IObuff.
3348 */
3349 static char_u *
3350get_spaces(int len)
3351{
3352 vim_memset(IObuff, ' ', (size_t)len);
3353 IObuff[len] = NUL;
3354 return IObuff;
3355}
3356
3357/*
3358 * Update popup windows. They are drawn on top of normal windows.
3359 * "win_update" is called for each popup window, lowest zindex first.
3360 */
3361 void
3362update_popups(void (*win_update)(win_T *wp))
3363{
3364 win_T *wp;
3365 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003366 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003367 int total_width;
3368 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003369 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003370 int popup_attr;
3371 int border_attr[4];
3372 int border_char[8];
3373 char_u buf[MB_MAXBYTES];
3374 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003375 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003376 int padcol = 0;
3377 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003378 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003379 int sb_thumb_top = 0;
3380 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003381 int attr_scroll = 0;
3382 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003383
3384 // Find the window with the lowest zindex that hasn't been updated yet,
3385 // so that the window with a higher zindex is drawn later, thus goes on
3386 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003387 popup_reset_handled(POPUP_HANDLED_5);
3388 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003389 {
3390 // This drawing uses the zindex of the popup window, so that it's on
3391 // top of the text but doesn't draw when another popup with higher
3392 // zindex is on top of the character.
3393 screen_zindex = wp->w_zindex;
3394
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003395 // Set flags in popup_transparent[] for masked cells.
3396 update_popup_transparent(wp, 1);
3397
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003398 // adjust w_winrow and w_wincol for border and padding, since
3399 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003400 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003401 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3402 - wp->w_popup_leftoff;
3403 if (wp->w_wincol + left_extra < 0)
3404 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003405 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003406 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003407
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003408 // Draw the popup text, unless it's off screen.
3409 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003410 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003411 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003412
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003413 // move the cursor into the visible lines, otherwise executing
3414 // commands with win_execute() may cause the text to jump.
3415 if (wp->w_cursor.lnum < wp->w_topline)
3416 wp->w_cursor.lnum = wp->w_topline;
3417 else if (wp->w_cursor.lnum >= wp->w_botline)
3418 wp->w_cursor.lnum = wp->w_botline - 1;
3419 }
3420
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003421 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003422 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003423
Bram Moolenaard529ba52019-07-02 23:13:53 +02003424 total_width = popup_width(wp);
3425 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003426 popup_attr = get_wcr_attr(wp);
3427
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003428 if (wp->w_winrow + total_height > cmdline_row)
3429 wp->w_popup_flags |= POPF_ON_CMDLINE;
3430 else
3431 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3432
3433
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003434 // We can only use these line drawing characters when 'encoding' is
3435 // "utf-8" and 'ambiwidth' is "single".
3436 if (enc_utf8 && *p_ambw == 's')
3437 {
3438 border_char[0] = border_char[2] = 0x2550;
3439 border_char[1] = border_char[3] = 0x2551;
3440 border_char[4] = 0x2554;
3441 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003442 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3443 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003444 border_char[7] = 0x255a;
3445 }
3446 else
3447 {
3448 border_char[0] = border_char[2] = '-';
3449 border_char[1] = border_char[3] = '|';
3450 for (i = 4; i < 8; ++i)
3451 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003452 if (wp->w_popup_flags & POPF_RESIZE)
3453 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003454 }
3455 for (i = 0; i < 8; ++i)
3456 if (wp->w_border_char[i] != 0)
3457 border_char[i] = wp->w_border_char[i];
3458
3459 for (i = 0; i < 4; ++i)
3460 {
3461 border_attr[i] = popup_attr;
3462 if (wp->w_border_highlight[i] != NULL)
3463 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3464 }
3465
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003466 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003467 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003468 if (wp->w_popup_border[0] > 0)
3469 {
3470 // top border
3471 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003472 wincol < 0 ? 0 : wincol, wincol + total_width,
3473 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003474 ? border_char[4] : border_char[0],
3475 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003476 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003477 {
3478 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3479 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003480 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003481 }
3482 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003483 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3484 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003485
Bram Moolenaard529ba52019-07-02 23:13:53 +02003486 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3487 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003488 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003489 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3490 - wp->w_has_scrollbar;
3491 if (padcol < 0)
3492 {
3493 padwidth += padcol;
3494 padcol = 0;
3495 }
3496 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003497 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003498 {
3499 // top padding
3500 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003501 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003502 ' ', ' ', popup_attr);
3503 }
3504
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003505 // Title goes on top of border or padding.
3506 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003507 {
3508 int len = (int)STRLEN(wp->w_popup_title) + 1;
3509 char_u *title = alloc(len);
3510
3511 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3512 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003513 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003514 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003515 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003516
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003517 // Compute scrollbar thumb position and size.
3518 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003519 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003520 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3521 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003522
Bram Moolenaar076d9882019-09-14 22:23:29 +02003523 sb_thumb_height = (height * height + linecount / 2) / linecount;
3524 if (wp->w_topline > 1 && sb_thumb_height == height)
3525 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003526 if (sb_thumb_height == 0)
3527 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003528 if (linecount <= wp->w_height)
3529 // it just fits, avoid divide by zero
3530 sb_thumb_top = 0;
3531 else
3532 sb_thumb_top = (wp->w_topline - 1
3533 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003534 * (wp->w_height - sb_thumb_height)
3535 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003536 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3537 sb_thumb_top = 1; // show it's scrolled
3538
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003539 if (wp->w_scrollbar_highlight != NULL)
3540 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3541 else
3542 attr_scroll = highlight_attr[HLF_PSB];
3543 if (wp->w_thumb_highlight != NULL)
3544 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3545 else
3546 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003547 }
3548
3549 for (i = wp->w_popup_border[0];
3550 i < total_height - wp->w_popup_border[2]; ++i)
3551 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003552 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003553 // left and right padding only needed next to the body
3554 int do_padding =
3555 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3556 && i < total_height - wp->w_popup_border[2]
3557 - wp->w_popup_padding[2];
3558
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003559 row = wp->w_winrow + i;
3560
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003561 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003562 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003563 {
3564 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003565 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003566 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003567 if (do_padding && wp->w_popup_padding[3] > 0)
3568 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003569 int col = wincol + wp->w_popup_border[3];
3570
Bram Moolenaard529ba52019-07-02 23:13:53 +02003571 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003572 pad_left = wp->w_popup_padding[3];
3573 if (col < 0)
3574 {
3575 pad_left += col;
3576 col = 0;
3577 }
3578 if (pad_left > 0)
3579 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3580 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003581 // scrollbar
3582 if (wp->w_has_scrollbar)
3583 {
3584 int line = i - top_off;
3585 int scroll_col = wp->w_wincol + total_width - 1
3586 - wp->w_popup_border[1];
3587
3588 if (line >= 0 && line < wp->w_height)
3589 screen_putchar(' ', row, scroll_col,
3590 line >= sb_thumb_top
3591 && line < sb_thumb_top + sb_thumb_height
3592 ? attr_thumb : attr_scroll);
3593 else
3594 screen_putchar(' ', row, scroll_col, popup_attr);
3595 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003596 // right border
3597 if (wp->w_popup_border[1] > 0)
3598 {
3599 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003600 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003601 }
3602 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003603 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003604 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003605 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003606 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3607 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003608 }
3609
3610 if (wp->w_popup_padding[2] > 0)
3611 {
3612 // bottom padding
3613 row = wp->w_winrow + wp->w_popup_border[0]
3614 + wp->w_popup_padding[0] + wp->w_height;
3615 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003616 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003617 }
3618
3619 if (wp->w_popup_border[2] > 0)
3620 {
3621 // bottom border
3622 row = wp->w_winrow + total_height - 1;
3623 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003624 wincol < 0 ? 0 : wincol,
3625 wincol + total_width,
3626 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003627 ? border_char[7] : border_char[2],
3628 border_char[2], border_attr[2]);
3629 if (wp->w_popup_border[1] > 0)
3630 {
3631 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003632 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003633 }
3634 }
3635
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003636 if (wp->w_popup_close == POPCLOSE_BUTTON)
3637 {
3638 // close button goes on top of anything at the top-right corner
3639 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003640 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003641 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3642 }
3643
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003644 update_popup_transparent(wp, 0);
3645
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003646 // Back to the normal zindex.
3647 screen_zindex = 0;
3648 }
3649}
3650
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003651/*
3652 * Mark references in callbacks of one popup window.
3653 */
3654 static int
3655set_ref_in_one_popup(win_T *wp, int copyID)
3656{
3657 int abort = FALSE;
3658 typval_T tv;
3659
3660 if (wp->w_close_cb.cb_partial != NULL)
3661 {
3662 tv.v_type = VAR_PARTIAL;
3663 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3664 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3665 }
3666 if (wp->w_filter_cb.cb_partial != NULL)
3667 {
3668 tv.v_type = VAR_PARTIAL;
3669 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3670 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3671 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003672 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003673 return abort;
3674}
3675
3676/*
3677 * Set reference in callbacks of popup windows.
3678 */
3679 int
3680set_ref_in_popups(int copyID)
3681{
3682 int abort = FALSE;
3683 win_T *wp;
3684 tabpage_T *tp;
3685
3686 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3687 abort = abort || set_ref_in_one_popup(wp, copyID);
3688
3689 FOR_ALL_TABPAGES(tp)
3690 {
3691 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3692 abort = abort || set_ref_in_one_popup(wp, copyID);
3693 if (abort)
3694 break;
3695 }
3696 return abort;
3697}
Bram Moolenaar79648732019-07-18 21:43:07 +02003698
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003699 int
3700popup_is_popup(win_T *wp)
3701{
3702 return wp->w_popup_flags != 0;
3703}
3704
3705#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003706/*
3707 * Find an existing popup used as the preview window, in the current tab page.
3708 * Return NULL if not found.
3709 */
3710 win_T *
3711popup_find_preview_window(void)
3712{
3713 win_T *wp;
3714
3715 // Preview window popup is always local to tab page.
3716 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3717 if (wp->w_p_pvw)
3718 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003719 return NULL;
3720}
3721
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003722/*
3723 * Find an existing popup used as the info window, in the current tab page.
3724 * Return NULL if not found.
3725 */
3726 win_T *
3727popup_find_info_window(void)
3728{
3729 win_T *wp;
3730
3731 // info window popup is always local to tab page.
3732 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3733 if (wp->w_popup_flags & POPF_INFO)
3734 return wp;
3735 return NULL;
3736}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003737#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003738
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003739 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003740f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3741{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003742#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003743 win_T *wp = popup_find_info_window();
3744
3745 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003746#else
3747 rettv->vval.v_number = 0;
3748#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003749}
3750
3751 void
3752f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003753{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003754#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003755 win_T *wp = popup_find_preview_window();
3756
3757 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003758#else
3759 rettv->vval.v_number = 0;
3760#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003761}
3762
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003763#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003764/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003765 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003766 * NOTE: this makes the popup the current window, so that the file can be
3767 * edited. However, it must not remain to be the current window, the caller
3768 * must make sure of that.
3769 */
3770 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003771popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003772{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003773 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003774
3775 if (wp == NULL)
3776 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003777 if (info)
3778 wp->w_popup_flags |= POPF_INFO;
3779 else
3780 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003781
3782 // Set the width to a reasonable value, so that w_topline can be computed.
3783 if (wp->w_minwidth > 0)
3784 wp->w_width = wp->w_minwidth;
3785 else if (wp->w_maxwidth > 0)
3786 wp->w_width = wp->w_maxwidth;
3787 else
3788 wp->w_width = curwin->w_width;
3789
3790 // Will switch to another buffer soon, dummy one can be wiped.
3791 wp->w_buffer->b_locked = FALSE;
3792
3793 win_enter(wp, FALSE);
3794 return OK;
3795}
3796
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003797/*
3798 * Close any preview popup.
3799 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003800 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003801popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003802{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003803 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003804
3805 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003806 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003807}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003808
3809/*
3810 * Hide the info popup.
3811 */
3812 void
3813popup_hide_info(void)
3814{
3815 win_T *wp = popup_find_info_window();
3816
3817 if (wp != NULL)
3818 popup_hide(wp);
3819}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003820#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003821
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003822/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003823 * Close any popup for a text property associated with "win".
3824 * Return TRUE if a popup was closed.
3825 */
3826 int
3827popup_win_closed(win_T *win)
3828{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003829 int round;
3830 win_T *wp;
3831 win_T *next;
3832 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003833
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003834 for (round = 1; round <= 2; ++round)
3835 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3836 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003837 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003838 next = wp->w_next;
3839 if (wp->w_popup_prop_win == win)
3840 {
3841 popup_close_with_retval(wp, -1);
3842 ret = TRUE;
3843 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003844 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003845 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003846}
3847
3848/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003849 * Set the title of the popup window to the file name.
3850 */
3851 void
3852popup_set_title(win_T *wp)
3853{
3854 if (wp->w_buffer->b_fname != NULL)
3855 {
3856 char_u dirname[MAXPATHL];
3857 size_t len;
3858
3859 mch_dirname(dirname, MAXPATHL);
3860 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3861
3862 vim_free(wp->w_popup_title);
3863 len = STRLEN(wp->w_buffer->b_fname) + 3;
3864 wp->w_popup_title = alloc((int)len);
3865 if (wp->w_popup_title != NULL)
3866 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3867 wp->w_buffer->b_fname);
3868 redraw_win_later(wp, VALID);
3869 }
3870}
3871
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003872# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003873/*
3874 * If there is a preview window, update the title.
3875 * Used after changing directory.
3876 */
3877 void
3878popup_update_preview_title(void)
3879{
3880 win_T *wp = popup_find_preview_window();
3881
3882 if (wp != NULL)
3883 popup_set_title(wp);
3884}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003885# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003886
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003887#endif // FEAT_PROP_POPUP