blob: 8e74e4201023fac33274b30698506bcbdbb77418 [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 Moolenaar43568642022-08-28 13:02:45 +010031#ifdef HAS_MESSAGE_WINDOW
32// Window used for messages when 'winheight' is zero.
33static win_T *message_win = NULL;
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void popup_adjust_position(win_T *wp);
37
Bram Moolenaar4d784b22019-05-25 19:51:39 +020038/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020039 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020041 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020042 */
43 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020044popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020045{
46 dictitem_T *di;
47 char_u *val;
48 char_u *s;
49 char_u *endp;
50 int n = 0;
51
52 di = dict_find(dict, key, -1);
53 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020054 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055
56 val = tv_get_string(&di->di_tv);
57 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059
60 setcursor_mayforce(TRUE);
61 s = val + 6;
62 if (*s != NUL)
63 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020064 endp = s;
65 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
66 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067 if (endp != NULL && *skipwhite(endp) != NUL)
68 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020069 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020070 return 0;
71 }
72 }
73
74 if (STRCMP(key, "line") == 0)
75 n = screen_screenrow() + 1 + n;
76 else // "col"
77 n = screen_screencol() + 1 + n;
78
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020079 // Zero means "not set", use -1 instead.
80 if (n == 0)
81 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020082 return n;
83}
84
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020085 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020086set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020087{
88 dictitem_T *di;
89
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020090 di = dict_find(dict, (char_u *)name, -1);
91 if (di != NULL)
92 {
93 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +000094 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020095 else
96 {
97 list_T *list = di->di_tv.vval.v_list;
98 listitem_T *li;
99 int i;
100 int nr;
101
102 for (i = 0; i < 4; ++i)
103 array[i] = 1;
104 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100105 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200106 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200107 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
108 ++i, li = li->li_next)
109 {
110 nr = (int)tv_get_number(&li->li_tv);
111 if (nr >= 0)
112 array[i] = nr > max_val ? max_val : nr;
113 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100114 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200115 }
116 }
117}
118
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200119/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200120 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200121 */
122 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200123set_moved_values(win_T *wp)
124{
125 wp->w_popup_curwin = curwin;
126 wp->w_popup_lnum = curwin->w_cursor.lnum;
127 wp->w_popup_mincol = curwin->w_cursor.col;
128 wp->w_popup_maxcol = curwin->w_cursor.col;
129}
130
131/*
132 * Used when popup options contain "moved" with "word" or "WORD".
133 */
134 static void
135set_moved_columns(win_T *wp, int flags)
136{
137 char_u *ptr;
138 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
139
140 if (len > 0)
141 {
142 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
143 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
144 }
145}
146
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200147/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200148 * Used when popup options contain "mousemoved": set default moved values.
149 */
150 static void
151set_mousemoved_values(win_T *wp)
152{
153 wp->w_popup_mouse_row = mouse_row;
154 wp->w_popup_mouse_mincol = mouse_col;
155 wp->w_popup_mouse_maxcol = mouse_col;
156}
157
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000158 static void
159update_popup_uses_mouse_move(void)
160{
161 popup_uses_mouse_move = FALSE;
162 if (popup_visible)
163 {
164 win_T *wp;
165
166 FOR_ALL_POPUPWINS(wp)
167 if (wp->w_popup_mouse_row != 0)
168 {
169 popup_uses_mouse_move = TRUE;
170 return;
171 }
172 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
173 if (wp->w_popup_mouse_row != 0)
174 {
175 popup_uses_mouse_move = TRUE;
176 return;
177 }
178 }
179}
180
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200223 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
224 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200225 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200226 * Caller should check the left mouse button was clicked.
227 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200228 */
229 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200230popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200231{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200232 if (wp->w_popup_close == POPCLOSE_BUTTON
233 && row == 0 && col == popup_width(wp) - 1)
234 {
235 popup_close_for_mouse_click(wp);
236 return TRUE;
237 }
238 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200239}
240
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200241// Values set when dragging a popup window starts.
242static int drag_start_row;
243static int drag_start_col;
244static int drag_start_wantline;
245static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200246static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200247
248/*
249 * Mouse down on border of popup window: start dragging it.
250 * Uses mouse_col and mouse_row.
251 */
252 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200253popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254{
255 drag_start_row = mouse_row;
256 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200257 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 drag_start_wantline = wp->w_winrow + 1;
259 else
260 drag_start_wantline = wp->w_wantline;
261 if (wp->w_wantcol == 0)
262 drag_start_wantcol = wp->w_wincol + 1;
263 else
264 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200265
266 // Stop centering the popup
267 if (wp->w_popup_pos == POPPOS_CENTER)
268 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200269
270 drag_on_resize_handle = wp->w_popup_border[1] > 0
271 && wp->w_popup_border[2] > 0
272 && row == popup_height(wp) - 1
273 && col == popup_width(wp) - 1;
274
275 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
276 {
277 if (wp->w_popup_pos == POPPOS_TOPRIGHT
278 || wp->w_popup_pos == POPPOS_BOTRIGHT)
279 wp->w_wantcol = wp->w_wincol + 1;
280 if (wp->w_popup_pos == POPPOS_BOTLEFT)
281 wp->w_wantline = wp->w_winrow + 1;
282 wp->w_popup_pos = POPPOS_TOPLEFT;
283 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284}
285
286/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200287 * Mouse moved while dragging a popup window: adjust the window popup position
288 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200289 */
290 void
291popup_drag(win_T *wp)
292{
293 // The popup may be closed before dragging stops.
294 if (!win_valid_popup(wp))
295 return;
296
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200297 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
298 {
299 int width_inc = mouse_col - drag_start_col;
300 int height_inc = mouse_row - drag_start_row;
301
302 if (width_inc != 0)
303 {
304 int width = wp->w_width + width_inc;
305
306 if (width < 1)
307 width = 1;
308 wp->w_minwidth = width;
309 wp->w_maxwidth = width;
310 drag_start_col = mouse_col;
311 }
312
313 if (height_inc != 0)
314 {
315 int height = wp->w_height + height_inc;
316
317 if (height < 1)
318 height = 1;
319 wp->w_minheight = height;
320 wp->w_maxheight = height;
321 drag_start_row = mouse_row;
322 }
323
324 popup_adjust_position(wp);
325 return;
326 }
327
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000328 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200329 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200330 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
331 if (wp->w_wantline < 1)
332 wp->w_wantline = 1;
333 if (wp->w_wantline > Rows)
334 wp->w_wantline = Rows;
335 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
336 if (wp->w_wantcol < 1)
337 wp->w_wantcol = 1;
338 if (wp->w_wantcol > Columns)
339 wp->w_wantcol = Columns;
340
341 popup_adjust_position(wp);
342}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200343
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200344/*
345 * Set w_firstline to match the current "wp->w_topline".
346 */
347 void
348popup_set_firstline(win_T *wp)
349{
350 int height = wp->w_height;
351
352 wp->w_firstline = wp->w_topline;
353 popup_adjust_position(wp);
354
355 // we don't want the popup to get smaller, decrement the first line
356 // until it doesn't
357 while (wp->w_firstline > 1 && wp->w_height < height)
358 {
359 --wp->w_firstline;
360 popup_adjust_position(wp);
361 }
362}
363
364/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200365 * Return TRUE if the position is in the popup window scrollbar.
366 */
367 int
368popup_is_in_scrollbar(win_T *wp, int row, int col)
369{
370 return wp->w_has_scrollbar
371 && row >= wp->w_popup_border[0]
372 && row < popup_height(wp) - wp->w_popup_border[2]
373 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
374}
375
376
377/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200378 * Handle a click in a popup window, if it is in the scrollbar.
379 */
380 void
381popup_handle_scrollbar_click(win_T *wp, int row, int col)
382{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200383 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200384 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100385 int height = popup_height(wp);
386 int new_topline = wp->w_topline;
387
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 if (row >= height / 2)
389 {
390 // Click in lower half, scroll down.
391 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100392 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 }
394 else if (wp->w_topline > 1)
395 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100396 --new_topline;
397 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200398 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100399 set_topline(wp, new_topline);
400 if (wp == curwin)
401 {
402 if (wp->w_cursor.lnum < wp->w_topline)
403 {
404 wp->w_cursor.lnum = wp->w_topline;
405 check_cursor();
406 }
407 else if (wp->w_cursor.lnum >= wp->w_botline)
408 {
409 wp->w_cursor.lnum = wp->w_botline - 1;
410 check_cursor();
411 }
412 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200413 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200415 }
416 }
417}
418
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200419#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100420/*
421 * Add a timer to "wp" with "time".
422 * If "close" is true use popup_close(), otherwise popup_hide().
423 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200424 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100425popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200426{
427 char_u cbbuf[50];
428 char_u *ptr = cbbuf;
429 typval_T tv;
430
431 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
433 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200434 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200435 {
436 wp->w_popup_timer = create_timer(time, 0);
437 wp->w_popup_timer->tr_callback = get_callback(&tv);
438 clear_tv(&tv);
439 }
440}
441#endif
442
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 static poppos_T
444get_pos_entry(dict_T *d, int give_error)
445{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100446 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100447 int nr;
448
449 if (str == NULL)
450 return POPPOS_NONE;
451
K.Takataeeec2542021-06-02 13:28:16 +0200452 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
454 return poppos_entries[nr].pp_val;
455
456 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100458 return POPPOS_NONE;
459}
460
Bram Moolenaar17627312019-06-02 19:53:44 +0200461/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200462 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200463 */
464 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200465apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200467 int nr;
468 char_u *str;
469 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200470
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200472 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100473 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200474 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200476 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200478 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200479
480 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200481 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200482 wp->w_wantline = nr;
483 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200484 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200485 wp->w_wantcol = nr;
486
Bram Moolenaar55881332020-08-18 13:04:15 +0200487
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200489 if (nr != -1)
490 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200491
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100493 poppos_T ppt = get_pos_entry(d, TRUE);
494
495 if (ppt != POPPOS_NONE)
496 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497 }
498
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200500 if (str != NULL)
501 {
502 wp->w_popup_prop_type = 0;
503 if (*str != NUL)
504 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100505 wp->w_popup_prop_win = curwin;
506 di = dict_find(d, (char_u *)"textpropwin", -1);
507 if (di != NULL)
508 {
509 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100510 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100511 wp->w_popup_prop_win = curwin;
512 }
513
514 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 if (nr <= 0)
516 nr = find_prop_type_id(str, NULL);
517 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000518 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200519 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200520 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200521 }
522 }
523
524 di = dict_find(d, (char_u *)"textpropid", -1);
525 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100526 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200527}
528
Bram Moolenaarb0992022020-01-30 14:55:42 +0100529/*
530 * Handle "moved" and "mousemoved" arguments.
531 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200532 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200533handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
534{
535 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
536 {
537 char_u *s = di->di_tv.vval.v_string;
538 int flags = 0;
539
540 if (STRCMP(s, "word") == 0)
541 flags = FIND_IDENT | FIND_STRING;
542 else if (STRCMP(s, "WORD") == 0)
543 flags = FIND_STRING;
544 else if (STRCMP(s, "expr") == 0)
545 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
546 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000547 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200548 if (flags != 0)
549 {
550 if (mousemoved)
551 set_mousemoved_columns(wp, flags);
552 else
553 set_moved_columns(wp, flags);
554 }
555 }
556 else if (di->di_tv.v_type == VAR_LIST
557 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200558 && (di->di_tv.vval.v_list->lv_len == 2
559 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200560 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200561 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100562 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200563 int mincol;
564 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200565
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200566 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100567 li = l->lv_first;
568 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200569 {
570 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
571
572 // Three numbers, might be from popup_getoptions().
573 if (mousemoved)
574 wp->w_popup_mouse_row = nr;
575 else
576 wp->w_popup_lnum = nr;
577 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100578 if (nr == 0)
579 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200580 }
581
582 mincol = tv_get_number(&li->li_tv);
583 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200584 if (mousemoved)
585 {
586 wp->w_popup_mouse_mincol = mincol;
587 wp->w_popup_mouse_maxcol = maxcol;
588 }
589 else
590 {
591 wp->w_popup_mincol = mincol;
592 wp->w_popup_maxcol = maxcol;
593 }
594 }
595 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000596 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200597}
598
599 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200600check_highlight(dict_T *dict, char *name, char_u **pval)
601{
602 dictitem_T *di;
603 char_u *str;
604
605 di = dict_find(dict, (char_u *)name, -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000609 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200610 else
611 {
612 str = tv_get_string(&di->di_tv);
613 if (*str != NUL)
614 *pval = vim_strsave(str);
615 }
616 }
617}
618
Bram Moolenaarae943152019-06-16 22:54:14 +0200619/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200620 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200621 */
622 static void
623popup_show_curline(win_T *wp)
624{
625 if (wp->w_cursor.lnum < wp->w_topline)
626 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200627 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200628 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200629 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200630 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200631 if (wp->w_topline < 1)
632 wp->w_topline = 1;
633 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
634 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200635 while (wp->w_topline < wp->w_cursor.lnum
636 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
637 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
638 > wp->w_height)
639 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200640 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200641
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200642 // Don't let "firstline" cause a scroll.
643 if (wp->w_firstline > 0)
644 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200645}
646
647/*
648 * Get the sign group name for window "wp".
649 * Returns a pointer to a static buffer, overwritten on the next call.
650 */
651 static char_u *
652popup_get_sign_name(win_T *wp)
653{
654 static char buf[30];
655
656 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
657 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200658}
659
660/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200661 * Highlight the line with the cursor.
662 * Also scrolls the text to put the cursor line in view.
663 */
664 static void
665popup_highlight_curline(win_T *wp)
666{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200667 int sign_id = 0;
668 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200669
Bram Moolenaar72570732019-11-30 14:21:53 +0100670 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200671
672 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
673 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200674 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200675
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200676 if (!sign_exists_by_name(sign_name))
677 {
678 char *linehl = "PopupSelected";
679
680 if (syn_name2id((char_u *)linehl) == 0)
681 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100682 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
683 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200684 }
685
Bram Moolenaar72570732019-11-30 14:21:53 +0100686 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200687 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100688 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200690 else
691 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200692 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693}
694
695/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200696 * Shared between popup_create() and f_popup_setoptions().
697 */
698 static void
699apply_general_options(win_T *wp, dict_T *dict)
700{
701 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200702 int nr;
703 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200704
Bram Moolenaarae943152019-06-16 22:54:14 +0200705 // TODO: flip
706
707 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200708 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200709 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200711 if (wp->w_firstline < 0)
712 wp->w_firstline = -1;
713 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200714
Bram Moolenaard61efa52022-07-23 09:52:04 +0100715 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200716 if (nr != -1)
717 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200718
Bram Moolenaard61efa52022-07-23 09:52:04 +0100719 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200720 if (str != NULL)
721 {
722 vim_free(wp->w_popup_title);
723 wp->w_popup_title = vim_strsave(str);
724 }
725
Bram Moolenaard61efa52022-07-23 09:52:04 +0100726 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200727 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200728 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200729
Bram Moolenaard61efa52022-07-23 09:52:04 +0100730 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200731 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200732 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200733 if (nr)
734 wp->w_popup_flags |= POPF_DRAG;
735 else
736 wp->w_popup_flags &= ~POPF_DRAG;
737 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100738 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000739 if (nr != -1)
740 {
741 if (nr)
742 wp->w_popup_flags |= POPF_DRAGALL;
743 else
744 wp->w_popup_flags &= ~POPF_DRAGALL;
745 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200746
Bram Moolenaard61efa52022-07-23 09:52:04 +0100747 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100749 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100750 if (nr)
751 wp->w_popup_flags |= POPF_POSINVERT;
752 else
753 wp->w_popup_flags &= ~POPF_POSINVERT;
754 }
755
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200757 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200758 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200759 if (nr)
760 wp->w_popup_flags |= POPF_RESIZE;
761 else
762 wp->w_popup_flags &= ~POPF_RESIZE;
763 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200764
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200765 di = dict_find(dict, (char_u *)"close", -1);
766 if (di != NULL)
767 {
768 int ok = TRUE;
769
770 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
771 {
772 char_u *s = di->di_tv.vval.v_string;
773
774 if (STRCMP(s, "none") == 0)
775 wp->w_popup_close = POPCLOSE_NONE;
776 else if (STRCMP(s, "button") == 0)
777 wp->w_popup_close = POPCLOSE_BUTTON;
778 else if (STRCMP(s, "click") == 0)
779 wp->w_popup_close = POPCLOSE_CLICK;
780 else
781 ok = FALSE;
782 }
783 else
784 ok = FALSE;
785 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000786 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200787 }
788
Bram Moolenaard61efa52022-07-23 09:52:04 +0100789 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000791 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
793 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000794#ifdef FEAT_TERMINAL
795 term_update_wincolor(wp);
796#endif
797 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200798
Bram Moolenaarae943152019-06-16 22:54:14 +0200799 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
800 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200801
Bram Moolenaar790498b2019-06-01 22:15:29 +0200802 di = dict_find(dict, (char_u *)"borderhighlight", -1);
803 if (di != NULL)
804 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200805 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000806 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200807 else
808 {
809 list_T *list = di->di_tv.vval.v_list;
810 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200812
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200813 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200814 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
815 ++i, li = li->li_next)
816 {
817 str = tv_get_string(&li->li_tv);
818 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100819 {
820 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200821 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100822 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200824 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
825 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100826 {
827 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200828 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200829 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 }
832 }
833
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 di = dict_find(dict, (char_u *)"borderchars", -1);
835 if (di != NULL)
836 {
837 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000838 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839 else
840 {
841 list_T *list = di->di_tv.vval.v_list;
842 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200843 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200844
845 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100846 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200848 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
849 ++i, li = li->li_next)
850 {
851 str = tv_get_string(&li->li_tv);
852 if (*str != NUL)
853 wp->w_border_char[i] = mb_ptr2char(str);
854 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200855 if (list->lv_len == 1)
856 for (i = 1; i < 8; ++i)
857 wp->w_border_char[i] = wp->w_border_char[0];
858 if (list->lv_len == 2)
859 {
860 for (i = 4; i < 8; ++i)
861 wp->w_border_char[i] = wp->w_border_char[1];
862 for (i = 1; i < 4; ++i)
863 wp->w_border_char[i] = wp->w_border_char[0];
864 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200865 }
866 }
867 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200868
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200869 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
870 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
871
Bram Moolenaarae943152019-06-16 22:54:14 +0200872 di = dict_find(dict, (char_u *)"zindex", -1);
873 if (di != NULL)
874 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100875 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200876 if (wp->w_zindex < 1)
877 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
878 if (wp->w_zindex > 32000)
879 wp->w_zindex = 32000;
880 }
881
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200882 di = dict_find(dict, (char_u *)"mask", -1);
883 if (di != NULL)
884 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200885 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200886
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200887 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200888 {
889 listitem_T *li;
890
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200891 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200892 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200893 {
894 if (li->li_tv.v_type != VAR_LIST
895 || li->li_tv.vval.v_list == NULL
896 || li->li_tv.vval.v_list->lv_len != 4)
897 {
898 ok = FALSE;
899 break;
900 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100901 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200902 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200903 }
904 }
905 if (ok)
906 {
907 wp->w_popup_mask = di->di_tv.vval.v_list;
908 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200909 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910 }
911 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913 }
914
Bram Moolenaarae943152019-06-16 22:54:14 +0200915#if defined(FEAT_TIMERS)
916 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100917 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200918 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100919 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200920#endif
921
Bram Moolenaar3397f742019-06-02 18:40:06 +0200922 di = dict_find(dict, (char_u *)"moved", -1);
923 if (di != NULL)
924 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200925 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200926 handle_moved_argument(wp, di, FALSE);
927 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200928
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200929 di = dict_find(dict, (char_u *)"mousemoved", -1);
930 if (di != NULL)
931 {
932 set_mousemoved_values(wp);
933 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200934 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200935
Bram Moolenaard61efa52022-07-23 09:52:04 +0100936 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100937 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200938 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100939 if (nr != 0)
940 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200941 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100942 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200943 }
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 di = dict_find(dict, (char_u *)"filter", -1);
946 if (di != NULL)
947 {
948 callback_T callback = get_callback(&di->di_tv);
949
950 if (callback.cb_name != NULL)
951 {
952 free_callback(&wp->w_filter_cb);
953 set_callback(&wp->w_filter_cb, &callback);
954 }
955 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100956 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200957 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200958 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200959 if (nr)
960 wp->w_popup_flags |= POPF_MAPPING;
961 else
962 wp->w_popup_flags &= ~POPF_MAPPING;
963 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200964
Bram Moolenaard61efa52022-07-23 09:52:04 +0100965 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200966 if (str != NULL)
967 {
968 if (STRCMP(str, "a") == 0)
969 wp->w_filter_mode = MODE_ALL;
970 else
971 wp->w_filter_mode = mode_str2flags(str);
972 }
973
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 di = dict_find(dict, (char_u *)"callback", -1);
975 if (di != NULL)
976 {
977 callback_T callback = get_callback(&di->di_tv);
978
979 if (callback.cb_name != NULL)
980 {
981 free_callback(&wp->w_close_cb);
982 set_callback(&wp->w_close_cb, &callback);
983 }
984 }
985}
986
987/*
988 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200989 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200990 */
991 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200992apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +0200993{
994 int nr;
995
996 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +0200997
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200998 if (create)
999 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001000 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1001
Bram Moolenaarae943152019-06-16 22:54:14 +02001002 apply_general_options(wp, dict);
1003
Bram Moolenaard61efa52022-07-23 09:52:04 +01001004 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001005 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001006 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001007
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001008 // when "firstline" and "cursorline" are both set and the cursor would be
1009 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001010 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1011 {
1012 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1013 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001014 else if (wp->w_cursor.lnum < wp->w_firstline
1015 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001016 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001017 wp->w_topline = wp->w_firstline;
1018 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001019 }
1020
Bram Moolenaar33796b32019-06-08 16:01:13 +02001021 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001022 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023}
1024
1025/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001026 * Add lines to the popup from a list of strings.
1027 */
1028 static void
1029add_popup_strings(buf_T *buf, list_T *l)
1030{
1031 listitem_T *li;
1032 linenr_T lnum = 0;
1033 char_u *p;
1034
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001035 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 if (li->li_tv.v_type == VAR_STRING)
1037 {
1038 p = li->li_tv.vval.v_string;
1039 ml_append_buf(buf, lnum++,
1040 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1041 }
1042}
1043
1044/*
1045 * Add lines to the popup from a list of dictionaries.
1046 */
1047 static void
1048add_popup_dicts(buf_T *buf, list_T *l)
1049{
1050 listitem_T *li;
1051 listitem_T *pli;
1052 linenr_T lnum = 0;
1053 char_u *p;
1054 dict_T *dict;
1055
1056 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001057 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001058 {
1059 if (li->li_tv.v_type != VAR_DICT)
1060 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001061 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001062 return;
1063 }
1064 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001065 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001066 ml_append_buf(buf, lnum++,
1067 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1068 }
1069
1070 // add the text properties
1071 lnum = 1;
1072 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1073 {
1074 dictitem_T *di;
1075 list_T *plist;
1076
1077 dict = li->li_tv.vval.v_dict;
1078 di = dict_find(dict, (char_u *)"props", -1);
1079 if (di != NULL)
1080 {
1081 if (di->di_tv.v_type != VAR_LIST)
1082 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001083 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001084 return;
1085 }
1086 plist = di->di_tv.vval.v_list;
1087 if (plist != NULL)
1088 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001089 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001090 {
1091 if (pli->li_tv.v_type != VAR_DICT)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001094 return;
1095 }
1096 dict = pli->li_tv.vval.v_dict;
1097 if (dict != NULL)
1098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001101 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001102 }
1103 }
1104 }
1105 }
1106 }
1107}
1108
1109/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001110 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1111 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001112 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001113popup_top_extra(win_T *wp)
1114{
1115 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1116
1117 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1118 return 1;
1119 return extra;
1120}
1121
1122/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001123 * Get the padding plus border at the left.
1124 */
1125 int
1126popup_left_extra(win_T *wp)
1127{
1128 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1129}
1130
1131/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001132 * Return the height of popup window "wp", including border and padding.
1133 */
1134 int
1135popup_height(win_T *wp)
1136{
1137 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001138 + popup_top_extra(wp)
1139 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140}
1141
1142/*
1143 * Return the width of popup window "wp", including border, padding and
1144 * scrollbar.
1145 */
1146 int
1147popup_width(win_T *wp)
1148{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001149 // w_leftcol is how many columns of the core are left of the screen
1150 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001152 + popup_extra_width(wp)
1153 + wp->w_popup_rightoff;
1154}
1155
1156/*
1157 * Return the extra width of popup window "wp": border, padding and scrollbar.
1158 */
1159 int
1160popup_extra_width(win_T *wp)
1161{
1162 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1163 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1164 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001165}
1166
1167/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001168 * Adjust the position and size of the popup to fit on the screen.
1169 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001170 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171popup_adjust_position(win_T *wp)
1172{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001173 linenr_T lnum;
1174 int wrapped = 0;
1175 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001176 int maxwidth_no_scrollbar;
1177 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001178 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001179 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001180 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001181 int center_vert = FALSE;
1182 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001183 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001184 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001185 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1186 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1187 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1188 int extra_height = top_extra + bot_extra;
1189 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001190 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001191 int org_winrow = wp->w_winrow;
1192 int org_wincol = wp->w_wincol;
1193 int org_width = wp->w_width;
1194 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001195 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001196 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001197 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001198 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199 int wantline = wp->w_wantline; // adjusted for textprop
1200 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001201 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001202 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 wp->w_winrow = 0;
1205 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001206 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001207 wp->w_popup_leftoff = 0;
1208 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001210 // May need to update the "cursorline" highlighting, which may also change
1211 // "topline"
1212 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1213 popup_highlight_curline(wp);
1214
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1216 {
1217 win_T *prop_win = wp->w_popup_prop_win;
1218 textprop_T prop;
1219 linenr_T prop_lnum;
1220 pos_T pos;
1221 int screen_row;
1222 int screen_scol;
1223 int screen_ccol;
1224 int screen_ecol;
1225
1226 // Popup window is positioned relative to a text property.
1227 if (find_visible_prop(prop_win,
1228 wp->w_popup_prop_type, wp->w_popup_prop_id,
1229 &prop, &prop_lnum) == FAIL)
1230 {
1231 // Text property is no longer visible, hide the popup.
1232 // Unhiding the popup is done in check_popup_unhidden().
1233 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1234 {
1235 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001237 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001238 }
1239 return;
1240 }
1241
1242 // Compute the desired position from the position of the text
1243 // property. Use "wantline" and "wantcol" as offsets.
1244 pos.lnum = prop_lnum;
1245 pos.col = prop.tp_col;
1246 if (wp->w_popup_pos == POPPOS_TOPLEFT
1247 || wp->w_popup_pos == POPPOS_BOTLEFT)
1248 pos.col += prop.tp_len - 1;
1249 textpos2screenpos(prop_win, &pos, &screen_row,
1250 &screen_scol, &screen_ccol, &screen_ecol);
1251
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001252 if (screen_scol == 0)
1253 {
1254 // position is off screen, make the width zero to hide it.
1255 wp->w_width = 0;
1256 return;
1257 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001258 if (wp->w_popup_pos == POPPOS_TOPLEFT
1259 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1260 // below the text
1261 wantline = screen_row + wantline + 1;
1262 else
1263 // above the text
1264 wantline = screen_row + wantline - 1;
1265 center_vert = FALSE;
1266 if (wp->w_popup_pos == POPPOS_TOPLEFT
1267 || wp->w_popup_pos == POPPOS_BOTLEFT)
1268 // right of the text
1269 wantcol = screen_ecol + wantcol;
1270 else
1271 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001272 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001273 use_wantcol = TRUE;
1274 }
1275 else
1276 {
1277 // If no line was specified default to vertical centering.
1278 if (wantline == 0)
1279 center_vert = TRUE;
1280 else if (wantline < 0)
1281 // If "wantline" is negative it actually means zero.
1282 wantline = 0;
1283 if (wantcol < 0)
1284 // If "wantcol" is negative it actually means zero.
1285 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001286 }
1287
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 if (wp->w_popup_pos == POPPOS_CENTER)
1289 {
1290 // center after computing the size
1291 center_vert = TRUE;
1292 center_hor = TRUE;
1293 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001294 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001296 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001299 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001300 if (wp->w_winrow >= Rows)
1301 wp->w_winrow = Rows - 1;
1302 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001303 if (wp->w_popup_pos == POPPOS_BOTTOM)
1304 // assume that each buffer line takes one screen line
1305 wp->w_winrow = MAX(Rows - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001306
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001307 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001308 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001309 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1310 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001311 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001312 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001313 // Need to see at least one character after the decoration.
1314 if (wp->w_wincol > Columns - left_extra - 1)
1315 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001316 }
1317 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001318
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001319 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 // When left aligned use the space available, but shift to the left when we
1321 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001322 maxspace = Columns - wp->w_wincol - left_extra;
1323 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001324 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001325 {
1326 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001327 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001328 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001329
1330 if (wp->w_p_nu || wp->w_p_rnu)
1331 margin_width = number_width(wp) + 1;
1332#ifdef FEAT_FOLDING
1333 margin_width += wp->w_p_fdc;
1334#endif
1335#ifdef FEAT_SIGNS
1336 if (signcolumn_on(wp))
1337 margin_width += 2;
1338#endif
1339 if (margin_width >= maxwidth)
1340 margin_width = maxwidth - 1;
1341
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001342 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001343 minheight = wp->w_minheight;
1344#ifdef FEAT_TERMINAL
1345 // A terminal popup initially does not have content, use a default minimal
1346 // width of 20 characters and height of 5 lines.
1347 if (wp->w_buffer->b_term != NULL)
1348 {
1349 if (minwidth == 0)
1350 minwidth = 20;
1351 if (minheight == 0)
1352 minheight = 5;
1353 }
1354#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001355
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001356 if (wp->w_maxheight > 0)
1357 maxheight = wp->w_maxheight;
1358
Bram Moolenaar8d241042019-06-12 23:40:01 +02001359 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001360 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001361 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001362 if (wp->w_topline < 1)
1363 wp->w_topline = 1;
1364 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001365 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1366
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001367 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001368 // Use a minimum width of one, so that something shows when there is no
1369 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001370 // When "firstline" is -1 then start with the last buffer line and go
1371 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001372 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001373 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001374 if (wp->w_firstline < 0)
1375 lnum = wp->w_buffer->b_ml.ml_line_count;
1376 else
1377 lnum = wp->w_topline;
1378 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001379 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001380 int len;
1381 int w_width = wp->w_width;
1382
1383 // Count Tabs for what they are worth and compute the length based on
1384 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001385 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001386 if (wp->w_width < maxwidth)
1387 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001388 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001389 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001390 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001391
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001392 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001393 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001394 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001395 {
1396 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001397 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001399 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001400 }
1401 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001402 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403 && allow_adjust_left
1404 && (wp->w_popup_pos == POPPOS_TOPLEFT
1405 || wp->w_popup_pos == POPPOS_BOTLEFT))
1406 {
1407 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001408 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001409
Bram Moolenaar51c31312019-06-15 22:27:23 +02001410 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001411 {
1412 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001413
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001414 len -= truncate_shift;
1415 shift_by -= truncate_shift;
1416 }
1417
1418 wp->w_wincol -= shift_by;
1419 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001420 wp->w_width = maxwidth;
1421 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001422 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001423 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001424 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001425 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1426 wp->w_width = wp->w_maxwidth;
1427 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001428
1429 if (wp->w_firstline < 0)
1430 --lnum;
1431 else
1432 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001433
1434 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001435 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001436 && (wp->w_firstline >= 0
1437 ? lnum - wp->w_topline
1438 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001439 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001440 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001441 }
1442
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001443 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001444 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001445
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001446 wp->w_has_scrollbar = wp->w_want_scrollbar
1447 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001448#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001449 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1450 // Terminal window with running job never has a scrollbar, adjusts to
1451 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001452 wp->w_has_scrollbar = FALSE;
1453#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001454 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001455 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001456 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001457 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001458 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001459 // make space for the scrollbar if needed, when lines wrap and when
1460 // applying minwidth
1461 if (maxwidth + right_extra >= maxspace
1462 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1463 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001464 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001465
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001466 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1467 {
1468 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1469
1470 if (minwidth < title_len)
1471 minwidth = title_len;
1472 }
1473
1474 if (minwidth > 0 && wp->w_width < minwidth)
1475 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001476 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001477 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001478 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001479 // some columns cut off on the right
1480 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001481
1482 // If the window doesn't fit because 'minwidth' is set then the
1483 // scrollbar is at the far right of the screen, use the size without
1484 // the scrollbar.
1485 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1486 {
1487 int off = wp->w_width - maxwidth;
1488
1489 if (off > right_extra)
1490 extra_width -= right_extra;
1491 else
1492 extra_width -= off;
1493 wp->w_width = maxwidth_no_scrollbar;
1494 }
1495 else
1496 {
1497 wp->w_width = maxwidth;
1498
1499 // when adding a scrollbar below need to adjust the width
1500 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1501 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001502 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001503 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001504 {
1505 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1506 if (wp->w_wincol < 0)
1507 wp->w_wincol = 0;
1508 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001509 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1510 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1511 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001512 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001513
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001514 // Right aligned: move to the right if needed.
1515 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001516 if (leftoff >= 0)
1517 wp->w_wincol = leftoff;
1518 else if (wp->w_popup_fixed)
1519 {
1520 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001521 // columns when displaying the window, minus left border and
1522 // padding.
1523 if (-leftoff > left_extra)
1524 wp->w_leftcol = -leftoff - left_extra;
1525 wp->w_width -= wp->w_leftcol;
1526 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001527 if (wp->w_width < 0)
1528 wp->w_width = 0;
1529 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001530 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001531
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001532 if (wp->w_p_wrap || (!wp->w_popup_fixed
1533 && (wp->w_popup_pos == POPPOS_TOPLEFT
1534 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001535 {
1536 int want_col = 0;
1537
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001538 // try to show the right border and any scrollbar
1539 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001540 if (want_col > 0 && wp->w_wincol > 0
1541 && wp->w_wincol + want_col >= Columns)
1542 {
1543 wp->w_wincol = Columns - want_col;
1544 if (wp->w_wincol < 0)
1545 wp->w_wincol = 0;
1546 }
1547 }
1548
Bram Moolenaar8d241042019-06-12 23:40:01 +02001549 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1550 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001551 if (minheight > 0 && wp->w_height < minheight)
1552 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001553 if (maxheight > 0 && wp->w_height > maxheight)
1554 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001555 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001556 if (wp->w_height > Rows - wp->w_winrow)
1557 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001558
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001559 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001560 {
1561 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1562 if (wp->w_winrow < 0)
1563 wp->w_winrow = 0;
1564 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001565 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001566 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001567 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001568 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001569 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001570 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001571 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1572 {
1573 // Bottom aligned but does not fit, and less space on the other
1574 // side or "posinvert" is off: reduce height.
1575 wp->w_winrow = 0;
1576 wp->w_height = wantline - extra_height;
1577 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001578 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001579 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001580 // Not enough space and more space on the other side: make top
1581 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001582 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001583 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001584 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001585 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001586 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1587 || wp->w_popup_pos == POPPOS_TOPLEFT)
1588 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001589 if (wp != popup_dragwin
1590 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001591 && wantline * 2 > Rows
1592 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001593 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001594 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001595 // make bottom aligned and recompute the height
1596 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001597 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001598 if (wp->w_winrow < 0)
1599 {
1600 wp->w_height += wp->w_winrow;
1601 wp->w_winrow = 0;
1602 }
1603 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001604 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001605 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001606 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001607 adjust_height_for_top_aligned = TRUE;
1608 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001609 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001610
1611 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1612 && wp->w_winrow + wp->w_height + extra_height > Rows)
1613 {
1614 // Bottom of the popup goes below the last line, reduce the height and
1615 // add a scrollbar.
1616 wp->w_height = Rows - wp->w_winrow - extra_height;
1617#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001618 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001619#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001620 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001621 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001622 if (width_with_scrollbar > 0)
1623 wp->w_width = width_with_scrollbar;
1624 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001625 }
1626
1627 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001628 if (wp->w_winrow >= Rows)
1629 wp->w_winrow = Rows - 1;
1630 else if (wp->w_winrow < 0)
1631 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001632
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001633 if (wp->w_height != org_height)
1634 win_comp_scroll(wp);
1635
Bram Moolenaar17146962019-05-30 00:12:11 +02001636 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001637 if (win_valid(wp->w_popup_prop_win))
1638 {
1639 wp->w_popup_prop_changedtick =
1640 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1641 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1642 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001643
1644 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001645 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001646 if (org_winrow != wp->w_winrow
1647 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001648 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001649 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001650 || org_width != wp->w_width
1651 || org_height != wp->w_height)
1652 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001653 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001654 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1655 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001656 popup_mask_refresh = TRUE;
1657 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001658}
1659
Bram Moolenaar17627312019-06-02 19:53:44 +02001660typedef enum
1661{
1662 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001663 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001664 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001665 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001666 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001667 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001668 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001669 TYPE_PREVIEW, // preview window
1670 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001671} create_type_T;
1672
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001673/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001674 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1675 */
1676 static int
1677popup_is_notification(create_type_T type)
1678{
1679 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1680}
1681
1682/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001683 * Make "buf" empty and set the contents to "text".
1684 * Used by popup_create() and popup_settext().
1685 */
1686 static void
1687popup_set_buffer_text(buf_T *buf, typval_T text)
1688{
1689 int lnum;
1690
1691 // Clear the buffer, then replace the lines.
1692 curbuf = buf;
1693 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001694 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001695 curbuf = curwin->w_buffer;
1696
1697 // Add text to the buffer.
1698 if (text.v_type == VAR_STRING)
1699 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001700 char_u *s = text.vval.v_string;
1701
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001702 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001703 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001704 }
1705 else
1706 {
1707 list_T *l = text.vval.v_list;
1708
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001709 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001710 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001711 if (l->lv_first == &range_list_item)
1712 emsg(_(e_using_number_as_string));
1713 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001714 // list of strings
1715 add_popup_strings(buf, l);
1716 else
1717 // list of dictionaries
1718 add_popup_dicts(buf, l);
1719 }
1720 }
1721
1722 // delete the line that was in the empty buffer
1723 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001724 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001725 curbuf = curwin->w_buffer;
1726}
1727
1728/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001729 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1730 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001731 * Return FAIL if the parsing fails.
1732 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001733 static int
1734parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001735{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001736 char_u *p =
1737#ifdef FEAT_QUICKFIX
1738 !is_preview ? p_cpp :
1739#endif
1740 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001741
Bram Moolenaar258cef52019-08-21 17:29:29 +02001742 if (wp != NULL)
1743 wp->w_popup_flags &= ~POPF_INFO_MENU;
1744
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001745 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001746 {
1747 char_u *e, *dig;
1748 char_u *s = p;
1749 int x;
1750
1751 e = vim_strchr(p, ':');
1752 if (e == NULL || e[1] == NUL)
1753 return FAIL;
1754
1755 p = vim_strchr(e, ',');
1756 if (p == NULL)
1757 p = e + STRLEN(e);
1758 dig = e + 1;
1759 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001760
1761 if (STRNCMP(s, "height:", 7) == 0)
1762 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001763 if (dig != p)
1764 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001765 if (wp != NULL)
1766 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001767 if (is_preview)
1768 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001769 wp->w_maxheight = x;
1770 }
1771 }
1772 else if (STRNCMP(s, "width:", 6) == 0)
1773 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001774 if (dig != p)
1775 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 if (wp != NULL)
1777 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001778 if (is_preview)
1779 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001780 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001781 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001782 }
1783 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001784 else if (STRNCMP(s, "highlight:", 10) == 0)
1785 {
1786 if (wp != NULL)
1787 {
1788 int c = *p;
1789
1790 *p = NUL;
1791 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1792 s + 10, OPT_FREE|OPT_LOCAL, 0);
1793 *p = c;
1794 }
1795 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001796 else if (STRNCMP(s, "border:", 7) == 0)
1797 {
1798 char_u *arg = s + 7;
1799 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1800 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1801 int i;
1802
1803 if (!on && !off)
1804 return FAIL;
1805 if (wp != NULL)
1806 {
1807 for (i = 0; i < 4; ++i)
1808 wp->w_popup_border[i] = on ? 1 : 0;
1809 if (off)
1810 // only show the X for close when there is a border
1811 wp->w_popup_close = POPCLOSE_NONE;
1812 }
1813 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001814 else if (STRNCMP(s, "align:", 6) == 0)
1815 {
1816 char_u *arg = s + 6;
1817 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1818 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1819
1820 if (!menu && !item)
1821 return FAIL;
1822 if (wp != NULL && menu)
1823 wp->w_popup_flags |= POPF_INFO_MENU;
1824 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001825 else
1826 return FAIL;
1827 }
1828 return OK;
1829}
1830
1831/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001832 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1833 * is not NULL.
1834 * Return FAIL if the parsing fails.
1835 */
1836 int
1837parse_previewpopup(win_T *wp)
1838{
1839 return parse_popup_option(wp, TRUE);
1840}
1841
1842/*
1843 * Parse the 'completepopup' option and apply the values to window "wp" if it
1844 * is not NULL.
1845 * Return FAIL if the parsing fails.
1846 */
1847 int
1848parse_completepopup(win_T *wp)
1849{
1850 return parse_popup_option(wp, FALSE);
1851}
1852
1853/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001854 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001855 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001856 */
1857 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001858popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001859{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001860 poppos_T ppt = POPPOS_NONE;
1861
1862 if (d != NULL)
1863 ppt = get_pos_entry(d, FALSE);
1864
Bram Moolenaar79648732019-07-18 21:43:07 +02001865 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001866 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001867 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001868 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1869 }
1870 else
1871 {
1872 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1873 if (wp->w_wantline == 0) // cursor in first line
1874 {
1875 wp->w_wantline = 2;
1876 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1877 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1878 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001879 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001880
Bram Moolenaar79648732019-07-18 21:43:07 +02001881 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001882 if (wp->w_wantcol > Columns - width)
1883 {
1884 wp->w_wantcol = Columns - width;
1885 if (wp->w_wantcol < 1)
1886 wp->w_wantcol = 1;
1887 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001888
Bram Moolenaar79648732019-07-18 21:43:07 +02001889 popup_adjust_position(wp);
1890}
1891
1892/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001893 * Set w_wantline and w_wantcol for the a given screen position.
1894 * Caller must take care of running into the window border.
1895 */
1896 void
1897popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1898{
1899 wp->w_wantline = row;
1900 wp->w_wantcol = col;
1901 popup_adjust_position(wp);
1902}
1903
1904/*
1905 * Add a border and lef&right padding.
1906 */
1907 static void
1908add_border_left_right_padding(win_T *wp)
1909{
1910 int i;
1911
1912 for (i = 0; i < 4; ++i)
1913 {
1914 wp->w_popup_border[i] = 1;
1915 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1916 }
1917}
1918
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001919#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001920/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001921 * Return TRUE if there is any popup window with a terminal buffer.
1922 */
1923 static int
1924popup_terminal_exists(void)
1925{
1926 win_T *wp;
1927 tabpage_T *tp;
1928
1929 FOR_ALL_POPUPWINS(wp)
1930 if (wp->w_buffer->b_term != NULL)
1931 return TRUE;
1932 FOR_ALL_TABPAGES(tp)
1933 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1934 if (wp->w_buffer->b_term != NULL)
1935 return TRUE;
1936 return FALSE;
1937}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001938#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001939
1940/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001941 * Set the color for a notification window.
1942 */
1943 static void
1944popup_update_color(win_T *wp, create_type_T type)
1945{
1946 char *hiname = type == TYPE_MESSAGE_WIN
1947 ? "MessageWindow" : "PopupNotification";
1948 int nr = syn_name2id((char_u *)hiname);
1949
1950 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1951 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1952 OPT_FREE|OPT_LOCAL, 0);
1953}
1954
1955/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001956 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001957 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001958 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001959 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001960 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001961 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001962popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001963{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001964 win_T *wp;
1965 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001966 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001967 int new_buffer;
1968 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001969 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001970 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001971
Bram Moolenaar79648732019-07-18 21:43:07 +02001972 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001973 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00001974 if (in_vim9script()
1975 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
1976 || check_for_dict_arg(argvars, 1) == FAIL))
1977 return NULL;
1978
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001979 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001980 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001981 {
Bram Moolenaar49540192019-12-11 19:34:54 +01001982 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001983 if (buf == NULL)
1984 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001985 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02001986 return NULL;
1987 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001988#ifdef FEAT_TERMINAL
1989 if (buf->b_term != NULL && popup_terminal_exists())
1990 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001991 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001992 return NULL;
1993 }
1994#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001995 }
1996 else if (!(argvars[0].v_type == VAR_STRING
1997 && argvars[0].vval.v_string != NULL)
1998 && !(argvars[0].v_type == VAR_LIST
1999 && argvars[0].vval.v_list != NULL))
2000 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002001 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002002 return NULL;
2003 }
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002005 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002006 emsg(_(e_dictionary_required));
Bram Moolenaara3fce622019-06-20 02:31:49 +02002007 return NULL;
2008 }
Bram Moolenaar79648732019-07-18 21:43:07 +02002009 d = argvars[1].vval.v_dict;
2010 }
2011
2012 if (d != NULL)
2013 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002014 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002015 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002016 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002017 tabnr = -1; // notifications are global by default
2018 else
2019 tabnr = 0;
2020 if (tabnr > 0)
2021 {
2022 tp = find_tabpage(tabnr);
2023 if (tp == NULL)
2024 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002025 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002026 return NULL;
2027 }
2028 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002029 }
2030
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002031 // Create the window and buffer.
2032 wp = win_alloc_popup_win();
2033 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002034 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002035 if (rettv != NULL)
2036 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002037 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002038 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002039
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002040 if (buf != NULL)
2041 {
2042 // use existing buffer
2043 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002044 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002045 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002046 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002047 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002048 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002049 }
2050 else
2051 {
2052 // create a new buffer associated with the popup
2053 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002054 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002055 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002056 {
2057 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002058 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002059 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002060 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002061
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002062 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002063
Bram Moolenaar46451042019-08-24 15:50:46 +02002064 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002065 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002066 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002067 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002068 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002069 buf->b_p_ul = -1; // no undo
2070 buf->b_p_swf = FALSE; // no swap file
2071 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002072 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002073
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 // Avoid that 'buftype' is reset when this buffer is entered.
2075 buf->b_p_initialized = TRUE;
2076 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002077 wp->w_p_wrap = TRUE; // 'wrap' is default on
2078 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002079
Bram Moolenaara3fce622019-06-20 02:31:49 +02002080 if (tp != NULL)
2081 {
2082 // popup on specified tab page
2083 wp->w_next = tp->tp_first_popupwin;
2084 tp->tp_first_popupwin = wp;
2085 }
2086 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002087 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002088 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002089 wp->w_next = curtab->tp_first_popupwin;
2090 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002091 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002092 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002093 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002094 win_T *prev = first_popupwin;
2095
2096 // Global popup: add at the end, so that it gets displayed on top of
2097 // older ones with the same zindex. Matters for notifications.
2098 if (first_popupwin == NULL)
2099 first_popupwin = wp;
2100 else
2101 {
2102 while (prev->w_next != NULL)
2103 prev = prev->w_next;
2104 prev->w_next = wp;
2105 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002106 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002107
Bram Moolenaar79648732019-07-18 21:43:07 +02002108 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002109 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002110
Bram Moolenaar79648732019-07-18 21:43:07 +02002111 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002112 {
2113 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002114 }
2115 if (type == TYPE_ATCURSOR)
2116 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002117 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002118 set_moved_values(wp);
2119 set_moved_columns(wp, FIND_STRING);
2120 }
2121
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002122 if (type == TYPE_BEVAL)
2123 {
2124 wp->w_popup_pos = POPPOS_BOTLEFT;
2125
2126 // by default use the mouse position
2127 wp->w_wantline = mouse_row;
2128 if (wp->w_wantline <= 0) // mouse on first line
2129 {
2130 wp->w_wantline = 2;
2131 wp->w_popup_pos = POPPOS_TOPLEFT;
2132 }
2133 wp->w_wantcol = mouse_col + 1;
2134 set_mousemoved_values(wp);
2135 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2136 }
2137
Bram Moolenaar33796b32019-06-08 16:01:13 +02002138 // set default values
2139 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002140 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002141
Bram Moolenaar9198de32022-08-27 21:30:03 +01002142 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002143 {
2144 win_T *twp, *nextwin;
2145 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002146
2147 // Try to not overlap with another global popup. Guess we need 3
2148 // more screen lines than buffer lines.
2149 wp->w_wantline = 1;
2150 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2151 {
2152 nextwin = twp->w_next;
2153 if (twp != wp
2154 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2155 && twp->w_winrow <= wp->w_wantline - 1 + height
2156 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2157 {
2158 // move to below this popup and restart the loop to check for
2159 // overlap with other popups
2160 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2161 nextwin = first_popupwin;
2162 }
2163 }
2164 if (wp->w_wantline + height > Rows)
2165 {
2166 // can't avoid overlap, put on top in the hope that message goes
2167 // away soon.
2168 wp->w_wantline = 1;
2169 }
2170
2171 wp->w_wantcol = 10;
2172 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002173 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002174 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002175 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002176 for (i = 0; i < 4; ++i)
2177 wp->w_popup_border[i] = 1;
2178 wp->w_popup_padding[1] = 1;
2179 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002180
Bram Moolenaar9198de32022-08-27 21:30:03 +01002181 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002182 }
2183
Bram Moolenaara730e552019-06-16 19:05:31 +02002184 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002185 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002186 wp->w_popup_pos = POPPOS_CENTER;
2187 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002188 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002189 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002190 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002191 }
2192
Bram Moolenaara730e552019-06-16 19:05:31 +02002193 if (type == TYPE_MENU)
2194 {
2195 typval_T tv;
2196 callback_T callback;
2197
2198 tv.v_type = VAR_STRING;
2199 tv.vval.v_string = (char_u *)"popup_filter_menu";
2200 callback = get_callback(&tv);
2201 if (callback.cb_name != NULL)
2202 set_callback(&wp->w_filter_cb, &callback);
2203
2204 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002205 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002206 }
2207
Bram Moolenaar79648732019-07-18 21:43:07 +02002208 if (type == TYPE_PREVIEW)
2209 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002210 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002211 wp->w_popup_close = POPCLOSE_BUTTON;
2212 for (i = 0; i < 4; ++i)
2213 wp->w_popup_border[i] = 1;
2214 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002215 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002216 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002217# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002218 if (type == TYPE_INFO)
2219 {
2220 wp->w_popup_pos = POPPOS_TOPLEFT;
2221 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2222 wp->w_popup_close = POPCLOSE_BUTTON;
2223 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002224 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002225 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002226# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002227
Bram Moolenaarae943152019-06-16 22:54:14 +02002228 for (i = 0; i < 4; ++i)
2229 VIM_CLEAR(wp->w_border_highlight[i]);
2230 for (i = 0; i < 8; ++i)
2231 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002232 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002233 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002234 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002235
Bram Moolenaar79648732019-07-18 21:43:07 +02002236 if (d != NULL)
2237 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002238 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002239
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002240#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002241 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2242 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002243#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002244
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002245 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002246
2247 wp->w_vsep_width = 0;
2248
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002249 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002250 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002251
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002252#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002253 // When running a terminal in the popup it becomes the current window.
2254 if (buf->b_term != NULL)
2255 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002256#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002257
Bram Moolenaara730e552019-06-16 19:05:31 +02002258 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002259}
2260
2261/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002262 * popup_clear()
2263 */
2264 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002265f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002266{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002267 int force = FALSE;
2268
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002269 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2270 return;
2271
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002272 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002273 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002274 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002275}
2276
2277/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002278 * popup_create({text}, {options})
2279 */
2280 void
2281f_popup_create(typval_T *argvars, typval_T *rettv)
2282{
Bram Moolenaar17627312019-06-02 19:53:44 +02002283 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002284}
2285
2286/*
2287 * popup_atcursor({text}, {options})
2288 */
2289 void
2290f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2291{
Bram Moolenaar17627312019-06-02 19:53:44 +02002292 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002293}
2294
2295/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002296 * popup_beval({text}, {options})
2297 */
2298 void
2299f_popup_beval(typval_T *argvars, typval_T *rettv)
2300{
2301 popup_create(argvars, rettv, TYPE_BEVAL);
2302}
2303
2304/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002305 * Invoke the close callback for window "wp" with value "result".
2306 * Careful: The callback may make "wp" invalid!
2307 */
2308 static void
2309invoke_popup_callback(win_T *wp, typval_T *result)
2310{
2311 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002312 typval_T argv[3];
2313
2314 argv[0].v_type = VAR_NUMBER;
2315 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2316
2317 if (result != NULL && result->v_type != VAR_UNKNOWN)
2318 copy_tv(result, &argv[1]);
2319 else
2320 {
2321 argv[1].v_type = VAR_NUMBER;
2322 argv[1].vval.v_number = 0;
2323 }
2324
2325 argv[2].v_type = VAR_UNKNOWN;
2326
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002327 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002328 if (result != NULL)
2329 clear_tv(&argv[1]);
2330 clear_tv(&rettv);
2331}
2332
2333/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002334 * Make "prevwin" the current window, unless it's equal to "wp".
2335 * Otherwise make "firstwin" the current window.
2336 */
2337 static void
2338back_to_prevwin(win_T *wp)
2339{
2340 if (win_valid(prevwin) && wp != prevwin)
2341 win_enter(prevwin, FALSE);
2342 else
2343 win_enter(firstwin, FALSE);
2344}
2345
2346/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002347 * Close popup "wp" and invoke any close callback for it.
2348 */
2349 static void
2350popup_close_and_callback(win_T *wp, typval_T *arg)
2351{
2352 int id = wp->w_id;
2353
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002354#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002355 if (wp == curwin && curbuf->b_term != NULL)
2356 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002357 win_T *owp;
2358
2359 // Closing popup window with a terminal: put focus back on the first
2360 // that works:
2361 // - another popup window with a terminal
2362 // - the previous window
2363 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002364 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002365 if (owp != curwin && owp->w_buffer->b_term != NULL)
2366 break;
2367 if (owp != NULL)
2368 win_enter(owp, FALSE);
2369 else
2370 {
2371 for (owp = curtab->tp_first_popupwin; owp != NULL;
2372 owp = owp->w_next)
2373 if (owp != curwin && owp->w_buffer->b_term != NULL)
2374 break;
2375 if (owp != NULL)
2376 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002377 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002378 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002379 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002380 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002381#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002382
Bram Moolenaar49540192019-12-11 19:34:54 +01002383 // Just in case a check higher up is missing.
2384 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002385 {
2386 // To avoid getting stuck when win_execute() does something that causes
2387 // an error, stop calling the filter callback.
2388 free_callback(&wp->w_filter_cb);
2389
Bram Moolenaar49540192019-12-11 19:34:54 +01002390 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002391 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002392
Bram Moolenaarcee52202020-03-11 14:19:58 +01002393 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002394 if (wp->w_close_cb.cb_name != NULL)
2395 // Careful: This may make "wp" invalid.
2396 invoke_popup_callback(wp, arg);
2397
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002398 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002399 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002400}
2401
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002402 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002403popup_close_with_retval(win_T *wp, int retval)
2404{
2405 typval_T res;
2406
2407 res.v_type = VAR_NUMBER;
2408 res.vval.v_number = retval;
2409 popup_close_and_callback(wp, &res);
2410}
2411
Bram Moolenaar3397f742019-06-02 18:40:06 +02002412/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002413 * Close popup "wp" because of a mouse click.
2414 */
2415 void
2416popup_close_for_mouse_click(win_T *wp)
2417{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002418 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002419}
2420
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002421 static void
2422check_mouse_moved(win_T *wp, win_T *mouse_wp)
2423{
2424 // Close the popup when all if these are true:
2425 // - the mouse is not on this popup
2426 // - "mousemoved" was used
2427 // - the mouse is no longer on the same screen row or the mouse column is
2428 // outside of the relevant text
2429 if (wp != mouse_wp
2430 && wp->w_popup_mouse_row != 0
2431 && (wp->w_popup_mouse_row != mouse_row
2432 || mouse_col < wp->w_popup_mouse_mincol
2433 || mouse_col > wp->w_popup_mouse_maxcol))
2434 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002435 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002436 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002437 }
2438}
2439
2440/*
2441 * Called when the mouse moved: may close a popup with "mousemoved".
2442 */
2443 void
2444popup_handle_mouse_moved(void)
2445{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002446 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002447 win_T *mouse_wp;
2448 int row = mouse_row;
2449 int col = mouse_col;
2450
2451 // find the window where the mouse is in
2452 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2453
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002454 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2455 {
2456 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002457 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002458 }
2459 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2460 {
2461 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002462 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002463 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002464}
2465
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002466/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002467 * In a filter: check if the typed key is a mouse event that is used for
2468 * dragging the popup.
2469 */
2470 static void
2471filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2472{
2473 int row = mouse_row;
2474 int col = mouse_col;
2475
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002476 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002477 && is_mouse_key(c)
2478 && (wp == popup_dragwin
2479 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2480 // do not consume the key, allow for dragging the popup
2481 rettv->vval.v_number = 0;
2482}
2483
Bram Moolenaara730e552019-06-16 19:05:31 +02002484/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002485 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002486 */
2487 void
2488f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2489{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002490 int id;
2491 win_T *wp;
2492 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002493 typval_T res;
2494 int c;
2495 linenr_T old_lnum;
2496
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002497 if (in_vim9script()
2498 && (check_for_number_arg(argvars, 0) == FAIL
2499 || check_for_string_arg(argvars, 1) == FAIL))
2500 return;
2501
2502 id = tv_get_number(&argvars[0]);
2503 wp = win_id2wp(id);
2504 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002505 // If the popup has been closed do not consume the key.
2506 if (wp == NULL)
2507 return;
2508
2509 c = *key;
2510 if (c == K_SPECIAL && key[1] != NUL)
2511 c = TO_SPECIAL(key[1], key[2]);
2512
2513 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002514 rettv->v_type = VAR_BOOL;
2515 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002516 res.v_type = VAR_NUMBER;
2517
2518 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002519 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2520 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002521 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002522 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002523 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2524 ++wp->w_cursor.lnum;
2525 if (old_lnum != wp->w_cursor.lnum)
2526 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002527 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002528 return;
2529 }
2530
2531 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2532 {
2533 // Cancelled, invoke callback with -1
2534 res.vval.v_number = -1;
2535 popup_close_and_callback(wp, &res);
2536 return;
2537 }
2538 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2539 {
2540 // Invoke callback with current index.
2541 res.vval.v_number = wp->w_cursor.lnum;
2542 popup_close_and_callback(wp, &res);
2543 return;
2544 }
2545
2546 filter_handle_drag(wp, c, rettv);
2547}
2548
2549/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002550 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002551 */
2552 void
2553f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2554{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002555 int id;
2556 win_T *wp;
2557 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002558 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002559 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002560
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002561 if (in_vim9script()
2562 && (check_for_number_arg(argvars, 0) == FAIL
2563 || check_for_string_arg(argvars, 1) == FAIL))
2564 return;
2565
2566 id = tv_get_number(&argvars[0]);
2567 wp = win_id2wp(id);
2568 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002569 // If the popup has been closed don't consume the key.
2570 if (wp == NULL)
2571 return;
2572
Bram Moolenaara730e552019-06-16 19:05:31 +02002573 c = *key;
2574 if (c == K_SPECIAL && key[1] != NUL)
2575 c = TO_SPECIAL(key[1], key[2]);
2576
Bram Moolenaara42d9452019-06-15 21:46:30 +02002577 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002578 rettv->v_type = VAR_BOOL;
2579 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002580
Bram Moolenaara730e552019-06-16 19:05:31 +02002581 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002582 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002583 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002584 res.vval.v_number = 0;
2585 else
2586 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002587 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002588 return;
2589 }
2590
2591 // Invoke callback
2592 res.v_type = VAR_NUMBER;
2593 popup_close_and_callback(wp, &res);
2594}
2595
2596/*
2597 * popup_dialog({text}, {options})
2598 */
2599 void
2600f_popup_dialog(typval_T *argvars, typval_T *rettv)
2601{
2602 popup_create(argvars, rettv, TYPE_DIALOG);
2603}
2604
2605/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002606 * popup_menu({text}, {options})
2607 */
2608 void
2609f_popup_menu(typval_T *argvars, typval_T *rettv)
2610{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002611 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002612}
2613
2614/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002615 * popup_notification({text}, {options})
2616 */
2617 void
2618f_popup_notification(typval_T *argvars, typval_T *rettv)
2619{
2620 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2621}
2622
2623/*
2624 * Find the popup window with window-ID "id".
2625 * If the popup window does not exist NULL is returned.
2626 * If the window is not a popup window, and error message is given.
2627 */
2628 static win_T *
2629find_popup_win(int id)
2630{
2631 win_T *wp = win_id2wp(id);
2632
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002633 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002634 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002635 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002636 return NULL;
2637 }
2638 return wp;
2639}
2640
2641/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002642 * popup_close({id})
2643 */
2644 void
2645f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2646{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002647 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002648 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002649
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002650 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2651 return;
2652
2653 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002654 if (
2655# ifdef FEAT_TERMINAL
2656 // if the popup contains a terminal it will become hidden
2657 curbuf->b_term == NULL &&
2658# endif
2659 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002660 return;
2661
2662 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002663 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002664 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002665}
2666
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002667 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002668popup_hide(win_T *wp)
2669{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002670#ifdef FEAT_TERMINAL
2671 if (error_if_term_popup_window())
2672 return;
2673#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002674 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2675 {
2676 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002677 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002678 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002679 popup_mask_refresh = TRUE;
2680 }
2681}
2682
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002683/*
2684 * popup_hide({id})
2685 */
2686 void
2687f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2688{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002689 int id;
2690 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002691
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002692 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2693 return;
2694
2695 id = (int)tv_get_number(argvars);
2696 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002697 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002698 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002699 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002700 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2701 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002702}
2703
2704 void
2705popup_show(win_T *wp)
2706{
2707 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002708 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002709 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002710 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002711 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002712 }
2713}
2714
2715/*
2716 * popup_show({id})
2717 */
2718 void
2719f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2720{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002721 int id;
2722 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002723
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002724 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2725 return;
2726
2727 id = (int)tv_get_number(argvars);
2728 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002729 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002730 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002731 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002732 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002733#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002734 if (wp->w_popup_flags & POPF_INFO)
2735 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002736#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002737 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002738}
2739
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002740/*
2741 * popup_settext({id}, {text})
2742 */
2743 void
2744f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2745{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002746 int id;
2747 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002748
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002749 if (in_vim9script()
2750 && (check_for_number_arg(argvars, 0) == FAIL
2751 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2752 return;
2753
2754 id = (int)tv_get_number(&argvars[0]);
2755 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002756 if (wp != NULL)
2757 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002758 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002759 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002760 else
2761 {
2762 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002763 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002764 popup_adjust_position(wp);
2765 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002766 }
2767}
2768
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002769 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002770popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002771{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002772 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002773 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002774 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002775 clear_cmdline = TRUE;
2776 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002777
Bram Moolenaar43568642022-08-28 13:02:45 +01002778#ifdef HAS_MESSAGE_WINDOW
2779 if (wp == message_win)
2780 message_win = NULL;
2781#endif
2782
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002783 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002784 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002785}
2786
Bram Moolenaar82106932020-03-13 14:34:38 +01002787 static void
2788error_for_popup_window(void)
2789{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002790 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002791}
2792
2793 int
2794error_if_popup_window(int also_with_term UNUSED)
2795{
2796 // win_execute() may set "curwin" to a popup window temporarily, but many
2797 // commands are disallowed then. When a terminal runs in the popup most
2798 // things are allowed. When a terminal is finished it can be closed.
2799 if (WIN_IS_POPUP(curwin)
2800# ifdef FEAT_TERMINAL
2801 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002802# endif
2803 )
2804 {
2805 error_for_popup_window();
2806 return TRUE;
2807 }
2808 return FALSE;
2809}
2810
Bram Moolenaarec583842019-05-26 14:11:23 +02002811/*
2812 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002813 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002814 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002815 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002816 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002817popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002818{
2819 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002820 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002821 win_T *prev = NULL;
2822
Bram Moolenaarec583842019-05-26 14:11:23 +02002823 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002824 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002825 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002826 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002827 if (wp == curwin)
2828 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002829 if (!force)
2830 {
2831 error_for_popup_window();
2832 return FAIL;
2833 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002834 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002835 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002836 if (prev == NULL)
2837 first_popupwin = wp->w_next;
2838 else
2839 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002840 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002841 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002842 }
2843
Bram Moolenaarec583842019-05-26 14:11:23 +02002844 // go through tab-local popups
2845 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002846 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002847 return OK;
2848 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002849}
2850
2851/*
2852 * Close a popup window with Window-id "id" in tabpage "tp".
2853 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002854 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002855popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002856{
2857 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002858 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002859 win_T *prev = NULL;
2860
Bram Moolenaarec583842019-05-26 14:11:23 +02002861 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2862 if (wp->w_id == id)
2863 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002864 if (wp == curwin)
2865 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002866 if (!force)
2867 {
2868 error_for_popup_window();
2869 return FAIL;
2870 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002871 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002872 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002873 if (prev == NULL)
2874 *root = wp->w_next;
2875 else
2876 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002877 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002878 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002879 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002880 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002881}
2882
2883 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002884close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002885{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002886 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002887 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002888 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002889 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002890 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002891 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002892 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002893 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002894}
2895
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002896/*
2897 * popup_move({id}, {options})
2898 */
2899 void
2900f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2901{
Bram Moolenaarae943152019-06-16 22:54:14 +02002902 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002903 int id;
2904 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002905
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002906 if (in_vim9script()
2907 && (check_for_number_arg(argvars, 0) == FAIL
2908 || check_for_dict_arg(argvars, 1) == FAIL))
2909 return;
2910
2911 id = (int)tv_get_number(argvars);
2912 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002913 if (wp == NULL)
2914 return; // invalid {id}
2915
2916 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2917 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002918 emsg(_(e_dictionary_required));
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002919 return;
2920 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002921 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002922
Bram Moolenaarae943152019-06-16 22:54:14 +02002923 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002924
2925 if (wp->w_winrow + wp->w_height >= cmdline_row)
2926 clear_cmdline = TRUE;
2927 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002928}
2929
Bram Moolenaarbc133542019-05-29 20:26:46 +02002930/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002931 * popup_setoptions({id}, {options})
2932 */
2933 void
2934f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2935{
2936 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002937 int id;
2938 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002939 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002940
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002941 if (in_vim9script()
2942 && (check_for_number_arg(argvars, 0) == FAIL
2943 || check_for_dict_arg(argvars, 1) == FAIL))
2944 return;
2945
2946 id = (int)tv_get_number(argvars);
2947 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002948 if (wp == NULL)
2949 return; // invalid {id}
2950
2951 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2952 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002953 emsg(_(e_dictionary_required));
Bram Moolenaarae943152019-06-16 22:54:14 +02002954 return;
2955 }
2956 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002957 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002958
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002959 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002960
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002961 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002962 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002963 popup_adjust_position(wp);
2964}
2965
2966/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002967 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002968 */
2969 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002970f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002971{
2972 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002973 int id;
2974 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002975 int top_extra;
2976 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002977
2978 if (rettv_dict_alloc(rettv) == OK)
2979 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002980 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2981 return;
2982
2983 id = (int)tv_get_number(argvars);
2984 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02002985 if (wp == NULL)
2986 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002987 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002988 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2989
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002990 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002991 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002992 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002993
Bram Moolenaarbc133542019-05-29 20:26:46 +02002994 dict_add_number(dict, "line", wp->w_winrow + 1);
2995 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002996 dict_add_number(dict, "width", wp->w_width + left_extra
2997 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2998 dict_add_number(dict, "height", wp->w_height + top_extra
2999 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003000
3001 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3002 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3003 dict_add_number(dict, "core_width", wp->w_width);
3004 dict_add_number(dict, "core_height", wp->w_height);
3005
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003006 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003007 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003008 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003009 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003010 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003011
3012 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003013 }
3014}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003015
3016/*
3017 * popup_list()
3018 */
3019 void
3020f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3021{
3022 win_T *wp;
3023 tabpage_T *tp;
3024
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003025 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003026 return;
3027 FOR_ALL_POPUPWINS(wp)
3028 list_append_number(rettv->vval.v_list, wp->w_id);
3029 FOR_ALL_TABPAGES(tp)
3030 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3031 list_append_number(rettv->vval.v_list, wp->w_id);
3032}
3033
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003034/*
3035 * popup_locate({row}, {col})
3036 */
3037 void
3038f_popup_locate(typval_T *argvars, typval_T *rettv)
3039{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003040 int row;
3041 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003042 win_T *wp;
3043
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003044 if (in_vim9script()
3045 && (check_for_number_arg(argvars, 0) == FAIL
3046 || check_for_number_arg(argvars, 1) == FAIL))
3047 return;
3048
3049 row = tv_get_number(&argvars[0]) - 1;
3050 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003051 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003052 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003053 rettv->vval.v_number = wp->w_id;
3054}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003055
3056/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003057 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3058 */
3059 static void
3060get_padding_border(dict_T *dict, int *array, char *name)
3061{
3062 list_T *list;
3063 int i;
3064
3065 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3066 return;
3067
3068 list = list_alloc();
3069 if (list != NULL)
3070 {
3071 dict_add_list(dict, name, list);
3072 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3073 for (i = 0; i < 4; ++i)
3074 list_append_number(list, array[i]);
3075 }
3076}
3077
3078/*
3079 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3080 */
3081 static void
3082get_borderhighlight(dict_T *dict, win_T *wp)
3083{
3084 list_T *list;
3085 int i;
3086
3087 for (i = 0; i < 4; ++i)
3088 if (wp->w_border_highlight[i] != NULL)
3089 break;
3090 if (i == 4)
3091 return;
3092
3093 list = list_alloc();
3094 if (list != NULL)
3095 {
3096 dict_add_list(dict, "borderhighlight", list);
3097 for (i = 0; i < 4; ++i)
3098 list_append_string(list, wp->w_border_highlight[i], -1);
3099 }
3100}
3101
3102/*
3103 * For popup_getoptions(): add a "borderchars" entry to "dict".
3104 */
3105 static void
3106get_borderchars(dict_T *dict, win_T *wp)
3107{
3108 list_T *list;
3109 int i;
3110 char_u buf[NUMBUFLEN];
3111 int len;
3112
3113 for (i = 0; i < 8; ++i)
3114 if (wp->w_border_char[i] != 0)
3115 break;
3116 if (i == 8)
3117 return;
3118
3119 list = list_alloc();
3120 if (list != NULL)
3121 {
3122 dict_add_list(dict, "borderchars", list);
3123 for (i = 0; i < 8; ++i)
3124 {
3125 len = mb_char2bytes(wp->w_border_char[i], buf);
3126 list_append_string(list, buf, len);
3127 }
3128 }
3129}
3130
3131/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003132 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003133 */
3134 static void
3135get_moved_list(dict_T *dict, win_T *wp)
3136{
3137 list_T *list;
3138
3139 list = list_alloc();
3140 if (list != NULL)
3141 {
3142 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003143 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003144 list_append_number(list, wp->w_popup_mincol);
3145 list_append_number(list, wp->w_popup_maxcol);
3146 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003147 list = list_alloc();
3148 if (list != NULL)
3149 {
3150 dict_add_list(dict, "mousemoved", list);
3151 list_append_number(list, wp->w_popup_mouse_row);
3152 list_append_number(list, wp->w_popup_mouse_mincol);
3153 list_append_number(list, wp->w_popup_mouse_maxcol);
3154 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003155}
3156
3157/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003158 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003159 */
3160 void
3161f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3162{
3163 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003164 int id;
3165 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003166 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003167 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003168
3169 if (rettv_dict_alloc(rettv) == OK)
3170 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003171 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3172 return;
3173
3174 id = (int)tv_get_number(argvars);
3175 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003176 if (wp == NULL)
3177 return;
3178
3179 dict = rettv->vval.v_dict;
3180 dict_add_number(dict, "line", wp->w_wantline);
3181 dict_add_number(dict, "col", wp->w_wantcol);
3182 dict_add_number(dict, "minwidth", wp->w_minwidth);
3183 dict_add_number(dict, "minheight", wp->w_minheight);
3184 dict_add_number(dict, "maxheight", wp->w_maxheight);
3185 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003186 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003187 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003188 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003189 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003190 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003191 {
3192 proptype_T *pt = text_prop_type_by_id(
3193 wp->w_popup_prop_win->w_buffer,
3194 wp->w_popup_prop_type);
3195
3196 if (pt != NULL)
3197 dict_add_string(dict, "textprop", pt->pt_name);
3198 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3199 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3200 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003201 dict_add_string(dict, "title", wp->w_popup_title);
3202 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003203 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003204 dict_add_number(dict, "dragall",
3205 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003206 dict_add_number(dict, "mapping",
3207 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003208 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003209 dict_add_number(dict, "posinvert",
3210 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003211 dict_add_number(dict, "cursorline",
3212 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003213 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003214 if (wp->w_scrollbar_highlight != NULL)
3215 dict_add_string(dict, "scrollbarhighlight",
3216 wp->w_scrollbar_highlight);
3217 if (wp->w_thumb_highlight != NULL)
3218 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003219
Bram Moolenaara3fce622019-06-20 02:31:49 +02003220 // find the tabpage that holds this popup
3221 i = 1;
3222 FOR_ALL_TABPAGES(tp)
3223 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003224 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003225
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003226 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3227 if (twp->w_id == id)
3228 break;
3229 if (twp != NULL)
3230 break;
3231 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003232 }
3233 if (tp == NULL)
3234 i = -1; // must be global
3235 else if (tp == curtab)
3236 i = 0;
3237 dict_add_number(dict, "tabpage", i);
3238
Bram Moolenaarae943152019-06-16 22:54:14 +02003239 get_padding_border(dict, wp->w_popup_padding, "padding");
3240 get_padding_border(dict, wp->w_popup_border, "border");
3241 get_borderhighlight(dict, wp);
3242 get_borderchars(dict, wp);
3243 get_moved_list(dict, wp);
3244
3245 if (wp->w_filter_cb.cb_name != NULL)
3246 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3247 if (wp->w_close_cb.cb_name != NULL)
3248 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003249
K.Takataeeec2542021-06-02 13:28:16 +02003250 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003251 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3252 {
3253 dict_add_string(dict, "pos",
3254 (char_u *)poppos_entries[i].pp_name);
3255 break;
3256 }
3257
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003258 dict_add_string(dict, "close", (char_u *)(
3259 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3260 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3261
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003262# if defined(FEAT_TIMERS)
3263 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3264 ? (long)wp->w_popup_timer->tr_interval : 0L);
3265# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003266 }
3267}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003268
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003269# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003270/*
3271 * Return TRUE if the current window is running a terminal in a popup window.
3272 * Return FALSE when the job has ended.
3273 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003274 int
3275error_if_term_popup_window()
3276{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003277 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3278 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003279 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003280 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003281 return TRUE;
3282 }
3283 return FALSE;
3284}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003285# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003286
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003287/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003288 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003289 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003290 * Each calling function should use a different flag, see the list at
3291 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003292 */
3293 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003294popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003295{
3296 win_T *wp;
3297
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003298 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003299 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003300 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003301 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003302}
3303
3304/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003305 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003306 * Must have called popup_reset_handled() first.
3307 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3308 * popup with the highest zindex.
3309 */
3310 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003311find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003312{
3313 win_T *wp;
3314 win_T *found_wp;
3315 int found_zindex;
3316
3317 found_zindex = lowest ? INT_MAX : 0;
3318 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003319 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003320 if ((wp->w_popup_handled & handled_flag) == 0
3321 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003322 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003323 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003324 {
3325 found_zindex = wp->w_zindex;
3326 found_wp = wp;
3327 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003328 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003329 if ((wp->w_popup_handled & handled_flag) == 0
3330 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003331 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003332 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003333 {
3334 found_zindex = wp->w_zindex;
3335 found_wp = wp;
3336 }
3337
3338 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003339 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003340 return found_wp;
3341}
3342
3343/*
3344 * Invoke the filter callback for window "wp" with typed character "c".
3345 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003346 * Returns the return value of the filter or -1 for CTRL-C in the current
3347 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003348 * Careful: The filter may make "wp" invalid!
3349 */
3350 static int
3351invoke_popup_filter(win_T *wp, int c)
3352{
3353 int res;
3354 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003355 typval_T argv[3];
3356 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003357 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003358 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003359
Bram Moolenaara42d9452019-06-15 21:46:30 +02003360 // Emergency exit: CTRL-C closes the popup.
3361 if (c == Ctrl_C)
3362 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003363 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003364 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003365
3366 // Reset got_int to avoid the callback isn't called.
3367 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003368 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003369 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003370
3371 // If the popup is the current window it probably fails to close. Then
3372 // do not consume the key.
3373 if (was_curwin && wp == curwin)
3374 return -1;
3375 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003376 }
3377
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003378 argv[0].v_type = VAR_NUMBER;
3379 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3380
3381 // Convert the number to a string, so that the function can use:
3382 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003383 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003384 argv[1].v_type = VAR_STRING;
3385 argv[1].vval.v_string = vim_strsave(buf);
3386
3387 argv[2].v_type = VAR_UNKNOWN;
3388
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003389 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003390 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3391 {
3392 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003393 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003394 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003395 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003396 }
3397 else
3398 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003399 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3400 popup_highlight_curline(wp);
3401
Bram Moolenaar371806e2020-10-22 13:44:54 +02003402 // If an error message was given always return FALSE, so that keys are
3403 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003404 // If we get three errors in a row then close the popup. Decrement the
3405 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3406 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003407 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003408 {
3409 wp->w_filter_errors += 10;
3410 if (wp->w_filter_errors >= 30)
3411 popup_close_with_retval(wp, -1);
3412 res = FALSE;
3413 }
3414 else
3415 {
3416 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3417 --wp->w_filter_errors;
3418 res = tv_get_bool(&rettv);
3419 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003420 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003421
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003422 vim_free(argv[1].vval.v_string);
3423 clear_tv(&rettv);
3424 return res;
3425}
3426
3427/*
3428 * Called when "c" was typed: invoke popup filter callbacks.
3429 * Returns TRUE when the character was consumed,
3430 */
3431 int
3432popup_do_filter(int c)
3433{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003434 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003435 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003436 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003437 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003438 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003439 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003440
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003441#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003442 // Popup window with terminal always gets focus.
3443 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3444 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003445#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003446
Bram Moolenaar934470e2019-09-01 23:27:05 +02003447 if (recursive)
3448 return FALSE;
3449 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003450
Bram Moolenaarf6396232019-08-24 19:36:00 +02003451 if (c == K_LEFTMOUSE)
3452 {
3453 int row = mouse_row;
3454 int col = mouse_col;
3455
3456 wp = mouse_find_win(&row, &col, FIND_POPUP);
3457 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003458 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003459 }
3460
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003461 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003462 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003463 while (res == FALSE
3464 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003465 if (wp->w_filter_cb.cb_name != NULL
3466 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003467 res = invoke_popup_filter(wp, c);
3468
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003469 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003470 {
3471 int save_got_int = got_int;
3472
3473 // Reset got_int to avoid a function used in the statusline aborts.
3474 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003475 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003476 got_int |= save_got_int;
3477 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003478 recursive = FALSE;
3479 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003480
3481 // When interrupted return FALSE to avoid looping.
3482 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003483}
3484
Bram Moolenaar3397f742019-06-02 18:40:06 +02003485/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003486 * Return TRUE if there is a popup visible with a filter callback and the
3487 * "mapping" property off.
3488 */
3489 int
3490popup_no_mapping(void)
3491{
3492 int round;
3493 win_T *wp;
3494
3495 for (round = 1; round <= 2; ++round)
3496 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3497 wp != NULL; wp = wp->w_next)
3498 if (wp->w_filter_cb.cb_name != NULL
3499 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3500 return TRUE;
3501 return FALSE;
3502}
3503
3504/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003505 * Called when the cursor moved: check if any popup needs to be closed if the
3506 * cursor moved far enough.
3507 */
3508 void
3509popup_check_cursor_pos()
3510{
3511 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003512
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003513 popup_reset_handled(POPUP_HANDLED_3);
3514 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003515 if (wp->w_popup_curwin != NULL
3516 && (curwin != wp->w_popup_curwin
3517 || curwin->w_cursor.lnum != wp->w_popup_lnum
3518 || curwin->w_cursor.col < wp->w_popup_mincol
3519 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003520 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003521}
3522
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003523/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003524 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003525 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003526 static void
3527popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003528{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003529 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003530 char_u *cells;
3531 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003532
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003533 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3534 {
3535 vim_free(wp->w_popup_mask_cells);
3536 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003537 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003538 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003539 if (wp->w_popup_mask_cells != NULL
3540 && wp->w_popup_mask_height == height
3541 && wp->w_popup_mask_width == width)
3542 return; // cache is still valid
3543
3544 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003545 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003546 if (wp->w_popup_mask_cells == NULL)
3547 return;
3548 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003549
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003550 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003551 {
3552 int cols, cole;
3553 int lines, linee;
3554
3555 li = lio->li_tv.vval.v_list->lv_first;
3556 cols = tv_get_number(&li->li_tv);
3557 if (cols < 0)
3558 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003559 if (cols <= 0)
3560 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003561 li = li->li_next;
3562 cole = tv_get_number(&li->li_tv);
3563 if (cole < 0)
3564 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003565 if (cole > width)
3566 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003567 li = li->li_next;
3568 lines = tv_get_number(&li->li_tv);
3569 if (lines < 0)
3570 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003571 if (lines <= 0)
3572 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003573 li = li->li_next;
3574 linee = tv_get_number(&li->li_tv);
3575 if (linee < 0)
3576 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003577 if (linee > height)
3578 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003579
Bram Moolenaar4012d262020-12-29 11:57:46 +01003580 for (row = lines - 1; row < linee; ++row)
3581 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003582 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003583 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003584}
3585
3586/*
3587 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3588 * "col" and "line" are screen coordinates.
3589 */
3590 static int
3591popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3592{
3593 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3594 int line = screenline - wp->w_winrow;
3595
3596 return col >= 0 && col < width
3597 && line >= 0 && line < height
3598 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003599}
3600
3601/*
3602 * Set flags in popup_transparent[] for window "wp" to "val".
3603 */
3604 static void
3605update_popup_transparent(win_T *wp, int val)
3606{
3607 if (wp->w_popup_mask != NULL)
3608 {
3609 int width = popup_width(wp);
3610 int height = popup_height(wp);
3611 listitem_T *lio, *li;
3612 int cols, cole;
3613 int lines, linee;
3614 int col, line;
3615
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003616 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003617 {
3618 li = lio->li_tv.vval.v_list->lv_first;
3619 cols = tv_get_number(&li->li_tv);
3620 if (cols < 0)
3621 cols = width + cols + 1;
3622 li = li->li_next;
3623 cole = tv_get_number(&li->li_tv);
3624 if (cole < 0)
3625 cole = width + cole + 1;
3626 li = li->li_next;
3627 lines = tv_get_number(&li->li_tv);
3628 if (lines < 0)
3629 lines = height + lines + 1;
3630 li = li->li_next;
3631 linee = tv_get_number(&li->li_tv);
3632 if (linee < 0)
3633 linee = height + linee + 1;
3634
3635 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003636 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003637 if (cols < 0)
3638 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003639 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003640 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003641 if (lines < 0)
3642 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003643 for (line = lines; line < linee
3644 && line + wp->w_winrow < screen_Rows; ++line)
3645 for (col = cols; col < cole
3646 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003647 popup_transparent[(line + wp->w_winrow) * screen_Columns
3648 + col + wp->w_wincol] = val;
3649 }
3650 }
3651}
3652
3653/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003654 * Only called when popup window "wp" is hidden: If the window is positioned
3655 * next to a text property, and it is now visible, then unhide the popup.
3656 * We don't check if visible popups become hidden, that is done in
3657 * popup_adjust_position().
3658 * Return TRUE if the popup became unhidden.
3659 */
3660 static int
3661check_popup_unhidden(win_T *wp)
3662{
3663 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3664 {
3665 textprop_T prop;
3666 linenr_T lnum;
3667
Bram Moolenaar27724252022-05-08 15:00:04 +01003668 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3669 && find_visible_prop(wp->w_popup_prop_win,
3670 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003671 &prop, &lnum) == OK)
3672 {
3673 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003674 wp->w_popup_prop_topline = 0; // force repositioning
3675 return TRUE;
3676 }
3677 }
3678 return FALSE;
3679}
3680
3681/*
3682 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3683 * That is when the buffer in the popup was changed, or the popup is following
3684 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003685 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003686 */
3687 static int
3688popup_need_position_adjust(win_T *wp)
3689{
3690 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3691 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003692 if (win_valid(wp->w_popup_prop_win)
3693 && (wp->w_popup_prop_changedtick
3694 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3695 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3696 return TRUE;
3697
3698 // May need to adjust the width if the cursor moved.
3699 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003700}
3701
3702/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003703 * Update "popup_mask" if needed.
3704 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003705 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003706 * Also marks window lines for redrawing.
3707 */
3708 void
3709may_update_popup_mask(int type)
3710{
3711 win_T *wp;
3712 short *mask;
3713 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003714 int redraw_all_popups = FALSE;
3715 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003716
3717 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003718 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3719 // basic (such as the screen size) must have changed.
3720 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003721 {
3722 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003723 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003724 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003725
3726 // Check if any popup window buffer has changed and if any popup connected
3727 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003728 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003729 if (wp->w_popup_flags & POPF_HIDDEN)
3730 popup_mask_refresh |= check_popup_unhidden(wp);
3731 else if (popup_need_position_adjust(wp))
3732 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003733 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003734 if (wp->w_popup_flags & POPF_HIDDEN)
3735 popup_mask_refresh |= check_popup_unhidden(wp);
3736 else if (popup_need_position_adjust(wp))
3737 popup_mask_refresh = TRUE;
3738
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003740 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003741
3742 // Need to update the mask, something has changed.
3743 popup_mask_refresh = FALSE;
3744 popup_mask_tab = curtab;
3745 popup_visible = FALSE;
3746
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003747 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003748 // If redrawing only what is needed, update "popup_mask_next" and then
3749 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003750 redrawing_all_win = TRUE;
3751 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003752 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003753 redrawing_all_win = FALSE;
3754 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003755 mask = popup_mask;
3756 else
3757 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003758 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003759
3760 // Find the window with the lowest zindex that hasn't been handled yet,
3761 // so that the window with a higher zindex overwrites the value in
3762 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003763 popup_reset_handled(POPUP_HANDLED_4);
3764 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003765 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003766 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003767 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003768
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003769 popup_visible = TRUE;
3770
Bram Moolenaar12034e22019-08-25 22:25:02 +02003771 // Recompute the position if the text changed. It may make the popup
3772 // hidden if it's attach to a text property that is no longer visible.
3773 if (redraw_all_popups || popup_need_position_adjust(wp))
3774 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003776 if (wp->w_popup_flags & POPF_HIDDEN)
3777 continue;
3778 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003779
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003780 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003781 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003782 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003783 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003784 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003786 col < wp->w_wincol + width - wp->w_popup_leftoff
3787 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003788 if (wp->w_zindex < POPUPMENU_ZINDEX
3789 && pum_visible()
3790 && pum_under_menu(line, col, FALSE))
3791 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3792 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003793 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003794 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003795 }
3796
3797 // Only check which lines are to be updated if not already
3798 // updating all lines.
3799 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003800 {
3801 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3802 win_T *prev_wp = NULL;
3803
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003804 for (line = 0; line < screen_Rows; ++line)
3805 {
3806 int col_done = 0;
3807
3808 for (col = 0; col < screen_Columns; ++col)
3809 {
3810 int off = line * screen_Columns + col;
3811
3812 if (popup_mask[off] != popup_mask_next[off])
3813 {
3814 popup_mask[off] = popup_mask_next[off];
3815
3816 if (line >= cmdline_row)
3817 {
3818 // the command line needs to be cleared if text below
3819 // the popup is now visible.
3820 if (!msg_scrolled && popup_mask_next[off] == 0)
3821 clear_cmdline = TRUE;
3822 }
3823 else if (col >= col_done)
3824 {
3825 linenr_T lnum;
3826 int line_cp = line;
3827 int col_cp = col;
3828
3829 // The screen position "line" / "col" needs to be
3830 // redrawn. Figure out what window that is and update
3831 // w_redraw_top and w_redr_bot. Only needs to be done
3832 // once for each window line.
3833 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3834 if (wp != NULL)
3835 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003836#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003837 // A terminal window needs to be redrawn.
3838 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003839 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003840 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003841#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003842 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003843 if (wp != prev_wp)
3844 {
3845 vim_memset(plines_cache, 0,
3846 sizeof(int) * Rows);
3847 prev_wp = wp;
3848 }
3849
3850 if (line_cp >= wp->w_height)
3851 // In (or below) status line
3852 wp->w_redr_status = TRUE;
3853 else
3854 {
3855 // compute the position in the buffer line
3856 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003857 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003858 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003859 redrawWinline(wp, lnum);
3860 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003861 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003862
3863 // This line is going to be redrawn, no need to
3864 // check until the right side of the window.
3865 col_done = wp->w_wincol + wp->w_width - 1;
3866 }
3867 }
3868 }
3869 }
3870 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003871
3872 vim_free(plines_cache);
3873 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003874
3875 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003876}
3877
3878/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003879 * If the current window is a popup and something relevant changed, recompute
3880 * the position and size.
3881 */
3882 void
3883may_update_popup_position(void)
3884{
3885 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3886 popup_adjust_position(curwin);
3887}
3888
3889/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003890 * Return a string of "len" spaces in IObuff.
3891 */
3892 static char_u *
3893get_spaces(int len)
3894{
3895 vim_memset(IObuff, ' ', (size_t)len);
3896 IObuff[len] = NUL;
3897 return IObuff;
3898}
3899
3900/*
3901 * Update popup windows. They are drawn on top of normal windows.
3902 * "win_update" is called for each popup window, lowest zindex first.
3903 */
3904 void
3905update_popups(void (*win_update)(win_T *wp))
3906{
3907 win_T *wp;
3908 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003909 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003910 int total_width;
3911 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003912 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003913 int popup_attr;
3914 int border_attr[4];
3915 int border_char[8];
3916 char_u buf[MB_MAXBYTES];
3917 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003918 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003919 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003920 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003921 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003922 int sb_thumb_top = 0;
3923 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003924 int attr_scroll = 0;
3925 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003926
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003927 // hide the cursor until redrawing is done.
3928 cursor_off();
3929
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003930 // Find the window with the lowest zindex that hasn't been updated yet,
3931 // so that the window with a higher zindex is drawn later, thus goes on
3932 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003933 popup_reset_handled(POPUP_HANDLED_5);
3934 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003936 int title_len = 0;
3937 int title_wincol;
3938
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003939 // This drawing uses the zindex of the popup window, so that it's on
3940 // top of the text but doesn't draw when another popup with higher
3941 // zindex is on top of the character.
3942 screen_zindex = wp->w_zindex;
3943
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003944 // Set flags in popup_transparent[] for masked cells.
3945 update_popup_transparent(wp, 1);
3946
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003947 // adjust w_winrow and w_wincol for border and padding, since
3948 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003949 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003950 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3951 - wp->w_popup_leftoff;
3952 if (wp->w_wincol + left_extra < 0)
3953 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003954 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003955 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003956
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003957 // Draw the popup text, unless it's off screen.
3958 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003959 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003960 // May need to update the "cursorline" highlighting, which may also
3961 // change "topline"
3962 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3963 popup_highlight_curline(wp);
3964
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003965 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003966
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003967 // move the cursor into the visible lines, otherwise executing
3968 // commands with win_execute() may cause the text to jump.
3969 if (wp->w_cursor.lnum < wp->w_topline)
3970 wp->w_cursor.lnum = wp->w_topline;
3971 else if (wp->w_cursor.lnum >= wp->w_botline)
3972 wp->w_cursor.lnum = wp->w_botline - 1;
3973 }
3974
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003975 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003976 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003977
3978 // Add offset for border and padding if not done already.
3979 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
3980 {
3981 wp->w_wcol += left_extra;
3982 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
3983 }
3984 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003985 {
3986 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003987 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003988 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003989
Bram Moolenaareabddc42022-04-02 15:32:16 +01003990 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003991 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003992 popup_attr = get_wcr_attr(wp);
3993
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003994 if (wp->w_winrow + total_height > cmdline_row)
3995 wp->w_popup_flags |= POPF_ON_CMDLINE;
3996 else
3997 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3998
3999
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004000 // We can only use these line drawing characters when 'encoding' is
4001 // "utf-8" and 'ambiwidth' is "single".
4002 if (enc_utf8 && *p_ambw == 's')
4003 {
4004 border_char[0] = border_char[2] = 0x2550;
4005 border_char[1] = border_char[3] = 0x2551;
4006 border_char[4] = 0x2554;
4007 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004008 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4009 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004010 border_char[7] = 0x255a;
4011 }
4012 else
4013 {
4014 border_char[0] = border_char[2] = '-';
4015 border_char[1] = border_char[3] = '|';
4016 for (i = 4; i < 8; ++i)
4017 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004018 if (wp->w_popup_flags & POPF_RESIZE)
4019 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004020 }
4021 for (i = 0; i < 8; ++i)
4022 if (wp->w_border_char[i] != 0)
4023 border_char[i] = wp->w_border_char[i];
4024
4025 for (i = 0; i < 4; ++i)
4026 {
4027 border_attr[i] = popup_attr;
4028 if (wp->w_border_highlight[i] != NULL)
4029 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4030 }
4031
Bram Moolenaard91467f2020-11-21 12:42:09 +01004032 // Title goes on top of border or padding.
4033 title_wincol = wp->w_wincol + 1;
4034 if (wp->w_popup_title != NULL)
4035 {
rbtnnc6119412021-08-07 13:08:45 +02004036 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004037
Ralf Schandlbc869872021-05-28 14:12:14 +02004038 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004039 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004040 {
4041 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4042 char_u *title_text = alloc(title_byte_len + 1);
4043
4044 if (title_text != NULL)
4045 {
4046 trunc_string(wp->w_popup_title, title_text,
4047 total_width - 2, title_byte_len + 1);
4048 screen_puts(title_text, wp->w_winrow, title_wincol,
4049 wp->w_popup_border[0] > 0
4050 ? border_attr[0] : popup_attr);
4051 vim_free(title_text);
4052 }
4053
Bram Moolenaard91467f2020-11-21 12:42:09 +01004054 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004055 }
4056 else
4057 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4058 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004059 }
4060
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004061 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004062 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004063 if (wp->w_popup_border[0] > 0)
4064 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004065 // top border; do not draw over the title
4066 if (title_len > 0)
4067 {
4068 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4069 wincol < 0 ? 0 : wincol, title_wincol,
4070 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004071 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004072 border_char[0], border_attr[0]);
4073 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4074 title_wincol + title_len, wincol + total_width,
4075 border_char[0], border_char[0], border_attr[0]);
4076 }
4077 else
4078 {
4079 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4080 wincol < 0 ? 0 : wincol, wincol + total_width,
4081 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4082 ? border_char[4] : border_char[0],
4083 border_char[0], border_attr[0]);
4084 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004085 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004086 {
4087 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4088 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004089 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004090 }
4091 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004092 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4093 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004094
Bram Moolenaard529ba52019-07-02 23:13:53 +02004095 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4096 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004097 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004098 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004099 - wp->w_has_scrollbar;
4100 if (padcol < 0)
4101 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004102 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004103 padcol = 0;
4104 }
4105 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004106 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004107 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004109 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004110 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004111 // top padding and no border; do not draw over the title
4112 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004113 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004114 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004115 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004116 row += 1;
4117 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004118 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004119 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004120 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004121 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004122
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004123 // Compute scrollbar thumb position and size.
4124 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004125 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004126 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4127 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004128 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004129
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004130 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4131 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004132 if (wp->w_topline > 1 && sb_thumb_height == height)
4133 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004134 if (sb_thumb_height == 0)
4135 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004136 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004137 // it just fits, avoid divide by zero
4138 sb_thumb_top = 0;
4139 else
4140 sb_thumb_top = (wp->w_topline - 1
4141 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004142 * (wp->w_height - sb_thumb_height)
4143 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004144 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4145 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004146 last = total_height - top_off - wp->w_popup_border[2];
4147 if (sb_thumb_top >= last)
4148 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004149 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004150
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004151 if (wp->w_scrollbar_highlight != NULL)
4152 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4153 else
4154 attr_scroll = highlight_attr[HLF_PSB];
4155 if (wp->w_thumb_highlight != NULL)
4156 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4157 else
4158 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004159 }
4160
4161 for (i = wp->w_popup_border[0];
4162 i < total_height - wp->w_popup_border[2]; ++i)
4163 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004164 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004165 // left and right padding only needed next to the body
4166 int do_padding =
4167 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4168 && i < total_height - wp->w_popup_border[2]
4169 - wp->w_popup_padding[2];
4170
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004171 row = wp->w_winrow + i;
4172
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004173 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004174 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004175 {
4176 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004177 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004178 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004179 if (do_padding && wp->w_popup_padding[3] > 0)
4180 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004181 int col = wincol + wp->w_popup_border[3];
4182
Bram Moolenaard529ba52019-07-02 23:13:53 +02004183 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004184 pad_left = wp->w_popup_padding[3];
4185 if (col < 0)
4186 {
4187 pad_left += col;
4188 col = 0;
4189 }
4190 if (pad_left > 0)
4191 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4192 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004193 // scrollbar
4194 if (wp->w_has_scrollbar)
4195 {
4196 int line = i - top_off;
4197 int scroll_col = wp->w_wincol + total_width - 1
4198 - wp->w_popup_border[1];
4199
4200 if (line >= 0 && line < wp->w_height)
4201 screen_putchar(' ', row, scroll_col,
4202 line >= sb_thumb_top
4203 && line < sb_thumb_top + sb_thumb_height
4204 ? attr_thumb : attr_scroll);
4205 else
4206 screen_putchar(' ', row, scroll_col, popup_attr);
4207 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004208 // right border
4209 if (wp->w_popup_border[1] > 0)
4210 {
4211 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004212 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004213 }
4214 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004215 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004216 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004217 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004218 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4219 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004220 }
4221
4222 if (wp->w_popup_padding[2] > 0)
4223 {
4224 // bottom padding
4225 row = wp->w_winrow + wp->w_popup_border[0]
4226 + wp->w_popup_padding[0] + wp->w_height;
4227 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004228 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004229 }
4230
4231 if (wp->w_popup_border[2] > 0)
4232 {
4233 // bottom border
4234 row = wp->w_winrow + total_height - 1;
4235 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004236 wincol < 0 ? 0 : wincol,
4237 wincol + total_width,
4238 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004239 ? border_char[7] : border_char[2],
4240 border_char[2], border_attr[2]);
4241 if (wp->w_popup_border[1] > 0)
4242 {
4243 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004244 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004245 }
4246 }
4247
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004248 if (wp->w_popup_close == POPCLOSE_BUTTON)
4249 {
4250 // close button goes on top of anything at the top-right corner
4251 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004252 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004253 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4254 }
4255
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004256 update_popup_transparent(wp, 0);
4257
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004258 // Back to the normal zindex.
4259 screen_zindex = 0;
4260 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004261
4262#if defined(FEAT_SEARCH_EXTRA)
4263 // In case win_update() called start_search_hl().
4264 end_search_hl();
4265#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004266}
4267
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004268/*
4269 * Mark references in callbacks of one popup window.
4270 */
4271 static int
4272set_ref_in_one_popup(win_T *wp, int copyID)
4273{
4274 int abort = FALSE;
4275 typval_T tv;
4276
4277 if (wp->w_close_cb.cb_partial != NULL)
4278 {
4279 tv.v_type = VAR_PARTIAL;
4280 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4281 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4282 }
4283 if (wp->w_filter_cb.cb_partial != NULL)
4284 {
4285 tv.v_type = VAR_PARTIAL;
4286 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4287 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4288 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004289 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004290 return abort;
4291}
4292
4293/*
4294 * Set reference in callbacks of popup windows.
4295 */
4296 int
4297set_ref_in_popups(int copyID)
4298{
4299 int abort = FALSE;
4300 win_T *wp;
4301 tabpage_T *tp;
4302
4303 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4304 abort = abort || set_ref_in_one_popup(wp, copyID);
4305
4306 FOR_ALL_TABPAGES(tp)
4307 {
4308 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4309 abort = abort || set_ref_in_one_popup(wp, copyID);
4310 if (abort)
4311 break;
4312 }
4313 return abort;
4314}
Bram Moolenaar79648732019-07-18 21:43:07 +02004315
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004316 int
4317popup_is_popup(win_T *wp)
4318{
4319 return wp->w_popup_flags != 0;
4320}
4321
4322#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004323/*
4324 * Find an existing popup used as the preview window, in the current tab page.
4325 * Return NULL if not found.
4326 */
4327 win_T *
4328popup_find_preview_window(void)
4329{
4330 win_T *wp;
4331
4332 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004333 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004334 if (wp->w_p_pvw)
4335 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004336 return NULL;
4337}
4338
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004339/*
4340 * Find an existing popup used as the info window, in the current tab page.
4341 * Return NULL if not found.
4342 */
4343 win_T *
4344popup_find_info_window(void)
4345{
4346 win_T *wp;
4347
4348 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004349 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004350 if (wp->w_popup_flags & POPF_INFO)
4351 return wp;
4352 return NULL;
4353}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004354#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004355
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004356 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004357f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4358{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004359#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004360 win_T *wp = popup_find_info_window();
4361
4362 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004363#else
4364 rettv->vval.v_number = 0;
4365#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004366}
4367
4368 void
4369f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004370{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004371#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004372 win_T *wp = popup_find_preview_window();
4373
4374 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004375#else
4376 rettv->vval.v_number = 0;
4377#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004378}
4379
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004380#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004381/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004382 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004383 * NOTE: this makes the popup the current window, so that the file can be
4384 * edited. However, it must not remain to be the current window, the caller
4385 * must make sure of that.
4386 */
4387 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004388popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004389{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004390 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004391
4392 if (wp == NULL)
4393 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004394 if (info)
4395 wp->w_popup_flags |= POPF_INFO;
4396 else
4397 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004398
4399 // Set the width to a reasonable value, so that w_topline can be computed.
4400 if (wp->w_minwidth > 0)
4401 wp->w_width = wp->w_minwidth;
4402 else if (wp->w_maxwidth > 0)
4403 wp->w_width = wp->w_maxwidth;
4404 else
4405 wp->w_width = curwin->w_width;
4406
4407 // Will switch to another buffer soon, dummy one can be wiped.
4408 wp->w_buffer->b_locked = FALSE;
4409
4410 win_enter(wp, FALSE);
4411 return OK;
4412}
4413
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004414/*
4415 * Close any preview popup.
4416 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004417 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004418popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004419{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004420 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004421
4422 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004423 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004424}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004425
4426/*
4427 * Hide the info popup.
4428 */
4429 void
4430popup_hide_info(void)
4431{
4432 win_T *wp = popup_find_info_window();
4433
4434 if (wp != NULL)
4435 popup_hide(wp);
4436}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004437
4438/*
4439 * Close any info popup.
4440 */
4441 void
4442popup_close_info(void)
4443{
4444 win_T *wp = popup_find_info_window();
4445
4446 if (wp != NULL)
4447 popup_close_with_retval(wp, -1);
4448}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004449#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004450
Bram Moolenaar9198de32022-08-27 21:30:03 +01004451#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4452
Bram Moolenaar9198de32022-08-27 21:30:03 +01004453/*
4454 * Get the message window.
4455 * Returns NULL if something failed.
4456 */
4457 win_T *
4458popup_get_message_win(void)
4459{
4460 if (message_win == NULL)
4461 {
4462 int i;
4463
4464 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4465
4466 if (message_win == NULL)
4467 return NULL;
4468
4469 // use the full screen width
4470 message_win->w_width = Columns;
4471
4472 // position at bottom of screen
4473 message_win->w_popup_pos = POPPOS_BOTTOM;
4474 message_win->w_wantcol = 1;
4475 message_win->w_minwidth = 9999;
4476
4477 // no padding, border at the top
4478 for (i = 0; i < 4; ++i)
4479 message_win->w_popup_padding[i] = 0;
4480 for (i = 1; i < 4; ++i)
4481 message_win->w_popup_border[i] = 0;
4482
4483 if (message_win->w_popup_timer != NULL)
4484 message_win->w_popup_timer->tr_keep = TRUE;
4485 }
4486 return message_win;
4487}
4488
4489/*
4490 * If the message window is not visible: show it
4491 * If the message window is visible: reset the timeout
4492 */
4493 void
4494popup_show_message_win(void)
4495{
4496 if (message_win != NULL)
4497 {
4498 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4499 {
4500 // the highlight may have changed.
4501 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4502 popup_show(message_win);
4503 }
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004504 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004505 timer_start(message_win->w_popup_timer);
4506 }
4507}
4508
4509 int
4510popup_message_win_visible(void)
4511{
4512 return message_win != NULL
4513 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4514}
4515
4516/*
4517 * If the message window is visible: hide it.
4518 */
4519 void
4520popup_hide_message_win(void)
4521{
4522 if (message_win != NULL)
4523 popup_hide(message_win);
4524}
4525
Bram Moolenaar43568642022-08-28 13:02:45 +01004526/*
4527 * If the message window exists: close it.
4528 */
4529 void
4530popup_close_message_win(void)
4531{
4532 if (message_win != NULL)
4533 popup_close(message_win->w_id, TRUE);
4534}
4535
Bram Moolenaar9198de32022-08-27 21:30:03 +01004536#endif
4537
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004538/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004539 * Close any popup for a text property associated with "win".
4540 * Return TRUE if a popup was closed.
4541 */
4542 int
4543popup_win_closed(win_T *win)
4544{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004545 int round;
4546 win_T *wp;
4547 win_T *next;
4548 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004549
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004550 for (round = 1; round <= 2; ++round)
4551 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4552 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004553 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004554 next = wp->w_next;
4555 if (wp->w_popup_prop_win == win)
4556 {
4557 popup_close_with_retval(wp, -1);
4558 ret = TRUE;
4559 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004560 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004561 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004562}
4563
4564/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004565 * Set the title of the popup window to the file name.
4566 */
4567 void
4568popup_set_title(win_T *wp)
4569{
4570 if (wp->w_buffer->b_fname != NULL)
4571 {
4572 char_u dirname[MAXPATHL];
4573 size_t len;
4574
4575 mch_dirname(dirname, MAXPATHL);
4576 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4577
4578 vim_free(wp->w_popup_title);
4579 len = STRLEN(wp->w_buffer->b_fname) + 3;
4580 wp->w_popup_title = alloc((int)len);
4581 if (wp->w_popup_title != NULL)
4582 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4583 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004584 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004585 }
4586}
4587
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004588# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004589/*
4590 * If there is a preview window, update the title.
4591 * Used after changing directory.
4592 */
4593 void
4594popup_update_preview_title(void)
4595{
4596 win_T *wp = popup_find_preview_window();
4597
4598 if (wp != NULL)
4599 popup_set_title(wp);
4600}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004601# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004602
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004603#endif // FEAT_PROP_POPUP