blob: 47e9238bab86c68279871a1eeec9315987205228 [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)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200940 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200941
Bram Moolenaar33796b32019-06-08 16:01:13 +0200942 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200943 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200944}
945
946/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200947 * Add lines to the popup from a list of strings.
948 */
949 static void
950add_popup_strings(buf_T *buf, list_T *l)
951{
952 listitem_T *li;
953 linenr_T lnum = 0;
954 char_u *p;
955
956 for (li = l->lv_first; li != NULL; li = li->li_next)
957 if (li->li_tv.v_type == VAR_STRING)
958 {
959 p = li->li_tv.vval.v_string;
960 ml_append_buf(buf, lnum++,
961 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
962 }
963}
964
965/*
966 * Add lines to the popup from a list of dictionaries.
967 */
968 static void
969add_popup_dicts(buf_T *buf, list_T *l)
970{
971 listitem_T *li;
972 listitem_T *pli;
973 linenr_T lnum = 0;
974 char_u *p;
975 dict_T *dict;
976
977 // first add the text lines
978 for (li = l->lv_first; li != NULL; li = li->li_next)
979 {
980 if (li->li_tv.v_type != VAR_DICT)
981 {
982 emsg(_(e_dictreq));
983 return;
984 }
985 dict = li->li_tv.vval.v_dict;
986 p = dict == NULL ? NULL
987 : dict_get_string(dict, (char_u *)"text", FALSE);
988 ml_append_buf(buf, lnum++,
989 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
990 }
991
992 // add the text properties
993 lnum = 1;
994 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
995 {
996 dictitem_T *di;
997 list_T *plist;
998
999 dict = li->li_tv.vval.v_dict;
1000 di = dict_find(dict, (char_u *)"props", -1);
1001 if (di != NULL)
1002 {
1003 if (di->di_tv.v_type != VAR_LIST)
1004 {
1005 emsg(_(e_listreq));
1006 return;
1007 }
1008 plist = di->di_tv.vval.v_list;
1009 if (plist != NULL)
1010 {
1011 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
1012 {
1013 if (pli->li_tv.v_type != VAR_DICT)
1014 {
1015 emsg(_(e_dictreq));
1016 return;
1017 }
1018 dict = pli->li_tv.vval.v_dict;
1019 if (dict != NULL)
1020 {
1021 int col = dict_get_number(dict, (char_u *)"col");
1022
1023 prop_add_common( lnum, col, dict, buf, NULL);
1024 }
1025 }
1026 }
1027 }
1028 }
1029}
1030
1031/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001032 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1033 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001034 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001035popup_top_extra(win_T *wp)
1036{
1037 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1038
1039 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1040 return 1;
1041 return extra;
1042}
1043
1044/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001045 * Get the padding plus border at the left.
1046 */
1047 int
1048popup_left_extra(win_T *wp)
1049{
1050 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1051}
1052
1053/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001054 * Return the height of popup window "wp", including border and padding.
1055 */
1056 int
1057popup_height(win_T *wp)
1058{
1059 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001060 + popup_top_extra(wp)
1061 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001062}
1063
1064/*
1065 * Return the width of popup window "wp", including border, padding and
1066 * scrollbar.
1067 */
1068 int
1069popup_width(win_T *wp)
1070{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001071 // w_leftcol is how many columns of the core are left of the screen
1072 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001073 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001074 + popup_extra_width(wp)
1075 + wp->w_popup_rightoff;
1076}
1077
1078/*
1079 * Return the extra width of popup window "wp": border, padding and scrollbar.
1080 */
1081 int
1082popup_extra_width(win_T *wp)
1083{
1084 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1085 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1086 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001087}
1088
1089/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001090 * Adjust the position and size of the popup to fit on the screen.
1091 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001092 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001093popup_adjust_position(win_T *wp)
1094{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001095 linenr_T lnum;
1096 int wrapped = 0;
1097 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001098 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001099 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001100 int center_vert = FALSE;
1101 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001102 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001103 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001104 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1105 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1106 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1107 int extra_height = top_extra + bot_extra;
1108 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001109 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001110 int org_winrow = wp->w_winrow;
1111 int org_wincol = wp->w_wincol;
1112 int org_width = wp->w_width;
1113 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001114 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001115 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001116 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001117 int wantline = wp->w_wantline; // adjusted for textprop
1118 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001119 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001120
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001121 wp->w_winrow = 0;
1122 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001123 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001124 wp->w_popup_leftoff = 0;
1125 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001126
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001127 // May need to update the "cursorline" highlighting, which may also change
1128 // "topline"
1129 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1130 popup_highlight_curline(wp);
1131
Bram Moolenaar12034e22019-08-25 22:25:02 +02001132 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1133 {
1134 win_T *prop_win = wp->w_popup_prop_win;
1135 textprop_T prop;
1136 linenr_T prop_lnum;
1137 pos_T pos;
1138 int screen_row;
1139 int screen_scol;
1140 int screen_ccol;
1141 int screen_ecol;
1142
1143 // Popup window is positioned relative to a text property.
1144 if (find_visible_prop(prop_win,
1145 wp->w_popup_prop_type, wp->w_popup_prop_id,
1146 &prop, &prop_lnum) == FAIL)
1147 {
1148 // Text property is no longer visible, hide the popup.
1149 // Unhiding the popup is done in check_popup_unhidden().
1150 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1151 {
1152 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001153 if (win_valid(wp->w_popup_prop_win))
1154 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1155 }
1156 return;
1157 }
1158
1159 // Compute the desired position from the position of the text
1160 // property. Use "wantline" and "wantcol" as offsets.
1161 pos.lnum = prop_lnum;
1162 pos.col = prop.tp_col;
1163 if (wp->w_popup_pos == POPPOS_TOPLEFT
1164 || wp->w_popup_pos == POPPOS_BOTLEFT)
1165 pos.col += prop.tp_len - 1;
1166 textpos2screenpos(prop_win, &pos, &screen_row,
1167 &screen_scol, &screen_ccol, &screen_ecol);
1168
1169 if (wp->w_popup_pos == POPPOS_TOPLEFT
1170 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1171 // below the text
1172 wantline = screen_row + wantline + 1;
1173 else
1174 // above the text
1175 wantline = screen_row + wantline - 1;
1176 center_vert = FALSE;
1177 if (wp->w_popup_pos == POPPOS_TOPLEFT
1178 || wp->w_popup_pos == POPPOS_BOTLEFT)
1179 // right of the text
1180 wantcol = screen_ecol + wantcol;
1181 else
1182 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001183 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001184 use_wantcol = TRUE;
1185 }
1186 else
1187 {
1188 // If no line was specified default to vertical centering.
1189 if (wantline == 0)
1190 center_vert = TRUE;
1191 else if (wantline < 0)
1192 // If "wantline" is negative it actually means zero.
1193 wantline = 0;
1194 if (wantcol < 0)
1195 // If "wantcol" is negative it actually means zero.
1196 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001197 }
1198
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001199 if (wp->w_popup_pos == POPPOS_CENTER)
1200 {
1201 // center after computing the size
1202 center_vert = TRUE;
1203 center_hor = TRUE;
1204 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001205 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001206 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001207 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1208 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001209 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001210 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001211 if (wp->w_winrow >= Rows)
1212 wp->w_winrow = Rows - 1;
1213 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001214
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001215 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001216 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001217 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1218 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001219 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001220 wp->w_wincol = wantcol - 1;
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001221 if (wp->w_wincol >= Columns - 1)
1222 wp->w_wincol = Columns - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001223 }
1224 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001225
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001226 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001227 // When left aligned use the space available, but shift to the left when we
1228 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001229 maxspace = Columns - wp->w_wincol - left_extra;
1230 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001231 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001232 {
1233 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001234 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001235 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001236 minwidth = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001237
Bram Moolenaar8d241042019-06-12 23:40:01 +02001238 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001239 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001240 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001241 if (wp->w_topline < 1)
1242 wp->w_topline = 1;
1243 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001244 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1245
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001246 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001247 // Use a minimum width of one, so that something shows when there is no
1248 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001249 // When "firstline" is -1 then start with the last buffer line and go
1250 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001251 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001252 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001253 if (wp->w_firstline < 0)
1254 lnum = wp->w_buffer->b_ml.ml_line_count;
1255 else
1256 lnum = wp->w_topline;
1257 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001258 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001259 int len;
1260 int w_width = wp->w_width;
1261
1262 // Count Tabs for what they are worth and compute the length based on
1263 // the maximum width (matters when 'showbreak' is set).
1264 if (wp->w_width < maxwidth)
1265 wp->w_width = maxwidth;
1266 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001267 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001268 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001269
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001270 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001271 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001272 while (len > maxwidth)
1273 {
1274 ++wrapped;
1275 len -= maxwidth;
1276 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001277 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001278 }
1279 }
1280 else if (len > maxwidth
1281 && allow_adjust_left
1282 && (wp->w_popup_pos == POPPOS_TOPLEFT
1283 || wp->w_popup_pos == POPPOS_BOTLEFT))
1284 {
1285 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001286 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001287
Bram Moolenaar51c31312019-06-15 22:27:23 +02001288 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001289 {
1290 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001291
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001292 len -= truncate_shift;
1293 shift_by -= truncate_shift;
1294 }
1295
1296 wp->w_wincol -= shift_by;
1297 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001298 wp->w_width = maxwidth;
1299 }
1300 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001301 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001302 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001303 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1304 wp->w_width = wp->w_maxwidth;
1305 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001306
1307 if (wp->w_firstline < 0)
1308 --lnum;
1309 else
1310 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001311
1312 // do not use the width of lines we're not going to show
1313 if (wp->w_maxheight > 0
1314 && (wp->w_firstline >= 0
1315 ? lnum - wp->w_topline
1316 : wp->w_buffer->b_ml.ml_line_count - lnum)
1317 + wrapped >= wp->w_maxheight)
1318 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001319 }
1320
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001321 if (wp->w_firstline < 0)
1322 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1323
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001324 wp->w_has_scrollbar = wp->w_want_scrollbar
1325 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001326 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001327 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001328 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001329 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001330 // make space for the scrollbar if needed, when lines wrap and when
1331 // applying minwidth
1332 if (maxwidth + right_extra >= maxspace
1333 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1334 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001335 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001336
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001337 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1338 {
1339 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1340
1341 if (minwidth < title_len)
1342 minwidth = title_len;
1343 }
1344
1345 if (minwidth > 0 && wp->w_width < minwidth)
1346 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001347 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001348 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001349 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001350 // some columns cut off on the right
1351 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001352 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001353 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001354 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001355 {
1356 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1357 if (wp->w_wincol < 0)
1358 wp->w_wincol = 0;
1359 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001360 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1361 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1362 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001363 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001364
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001365 // Right aligned: move to the right if needed.
1366 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001367 if (leftoff >= 0)
1368 wp->w_wincol = leftoff;
1369 else if (wp->w_popup_fixed)
1370 {
1371 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001372 // columns when displaying the window, minus left border and
1373 // padding.
1374 if (-leftoff > left_extra)
1375 wp->w_leftcol = -leftoff - left_extra;
1376 wp->w_width -= wp->w_leftcol;
1377 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001378 if (wp->w_width < 0)
1379 wp->w_width = 0;
1380 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001381 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001382
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001383 if (wp->w_p_wrap || (!wp->w_popup_fixed
1384 && (wp->w_popup_pos == POPPOS_TOPLEFT
1385 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001386 {
1387 int want_col = 0;
1388
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001389 // try to show the right border and any scrollbar
1390 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001391 if (want_col > 0 && wp->w_wincol > 0
1392 && wp->w_wincol + want_col >= Columns)
1393 {
1394 wp->w_wincol = Columns - want_col;
1395 if (wp->w_wincol < 0)
1396 wp->w_wincol = 0;
1397 }
1398 }
1399
Bram Moolenaar8d241042019-06-12 23:40:01 +02001400 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1401 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001402 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1403 wp->w_height = wp->w_minheight;
1404 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1405 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001406 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001407 if (wp->w_height > Rows - wp->w_winrow)
1408 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001409 if (wp->w_height != org_height)
1410 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001411
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001412 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001413 {
1414 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1415 if (wp->w_winrow < 0)
1416 wp->w_winrow = 0;
1417 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001418 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001419 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001420 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001421 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001422 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001423 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001424 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1425 {
1426 // Bottom aligned but does not fit, and less space on the other
1427 // side or "posinvert" is off: reduce height.
1428 wp->w_winrow = 0;
1429 wp->w_height = wantline - extra_height;
1430 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001431 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001432 // Not enough space and more space on the other side: make top
1433 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001434 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001435 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001436 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1437 || wp->w_popup_pos == POPPOS_TOPLEFT)
1438 {
1439 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1440 && wantline * 2 > Rows
1441 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001442 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001443 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001444 // make bottom aligned and recompute the height
1445 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001446 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001447 if (wp->w_winrow < 0)
1448 {
1449 wp->w_height += wp->w_winrow;
1450 wp->w_winrow = 0;
1451 }
1452 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001453 else
1454 wp->w_winrow = wantline - 1;
1455 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001456 if (wp->w_winrow >= Rows)
1457 wp->w_winrow = Rows - 1;
1458 else if (wp->w_winrow < 0)
1459 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001460
Bram Moolenaar17146962019-05-30 00:12:11 +02001461 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001462 if (win_valid(wp->w_popup_prop_win))
1463 {
1464 wp->w_popup_prop_changedtick =
1465 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1466 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1467 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001468
1469 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001470 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001471 if (org_winrow != wp->w_winrow
1472 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001473 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001474 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001475 || org_width != wp->w_width
1476 || org_height != wp->w_height)
1477 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001478 redraw_win_later(wp, NOT_VALID);
1479 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1480 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001481 popup_mask_refresh = TRUE;
1482 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001483}
1484
Bram Moolenaar17627312019-06-02 19:53:44 +02001485typedef enum
1486{
1487 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001488 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001489 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001490 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001491 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001492 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001493 TYPE_PREVIEW, // preview window
1494 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001495} create_type_T;
1496
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001497/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001498 * Make "buf" empty and set the contents to "text".
1499 * Used by popup_create() and popup_settext().
1500 */
1501 static void
1502popup_set_buffer_text(buf_T *buf, typval_T text)
1503{
1504 int lnum;
1505
1506 // Clear the buffer, then replace the lines.
1507 curbuf = buf;
1508 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1509 ml_delete(lnum, FALSE);
1510 curbuf = curwin->w_buffer;
1511
1512 // Add text to the buffer.
1513 if (text.v_type == VAR_STRING)
1514 {
1515 // just a string
1516 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1517 }
1518 else
1519 {
1520 list_T *l = text.vval.v_list;
1521
1522 if (l->lv_len > 0)
1523 {
1524 if (l->lv_first->li_tv.v_type == VAR_STRING)
1525 // list of strings
1526 add_popup_strings(buf, l);
1527 else
1528 // list of dictionaries
1529 add_popup_dicts(buf, l);
1530 }
1531 }
1532
1533 // delete the line that was in the empty buffer
1534 curbuf = buf;
1535 ml_delete(buf->b_ml.ml_line_count, FALSE);
1536 curbuf = curwin->w_buffer;
1537}
1538
1539/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001540 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1541 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001542 * Return FAIL if the parsing fails.
1543 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001544 static int
1545parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001546{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001547 char_u *p =
1548#ifdef FEAT_QUICKFIX
1549 !is_preview ? p_cpp :
1550#endif
1551 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001552
Bram Moolenaar258cef52019-08-21 17:29:29 +02001553 if (wp != NULL)
1554 wp->w_popup_flags &= ~POPF_INFO_MENU;
1555
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001556 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001557 {
1558 char_u *e, *dig;
1559 char_u *s = p;
1560 int x;
1561
1562 e = vim_strchr(p, ':');
1563 if (e == NULL || e[1] == NUL)
1564 return FAIL;
1565
1566 p = vim_strchr(e, ',');
1567 if (p == NULL)
1568 p = e + STRLEN(e);
1569 dig = e + 1;
1570 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001571
1572 if (STRNCMP(s, "height:", 7) == 0)
1573 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001574 if (dig != p)
1575 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001576 if (wp != NULL)
1577 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001578 if (is_preview)
1579 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001580 wp->w_maxheight = x;
1581 }
1582 }
1583 else if (STRNCMP(s, "width:", 6) == 0)
1584 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001585 if (dig != p)
1586 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001587 if (wp != NULL)
1588 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001589 if (is_preview)
1590 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001591 wp->w_maxwidth = x;
1592 }
1593 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001594 else if (STRNCMP(s, "highlight:", 10) == 0)
1595 {
1596 if (wp != NULL)
1597 {
1598 int c = *p;
1599
1600 *p = NUL;
1601 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1602 s + 10, OPT_FREE|OPT_LOCAL, 0);
1603 *p = c;
1604 }
1605 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001606 else if (STRNCMP(s, "border:", 7) == 0)
1607 {
1608 char_u *arg = s + 7;
1609 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1610 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1611 int i;
1612
1613 if (!on && !off)
1614 return FAIL;
1615 if (wp != NULL)
1616 {
1617 for (i = 0; i < 4; ++i)
1618 wp->w_popup_border[i] = on ? 1 : 0;
1619 if (off)
1620 // only show the X for close when there is a border
1621 wp->w_popup_close = POPCLOSE_NONE;
1622 }
1623 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001624 else if (STRNCMP(s, "align:", 6) == 0)
1625 {
1626 char_u *arg = s + 6;
1627 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1628 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1629
1630 if (!menu && !item)
1631 return FAIL;
1632 if (wp != NULL && menu)
1633 wp->w_popup_flags |= POPF_INFO_MENU;
1634 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001635 else
1636 return FAIL;
1637 }
1638 return OK;
1639}
1640
1641/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001642 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1643 * is not NULL.
1644 * Return FAIL if the parsing fails.
1645 */
1646 int
1647parse_previewpopup(win_T *wp)
1648{
1649 return parse_popup_option(wp, TRUE);
1650}
1651
1652/*
1653 * Parse the 'completepopup' option and apply the values to window "wp" if it
1654 * is not NULL.
1655 * Return FAIL if the parsing fails.
1656 */
1657 int
1658parse_completepopup(win_T *wp)
1659{
1660 return parse_popup_option(wp, FALSE);
1661}
1662
1663/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001664 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001665 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 */
1667 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001668popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001669{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001670 poppos_T ppt = POPPOS_NONE;
1671
1672 if (d != NULL)
1673 ppt = get_pos_entry(d, FALSE);
1674
Bram Moolenaar79648732019-07-18 21:43:07 +02001675 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001676 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001677 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001678 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1679 }
1680 else
1681 {
1682 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1683 if (wp->w_wantline == 0) // cursor in first line
1684 {
1685 wp->w_wantline = 2;
1686 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1687 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1688 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001689 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001690
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001692 if (wp->w_wantcol > Columns - width)
1693 {
1694 wp->w_wantcol = Columns - width;
1695 if (wp->w_wantcol < 1)
1696 wp->w_wantcol = 1;
1697 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001698
Bram Moolenaar79648732019-07-18 21:43:07 +02001699 popup_adjust_position(wp);
1700}
1701
1702/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001703 * Set w_wantline and w_wantcol for the a given screen position.
1704 * Caller must take care of running into the window border.
1705 */
1706 void
1707popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1708{
1709 wp->w_wantline = row;
1710 wp->w_wantcol = col;
1711 popup_adjust_position(wp);
1712}
1713
1714/*
1715 * Add a border and lef&right padding.
1716 */
1717 static void
1718add_border_left_right_padding(win_T *wp)
1719{
1720 int i;
1721
1722 for (i = 0; i < 4; ++i)
1723 {
1724 wp->w_popup_border[i] = 1;
1725 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1726 }
1727}
1728
1729/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001730 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001731 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001732 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001733 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001735 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001736popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001737{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001738 win_T *wp;
1739 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001741 int new_buffer;
1742 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001743 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001744 int nr;
1745 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001746
Bram Moolenaar79648732019-07-18 21:43:07 +02001747 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001748 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001749 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001750 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001751 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001752 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001753 if (buf == NULL)
1754 {
1755 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1756 return NULL;
1757 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001758#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001759 if (buf->b_term != NULL)
1760 {
1761 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1762 return NULL;
1763 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001764#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001765 }
1766 else if (!(argvars[0].v_type == VAR_STRING
1767 && argvars[0].vval.v_string != NULL)
1768 && !(argvars[0].v_type == VAR_LIST
1769 && argvars[0].vval.v_list != NULL))
1770 {
1771 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001772 return NULL;
1773 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001774 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001775 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001777 return NULL;
1778 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001779 d = argvars[1].vval.v_dict;
1780 }
1781
1782 if (d != NULL)
1783 {
1784 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1785 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1786 else if (type == TYPE_NOTIFICATION)
1787 tabnr = -1; // notifications are global by default
1788 else
1789 tabnr = 0;
1790 if (tabnr > 0)
1791 {
1792 tp = find_tabpage(tabnr);
1793 if (tp == NULL)
1794 {
1795 semsg(_("E997: Tabpage not found: %d"), tabnr);
1796 return NULL;
1797 }
1798 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001799 }
1800
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001801 // Create the window and buffer.
1802 wp = win_alloc_popup_win();
1803 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001804 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001805 if (rettv != NULL)
1806 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001807 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001808 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001809
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001810 if (buf != NULL)
1811 {
1812 // use existing buffer
1813 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001814 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001815 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001816 buffer_ensure_loaded(buf);
1817 }
1818 else
1819 {
1820 // create a new buffer associated with the popup
1821 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001822 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001823 if (buf == NULL)
1824 return NULL;
1825 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001826
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001827 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001828
Bram Moolenaar46451042019-08-24 15:50:46 +02001829 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001830 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001831 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001832 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001833 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001834 buf->b_p_ul = -1; // no undo
1835 buf->b_p_swf = FALSE; // no swap file
1836 buf->b_p_bl = FALSE; // unlisted buffer
1837 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001838
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001839 // Avoid that 'buftype' is reset when this buffer is entered.
1840 buf->b_p_initialized = TRUE;
1841 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001842 wp->w_p_wrap = TRUE; // 'wrap' is default on
1843 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001844
Bram Moolenaara3fce622019-06-20 02:31:49 +02001845 if (tp != NULL)
1846 {
1847 // popup on specified tab page
1848 wp->w_next = tp->tp_first_popupwin;
1849 tp->tp_first_popupwin = wp;
1850 }
1851 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001852 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001853 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001854 wp->w_next = curtab->tp_first_popupwin;
1855 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001856 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001857 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001858 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001859 win_T *prev = first_popupwin;
1860
1861 // Global popup: add at the end, so that it gets displayed on top of
1862 // older ones with the same zindex. Matters for notifications.
1863 if (first_popupwin == NULL)
1864 first_popupwin = wp;
1865 else
1866 {
1867 while (prev->w_next != NULL)
1868 prev = prev->w_next;
1869 prev->w_next = wp;
1870 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001871 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001872
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001874 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001875
Bram Moolenaar79648732019-07-18 21:43:07 +02001876 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001877 {
1878 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001879 }
1880 if (type == TYPE_ATCURSOR)
1881 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001882 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001883 set_moved_values(wp);
1884 set_moved_columns(wp, FIND_STRING);
1885 }
1886
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001887 if (type == TYPE_BEVAL)
1888 {
1889 wp->w_popup_pos = POPPOS_BOTLEFT;
1890
1891 // by default use the mouse position
1892 wp->w_wantline = mouse_row;
1893 if (wp->w_wantline <= 0) // mouse on first line
1894 {
1895 wp->w_wantline = 2;
1896 wp->w_popup_pos = POPPOS_TOPLEFT;
1897 }
1898 wp->w_wantcol = mouse_col + 1;
1899 set_mousemoved_values(wp);
1900 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1901 }
1902
Bram Moolenaar33796b32019-06-08 16:01:13 +02001903 // set default values
1904 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001905 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001906
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001907 if (type == TYPE_NOTIFICATION)
1908 {
1909 win_T *twp, *nextwin;
1910 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001911
1912 // Try to not overlap with another global popup. Guess we need 3
1913 // more screen lines than buffer lines.
1914 wp->w_wantline = 1;
1915 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1916 {
1917 nextwin = twp->w_next;
1918 if (twp != wp
1919 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1920 && twp->w_winrow <= wp->w_wantline - 1 + height
1921 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1922 {
1923 // move to below this popup and restart the loop to check for
1924 // overlap with other popups
1925 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1926 nextwin = first_popupwin;
1927 }
1928 }
1929 if (wp->w_wantline + height > Rows)
1930 {
1931 // can't avoid overlap, put on top in the hope that message goes
1932 // away soon.
1933 wp->w_wantline = 1;
1934 }
1935
1936 wp->w_wantcol = 10;
1937 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001938 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001939 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001940 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001941 for (i = 0; i < 4; ++i)
1942 wp->w_popup_border[i] = 1;
1943 wp->w_popup_padding[1] = 1;
1944 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001945
1946 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001947 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001948 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1949 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001950 }
1951
Bram Moolenaara730e552019-06-16 19:05:31 +02001952 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001953 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001954 wp->w_popup_pos = POPPOS_CENTER;
1955 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001956 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001957 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001958 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001959 }
1960
Bram Moolenaara730e552019-06-16 19:05:31 +02001961 if (type == TYPE_MENU)
1962 {
1963 typval_T tv;
1964 callback_T callback;
1965
1966 tv.v_type = VAR_STRING;
1967 tv.vval.v_string = (char_u *)"popup_filter_menu";
1968 callback = get_callback(&tv);
1969 if (callback.cb_name != NULL)
1970 set_callback(&wp->w_filter_cb, &callback);
1971
1972 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001973 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001974 }
1975
Bram Moolenaar79648732019-07-18 21:43:07 +02001976 if (type == TYPE_PREVIEW)
1977 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001978 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001979 wp->w_popup_close = POPCLOSE_BUTTON;
1980 for (i = 0; i < 4; ++i)
1981 wp->w_popup_border[i] = 1;
1982 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001983 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001984 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001985# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001986 if (type == TYPE_INFO)
1987 {
1988 wp->w_popup_pos = POPPOS_TOPLEFT;
1989 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1990 wp->w_popup_close = POPCLOSE_BUTTON;
1991 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001992 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001993 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001994# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001995
Bram Moolenaarae943152019-06-16 22:54:14 +02001996 for (i = 0; i < 4; ++i)
1997 VIM_CLEAR(wp->w_border_highlight[i]);
1998 for (i = 0; i < 8; ++i)
1999 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002000 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002001 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002002 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002003
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 if (d != NULL)
2005 // Deal with options.
2006 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002007
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002008#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002009 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2010 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002011#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002012
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002013 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002014
2015 wp->w_vsep_width = 0;
2016
2017 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002018 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002019
2020 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002021}
2022
2023/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002024 * popup_clear()
2025 */
2026 void
2027f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2028{
2029 close_all_popups();
2030}
2031
2032/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002033 * popup_create({text}, {options})
2034 */
2035 void
2036f_popup_create(typval_T *argvars, typval_T *rettv)
2037{
Bram Moolenaar17627312019-06-02 19:53:44 +02002038 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002039}
2040
2041/*
2042 * popup_atcursor({text}, {options})
2043 */
2044 void
2045f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2046{
Bram Moolenaar17627312019-06-02 19:53:44 +02002047 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002048}
2049
2050/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002051 * popup_beval({text}, {options})
2052 */
2053 void
2054f_popup_beval(typval_T *argvars, typval_T *rettv)
2055{
2056 popup_create(argvars, rettv, TYPE_BEVAL);
2057}
2058
2059/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002060 * Invoke the close callback for window "wp" with value "result".
2061 * Careful: The callback may make "wp" invalid!
2062 */
2063 static void
2064invoke_popup_callback(win_T *wp, typval_T *result)
2065{
2066 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002067 typval_T argv[3];
2068
2069 argv[0].v_type = VAR_NUMBER;
2070 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2071
2072 if (result != NULL && result->v_type != VAR_UNKNOWN)
2073 copy_tv(result, &argv[1]);
2074 else
2075 {
2076 argv[1].v_type = VAR_NUMBER;
2077 argv[1].vval.v_number = 0;
2078 }
2079
2080 argv[2].v_type = VAR_UNKNOWN;
2081
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002082 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002083 if (result != NULL)
2084 clear_tv(&argv[1]);
2085 clear_tv(&rettv);
2086}
2087
2088/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002089 * Close popup "wp" and invoke any close callback for it.
2090 */
2091 static void
2092popup_close_and_callback(win_T *wp, typval_T *arg)
2093{
2094 int id = wp->w_id;
2095
Bram Moolenaar49540192019-12-11 19:34:54 +01002096 // Just in case a check higher up is missing.
2097 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2098 return;
2099
Bram Moolenaar3397f742019-06-02 18:40:06 +02002100 if (wp->w_close_cb.cb_name != NULL)
2101 // Careful: This may make "wp" invalid.
2102 invoke_popup_callback(wp, arg);
2103
2104 popup_close(id);
2105}
2106
Bram Moolenaar12034e22019-08-25 22:25:02 +02002107 static void
2108popup_close_with_retval(win_T *wp, int retval)
2109{
2110 typval_T res;
2111
2112 res.v_type = VAR_NUMBER;
2113 res.vval.v_number = retval;
2114 popup_close_and_callback(wp, &res);
2115}
2116
Bram Moolenaar3397f742019-06-02 18:40:06 +02002117/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002118 * Close popup "wp" because of a mouse click.
2119 */
2120 void
2121popup_close_for_mouse_click(win_T *wp)
2122{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002123 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002124}
2125
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002126 static void
2127check_mouse_moved(win_T *wp, win_T *mouse_wp)
2128{
2129 // Close the popup when all if these are true:
2130 // - the mouse is not on this popup
2131 // - "mousemoved" was used
2132 // - the mouse is no longer on the same screen row or the mouse column is
2133 // outside of the relevant text
2134 if (wp != mouse_wp
2135 && wp->w_popup_mouse_row != 0
2136 && (wp->w_popup_mouse_row != mouse_row
2137 || mouse_col < wp->w_popup_mouse_mincol
2138 || mouse_col > wp->w_popup_mouse_maxcol))
2139 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002140 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002141 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002142 }
2143}
2144
2145/*
2146 * Called when the mouse moved: may close a popup with "mousemoved".
2147 */
2148 void
2149popup_handle_mouse_moved(void)
2150{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002151 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002152 win_T *mouse_wp;
2153 int row = mouse_row;
2154 int col = mouse_col;
2155
2156 // find the window where the mouse is in
2157 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2158
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002159 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2160 {
2161 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002162 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002163 }
2164 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2165 {
2166 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002167 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002168 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002169}
2170
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002171/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002172 * In a filter: check if the typed key is a mouse event that is used for
2173 * dragging the popup.
2174 */
2175 static void
2176filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2177{
2178 int row = mouse_row;
2179 int col = mouse_col;
2180
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002181 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002182 && is_mouse_key(c)
2183 && (wp == popup_dragwin
2184 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2185 // do not consume the key, allow for dragging the popup
2186 rettv->vval.v_number = 0;
2187}
2188
Bram Moolenaara730e552019-06-16 19:05:31 +02002189/*
2190 * popup_filter_menu({text}, {options})
2191 */
2192 void
2193f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2194{
2195 int id = tv_get_number(&argvars[0]);
2196 win_T *wp = win_id2wp(id);
2197 char_u *key = tv_get_string(&argvars[1]);
2198 typval_T res;
2199 int c;
2200 linenr_T old_lnum;
2201
2202 // If the popup has been closed do not consume the key.
2203 if (wp == NULL)
2204 return;
2205
2206 c = *key;
2207 if (c == K_SPECIAL && key[1] != NUL)
2208 c = TO_SPECIAL(key[1], key[2]);
2209
2210 // consume all keys until done
2211 rettv->vval.v_number = 1;
2212 res.v_type = VAR_NUMBER;
2213
2214 old_lnum = wp->w_cursor.lnum;
2215 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2216 --wp->w_cursor.lnum;
2217 if ((c == 'j' || c == 'J' || c == K_DOWN)
2218 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2219 ++wp->w_cursor.lnum;
2220 if (old_lnum != wp->w_cursor.lnum)
2221 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002222 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002223 return;
2224 }
2225
2226 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2227 {
2228 // Cancelled, invoke callback with -1
2229 res.vval.v_number = -1;
2230 popup_close_and_callback(wp, &res);
2231 return;
2232 }
2233 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2234 {
2235 // Invoke callback with current index.
2236 res.vval.v_number = wp->w_cursor.lnum;
2237 popup_close_and_callback(wp, &res);
2238 return;
2239 }
2240
2241 filter_handle_drag(wp, c, rettv);
2242}
2243
2244/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002245 * popup_filter_yesno({text}, {options})
2246 */
2247 void
2248f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2249{
2250 int id = tv_get_number(&argvars[0]);
2251 win_T *wp = win_id2wp(id);
2252 char_u *key = tv_get_string(&argvars[1]);
2253 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002254 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002255
2256 // If the popup has been closed don't consume the key.
2257 if (wp == NULL)
2258 return;
2259
Bram Moolenaara730e552019-06-16 19:05:31 +02002260 c = *key;
2261 if (c == K_SPECIAL && key[1] != NUL)
2262 c = TO_SPECIAL(key[1], key[2]);
2263
Bram Moolenaara42d9452019-06-15 21:46:30 +02002264 // consume all keys until done
2265 rettv->vval.v_number = 1;
2266
Bram Moolenaara730e552019-06-16 19:05:31 +02002267 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002268 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002269 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002270 res.vval.v_number = 0;
2271 else
2272 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002273 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002274 return;
2275 }
2276
2277 // Invoke callback
2278 res.v_type = VAR_NUMBER;
2279 popup_close_and_callback(wp, &res);
2280}
2281
2282/*
2283 * popup_dialog({text}, {options})
2284 */
2285 void
2286f_popup_dialog(typval_T *argvars, typval_T *rettv)
2287{
2288 popup_create(argvars, rettv, TYPE_DIALOG);
2289}
2290
2291/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002292 * popup_menu({text}, {options})
2293 */
2294 void
2295f_popup_menu(typval_T *argvars, typval_T *rettv)
2296{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002297 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002298}
2299
2300/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002301 * popup_notification({text}, {options})
2302 */
2303 void
2304f_popup_notification(typval_T *argvars, typval_T *rettv)
2305{
2306 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2307}
2308
2309/*
2310 * Find the popup window with window-ID "id".
2311 * If the popup window does not exist NULL is returned.
2312 * If the window is not a popup window, and error message is given.
2313 */
2314 static win_T *
2315find_popup_win(int id)
2316{
2317 win_T *wp = win_id2wp(id);
2318
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002319 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002320 {
2321 semsg(_("E993: window %d is not a popup window"), id);
2322 return NULL;
2323 }
2324 return wp;
2325}
2326
2327/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002328 * popup_close({id})
2329 */
2330 void
2331f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2332{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002333 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002334 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002335
Bram Moolenaar49540192019-12-11 19:34:54 +01002336 if (ERROR_IF_POPUP_WINDOW)
2337 return;
2338
2339 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002340 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002341 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002342}
2343
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002344 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002345popup_hide(win_T *wp)
2346{
2347 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2348 {
2349 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002350 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002351 redraw_all_later(NOT_VALID);
2352 popup_mask_refresh = TRUE;
2353 }
2354}
2355
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002356/*
2357 * popup_hide({id})
2358 */
2359 void
2360f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2361{
2362 int id = (int)tv_get_number(argvars);
2363 win_T *wp = find_popup_win(id);
2364
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002365 if (wp != NULL)
2366 popup_hide(wp);
2367}
2368
2369 void
2370popup_show(win_T *wp)
2371{
2372 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002373 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002374 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002375 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002376 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002377 }
2378}
2379
2380/*
2381 * popup_show({id})
2382 */
2383 void
2384f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2385{
2386 int id = (int)tv_get_number(argvars);
2387 win_T *wp = find_popup_win(id);
2388
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002389 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002390 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002391 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002392#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002393 if (wp->w_popup_flags & POPF_INFO)
2394 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002395#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002396 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002397}
2398
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002399/*
2400 * popup_settext({id}, {text})
2401 */
2402 void
2403f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2404{
2405 int id = (int)tv_get_number(&argvars[0]);
2406 win_T *wp = find_popup_win(id);
2407
2408 if (wp != NULL)
2409 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002410 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2411 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2412 else
2413 {
2414 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002415 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002416 popup_adjust_position(wp);
2417 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002418 }
2419}
2420
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002421 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002422popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002423{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002424 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002425 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002426 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002427 clear_cmdline = TRUE;
2428 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002429
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002430 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002431 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002432}
2433
Bram Moolenaarec583842019-05-26 14:11:23 +02002434/*
2435 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002436 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002437 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002438 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002439popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002440{
2441 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002442 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002443 win_T *prev = NULL;
2444
Bram Moolenaarec583842019-05-26 14:11:23 +02002445 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002446 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002447 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002448 {
2449 if (prev == NULL)
2450 first_popupwin = wp->w_next;
2451 else
2452 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002453 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002454 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002455 }
2456
Bram Moolenaarec583842019-05-26 14:11:23 +02002457 // go through tab-local popups
2458 FOR_ALL_TABPAGES(tp)
2459 popup_close_tabpage(tp, id);
2460}
2461
2462/*
2463 * Close a popup window with Window-id "id" in tabpage "tp".
2464 */
2465 void
2466popup_close_tabpage(tabpage_T *tp, int id)
2467{
2468 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002469 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002470 win_T *prev = NULL;
2471
Bram Moolenaarec583842019-05-26 14:11:23 +02002472 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2473 if (wp->w_id == id)
2474 {
2475 if (prev == NULL)
2476 *root = wp->w_next;
2477 else
2478 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002479 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002480 return;
2481 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002482}
2483
2484 void
2485close_all_popups(void)
2486{
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002487 if (ERROR_IF_POPUP_WINDOW)
2488 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002489 while (first_popupwin != NULL)
2490 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002491 while (curtab->tp_first_popupwin != NULL)
2492 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002493}
2494
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002495/*
2496 * popup_move({id}, {options})
2497 */
2498 void
2499f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2500{
Bram Moolenaarae943152019-06-16 22:54:14 +02002501 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002502 int id = (int)tv_get_number(argvars);
2503 win_T *wp = find_popup_win(id);
2504
2505 if (wp == NULL)
2506 return; // invalid {id}
2507
2508 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2509 {
2510 emsg(_(e_dictreq));
2511 return;
2512 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002513 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002514
Bram Moolenaarae943152019-06-16 22:54:14 +02002515 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002516
2517 if (wp->w_winrow + wp->w_height >= cmdline_row)
2518 clear_cmdline = TRUE;
2519 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002520}
2521
Bram Moolenaarbc133542019-05-29 20:26:46 +02002522/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002523 * popup_setoptions({id}, {options})
2524 */
2525 void
2526f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2527{
2528 dict_T *dict;
2529 int id = (int)tv_get_number(argvars);
2530 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002531 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002532
2533 if (wp == NULL)
2534 return; // invalid {id}
2535
2536 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2537 {
2538 emsg(_(e_dictreq));
2539 return;
2540 }
2541 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002542 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002543
2544 apply_move_options(wp, dict);
2545 apply_general_options(wp, dict);
2546
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002547 if (old_firstline != wp->w_firstline)
2548 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002549 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002550 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002551 popup_adjust_position(wp);
2552}
2553
2554/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002555 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002556 */
2557 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002558f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002559{
2560 dict_T *dict;
2561 int id = (int)tv_get_number(argvars);
2562 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002563 int top_extra;
2564 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002565
2566 if (rettv_dict_alloc(rettv) == OK)
2567 {
2568 if (wp == NULL)
2569 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002570 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002571 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2572
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002573 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002574 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002575 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002576
Bram Moolenaarbc133542019-05-29 20:26:46 +02002577 dict_add_number(dict, "line", wp->w_winrow + 1);
2578 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002579 dict_add_number(dict, "width", wp->w_width + left_extra
2580 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2581 dict_add_number(dict, "height", wp->w_height + top_extra
2582 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002583
2584 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2585 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2586 dict_add_number(dict, "core_width", wp->w_width);
2587 dict_add_number(dict, "core_height", wp->w_height);
2588
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002589 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002590 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002591 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002592 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002593 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002594
2595 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002596 }
2597}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002598/*
2599 * popup_locate({row}, {col})
2600 */
2601 void
2602f_popup_locate(typval_T *argvars, typval_T *rettv)
2603{
2604 int row = tv_get_number(&argvars[0]) - 1;
2605 int col = tv_get_number(&argvars[1]) - 1;
2606 win_T *wp;
2607
2608 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002609 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002610 rettv->vval.v_number = wp->w_id;
2611}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002612
2613/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002614 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2615 */
2616 static void
2617get_padding_border(dict_T *dict, int *array, char *name)
2618{
2619 list_T *list;
2620 int i;
2621
2622 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2623 return;
2624
2625 list = list_alloc();
2626 if (list != NULL)
2627 {
2628 dict_add_list(dict, name, list);
2629 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2630 for (i = 0; i < 4; ++i)
2631 list_append_number(list, array[i]);
2632 }
2633}
2634
2635/*
2636 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2637 */
2638 static void
2639get_borderhighlight(dict_T *dict, win_T *wp)
2640{
2641 list_T *list;
2642 int i;
2643
2644 for (i = 0; i < 4; ++i)
2645 if (wp->w_border_highlight[i] != NULL)
2646 break;
2647 if (i == 4)
2648 return;
2649
2650 list = list_alloc();
2651 if (list != NULL)
2652 {
2653 dict_add_list(dict, "borderhighlight", list);
2654 for (i = 0; i < 4; ++i)
2655 list_append_string(list, wp->w_border_highlight[i], -1);
2656 }
2657}
2658
2659/*
2660 * For popup_getoptions(): add a "borderchars" entry to "dict".
2661 */
2662 static void
2663get_borderchars(dict_T *dict, win_T *wp)
2664{
2665 list_T *list;
2666 int i;
2667 char_u buf[NUMBUFLEN];
2668 int len;
2669
2670 for (i = 0; i < 8; ++i)
2671 if (wp->w_border_char[i] != 0)
2672 break;
2673 if (i == 8)
2674 return;
2675
2676 list = list_alloc();
2677 if (list != NULL)
2678 {
2679 dict_add_list(dict, "borderchars", list);
2680 for (i = 0; i < 8; ++i)
2681 {
2682 len = mb_char2bytes(wp->w_border_char[i], buf);
2683 list_append_string(list, buf, len);
2684 }
2685 }
2686}
2687
2688/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002689 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002690 */
2691 static void
2692get_moved_list(dict_T *dict, win_T *wp)
2693{
2694 list_T *list;
2695
2696 list = list_alloc();
2697 if (list != NULL)
2698 {
2699 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002700 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002701 list_append_number(list, wp->w_popup_mincol);
2702 list_append_number(list, wp->w_popup_maxcol);
2703 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002704 list = list_alloc();
2705 if (list != NULL)
2706 {
2707 dict_add_list(dict, "mousemoved", list);
2708 list_append_number(list, wp->w_popup_mouse_row);
2709 list_append_number(list, wp->w_popup_mouse_mincol);
2710 list_append_number(list, wp->w_popup_mouse_maxcol);
2711 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002712}
2713
2714/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002715 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002716 */
2717 void
2718f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2719{
2720 dict_T *dict;
2721 int id = (int)tv_get_number(argvars);
2722 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002723 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002724 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002725
2726 if (rettv_dict_alloc(rettv) == OK)
2727 {
2728 if (wp == NULL)
2729 return;
2730
2731 dict = rettv->vval.v_dict;
2732 dict_add_number(dict, "line", wp->w_wantline);
2733 dict_add_number(dict, "col", wp->w_wantcol);
2734 dict_add_number(dict, "minwidth", wp->w_minwidth);
2735 dict_add_number(dict, "minheight", wp->w_minheight);
2736 dict_add_number(dict, "maxheight", wp->w_maxheight);
2737 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002738 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002739 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002740 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002741 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002742 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2743 {
2744 proptype_T *pt = text_prop_type_by_id(
2745 wp->w_popup_prop_win->w_buffer,
2746 wp->w_popup_prop_type);
2747
2748 if (pt != NULL)
2749 dict_add_string(dict, "textprop", pt->pt_name);
2750 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2751 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2752 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002753 dict_add_string(dict, "title", wp->w_popup_title);
2754 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002755 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002756 dict_add_number(dict, "mapping",
2757 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002758 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002759 dict_add_number(dict, "posinvert",
2760 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002761 dict_add_number(dict, "cursorline",
2762 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002763 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002764 if (wp->w_scrollbar_highlight != NULL)
2765 dict_add_string(dict, "scrollbarhighlight",
2766 wp->w_scrollbar_highlight);
2767 if (wp->w_thumb_highlight != NULL)
2768 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002769
Bram Moolenaara3fce622019-06-20 02:31:49 +02002770 // find the tabpage that holds this popup
2771 i = 1;
2772 FOR_ALL_TABPAGES(tp)
2773 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002774 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002775
Bram Moolenaar1824f452019-10-02 23:06:46 +02002776 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2777 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002778 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002779 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002780 break;
2781 ++i;
2782 }
2783 if (tp == NULL)
2784 i = -1; // must be global
2785 else if (tp == curtab)
2786 i = 0;
2787 dict_add_number(dict, "tabpage", i);
2788
Bram Moolenaarae943152019-06-16 22:54:14 +02002789 get_padding_border(dict, wp->w_popup_padding, "padding");
2790 get_padding_border(dict, wp->w_popup_border, "border");
2791 get_borderhighlight(dict, wp);
2792 get_borderchars(dict, wp);
2793 get_moved_list(dict, wp);
2794
2795 if (wp->w_filter_cb.cb_name != NULL)
2796 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2797 if (wp->w_close_cb.cb_name != NULL)
2798 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002799
2800 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2801 ++i)
2802 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2803 {
2804 dict_add_string(dict, "pos",
2805 (char_u *)poppos_entries[i].pp_name);
2806 break;
2807 }
2808
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002809 dict_add_string(dict, "close", (char_u *)(
2810 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2811 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2812
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002813# if defined(FEAT_TIMERS)
2814 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2815 ? (long)wp->w_popup_timer->tr_interval : 0L);
2816# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002817 }
2818}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002819
2820 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002821error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002822{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002823 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002824 {
2825 emsg(_("E994: Not allowed in a popup window"));
2826 return TRUE;
2827 }
2828 return FALSE;
2829}
2830
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002831/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002832 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002833 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002834 * Each calling function should use a different flag, see the list at
2835 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002836 */
2837 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002838popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002839{
2840 win_T *wp;
2841
2842 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002843 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002844 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002845 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002846}
2847
2848/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002849 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002850 * Must have called popup_reset_handled() first.
2851 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2852 * popup with the highest zindex.
2853 */
2854 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002855find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002856{
2857 win_T *wp;
2858 win_T *found_wp;
2859 int found_zindex;
2860
2861 found_zindex = lowest ? INT_MAX : 0;
2862 found_wp = NULL;
2863 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002864 if ((wp->w_popup_handled & handled_flag) == 0
2865 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002866 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002867 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002868 {
2869 found_zindex = wp->w_zindex;
2870 found_wp = wp;
2871 }
2872 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002873 if ((wp->w_popup_handled & handled_flag) == 0
2874 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002875 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002876 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002877 {
2878 found_zindex = wp->w_zindex;
2879 found_wp = wp;
2880 }
2881
2882 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002883 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002884 return found_wp;
2885}
2886
2887/*
2888 * Invoke the filter callback for window "wp" with typed character "c".
2889 * Uses the global "mod_mask" for modifiers.
2890 * Returns the return value of the filter.
2891 * Careful: The filter may make "wp" invalid!
2892 */
2893 static int
2894invoke_popup_filter(win_T *wp, int c)
2895{
2896 int res;
2897 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002898 typval_T argv[3];
2899 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002900 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002901
Bram Moolenaara42d9452019-06-15 21:46:30 +02002902 // Emergency exit: CTRL-C closes the popup.
2903 if (c == Ctrl_C)
2904 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002905 int save_got_int = got_int;
2906
2907 // Reset got_int to avoid the callback isn't called.
2908 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002909 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002910 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002911 return 1;
2912 }
2913
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002914 argv[0].v_type = VAR_NUMBER;
2915 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2916
2917 // Convert the number to a string, so that the function can use:
2918 // if a:c == "\<F2>"
2919 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2920 argv[1].v_type = VAR_STRING;
2921 argv[1].vval.v_string = vim_strsave(buf);
2922
2923 argv[2].v_type = VAR_UNKNOWN;
2924
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002925 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002926 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002927 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002928 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002929 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002930
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002931 vim_free(argv[1].vval.v_string);
2932 clear_tv(&rettv);
2933 return res;
2934}
2935
2936/*
2937 * Called when "c" was typed: invoke popup filter callbacks.
2938 * Returns TRUE when the character was consumed,
2939 */
2940 int
2941popup_do_filter(int c)
2942{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002943 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002944 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002945 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002946 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002947 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002948 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002949
2950 if (recursive)
2951 return FALSE;
2952 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002953
Bram Moolenaarf6396232019-08-24 19:36:00 +02002954 if (c == K_LEFTMOUSE)
2955 {
2956 int row = mouse_row;
2957 int col = mouse_col;
2958
2959 wp = mouse_find_win(&row, &col, FIND_POPUP);
2960 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002961 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002962 }
2963
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002964 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02002965 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002966 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002967 if (wp->w_filter_cb.cb_name != NULL
2968 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002969 res = invoke_popup_filter(wp, c);
2970
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002971 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002972 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002973 recursive = FALSE;
2974 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002975 return res;
2976}
2977
Bram Moolenaar3397f742019-06-02 18:40:06 +02002978/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002979 * Return TRUE if there is a popup visible with a filter callback and the
2980 * "mapping" property off.
2981 */
2982 int
2983popup_no_mapping(void)
2984{
2985 int round;
2986 win_T *wp;
2987
2988 for (round = 1; round <= 2; ++round)
2989 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2990 wp != NULL; wp = wp->w_next)
2991 if (wp->w_filter_cb.cb_name != NULL
2992 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2993 return TRUE;
2994 return FALSE;
2995}
2996
2997/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002998 * Called when the cursor moved: check if any popup needs to be closed if the
2999 * cursor moved far enough.
3000 */
3001 void
3002popup_check_cursor_pos()
3003{
3004 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003005
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003006 popup_reset_handled(POPUP_HANDLED_3);
3007 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003008 if (wp->w_popup_curwin != NULL
3009 && (curwin != wp->w_popup_curwin
3010 || curwin->w_cursor.lnum != wp->w_popup_lnum
3011 || curwin->w_cursor.col < wp->w_popup_mincol
3012 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003013 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003014}
3015
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003016/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003017 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003018 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003019 static void
3020popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003021{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003022 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003023 char_u *cells;
3024 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003025
3026 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003027 return;
3028 if (wp->w_popup_mask_cells != NULL
3029 && wp->w_popup_mask_height == height
3030 && wp->w_popup_mask_width == width)
3031 return; // cache is still valid
3032
3033 vim_free(wp->w_popup_mask_cells);
3034 wp->w_popup_mask_cells = alloc_clear(width * height);
3035 if (wp->w_popup_mask_cells == NULL)
3036 return;
3037 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003038
3039 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3040 {
3041 int cols, cole;
3042 int lines, linee;
3043
3044 li = lio->li_tv.vval.v_list->lv_first;
3045 cols = tv_get_number(&li->li_tv);
3046 if (cols < 0)
3047 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003048 li = li->li_next;
3049 cole = tv_get_number(&li->li_tv);
3050 if (cole < 0)
3051 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003052 li = li->li_next;
3053 lines = tv_get_number(&li->li_tv);
3054 if (lines < 0)
3055 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003056 li = li->li_next;
3057 linee = tv_get_number(&li->li_tv);
3058 if (linee < 0)
3059 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003060
3061 for (row = lines - 1; row < linee && row < height; ++row)
3062 for (col = cols - 1; col < cole && col < width; ++col)
3063 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003064 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003065}
3066
3067/*
3068 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3069 * "col" and "line" are screen coordinates.
3070 */
3071 static int
3072popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3073{
3074 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3075 int line = screenline - wp->w_winrow;
3076
3077 return col >= 0 && col < width
3078 && line >= 0 && line < height
3079 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003080}
3081
3082/*
3083 * Set flags in popup_transparent[] for window "wp" to "val".
3084 */
3085 static void
3086update_popup_transparent(win_T *wp, int val)
3087{
3088 if (wp->w_popup_mask != NULL)
3089 {
3090 int width = popup_width(wp);
3091 int height = popup_height(wp);
3092 listitem_T *lio, *li;
3093 int cols, cole;
3094 int lines, linee;
3095 int col, line;
3096
3097 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3098 {
3099 li = lio->li_tv.vval.v_list->lv_first;
3100 cols = tv_get_number(&li->li_tv);
3101 if (cols < 0)
3102 cols = width + cols + 1;
3103 li = li->li_next;
3104 cole = tv_get_number(&li->li_tv);
3105 if (cole < 0)
3106 cole = width + cole + 1;
3107 li = li->li_next;
3108 lines = tv_get_number(&li->li_tv);
3109 if (lines < 0)
3110 lines = height + lines + 1;
3111 li = li->li_next;
3112 linee = tv_get_number(&li->li_tv);
3113 if (linee < 0)
3114 linee = height + linee + 1;
3115
3116 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003117 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003118 if (cols < 0)
3119 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003120 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003121 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003122 if (lines < 0)
3123 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003124 for (line = lines; line < linee
3125 && line + wp->w_winrow < screen_Rows; ++line)
3126 for (col = cols; col < cole
3127 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003128 popup_transparent[(line + wp->w_winrow) * screen_Columns
3129 + col + wp->w_wincol] = val;
3130 }
3131 }
3132}
3133
3134/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003135 * Only called when popup window "wp" is hidden: If the window is positioned
3136 * next to a text property, and it is now visible, then unhide the popup.
3137 * We don't check if visible popups become hidden, that is done in
3138 * popup_adjust_position().
3139 * Return TRUE if the popup became unhidden.
3140 */
3141 static int
3142check_popup_unhidden(win_T *wp)
3143{
3144 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3145 {
3146 textprop_T prop;
3147 linenr_T lnum;
3148
3149 if (find_visible_prop(wp->w_popup_prop_win,
3150 wp->w_popup_prop_type, wp->w_popup_prop_id,
3151 &prop, &lnum) == OK)
3152 {
3153 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003154 wp->w_popup_prop_topline = 0; // force repositioning
3155 return TRUE;
3156 }
3157 }
3158 return FALSE;
3159}
3160
3161/*
3162 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3163 * That is when the buffer in the popup was changed, or the popup is following
3164 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003165 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003166 */
3167 static int
3168popup_need_position_adjust(win_T *wp)
3169{
3170 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3171 return TRUE;
3172 if (win_valid(wp->w_popup_prop_win))
3173 return wp->w_popup_prop_changedtick
3174 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003175 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3176 || ((wp->w_popup_flags & POPF_CURSORLINE)
3177 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003178 return FALSE;
3179}
3180
3181/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003182 * Update "popup_mask" if needed.
3183 * Also recomputes the popup size and positions.
3184 * Also updates "popup_visible".
3185 * Also marks window lines for redrawing.
3186 */
3187 void
3188may_update_popup_mask(int type)
3189{
3190 win_T *wp;
3191 short *mask;
3192 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003193 int redraw_all_popups = FALSE;
3194 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003195
3196 // Need to recompute when switching tabs.
3197 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3198 // (such as the screen size) must have changed.
3199 if (popup_mask_tab != curtab || type >= NOT_VALID)
3200 {
3201 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003202 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003203 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003204
3205 // Check if any popup window buffer has changed and if any popup connected
3206 // to a text property has become visible.
3207 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3208 if (wp->w_popup_flags & POPF_HIDDEN)
3209 popup_mask_refresh |= check_popup_unhidden(wp);
3210 else if (popup_need_position_adjust(wp))
3211 popup_mask_refresh = TRUE;
3212 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3213 if (wp->w_popup_flags & POPF_HIDDEN)
3214 popup_mask_refresh |= check_popup_unhidden(wp);
3215 else if (popup_need_position_adjust(wp))
3216 popup_mask_refresh = TRUE;
3217
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003218 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003219 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003220
3221 // Need to update the mask, something has changed.
3222 popup_mask_refresh = FALSE;
3223 popup_mask_tab = curtab;
3224 popup_visible = FALSE;
3225
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003226 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003227 // If redrawing only what is needed, update "popup_mask_next" and then
3228 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003229 redrawing_all_win = TRUE;
3230 FOR_ALL_WINDOWS(wp)
3231 if (wp->w_redr_type < SOME_VALID)
3232 redrawing_all_win = FALSE;
3233 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003234 mask = popup_mask;
3235 else
3236 mask = popup_mask_next;
3237 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3238
3239 // Find the window with the lowest zindex that hasn't been handled yet,
3240 // so that the window with a higher zindex overwrites the value in
3241 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003242 popup_reset_handled(POPUP_HANDLED_4);
3243 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003244 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003245 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003246 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003247
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003248 popup_visible = TRUE;
3249
Bram Moolenaar12034e22019-08-25 22:25:02 +02003250 // Recompute the position if the text changed. It may make the popup
3251 // hidden if it's attach to a text property that is no longer visible.
3252 if (redraw_all_popups || popup_need_position_adjust(wp))
3253 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003254 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003255 if (wp->w_popup_flags & POPF_HIDDEN)
3256 continue;
3257 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003258
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003259 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003260 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003261 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003262 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003263 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003264 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003265 col < wp->w_wincol + width - wp->w_popup_leftoff
3266 && col < screen_Columns; ++col)
3267 if (wp->w_popup_mask_cells == NULL
3268 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003269 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003270 }
3271
3272 // Only check which lines are to be updated if not already
3273 // updating all lines.
3274 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003275 {
3276 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3277 win_T *prev_wp = NULL;
3278
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003279 for (line = 0; line < screen_Rows; ++line)
3280 {
3281 int col_done = 0;
3282
3283 for (col = 0; col < screen_Columns; ++col)
3284 {
3285 int off = line * screen_Columns + col;
3286
3287 if (popup_mask[off] != popup_mask_next[off])
3288 {
3289 popup_mask[off] = popup_mask_next[off];
3290
3291 if (line >= cmdline_row)
3292 {
3293 // the command line needs to be cleared if text below
3294 // the popup is now visible.
3295 if (!msg_scrolled && popup_mask_next[off] == 0)
3296 clear_cmdline = TRUE;
3297 }
3298 else if (col >= col_done)
3299 {
3300 linenr_T lnum;
3301 int line_cp = line;
3302 int col_cp = col;
3303
3304 // The screen position "line" / "col" needs to be
3305 // redrawn. Figure out what window that is and update
3306 // w_redraw_top and w_redr_bot. Only needs to be done
3307 // once for each window line.
3308 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3309 if (wp != NULL)
3310 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003311 if (wp != prev_wp)
3312 {
3313 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3314 prev_wp = wp;
3315 }
3316
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003317 if (line_cp >= wp->w_height)
3318 // In (or below) status line
3319 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003320 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003321 {
3322 // compute the position in the buffer line from
3323 // the position in the window
3324 mouse_comp_pos(wp, &line_cp, &col_cp,
3325 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003326 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003327 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003328
3329 // This line is going to be redrawn, no need to
3330 // check until the right side of the window.
3331 col_done = wp->w_wincol + wp->w_width - 1;
3332 }
3333 }
3334 }
3335 }
3336 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003337
3338 vim_free(plines_cache);
3339 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003340}
3341
3342/*
3343 * Return a string of "len" spaces in IObuff.
3344 */
3345 static char_u *
3346get_spaces(int len)
3347{
3348 vim_memset(IObuff, ' ', (size_t)len);
3349 IObuff[len] = NUL;
3350 return IObuff;
3351}
3352
3353/*
3354 * Update popup windows. They are drawn on top of normal windows.
3355 * "win_update" is called for each popup window, lowest zindex first.
3356 */
3357 void
3358update_popups(void (*win_update)(win_T *wp))
3359{
3360 win_T *wp;
3361 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003362 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003363 int total_width;
3364 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003365 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003366 int popup_attr;
3367 int border_attr[4];
3368 int border_char[8];
3369 char_u buf[MB_MAXBYTES];
3370 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003371 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003372 int padcol = 0;
3373 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003374 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003375 int sb_thumb_top = 0;
3376 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003377 int attr_scroll = 0;
3378 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003379
3380 // Find the window with the lowest zindex that hasn't been updated yet,
3381 // so that the window with a higher zindex is drawn later, thus goes on
3382 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003383 popup_reset_handled(POPUP_HANDLED_5);
3384 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003385 {
3386 // This drawing uses the zindex of the popup window, so that it's on
3387 // top of the text but doesn't draw when another popup with higher
3388 // zindex is on top of the character.
3389 screen_zindex = wp->w_zindex;
3390
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003391 // Set flags in popup_transparent[] for masked cells.
3392 update_popup_transparent(wp, 1);
3393
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003394 // adjust w_winrow and w_wincol for border and padding, since
3395 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003396 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003397 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3398 - wp->w_popup_leftoff;
3399 if (wp->w_wincol + left_extra < 0)
3400 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003401 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003402 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003403
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003404 // Draw the popup text, unless it's off screen.
3405 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003406 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003407 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003408
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003409 // move the cursor into the visible lines, otherwise executing
3410 // commands with win_execute() may cause the text to jump.
3411 if (wp->w_cursor.lnum < wp->w_topline)
3412 wp->w_cursor.lnum = wp->w_topline;
3413 else if (wp->w_cursor.lnum >= wp->w_botline)
3414 wp->w_cursor.lnum = wp->w_botline - 1;
3415 }
3416
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003417 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003418 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003419
Bram Moolenaard529ba52019-07-02 23:13:53 +02003420 total_width = popup_width(wp);
3421 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003422 popup_attr = get_wcr_attr(wp);
3423
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003424 if (wp->w_winrow + total_height > cmdline_row)
3425 wp->w_popup_flags |= POPF_ON_CMDLINE;
3426 else
3427 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3428
3429
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003430 // We can only use these line drawing characters when 'encoding' is
3431 // "utf-8" and 'ambiwidth' is "single".
3432 if (enc_utf8 && *p_ambw == 's')
3433 {
3434 border_char[0] = border_char[2] = 0x2550;
3435 border_char[1] = border_char[3] = 0x2551;
3436 border_char[4] = 0x2554;
3437 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003438 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3439 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003440 border_char[7] = 0x255a;
3441 }
3442 else
3443 {
3444 border_char[0] = border_char[2] = '-';
3445 border_char[1] = border_char[3] = '|';
3446 for (i = 4; i < 8; ++i)
3447 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003448 if (wp->w_popup_flags & POPF_RESIZE)
3449 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003450 }
3451 for (i = 0; i < 8; ++i)
3452 if (wp->w_border_char[i] != 0)
3453 border_char[i] = wp->w_border_char[i];
3454
3455 for (i = 0; i < 4; ++i)
3456 {
3457 border_attr[i] = popup_attr;
3458 if (wp->w_border_highlight[i] != NULL)
3459 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3460 }
3461
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003462 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003463 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003464 if (wp->w_popup_border[0] > 0)
3465 {
3466 // top border
3467 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003468 wincol < 0 ? 0 : wincol, wincol + total_width,
3469 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003470 ? border_char[4] : border_char[0],
3471 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003472 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003473 {
3474 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3475 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003476 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003477 }
3478 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003479 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3480 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003481
Bram Moolenaard529ba52019-07-02 23:13:53 +02003482 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3483 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003484 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003485 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3486 - wp->w_has_scrollbar;
3487 if (padcol < 0)
3488 {
3489 padwidth += padcol;
3490 padcol = 0;
3491 }
3492 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003493 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003494 {
3495 // top padding
3496 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003497 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003498 ' ', ' ', popup_attr);
3499 }
3500
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003501 // Title goes on top of border or padding.
3502 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003503 {
3504 int len = (int)STRLEN(wp->w_popup_title) + 1;
3505 char_u *title = alloc(len);
3506
3507 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3508 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003509 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003510 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003511 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003512
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003513 // Compute scrollbar thumb position and size.
3514 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003515 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003516 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3517 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003518
Bram Moolenaar076d9882019-09-14 22:23:29 +02003519 sb_thumb_height = (height * height + linecount / 2) / linecount;
3520 if (wp->w_topline > 1 && sb_thumb_height == height)
3521 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003522 if (sb_thumb_height == 0)
3523 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003524 if (linecount <= wp->w_height)
3525 // it just fits, avoid divide by zero
3526 sb_thumb_top = 0;
3527 else
3528 sb_thumb_top = (wp->w_topline - 1
3529 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003530 * (wp->w_height - sb_thumb_height)
3531 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003532 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3533 sb_thumb_top = 1; // show it's scrolled
3534
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003535 if (wp->w_scrollbar_highlight != NULL)
3536 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3537 else
3538 attr_scroll = highlight_attr[HLF_PSB];
3539 if (wp->w_thumb_highlight != NULL)
3540 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3541 else
3542 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003543 }
3544
3545 for (i = wp->w_popup_border[0];
3546 i < total_height - wp->w_popup_border[2]; ++i)
3547 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003548 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003549 // left and right padding only needed next to the body
3550 int do_padding =
3551 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3552 && i < total_height - wp->w_popup_border[2]
3553 - wp->w_popup_padding[2];
3554
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003555 row = wp->w_winrow + i;
3556
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003557 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003558 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003559 {
3560 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003561 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003562 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003563 if (do_padding && wp->w_popup_padding[3] > 0)
3564 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003565 int col = wincol + wp->w_popup_border[3];
3566
Bram Moolenaard529ba52019-07-02 23:13:53 +02003567 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003568 pad_left = wp->w_popup_padding[3];
3569 if (col < 0)
3570 {
3571 pad_left += col;
3572 col = 0;
3573 }
3574 if (pad_left > 0)
3575 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3576 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003577 // scrollbar
3578 if (wp->w_has_scrollbar)
3579 {
3580 int line = i - top_off;
3581 int scroll_col = wp->w_wincol + total_width - 1
3582 - wp->w_popup_border[1];
3583
3584 if (line >= 0 && line < wp->w_height)
3585 screen_putchar(' ', row, scroll_col,
3586 line >= sb_thumb_top
3587 && line < sb_thumb_top + sb_thumb_height
3588 ? attr_thumb : attr_scroll);
3589 else
3590 screen_putchar(' ', row, scroll_col, popup_attr);
3591 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003592 // right border
3593 if (wp->w_popup_border[1] > 0)
3594 {
3595 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003596 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003597 }
3598 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003599 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003600 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003601 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003602 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3603 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003604 }
3605
3606 if (wp->w_popup_padding[2] > 0)
3607 {
3608 // bottom padding
3609 row = wp->w_winrow + wp->w_popup_border[0]
3610 + wp->w_popup_padding[0] + wp->w_height;
3611 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003612 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003613 }
3614
3615 if (wp->w_popup_border[2] > 0)
3616 {
3617 // bottom border
3618 row = wp->w_winrow + total_height - 1;
3619 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003620 wincol < 0 ? 0 : wincol,
3621 wincol + total_width,
3622 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003623 ? border_char[7] : border_char[2],
3624 border_char[2], border_attr[2]);
3625 if (wp->w_popup_border[1] > 0)
3626 {
3627 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003628 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003629 }
3630 }
3631
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003632 if (wp->w_popup_close == POPCLOSE_BUTTON)
3633 {
3634 // close button goes on top of anything at the top-right corner
3635 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003636 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003637 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3638 }
3639
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003640 update_popup_transparent(wp, 0);
3641
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003642 // Back to the normal zindex.
3643 screen_zindex = 0;
3644 }
3645}
3646
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003647/*
3648 * Mark references in callbacks of one popup window.
3649 */
3650 static int
3651set_ref_in_one_popup(win_T *wp, int copyID)
3652{
3653 int abort = FALSE;
3654 typval_T tv;
3655
3656 if (wp->w_close_cb.cb_partial != NULL)
3657 {
3658 tv.v_type = VAR_PARTIAL;
3659 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3660 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3661 }
3662 if (wp->w_filter_cb.cb_partial != NULL)
3663 {
3664 tv.v_type = VAR_PARTIAL;
3665 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3666 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3667 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003668 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003669 return abort;
3670}
3671
3672/*
3673 * Set reference in callbacks of popup windows.
3674 */
3675 int
3676set_ref_in_popups(int copyID)
3677{
3678 int abort = FALSE;
3679 win_T *wp;
3680 tabpage_T *tp;
3681
3682 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3683 abort = abort || set_ref_in_one_popup(wp, copyID);
3684
3685 FOR_ALL_TABPAGES(tp)
3686 {
3687 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3688 abort = abort || set_ref_in_one_popup(wp, copyID);
3689 if (abort)
3690 break;
3691 }
3692 return abort;
3693}
Bram Moolenaar79648732019-07-18 21:43:07 +02003694
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003695 int
3696popup_is_popup(win_T *wp)
3697{
3698 return wp->w_popup_flags != 0;
3699}
3700
3701#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003702/*
3703 * Find an existing popup used as the preview window, in the current tab page.
3704 * Return NULL if not found.
3705 */
3706 win_T *
3707popup_find_preview_window(void)
3708{
3709 win_T *wp;
3710
3711 // Preview window popup is always local to tab page.
3712 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3713 if (wp->w_p_pvw)
3714 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003715 return NULL;
3716}
3717
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003718/*
3719 * Find an existing popup used as the info window, in the current tab page.
3720 * Return NULL if not found.
3721 */
3722 win_T *
3723popup_find_info_window(void)
3724{
3725 win_T *wp;
3726
3727 // info window popup is always local to tab page.
3728 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3729 if (wp->w_popup_flags & POPF_INFO)
3730 return wp;
3731 return NULL;
3732}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003733#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003734
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003735 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003736f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3737{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003738#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003739 win_T *wp = popup_find_info_window();
3740
3741 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003742#else
3743 rettv->vval.v_number = 0;
3744#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003745}
3746
3747 void
3748f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003749{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003750#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003751 win_T *wp = popup_find_preview_window();
3752
3753 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003754#else
3755 rettv->vval.v_number = 0;
3756#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003757}
3758
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003759#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003760/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003761 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003762 * NOTE: this makes the popup the current window, so that the file can be
3763 * edited. However, it must not remain to be the current window, the caller
3764 * must make sure of that.
3765 */
3766 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003767popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003768{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003769 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003770
3771 if (wp == NULL)
3772 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003773 if (info)
3774 wp->w_popup_flags |= POPF_INFO;
3775 else
3776 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003777
3778 // Set the width to a reasonable value, so that w_topline can be computed.
3779 if (wp->w_minwidth > 0)
3780 wp->w_width = wp->w_minwidth;
3781 else if (wp->w_maxwidth > 0)
3782 wp->w_width = wp->w_maxwidth;
3783 else
3784 wp->w_width = curwin->w_width;
3785
3786 // Will switch to another buffer soon, dummy one can be wiped.
3787 wp->w_buffer->b_locked = FALSE;
3788
3789 win_enter(wp, FALSE);
3790 return OK;
3791}
3792
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003793/*
3794 * Close any preview popup.
3795 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003796 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003797popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003798{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003799 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003800
3801 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003802 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003803}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003804
3805/*
3806 * Hide the info popup.
3807 */
3808 void
3809popup_hide_info(void)
3810{
3811 win_T *wp = popup_find_info_window();
3812
3813 if (wp != NULL)
3814 popup_hide(wp);
3815}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003816#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003817
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003818/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003819 * Close any popup for a text property associated with "win".
3820 * Return TRUE if a popup was closed.
3821 */
3822 int
3823popup_win_closed(win_T *win)
3824{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003825 int round;
3826 win_T *wp;
3827 win_T *next;
3828 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003829
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003830 for (round = 1; round <= 2; ++round)
3831 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3832 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003833 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003834 next = wp->w_next;
3835 if (wp->w_popup_prop_win == win)
3836 {
3837 popup_close_with_retval(wp, -1);
3838 ret = TRUE;
3839 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003840 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003841 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003842}
3843
3844/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003845 * Set the title of the popup window to the file name.
3846 */
3847 void
3848popup_set_title(win_T *wp)
3849{
3850 if (wp->w_buffer->b_fname != NULL)
3851 {
3852 char_u dirname[MAXPATHL];
3853 size_t len;
3854
3855 mch_dirname(dirname, MAXPATHL);
3856 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3857
3858 vim_free(wp->w_popup_title);
3859 len = STRLEN(wp->w_buffer->b_fname) + 3;
3860 wp->w_popup_title = alloc((int)len);
3861 if (wp->w_popup_title != NULL)
3862 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3863 wp->w_buffer->b_fname);
3864 redraw_win_later(wp, VALID);
3865 }
3866}
3867
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003868# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003869/*
3870 * If there is a preview window, update the title.
3871 * Used after changing directory.
3872 */
3873 void
3874popup_update_preview_title(void)
3875{
3876 win_T *wp = popup_find_preview_window();
3877
3878 if (wp != NULL)
3879 popup_set_title(wp);
3880}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003881# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003882
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003883#endif // FEAT_PROP_POPUP