blob: 6e1d925690a0d922e7a063300d365f04a9aaf064 [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
Bram Moolenaara2a89732022-08-31 14:46:18 +010032// Window used for ":echowindow"
Bram Moolenaar43568642022-08-28 13:02:45 +010033static win_T *message_win = NULL;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +010034
Bram Moolenaarbdc09a12022-10-07 14:31:45 +010035// Time used for the next ":echowindow" message in msec.
36static int message_win_time = 3000;
37
Bram Moolenaarcf0995d2022-09-11 21:36:17 +010038// Flag set when a message is added to the message window, timer is started
39// when the message window is drawn. This might be after pressing Enter at the
40// hit-enter prompt.
41static int start_message_win_timer = FALSE;
42
43static void may_start_message_win_timer(win_T *wp);
Bram Moolenaar43568642022-08-28 13:02:45 +010044#endif
45
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020046static void popup_adjust_position(win_T *wp);
47
Bram Moolenaar4d784b22019-05-25 19:51:39 +020048/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020049 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020051 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020052 */
53 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020054popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055{
56 dictitem_T *di;
57 char_u *val;
58 char_u *s;
59 char_u *endp;
60 int n = 0;
61
62 di = dict_find(dict, key, -1);
63 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020064 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065
66 val = tv_get_string(&di->di_tv);
67 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020068 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020069
70 setcursor_mayforce(TRUE);
71 s = val + 6;
72 if (*s != NUL)
73 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020074 endp = s;
75 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
76 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 if (endp != NULL && *skipwhite(endp) != NUL)
78 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020079 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020080 return 0;
81 }
82 }
83
84 if (STRCMP(key, "line") == 0)
85 n = screen_screenrow() + 1 + n;
86 else // "col"
87 n = screen_screencol() + 1 + n;
88
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020089 // Zero means "not set", use -1 instead.
90 if (n == 0)
91 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020092 return n;
93}
94
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020095 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020096set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020097{
98 dictitem_T *di;
99
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000101 if (di == NULL)
102 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000104 if (di->di_tv.v_type != VAR_LIST)
105 {
106 emsg(_(e_list_required));
107 return;
108 }
109
110 list_T *list = di->di_tv.vval.v_list;
111 listitem_T *li;
112 int i;
113 int nr;
114
115 for (i = 0; i < 4; ++i)
116 array[i] = 1;
117 if (list == NULL)
118 return;
119
120 CHECK_LIST_MATERIALIZE(list);
121 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
122 ++i, li = li->li_next)
123 {
124 nr = (int)tv_get_number(&li->li_tv);
125 if (nr >= 0)
126 array[i] = nr > max_val ? max_val : nr;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200127 }
128}
129
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200130/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200131 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200132 */
133 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200134set_moved_values(win_T *wp)
135{
136 wp->w_popup_curwin = curwin;
137 wp->w_popup_lnum = curwin->w_cursor.lnum;
138 wp->w_popup_mincol = curwin->w_cursor.col;
139 wp->w_popup_maxcol = curwin->w_cursor.col;
140}
141
142/*
143 * Used when popup options contain "moved" with "word" or "WORD".
144 */
145 static void
146set_moved_columns(win_T *wp, int flags)
147{
148 char_u *ptr;
149 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
150
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000151 if (len <= 0)
152 return;
153
154 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
155 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
Bram Moolenaar17627312019-06-02 19:53:44 +0200156}
157
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200158/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200159 * Used when popup options contain "mousemoved": set default moved values.
160 */
161 static void
162set_mousemoved_values(win_T *wp)
163{
164 wp->w_popup_mouse_row = mouse_row;
165 wp->w_popup_mouse_mincol = mouse_col;
166 wp->w_popup_mouse_maxcol = mouse_col;
167}
168
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000169 static void
170update_popup_uses_mouse_move(void)
171{
172 popup_uses_mouse_move = FALSE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000173 if (!popup_visible)
174 return;
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000175
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000176 win_T *wp;
177
178 FOR_ALL_POPUPWINS(wp)
179 if (wp->w_popup_mouse_row != 0)
180 {
181 popup_uses_mouse_move = TRUE;
182 return;
183 }
184 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
185 if (wp->w_popup_mouse_row != 0)
186 {
187 popup_uses_mouse_move = TRUE;
188 return;
189 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000190}
191
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192/*
193 * Used when popup options contain "moved" with "word" or "WORD".
194 */
195 static void
196set_mousemoved_columns(win_T *wp, int flags)
197{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200199 char_u *text;
200 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200201 pos_T pos;
202 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200203
204 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000205 &textwp, &pos.lnum, &text, NULL, &col) != OK)
206 return;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200207
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000208 // convert text column to mouse column
209 pos.col = col;
210 pos.coladd = 0;
211 getvcol(textwp, &pos, &mcol, NULL, NULL);
212 wp->w_popup_mouse_mincol = mcol;
213
214 pos.col = col + (colnr_T)STRLEN(text) - 1;
215 getvcol(textwp, &pos, NULL, NULL, &mcol);
216 wp->w_popup_mouse_maxcol = mcol;
217 vim_free(text);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200218}
219
220/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200221 * Return TRUE if "row"/"col" is on the border of the popup.
222 * The values are relative to the top-left corner.
223 */
224 int
225popup_on_border(win_T *wp, int row, int col)
226{
227 return (row == 0 && wp->w_popup_border[0] > 0)
228 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
229 || (col == 0 && wp->w_popup_border[3] > 0)
230 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
231}
232
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200233/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200234 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
235 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200236 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200237 * Caller should check the left mouse button was clicked.
238 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200239 */
240 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200241popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200242{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200243 if (wp->w_popup_close == POPCLOSE_BUTTON
244 && row == 0 && col == popup_width(wp) - 1)
245 {
246 popup_close_for_mouse_click(wp);
247 return TRUE;
248 }
249 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200250}
251
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200252// Values set when dragging a popup window starts.
253static int drag_start_row;
254static int drag_start_col;
255static int drag_start_wantline;
256static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200257static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258
259/*
260 * Mouse down on border of popup window: start dragging it.
261 * Uses mouse_col and mouse_row.
262 */
263 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200265{
266 drag_start_row = mouse_row;
267 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200268 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200269 drag_start_wantline = wp->w_winrow + 1;
270 else
271 drag_start_wantline = wp->w_wantline;
272 if (wp->w_wantcol == 0)
273 drag_start_wantcol = wp->w_wincol + 1;
274 else
275 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200276
277 // Stop centering the popup
278 if (wp->w_popup_pos == POPPOS_CENTER)
279 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200280
281 drag_on_resize_handle = wp->w_popup_border[1] > 0
282 && wp->w_popup_border[2] > 0
283 && row == popup_height(wp) - 1
284 && col == popup_width(wp) - 1;
285
286 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
287 {
288 if (wp->w_popup_pos == POPPOS_TOPRIGHT
289 || wp->w_popup_pos == POPPOS_BOTRIGHT)
290 wp->w_wantcol = wp->w_wincol + 1;
291 if (wp->w_popup_pos == POPPOS_BOTLEFT)
292 wp->w_wantline = wp->w_winrow + 1;
293 wp->w_popup_pos = POPPOS_TOPLEFT;
294 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200295}
296
297/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200298 * Mouse moved while dragging a popup window: adjust the window popup position
299 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200300 */
301 void
302popup_drag(win_T *wp)
303{
304 // The popup may be closed before dragging stops.
305 if (!win_valid_popup(wp))
306 return;
307
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200308 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
309 {
310 int width_inc = mouse_col - drag_start_col;
311 int height_inc = mouse_row - drag_start_row;
312
313 if (width_inc != 0)
314 {
315 int width = wp->w_width + width_inc;
316
317 if (width < 1)
318 width = 1;
319 wp->w_minwidth = width;
320 wp->w_maxwidth = width;
321 drag_start_col = mouse_col;
322 }
323
324 if (height_inc != 0)
325 {
326 int height = wp->w_height + height_inc;
327
328 if (height < 1)
329 height = 1;
330 wp->w_minheight = height;
331 wp->w_maxheight = height;
332 drag_start_row = mouse_row;
333 }
334
335 popup_adjust_position(wp);
336 return;
337 }
338
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000339 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200340 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200341 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
342 if (wp->w_wantline < 1)
343 wp->w_wantline = 1;
344 if (wp->w_wantline > Rows)
345 wp->w_wantline = Rows;
346 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
347 if (wp->w_wantcol < 1)
348 wp->w_wantcol = 1;
349 if (wp->w_wantcol > Columns)
350 wp->w_wantcol = Columns;
351
352 popup_adjust_position(wp);
353}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200354
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200355/*
356 * Set w_firstline to match the current "wp->w_topline".
357 */
358 void
359popup_set_firstline(win_T *wp)
360{
361 int height = wp->w_height;
362
363 wp->w_firstline = wp->w_topline;
364 popup_adjust_position(wp);
365
366 // we don't want the popup to get smaller, decrement the first line
367 // until it doesn't
368 while (wp->w_firstline > 1 && wp->w_height < height)
369 {
370 --wp->w_firstline;
371 popup_adjust_position(wp);
372 }
373}
374
375/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200376 * Return TRUE if the position is in the popup window scrollbar.
377 */
378 int
379popup_is_in_scrollbar(win_T *wp, int row, int col)
380{
381 return wp->w_has_scrollbar
382 && row >= wp->w_popup_border[0]
383 && row < popup_height(wp) - wp->w_popup_border[2]
384 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
385}
386
387
388/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200389 * Handle a click in a popup window, if it is in the scrollbar.
390 */
391 void
392popup_handle_scrollbar_click(win_T *wp, int row, int col)
393{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000394 if (!popup_is_in_scrollbar(wp, row, col))
395 return;
Bram Moolenaard28950f2022-05-29 14:13:04 +0100396
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000397 int height = popup_height(wp);
398 int new_topline = wp->w_topline;
399
400 if (row >= height / 2)
401 {
402 // Click in lower half, scroll down.
403 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
404 ++new_topline;
405 }
406 else if (wp->w_topline > 1)
407 // click on upper half, scroll up.
408 --new_topline;
409
410 if (new_topline == wp->w_topline)
411 return;
412
413 set_topline(wp, new_topline);
414 if (wp == curwin)
415 {
416 if (wp->w_cursor.lnum < wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200417 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000418 wp->w_cursor.lnum = wp->w_topline;
419 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200420 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000421 else if (wp->w_cursor.lnum >= wp->w_botline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200422 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000423 wp->w_cursor.lnum = wp->w_botline - 1;
424 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200425 }
426 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000427 popup_set_firstline(wp);
428 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200429}
430
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200431#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432/*
433 * Add a timer to "wp" with "time".
434 * If "close" is true use popup_close(), otherwise popup_hide().
435 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200436 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100437popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200438{
439 char_u cbbuf[50];
440 char_u *ptr = cbbuf;
441 typval_T tv;
442
443 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100444 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
445 wp->w_id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000446 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) != OK)
447 return;
448
449 wp->w_popup_timer = create_timer(time, 0);
450 callback_T cb = get_callback(&tv);
451 if (cb.cb_name != NULL && !cb.cb_free_name)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200452 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000453 cb.cb_name = vim_strsave(cb.cb_name);
454 cb.cb_free_name = TRUE;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200455 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000456 wp->w_popup_timer->tr_callback = cb;
457 clear_tv(&tv);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200458}
459#endif
460
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100461 static poppos_T
462get_pos_entry(dict_T *d, int give_error)
463{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100465 int nr;
466
467 if (str == NULL)
468 return POPPOS_NONE;
469
K.Takataeeec2542021-06-02 13:28:16 +0200470 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100471 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
472 return poppos_entries[nr].pp_val;
473
474 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000475 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100476 return POPPOS_NONE;
477}
478
Bram Moolenaar17627312019-06-02 19:53:44 +0200479/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200480 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200481 */
482 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200483apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200484{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200485 int nr;
486 char_u *str;
487 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200488
Bram Moolenaard61efa52022-07-23 09:52:04 +0100489 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200490 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100491 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200492 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100493 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200494 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200496 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200497
498 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200499 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200500 wp->w_wantline = nr;
501 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200502 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200503 wp->w_wantcol = nr;
504
Bram Moolenaar55881332020-08-18 13:04:15 +0200505
Bram Moolenaard61efa52022-07-23 09:52:04 +0100506 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200507 if (nr != -1)
508 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200509
Bram Moolenaar12034e22019-08-25 22:25:02 +0200510 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100511 poppos_T ppt = get_pos_entry(d, TRUE);
512
513 if (ppt != POPPOS_NONE)
514 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200515 }
516
Bram Moolenaard61efa52022-07-23 09:52:04 +0100517 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200518 if (str != NULL)
519 {
520 wp->w_popup_prop_type = 0;
521 if (*str != NUL)
522 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100523 wp->w_popup_prop_win = curwin;
524 di = dict_find(d, (char_u *)"textpropwin", -1);
525 if (di != NULL)
526 {
527 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100528 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100529 wp->w_popup_prop_win = curwin;
530 }
531
532 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200533 if (nr <= 0)
534 nr = find_prop_type_id(str, NULL);
535 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000536 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200537 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200538 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200539 }
540 }
541
542 di = dict_find(d, (char_u *)"textpropid", -1);
543 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100544 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200545}
546
Bram Moolenaarb0992022020-01-30 14:55:42 +0100547/*
548 * Handle "moved" and "mousemoved" arguments.
549 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200550 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200551handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
552{
553 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
554 {
555 char_u *s = di->di_tv.vval.v_string;
556 int flags = 0;
557
558 if (STRCMP(s, "word") == 0)
559 flags = FIND_IDENT | FIND_STRING;
560 else if (STRCMP(s, "WORD") == 0)
561 flags = FIND_STRING;
562 else if (STRCMP(s, "expr") == 0)
563 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
564 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000565 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200566 if (flags != 0)
567 {
568 if (mousemoved)
569 set_mousemoved_columns(wp, flags);
570 else
571 set_moved_columns(wp, flags);
572 }
573 }
574 else if (di->di_tv.v_type == VAR_LIST
575 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200576 && (di->di_tv.vval.v_list->lv_len == 2
577 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200578 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200579 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100580 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200581 int mincol;
582 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200583
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200584 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100585 li = l->lv_first;
586 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200587 {
588 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
589
590 // Three numbers, might be from popup_getoptions().
591 if (mousemoved)
592 wp->w_popup_mouse_row = nr;
593 else
594 wp->w_popup_lnum = nr;
595 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100596 if (nr == 0)
597 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200598 }
599
600 mincol = tv_get_number(&li->li_tv);
601 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200602 if (mousemoved)
603 {
604 wp->w_popup_mouse_mincol = mincol;
605 wp->w_popup_mouse_maxcol = maxcol;
606 }
607 else
608 {
609 wp->w_popup_mincol = mincol;
610 wp->w_popup_maxcol = maxcol;
611 }
612 }
613 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000614 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200615}
616
617 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200618check_highlight(dict_T *dict, char *name, char_u **pval)
619{
620 dictitem_T *di;
621 char_u *str;
622
623 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000624 if (di == NULL)
625 return;
626
627 if (di->di_tv.v_type != VAR_STRING)
628 semsg(_(e_invalid_value_for_argument_str), name);
629 else
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200630 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000631 str = tv_get_string(&di->di_tv);
632 if (*str != NUL)
633 *pval = vim_strsave(str);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200634 }
635}
636
Bram Moolenaarae943152019-06-16 22:54:14 +0200637/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200638 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200639 */
640 static void
641popup_show_curline(win_T *wp)
642{
643 if (wp->w_cursor.lnum < wp->w_topline)
644 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200645 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200646 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200647 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200648 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200649 if (wp->w_topline < 1)
650 wp->w_topline = 1;
651 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
652 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200653 while (wp->w_topline < wp->w_cursor.lnum
654 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
655 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
656 > wp->w_height)
657 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200658 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200659
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200660 // Don't let "firstline" cause a scroll.
661 if (wp->w_firstline > 0)
662 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200663}
664
665/*
666 * Get the sign group name for window "wp".
667 * Returns a pointer to a static buffer, overwritten on the next call.
668 */
669 static char_u *
670popup_get_sign_name(win_T *wp)
671{
672 static char buf[30];
673
674 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
675 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200676}
677
678/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200679 * Highlight the line with the cursor.
680 * Also scrolls the text to put the cursor line in view.
681 */
682 static void
683popup_highlight_curline(win_T *wp)
684{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200685 int sign_id = 0;
686 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200687
Bram Moolenaar72570732019-11-30 14:21:53 +0100688 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689
690 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
691 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200692 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200694 if (!sign_exists_by_name(sign_name))
695 {
696 char *linehl = "PopupSelected";
697
698 if (syn_name2id((char_u *)linehl) == 0)
699 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100700 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
701 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200702 }
703
Bram Moolenaar72570732019-11-30 14:21:53 +0100704 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200705 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100706 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200707 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200708 else
709 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200710 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200711}
712
713/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200714 * Shared between popup_create() and f_popup_setoptions().
715 */
716 static void
717apply_general_options(win_T *wp, dict_T *dict)
718{
719 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200720 int nr;
721 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200722
Bram Moolenaarae943152019-06-16 22:54:14 +0200723 // TODO: flip
724
725 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200726 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200727 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100728 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200729 if (wp->w_firstline < 0)
730 wp->w_firstline = -1;
731 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200732
Bram Moolenaard61efa52022-07-23 09:52:04 +0100733 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200734 if (nr != -1)
735 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200736
Bram Moolenaard61efa52022-07-23 09:52:04 +0100737 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200738 if (str != NULL)
739 {
740 vim_free(wp->w_popup_title);
741 wp->w_popup_title = vim_strsave(str);
742 }
743
Bram Moolenaard61efa52022-07-23 09:52:04 +0100744 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200745 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200746 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200747
Bram Moolenaard61efa52022-07-23 09:52:04 +0100748 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200749 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200750 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200751 if (nr)
752 wp->w_popup_flags |= POPF_DRAG;
753 else
754 wp->w_popup_flags &= ~POPF_DRAG;
755 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000757 if (nr != -1)
758 {
759 if (nr)
760 wp->w_popup_flags |= POPF_DRAGALL;
761 else
762 wp->w_popup_flags &= ~POPF_DRAGALL;
763 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200764
Bram Moolenaard61efa52022-07-23 09:52:04 +0100765 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200766 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100767 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100768 if (nr)
769 wp->w_popup_flags |= POPF_POSINVERT;
770 else
771 wp->w_popup_flags &= ~POPF_POSINVERT;
772 }
773
Bram Moolenaard61efa52022-07-23 09:52:04 +0100774 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200775 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200776 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200777 if (nr)
778 wp->w_popup_flags |= POPF_RESIZE;
779 else
780 wp->w_popup_flags &= ~POPF_RESIZE;
781 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200782
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200783 di = dict_find(dict, (char_u *)"close", -1);
784 if (di != NULL)
785 {
786 int ok = TRUE;
787
788 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
789 {
790 char_u *s = di->di_tv.vval.v_string;
791
792 if (STRCMP(s, "none") == 0)
793 wp->w_popup_close = POPCLOSE_NONE;
794 else if (STRCMP(s, "button") == 0)
795 wp->w_popup_close = POPCLOSE_BUTTON;
796 else if (STRCMP(s, "click") == 0)
797 wp->w_popup_close = POPCLOSE_CLICK;
798 else
799 ok = FALSE;
800 }
801 else
802 ok = FALSE;
803 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000804 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200805 }
806
Bram Moolenaard61efa52022-07-23 09:52:04 +0100807 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200808 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000809 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200810 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
811 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000812#ifdef FEAT_TERMINAL
813 term_update_wincolor(wp);
814#endif
815 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816
Bram Moolenaarae943152019-06-16 22:54:14 +0200817 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
818 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200819
Bram Moolenaar790498b2019-06-01 22:15:29 +0200820 di = dict_find(dict, (char_u *)"borderhighlight", -1);
821 if (di != NULL)
822 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000824 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200825 else
826 {
827 list_T *list = di->di_tv.vval.v_list;
828 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200829 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200830
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200831 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200832 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
833 ++i, li = li->li_next)
834 {
835 str = tv_get_string(&li->li_tv);
836 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100837 {
838 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200839 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100840 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200841 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200842 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
843 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100844 {
845 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200846 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200847 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100848 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200849 }
850 }
851
Bram Moolenaar790498b2019-06-01 22:15:29 +0200852 di = dict_find(dict, (char_u *)"borderchars", -1);
853 if (di != NULL)
854 {
855 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000856 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200857 else
858 {
859 list_T *list = di->di_tv.vval.v_list;
860 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200861 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200862
863 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100864 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200865 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200866 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
867 ++i, li = li->li_next)
868 {
869 str = tv_get_string(&li->li_tv);
870 if (*str != NUL)
871 wp->w_border_char[i] = mb_ptr2char(str);
872 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200873 if (list->lv_len == 1)
874 for (i = 1; i < 8; ++i)
875 wp->w_border_char[i] = wp->w_border_char[0];
876 if (list->lv_len == 2)
877 {
878 for (i = 4; i < 8; ++i)
879 wp->w_border_char[i] = wp->w_border_char[1];
880 for (i = 1; i < 4; ++i)
881 wp->w_border_char[i] = wp->w_border_char[0];
882 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200883 }
884 }
885 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200886
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200887 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
888 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
889
Bram Moolenaarae943152019-06-16 22:54:14 +0200890 di = dict_find(dict, (char_u *)"zindex", -1);
891 if (di != NULL)
892 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100893 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200894 if (wp->w_zindex < 1)
895 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
896 if (wp->w_zindex > 32000)
897 wp->w_zindex = 32000;
898 }
899
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200900 di = dict_find(dict, (char_u *)"mask", -1);
901 if (di != NULL)
902 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200903 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200904
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200905 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200906 {
907 listitem_T *li;
908
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200909 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200910 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200911 {
912 if (li->li_tv.v_type != VAR_LIST
913 || li->li_tv.vval.v_list == NULL
914 || li->li_tv.vval.v_list->lv_len != 4)
915 {
916 ok = FALSE;
917 break;
918 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100919 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200920 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200921 }
922 }
923 if (ok)
924 {
925 wp->w_popup_mask = di->di_tv.vval.v_list;
926 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200927 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200928 }
929 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000930 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200931 }
932
Bram Moolenaarae943152019-06-16 22:54:14 +0200933#if defined(FEAT_TIMERS)
934 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100935 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200936 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100937 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200938#endif
939
Bram Moolenaar3397f742019-06-02 18:40:06 +0200940 di = dict_find(dict, (char_u *)"moved", -1);
941 if (di != NULL)
942 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200943 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200944 handle_moved_argument(wp, di, FALSE);
945 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200946
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200947 di = dict_find(dict, (char_u *)"mousemoved", -1);
948 if (di != NULL)
949 {
950 set_mousemoved_values(wp);
951 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200952 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200953
Bram Moolenaard61efa52022-07-23 09:52:04 +0100954 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100955 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200956 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100957 if (nr != 0)
958 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200959 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100960 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200961 }
962
Bram Moolenaarae943152019-06-16 22:54:14 +0200963 di = dict_find(dict, (char_u *)"filter", -1);
964 if (di != NULL)
965 {
966 callback_T callback = get_callback(&di->di_tv);
967
968 if (callback.cb_name != NULL)
969 {
970 free_callback(&wp->w_filter_cb);
971 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000972 if (callback.cb_free_name)
973 vim_free(callback.cb_name);
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 }
975 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100976 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200977 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200978 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200979 if (nr)
980 wp->w_popup_flags |= POPF_MAPPING;
981 else
982 wp->w_popup_flags &= ~POPF_MAPPING;
983 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200984
Bram Moolenaard61efa52022-07-23 09:52:04 +0100985 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200986 if (str != NULL)
987 {
988 if (STRCMP(str, "a") == 0)
989 wp->w_filter_mode = MODE_ALL;
990 else
991 wp->w_filter_mode = mode_str2flags(str);
992 }
993
Bram Moolenaarae943152019-06-16 22:54:14 +0200994 di = dict_find(dict, (char_u *)"callback", -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000995 if (di == NULL)
996 return;
Bram Moolenaarae943152019-06-16 22:54:14 +0200997
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000998 callback_T callback = get_callback(&di->di_tv);
999 if (callback.cb_name == NULL)
1000 return;
1001
1002 free_callback(&wp->w_close_cb);
1003 set_callback(&wp->w_close_cb, &callback);
1004 if (callback.cb_free_name)
1005 vim_free(callback.cb_name);
Bram Moolenaarae943152019-06-16 22:54:14 +02001006}
1007
1008/*
1009 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001010 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +02001011 */
1012 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001013apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001014{
1015 int nr;
1016
1017 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001018
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001019 if (create)
1020 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001021 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1022
Bram Moolenaarae943152019-06-16 22:54:14 +02001023 apply_general_options(wp, dict);
1024
Bram Moolenaard61efa52022-07-23 09:52:04 +01001025 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001026 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001027 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001028
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001029 // when "firstline" and "cursorline" are both set and the cursor would be
1030 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001031 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1032 {
1033 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1034 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001035 else if (wp->w_cursor.lnum < wp->w_firstline
1036 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001037 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001038 wp->w_topline = wp->w_firstline;
1039 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001040 }
1041
Bram Moolenaar33796b32019-06-08 16:01:13 +02001042 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001043 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001044}
1045
1046/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001047 * Add lines to the popup from a list of strings.
1048 */
1049 static void
1050add_popup_strings(buf_T *buf, list_T *l)
1051{
1052 listitem_T *li;
1053 linenr_T lnum = 0;
1054 char_u *p;
1055
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001056 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001057 if (li->li_tv.v_type == VAR_STRING)
1058 {
1059 p = li->li_tv.vval.v_string;
1060 ml_append_buf(buf, lnum++,
1061 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1062 }
1063}
1064
1065/*
1066 * Add lines to the popup from a list of dictionaries.
1067 */
1068 static void
1069add_popup_dicts(buf_T *buf, list_T *l)
1070{
1071 listitem_T *li;
1072 listitem_T *pli;
1073 linenr_T lnum = 0;
1074 char_u *p;
1075 dict_T *dict;
1076
1077 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001078 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001079 {
1080 if (li->li_tv.v_type != VAR_DICT)
1081 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001082 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001083 return;
1084 }
1085 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001086 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001087 ml_append_buf(buf, lnum++,
1088 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1089 }
1090
1091 // add the text properties
1092 lnum = 1;
1093 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1094 {
1095 dictitem_T *di;
1096 list_T *plist;
1097
1098 dict = li->li_tv.vval.v_dict;
1099 di = dict_find(dict, (char_u *)"props", -1);
1100 if (di != NULL)
1101 {
1102 if (di->di_tv.v_type != VAR_LIST)
1103 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001104 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001105 return;
1106 }
1107 plist = di->di_tv.vval.v_list;
1108 if (plist != NULL)
1109 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001110 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001111 {
1112 if (pli->li_tv.v_type != VAR_DICT)
1113 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001114 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001115 return;
1116 }
1117 dict = pli->li_tv.vval.v_dict;
1118 if (dict != NULL)
1119 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001120 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001121
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001122 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001123 }
1124 }
1125 }
1126 }
1127 }
1128}
1129
1130/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001131 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1132 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001133 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001134popup_top_extra(win_T *wp)
1135{
1136 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1137
1138 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1139 return 1;
1140 return extra;
1141}
1142
1143/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001144 * Get the padding plus border at the left.
1145 */
1146 int
1147popup_left_extra(win_T *wp)
1148{
1149 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1150}
1151
1152/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001153 * Return the height of popup window "wp", including border and padding.
1154 */
1155 int
1156popup_height(win_T *wp)
1157{
1158 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001159 + popup_top_extra(wp)
1160 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001161}
1162
1163/*
1164 * Return the width of popup window "wp", including border, padding and
1165 * scrollbar.
1166 */
1167 int
1168popup_width(win_T *wp)
1169{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001170 // w_leftcol is how many columns of the core are left of the screen
1171 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001172 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001173 + popup_extra_width(wp)
1174 + wp->w_popup_rightoff;
1175}
1176
1177/*
1178 * Return the extra width of popup window "wp": border, padding and scrollbar.
1179 */
1180 int
1181popup_extra_width(win_T *wp)
1182{
1183 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1184 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1185 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001186}
1187
1188/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001189 * Adjust the position and size of the popup to fit on the screen.
1190 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001191 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001192popup_adjust_position(win_T *wp)
1193{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001194 linenr_T lnum;
1195 int wrapped = 0;
1196 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001197 int maxwidth_no_scrollbar;
1198 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001199 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001200 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001201 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001202 int center_vert = FALSE;
1203 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001204 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001205 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001206 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1207 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1208 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1209 int extra_height = top_extra + bot_extra;
1210 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001211 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001212 int org_winrow = wp->w_winrow;
1213 int org_wincol = wp->w_wincol;
1214 int org_width = wp->w_width;
1215 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001216 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001217 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001218 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001219 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001220 int wantline = wp->w_wantline; // adjusted for textprop
1221 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001222 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001223 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001224
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001225 wp->w_winrow = 0;
1226 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001227 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001228 wp->w_popup_leftoff = 0;
1229 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001230
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001231 // May need to update the "cursorline" highlighting, which may also change
1232 // "topline"
1233 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1234 popup_highlight_curline(wp);
1235
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1237 {
1238 win_T *prop_win = wp->w_popup_prop_win;
1239 textprop_T prop;
1240 linenr_T prop_lnum;
1241 pos_T pos;
1242 int screen_row;
1243 int screen_scol;
1244 int screen_ccol;
1245 int screen_ecol;
1246
1247 // Popup window is positioned relative to a text property.
1248 if (find_visible_prop(prop_win,
1249 wp->w_popup_prop_type, wp->w_popup_prop_id,
1250 &prop, &prop_lnum) == FAIL)
1251 {
1252 // Text property is no longer visible, hide the popup.
1253 // Unhiding the popup is done in check_popup_unhidden().
1254 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1255 {
1256 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001257 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001258 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001259 }
1260 return;
1261 }
1262
1263 // Compute the desired position from the position of the text
1264 // property. Use "wantline" and "wantcol" as offsets.
1265 pos.lnum = prop_lnum;
1266 pos.col = prop.tp_col;
1267 if (wp->w_popup_pos == POPPOS_TOPLEFT
1268 || wp->w_popup_pos == POPPOS_BOTLEFT)
1269 pos.col += prop.tp_len - 1;
1270 textpos2screenpos(prop_win, &pos, &screen_row,
1271 &screen_scol, &screen_ccol, &screen_ecol);
1272
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001273 if (screen_scol == 0)
1274 {
1275 // position is off screen, make the width zero to hide it.
1276 wp->w_width = 0;
1277 return;
1278 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001279 if (wp->w_popup_pos == POPPOS_TOPLEFT
1280 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1281 // below the text
1282 wantline = screen_row + wantline + 1;
1283 else
1284 // above the text
1285 wantline = screen_row + wantline - 1;
1286 center_vert = FALSE;
1287 if (wp->w_popup_pos == POPPOS_TOPLEFT
1288 || wp->w_popup_pos == POPPOS_BOTLEFT)
1289 // right of the text
1290 wantcol = screen_ecol + wantcol;
1291 else
1292 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001293 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001294 use_wantcol = TRUE;
1295 }
1296 else
1297 {
1298 // If no line was specified default to vertical centering.
1299 if (wantline == 0)
1300 center_vert = TRUE;
1301 else if (wantline < 0)
1302 // If "wantline" is negative it actually means zero.
1303 wantline = 0;
1304 if (wantcol < 0)
1305 // If "wantcol" is negative it actually means zero.
1306 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001307 }
1308
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001309 if (wp->w_popup_pos == POPPOS_CENTER)
1310 {
1311 // center after computing the size
1312 center_vert = TRUE;
1313 center_hor = TRUE;
1314 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001315 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001316 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001317 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1318 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001319 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001320 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001321 if (wp->w_winrow >= Rows)
1322 wp->w_winrow = Rows - 1;
1323 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001324 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001325 {
1326 // Assume that each buffer line takes one screen line, and one line
1327 // for the top border. First make sure cmdline_row is valid,
1328 // calling update_screen() will set it only later.
1329 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001330 wp->w_winrow = MAX(cmdline_row
1331 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001332 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001333
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001334 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001335 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001336 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1337 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001338 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001339 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001340 // Need to see at least one character after the decoration.
1341 if (wp->w_wincol > Columns - left_extra - 1)
1342 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001343 }
1344 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001345
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001346 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001347 // When left aligned use the space available, but shift to the left when we
1348 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001349 maxspace = Columns - wp->w_wincol - left_extra;
1350 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001351 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001352 {
1353 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001354 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001355 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001356
1357 if (wp->w_p_nu || wp->w_p_rnu)
1358 margin_width = number_width(wp) + 1;
1359#ifdef FEAT_FOLDING
1360 margin_width += wp->w_p_fdc;
1361#endif
1362#ifdef FEAT_SIGNS
1363 if (signcolumn_on(wp))
1364 margin_width += 2;
1365#endif
1366 if (margin_width >= maxwidth)
1367 margin_width = maxwidth - 1;
1368
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001369 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001370 minheight = wp->w_minheight;
1371#ifdef FEAT_TERMINAL
1372 // A terminal popup initially does not have content, use a default minimal
1373 // width of 20 characters and height of 5 lines.
1374 if (wp->w_buffer->b_term != NULL)
1375 {
1376 if (minwidth == 0)
1377 minwidth = 20;
1378 if (minheight == 0)
1379 minheight = 5;
1380 }
1381#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001382
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001383 if (wp->w_maxheight > 0)
1384 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001385 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1386 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001387
Bram Moolenaar8d241042019-06-12 23:40:01 +02001388 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001389 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001390 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001391 if (wp->w_topline < 1)
1392 wp->w_topline = 1;
1393 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001394 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1395
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001396 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001397 // Use a minimum width of one, so that something shows when there is no
1398 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001399 // When "firstline" is -1 then start with the last buffer line and go
1400 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001401 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001402 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001403 if (wp->w_firstline < 0)
1404 lnum = wp->w_buffer->b_ml.ml_line_count;
1405 else
1406 lnum = wp->w_topline;
1407 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001408 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001409 int len;
1410 int w_width = wp->w_width;
1411
1412 // Count Tabs for what they are worth and compute the length based on
1413 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001414 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001415 if (wp->w_width < maxwidth)
1416 wp->w_width = maxwidth;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001417 len = linetabsize(wp, lnum);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001418 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001419
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001420 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001421 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001422 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001423 {
1424 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001425 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001426 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001427 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001428 }
1429 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001430 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001431 && allow_adjust_left
1432 && (wp->w_popup_pos == POPPOS_TOPLEFT
1433 || wp->w_popup_pos == POPPOS_BOTLEFT))
1434 {
1435 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001436 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001437
Bram Moolenaar51c31312019-06-15 22:27:23 +02001438 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001439 {
1440 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001441
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001442 len -= truncate_shift;
1443 shift_by -= truncate_shift;
1444 }
1445
1446 wp->w_wincol -= shift_by;
1447 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001448 wp->w_width = maxwidth;
1449 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001450 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001451 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001452 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001453 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1454 wp->w_width = wp->w_maxwidth;
1455 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001456
1457 if (wp->w_firstline < 0)
1458 --lnum;
1459 else
1460 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001461
1462 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001463 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001464 && (wp->w_firstline >= 0
1465 ? lnum - wp->w_topline
1466 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001467 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001468 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001469 }
1470
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001471 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001472 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001473
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001474 wp->w_has_scrollbar = wp->w_want_scrollbar
1475 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001476#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001477 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1478 // Terminal window with running job never has a scrollbar, adjusts to
1479 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001480 wp->w_has_scrollbar = FALSE;
1481#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001482 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001483 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001484 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001485 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001486 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001487 // make space for the scrollbar if needed, when lines wrap and when
1488 // applying minwidth
1489 if (maxwidth + right_extra >= maxspace
1490 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1491 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001492 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001493
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001494 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1495 {
1496 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1497
1498 if (minwidth < title_len)
1499 minwidth = title_len;
1500 }
1501
1502 if (minwidth > 0 && wp->w_width < minwidth)
1503 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001504 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001505 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001506 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001507 // some columns cut off on the right
1508 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001509
1510 // If the window doesn't fit because 'minwidth' is set then the
1511 // scrollbar is at the far right of the screen, use the size without
1512 // the scrollbar.
1513 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1514 {
1515 int off = wp->w_width - maxwidth;
1516
1517 if (off > right_extra)
1518 extra_width -= right_extra;
1519 else
1520 extra_width -= off;
1521 wp->w_width = maxwidth_no_scrollbar;
1522 }
1523 else
1524 {
1525 wp->w_width = maxwidth;
1526
1527 // when adding a scrollbar below need to adjust the width
1528 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1529 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001530 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001532 {
1533 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1534 if (wp->w_wincol < 0)
1535 wp->w_wincol = 0;
1536 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001537 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1538 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1539 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001540 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001541
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001542 // Right aligned: move to the right if needed.
1543 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001544 if (leftoff >= 0)
1545 wp->w_wincol = leftoff;
1546 else if (wp->w_popup_fixed)
1547 {
1548 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001549 // columns when displaying the window, minus left border and
1550 // padding.
1551 if (-leftoff > left_extra)
1552 wp->w_leftcol = -leftoff - left_extra;
1553 wp->w_width -= wp->w_leftcol;
1554 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001555 if (wp->w_width < 0)
1556 wp->w_width = 0;
1557 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001558 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001559
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001560 if (wp->w_p_wrap || (!wp->w_popup_fixed
1561 && (wp->w_popup_pos == POPPOS_TOPLEFT
1562 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001563 {
1564 int want_col = 0;
1565
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001566 // try to show the right border and any scrollbar
1567 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001568 if (want_col > 0 && wp->w_wincol > 0
1569 && wp->w_wincol + want_col >= Columns)
1570 {
1571 wp->w_wincol = Columns - want_col;
1572 if (wp->w_wincol < 0)
1573 wp->w_wincol = 0;
1574 }
1575 }
1576
Bram Moolenaar8d241042019-06-12 23:40:01 +02001577 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1578 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001579 if (minheight > 0 && wp->w_height < minheight)
1580 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001581 if (maxheight > 0 && wp->w_height > maxheight)
1582 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001583 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001584 if (wp->w_height > Rows - wp->w_winrow)
1585 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001586
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001587 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001588 {
1589 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1590 if (wp->w_winrow < 0)
1591 wp->w_winrow = 0;
1592 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001593 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001594 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001595 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001596 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001597 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001598 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001599 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1600 {
1601 // Bottom aligned but does not fit, and less space on the other
1602 // side or "posinvert" is off: reduce height.
1603 wp->w_winrow = 0;
1604 wp->w_height = wantline - extra_height;
1605 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001606 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001607 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001608 // Not enough space and more space on the other side: make top
1609 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001610 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001611 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001612 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001613 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001614 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1615 || wp->w_popup_pos == POPPOS_TOPLEFT)
1616 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001617 if (wp != popup_dragwin
1618 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001619 && wantline * 2 > Rows
1620 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001621 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001622 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001623 // make bottom aligned and recompute the height
1624 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001625 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001626 if (wp->w_winrow < 0)
1627 {
1628 wp->w_height += wp->w_winrow;
1629 wp->w_winrow = 0;
1630 }
1631 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001632 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001633 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001634 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001635 adjust_height_for_top_aligned = TRUE;
1636 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001637 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001638
1639 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1640 && wp->w_winrow + wp->w_height + extra_height > Rows)
1641 {
1642 // Bottom of the popup goes below the last line, reduce the height and
1643 // add a scrollbar.
1644 wp->w_height = Rows - wp->w_winrow - extra_height;
1645#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001646 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001647#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001648 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001649 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001650 if (width_with_scrollbar > 0)
1651 wp->w_width = width_with_scrollbar;
1652 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001653 }
1654
1655 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001656 if (wp->w_winrow >= Rows)
1657 wp->w_winrow = Rows - 1;
1658 else if (wp->w_winrow < 0)
1659 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001660
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001661 if (wp->w_height != org_height)
1662 win_comp_scroll(wp);
1663
Bram Moolenaar17146962019-05-30 00:12:11 +02001664 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001665 if (win_valid(wp->w_popup_prop_win))
1666 {
1667 wp->w_popup_prop_changedtick =
1668 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1669 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1670 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001671
1672 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001673 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001674 if (org_winrow != wp->w_winrow
1675 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001676 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001677 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001678 || org_width != wp->w_width
1679 || org_height != wp->w_height)
1680 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001681 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001682 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1683 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001684 popup_mask_refresh = TRUE;
1685 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001686}
1687
Bram Moolenaar17627312019-06-02 19:53:44 +02001688typedef enum
1689{
1690 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001691 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001692 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001693 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001694 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001695 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001696 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001697 TYPE_PREVIEW, // preview window
1698 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001699} create_type_T;
1700
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001701/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001702 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1703 */
1704 static int
1705popup_is_notification(create_type_T type)
1706{
1707 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1708}
1709
1710/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001711 * Make "buf" empty and set the contents to "text".
1712 * Used by popup_create() and popup_settext().
1713 */
1714 static void
1715popup_set_buffer_text(buf_T *buf, typval_T text)
1716{
1717 int lnum;
1718
1719 // Clear the buffer, then replace the lines.
1720 curbuf = buf;
1721 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001722 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001723 curbuf = curwin->w_buffer;
1724
1725 // Add text to the buffer.
1726 if (text.v_type == VAR_STRING)
1727 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001728 char_u *s = text.vval.v_string;
1729
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001730 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001731 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001732 }
1733 else
1734 {
1735 list_T *l = text.vval.v_list;
1736
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001737 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001738 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001739 if (l->lv_first == &range_list_item)
1740 emsg(_(e_using_number_as_string));
1741 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001742 // list of strings
1743 add_popup_strings(buf, l);
1744 else
1745 // list of dictionaries
1746 add_popup_dicts(buf, l);
1747 }
1748 }
1749
1750 // delete the line that was in the empty buffer
1751 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001752 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001753 curbuf = curwin->w_buffer;
1754}
1755
1756/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001757 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1758 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001759 * Return FAIL if the parsing fails.
1760 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001761 static int
1762parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001763{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001764 char_u *p =
1765#ifdef FEAT_QUICKFIX
1766 !is_preview ? p_cpp :
1767#endif
1768 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001769
Bram Moolenaar258cef52019-08-21 17:29:29 +02001770 if (wp != NULL)
1771 wp->w_popup_flags &= ~POPF_INFO_MENU;
1772
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001773 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001774 {
1775 char_u *e, *dig;
1776 char_u *s = p;
1777 int x;
1778
1779 e = vim_strchr(p, ':');
1780 if (e == NULL || e[1] == NUL)
1781 return FAIL;
1782
1783 p = vim_strchr(e, ',');
1784 if (p == NULL)
1785 p = e + STRLEN(e);
1786 dig = e + 1;
1787 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001788
1789 if (STRNCMP(s, "height:", 7) == 0)
1790 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001791 if (dig != p)
1792 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001793 if (wp != NULL)
1794 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001795 if (is_preview)
1796 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001797 wp->w_maxheight = x;
1798 }
1799 }
1800 else if (STRNCMP(s, "width:", 6) == 0)
1801 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001802 if (dig != p)
1803 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001804 if (wp != NULL)
1805 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001806 if (is_preview)
1807 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001808 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001809 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001810 }
1811 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001812 else if (STRNCMP(s, "highlight:", 10) == 0)
1813 {
1814 if (wp != NULL)
1815 {
1816 int c = *p;
1817
1818 *p = NUL;
1819 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1820 s + 10, OPT_FREE|OPT_LOCAL, 0);
1821 *p = c;
1822 }
1823 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001824 else if (STRNCMP(s, "border:", 7) == 0)
1825 {
1826 char_u *arg = s + 7;
1827 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1828 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1829 int i;
1830
1831 if (!on && !off)
1832 return FAIL;
1833 if (wp != NULL)
1834 {
1835 for (i = 0; i < 4; ++i)
1836 wp->w_popup_border[i] = on ? 1 : 0;
1837 if (off)
1838 // only show the X for close when there is a border
1839 wp->w_popup_close = POPCLOSE_NONE;
1840 }
1841 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001842 else if (STRNCMP(s, "align:", 6) == 0)
1843 {
1844 char_u *arg = s + 6;
1845 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1846 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1847
1848 if (!menu && !item)
1849 return FAIL;
1850 if (wp != NULL && menu)
1851 wp->w_popup_flags |= POPF_INFO_MENU;
1852 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001853 else
1854 return FAIL;
1855 }
1856 return OK;
1857}
1858
1859/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001860 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1861 * is not NULL.
1862 * Return FAIL if the parsing fails.
1863 */
1864 int
1865parse_previewpopup(win_T *wp)
1866{
1867 return parse_popup_option(wp, TRUE);
1868}
1869
1870/*
1871 * Parse the 'completepopup' option and apply the values to window "wp" if it
1872 * is not NULL.
1873 * Return FAIL if the parsing fails.
1874 */
1875 int
1876parse_completepopup(win_T *wp)
1877{
1878 return parse_popup_option(wp, FALSE);
1879}
1880
1881/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001883 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 */
1885 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001886popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001887{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001888 poppos_T ppt = POPPOS_NONE;
1889
1890 if (d != NULL)
1891 ppt = get_pos_entry(d, FALSE);
1892
Bram Moolenaar79648732019-07-18 21:43:07 +02001893 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001894 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001895 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001896 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1897 }
1898 else
1899 {
1900 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1901 if (wp->w_wantline == 0) // cursor in first line
1902 {
1903 wp->w_wantline = 2;
1904 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1905 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1906 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001907 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001908
Bram Moolenaar79648732019-07-18 21:43:07 +02001909 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001910 if (wp->w_wantcol > Columns - width)
1911 {
1912 wp->w_wantcol = Columns - width;
1913 if (wp->w_wantcol < 1)
1914 wp->w_wantcol = 1;
1915 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001916
Bram Moolenaar79648732019-07-18 21:43:07 +02001917 popup_adjust_position(wp);
1918}
1919
1920/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001921 * Set w_wantline and w_wantcol for the a given screen position.
1922 * Caller must take care of running into the window border.
1923 */
1924 void
1925popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1926{
1927 wp->w_wantline = row;
1928 wp->w_wantcol = col;
1929 popup_adjust_position(wp);
1930}
1931
1932/*
1933 * Add a border and lef&right padding.
1934 */
1935 static void
1936add_border_left_right_padding(win_T *wp)
1937{
1938 int i;
1939
1940 for (i = 0; i < 4; ++i)
1941 {
1942 wp->w_popup_border[i] = 1;
1943 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1944 }
1945}
1946
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001947#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001948/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001949 * Return TRUE if there is any popup window with a terminal buffer.
1950 */
1951 static int
1952popup_terminal_exists(void)
1953{
1954 win_T *wp;
1955 tabpage_T *tp;
1956
1957 FOR_ALL_POPUPWINS(wp)
1958 if (wp->w_buffer->b_term != NULL)
1959 return TRUE;
1960 FOR_ALL_TABPAGES(tp)
1961 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1962 if (wp->w_buffer->b_term != NULL)
1963 return TRUE;
1964 return FALSE;
1965}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001966#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001967
1968/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001969 * Mark all popup windows in the current tab and global for redrawing.
1970 */
1971 void
1972popup_redraw_all(void)
1973{
1974 win_T *wp;
1975
1976 FOR_ALL_POPUPWINS(wp)
1977 wp->w_redr_type = UPD_NOT_VALID;
1978 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1979 wp->w_redr_type = UPD_NOT_VALID;
1980}
1981
1982/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001983 * Set the color for a notification window.
1984 */
1985 static void
1986popup_update_color(win_T *wp, create_type_T type)
1987{
1988 char *hiname = type == TYPE_MESSAGE_WIN
1989 ? "MessageWindow" : "PopupNotification";
1990 int nr = syn_name2id((char_u *)hiname);
1991
1992 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1993 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1994 OPT_FREE|OPT_LOCAL, 0);
1995}
1996
1997/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001998 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001999 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02002000 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002001 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002002 */
Bram Moolenaara730e552019-06-16 19:05:31 +02002003 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02002004popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002005{
Bram Moolenaara3fce622019-06-20 02:31:49 +02002006 win_T *wp;
2007 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002008 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002009 int new_buffer;
2010 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002011 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002012 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002013
Bram Moolenaar79648732019-07-18 21:43:07 +02002014 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002015 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002016 if (in_vim9script()
2017 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2018 || check_for_dict_arg(argvars, 1) == FAIL))
2019 return NULL;
2020
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002021 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002022 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002023 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002024 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002025 if (buf == NULL)
2026 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002027 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002028 return NULL;
2029 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002030#ifdef FEAT_TERMINAL
2031 if (buf->b_term != NULL && popup_terminal_exists())
2032 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002033 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002034 return NULL;
2035 }
2036#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002037 }
2038 else if (!(argvars[0].v_type == VAR_STRING
2039 && argvars[0].vval.v_string != NULL)
2040 && !(argvars[0].v_type == VAR_LIST
2041 && argvars[0].vval.v_list != NULL))
2042 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002043 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002044 return NULL;
2045 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002046 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002047 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002048 d = argvars[1].vval.v_dict;
2049 }
2050
2051 if (d != NULL)
2052 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002053 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002054 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002055 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002056 tabnr = -1; // notifications are global by default
2057 else
2058 tabnr = 0;
2059 if (tabnr > 0)
2060 {
2061 tp = find_tabpage(tabnr);
2062 if (tp == NULL)
2063 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002064 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002065 return NULL;
2066 }
2067 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002068 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002069 else if (popup_is_notification(type))
2070 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002071
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002072 // Create the window and buffer.
2073 wp = win_alloc_popup_win();
2074 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002075 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002076 if (rettv != NULL)
2077 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002078 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002079 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002080
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002081 if (buf != NULL)
2082 {
2083 // use existing buffer
2084 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002085 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002086 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002087 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002088 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002089 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002090 }
2091 else
2092 {
2093 // create a new buffer associated with the popup
2094 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002095 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002096 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002097 {
2098 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002099 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002100 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002101 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002102
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002103 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002104
Bram Moolenaar46451042019-08-24 15:50:46 +02002105 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002106 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002107 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002108 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002109 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002110 buf->b_p_ul = -1; // no undo
2111 buf->b_p_swf = FALSE; // no swap file
2112 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002113 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002114
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002115 // Avoid that 'buftype' is reset when this buffer is entered.
2116 buf->b_p_initialized = TRUE;
2117 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002118 wp->w_p_wrap = TRUE; // 'wrap' is default on
2119 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002120
Bram Moolenaara3fce622019-06-20 02:31:49 +02002121 if (tp != NULL)
2122 {
2123 // popup on specified tab page
2124 wp->w_next = tp->tp_first_popupwin;
2125 tp->tp_first_popupwin = wp;
2126 }
2127 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002128 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002129 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002130 wp->w_next = curtab->tp_first_popupwin;
2131 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002132 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002133 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002134 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002135 win_T *prev = first_popupwin;
2136
2137 // Global popup: add at the end, so that it gets displayed on top of
2138 // older ones with the same zindex. Matters for notifications.
2139 if (first_popupwin == NULL)
2140 first_popupwin = wp;
2141 else
2142 {
2143 while (prev->w_next != NULL)
2144 prev = prev->w_next;
2145 prev->w_next = wp;
2146 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002147 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002148
Bram Moolenaar79648732019-07-18 21:43:07 +02002149 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002150 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002151
Bram Moolenaar79648732019-07-18 21:43:07 +02002152 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002153 {
2154 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002155 }
2156 if (type == TYPE_ATCURSOR)
2157 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002158 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002159 set_moved_values(wp);
2160 set_moved_columns(wp, FIND_STRING);
2161 }
2162
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002163 if (type == TYPE_BEVAL)
2164 {
2165 wp->w_popup_pos = POPPOS_BOTLEFT;
2166
2167 // by default use the mouse position
2168 wp->w_wantline = mouse_row;
2169 if (wp->w_wantline <= 0) // mouse on first line
2170 {
2171 wp->w_wantline = 2;
2172 wp->w_popup_pos = POPPOS_TOPLEFT;
2173 }
2174 wp->w_wantcol = mouse_col + 1;
2175 set_mousemoved_values(wp);
2176 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2177 }
2178
Bram Moolenaar33796b32019-06-08 16:01:13 +02002179 // set default values
2180 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002181 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002182
Bram Moolenaar9198de32022-08-27 21:30:03 +01002183 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002184 {
2185 win_T *twp, *nextwin;
2186 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002187
2188 // Try to not overlap with another global popup. Guess we need 3
2189 // more screen lines than buffer lines.
2190 wp->w_wantline = 1;
2191 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2192 {
2193 nextwin = twp->w_next;
2194 if (twp != wp
2195 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2196 && twp->w_winrow <= wp->w_wantline - 1 + height
2197 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2198 {
2199 // move to below this popup and restart the loop to check for
2200 // overlap with other popups
2201 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2202 nextwin = first_popupwin;
2203 }
2204 }
2205 if (wp->w_wantline + height > Rows)
2206 {
2207 // can't avoid overlap, put on top in the hope that message goes
2208 // away soon.
2209 wp->w_wantline = 1;
2210 }
2211
2212 wp->w_wantcol = 10;
2213 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002214 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002215 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002216 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002217 for (i = 0; i < 4; ++i)
2218 wp->w_popup_border[i] = 1;
2219 wp->w_popup_padding[1] = 1;
2220 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002221
Bram Moolenaar9198de32022-08-27 21:30:03 +01002222 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002223 }
2224
Bram Moolenaara730e552019-06-16 19:05:31 +02002225 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002226 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002227 wp->w_popup_pos = POPPOS_CENTER;
2228 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002229 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002230 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002231 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002232 }
2233
Bram Moolenaara730e552019-06-16 19:05:31 +02002234 if (type == TYPE_MENU)
2235 {
2236 typval_T tv;
2237 callback_T callback;
2238
2239 tv.v_type = VAR_STRING;
2240 tv.vval.v_string = (char_u *)"popup_filter_menu";
2241 callback = get_callback(&tv);
2242 if (callback.cb_name != NULL)
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002243 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002244 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002245 if (callback.cb_free_name)
2246 vim_free(callback.cb_name);
2247 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002248
2249 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002250 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002251 }
2252
Bram Moolenaar79648732019-07-18 21:43:07 +02002253 if (type == TYPE_PREVIEW)
2254 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002255 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002256 wp->w_popup_close = POPCLOSE_BUTTON;
2257 for (i = 0; i < 4; ++i)
2258 wp->w_popup_border[i] = 1;
2259 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002260 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002261 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002262# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002263 if (type == TYPE_INFO)
2264 {
2265 wp->w_popup_pos = POPPOS_TOPLEFT;
2266 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2267 wp->w_popup_close = POPCLOSE_BUTTON;
2268 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002269 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002270 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002271# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002272
Bram Moolenaarae943152019-06-16 22:54:14 +02002273 for (i = 0; i < 4; ++i)
2274 VIM_CLEAR(wp->w_border_highlight[i]);
2275 for (i = 0; i < 8; ++i)
2276 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002277 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002278 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002279 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002280
Bram Moolenaar79648732019-07-18 21:43:07 +02002281 if (d != NULL)
2282 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002283 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002284
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002285#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002286 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2287 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002288#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002289
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002290 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002291
2292 wp->w_vsep_width = 0;
2293
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002294 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002295 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002296
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002297#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002298 // When running a terminal in the popup it becomes the current window.
2299 if (buf->b_term != NULL)
2300 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002301#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002302
Bram Moolenaara730e552019-06-16 19:05:31 +02002303 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002304}
2305
2306/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002307 * popup_clear()
2308 */
2309 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002310f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002311{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002312 int force = FALSE;
2313
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002314 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2315 return;
2316
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002317 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002318 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002319 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002320}
2321
2322/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002323 * popup_create({text}, {options})
2324 */
2325 void
2326f_popup_create(typval_T *argvars, typval_T *rettv)
2327{
Bram Moolenaar17627312019-06-02 19:53:44 +02002328 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002329}
2330
2331/*
2332 * popup_atcursor({text}, {options})
2333 */
2334 void
2335f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2336{
Bram Moolenaar17627312019-06-02 19:53:44 +02002337 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002338}
2339
2340/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002341 * popup_beval({text}, {options})
2342 */
2343 void
2344f_popup_beval(typval_T *argvars, typval_T *rettv)
2345{
2346 popup_create(argvars, rettv, TYPE_BEVAL);
2347}
2348
2349/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002350 * Invoke the close callback for window "wp" with value "result".
2351 * Careful: The callback may make "wp" invalid!
2352 */
2353 static void
2354invoke_popup_callback(win_T *wp, typval_T *result)
2355{
2356 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002357 typval_T argv[3];
2358
2359 argv[0].v_type = VAR_NUMBER;
2360 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2361
2362 if (result != NULL && result->v_type != VAR_UNKNOWN)
2363 copy_tv(result, &argv[1]);
2364 else
2365 {
2366 argv[1].v_type = VAR_NUMBER;
2367 argv[1].vval.v_number = 0;
2368 }
2369
2370 argv[2].v_type = VAR_UNKNOWN;
2371
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002372 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002373 if (result != NULL)
2374 clear_tv(&argv[1]);
2375 clear_tv(&rettv);
2376}
2377
2378/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002379 * Make "prevwin" the current window, unless it's equal to "wp".
2380 * Otherwise make "firstwin" the current window.
2381 */
2382 static void
2383back_to_prevwin(win_T *wp)
2384{
2385 if (win_valid(prevwin) && wp != prevwin)
2386 win_enter(prevwin, FALSE);
2387 else
2388 win_enter(firstwin, FALSE);
2389}
2390
2391/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002392 * Close popup "wp" and invoke any close callback for it.
2393 */
2394 static void
2395popup_close_and_callback(win_T *wp, typval_T *arg)
2396{
2397 int id = wp->w_id;
2398
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002399#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002400 if (wp == curwin && curbuf->b_term != NULL)
2401 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002402 win_T *owp;
2403
2404 // Closing popup window with a terminal: put focus back on the first
2405 // that works:
2406 // - another popup window with a terminal
2407 // - the previous window
2408 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002409 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002410 if (owp != curwin && owp->w_buffer->b_term != NULL)
2411 break;
2412 if (owp != NULL)
2413 win_enter(owp, FALSE);
2414 else
2415 {
2416 for (owp = curtab->tp_first_popupwin; owp != NULL;
2417 owp = owp->w_next)
2418 if (owp != curwin && owp->w_buffer->b_term != NULL)
2419 break;
2420 if (owp != NULL)
2421 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002422 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002423 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002424 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002425 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002426#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002427
Bram Moolenaar49540192019-12-11 19:34:54 +01002428 // Just in case a check higher up is missing.
2429 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002430 {
2431 // To avoid getting stuck when win_execute() does something that causes
2432 // an error, stop calling the filter callback.
2433 free_callback(&wp->w_filter_cb);
2434
Bram Moolenaar49540192019-12-11 19:34:54 +01002435 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002436 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002437
Bram Moolenaarcee52202020-03-11 14:19:58 +01002438 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002439 if (wp->w_close_cb.cb_name != NULL)
2440 // Careful: This may make "wp" invalid.
2441 invoke_popup_callback(wp, arg);
2442
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002443 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002444 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002445}
2446
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002447 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002448popup_close_with_retval(win_T *wp, int retval)
2449{
2450 typval_T res;
2451
2452 res.v_type = VAR_NUMBER;
2453 res.vval.v_number = retval;
2454 popup_close_and_callback(wp, &res);
2455}
2456
Bram Moolenaar3397f742019-06-02 18:40:06 +02002457/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002458 * Close popup "wp" because of a mouse click.
2459 */
2460 void
2461popup_close_for_mouse_click(win_T *wp)
2462{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002463 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002464}
2465
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002466 static void
2467check_mouse_moved(win_T *wp, win_T *mouse_wp)
2468{
2469 // Close the popup when all if these are true:
2470 // - the mouse is not on this popup
2471 // - "mousemoved" was used
2472 // - the mouse is no longer on the same screen row or the mouse column is
2473 // outside of the relevant text
2474 if (wp != mouse_wp
2475 && wp->w_popup_mouse_row != 0
2476 && (wp->w_popup_mouse_row != mouse_row
2477 || mouse_col < wp->w_popup_mouse_mincol
2478 || mouse_col > wp->w_popup_mouse_maxcol))
2479 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002480 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002481 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002482 }
2483}
2484
2485/*
2486 * Called when the mouse moved: may close a popup with "mousemoved".
2487 */
2488 void
2489popup_handle_mouse_moved(void)
2490{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002491 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002492 win_T *mouse_wp;
2493 int row = mouse_row;
2494 int col = mouse_col;
2495
2496 // find the window where the mouse is in
2497 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2498
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002499 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2500 {
2501 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002502 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002503 }
2504 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2505 {
2506 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002507 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002508 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002509}
2510
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002511/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002512 * In a filter: check if the typed key is a mouse event that is used for
2513 * dragging the popup.
2514 */
2515 static void
2516filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2517{
2518 int row = mouse_row;
2519 int col = mouse_col;
2520
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002521 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002522 && is_mouse_key(c)
2523 && (wp == popup_dragwin
2524 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2525 // do not consume the key, allow for dragging the popup
2526 rettv->vval.v_number = 0;
2527}
2528
Bram Moolenaara730e552019-06-16 19:05:31 +02002529/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002530 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002531 */
2532 void
2533f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2534{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002535 int id;
2536 win_T *wp;
2537 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002538 typval_T res;
2539 int c;
2540 linenr_T old_lnum;
2541
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002542 if (in_vim9script()
2543 && (check_for_number_arg(argvars, 0) == FAIL
2544 || check_for_string_arg(argvars, 1) == FAIL))
2545 return;
2546
2547 id = tv_get_number(&argvars[0]);
2548 wp = win_id2wp(id);
2549 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002550 // If the popup has been closed do not consume the key.
2551 if (wp == NULL)
2552 return;
2553
2554 c = *key;
2555 if (c == K_SPECIAL && key[1] != NUL)
2556 c = TO_SPECIAL(key[1], key[2]);
2557
2558 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002559 rettv->v_type = VAR_BOOL;
2560 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002561 res.v_type = VAR_NUMBER;
2562
2563 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002564 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2565 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002566 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002567 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002568 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2569 ++wp->w_cursor.lnum;
2570 if (old_lnum != wp->w_cursor.lnum)
2571 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002572 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002573 return;
2574 }
2575
2576 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2577 {
2578 // Cancelled, invoke callback with -1
2579 res.vval.v_number = -1;
2580 popup_close_and_callback(wp, &res);
2581 return;
2582 }
2583 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2584 {
2585 // Invoke callback with current index.
2586 res.vval.v_number = wp->w_cursor.lnum;
2587 popup_close_and_callback(wp, &res);
2588 return;
2589 }
2590
2591 filter_handle_drag(wp, c, rettv);
2592}
2593
2594/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002595 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002596 */
2597 void
2598f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2599{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002600 int id;
2601 win_T *wp;
2602 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002603 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002604 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002605
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002606 if (in_vim9script()
2607 && (check_for_number_arg(argvars, 0) == FAIL
2608 || check_for_string_arg(argvars, 1) == FAIL))
2609 return;
2610
2611 id = tv_get_number(&argvars[0]);
2612 wp = win_id2wp(id);
2613 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002614 // If the popup has been closed don't consume the key.
2615 if (wp == NULL)
2616 return;
2617
Bram Moolenaara730e552019-06-16 19:05:31 +02002618 c = *key;
2619 if (c == K_SPECIAL && key[1] != NUL)
2620 c = TO_SPECIAL(key[1], key[2]);
2621
Bram Moolenaara42d9452019-06-15 21:46:30 +02002622 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002623 rettv->v_type = VAR_BOOL;
2624 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002625
Bram Moolenaara730e552019-06-16 19:05:31 +02002626 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002627 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002628 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002629 res.vval.v_number = 0;
2630 else
2631 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002632 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002633 return;
2634 }
2635
2636 // Invoke callback
2637 res.v_type = VAR_NUMBER;
2638 popup_close_and_callback(wp, &res);
2639}
2640
2641/*
2642 * popup_dialog({text}, {options})
2643 */
2644 void
2645f_popup_dialog(typval_T *argvars, typval_T *rettv)
2646{
2647 popup_create(argvars, rettv, TYPE_DIALOG);
2648}
2649
2650/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002651 * popup_menu({text}, {options})
2652 */
2653 void
2654f_popup_menu(typval_T *argvars, typval_T *rettv)
2655{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002656 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002657}
2658
2659/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002660 * popup_notification({text}, {options})
2661 */
2662 void
2663f_popup_notification(typval_T *argvars, typval_T *rettv)
2664{
2665 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2666}
2667
2668/*
2669 * Find the popup window with window-ID "id".
2670 * If the popup window does not exist NULL is returned.
2671 * If the window is not a popup window, and error message is given.
2672 */
2673 static win_T *
2674find_popup_win(int id)
2675{
2676 win_T *wp = win_id2wp(id);
2677
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002678 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002679 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002680 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002681 return NULL;
2682 }
2683 return wp;
2684}
2685
2686/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002687 * popup_close({id})
2688 */
2689 void
2690f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2691{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002692 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002693 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002694
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002695 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2696 return;
2697
2698 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002699 if (
2700# ifdef FEAT_TERMINAL
2701 // if the popup contains a terminal it will become hidden
2702 curbuf->b_term == NULL &&
2703# endif
2704 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002705 return;
2706
2707 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002708 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002709 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002710}
2711
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002712 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002713popup_hide(win_T *wp)
2714{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002715#ifdef FEAT_TERMINAL
2716 if (error_if_term_popup_window())
2717 return;
2718#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002719 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2720 return;
2721
2722 wp->w_popup_flags |= POPF_HIDDEN;
2723 // Do not decrement b_nwindows, we still reference the buffer.
2724 redraw_all_later(UPD_NOT_VALID);
2725 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002726}
2727
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002728/*
2729 * popup_hide({id})
2730 */
2731 void
2732f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2733{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002734 int id;
2735 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002736
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002737 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2738 return;
2739
2740 id = (int)tv_get_number(argvars);
2741 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002742 if (wp == NULL)
2743 return;
2744
2745 popup_hide(wp);
2746 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002747}
2748
2749 void
2750popup_show(win_T *wp)
2751{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002752 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2753 return;
2754
2755 wp->w_popup_flags &= ~POPF_HIDDEN;
2756 redraw_all_later(UPD_NOT_VALID);
2757 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002758}
2759
2760/*
2761 * popup_show({id})
2762 */
2763 void
2764f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2765{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002766 int id;
2767 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002768
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002769 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2770 return;
2771
2772 id = (int)tv_get_number(argvars);
2773 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002774 if (wp == NULL)
2775 return;
2776
2777 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2778 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002779#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002780 if (wp->w_popup_flags & POPF_INFO)
2781 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002782#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002783}
2784
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002785/*
2786 * popup_settext({id}, {text})
2787 */
2788 void
2789f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2790{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002791 int id;
2792 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002793
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002794 if (in_vim9script()
2795 && (check_for_number_arg(argvars, 0) == FAIL
2796 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2797 return;
2798
2799 id = (int)tv_get_number(&argvars[0]);
2800 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002801 if (wp == NULL)
2802 return;
2803
2804 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2805 return;
2806
2807 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2808 redraw_win_later(wp, UPD_NOT_VALID);
2809 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002810}
2811
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002812 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002813popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002814{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002815 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002816 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002817 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002818 clear_cmdline = TRUE;
2819 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002820
Bram Moolenaar43568642022-08-28 13:02:45 +01002821#ifdef HAS_MESSAGE_WINDOW
2822 if (wp == message_win)
2823 message_win = NULL;
2824#endif
2825
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002826 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002827 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002828}
2829
Bram Moolenaar82106932020-03-13 14:34:38 +01002830 static void
2831error_for_popup_window(void)
2832{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002833 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002834}
2835
2836 int
2837error_if_popup_window(int also_with_term UNUSED)
2838{
2839 // win_execute() may set "curwin" to a popup window temporarily, but many
2840 // commands are disallowed then. When a terminal runs in the popup most
2841 // things are allowed. When a terminal is finished it can be closed.
2842 if (WIN_IS_POPUP(curwin)
2843# ifdef FEAT_TERMINAL
2844 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002845# endif
2846 )
2847 {
2848 error_for_popup_window();
2849 return TRUE;
2850 }
2851 return FALSE;
2852}
2853
Bram Moolenaarec583842019-05-26 14:11:23 +02002854/*
2855 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002856 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002857 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002858 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002859 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002860popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002861{
2862 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002863 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002864 win_T *prev = NULL;
2865
Bram Moolenaarec583842019-05-26 14:11:23 +02002866 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002867 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002868 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002869 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002870 if (wp == curwin)
2871 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002872 if (!force)
2873 {
2874 error_for_popup_window();
2875 return FAIL;
2876 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002877 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002878 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002879 if (prev == NULL)
2880 first_popupwin = wp->w_next;
2881 else
2882 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002883 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002884 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002885 }
2886
Bram Moolenaarec583842019-05-26 14:11:23 +02002887 // go through tab-local popups
2888 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002889 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002890 return OK;
2891 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002892}
2893
2894/*
2895 * Close a popup window with Window-id "id" in tabpage "tp".
2896 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002897 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002898popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002899{
2900 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002901 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002902 win_T *prev = NULL;
2903
Bram Moolenaarec583842019-05-26 14:11:23 +02002904 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2905 if (wp->w_id == id)
2906 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002907 if (wp == curwin)
2908 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002909 if (!force)
2910 {
2911 error_for_popup_window();
2912 return FAIL;
2913 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002914 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002915 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002916 if (prev == NULL)
2917 *root = wp->w_next;
2918 else
2919 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002920 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002921 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002922 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002923 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002924}
2925
2926 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002927close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002928{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002929 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002930 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002931 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002932 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002933 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002934 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002935 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002936 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002937}
2938
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002939/*
2940 * popup_move({id}, {options})
2941 */
2942 void
2943f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2944{
Bram Moolenaarae943152019-06-16 22:54:14 +02002945 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002946 int id;
2947 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002948
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002949 if (in_vim9script()
2950 && (check_for_number_arg(argvars, 0) == FAIL
2951 || check_for_dict_arg(argvars, 1) == FAIL))
2952 return;
2953
2954 id = (int)tv_get_number(argvars);
2955 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002956 if (wp == NULL)
2957 return; // invalid {id}
2958
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002959 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002960 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002961 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002962
Bram Moolenaarae943152019-06-16 22:54:14 +02002963 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002964
2965 if (wp->w_winrow + wp->w_height >= cmdline_row)
2966 clear_cmdline = TRUE;
2967 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002968}
2969
Bram Moolenaarbc133542019-05-29 20:26:46 +02002970/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002971 * popup_setoptions({id}, {options})
2972 */
2973 void
2974f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2975{
2976 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002977 int id;
2978 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002979 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002980
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002981 if (in_vim9script()
2982 && (check_for_number_arg(argvars, 0) == FAIL
2983 || check_for_dict_arg(argvars, 1) == FAIL))
2984 return;
2985
2986 id = (int)tv_get_number(argvars);
2987 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002988 if (wp == NULL)
2989 return; // invalid {id}
2990
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002991 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002992 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002993 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002994 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002995
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002996 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002997
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002998 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002999 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003000 popup_adjust_position(wp);
3001}
3002
3003/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003004 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003005 */
3006 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003007f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003008{
3009 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003010 int id;
3011 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003012 int top_extra;
3013 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003014
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003015 if (rettv_dict_alloc(rettv) == FAIL)
3016 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003017
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003018 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3019 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003020
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003021 id = (int)tv_get_number(argvars);
3022 wp = find_popup_win(id);
3023 if (wp == NULL)
3024 return; // invalid {id}
3025 top_extra = popup_top_extra(wp);
3026 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003027
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003028 // we know how much space we need, avoid resizing halfway
3029 dict = rettv->vval.v_dict;
3030 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003031
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003032 dict_add_number(dict, "line", wp->w_winrow + 1);
3033 dict_add_number(dict, "col", wp->w_wincol + 1);
3034 dict_add_number(dict, "width", wp->w_width + left_extra
3035 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3036 dict_add_number(dict, "height", wp->w_height + top_extra
3037 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003038
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003039 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3040 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3041 dict_add_number(dict, "core_width", wp->w_width);
3042 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003043
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003044 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3045 dict_add_number(dict, "firstline", wp->w_topline);
3046 dict_add_number(dict, "lastline", wp->w_botline - 1);
3047 dict_add_number(dict, "visible",
3048 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3049
3050 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003051}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003052
3053/*
3054 * popup_list()
3055 */
3056 void
3057f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3058{
3059 win_T *wp;
3060 tabpage_T *tp;
3061
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003063 return;
3064 FOR_ALL_POPUPWINS(wp)
3065 list_append_number(rettv->vval.v_list, wp->w_id);
3066 FOR_ALL_TABPAGES(tp)
3067 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3068 list_append_number(rettv->vval.v_list, wp->w_id);
3069}
3070
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003071/*
3072 * popup_locate({row}, {col})
3073 */
3074 void
3075f_popup_locate(typval_T *argvars, typval_T *rettv)
3076{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003077 int row;
3078 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003079 win_T *wp;
3080
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003081 if (in_vim9script()
3082 && (check_for_number_arg(argvars, 0) == FAIL
3083 || check_for_number_arg(argvars, 1) == FAIL))
3084 return;
3085
3086 row = tv_get_number(&argvars[0]) - 1;
3087 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003088 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003089 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003090 rettv->vval.v_number = wp->w_id;
3091}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003092
3093/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003094 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3095 */
3096 static void
3097get_padding_border(dict_T *dict, int *array, char *name)
3098{
3099 list_T *list;
3100 int i;
3101
3102 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3103 return;
3104
3105 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003106 if (list == NULL)
3107 return;
3108
3109 dict_add_list(dict, name, list);
3110 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3111 for (i = 0; i < 4; ++i)
3112 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003113}
3114
3115/*
3116 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3117 */
3118 static void
3119get_borderhighlight(dict_T *dict, win_T *wp)
3120{
3121 list_T *list;
3122 int i;
3123
3124 for (i = 0; i < 4; ++i)
3125 if (wp->w_border_highlight[i] != NULL)
3126 break;
3127 if (i == 4)
3128 return;
3129
3130 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003131 if (list == NULL)
3132 return;
3133
3134 dict_add_list(dict, "borderhighlight", list);
3135 for (i = 0; i < 4; ++i)
3136 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003137}
3138
3139/*
3140 * For popup_getoptions(): add a "borderchars" entry to "dict".
3141 */
3142 static void
3143get_borderchars(dict_T *dict, win_T *wp)
3144{
3145 list_T *list;
3146 int i;
3147 char_u buf[NUMBUFLEN];
3148 int len;
3149
3150 for (i = 0; i < 8; ++i)
3151 if (wp->w_border_char[i] != 0)
3152 break;
3153 if (i == 8)
3154 return;
3155
3156 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003157 if (list == NULL)
3158 return;
3159
3160 dict_add_list(dict, "borderchars", list);
3161 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003162 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003163 len = mb_char2bytes(wp->w_border_char[i], buf);
3164 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003165 }
3166}
3167
3168/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003169 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003170 */
3171 static void
3172get_moved_list(dict_T *dict, win_T *wp)
3173{
3174 list_T *list;
3175
3176 list = list_alloc();
3177 if (list != NULL)
3178 {
3179 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003180 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003181 list_append_number(list, wp->w_popup_mincol);
3182 list_append_number(list, wp->w_popup_maxcol);
3183 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003184 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003185 if (list == NULL)
3186 return;
3187
3188 dict_add_list(dict, "mousemoved", list);
3189 list_append_number(list, wp->w_popup_mouse_row);
3190 list_append_number(list, wp->w_popup_mouse_mincol);
3191 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003192}
3193
3194/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003195 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003196 */
3197 void
3198f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3199{
3200 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003201 int id;
3202 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003203 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003204 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003205
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003206 if (rettv_dict_alloc(rettv) == FAIL)
3207 return;
3208
3209 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3210 return;
3211
3212 id = (int)tv_get_number(argvars);
3213 wp = find_popup_win(id);
3214 if (wp == NULL)
3215 return;
3216
3217 dict = rettv->vval.v_dict;
3218 dict_add_number(dict, "line", wp->w_wantline);
3219 dict_add_number(dict, "col", wp->w_wantcol);
3220 dict_add_number(dict, "minwidth", wp->w_minwidth);
3221 dict_add_number(dict, "minheight", wp->w_minheight);
3222 dict_add_number(dict, "maxheight", wp->w_maxheight);
3223 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3224 dict_add_number(dict, "firstline", wp->w_firstline);
3225 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3226 dict_add_number(dict, "zindex", wp->w_zindex);
3227 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3228 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003229 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003230 proptype_T *pt = text_prop_type_by_id(
3231 wp->w_popup_prop_win->w_buffer,
3232 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003233
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003234 if (pt != NULL)
3235 dict_add_string(dict, "textprop", pt->pt_name);
3236 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3237 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3238 }
3239 dict_add_string(dict, "title", wp->w_popup_title);
3240 dict_add_number(dict, "wrap", wp->w_p_wrap);
3241 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3242 dict_add_number(dict, "dragall",
3243 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3244 dict_add_number(dict, "mapping",
3245 (wp->w_popup_flags & POPF_MAPPING) != 0);
3246 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3247 dict_add_number(dict, "posinvert",
3248 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3249 dict_add_number(dict, "cursorline",
3250 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3251 dict_add_string(dict, "highlight", wp->w_p_wcr);
3252 if (wp->w_scrollbar_highlight != NULL)
3253 dict_add_string(dict, "scrollbarhighlight",
3254 wp->w_scrollbar_highlight);
3255 if (wp->w_thumb_highlight != NULL)
3256 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003257
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003258 // find the tabpage that holds this popup
3259 i = 1;
3260 FOR_ALL_TABPAGES(tp)
3261 {
3262 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003263
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003264 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3265 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003266 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003267 if (twp != NULL)
3268 break;
3269 ++i;
3270 }
3271 if (tp == NULL)
3272 i = -1; // must be global
3273 else if (tp == curtab)
3274 i = 0;
3275 dict_add_number(dict, "tabpage", i);
3276
3277 get_padding_border(dict, wp->w_popup_padding, "padding");
3278 get_padding_border(dict, wp->w_popup_border, "border");
3279 get_borderhighlight(dict, wp);
3280 get_borderchars(dict, wp);
3281 get_moved_list(dict, wp);
3282
3283 if (wp->w_filter_cb.cb_name != NULL)
3284 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3285 if (wp->w_close_cb.cb_name != NULL)
3286 dict_add_callback(dict, "callback", &wp->w_close_cb);
3287
3288 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3289 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3290 {
3291 dict_add_string(dict, "pos",
3292 (char_u *)poppos_entries[i].pp_name);
3293 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003294 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003295
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003296 dict_add_string(dict, "close", (char_u *)(
3297 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3298 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003299
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003300# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003301 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3302 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003303# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003304}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003305
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003306# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003307/*
3308 * Return TRUE if the current window is running a terminal in a popup window.
3309 * Return FALSE when the job has ended.
3310 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003311 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003312error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003313{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003314 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3315 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003316 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003317 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003318 return TRUE;
3319 }
3320 return FALSE;
3321}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003322# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003323
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003324/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003325 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003326 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003327 * Each calling function should use a different flag, see the list at
3328 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003329 */
3330 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003331popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003332{
3333 win_T *wp;
3334
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003335 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003336 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003337 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003338 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003339}
3340
3341/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003342 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003343 * Must have called popup_reset_handled() first.
3344 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3345 * popup with the highest zindex.
3346 */
3347 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003348find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003349{
3350 win_T *wp;
3351 win_T *found_wp;
3352 int found_zindex;
3353
3354 found_zindex = lowest ? INT_MAX : 0;
3355 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003356 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003357 if ((wp->w_popup_handled & handled_flag) == 0
3358 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003359 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003360 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003361 {
3362 found_zindex = wp->w_zindex;
3363 found_wp = wp;
3364 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003365 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003366 if ((wp->w_popup_handled & handled_flag) == 0
3367 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003368 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003369 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003370 {
3371 found_zindex = wp->w_zindex;
3372 found_wp = wp;
3373 }
3374
3375 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003376 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003377 return found_wp;
3378}
3379
3380/*
3381 * Invoke the filter callback for window "wp" with typed character "c".
3382 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003383 * Returns the return value of the filter or -1 for CTRL-C in the current
3384 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003385 * Careful: The filter may make "wp" invalid!
3386 */
3387 static int
3388invoke_popup_filter(win_T *wp, int c)
3389{
3390 int res;
3391 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003392 typval_T argv[3];
3393 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003394 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003395 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003396
Bram Moolenaara42d9452019-06-15 21:46:30 +02003397 // Emergency exit: CTRL-C closes the popup.
3398 if (c == Ctrl_C)
3399 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003400 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003401 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003402
3403 // Reset got_int to avoid the callback isn't called.
3404 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003405 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003406 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003407
3408 // If the popup is the current window it probably fails to close. Then
3409 // do not consume the key.
3410 if (was_curwin && wp == curwin)
3411 return -1;
3412 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003413 }
3414
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003415 argv[0].v_type = VAR_NUMBER;
3416 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3417
3418 // Convert the number to a string, so that the function can use:
3419 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003420 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003421 argv[1].v_type = VAR_STRING;
3422 argv[1].vval.v_string = vim_strsave(buf);
3423
3424 argv[2].v_type = VAR_UNKNOWN;
3425
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003426 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003427 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3428 {
3429 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003430 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003431 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003432 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003433 }
3434 else
3435 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003436 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3437 popup_highlight_curline(wp);
3438
Bram Moolenaar371806e2020-10-22 13:44:54 +02003439 // If an error message was given always return FALSE, so that keys are
3440 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003441 // If we get three errors in a row then close the popup. Decrement the
3442 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3443 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003444 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003445 {
3446 wp->w_filter_errors += 10;
3447 if (wp->w_filter_errors >= 30)
3448 popup_close_with_retval(wp, -1);
3449 res = FALSE;
3450 }
3451 else
3452 {
3453 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3454 --wp->w_filter_errors;
3455 res = tv_get_bool(&rettv);
3456 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003457 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003458
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003459 vim_free(argv[1].vval.v_string);
3460 clear_tv(&rettv);
3461 return res;
3462}
3463
3464/*
3465 * Called when "c" was typed: invoke popup filter callbacks.
3466 * Returns TRUE when the character was consumed,
3467 */
3468 int
3469popup_do_filter(int c)
3470{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003471 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003472 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003473 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003474 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003475 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003476 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003477
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003478#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003479 // Popup window with terminal always gets focus.
3480 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3481 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003482#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003483
Bram Moolenaar934470e2019-09-01 23:27:05 +02003484 if (recursive)
3485 return FALSE;
3486 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003487
Bram Moolenaarf6396232019-08-24 19:36:00 +02003488 if (c == K_LEFTMOUSE)
3489 {
3490 int row = mouse_row;
3491 int col = mouse_col;
3492
3493 wp = mouse_find_win(&row, &col, FIND_POPUP);
3494 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003495 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003496 }
3497
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003498 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003499 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003500 while (res == FALSE
3501 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003502 if (wp->w_filter_cb.cb_name != NULL
3503 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003504 res = invoke_popup_filter(wp, c);
3505
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003506 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003507 {
3508 int save_got_int = got_int;
3509
3510 // Reset got_int to avoid a function used in the statusline aborts.
3511 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003512 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003513 got_int |= save_got_int;
3514 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003515 recursive = FALSE;
3516 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003517
3518 // When interrupted return FALSE to avoid looping.
3519 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003520}
3521
Bram Moolenaar3397f742019-06-02 18:40:06 +02003522/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003523 * Return TRUE if there is a popup visible with a filter callback and the
3524 * "mapping" property off.
3525 */
3526 int
3527popup_no_mapping(void)
3528{
3529 int round;
3530 win_T *wp;
3531
3532 for (round = 1; round <= 2; ++round)
3533 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3534 wp != NULL; wp = wp->w_next)
3535 if (wp->w_filter_cb.cb_name != NULL
3536 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3537 return TRUE;
3538 return FALSE;
3539}
3540
3541/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003542 * Called when the cursor moved: check if any popup needs to be closed if the
3543 * cursor moved far enough.
3544 */
3545 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003546popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003547{
3548 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003549
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003550 popup_reset_handled(POPUP_HANDLED_3);
3551 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003552 if (wp->w_popup_curwin != NULL
3553 && (curwin != wp->w_popup_curwin
3554 || curwin->w_cursor.lnum != wp->w_popup_lnum
3555 || curwin->w_cursor.col < wp->w_popup_mincol
3556 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003557 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003558}
3559
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003560/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003561 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003562 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003563 static void
3564popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003565{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003566 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003567 char_u *cells;
3568 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003569
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003570 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3571 {
3572 vim_free(wp->w_popup_mask_cells);
3573 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003574 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003575 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003576 if (wp->w_popup_mask_cells != NULL
3577 && wp->w_popup_mask_height == height
3578 && wp->w_popup_mask_width == width)
3579 return; // cache is still valid
3580
3581 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003582 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003583 if (wp->w_popup_mask_cells == NULL)
3584 return;
3585 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003586
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003587 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003588 {
3589 int cols, cole;
3590 int lines, linee;
3591
3592 li = lio->li_tv.vval.v_list->lv_first;
3593 cols = tv_get_number(&li->li_tv);
3594 if (cols < 0)
3595 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003596 if (cols <= 0)
3597 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003598 li = li->li_next;
3599 cole = tv_get_number(&li->li_tv);
3600 if (cole < 0)
3601 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003602 if (cole > width)
3603 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003604 li = li->li_next;
3605 lines = tv_get_number(&li->li_tv);
3606 if (lines < 0)
3607 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003608 if (lines <= 0)
3609 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003610 li = li->li_next;
3611 linee = tv_get_number(&li->li_tv);
3612 if (linee < 0)
3613 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003614 if (linee > height)
3615 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003616
Bram Moolenaar4012d262020-12-29 11:57:46 +01003617 for (row = lines - 1; row < linee; ++row)
3618 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003619 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003620 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003621}
3622
3623/*
3624 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3625 * "col" and "line" are screen coordinates.
3626 */
3627 static int
3628popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3629{
3630 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3631 int line = screenline - wp->w_winrow;
3632
3633 return col >= 0 && col < width
3634 && line >= 0 && line < height
3635 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003636}
3637
3638/*
3639 * Set flags in popup_transparent[] for window "wp" to "val".
3640 */
3641 static void
3642update_popup_transparent(win_T *wp, int val)
3643{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003644 if (wp->w_popup_mask == NULL)
3645 return;
3646
3647 int width = popup_width(wp);
3648 int height = popup_height(wp);
3649 listitem_T *lio, *li;
3650 int cols, cole;
3651 int lines, linee;
3652 int col, line;
3653
3654 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003655 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003656 li = lio->li_tv.vval.v_list->lv_first;
3657 cols = tv_get_number(&li->li_tv);
3658 if (cols < 0)
3659 cols = width + cols + 1;
3660 li = li->li_next;
3661 cole = tv_get_number(&li->li_tv);
3662 if (cole < 0)
3663 cole = width + cole + 1;
3664 li = li->li_next;
3665 lines = tv_get_number(&li->li_tv);
3666 if (lines < 0)
3667 lines = height + lines + 1;
3668 li = li->li_next;
3669 linee = tv_get_number(&li->li_tv);
3670 if (linee < 0)
3671 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003672
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003673 --cols;
3674 cols -= wp->w_popup_leftoff;
3675 if (cols < 0)
3676 cols = 0;
3677 cole -= wp->w_popup_leftoff;
3678 --lines;
3679 if (lines < 0)
3680 lines = 0;
3681 for (line = lines; line < linee
3682 && line + wp->w_winrow < screen_Rows; ++line)
3683 for (col = cols; col < cole
3684 && col + wp->w_wincol < screen_Columns; ++col)
3685 popup_transparent[(line + wp->w_winrow) * screen_Columns
3686 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003687 }
3688}
3689
3690/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003691 * Only called when popup window "wp" is hidden: If the window is positioned
3692 * next to a text property, and it is now visible, then unhide the popup.
3693 * We don't check if visible popups become hidden, that is done in
3694 * popup_adjust_position().
3695 * Return TRUE if the popup became unhidden.
3696 */
3697 static int
3698check_popup_unhidden(win_T *wp)
3699{
3700 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3701 {
3702 textprop_T prop;
3703 linenr_T lnum;
3704
Bram Moolenaar27724252022-05-08 15:00:04 +01003705 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3706 && find_visible_prop(wp->w_popup_prop_win,
3707 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003708 &prop, &lnum) == OK)
3709 {
3710 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003711 wp->w_popup_prop_topline = 0; // force repositioning
3712 return TRUE;
3713 }
3714 }
3715 return FALSE;
3716}
3717
3718/*
3719 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3720 * That is when the buffer in the popup was changed, or the popup is following
3721 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003722 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003723 */
3724 static int
3725popup_need_position_adjust(win_T *wp)
3726{
3727 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3728 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003729 if (win_valid(wp->w_popup_prop_win)
3730 && (wp->w_popup_prop_changedtick
3731 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3732 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3733 return TRUE;
3734
3735 // May need to adjust the width if the cursor moved.
3736 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003737}
3738
3739/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003740 * Update "popup_mask" if needed.
3741 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003742 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003743 * Also marks window lines for redrawing.
3744 */
3745 void
3746may_update_popup_mask(int type)
3747{
3748 win_T *wp;
3749 short *mask;
3750 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003751 int redraw_all_popups = FALSE;
3752 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003753
3754 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003755 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3756 // basic (such as the screen size) must have changed.
3757 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003758 {
3759 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003760 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003761 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003762
3763 // Check if any popup window buffer has changed and if any popup connected
3764 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003765 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003766 if (wp->w_popup_flags & POPF_HIDDEN)
3767 popup_mask_refresh |= check_popup_unhidden(wp);
3768 else if (popup_need_position_adjust(wp))
3769 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003770 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003771 if (wp->w_popup_flags & POPF_HIDDEN)
3772 popup_mask_refresh |= check_popup_unhidden(wp);
3773 else if (popup_need_position_adjust(wp))
3774 popup_mask_refresh = TRUE;
3775
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003776 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003777 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003778
3779 // Need to update the mask, something has changed.
3780 popup_mask_refresh = FALSE;
3781 popup_mask_tab = curtab;
3782 popup_visible = FALSE;
3783
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003784 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 // If redrawing only what is needed, update "popup_mask_next" and then
3786 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003787 redrawing_all_win = TRUE;
3788 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003789 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003790 redrawing_all_win = FALSE;
3791 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003792 mask = popup_mask;
3793 else
3794 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003795 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003796
3797 // Find the window with the lowest zindex that hasn't been handled yet,
3798 // so that the window with a higher zindex overwrites the value in
3799 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003800 popup_reset_handled(POPUP_HANDLED_4);
3801 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003802 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003803 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003804 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003805
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003806 popup_visible = TRUE;
3807
Bram Moolenaar12034e22019-08-25 22:25:02 +02003808 // Recompute the position if the text changed. It may make the popup
3809 // hidden if it's attach to a text property that is no longer visible.
3810 if (redraw_all_popups || popup_need_position_adjust(wp))
3811 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003812 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003813 if (wp->w_popup_flags & POPF_HIDDEN)
3814 continue;
3815 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003816
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003817 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003818 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003819 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003820 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003821 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003822 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003823 col < wp->w_wincol + width - wp->w_popup_leftoff
3824 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003825 if (wp->w_zindex < POPUPMENU_ZINDEX
3826 && pum_visible()
3827 && pum_under_menu(line, col, FALSE))
3828 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3829 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003830 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003831 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003832 }
3833
3834 // Only check which lines are to be updated if not already
3835 // updating all lines.
3836 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003837 {
3838 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3839 win_T *prev_wp = NULL;
3840
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003841 for (line = 0; line < screen_Rows; ++line)
3842 {
3843 int col_done = 0;
3844
3845 for (col = 0; col < screen_Columns; ++col)
3846 {
3847 int off = line * screen_Columns + col;
3848
3849 if (popup_mask[off] != popup_mask_next[off])
3850 {
3851 popup_mask[off] = popup_mask_next[off];
3852
3853 if (line >= cmdline_row)
3854 {
3855 // the command line needs to be cleared if text below
3856 // the popup is now visible.
3857 if (!msg_scrolled && popup_mask_next[off] == 0)
3858 clear_cmdline = TRUE;
3859 }
3860 else if (col >= col_done)
3861 {
3862 linenr_T lnum;
3863 int line_cp = line;
3864 int col_cp = col;
3865
3866 // The screen position "line" / "col" needs to be
3867 // redrawn. Figure out what window that is and update
3868 // w_redraw_top and w_redr_bot. Only needs to be done
3869 // once for each window line.
3870 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3871 if (wp != NULL)
3872 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003873#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003874 // A terminal window needs to be redrawn.
3875 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003876 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003877 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003878#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003879 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003880 if (wp != prev_wp)
3881 {
3882 vim_memset(plines_cache, 0,
3883 sizeof(int) * Rows);
3884 prev_wp = wp;
3885 }
3886
3887 if (line_cp >= wp->w_height)
3888 // In (or below) status line
3889 wp->w_redr_status = TRUE;
3890 else
3891 {
3892 // compute the position in the buffer line
3893 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003894 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003895 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003896 redrawWinline(wp, lnum);
3897 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003898 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899
3900 // This line is going to be redrawn, no need to
3901 // check until the right side of the window.
3902 col_done = wp->w_wincol + wp->w_width - 1;
3903 }
3904 }
3905 }
3906 }
3907 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003908
3909 vim_free(plines_cache);
3910 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003911
3912 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003913}
3914
3915/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003916 * If the current window is a popup and something relevant changed, recompute
3917 * the position and size.
3918 */
3919 void
3920may_update_popup_position(void)
3921{
3922 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3923 popup_adjust_position(curwin);
3924}
3925
3926/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927 * Return a string of "len" spaces in IObuff.
3928 */
3929 static char_u *
3930get_spaces(int len)
3931{
3932 vim_memset(IObuff, ' ', (size_t)len);
3933 IObuff[len] = NUL;
3934 return IObuff;
3935}
3936
3937/*
3938 * Update popup windows. They are drawn on top of normal windows.
3939 * "win_update" is called for each popup window, lowest zindex first.
3940 */
3941 void
3942update_popups(void (*win_update)(win_T *wp))
3943{
3944 win_T *wp;
3945 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003946 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003947 int total_width;
3948 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003949 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 int popup_attr;
3951 int border_attr[4];
3952 int border_char[8];
3953 char_u buf[MB_MAXBYTES];
3954 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003955 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003956 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003957 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003958 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003959 int sb_thumb_top = 0;
3960 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003961 int attr_scroll = 0;
3962 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003963
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003964 // hide the cursor until redrawing is done.
3965 cursor_off();
3966
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003967 // Find the window with the lowest zindex that hasn't been updated yet,
3968 // so that the window with a higher zindex is drawn later, thus goes on
3969 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003970 popup_reset_handled(POPUP_HANDLED_5);
3971 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003972 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003973 int title_len = 0;
3974 int title_wincol;
3975
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003976 // This drawing uses the zindex of the popup window, so that it's on
3977 // top of the text but doesn't draw when another popup with higher
3978 // zindex is on top of the character.
3979 screen_zindex = wp->w_zindex;
3980
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003981 // Set flags in popup_transparent[] for masked cells.
3982 update_popup_transparent(wp, 1);
3983
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003984 // adjust w_winrow and w_wincol for border and padding, since
3985 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003986 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003987 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3988 - wp->w_popup_leftoff;
3989 if (wp->w_wincol + left_extra < 0)
3990 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003991 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003992 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003993
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003994 // Draw the popup text, unless it's off screen.
3995 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003996 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003997 // May need to update the "cursorline" highlighting, which may also
3998 // change "topline"
3999 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4000 popup_highlight_curline(wp);
4001
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004002 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004003
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004004 // move the cursor into the visible lines, otherwise executing
4005 // commands with win_execute() may cause the text to jump.
4006 if (wp->w_cursor.lnum < wp->w_topline)
4007 wp->w_cursor.lnum = wp->w_topline;
4008 else if (wp->w_cursor.lnum >= wp->w_botline)
4009 wp->w_cursor.lnum = wp->w_botline - 1;
4010 }
4011
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004012 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004013 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004014
4015 // Add offset for border and padding if not done already.
4016 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4017 {
4018 wp->w_wcol += left_extra;
4019 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4020 }
4021 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004022 {
4023 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004024 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004025 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004026
Bram Moolenaareabddc42022-04-02 15:32:16 +01004027 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004028 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004029 popup_attr = get_wcr_attr(wp);
4030
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004031 if (wp->w_winrow + total_height > cmdline_row)
4032 wp->w_popup_flags |= POPF_ON_CMDLINE;
4033 else
4034 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4035
4036
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004037 // We can only use these line drawing characters when 'encoding' is
4038 // "utf-8" and 'ambiwidth' is "single".
4039 if (enc_utf8 && *p_ambw == 's')
4040 {
4041 border_char[0] = border_char[2] = 0x2550;
4042 border_char[1] = border_char[3] = 0x2551;
4043 border_char[4] = 0x2554;
4044 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004045 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4046 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004047 border_char[7] = 0x255a;
4048 }
4049 else
4050 {
4051 border_char[0] = border_char[2] = '-';
4052 border_char[1] = border_char[3] = '|';
4053 for (i = 4; i < 8; ++i)
4054 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004055 if (wp->w_popup_flags & POPF_RESIZE)
4056 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004057 }
4058 for (i = 0; i < 8; ++i)
4059 if (wp->w_border_char[i] != 0)
4060 border_char[i] = wp->w_border_char[i];
4061
4062 for (i = 0; i < 4; ++i)
4063 {
4064 border_attr[i] = popup_attr;
4065 if (wp->w_border_highlight[i] != NULL)
4066 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4067 }
4068
Bram Moolenaard91467f2020-11-21 12:42:09 +01004069 // Title goes on top of border or padding.
4070 title_wincol = wp->w_wincol + 1;
4071 if (wp->w_popup_title != NULL)
4072 {
rbtnnc6119412021-08-07 13:08:45 +02004073 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004074
Ralf Schandlbc869872021-05-28 14:12:14 +02004075 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004076 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004077 {
4078 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4079 char_u *title_text = alloc(title_byte_len + 1);
4080
4081 if (title_text != NULL)
4082 {
4083 trunc_string(wp->w_popup_title, title_text,
4084 total_width - 2, title_byte_len + 1);
4085 screen_puts(title_text, wp->w_winrow, title_wincol,
4086 wp->w_popup_border[0] > 0
4087 ? border_attr[0] : popup_attr);
4088 vim_free(title_text);
4089 }
4090
Bram Moolenaard91467f2020-11-21 12:42:09 +01004091 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004092 }
4093 else
4094 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4095 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004096 }
4097
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004098 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004099 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004100 if (wp->w_popup_border[0] > 0)
4101 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004102 // top border; do not draw over the title
4103 if (title_len > 0)
4104 {
4105 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4106 wincol < 0 ? 0 : wincol, title_wincol,
4107 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004109 border_char[0], border_attr[0]);
4110 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4111 title_wincol + title_len, wincol + total_width,
4112 border_char[0], border_char[0], border_attr[0]);
4113 }
4114 else
4115 {
4116 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4117 wincol < 0 ? 0 : wincol, wincol + total_width,
4118 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4119 ? border_char[4] : border_char[0],
4120 border_char[0], border_attr[0]);
4121 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004122 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004123 {
4124 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4125 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004126 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004127 }
4128 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004129 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4130 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004131
Bram Moolenaard529ba52019-07-02 23:13:53 +02004132 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4133 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004134 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004135 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004136 - wp->w_has_scrollbar;
4137 if (padcol < 0)
4138 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004139 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004140 padcol = 0;
4141 }
4142 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004143 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004144 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004145 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004146 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004147 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004148 // top padding and no border; do not draw over the title
4149 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004150 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004151 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004152 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004153 row += 1;
4154 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004155 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004156 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004157 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004158 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004159
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004160 // Compute scrollbar thumb position and size.
4161 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004162 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004163 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4164 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004165 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004166
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004167 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4168 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004169 if (wp->w_topline > 1 && sb_thumb_height == height)
4170 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004171 if (sb_thumb_height == 0)
4172 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004173 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004174 // it just fits, avoid divide by zero
4175 sb_thumb_top = 0;
4176 else
4177 sb_thumb_top = (wp->w_topline - 1
4178 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004179 * (wp->w_height - sb_thumb_height)
4180 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004181 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4182 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004183 last = total_height - top_off - wp->w_popup_border[2];
4184 if (sb_thumb_top >= last)
4185 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004186 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004187
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004188 if (wp->w_scrollbar_highlight != NULL)
4189 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4190 else
4191 attr_scroll = highlight_attr[HLF_PSB];
4192 if (wp->w_thumb_highlight != NULL)
4193 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4194 else
4195 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004196 }
4197
4198 for (i = wp->w_popup_border[0];
4199 i < total_height - wp->w_popup_border[2]; ++i)
4200 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004201 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004202 // left and right padding only needed next to the body
4203 int do_padding =
4204 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4205 && i < total_height - wp->w_popup_border[2]
4206 - wp->w_popup_padding[2];
4207
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004208 row = wp->w_winrow + i;
4209
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004210 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004211 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004212 {
4213 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004214 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004215 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004216 if (do_padding && wp->w_popup_padding[3] > 0)
4217 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004218 int col = wincol + wp->w_popup_border[3];
4219
Bram Moolenaard529ba52019-07-02 23:13:53 +02004220 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004221 pad_left = wp->w_popup_padding[3];
4222 if (col < 0)
4223 {
4224 pad_left += col;
4225 col = 0;
4226 }
4227 if (pad_left > 0)
4228 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4229 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004230 // scrollbar
4231 if (wp->w_has_scrollbar)
4232 {
4233 int line = i - top_off;
4234 int scroll_col = wp->w_wincol + total_width - 1
4235 - wp->w_popup_border[1];
4236
4237 if (line >= 0 && line < wp->w_height)
4238 screen_putchar(' ', row, scroll_col,
4239 line >= sb_thumb_top
4240 && line < sb_thumb_top + sb_thumb_height
4241 ? attr_thumb : attr_scroll);
4242 else
4243 screen_putchar(' ', row, scroll_col, popup_attr);
4244 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004245 // right border
4246 if (wp->w_popup_border[1] > 0)
4247 {
4248 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004249 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004250 }
4251 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004252 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004253 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004254 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004255 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4256 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004257 }
4258
4259 if (wp->w_popup_padding[2] > 0)
4260 {
4261 // bottom padding
4262 row = wp->w_winrow + wp->w_popup_border[0]
4263 + wp->w_popup_padding[0] + wp->w_height;
4264 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004265 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004266 }
4267
4268 if (wp->w_popup_border[2] > 0)
4269 {
4270 // bottom border
4271 row = wp->w_winrow + total_height - 1;
4272 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004273 wincol < 0 ? 0 : wincol,
4274 wincol + total_width,
4275 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004276 ? border_char[7] : border_char[2],
4277 border_char[2], border_attr[2]);
4278 if (wp->w_popup_border[1] > 0)
4279 {
4280 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004281 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004282 }
4283 }
4284
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004285 if (wp->w_popup_close == POPCLOSE_BUTTON)
4286 {
4287 // close button goes on top of anything at the top-right corner
4288 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004289 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004290 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4291 }
4292
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004293 update_popup_transparent(wp, 0);
4294
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004295 // Back to the normal zindex.
4296 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004297
4298#ifdef HAS_MESSAGE_WINDOW
4299 // if this was the message window popup may start the timer now
4300 may_start_message_win_timer(wp);
4301#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004302 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004303
4304#if defined(FEAT_SEARCH_EXTRA)
4305 // In case win_update() called start_search_hl().
4306 end_search_hl();
4307#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004308}
4309
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004310/*
4311 * Mark references in callbacks of one popup window.
4312 */
4313 static int
4314set_ref_in_one_popup(win_T *wp, int copyID)
4315{
4316 int abort = FALSE;
4317 typval_T tv;
4318
4319 if (wp->w_close_cb.cb_partial != NULL)
4320 {
4321 tv.v_type = VAR_PARTIAL;
4322 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4323 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4324 }
4325 if (wp->w_filter_cb.cb_partial != NULL)
4326 {
4327 tv.v_type = VAR_PARTIAL;
4328 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4329 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4330 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004331 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004332 return abort;
4333}
4334
4335/*
4336 * Set reference in callbacks of popup windows.
4337 */
4338 int
4339set_ref_in_popups(int copyID)
4340{
4341 int abort = FALSE;
4342 win_T *wp;
4343 tabpage_T *tp;
4344
4345 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4346 abort = abort || set_ref_in_one_popup(wp, copyID);
4347
4348 FOR_ALL_TABPAGES(tp)
4349 {
4350 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4351 abort = abort || set_ref_in_one_popup(wp, copyID);
4352 if (abort)
4353 break;
4354 }
4355 return abort;
4356}
Bram Moolenaar79648732019-07-18 21:43:07 +02004357
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004358 int
4359popup_is_popup(win_T *wp)
4360{
4361 return wp->w_popup_flags != 0;
4362}
4363
4364#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004365/*
4366 * Find an existing popup used as the preview window, in the current tab page.
4367 * Return NULL if not found.
4368 */
4369 win_T *
4370popup_find_preview_window(void)
4371{
4372 win_T *wp;
4373
4374 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004375 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004376 if (wp->w_p_pvw)
4377 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004378 return NULL;
4379}
4380
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004381/*
4382 * Find an existing popup used as the info window, in the current tab page.
4383 * Return NULL if not found.
4384 */
4385 win_T *
4386popup_find_info_window(void)
4387{
4388 win_T *wp;
4389
4390 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004391 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004392 if (wp->w_popup_flags & POPF_INFO)
4393 return wp;
4394 return NULL;
4395}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004396#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004397
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004398 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004399f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4400{
4401#ifdef HAS_MESSAGE_WINDOW
4402 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4403#else
4404 rettv->vval.v_number = 0;
4405#endif
4406}
4407
4408 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004409f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4410{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004411#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004412 win_T *wp = popup_find_info_window();
4413
4414 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004415#else
4416 rettv->vval.v_number = 0;
4417#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004418}
4419
4420 void
4421f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004422{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004423#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004424 win_T *wp = popup_find_preview_window();
4425
4426 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004427#else
4428 rettv->vval.v_number = 0;
4429#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004430}
4431
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004432#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004433/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004434 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004435 * NOTE: this makes the popup the current window, so that the file can be
4436 * edited. However, it must not remain to be the current window, the caller
4437 * must make sure of that.
4438 */
4439 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004440popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004441{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004442 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004443
4444 if (wp == NULL)
4445 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004446 if (info)
4447 wp->w_popup_flags |= POPF_INFO;
4448 else
4449 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004450
4451 // Set the width to a reasonable value, so that w_topline can be computed.
4452 if (wp->w_minwidth > 0)
4453 wp->w_width = wp->w_minwidth;
4454 else if (wp->w_maxwidth > 0)
4455 wp->w_width = wp->w_maxwidth;
4456 else
4457 wp->w_width = curwin->w_width;
4458
4459 // Will switch to another buffer soon, dummy one can be wiped.
4460 wp->w_buffer->b_locked = FALSE;
4461
4462 win_enter(wp, FALSE);
4463 return OK;
4464}
4465
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004466/*
4467 * Close any preview popup.
4468 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004469 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004470popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004471{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004472 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004473
4474 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004475 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004476}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004477
4478/*
4479 * Hide the info popup.
4480 */
4481 void
4482popup_hide_info(void)
4483{
4484 win_T *wp = popup_find_info_window();
4485
4486 if (wp != NULL)
4487 popup_hide(wp);
4488}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004489
4490/*
4491 * Close any info popup.
4492 */
4493 void
4494popup_close_info(void)
4495{
4496 win_T *wp = popup_find_info_window();
4497
4498 if (wp != NULL)
4499 popup_close_with_retval(wp, -1);
4500}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004501#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004502
Bram Moolenaar9198de32022-08-27 21:30:03 +01004503#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4504
Bram Moolenaar9198de32022-08-27 21:30:03 +01004505/*
4506 * Get the message window.
4507 * Returns NULL if something failed.
4508 */
4509 win_T *
4510popup_get_message_win(void)
4511{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004512 if (message_win != NULL)
4513 return message_win;
4514
4515 int i;
4516
4517 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4518
Bram Moolenaar9198de32022-08-27 21:30:03 +01004519 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004520 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004521
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004522 // use the full screen width
4523 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004524
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004525 // position at bottom of screen
4526 message_win->w_popup_pos = POPPOS_BOTTOM;
4527 message_win->w_wantcol = 1;
4528 message_win->w_minwidth = 9999;
4529 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004530
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004531 // no padding, border at the top
4532 for (i = 0; i < 4; ++i)
4533 message_win->w_popup_padding[i] = 0;
4534 for (i = 1; i < 4; ++i)
4535 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004536
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004537 if (message_win->w_popup_timer != NULL)
4538 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004539 return message_win;
4540}
4541
4542/*
4543 * If the message window is not visible: show it
4544 * If the message window is visible: reset the timeout
4545 */
4546 void
4547popup_show_message_win(void)
4548{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004549 if (message_win == NULL)
4550 return;
4551
4552 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004553 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004554 // the highlight may have changed.
4555 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4556 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004557 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004558 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004559}
4560
4561 static void
4562may_start_message_win_timer(win_T *wp)
4563{
4564 if (wp == message_win && start_message_win_timer)
4565 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004566 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004567 {
4568 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004569 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004570 message_win_time = 3000;
4571 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004572 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004573 }
4574}
4575
4576 int
4577popup_message_win_visible(void)
4578{
4579 return message_win != NULL
4580 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4581}
4582
4583/*
4584 * If the message window is visible: hide it.
4585 */
4586 void
4587popup_hide_message_win(void)
4588{
4589 if (message_win != NULL)
4590 popup_hide(message_win);
4591}
4592
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004593// Values saved in start_echowindow() and restored in end_echowindow()
4594static int save_msg_didout = FALSE;
4595static int save_msg_col = 0;
4596// Values saved in end_echowindow() and restored in start_echowindow()
4597static int ew_msg_didout = FALSE;
4598static int ew_msg_col = 0;
4599
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004600/*
4601 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004602 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004603 */
4604 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004605start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004606{
4607 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004608 save_msg_didout = msg_didout;
4609 save_msg_col = msg_col;
4610 msg_didout = ew_msg_didout;
4611 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004612 if (time_sec != 0)
4613 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004614}
4615
4616/*
4617 * Invoked after outputting a message for ":echowindow".
4618 */
4619 void
4620end_echowindow(void)
4621{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004622 in_echowindow = FALSE;
4623
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004624 if ((State & MODE_HITRETURN) == 0)
4625 // show the message window now
4626 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004627
4628 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004629 ew_msg_didout = TRUE;
4630 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4631 msg_didout = save_msg_didout;
4632 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004633}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004634#endif
4635
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004636/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004637 * Close any popup for a text property associated with "win".
4638 * Return TRUE if a popup was closed.
4639 */
4640 int
4641popup_win_closed(win_T *win)
4642{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004643 int round;
4644 win_T *wp;
4645 win_T *next;
4646 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004647
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004648 for (round = 1; round <= 2; ++round)
4649 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4650 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004651 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004652 next = wp->w_next;
4653 if (wp->w_popup_prop_win == win)
4654 {
4655 popup_close_with_retval(wp, -1);
4656 ret = TRUE;
4657 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004658 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004659 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004660}
4661
4662/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004663 * Set the title of the popup window to the file name.
4664 */
4665 void
4666popup_set_title(win_T *wp)
4667{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004668 if (wp->w_buffer->b_fname == NULL)
4669 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004670
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004671 char_u dirname[MAXPATHL];
4672 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004673
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004674 mch_dirname(dirname, MAXPATHL);
4675 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4676
4677 vim_free(wp->w_popup_title);
4678 len = STRLEN(wp->w_buffer->b_fname) + 3;
4679 wp->w_popup_title = alloc((int)len);
4680 if (wp->w_popup_title != NULL)
4681 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4682 wp->w_buffer->b_fname);
4683 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004684}
4685
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004686# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004687/*
4688 * If there is a preview window, update the title.
4689 * Used after changing directory.
4690 */
4691 void
4692popup_update_preview_title(void)
4693{
4694 win_T *wp = popup_find_preview_window();
4695
4696 if (wp != NULL)
4697 popup_set_title(wp);
4698}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004699# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004700
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004701#endif // FEAT_PROP_POPUP