blob: 4b85fec1932c9807edea619027c59d9c56c57598 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100100 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200101 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
103 ++i, li = li->li_next)
104 {
105 nr = (int)tv_get_number(&li->li_tv);
106 if (nr >= 0)
107 array[i] = nr > max_val ? max_val : nr;
108 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100109 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110 }
111 }
112}
113
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200114/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200115 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200116 */
117 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200118set_moved_values(win_T *wp)
119{
120 wp->w_popup_curwin = curwin;
121 wp->w_popup_lnum = curwin->w_cursor.lnum;
122 wp->w_popup_mincol = curwin->w_cursor.col;
123 wp->w_popup_maxcol = curwin->w_cursor.col;
124}
125
126/*
127 * Used when popup options contain "moved" with "word" or "WORD".
128 */
129 static void
130set_moved_columns(win_T *wp, int flags)
131{
132 char_u *ptr;
133 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
134
135 if (len > 0)
136 {
137 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
138 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
139 }
140}
141
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200142/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200143 * Used when popup options contain "mousemoved": set default moved values.
144 */
145 static void
146set_mousemoved_values(win_T *wp)
147{
148 wp->w_popup_mouse_row = mouse_row;
149 wp->w_popup_mouse_mincol = mouse_col;
150 wp->w_popup_mouse_maxcol = mouse_col;
151}
152
153/*
154 * Used when popup options contain "moved" with "word" or "WORD".
155 */
156 static void
157set_mousemoved_columns(win_T *wp, int flags)
158{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160 char_u *text;
161 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 pos_T pos;
163 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164
165 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200166 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200167 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200168 // convert text column to mouse column
169 pos.col = col;
170 pos.coladd = 0;
171 getvcol(textwp, &pos, &mcol, NULL, NULL);
172 wp->w_popup_mouse_mincol = mcol;
173
Bram Moolenaar10727682019-07-12 13:59:20 +0200174 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200175 getvcol(textwp, &pos, NULL, NULL, &mcol);
176 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200177 vim_free(text);
178 }
179}
180
181/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200182 * Return TRUE if "row"/"col" is on the border of the popup.
183 * The values are relative to the top-left corner.
184 */
185 int
186popup_on_border(win_T *wp, int row, int col)
187{
188 return (row == 0 && wp->w_popup_border[0] > 0)
189 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
190 || (col == 0 && wp->w_popup_border[3] > 0)
191 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
192}
193
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
196 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198 * Caller should check the left mouse button was clicked.
199 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200 */
201 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200202popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200203{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200204 if (wp->w_popup_close == POPCLOSE_BUTTON
205 && row == 0 && col == popup_width(wp) - 1)
206 {
207 popup_close_for_mouse_click(wp);
208 return TRUE;
209 }
210 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200211}
212
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200213// Values set when dragging a popup window starts.
214static int drag_start_row;
215static int drag_start_col;
216static int drag_start_wantline;
217static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200218static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200219
220/*
221 * Mouse down on border of popup window: start dragging it.
222 * Uses mouse_col and mouse_row.
223 */
224 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200225popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200226{
227 drag_start_row = mouse_row;
228 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200229 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200230 drag_start_wantline = wp->w_winrow + 1;
231 else
232 drag_start_wantline = wp->w_wantline;
233 if (wp->w_wantcol == 0)
234 drag_start_wantcol = wp->w_wincol + 1;
235 else
236 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200237
238 // Stop centering the popup
239 if (wp->w_popup_pos == POPPOS_CENTER)
240 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200241
242 drag_on_resize_handle = wp->w_popup_border[1] > 0
243 && wp->w_popup_border[2] > 0
244 && row == popup_height(wp) - 1
245 && col == popup_width(wp) - 1;
246
247 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
248 {
249 if (wp->w_popup_pos == POPPOS_TOPRIGHT
250 || wp->w_popup_pos == POPPOS_BOTRIGHT)
251 wp->w_wantcol = wp->w_wincol + 1;
252 if (wp->w_popup_pos == POPPOS_BOTLEFT)
253 wp->w_wantline = wp->w_winrow + 1;
254 wp->w_popup_pos = POPPOS_TOPLEFT;
255 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256}
257
258/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259 * Mouse moved while dragging a popup window: adjust the window popup position
260 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261 */
262 void
263popup_drag(win_T *wp)
264{
265 // The popup may be closed before dragging stops.
266 if (!win_valid_popup(wp))
267 return;
268
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
270 {
271 int width_inc = mouse_col - drag_start_col;
272 int height_inc = mouse_row - drag_start_row;
273
274 if (width_inc != 0)
275 {
276 int width = wp->w_width + width_inc;
277
278 if (width < 1)
279 width = 1;
280 wp->w_minwidth = width;
281 wp->w_maxwidth = width;
282 drag_start_col = mouse_col;
283 }
284
285 if (height_inc != 0)
286 {
287 int height = wp->w_height + height_inc;
288
289 if (height < 1)
290 height = 1;
291 wp->w_minheight = height;
292 wp->w_maxheight = height;
293 drag_start_row = mouse_row;
294 }
295
296 popup_adjust_position(wp);
297 return;
298 }
299
300 if (!(wp->w_popup_flags & POPF_DRAG))
301 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
303 if (wp->w_wantline < 1)
304 wp->w_wantline = 1;
305 if (wp->w_wantline > Rows)
306 wp->w_wantline = Rows;
307 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
308 if (wp->w_wantcol < 1)
309 wp->w_wantcol = 1;
310 if (wp->w_wantcol > Columns)
311 wp->w_wantcol = Columns;
312
313 popup_adjust_position(wp);
314}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200315
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200316/*
317 * Set w_firstline to match the current "wp->w_topline".
318 */
319 void
320popup_set_firstline(win_T *wp)
321{
322 int height = wp->w_height;
323
324 wp->w_firstline = wp->w_topline;
325 popup_adjust_position(wp);
326
327 // we don't want the popup to get smaller, decrement the first line
328 // until it doesn't
329 while (wp->w_firstline > 1 && wp->w_height < height)
330 {
331 --wp->w_firstline;
332 popup_adjust_position(wp);
333 }
334}
335
336/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200337 * Return TRUE if the position is in the popup window scrollbar.
338 */
339 int
340popup_is_in_scrollbar(win_T *wp, int row, int col)
341{
342 return wp->w_has_scrollbar
343 && row >= wp->w_popup_border[0]
344 && row < popup_height(wp) - wp->w_popup_border[2]
345 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
346}
347
348
349/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200350 * Handle a click in a popup window, if it is in the scrollbar.
351 */
352 void
353popup_handle_scrollbar_click(win_T *wp, int row, int col)
354{
355 int height = popup_height(wp);
356 int old_topline = wp->w_topline;
357
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359 {
360 if (row >= height / 2)
361 {
362 // Click in lower half, scroll down.
363 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
364 ++wp->w_topline;
365 }
366 else if (wp->w_topline > 1)
367 // click on upper half, scroll up.
368 --wp->w_topline;
369 if (wp->w_topline != old_topline)
370 {
371 popup_set_firstline(wp);
372 redraw_win_later(wp, NOT_VALID);
373 }
374 }
375}
376
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200377#if defined(FEAT_TIMERS)
378 static void
379popup_add_timeout(win_T *wp, int time)
380{
381 char_u cbbuf[50];
382 char_u *ptr = cbbuf;
383 typval_T tv;
384
385 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
386 "{_ -> popup_close(%d)}", wp->w_id);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200387 if (get_lambda_tv(&ptr, &tv, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200388 {
389 wp->w_popup_timer = create_timer(time, 0);
390 wp->w_popup_timer->tr_callback = get_callback(&tv);
391 clear_tv(&tv);
392 }
393}
394#endif
395
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100396 static poppos_T
397get_pos_entry(dict_T *d, int give_error)
398{
399 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
400 int nr;
401
402 if (str == NULL)
403 return POPPOS_NONE;
404
405 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
406 ++nr)
407 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
408 return poppos_entries[nr].pp_val;
409
410 if (give_error)
411 semsg(_(e_invarg2), str);
412 return POPPOS_NONE;
413}
414
Bram Moolenaar17627312019-06-02 19:53:44 +0200415/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200416 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200417 */
418 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200419apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200420{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200421 int nr;
422 char_u *str;
423 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200424
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200425 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200426 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200427 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200428 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200429 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200430 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200431 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200432 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433
434 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantline = nr;
437 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200438 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200439 wp->w_wantcol = nr;
440
Bram Moolenaar55881332020-08-18 13:04:15 +0200441
442 nr = dict_get_bool(d, (char_u *)"fixed", -1);
443 if (nr != -1)
444 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200445
Bram Moolenaar12034e22019-08-25 22:25:02 +0200446 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 poppos_T ppt = get_pos_entry(d, TRUE);
448
449 if (ppt != POPPOS_NONE)
450 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200451 }
452
453 str = dict_get_string(d, (char_u *)"textprop", FALSE);
454 if (str != NULL)
455 {
456 wp->w_popup_prop_type = 0;
457 if (*str != NUL)
458 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100459 wp->w_popup_prop_win = curwin;
460 di = dict_find(d, (char_u *)"textpropwin", -1);
461 if (di != NULL)
462 {
463 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
464 if (!win_valid(wp->w_popup_prop_win))
465 wp->w_popup_prop_win = curwin;
466 }
467
468 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200469 if (nr <= 0)
470 nr = find_prop_type_id(str, NULL);
471 if (nr <= 0)
472 semsg(_(e_invarg2), str);
473 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200475 }
476 }
477
478 di = dict_find(d, (char_u *)"textpropid", -1);
479 if (di != NULL)
480 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200481}
482
Bram Moolenaarb0992022020-01-30 14:55:42 +0100483/*
484 * Handle "moved" and "mousemoved" arguments.
485 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200486 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200487handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
488{
489 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
490 {
491 char_u *s = di->di_tv.vval.v_string;
492 int flags = 0;
493
494 if (STRCMP(s, "word") == 0)
495 flags = FIND_IDENT | FIND_STRING;
496 else if (STRCMP(s, "WORD") == 0)
497 flags = FIND_STRING;
498 else if (STRCMP(s, "expr") == 0)
499 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
500 else if (STRCMP(s, "any") != 0)
501 semsg(_(e_invarg2), s);
502 if (flags != 0)
503 {
504 if (mousemoved)
505 set_mousemoved_columns(wp, flags);
506 else
507 set_moved_columns(wp, flags);
508 }
509 }
510 else if (di->di_tv.v_type == VAR_LIST
511 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200512 && (di->di_tv.vval.v_list->lv_len == 2
513 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200514 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200515 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100516 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200517 int mincol;
518 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200519
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200520 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100521 li = l->lv_first;
522 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200523 {
524 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
525
526 // Three numbers, might be from popup_getoptions().
527 if (mousemoved)
528 wp->w_popup_mouse_row = nr;
529 else
530 wp->w_popup_lnum = nr;
531 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100532 if (nr == 0)
533 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200534 }
535
536 mincol = tv_get_number(&li->li_tv);
537 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200538 if (mousemoved)
539 {
540 wp->w_popup_mouse_mincol = mincol;
541 wp->w_popup_mouse_maxcol = maxcol;
542 }
543 else
544 {
545 wp->w_popup_mincol = mincol;
546 wp->w_popup_maxcol = maxcol;
547 }
548 }
549 else
550 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
551}
552
553 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200554check_highlight(dict_T *dict, char *name, char_u **pval)
555{
556 dictitem_T *di;
557 char_u *str;
558
559 di = dict_find(dict, (char_u *)name, -1);
560 if (di != NULL)
561 {
562 if (di->di_tv.v_type != VAR_STRING)
563 semsg(_(e_invargval), name);
564 else
565 {
566 str = tv_get_string(&di->di_tv);
567 if (*str != NUL)
568 *pval = vim_strsave(str);
569 }
570 }
571}
572
Bram Moolenaarae943152019-06-16 22:54:14 +0200573/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200574 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200575 */
576 static void
577popup_show_curline(win_T *wp)
578{
579 if (wp->w_cursor.lnum < wp->w_topline)
580 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200581 else if (wp->w_cursor.lnum >= wp->w_botline
582 && (curwin->w_valid & VALID_BOTLINE))
583 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200584 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200585 if (wp->w_topline < 1)
586 wp->w_topline = 1;
587 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
588 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200589 while (wp->w_topline < wp->w_cursor.lnum
590 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
591 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
592 > wp->w_height)
593 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200594 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200595
596 // Don't use "firstline" now.
597 wp->w_firstline = 0;
598}
599
600/*
601 * Get the sign group name for window "wp".
602 * Returns a pointer to a static buffer, overwritten on the next call.
603 */
604 static char_u *
605popup_get_sign_name(win_T *wp)
606{
607 static char buf[30];
608
609 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
610 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200611}
612
613/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200614 * Highlight the line with the cursor.
615 * Also scrolls the text to put the cursor line in view.
616 */
617 static void
618popup_highlight_curline(win_T *wp)
619{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200620 int sign_id = 0;
621 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622
Bram Moolenaar72570732019-11-30 14:21:53 +0100623 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200624
625 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
626 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200627 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200628
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200629 if (!sign_exists_by_name(sign_name))
630 {
631 char *linehl = "PopupSelected";
632
633 if (syn_name2id((char_u *)linehl) == 0)
634 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200635 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200636 }
637
Bram Moolenaar72570732019-11-30 14:21:53 +0100638 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200639 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
640 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200641 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200642 else
643 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200644 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200645}
646
647/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200648 * Shared between popup_create() and f_popup_setoptions().
649 */
650 static void
651apply_general_options(win_T *wp, dict_T *dict)
652{
653 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200654 int nr;
655 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200656
Bram Moolenaarae943152019-06-16 22:54:14 +0200657 // TODO: flip
658
659 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200660 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200661 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200662 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200663 if (wp->w_firstline < 0)
664 wp->w_firstline = -1;
665 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200666
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200667 di = dict_find(dict, (char_u *)"scrollbar", -1);
668 if (di != NULL)
669 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
670
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200671 str = dict_get_string(dict, (char_u *)"title", FALSE);
672 if (str != NULL)
673 {
674 vim_free(wp->w_popup_title);
675 wp->w_popup_title = vim_strsave(str);
676 }
677
Bram Moolenaar55881332020-08-18 13:04:15 +0200678 nr = dict_get_bool(dict, (char_u *)"wrap", -1);
679 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200680 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200681
Bram Moolenaar55881332020-08-18 13:04:15 +0200682 nr = dict_get_bool(dict, (char_u *)"drag", -1);
683 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200684 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200685 if (nr)
686 wp->w_popup_flags |= POPF_DRAG;
687 else
688 wp->w_popup_flags &= ~POPF_DRAG;
689 }
690
Bram Moolenaar55881332020-08-18 13:04:15 +0200691 nr = dict_get_bool(dict, (char_u *)"posinvert", -1);
692 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100693 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 if (nr)
695 wp->w_popup_flags |= POPF_POSINVERT;
696 else
697 wp->w_popup_flags &= ~POPF_POSINVERT;
698 }
699
Bram Moolenaar55881332020-08-18 13:04:15 +0200700 nr = dict_get_bool(dict, (char_u *)"resize", -1);
701 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200702 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200703 if (nr)
704 wp->w_popup_flags |= POPF_RESIZE;
705 else
706 wp->w_popup_flags &= ~POPF_RESIZE;
707 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200708
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200709 di = dict_find(dict, (char_u *)"close", -1);
710 if (di != NULL)
711 {
712 int ok = TRUE;
713
714 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
715 {
716 char_u *s = di->di_tv.vval.v_string;
717
718 if (STRCMP(s, "none") == 0)
719 wp->w_popup_close = POPCLOSE_NONE;
720 else if (STRCMP(s, "button") == 0)
721 wp->w_popup_close = POPCLOSE_BUTTON;
722 else if (STRCMP(s, "click") == 0)
723 wp->w_popup_close = POPCLOSE_CLICK;
724 else
725 ok = FALSE;
726 }
727 else
728 ok = FALSE;
729 if (!ok)
730 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
731 }
732
Bram Moolenaarae943152019-06-16 22:54:14 +0200733 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
734 if (str != NULL)
735 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
736 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200737
Bram Moolenaarae943152019-06-16 22:54:14 +0200738 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
739 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200740
Bram Moolenaar790498b2019-06-01 22:15:29 +0200741 di = dict_find(dict, (char_u *)"borderhighlight", -1);
742 if (di != NULL)
743 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200744 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200745 emsg(_(e_listreq));
746 else
747 {
748 list_T *list = di->di_tv.vval.v_list;
749 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200750 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200751
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200752 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200753 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
754 ++i, li = li->li_next)
755 {
756 str = tv_get_string(&li->li_tv);
757 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100758 {
759 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100761 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200762 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200763 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
764 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100765 {
766 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100769 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200770 }
771 }
772
Bram Moolenaar790498b2019-06-01 22:15:29 +0200773 di = dict_find(dict, (char_u *)"borderchars", -1);
774 if (di != NULL)
775 {
776 if (di->di_tv.v_type != VAR_LIST)
777 emsg(_(e_listreq));
778 else
779 {
780 list_T *list = di->di_tv.vval.v_list;
781 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200782 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200783
784 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100785 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200786 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200787 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
788 ++i, li = li->li_next)
789 {
790 str = tv_get_string(&li->li_tv);
791 if (*str != NUL)
792 wp->w_border_char[i] = mb_ptr2char(str);
793 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200794 if (list->lv_len == 1)
795 for (i = 1; i < 8; ++i)
796 wp->w_border_char[i] = wp->w_border_char[0];
797 if (list->lv_len == 2)
798 {
799 for (i = 4; i < 8; ++i)
800 wp->w_border_char[i] = wp->w_border_char[1];
801 for (i = 1; i < 4; ++i)
802 wp->w_border_char[i] = wp->w_border_char[0];
803 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200804 }
805 }
806 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200807
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200808 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
809 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
810
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 di = dict_find(dict, (char_u *)"zindex", -1);
812 if (di != NULL)
813 {
814 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
815 if (wp->w_zindex < 1)
816 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
817 if (wp->w_zindex > 32000)
818 wp->w_zindex = 32000;
819 }
820
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200821 di = dict_find(dict, (char_u *)"mask", -1);
822 if (di != NULL)
823 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200824 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200825
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200826 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200827 {
828 listitem_T *li;
829
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200830 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200831 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200832 {
833 if (li->li_tv.v_type != VAR_LIST
834 || li->li_tv.vval.v_list == NULL
835 || li->li_tv.vval.v_list->lv_len != 4)
836 {
837 ok = FALSE;
838 break;
839 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100840 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200841 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200842 }
843 }
844 if (ok)
845 {
846 wp->w_popup_mask = di->di_tv.vval.v_list;
847 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200848 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200849 }
850 else
851 semsg(_(e_invargval), "mask");
852 }
853
Bram Moolenaarae943152019-06-16 22:54:14 +0200854#if defined(FEAT_TIMERS)
855 // Add timer to close the popup after some time.
856 nr = dict_get_number(dict, (char_u *)"time");
857 if (nr > 0)
858 popup_add_timeout(wp, nr);
859#endif
860
Bram Moolenaar3397f742019-06-02 18:40:06 +0200861 di = dict_find(dict, (char_u *)"moved", -1);
862 if (di != NULL)
863 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200864 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200865 handle_moved_argument(wp, di, FALSE);
866 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200867
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200868 di = dict_find(dict, (char_u *)"mousemoved", -1);
869 if (di != NULL)
870 {
871 set_mousemoved_values(wp);
872 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200873 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200874
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200875 di = dict_find(dict, (char_u *)"cursorline", -1);
876 if (di != NULL)
877 {
878 if (di->di_tv.v_type == VAR_NUMBER)
879 {
880 if (di->di_tv.vval.v_number != 0)
881 wp->w_popup_flags |= POPF_CURSORLINE;
882 else
883 wp->w_popup_flags &= ~POPF_CURSORLINE;
884 }
885 else
886 semsg(_(e_invargval), "cursorline");
887 }
888
Bram Moolenaarae943152019-06-16 22:54:14 +0200889 di = dict_find(dict, (char_u *)"filter", -1);
890 if (di != NULL)
891 {
892 callback_T callback = get_callback(&di->di_tv);
893
894 if (callback.cb_name != NULL)
895 {
896 free_callback(&wp->w_filter_cb);
897 set_callback(&wp->w_filter_cb, &callback);
898 }
899 }
Bram Moolenaar55881332020-08-18 13:04:15 +0200900 nr = dict_get_bool(dict, (char_u *)"mapping", -1);
901 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200902 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200903 if (nr)
904 wp->w_popup_flags |= POPF_MAPPING;
905 else
906 wp->w_popup_flags &= ~POPF_MAPPING;
907 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200908
Bram Moolenaar581ba392019-09-03 22:08:33 +0200909 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
910 if (str != NULL)
911 {
912 if (STRCMP(str, "a") == 0)
913 wp->w_filter_mode = MODE_ALL;
914 else
915 wp->w_filter_mode = mode_str2flags(str);
916 }
917
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 di = dict_find(dict, (char_u *)"callback", -1);
919 if (di != NULL)
920 {
921 callback_T callback = get_callback(&di->di_tv);
922
923 if (callback.cb_name != NULL)
924 {
925 free_callback(&wp->w_close_cb);
926 set_callback(&wp->w_close_cb, &callback);
927 }
928 }
929}
930
931/*
932 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200933 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200934 */
935 static void
936apply_options(win_T *wp, dict_T *dict)
937{
938 int nr;
939
940 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200941
942 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
943 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 apply_general_options(wp, dict);
946
Bram Moolenaar55881332020-08-18 13:04:15 +0200947 nr = dict_get_bool(dict, (char_u *)"hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200948 if (nr > 0)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200949 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200950
Bram Moolenaar33796b32019-06-08 16:01:13 +0200951 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200952 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200953}
954
955/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200956 * Add lines to the popup from a list of strings.
957 */
958 static void
959add_popup_strings(buf_T *buf, list_T *l)
960{
961 listitem_T *li;
962 linenr_T lnum = 0;
963 char_u *p;
964
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200965 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200966 if (li->li_tv.v_type == VAR_STRING)
967 {
968 p = li->li_tv.vval.v_string;
969 ml_append_buf(buf, lnum++,
970 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
971 }
972}
973
974/*
975 * Add lines to the popup from a list of dictionaries.
976 */
977 static void
978add_popup_dicts(buf_T *buf, list_T *l)
979{
980 listitem_T *li;
981 listitem_T *pli;
982 linenr_T lnum = 0;
983 char_u *p;
984 dict_T *dict;
985
986 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200987 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200988 {
989 if (li->li_tv.v_type != VAR_DICT)
990 {
991 emsg(_(e_dictreq));
992 return;
993 }
994 dict = li->li_tv.vval.v_dict;
995 p = dict == NULL ? NULL
996 : dict_get_string(dict, (char_u *)"text", FALSE);
997 ml_append_buf(buf, lnum++,
998 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
999 }
1000
1001 // add the text properties
1002 lnum = 1;
1003 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1004 {
1005 dictitem_T *di;
1006 list_T *plist;
1007
1008 dict = li->li_tv.vval.v_dict;
1009 di = dict_find(dict, (char_u *)"props", -1);
1010 if (di != NULL)
1011 {
1012 if (di->di_tv.v_type != VAR_LIST)
1013 {
1014 emsg(_(e_listreq));
1015 return;
1016 }
1017 plist = di->di_tv.vval.v_list;
1018 if (plist != NULL)
1019 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001020 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001021 {
1022 if (pli->li_tv.v_type != VAR_DICT)
1023 {
1024 emsg(_(e_dictreq));
1025 return;
1026 }
1027 dict = pli->li_tv.vval.v_dict;
1028 if (dict != NULL)
1029 {
1030 int col = dict_get_number(dict, (char_u *)"col");
1031
1032 prop_add_common( lnum, col, dict, buf, NULL);
1033 }
1034 }
1035 }
1036 }
1037 }
1038}
1039
1040/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001041 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1042 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001043 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001044popup_top_extra(win_T *wp)
1045{
1046 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1047
1048 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1049 return 1;
1050 return extra;
1051}
1052
1053/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001054 * Get the padding plus border at the left.
1055 */
1056 int
1057popup_left_extra(win_T *wp)
1058{
1059 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1060}
1061
1062/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001063 * Return the height of popup window "wp", including border and padding.
1064 */
1065 int
1066popup_height(win_T *wp)
1067{
1068 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001069 + popup_top_extra(wp)
1070 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001071}
1072
1073/*
1074 * Return the width of popup window "wp", including border, padding and
1075 * scrollbar.
1076 */
1077 int
1078popup_width(win_T *wp)
1079{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001080 // w_leftcol is how many columns of the core are left of the screen
1081 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001082 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001083 + popup_extra_width(wp)
1084 + wp->w_popup_rightoff;
1085}
1086
1087/*
1088 * Return the extra width of popup window "wp": border, padding and scrollbar.
1089 */
1090 int
1091popup_extra_width(win_T *wp)
1092{
1093 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1094 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1095 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001096}
1097
1098/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001099 * Adjust the position and size of the popup to fit on the screen.
1100 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001101 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001102popup_adjust_position(win_T *wp)
1103{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001104 linenr_T lnum;
1105 int wrapped = 0;
1106 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001107 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001108 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001109 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001110 int center_vert = FALSE;
1111 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001112 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001113 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001114 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1115 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1116 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1117 int extra_height = top_extra + bot_extra;
1118 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001119 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001120 int org_winrow = wp->w_winrow;
1121 int org_wincol = wp->w_wincol;
1122 int org_width = wp->w_width;
1123 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001124 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001125 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001126 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001127 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001128 int wantline = wp->w_wantline; // adjusted for textprop
1129 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001130 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001131 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001132
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001133 wp->w_winrow = 0;
1134 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001135 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001136 wp->w_popup_leftoff = 0;
1137 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001138
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001139 // May need to update the "cursorline" highlighting, which may also change
1140 // "topline"
1141 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1142 popup_highlight_curline(wp);
1143
Bram Moolenaar12034e22019-08-25 22:25:02 +02001144 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1145 {
1146 win_T *prop_win = wp->w_popup_prop_win;
1147 textprop_T prop;
1148 linenr_T prop_lnum;
1149 pos_T pos;
1150 int screen_row;
1151 int screen_scol;
1152 int screen_ccol;
1153 int screen_ecol;
1154
1155 // Popup window is positioned relative to a text property.
1156 if (find_visible_prop(prop_win,
1157 wp->w_popup_prop_type, wp->w_popup_prop_id,
1158 &prop, &prop_lnum) == FAIL)
1159 {
1160 // Text property is no longer visible, hide the popup.
1161 // Unhiding the popup is done in check_popup_unhidden().
1162 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1163 {
1164 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001165 if (win_valid(wp->w_popup_prop_win))
1166 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1167 }
1168 return;
1169 }
1170
1171 // Compute the desired position from the position of the text
1172 // property. Use "wantline" and "wantcol" as offsets.
1173 pos.lnum = prop_lnum;
1174 pos.col = prop.tp_col;
1175 if (wp->w_popup_pos == POPPOS_TOPLEFT
1176 || wp->w_popup_pos == POPPOS_BOTLEFT)
1177 pos.col += prop.tp_len - 1;
1178 textpos2screenpos(prop_win, &pos, &screen_row,
1179 &screen_scol, &screen_ccol, &screen_ecol);
1180
1181 if (wp->w_popup_pos == POPPOS_TOPLEFT
1182 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1183 // below the text
1184 wantline = screen_row + wantline + 1;
1185 else
1186 // above the text
1187 wantline = screen_row + wantline - 1;
1188 center_vert = FALSE;
1189 if (wp->w_popup_pos == POPPOS_TOPLEFT
1190 || wp->w_popup_pos == POPPOS_BOTLEFT)
1191 // right of the text
1192 wantcol = screen_ecol + wantcol;
1193 else
1194 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001195 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001196 use_wantcol = TRUE;
1197 }
1198 else
1199 {
1200 // If no line was specified default to vertical centering.
1201 if (wantline == 0)
1202 center_vert = TRUE;
1203 else if (wantline < 0)
1204 // If "wantline" is negative it actually means zero.
1205 wantline = 0;
1206 if (wantcol < 0)
1207 // If "wantcol" is negative it actually means zero.
1208 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209 }
1210
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001211 if (wp->w_popup_pos == POPPOS_CENTER)
1212 {
1213 // center after computing the size
1214 center_vert = TRUE;
1215 center_hor = TRUE;
1216 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001217 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001218 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001219 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1220 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001221 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001222 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001223 if (wp->w_winrow >= Rows)
1224 wp->w_winrow = Rows - 1;
1225 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001226
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001227 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001228 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001229 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1230 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001231 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001232 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001233 // Need to see at least one character after the decoration.
1234 if (wp->w_wincol > Columns - left_extra - 1)
1235 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001236 }
1237 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001238
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001239 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001240 // When left aligned use the space available, but shift to the left when we
1241 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001242 maxspace = Columns - wp->w_wincol - left_extra;
1243 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001244 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001245 {
1246 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001247 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001248 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001249
1250 if (wp->w_p_nu || wp->w_p_rnu)
1251 margin_width = number_width(wp) + 1;
1252#ifdef FEAT_FOLDING
1253 margin_width += wp->w_p_fdc;
1254#endif
1255#ifdef FEAT_SIGNS
1256 if (signcolumn_on(wp))
1257 margin_width += 2;
1258#endif
1259 if (margin_width >= maxwidth)
1260 margin_width = maxwidth - 1;
1261
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001262 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001263 minheight = wp->w_minheight;
1264#ifdef FEAT_TERMINAL
1265 // A terminal popup initially does not have content, use a default minimal
1266 // width of 20 characters and height of 5 lines.
1267 if (wp->w_buffer->b_term != NULL)
1268 {
1269 if (minwidth == 0)
1270 minwidth = 20;
1271 if (minheight == 0)
1272 minheight = 5;
1273 }
1274#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001275
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001276 if (wp->w_maxheight > 0)
1277 maxheight = wp->w_maxheight;
1278
Bram Moolenaar8d241042019-06-12 23:40:01 +02001279 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001280 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001281 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001282 if (wp->w_topline < 1)
1283 wp->w_topline = 1;
1284 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001285 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1286
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001287 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001288 // Use a minimum width of one, so that something shows when there is no
1289 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001290 // When "firstline" is -1 then start with the last buffer line and go
1291 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001292 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001293 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001294 if (wp->w_firstline < 0)
1295 lnum = wp->w_buffer->b_ml.ml_line_count;
1296 else
1297 lnum = wp->w_topline;
1298 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001299 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001300 int len;
1301 int w_width = wp->w_width;
1302
1303 // Count Tabs for what they are worth and compute the length based on
1304 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001305 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001306 if (wp->w_width < maxwidth)
1307 wp->w_width = maxwidth;
1308 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001309 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001310 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001311
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001312 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001313 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001314 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315 {
1316 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001317 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001318 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001319 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 }
1321 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001322 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001323 && allow_adjust_left
1324 && (wp->w_popup_pos == POPPOS_TOPLEFT
1325 || wp->w_popup_pos == POPPOS_BOTLEFT))
1326 {
1327 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001328 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001329
Bram Moolenaar51c31312019-06-15 22:27:23 +02001330 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001331 {
1332 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001333
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001334 len -= truncate_shift;
1335 shift_by -= truncate_shift;
1336 }
1337
1338 wp->w_wincol -= shift_by;
1339 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001340 wp->w_width = maxwidth;
1341 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001342 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001343 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001344 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001345 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1346 wp->w_width = wp->w_maxwidth;
1347 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001348
1349 if (wp->w_firstline < 0)
1350 --lnum;
1351 else
1352 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001353
1354 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001355 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001356 && (wp->w_firstline >= 0
1357 ? lnum - wp->w_topline
1358 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001359 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001360 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001361 }
1362
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001363 if (wp->w_firstline < 0)
1364 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1365
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001366 wp->w_has_scrollbar = wp->w_want_scrollbar
1367 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001368#ifdef FEAT_TERMINAL
1369 if (wp->w_buffer->b_term != NULL)
1370 // Terminal window never has a scrollbar, adjusts to window height.
1371 wp->w_has_scrollbar = FALSE;
1372#endif
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001373 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001374 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001375 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001376 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001377 // make space for the scrollbar if needed, when lines wrap and when
1378 // applying minwidth
1379 if (maxwidth + right_extra >= maxspace
1380 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1381 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001382 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001383
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001384 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1385 {
1386 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1387
1388 if (minwidth < title_len)
1389 minwidth = title_len;
1390 }
1391
1392 if (minwidth > 0 && wp->w_width < minwidth)
1393 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001394 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001395 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001396 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001397 // some columns cut off on the right
1398 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001399 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001400 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001401 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001402 {
1403 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1404 if (wp->w_wincol < 0)
1405 wp->w_wincol = 0;
1406 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001407 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1408 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1409 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001410 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001411
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001412 // Right aligned: move to the right if needed.
1413 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001414 if (leftoff >= 0)
1415 wp->w_wincol = leftoff;
1416 else if (wp->w_popup_fixed)
1417 {
1418 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001419 // columns when displaying the window, minus left border and
1420 // padding.
1421 if (-leftoff > left_extra)
1422 wp->w_leftcol = -leftoff - left_extra;
1423 wp->w_width -= wp->w_leftcol;
1424 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001425 if (wp->w_width < 0)
1426 wp->w_width = 0;
1427 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001428 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001429
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001430 if (wp->w_p_wrap || (!wp->w_popup_fixed
1431 && (wp->w_popup_pos == POPPOS_TOPLEFT
1432 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001433 {
1434 int want_col = 0;
1435
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001436 // try to show the right border and any scrollbar
1437 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001438 if (want_col > 0 && wp->w_wincol > 0
1439 && wp->w_wincol + want_col >= Columns)
1440 {
1441 wp->w_wincol = Columns - want_col;
1442 if (wp->w_wincol < 0)
1443 wp->w_wincol = 0;
1444 }
1445 }
1446
Bram Moolenaar8d241042019-06-12 23:40:01 +02001447 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1448 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001449 if (minheight > 0 && wp->w_height < minheight)
1450 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001451 if (maxheight > 0 && wp->w_height > maxheight)
1452 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001453 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001454 if (wp->w_height > Rows - wp->w_winrow)
1455 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001456
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001457 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001458 {
1459 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1460 if (wp->w_winrow < 0)
1461 wp->w_winrow = 0;
1462 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001463 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001464 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001465 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001466 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001467 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001468 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001469 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1470 {
1471 // Bottom aligned but does not fit, and less space on the other
1472 // side or "posinvert" is off: reduce height.
1473 wp->w_winrow = 0;
1474 wp->w_height = wantline - extra_height;
1475 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001476 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001477 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001478 // Not enough space and more space on the other side: make top
1479 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001480 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001481 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001482 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001483 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001484 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1485 || wp->w_popup_pos == POPPOS_TOPLEFT)
1486 {
1487 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1488 && wantline * 2 > Rows
1489 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001490 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001491 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001492 // make bottom aligned and recompute the height
1493 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001494 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001495 if (wp->w_winrow < 0)
1496 {
1497 wp->w_height += wp->w_winrow;
1498 wp->w_winrow = 0;
1499 }
1500 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001501 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001502 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001503 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001504 adjust_height_for_top_aligned = TRUE;
1505 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001506 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001507
1508 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1509 && wp->w_winrow + wp->w_height + extra_height > Rows)
1510 {
1511 // Bottom of the popup goes below the last line, reduce the height and
1512 // add a scrollbar.
1513 wp->w_height = Rows - wp->w_winrow - extra_height;
1514#ifdef FEAT_TERMINAL
1515 if (wp->w_buffer->b_term == NULL)
1516#endif
1517 wp->w_has_scrollbar = TRUE;
1518 }
1519
1520 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001521 if (wp->w_winrow >= Rows)
1522 wp->w_winrow = Rows - 1;
1523 else if (wp->w_winrow < 0)
1524 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001525
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001526 if (wp->w_height != org_height)
1527 win_comp_scroll(wp);
1528
Bram Moolenaar17146962019-05-30 00:12:11 +02001529 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001530 if (win_valid(wp->w_popup_prop_win))
1531 {
1532 wp->w_popup_prop_changedtick =
1533 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1534 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1535 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001536
1537 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001538 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001539 if (org_winrow != wp->w_winrow
1540 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001541 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001542 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001543 || org_width != wp->w_width
1544 || org_height != wp->w_height)
1545 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001546 redraw_win_later(wp, NOT_VALID);
1547 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1548 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001549 popup_mask_refresh = TRUE;
1550 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001551}
1552
Bram Moolenaar17627312019-06-02 19:53:44 +02001553typedef enum
1554{
1555 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001556 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001557 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001558 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001559 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001560 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001561 TYPE_PREVIEW, // preview window
1562 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001563} create_type_T;
1564
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001565/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001566 * Make "buf" empty and set the contents to "text".
1567 * Used by popup_create() and popup_settext().
1568 */
1569 static void
1570popup_set_buffer_text(buf_T *buf, typval_T text)
1571{
1572 int lnum;
1573
1574 // Clear the buffer, then replace the lines.
1575 curbuf = buf;
1576 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001577 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001578 curbuf = curwin->w_buffer;
1579
1580 // Add text to the buffer.
1581 if (text.v_type == VAR_STRING)
1582 {
1583 // just a string
1584 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1585 }
1586 else
1587 {
1588 list_T *l = text.vval.v_list;
1589
1590 if (l->lv_len > 0)
1591 {
1592 if (l->lv_first->li_tv.v_type == VAR_STRING)
1593 // list of strings
1594 add_popup_strings(buf, l);
1595 else
1596 // list of dictionaries
1597 add_popup_dicts(buf, l);
1598 }
1599 }
1600
1601 // delete the line that was in the empty buffer
1602 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001603 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001604 curbuf = curwin->w_buffer;
1605}
1606
1607/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001608 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1609 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001610 * Return FAIL if the parsing fails.
1611 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001612 static int
1613parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001614{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001615 char_u *p =
1616#ifdef FEAT_QUICKFIX
1617 !is_preview ? p_cpp :
1618#endif
1619 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001620
Bram Moolenaar258cef52019-08-21 17:29:29 +02001621 if (wp != NULL)
1622 wp->w_popup_flags &= ~POPF_INFO_MENU;
1623
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001624 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001625 {
1626 char_u *e, *dig;
1627 char_u *s = p;
1628 int x;
1629
1630 e = vim_strchr(p, ':');
1631 if (e == NULL || e[1] == NUL)
1632 return FAIL;
1633
1634 p = vim_strchr(e, ',');
1635 if (p == NULL)
1636 p = e + STRLEN(e);
1637 dig = e + 1;
1638 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001639
1640 if (STRNCMP(s, "height:", 7) == 0)
1641 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001642 if (dig != p)
1643 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001644 if (wp != NULL)
1645 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001646 if (is_preview)
1647 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001648 wp->w_maxheight = x;
1649 }
1650 }
1651 else if (STRNCMP(s, "width:", 6) == 0)
1652 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001653 if (dig != p)
1654 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001655 if (wp != NULL)
1656 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001657 if (is_preview)
1658 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001659 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001660 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001661 }
1662 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001663 else if (STRNCMP(s, "highlight:", 10) == 0)
1664 {
1665 if (wp != NULL)
1666 {
1667 int c = *p;
1668
1669 *p = NUL;
1670 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1671 s + 10, OPT_FREE|OPT_LOCAL, 0);
1672 *p = c;
1673 }
1674 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001675 else if (STRNCMP(s, "border:", 7) == 0)
1676 {
1677 char_u *arg = s + 7;
1678 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1679 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1680 int i;
1681
1682 if (!on && !off)
1683 return FAIL;
1684 if (wp != NULL)
1685 {
1686 for (i = 0; i < 4; ++i)
1687 wp->w_popup_border[i] = on ? 1 : 0;
1688 if (off)
1689 // only show the X for close when there is a border
1690 wp->w_popup_close = POPCLOSE_NONE;
1691 }
1692 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001693 else if (STRNCMP(s, "align:", 6) == 0)
1694 {
1695 char_u *arg = s + 6;
1696 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1697 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1698
1699 if (!menu && !item)
1700 return FAIL;
1701 if (wp != NULL && menu)
1702 wp->w_popup_flags |= POPF_INFO_MENU;
1703 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001704 else
1705 return FAIL;
1706 }
1707 return OK;
1708}
1709
1710/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001711 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1712 * is not NULL.
1713 * Return FAIL if the parsing fails.
1714 */
1715 int
1716parse_previewpopup(win_T *wp)
1717{
1718 return parse_popup_option(wp, TRUE);
1719}
1720
1721/*
1722 * Parse the 'completepopup' option and apply the values to window "wp" if it
1723 * is not NULL.
1724 * Return FAIL if the parsing fails.
1725 */
1726 int
1727parse_completepopup(win_T *wp)
1728{
1729 return parse_popup_option(wp, FALSE);
1730}
1731
1732/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001733 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001734 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001735 */
1736 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001737popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001738{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001739 poppos_T ppt = POPPOS_NONE;
1740
1741 if (d != NULL)
1742 ppt = get_pos_entry(d, FALSE);
1743
Bram Moolenaar79648732019-07-18 21:43:07 +02001744 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001745 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001746 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001747 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1748 }
1749 else
1750 {
1751 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1752 if (wp->w_wantline == 0) // cursor in first line
1753 {
1754 wp->w_wantline = 2;
1755 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1756 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1757 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001758 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001759
Bram Moolenaar79648732019-07-18 21:43:07 +02001760 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001761 if (wp->w_wantcol > Columns - width)
1762 {
1763 wp->w_wantcol = Columns - width;
1764 if (wp->w_wantcol < 1)
1765 wp->w_wantcol = 1;
1766 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001767
Bram Moolenaar79648732019-07-18 21:43:07 +02001768 popup_adjust_position(wp);
1769}
1770
1771/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001772 * Set w_wantline and w_wantcol for the a given screen position.
1773 * Caller must take care of running into the window border.
1774 */
1775 void
1776popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1777{
1778 wp->w_wantline = row;
1779 wp->w_wantcol = col;
1780 popup_adjust_position(wp);
1781}
1782
1783/*
1784 * Add a border and lef&right padding.
1785 */
1786 static void
1787add_border_left_right_padding(win_T *wp)
1788{
1789 int i;
1790
1791 for (i = 0; i < 4; ++i)
1792 {
1793 wp->w_popup_border[i] = 1;
1794 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1795 }
1796}
1797
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001798#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001799/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001800 * Return TRUE if there is any popup window with a terminal buffer.
1801 */
1802 static int
1803popup_terminal_exists(void)
1804{
1805 win_T *wp;
1806 tabpage_T *tp;
1807
1808 FOR_ALL_POPUPWINS(wp)
1809 if (wp->w_buffer->b_term != NULL)
1810 return TRUE;
1811 FOR_ALL_TABPAGES(tp)
1812 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1813 if (wp->w_buffer->b_term != NULL)
1814 return TRUE;
1815 return FALSE;
1816}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001817#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001818
1819/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001820 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001821 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001822 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001823 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001824 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001825 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001826popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001827{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001828 win_T *wp;
1829 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001830 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001831 int new_buffer;
1832 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001833 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001834 int nr;
1835 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001836
Bram Moolenaar79648732019-07-18 21:43:07 +02001837 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001838 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001839 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001840 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001841 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001842 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001843 if (buf == NULL)
1844 {
1845 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1846 return NULL;
1847 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001848#ifdef FEAT_TERMINAL
1849 if (buf->b_term != NULL && popup_terminal_exists())
1850 {
1851 emsg(_("E861: Cannot open a second popup with a terminal"));
1852 return NULL;
1853 }
1854#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001855 }
1856 else if (!(argvars[0].v_type == VAR_STRING
1857 && argvars[0].vval.v_string != NULL)
1858 && !(argvars[0].v_type == VAR_LIST
1859 && argvars[0].vval.v_list != NULL))
1860 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001861 emsg(_("E450: buffer number, text or a list required"));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001862 return NULL;
1863 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001864 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001865 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001866 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001867 return NULL;
1868 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001869 d = argvars[1].vval.v_dict;
1870 }
1871
1872 if (d != NULL)
1873 {
1874 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1875 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1876 else if (type == TYPE_NOTIFICATION)
1877 tabnr = -1; // notifications are global by default
1878 else
1879 tabnr = 0;
1880 if (tabnr > 0)
1881 {
1882 tp = find_tabpage(tabnr);
1883 if (tp == NULL)
1884 {
1885 semsg(_("E997: Tabpage not found: %d"), tabnr);
1886 return NULL;
1887 }
1888 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001889 }
1890
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001891 // Create the window and buffer.
1892 wp = win_alloc_popup_win();
1893 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001894 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001895 if (rettv != NULL)
1896 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001897 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001898 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001899
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001900 if (buf != NULL)
1901 {
1902 // use existing buffer
1903 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001904 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001905 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001906 buffer_ensure_loaded(buf);
1907 }
1908 else
1909 {
1910 // create a new buffer associated with the popup
1911 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001912 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001913 if (buf == NULL)
1914 return NULL;
1915 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001916
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001917 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001918
Bram Moolenaar46451042019-08-24 15:50:46 +02001919 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001920 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001921 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001922 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001923 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001924 buf->b_p_ul = -1; // no undo
1925 buf->b_p_swf = FALSE; // no swap file
1926 buf->b_p_bl = FALSE; // unlisted buffer
1927 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001928
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001929 // Avoid that 'buftype' is reset when this buffer is entered.
1930 buf->b_p_initialized = TRUE;
1931 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001932 wp->w_p_wrap = TRUE; // 'wrap' is default on
1933 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001934
Bram Moolenaara3fce622019-06-20 02:31:49 +02001935 if (tp != NULL)
1936 {
1937 // popup on specified tab page
1938 wp->w_next = tp->tp_first_popupwin;
1939 tp->tp_first_popupwin = wp;
1940 }
1941 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001942 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001943 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001944 wp->w_next = curtab->tp_first_popupwin;
1945 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001946 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001947 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001948 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001949 win_T *prev = first_popupwin;
1950
1951 // Global popup: add at the end, so that it gets displayed on top of
1952 // older ones with the same zindex. Matters for notifications.
1953 if (first_popupwin == NULL)
1954 first_popupwin = wp;
1955 else
1956 {
1957 while (prev->w_next != NULL)
1958 prev = prev->w_next;
1959 prev->w_next = wp;
1960 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001961 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001962
Bram Moolenaar79648732019-07-18 21:43:07 +02001963 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001964 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001965
Bram Moolenaar79648732019-07-18 21:43:07 +02001966 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001967 {
1968 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001969 }
1970 if (type == TYPE_ATCURSOR)
1971 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001972 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001973 set_moved_values(wp);
1974 set_moved_columns(wp, FIND_STRING);
1975 }
1976
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001977 if (type == TYPE_BEVAL)
1978 {
1979 wp->w_popup_pos = POPPOS_BOTLEFT;
1980
1981 // by default use the mouse position
1982 wp->w_wantline = mouse_row;
1983 if (wp->w_wantline <= 0) // mouse on first line
1984 {
1985 wp->w_wantline = 2;
1986 wp->w_popup_pos = POPPOS_TOPLEFT;
1987 }
1988 wp->w_wantcol = mouse_col + 1;
1989 set_mousemoved_values(wp);
1990 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1991 }
1992
Bram Moolenaar33796b32019-06-08 16:01:13 +02001993 // set default values
1994 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001995 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001996
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001997 if (type == TYPE_NOTIFICATION)
1998 {
1999 win_T *twp, *nextwin;
2000 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002001
2002 // Try to not overlap with another global popup. Guess we need 3
2003 // more screen lines than buffer lines.
2004 wp->w_wantline = 1;
2005 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2006 {
2007 nextwin = twp->w_next;
2008 if (twp != wp
2009 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2010 && twp->w_winrow <= wp->w_wantline - 1 + height
2011 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2012 {
2013 // move to below this popup and restart the loop to check for
2014 // overlap with other popups
2015 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2016 nextwin = first_popupwin;
2017 }
2018 }
2019 if (wp->w_wantline + height > Rows)
2020 {
2021 // can't avoid overlap, put on top in the hope that message goes
2022 // away soon.
2023 wp->w_wantline = 1;
2024 }
2025
2026 wp->w_wantcol = 10;
2027 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002028 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002029 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002030 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002031 for (i = 0; i < 4; ++i)
2032 wp->w_popup_border[i] = 1;
2033 wp->w_popup_padding[1] = 1;
2034 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002035
2036 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002037 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002038 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
2039 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002040 }
2041
Bram Moolenaara730e552019-06-16 19:05:31 +02002042 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002043 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002044 wp->w_popup_pos = POPPOS_CENTER;
2045 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002046 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002047 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002048 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002049 }
2050
Bram Moolenaara730e552019-06-16 19:05:31 +02002051 if (type == TYPE_MENU)
2052 {
2053 typval_T tv;
2054 callback_T callback;
2055
2056 tv.v_type = VAR_STRING;
2057 tv.vval.v_string = (char_u *)"popup_filter_menu";
2058 callback = get_callback(&tv);
2059 if (callback.cb_name != NULL)
2060 set_callback(&wp->w_filter_cb, &callback);
2061
2062 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002063 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002064 }
2065
Bram Moolenaar79648732019-07-18 21:43:07 +02002066 if (type == TYPE_PREVIEW)
2067 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002068 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002069 wp->w_popup_close = POPCLOSE_BUTTON;
2070 for (i = 0; i < 4; ++i)
2071 wp->w_popup_border[i] = 1;
2072 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002073 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002074 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002075# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002076 if (type == TYPE_INFO)
2077 {
2078 wp->w_popup_pos = POPPOS_TOPLEFT;
2079 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2080 wp->w_popup_close = POPCLOSE_BUTTON;
2081 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002082 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002083 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002084# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002085
Bram Moolenaarae943152019-06-16 22:54:14 +02002086 for (i = 0; i < 4; ++i)
2087 VIM_CLEAR(wp->w_border_highlight[i]);
2088 for (i = 0; i < 8; ++i)
2089 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002090 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002091 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002092 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002093
Bram Moolenaar79648732019-07-18 21:43:07 +02002094 if (d != NULL)
2095 // Deal with options.
2096 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002097
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002098#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002099 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2100 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002101#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002102
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002103 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002104
2105 wp->w_vsep_width = 0;
2106
2107 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002108 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002109
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002110#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002111 // When running a terminal in the popup it becomes the current window.
2112 if (buf->b_term != NULL)
2113 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002114#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002115
Bram Moolenaara730e552019-06-16 19:05:31 +02002116 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002117}
2118
2119/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002120 * popup_clear()
2121 */
2122 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002123f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002124{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002125 int force = FALSE;
2126
2127 if (argvars[0].v_type != VAR_UNKNOWN)
2128 force = (int)tv_get_number(&argvars[0]);
2129 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002130}
2131
2132/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002133 * popup_create({text}, {options})
2134 */
2135 void
2136f_popup_create(typval_T *argvars, typval_T *rettv)
2137{
Bram Moolenaar17627312019-06-02 19:53:44 +02002138 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002139}
2140
2141/*
2142 * popup_atcursor({text}, {options})
2143 */
2144 void
2145f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2146{
Bram Moolenaar17627312019-06-02 19:53:44 +02002147 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002148}
2149
2150/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002151 * popup_beval({text}, {options})
2152 */
2153 void
2154f_popup_beval(typval_T *argvars, typval_T *rettv)
2155{
2156 popup_create(argvars, rettv, TYPE_BEVAL);
2157}
2158
2159/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002160 * Invoke the close callback for window "wp" with value "result".
2161 * Careful: The callback may make "wp" invalid!
2162 */
2163 static void
2164invoke_popup_callback(win_T *wp, typval_T *result)
2165{
2166 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002167 typval_T argv[3];
2168
2169 argv[0].v_type = VAR_NUMBER;
2170 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2171
2172 if (result != NULL && result->v_type != VAR_UNKNOWN)
2173 copy_tv(result, &argv[1]);
2174 else
2175 {
2176 argv[1].v_type = VAR_NUMBER;
2177 argv[1].vval.v_number = 0;
2178 }
2179
2180 argv[2].v_type = VAR_UNKNOWN;
2181
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002182 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002183 if (result != NULL)
2184 clear_tv(&argv[1]);
2185 clear_tv(&rettv);
2186}
2187
2188/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002189 * Make "prevwin" the current window, unless it's equal to "wp".
2190 * Otherwise make "firstwin" the current window.
2191 */
2192 static void
2193back_to_prevwin(win_T *wp)
2194{
2195 if (win_valid(prevwin) && wp != prevwin)
2196 win_enter(prevwin, FALSE);
2197 else
2198 win_enter(firstwin, FALSE);
2199}
2200
2201/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002202 * Close popup "wp" and invoke any close callback for it.
2203 */
2204 static void
2205popup_close_and_callback(win_T *wp, typval_T *arg)
2206{
2207 int id = wp->w_id;
2208
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002209#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002210 if (wp == curwin && curbuf->b_term != NULL)
2211 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002212 win_T *owp;
2213
2214 // Closing popup window with a terminal: put focus back on the first
2215 // that works:
2216 // - another popup window with a terminal
2217 // - the previous window
2218 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002219 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002220 if (owp != curwin && owp->w_buffer->b_term != NULL)
2221 break;
2222 if (owp != NULL)
2223 win_enter(owp, FALSE);
2224 else
2225 {
2226 for (owp = curtab->tp_first_popupwin; owp != NULL;
2227 owp = owp->w_next)
2228 if (owp != curwin && owp->w_buffer->b_term != NULL)
2229 break;
2230 if (owp != NULL)
2231 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002232 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002233 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002234 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002235 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002236#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002237
Bram Moolenaar49540192019-12-11 19:34:54 +01002238 // Just in case a check higher up is missing.
2239 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
2240 return;
2241
Bram Moolenaarcee52202020-03-11 14:19:58 +01002242 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002243 if (wp->w_close_cb.cb_name != NULL)
2244 // Careful: This may make "wp" invalid.
2245 invoke_popup_callback(wp, arg);
2246
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002247 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002248 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002249}
2250
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002251 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002252popup_close_with_retval(win_T *wp, int retval)
2253{
2254 typval_T res;
2255
2256 res.v_type = VAR_NUMBER;
2257 res.vval.v_number = retval;
2258 popup_close_and_callback(wp, &res);
2259}
2260
Bram Moolenaar3397f742019-06-02 18:40:06 +02002261/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002262 * Close popup "wp" because of a mouse click.
2263 */
2264 void
2265popup_close_for_mouse_click(win_T *wp)
2266{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002267 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002268}
2269
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002270 static void
2271check_mouse_moved(win_T *wp, win_T *mouse_wp)
2272{
2273 // Close the popup when all if these are true:
2274 // - the mouse is not on this popup
2275 // - "mousemoved" was used
2276 // - the mouse is no longer on the same screen row or the mouse column is
2277 // outside of the relevant text
2278 if (wp != mouse_wp
2279 && wp->w_popup_mouse_row != 0
2280 && (wp->w_popup_mouse_row != mouse_row
2281 || mouse_col < wp->w_popup_mouse_mincol
2282 || mouse_col > wp->w_popup_mouse_maxcol))
2283 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002284 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002285 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002286 }
2287}
2288
2289/*
2290 * Called when the mouse moved: may close a popup with "mousemoved".
2291 */
2292 void
2293popup_handle_mouse_moved(void)
2294{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002295 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002296 win_T *mouse_wp;
2297 int row = mouse_row;
2298 int col = mouse_col;
2299
2300 // find the window where the mouse is in
2301 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2302
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002303 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2304 {
2305 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002306 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002307 }
2308 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2309 {
2310 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002311 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002312 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002313}
2314
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002315/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002316 * In a filter: check if the typed key is a mouse event that is used for
2317 * dragging the popup.
2318 */
2319 static void
2320filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2321{
2322 int row = mouse_row;
2323 int col = mouse_col;
2324
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002325 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002326 && is_mouse_key(c)
2327 && (wp == popup_dragwin
2328 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2329 // do not consume the key, allow for dragging the popup
2330 rettv->vval.v_number = 0;
2331}
2332
Bram Moolenaara730e552019-06-16 19:05:31 +02002333/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002334 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002335 */
2336 void
2337f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2338{
2339 int id = tv_get_number(&argvars[0]);
2340 win_T *wp = win_id2wp(id);
2341 char_u *key = tv_get_string(&argvars[1]);
2342 typval_T res;
2343 int c;
2344 linenr_T old_lnum;
2345
2346 // If the popup has been closed do not consume the key.
2347 if (wp == NULL)
2348 return;
2349
2350 c = *key;
2351 if (c == K_SPECIAL && key[1] != NUL)
2352 c = TO_SPECIAL(key[1], key[2]);
2353
2354 // consume all keys until done
2355 rettv->vval.v_number = 1;
2356 res.v_type = VAR_NUMBER;
2357
2358 old_lnum = wp->w_cursor.lnum;
2359 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2360 --wp->w_cursor.lnum;
2361 if ((c == 'j' || c == 'J' || c == K_DOWN)
2362 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2363 ++wp->w_cursor.lnum;
2364 if (old_lnum != wp->w_cursor.lnum)
2365 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002366 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002367 return;
2368 }
2369
2370 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2371 {
2372 // Cancelled, invoke callback with -1
2373 res.vval.v_number = -1;
2374 popup_close_and_callback(wp, &res);
2375 return;
2376 }
2377 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2378 {
2379 // Invoke callback with current index.
2380 res.vval.v_number = wp->w_cursor.lnum;
2381 popup_close_and_callback(wp, &res);
2382 return;
2383 }
2384
2385 filter_handle_drag(wp, c, rettv);
2386}
2387
2388/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002389 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002390 */
2391 void
2392f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2393{
2394 int id = tv_get_number(&argvars[0]);
2395 win_T *wp = win_id2wp(id);
2396 char_u *key = tv_get_string(&argvars[1]);
2397 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002398 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002399
2400 // If the popup has been closed don't consume the key.
2401 if (wp == NULL)
2402 return;
2403
Bram Moolenaara730e552019-06-16 19:05:31 +02002404 c = *key;
2405 if (c == K_SPECIAL && key[1] != NUL)
2406 c = TO_SPECIAL(key[1], key[2]);
2407
Bram Moolenaara42d9452019-06-15 21:46:30 +02002408 // consume all keys until done
2409 rettv->vval.v_number = 1;
2410
Bram Moolenaara730e552019-06-16 19:05:31 +02002411 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002412 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002413 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002414 res.vval.v_number = 0;
2415 else
2416 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002417 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002418 return;
2419 }
2420
2421 // Invoke callback
2422 res.v_type = VAR_NUMBER;
2423 popup_close_and_callback(wp, &res);
2424}
2425
2426/*
2427 * popup_dialog({text}, {options})
2428 */
2429 void
2430f_popup_dialog(typval_T *argvars, typval_T *rettv)
2431{
2432 popup_create(argvars, rettv, TYPE_DIALOG);
2433}
2434
2435/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002436 * popup_menu({text}, {options})
2437 */
2438 void
2439f_popup_menu(typval_T *argvars, typval_T *rettv)
2440{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002441 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002442}
2443
2444/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002445 * popup_notification({text}, {options})
2446 */
2447 void
2448f_popup_notification(typval_T *argvars, typval_T *rettv)
2449{
2450 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2451}
2452
2453/*
2454 * Find the popup window with window-ID "id".
2455 * If the popup window does not exist NULL is returned.
2456 * If the window is not a popup window, and error message is given.
2457 */
2458 static win_T *
2459find_popup_win(int id)
2460{
2461 win_T *wp = win_id2wp(id);
2462
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002463 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002464 {
2465 semsg(_("E993: window %d is not a popup window"), id);
2466 return NULL;
2467 }
2468 return wp;
2469}
2470
2471/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002472 * popup_close({id})
2473 */
2474 void
2475f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2476{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002477 int id = (int)tv_get_number(argvars);
Bram Moolenaar49540192019-12-11 19:34:54 +01002478 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002479
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002480 if (
2481# ifdef FEAT_TERMINAL
2482 // if the popup contains a terminal it will become hidden
2483 curbuf->b_term == NULL &&
2484# endif
2485 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002486 return;
2487
2488 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002489 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002490 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002491}
2492
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002493 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002494popup_hide(win_T *wp)
2495{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002496#ifdef FEAT_TERMINAL
2497 if (error_if_term_popup_window())
2498 return;
2499#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002500 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2501 {
2502 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002503 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002504 redraw_all_later(NOT_VALID);
2505 popup_mask_refresh = TRUE;
2506 }
2507}
2508
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002509/*
2510 * popup_hide({id})
2511 */
2512 void
2513f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2514{
2515 int id = (int)tv_get_number(argvars);
2516 win_T *wp = find_popup_win(id);
2517
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002518 if (wp != NULL)
2519 popup_hide(wp);
2520}
2521
2522 void
2523popup_show(win_T *wp)
2524{
2525 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002526 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002527 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002528 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002529 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002530 }
2531}
2532
2533/*
2534 * popup_show({id})
2535 */
2536 void
2537f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2538{
2539 int id = (int)tv_get_number(argvars);
2540 win_T *wp = find_popup_win(id);
2541
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002542 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002543 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002544 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002545#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002546 if (wp->w_popup_flags & POPF_INFO)
2547 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002548#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002549 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002550}
2551
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002552/*
2553 * popup_settext({id}, {text})
2554 */
2555 void
2556f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2557{
2558 int id = (int)tv_get_number(&argvars[0]);
2559 win_T *wp = find_popup_win(id);
2560
2561 if (wp != NULL)
2562 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002563 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2564 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2565 else
2566 {
2567 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002568 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002569 popup_adjust_position(wp);
2570 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002571 }
2572}
2573
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002574 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002575popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002576{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002577 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002578 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002579 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002580 clear_cmdline = TRUE;
2581 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002582
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002583 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002584 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002585}
2586
Bram Moolenaar82106932020-03-13 14:34:38 +01002587 static void
2588error_for_popup_window(void)
2589{
2590 emsg(_("E994: Not allowed in a popup window"));
2591}
2592
2593 int
2594error_if_popup_window(int also_with_term UNUSED)
2595{
2596 // win_execute() may set "curwin" to a popup window temporarily, but many
2597 // commands are disallowed then. When a terminal runs in the popup most
2598 // things are allowed. When a terminal is finished it can be closed.
2599 if (WIN_IS_POPUP(curwin)
2600# ifdef FEAT_TERMINAL
2601 && (also_with_term || curbuf->b_term == NULL)
2602 && !term_is_finished(curbuf)
2603# endif
2604 )
2605 {
2606 error_for_popup_window();
2607 return TRUE;
2608 }
2609 return FALSE;
2610}
2611
Bram Moolenaarec583842019-05-26 14:11:23 +02002612/*
2613 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002614 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002615 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002616 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002617 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002618popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002619{
2620 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002621 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002622 win_T *prev = NULL;
2623
Bram Moolenaarec583842019-05-26 14:11:23 +02002624 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002625 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002626 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002627 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002628 if (wp == curwin)
2629 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002630 if (!force)
2631 {
2632 error_for_popup_window();
2633 return FAIL;
2634 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002635 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002636 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002637 if (prev == NULL)
2638 first_popupwin = wp->w_next;
2639 else
2640 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002641 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002642 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002643 }
2644
Bram Moolenaarec583842019-05-26 14:11:23 +02002645 // go through tab-local popups
2646 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002647 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002648 return OK;
2649 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002650}
2651
2652/*
2653 * Close a popup window with Window-id "id" in tabpage "tp".
2654 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002655 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002656popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002657{
2658 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002659 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002660 win_T *prev = NULL;
2661
Bram Moolenaarec583842019-05-26 14:11:23 +02002662 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2663 if (wp->w_id == id)
2664 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002665 if (wp == curwin)
2666 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002667 if (!force)
2668 {
2669 error_for_popup_window();
2670 return FAIL;
2671 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002672 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002673 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002674 if (prev == NULL)
2675 *root = wp->w_next;
2676 else
2677 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002678 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002679 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002680 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002681 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002682}
2683
2684 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002685close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002686{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002687 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002688 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002689 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002690 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002691 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002692 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002693 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002694 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002695}
2696
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002697/*
2698 * popup_move({id}, {options})
2699 */
2700 void
2701f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2702{
Bram Moolenaarae943152019-06-16 22:54:14 +02002703 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002704 int id = (int)tv_get_number(argvars);
2705 win_T *wp = find_popup_win(id);
2706
2707 if (wp == NULL)
2708 return; // invalid {id}
2709
2710 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2711 {
2712 emsg(_(e_dictreq));
2713 return;
2714 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002715 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002716
Bram Moolenaarae943152019-06-16 22:54:14 +02002717 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002718
2719 if (wp->w_winrow + wp->w_height >= cmdline_row)
2720 clear_cmdline = TRUE;
2721 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002722}
2723
Bram Moolenaarbc133542019-05-29 20:26:46 +02002724/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002725 * popup_setoptions({id}, {options})
2726 */
2727 void
2728f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2729{
2730 dict_T *dict;
2731 int id = (int)tv_get_number(argvars);
2732 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002733 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002734
2735 if (wp == NULL)
2736 return; // invalid {id}
2737
2738 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2739 {
2740 emsg(_(e_dictreq));
2741 return;
2742 }
2743 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002744 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002745
2746 apply_move_options(wp, dict);
2747 apply_general_options(wp, dict);
2748
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002749 if (old_firstline != wp->w_firstline)
2750 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002751 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002752 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002753 popup_adjust_position(wp);
2754}
2755
2756/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002757 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002758 */
2759 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002760f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002761{
2762 dict_T *dict;
2763 int id = (int)tv_get_number(argvars);
2764 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002765 int top_extra;
2766 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002767
2768 if (rettv_dict_alloc(rettv) == OK)
2769 {
2770 if (wp == NULL)
2771 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002772 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002773 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2774
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002775 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002776 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002777 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002778
Bram Moolenaarbc133542019-05-29 20:26:46 +02002779 dict_add_number(dict, "line", wp->w_winrow + 1);
2780 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002781 dict_add_number(dict, "width", wp->w_width + left_extra
2782 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2783 dict_add_number(dict, "height", wp->w_height + top_extra
2784 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002785
2786 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2787 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2788 dict_add_number(dict, "core_width", wp->w_width);
2789 dict_add_number(dict, "core_height", wp->w_height);
2790
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002791 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002792 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002793 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002794 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002795 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002796
2797 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002798 }
2799}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02002800
2801/*
2802 * popup_list()
2803 */
2804 void
2805f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
2806{
2807 win_T *wp;
2808 tabpage_T *tp;
2809
2810 if (rettv_list_alloc(rettv) != OK)
2811 return;
2812 FOR_ALL_POPUPWINS(wp)
2813 list_append_number(rettv->vval.v_list, wp->w_id);
2814 FOR_ALL_TABPAGES(tp)
2815 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
2816 list_append_number(rettv->vval.v_list, wp->w_id);
2817}
2818
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002819/*
2820 * popup_locate({row}, {col})
2821 */
2822 void
2823f_popup_locate(typval_T *argvars, typval_T *rettv)
2824{
2825 int row = tv_get_number(&argvars[0]) - 1;
2826 int col = tv_get_number(&argvars[1]) - 1;
2827 win_T *wp;
2828
2829 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01002830 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002831 rettv->vval.v_number = wp->w_id;
2832}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002833
2834/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002835 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2836 */
2837 static void
2838get_padding_border(dict_T *dict, int *array, char *name)
2839{
2840 list_T *list;
2841 int i;
2842
2843 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2844 return;
2845
2846 list = list_alloc();
2847 if (list != NULL)
2848 {
2849 dict_add_list(dict, name, list);
2850 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2851 for (i = 0; i < 4; ++i)
2852 list_append_number(list, array[i]);
2853 }
2854}
2855
2856/*
2857 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2858 */
2859 static void
2860get_borderhighlight(dict_T *dict, win_T *wp)
2861{
2862 list_T *list;
2863 int i;
2864
2865 for (i = 0; i < 4; ++i)
2866 if (wp->w_border_highlight[i] != NULL)
2867 break;
2868 if (i == 4)
2869 return;
2870
2871 list = list_alloc();
2872 if (list != NULL)
2873 {
2874 dict_add_list(dict, "borderhighlight", list);
2875 for (i = 0; i < 4; ++i)
2876 list_append_string(list, wp->w_border_highlight[i], -1);
2877 }
2878}
2879
2880/*
2881 * For popup_getoptions(): add a "borderchars" entry to "dict".
2882 */
2883 static void
2884get_borderchars(dict_T *dict, win_T *wp)
2885{
2886 list_T *list;
2887 int i;
2888 char_u buf[NUMBUFLEN];
2889 int len;
2890
2891 for (i = 0; i < 8; ++i)
2892 if (wp->w_border_char[i] != 0)
2893 break;
2894 if (i == 8)
2895 return;
2896
2897 list = list_alloc();
2898 if (list != NULL)
2899 {
2900 dict_add_list(dict, "borderchars", list);
2901 for (i = 0; i < 8; ++i)
2902 {
2903 len = mb_char2bytes(wp->w_border_char[i], buf);
2904 list_append_string(list, buf, len);
2905 }
2906 }
2907}
2908
2909/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002910 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002911 */
2912 static void
2913get_moved_list(dict_T *dict, win_T *wp)
2914{
2915 list_T *list;
2916
2917 list = list_alloc();
2918 if (list != NULL)
2919 {
2920 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002921 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002922 list_append_number(list, wp->w_popup_mincol);
2923 list_append_number(list, wp->w_popup_maxcol);
2924 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002925 list = list_alloc();
2926 if (list != NULL)
2927 {
2928 dict_add_list(dict, "mousemoved", list);
2929 list_append_number(list, wp->w_popup_mouse_row);
2930 list_append_number(list, wp->w_popup_mouse_mincol);
2931 list_append_number(list, wp->w_popup_mouse_maxcol);
2932 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002933}
2934
2935/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002936 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002937 */
2938 void
2939f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2940{
2941 dict_T *dict;
2942 int id = (int)tv_get_number(argvars);
2943 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002944 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002945 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002946
2947 if (rettv_dict_alloc(rettv) == OK)
2948 {
2949 if (wp == NULL)
2950 return;
2951
2952 dict = rettv->vval.v_dict;
2953 dict_add_number(dict, "line", wp->w_wantline);
2954 dict_add_number(dict, "col", wp->w_wantcol);
2955 dict_add_number(dict, "minwidth", wp->w_minwidth);
2956 dict_add_number(dict, "minheight", wp->w_minheight);
2957 dict_add_number(dict, "maxheight", wp->w_maxheight);
2958 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002959 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002960 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002961 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002962 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002963 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2964 {
2965 proptype_T *pt = text_prop_type_by_id(
2966 wp->w_popup_prop_win->w_buffer,
2967 wp->w_popup_prop_type);
2968
2969 if (pt != NULL)
2970 dict_add_string(dict, "textprop", pt->pt_name);
2971 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2972 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2973 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002974 dict_add_string(dict, "title", wp->w_popup_title);
2975 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002976 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002977 dict_add_number(dict, "mapping",
2978 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002979 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002980 dict_add_number(dict, "posinvert",
2981 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002982 dict_add_number(dict, "cursorline",
2983 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002984 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002985 if (wp->w_scrollbar_highlight != NULL)
2986 dict_add_string(dict, "scrollbarhighlight",
2987 wp->w_scrollbar_highlight);
2988 if (wp->w_thumb_highlight != NULL)
2989 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002990
Bram Moolenaara3fce622019-06-20 02:31:49 +02002991 // find the tabpage that holds this popup
2992 i = 1;
2993 FOR_ALL_TABPAGES(tp)
2994 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002995 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002996
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002997 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
2998 if (twp->w_id == id)
2999 break;
3000 if (twp != NULL)
3001 break;
3002 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003003 }
3004 if (tp == NULL)
3005 i = -1; // must be global
3006 else if (tp == curtab)
3007 i = 0;
3008 dict_add_number(dict, "tabpage", i);
3009
Bram Moolenaarae943152019-06-16 22:54:14 +02003010 get_padding_border(dict, wp->w_popup_padding, "padding");
3011 get_padding_border(dict, wp->w_popup_border, "border");
3012 get_borderhighlight(dict, wp);
3013 get_borderchars(dict, wp);
3014 get_moved_list(dict, wp);
3015
3016 if (wp->w_filter_cb.cb_name != NULL)
3017 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3018 if (wp->w_close_cb.cb_name != NULL)
3019 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003020
3021 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
3022 ++i)
3023 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3024 {
3025 dict_add_string(dict, "pos",
3026 (char_u *)poppos_entries[i].pp_name);
3027 break;
3028 }
3029
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003030 dict_add_string(dict, "close", (char_u *)(
3031 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3032 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3033
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003034# if defined(FEAT_TIMERS)
3035 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3036 ? (long)wp->w_popup_timer->tr_interval : 0L);
3037# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003038 }
3039}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003040
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003041# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003042/*
3043 * Return TRUE if the current window is running a terminal in a popup window.
3044 * Return FALSE when the job has ended.
3045 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003046 int
3047error_if_term_popup_window()
3048{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003049 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3050 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003051 {
Bram Moolenaara5edb672020-02-03 22:58:48 +01003052 emsg(_("E863: Not allowed for a terminal in a popup window"));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003053 return TRUE;
3054 }
3055 return FALSE;
3056}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003057# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003058
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003059/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003060 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003061 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003062 * Each calling function should use a different flag, see the list at
3063 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003064 */
3065 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003066popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003067{
3068 win_T *wp;
3069
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003070 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003071 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003072 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003073 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003074}
3075
3076/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003077 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003078 * Must have called popup_reset_handled() first.
3079 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3080 * popup with the highest zindex.
3081 */
3082 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003083find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003084{
3085 win_T *wp;
3086 win_T *found_wp;
3087 int found_zindex;
3088
3089 found_zindex = lowest ? INT_MAX : 0;
3090 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003091 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003092 if ((wp->w_popup_handled & handled_flag) == 0
3093 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003094 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003095 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003096 {
3097 found_zindex = wp->w_zindex;
3098 found_wp = wp;
3099 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003100 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003101 if ((wp->w_popup_handled & handled_flag) == 0
3102 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003103 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003104 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003105 {
3106 found_zindex = wp->w_zindex;
3107 found_wp = wp;
3108 }
3109
3110 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003111 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003112 return found_wp;
3113}
3114
3115/*
3116 * Invoke the filter callback for window "wp" with typed character "c".
3117 * Uses the global "mod_mask" for modifiers.
3118 * Returns the return value of the filter.
3119 * Careful: The filter may make "wp" invalid!
3120 */
3121 static int
3122invoke_popup_filter(win_T *wp, int c)
3123{
3124 int res;
3125 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003126 typval_T argv[3];
3127 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003128 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003129
Bram Moolenaara42d9452019-06-15 21:46:30 +02003130 // Emergency exit: CTRL-C closes the popup.
3131 if (c == Ctrl_C)
3132 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003133 int save_got_int = got_int;
3134
3135 // Reset got_int to avoid the callback isn't called.
3136 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003137 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003138 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003139 return 1;
3140 }
3141
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003142 argv[0].v_type = VAR_NUMBER;
3143 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3144
3145 // Convert the number to a string, so that the function can use:
3146 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003147 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003148 argv[1].v_type = VAR_STRING;
3149 argv[1].vval.v_string = vim_strsave(buf);
3150
3151 argv[2].v_type = VAR_UNKNOWN;
3152
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003153 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003154 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003155 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003156 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003157 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003158
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003159 vim_free(argv[1].vval.v_string);
3160 clear_tv(&rettv);
3161 return res;
3162}
3163
3164/*
3165 * Called when "c" was typed: invoke popup filter callbacks.
3166 * Returns TRUE when the character was consumed,
3167 */
3168 int
3169popup_do_filter(int c)
3170{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003171 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003172 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003173 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003174 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003175 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003176 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003177
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003178#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003179 // Popup window with terminal always gets focus.
3180 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3181 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003182#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003183
Bram Moolenaar934470e2019-09-01 23:27:05 +02003184 if (recursive)
3185 return FALSE;
3186 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003187
Bram Moolenaarf6396232019-08-24 19:36:00 +02003188 if (c == K_LEFTMOUSE)
3189 {
3190 int row = mouse_row;
3191 int col = mouse_col;
3192
3193 wp = mouse_find_win(&row, &col, FIND_POPUP);
3194 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003195 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003196 }
3197
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003198 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003199 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003200 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003201 if (wp->w_filter_cb.cb_name != NULL
3202 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003203 res = invoke_popup_filter(wp, c);
3204
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003205 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003206 {
3207 int save_got_int = got_int;
3208
3209 // Reset got_int to avoid a function used in the statusline aborts.
3210 got_int = FALSE;
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003211 redraw_after_callback(FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003212 got_int |= save_got_int;
3213 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003214 recursive = FALSE;
3215 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003216 return res;
3217}
3218
Bram Moolenaar3397f742019-06-02 18:40:06 +02003219/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003220 * Return TRUE if there is a popup visible with a filter callback and the
3221 * "mapping" property off.
3222 */
3223 int
3224popup_no_mapping(void)
3225{
3226 int round;
3227 win_T *wp;
3228
3229 for (round = 1; round <= 2; ++round)
3230 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3231 wp != NULL; wp = wp->w_next)
3232 if (wp->w_filter_cb.cb_name != NULL
3233 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3234 return TRUE;
3235 return FALSE;
3236}
3237
3238/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003239 * Called when the cursor moved: check if any popup needs to be closed if the
3240 * cursor moved far enough.
3241 */
3242 void
3243popup_check_cursor_pos()
3244{
3245 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003246
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003247 popup_reset_handled(POPUP_HANDLED_3);
3248 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003249 if (wp->w_popup_curwin != NULL
3250 && (curwin != wp->w_popup_curwin
3251 || curwin->w_cursor.lnum != wp->w_popup_lnum
3252 || curwin->w_cursor.col < wp->w_popup_mincol
3253 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003254 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003255}
3256
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003257/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003258 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003259 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003260 static void
3261popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003262{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003263 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003264 char_u *cells;
3265 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003266
3267 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003268 return;
3269 if (wp->w_popup_mask_cells != NULL
3270 && wp->w_popup_mask_height == height
3271 && wp->w_popup_mask_width == width)
3272 return; // cache is still valid
3273
3274 vim_free(wp->w_popup_mask_cells);
3275 wp->w_popup_mask_cells = alloc_clear(width * height);
3276 if (wp->w_popup_mask_cells == NULL)
3277 return;
3278 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003279
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003280 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003281 {
3282 int cols, cole;
3283 int lines, linee;
3284
3285 li = lio->li_tv.vval.v_list->lv_first;
3286 cols = tv_get_number(&li->li_tv);
3287 if (cols < 0)
3288 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003289 li = li->li_next;
3290 cole = tv_get_number(&li->li_tv);
3291 if (cole < 0)
3292 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003293 li = li->li_next;
3294 lines = tv_get_number(&li->li_tv);
3295 if (lines < 0)
3296 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003297 li = li->li_next;
3298 linee = tv_get_number(&li->li_tv);
3299 if (linee < 0)
3300 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003301
3302 for (row = lines - 1; row < linee && row < height; ++row)
3303 for (col = cols - 1; col < cole && col < width; ++col)
3304 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003305 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003306}
3307
3308/*
3309 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3310 * "col" and "line" are screen coordinates.
3311 */
3312 static int
3313popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3314{
3315 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3316 int line = screenline - wp->w_winrow;
3317
3318 return col >= 0 && col < width
3319 && line >= 0 && line < height
3320 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003321}
3322
3323/*
3324 * Set flags in popup_transparent[] for window "wp" to "val".
3325 */
3326 static void
3327update_popup_transparent(win_T *wp, int val)
3328{
3329 if (wp->w_popup_mask != NULL)
3330 {
3331 int width = popup_width(wp);
3332 int height = popup_height(wp);
3333 listitem_T *lio, *li;
3334 int cols, cole;
3335 int lines, linee;
3336 int col, line;
3337
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003338 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003339 {
3340 li = lio->li_tv.vval.v_list->lv_first;
3341 cols = tv_get_number(&li->li_tv);
3342 if (cols < 0)
3343 cols = width + cols + 1;
3344 li = li->li_next;
3345 cole = tv_get_number(&li->li_tv);
3346 if (cole < 0)
3347 cole = width + cole + 1;
3348 li = li->li_next;
3349 lines = tv_get_number(&li->li_tv);
3350 if (lines < 0)
3351 lines = height + lines + 1;
3352 li = li->li_next;
3353 linee = tv_get_number(&li->li_tv);
3354 if (linee < 0)
3355 linee = height + linee + 1;
3356
3357 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003358 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003359 if (cols < 0)
3360 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003361 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003362 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003363 if (lines < 0)
3364 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003365 for (line = lines; line < linee
3366 && line + wp->w_winrow < screen_Rows; ++line)
3367 for (col = cols; col < cole
3368 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003369 popup_transparent[(line + wp->w_winrow) * screen_Columns
3370 + col + wp->w_wincol] = val;
3371 }
3372 }
3373}
3374
3375/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003376 * Only called when popup window "wp" is hidden: If the window is positioned
3377 * next to a text property, and it is now visible, then unhide the popup.
3378 * We don't check if visible popups become hidden, that is done in
3379 * popup_adjust_position().
3380 * Return TRUE if the popup became unhidden.
3381 */
3382 static int
3383check_popup_unhidden(win_T *wp)
3384{
3385 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3386 {
3387 textprop_T prop;
3388 linenr_T lnum;
3389
3390 if (find_visible_prop(wp->w_popup_prop_win,
3391 wp->w_popup_prop_type, wp->w_popup_prop_id,
3392 &prop, &lnum) == OK)
3393 {
3394 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003395 wp->w_popup_prop_topline = 0; // force repositioning
3396 return TRUE;
3397 }
3398 }
3399 return FALSE;
3400}
3401
3402/*
3403 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3404 * That is when the buffer in the popup was changed, or the popup is following
3405 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003406 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003407 */
3408 static int
3409popup_need_position_adjust(win_T *wp)
3410{
3411 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3412 return TRUE;
3413 if (win_valid(wp->w_popup_prop_win))
3414 return wp->w_popup_prop_changedtick
3415 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003416 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3417 || ((wp->w_popup_flags & POPF_CURSORLINE)
3418 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003419 return FALSE;
3420}
3421
3422/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003423 * Update "popup_mask" if needed.
3424 * Also recomputes the popup size and positions.
3425 * Also updates "popup_visible".
3426 * Also marks window lines for redrawing.
3427 */
3428 void
3429may_update_popup_mask(int type)
3430{
3431 win_T *wp;
3432 short *mask;
3433 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003434 int redraw_all_popups = FALSE;
3435 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003436
3437 // Need to recompute when switching tabs.
3438 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3439 // (such as the screen size) must have changed.
3440 if (popup_mask_tab != curtab || type >= NOT_VALID)
3441 {
3442 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003443 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003444 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003445
3446 // Check if any popup window buffer has changed and if any popup connected
3447 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003448 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003449 if (wp->w_popup_flags & POPF_HIDDEN)
3450 popup_mask_refresh |= check_popup_unhidden(wp);
3451 else if (popup_need_position_adjust(wp))
3452 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003453 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003454 if (wp->w_popup_flags & POPF_HIDDEN)
3455 popup_mask_refresh |= check_popup_unhidden(wp);
3456 else if (popup_need_position_adjust(wp))
3457 popup_mask_refresh = TRUE;
3458
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003459 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003460 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003461
3462 // Need to update the mask, something has changed.
3463 popup_mask_refresh = FALSE;
3464 popup_mask_tab = curtab;
3465 popup_visible = FALSE;
3466
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003467 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003468 // If redrawing only what is needed, update "popup_mask_next" and then
3469 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003470 redrawing_all_win = TRUE;
3471 FOR_ALL_WINDOWS(wp)
3472 if (wp->w_redr_type < SOME_VALID)
3473 redrawing_all_win = FALSE;
3474 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003475 mask = popup_mask;
3476 else
3477 mask = popup_mask_next;
3478 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3479
3480 // Find the window with the lowest zindex that hasn't been handled yet,
3481 // so that the window with a higher zindex overwrites the value in
3482 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003483 popup_reset_handled(POPUP_HANDLED_4);
3484 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003485 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003486 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003487 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003488
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003489 popup_visible = TRUE;
3490
Bram Moolenaar12034e22019-08-25 22:25:02 +02003491 // Recompute the position if the text changed. It may make the popup
3492 // hidden if it's attach to a text property that is no longer visible.
3493 if (redraw_all_popups || popup_need_position_adjust(wp))
3494 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003495 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003496 if (wp->w_popup_flags & POPF_HIDDEN)
3497 continue;
3498 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003499
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003500 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003501 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003502 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003503 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003504 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003505 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003506 col < wp->w_wincol + width - wp->w_popup_leftoff
3507 && col < screen_Columns; ++col)
3508 if (wp->w_popup_mask_cells == NULL
3509 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003510 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003511 }
3512
3513 // Only check which lines are to be updated if not already
3514 // updating all lines.
3515 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003516 {
3517 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3518 win_T *prev_wp = NULL;
3519
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003520 for (line = 0; line < screen_Rows; ++line)
3521 {
3522 int col_done = 0;
3523
3524 for (col = 0; col < screen_Columns; ++col)
3525 {
3526 int off = line * screen_Columns + col;
3527
3528 if (popup_mask[off] != popup_mask_next[off])
3529 {
3530 popup_mask[off] = popup_mask_next[off];
3531
3532 if (line >= cmdline_row)
3533 {
3534 // the command line needs to be cleared if text below
3535 // the popup is now visible.
3536 if (!msg_scrolled && popup_mask_next[off] == 0)
3537 clear_cmdline = TRUE;
3538 }
3539 else if (col >= col_done)
3540 {
3541 linenr_T lnum;
3542 int line_cp = line;
3543 int col_cp = col;
3544
3545 // The screen position "line" / "col" needs to be
3546 // redrawn. Figure out what window that is and update
3547 // w_redraw_top and w_redr_bot. Only needs to be done
3548 // once for each window line.
3549 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3550 if (wp != NULL)
3551 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003552#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003553 // A terminal window needs to be redrawn.
3554 if (bt_terminal(wp->w_buffer))
3555 redraw_win_later(wp, NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003556 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003557#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003558 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003559 if (wp != prev_wp)
3560 {
3561 vim_memset(plines_cache, 0,
3562 sizeof(int) * Rows);
3563 prev_wp = wp;
3564 }
3565
3566 if (line_cp >= wp->w_height)
3567 // In (or below) status line
3568 wp->w_redr_status = TRUE;
3569 else
3570 {
3571 // compute the position in the buffer line
3572 // from the position in the window
3573 mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003574 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003575 redrawWinline(wp, lnum);
3576 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003577 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003578
3579 // This line is going to be redrawn, no need to
3580 // check until the right side of the window.
3581 col_done = wp->w_wincol + wp->w_width - 1;
3582 }
3583 }
3584 }
3585 }
3586 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003587
3588 vim_free(plines_cache);
3589 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003590}
3591
3592/*
3593 * Return a string of "len" spaces in IObuff.
3594 */
3595 static char_u *
3596get_spaces(int len)
3597{
3598 vim_memset(IObuff, ' ', (size_t)len);
3599 IObuff[len] = NUL;
3600 return IObuff;
3601}
3602
3603/*
3604 * Update popup windows. They are drawn on top of normal windows.
3605 * "win_update" is called for each popup window, lowest zindex first.
3606 */
3607 void
3608update_popups(void (*win_update)(win_T *wp))
3609{
3610 win_T *wp;
3611 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003612 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003613 int total_width;
3614 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003615 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003616 int popup_attr;
3617 int border_attr[4];
3618 int border_char[8];
3619 char_u buf[MB_MAXBYTES];
3620 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003621 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003622 int padcol = 0;
3623 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003624 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003625 int sb_thumb_top = 0;
3626 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003627 int attr_scroll = 0;
3628 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003629
3630 // Find the window with the lowest zindex that hasn't been updated yet,
3631 // so that the window with a higher zindex is drawn later, thus goes on
3632 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003633 popup_reset_handled(POPUP_HANDLED_5);
3634 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003635 {
3636 // This drawing uses the zindex of the popup window, so that it's on
3637 // top of the text but doesn't draw when another popup with higher
3638 // zindex is on top of the character.
3639 screen_zindex = wp->w_zindex;
3640
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003641 // Set flags in popup_transparent[] for masked cells.
3642 update_popup_transparent(wp, 1);
3643
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003644 // adjust w_winrow and w_wincol for border and padding, since
3645 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003646 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003647 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3648 - wp->w_popup_leftoff;
3649 if (wp->w_wincol + left_extra < 0)
3650 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003651 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003652 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003653
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003654 // Draw the popup text, unless it's off screen.
3655 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003656 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003657 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003658
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003659 // move the cursor into the visible lines, otherwise executing
3660 // commands with win_execute() may cause the text to jump.
3661 if (wp->w_cursor.lnum < wp->w_topline)
3662 wp->w_cursor.lnum = wp->w_topline;
3663 else if (wp->w_cursor.lnum >= wp->w_botline)
3664 wp->w_cursor.lnum = wp->w_botline - 1;
3665 }
3666
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003667 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003668 wp->w_wincol -= left_extra;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003669 // cursor position matters in terminal in job mode
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003670#ifdef FEAT_TERMINAL
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003671 if (wp != curwin || !term_in_normal_mode())
Bram Moolenaar57c732e2020-02-28 22:51:54 +01003672#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003673 {
3674 wp->w_wrow += top_off;
3675 wp->w_wcol += left_extra;
3676 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003677
Bram Moolenaard529ba52019-07-02 23:13:53 +02003678 total_width = popup_width(wp);
3679 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003680 popup_attr = get_wcr_attr(wp);
3681
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003682 if (wp->w_winrow + total_height > cmdline_row)
3683 wp->w_popup_flags |= POPF_ON_CMDLINE;
3684 else
3685 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3686
3687
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003688 // We can only use these line drawing characters when 'encoding' is
3689 // "utf-8" and 'ambiwidth' is "single".
3690 if (enc_utf8 && *p_ambw == 's')
3691 {
3692 border_char[0] = border_char[2] = 0x2550;
3693 border_char[1] = border_char[3] = 0x2551;
3694 border_char[4] = 0x2554;
3695 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003696 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3697 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003698 border_char[7] = 0x255a;
3699 }
3700 else
3701 {
3702 border_char[0] = border_char[2] = '-';
3703 border_char[1] = border_char[3] = '|';
3704 for (i = 4; i < 8; ++i)
3705 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003706 if (wp->w_popup_flags & POPF_RESIZE)
3707 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003708 }
3709 for (i = 0; i < 8; ++i)
3710 if (wp->w_border_char[i] != 0)
3711 border_char[i] = wp->w_border_char[i];
3712
3713 for (i = 0; i < 4; ++i)
3714 {
3715 border_attr[i] = popup_attr;
3716 if (wp->w_border_highlight[i] != NULL)
3717 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3718 }
3719
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003720 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003721 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003722 if (wp->w_popup_border[0] > 0)
3723 {
3724 // top border
3725 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003726 wincol < 0 ? 0 : wincol, wincol + total_width,
3727 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003728 ? border_char[4] : border_char[0],
3729 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003730 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003731 {
3732 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3733 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003734 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003735 }
3736 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003737 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3738 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739
Bram Moolenaard529ba52019-07-02 23:13:53 +02003740 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3741 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003742 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003743 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3744 - wp->w_has_scrollbar;
3745 if (padcol < 0)
3746 {
3747 padwidth += padcol;
3748 padcol = 0;
3749 }
3750 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003751 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003752 {
3753 // top padding
3754 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003755 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003756 ' ', ' ', popup_attr);
3757 }
3758
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003759 // Title goes on top of border or padding.
3760 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003761 {
3762 int len = (int)STRLEN(wp->w_popup_title) + 1;
3763 char_u *title = alloc(len);
3764
3765 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3766 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003767 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003768 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003769 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003770
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003771 // Compute scrollbar thumb position and size.
3772 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003773 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003774 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3775 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003776
Bram Moolenaar076d9882019-09-14 22:23:29 +02003777 sb_thumb_height = (height * height + linecount / 2) / linecount;
3778 if (wp->w_topline > 1 && sb_thumb_height == height)
3779 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003780 if (sb_thumb_height == 0)
3781 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003782 if (linecount <= wp->w_height)
3783 // it just fits, avoid divide by zero
3784 sb_thumb_top = 0;
3785 else
3786 sb_thumb_top = (wp->w_topline - 1
3787 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003788 * (wp->w_height - sb_thumb_height)
3789 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003790 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3791 sb_thumb_top = 1; // show it's scrolled
3792
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003793 if (wp->w_scrollbar_highlight != NULL)
3794 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3795 else
3796 attr_scroll = highlight_attr[HLF_PSB];
3797 if (wp->w_thumb_highlight != NULL)
3798 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3799 else
3800 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003801 }
3802
3803 for (i = wp->w_popup_border[0];
3804 i < total_height - wp->w_popup_border[2]; ++i)
3805 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003806 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003807 // left and right padding only needed next to the body
3808 int do_padding =
3809 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3810 && i < total_height - wp->w_popup_border[2]
3811 - wp->w_popup_padding[2];
3812
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003813 row = wp->w_winrow + i;
3814
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003815 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003816 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003817 {
3818 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003819 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003820 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003821 if (do_padding && wp->w_popup_padding[3] > 0)
3822 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003823 int col = wincol + wp->w_popup_border[3];
3824
Bram Moolenaard529ba52019-07-02 23:13:53 +02003825 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003826 pad_left = wp->w_popup_padding[3];
3827 if (col < 0)
3828 {
3829 pad_left += col;
3830 col = 0;
3831 }
3832 if (pad_left > 0)
3833 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3834 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003835 // scrollbar
3836 if (wp->w_has_scrollbar)
3837 {
3838 int line = i - top_off;
3839 int scroll_col = wp->w_wincol + total_width - 1
3840 - wp->w_popup_border[1];
3841
3842 if (line >= 0 && line < wp->w_height)
3843 screen_putchar(' ', row, scroll_col,
3844 line >= sb_thumb_top
3845 && line < sb_thumb_top + sb_thumb_height
3846 ? attr_thumb : attr_scroll);
3847 else
3848 screen_putchar(' ', row, scroll_col, popup_attr);
3849 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003850 // right border
3851 if (wp->w_popup_border[1] > 0)
3852 {
3853 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003854 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003855 }
3856 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003857 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003858 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003859 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003860 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3861 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003862 }
3863
3864 if (wp->w_popup_padding[2] > 0)
3865 {
3866 // bottom padding
3867 row = wp->w_winrow + wp->w_popup_border[0]
3868 + wp->w_popup_padding[0] + wp->w_height;
3869 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003870 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003871 }
3872
3873 if (wp->w_popup_border[2] > 0)
3874 {
3875 // bottom border
3876 row = wp->w_winrow + total_height - 1;
3877 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003878 wincol < 0 ? 0 : wincol,
3879 wincol + total_width,
3880 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003881 ? border_char[7] : border_char[2],
3882 border_char[2], border_attr[2]);
3883 if (wp->w_popup_border[1] > 0)
3884 {
3885 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003886 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003887 }
3888 }
3889
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003890 if (wp->w_popup_close == POPCLOSE_BUTTON)
3891 {
3892 // close button goes on top of anything at the top-right corner
3893 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003894 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003895 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3896 }
3897
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003898 update_popup_transparent(wp, 0);
3899
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003900 // Back to the normal zindex.
3901 screen_zindex = 0;
3902 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02003903
3904#if defined(FEAT_SEARCH_EXTRA)
3905 // In case win_update() called start_search_hl().
3906 end_search_hl();
3907#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003908}
3909
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003910/*
3911 * Mark references in callbacks of one popup window.
3912 */
3913 static int
3914set_ref_in_one_popup(win_T *wp, int copyID)
3915{
3916 int abort = FALSE;
3917 typval_T tv;
3918
3919 if (wp->w_close_cb.cb_partial != NULL)
3920 {
3921 tv.v_type = VAR_PARTIAL;
3922 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3923 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3924 }
3925 if (wp->w_filter_cb.cb_partial != NULL)
3926 {
3927 tv.v_type = VAR_PARTIAL;
3928 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3929 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3930 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003931 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003932 return abort;
3933}
3934
3935/*
3936 * Set reference in callbacks of popup windows.
3937 */
3938 int
3939set_ref_in_popups(int copyID)
3940{
3941 int abort = FALSE;
3942 win_T *wp;
3943 tabpage_T *tp;
3944
3945 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3946 abort = abort || set_ref_in_one_popup(wp, copyID);
3947
3948 FOR_ALL_TABPAGES(tp)
3949 {
3950 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3951 abort = abort || set_ref_in_one_popup(wp, copyID);
3952 if (abort)
3953 break;
3954 }
3955 return abort;
3956}
Bram Moolenaar79648732019-07-18 21:43:07 +02003957
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003958 int
3959popup_is_popup(win_T *wp)
3960{
3961 return wp->w_popup_flags != 0;
3962}
3963
3964#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003965/*
3966 * Find an existing popup used as the preview window, in the current tab page.
3967 * Return NULL if not found.
3968 */
3969 win_T *
3970popup_find_preview_window(void)
3971{
3972 win_T *wp;
3973
3974 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003975 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02003976 if (wp->w_p_pvw)
3977 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003978 return NULL;
3979}
3980
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003981/*
3982 * Find an existing popup used as the info window, in the current tab page.
3983 * Return NULL if not found.
3984 */
3985 win_T *
3986popup_find_info_window(void)
3987{
3988 win_T *wp;
3989
3990 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003991 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003992 if (wp->w_popup_flags & POPF_INFO)
3993 return wp;
3994 return NULL;
3995}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003996#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003997
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003998 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003999f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4000{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004001#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004002 win_T *wp = popup_find_info_window();
4003
4004 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004005#else
4006 rettv->vval.v_number = 0;
4007#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004008}
4009
4010 void
4011f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004012{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004013#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004014 win_T *wp = popup_find_preview_window();
4015
4016 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004017#else
4018 rettv->vval.v_number = 0;
4019#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004020}
4021
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004022#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004023/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004024 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004025 * NOTE: this makes the popup the current window, so that the file can be
4026 * edited. However, it must not remain to be the current window, the caller
4027 * must make sure of that.
4028 */
4029 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004030popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004031{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004032 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004033
4034 if (wp == NULL)
4035 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004036 if (info)
4037 wp->w_popup_flags |= POPF_INFO;
4038 else
4039 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004040
4041 // Set the width to a reasonable value, so that w_topline can be computed.
4042 if (wp->w_minwidth > 0)
4043 wp->w_width = wp->w_minwidth;
4044 else if (wp->w_maxwidth > 0)
4045 wp->w_width = wp->w_maxwidth;
4046 else
4047 wp->w_width = curwin->w_width;
4048
4049 // Will switch to another buffer soon, dummy one can be wiped.
4050 wp->w_buffer->b_locked = FALSE;
4051
4052 win_enter(wp, FALSE);
4053 return OK;
4054}
4055
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004056/*
4057 * Close any preview popup.
4058 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004059 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004060popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004061{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004062 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004063
4064 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004065 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004066}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004067
4068/*
4069 * Hide the info popup.
4070 */
4071 void
4072popup_hide_info(void)
4073{
4074 win_T *wp = popup_find_info_window();
4075
4076 if (wp != NULL)
4077 popup_hide(wp);
4078}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004079
4080/*
4081 * Close any info popup.
4082 */
4083 void
4084popup_close_info(void)
4085{
4086 win_T *wp = popup_find_info_window();
4087
4088 if (wp != NULL)
4089 popup_close_with_retval(wp, -1);
4090}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004091#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004092
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004093/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004094 * Close any popup for a text property associated with "win".
4095 * Return TRUE if a popup was closed.
4096 */
4097 int
4098popup_win_closed(win_T *win)
4099{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004100 int round;
4101 win_T *wp;
4102 win_T *next;
4103 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004104
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004105 for (round = 1; round <= 2; ++round)
4106 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4107 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004108 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004109 next = wp->w_next;
4110 if (wp->w_popup_prop_win == win)
4111 {
4112 popup_close_with_retval(wp, -1);
4113 ret = TRUE;
4114 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004115 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004116 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004117}
4118
4119/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004120 * Set the title of the popup window to the file name.
4121 */
4122 void
4123popup_set_title(win_T *wp)
4124{
4125 if (wp->w_buffer->b_fname != NULL)
4126 {
4127 char_u dirname[MAXPATHL];
4128 size_t len;
4129
4130 mch_dirname(dirname, MAXPATHL);
4131 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4132
4133 vim_free(wp->w_popup_title);
4134 len = STRLEN(wp->w_buffer->b_fname) + 3;
4135 wp->w_popup_title = alloc((int)len);
4136 if (wp->w_popup_title != NULL)
4137 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4138 wp->w_buffer->b_fname);
4139 redraw_win_later(wp, VALID);
4140 }
4141}
4142
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004143# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004144/*
4145 * If there is a preview window, update the title.
4146 * Used after changing directory.
4147 */
4148 void
4149popup_update_preview_title(void)
4150{
4151 win_T *wp = popup_find_preview_window();
4152
4153 if (wp != NULL)
4154 popup_set_title(wp);
4155}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004156# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004157
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004158#endif // FEAT_PROP_POPUP