blob: 536e1b64c678279ce31ed627cc5b213bb0ef7373 [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
John Marriott45377e22025-03-27 18:12:32 +010046static int popup_on_cmdline = FALSE;
47
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020048static void popup_adjust_position(win_T *wp);
49
Bram Moolenaar4d784b22019-05-25 19:51:39 +020050/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020051 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020052 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020053 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054 */
55 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020056popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020057{
58 dictitem_T *di;
59 char_u *val;
60 char_u *s;
61 char_u *endp;
62 int n = 0;
63
64 di = dict_find(dict, key, -1);
65 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020066 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020067
68 val = tv_get_string(&di->di_tv);
69 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020070 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020071
72 setcursor_mayforce(TRUE);
73 s = val + 6;
74 if (*s != NUL)
75 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020076 endp = s;
77 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
78 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020079 if (endp != NULL && *skipwhite(endp) != NUL)
80 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020081 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020082 return 0;
83 }
84 }
85
86 if (STRCMP(key, "line") == 0)
87 n = screen_screenrow() + 1 + n;
88 else // "col"
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +020089 n = screen_screencol() + 1 + n;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020090
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020091 // Zero means "not set", use -1 instead.
92 if (n == 0)
93 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020094 return n;
95}
96
Christian Brabandtf6cdab32023-08-11 23:42:02 +020097 static int
Bram Moolenaarae943152019-06-16 22:54:14 +020098set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020099{
100 dictitem_T *di;
101
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000103 if (di == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200104 return OK;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000106 if (di->di_tv.v_type != VAR_LIST)
107 {
108 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200109 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000110 }
111
112 list_T *list = di->di_tv.vval.v_list;
113 listitem_T *li;
114 int i;
115 int nr;
116
117 for (i = 0; i < 4; ++i)
118 array[i] = 1;
119 if (list == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200120 return OK;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000121
122 CHECK_LIST_MATERIALIZE(list);
123 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
124 ++i, li = li->li_next)
125 {
126 nr = (int)tv_get_number(&li->li_tv);
127 if (nr >= 0)
128 array[i] = nr > max_val ? max_val : nr;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200129 }
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200130
131 return OK;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200132}
133
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200134/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200135 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200136 */
137 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200138set_moved_values(win_T *wp)
139{
140 wp->w_popup_curwin = curwin;
141 wp->w_popup_lnum = curwin->w_cursor.lnum;
142 wp->w_popup_mincol = curwin->w_cursor.col;
143 wp->w_popup_maxcol = curwin->w_cursor.col;
144}
145
146/*
147 * Used when popup options contain "moved" with "word" or "WORD".
148 */
149 static void
150set_moved_columns(win_T *wp, int flags)
151{
152 char_u *ptr;
153 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
154
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000155 if (len <= 0)
156 return;
157
158 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
159 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
Bram Moolenaar17627312019-06-02 19:53:44 +0200160}
161
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200162/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200163 * Used when popup options contain "mousemoved": set default moved values.
164 */
165 static void
166set_mousemoved_values(win_T *wp)
167{
168 wp->w_popup_mouse_row = mouse_row;
169 wp->w_popup_mouse_mincol = mouse_col;
170 wp->w_popup_mouse_maxcol = mouse_col;
171}
172
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000173 static void
174update_popup_uses_mouse_move(void)
175{
176 popup_uses_mouse_move = FALSE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000177 if (!popup_visible)
178 return;
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000179
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000180 win_T *wp;
181
182 FOR_ALL_POPUPWINS(wp)
183 if (wp->w_popup_mouse_row != 0)
184 {
185 popup_uses_mouse_move = TRUE;
186 return;
187 }
188 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
189 if (wp->w_popup_mouse_row != 0)
190 {
191 popup_uses_mouse_move = TRUE;
192 return;
193 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000194}
195
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200196/*
197 * Used when popup options contain "moved" with "word" or "WORD".
198 */
199 static void
200set_mousemoved_columns(win_T *wp, int flags)
201{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200202 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200203 char_u *text;
204 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200205 pos_T pos;
206 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200207
208 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000209 &textwp, &pos.lnum, &text, NULL, &col) != OK)
210 return;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200211
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000212 // convert text column to mouse column
213 pos.col = col;
214 pos.coladd = 0;
215 getvcol(textwp, &pos, &mcol, NULL, NULL);
216 wp->w_popup_mouse_mincol = mcol;
217
218 pos.col = col + (colnr_T)STRLEN(text) - 1;
219 getvcol(textwp, &pos, NULL, NULL, &mcol);
220 wp->w_popup_mouse_maxcol = mcol;
221 vim_free(text);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200222}
223
224/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200225 * Return TRUE if "row"/"col" is on the border of the popup.
226 * The values are relative to the top-left corner.
227 */
228 int
229popup_on_border(win_T *wp, int row, int col)
230{
231 return (row == 0 && wp->w_popup_border[0] > 0)
232 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
233 || (col == 0 && wp->w_popup_border[3] > 0)
234 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
235}
236
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200237/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200238 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
239 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200240 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200241 * Caller should check the left mouse button was clicked.
242 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200243 */
244 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200245popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200246{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200247 if (wp->w_popup_close == POPCLOSE_BUTTON
248 && row == 0 && col == popup_width(wp) - 1)
249 {
250 popup_close_for_mouse_click(wp);
251 return TRUE;
252 }
253 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200254}
255
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256// Values set when dragging a popup window starts.
257static int drag_start_row;
258static int drag_start_col;
259static int drag_start_wantline;
260static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200261static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200262
263/*
264 * Mouse down on border of popup window: start dragging it.
265 * Uses mouse_col and mouse_row.
266 */
267 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200268popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200269{
270 drag_start_row = mouse_row;
271 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200272 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200273 drag_start_wantline = wp->w_winrow + 1;
274 else
275 drag_start_wantline = wp->w_wantline;
276 if (wp->w_wantcol == 0)
277 drag_start_wantcol = wp->w_wincol + 1;
278 else
279 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200280
281 // Stop centering the popup
282 if (wp->w_popup_pos == POPPOS_CENTER)
283 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200284
285 drag_on_resize_handle = wp->w_popup_border[1] > 0
286 && wp->w_popup_border[2] > 0
287 && row == popup_height(wp) - 1
288 && col == popup_width(wp) - 1;
289
290 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
291 {
292 if (wp->w_popup_pos == POPPOS_TOPRIGHT
293 || wp->w_popup_pos == POPPOS_BOTRIGHT)
294 wp->w_wantcol = wp->w_wincol + 1;
295 if (wp->w_popup_pos == POPPOS_BOTLEFT)
296 wp->w_wantline = wp->w_winrow + 1;
297 wp->w_popup_pos = POPPOS_TOPLEFT;
298 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200299}
300
301/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200302 * Mouse moved while dragging a popup window: adjust the window popup position
303 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200304 */
305 void
306popup_drag(win_T *wp)
307{
308 // The popup may be closed before dragging stops.
309 if (!win_valid_popup(wp))
310 return;
311
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200312 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
313 {
314 int width_inc = mouse_col - drag_start_col;
315 int height_inc = mouse_row - drag_start_row;
316
317 if (width_inc != 0)
318 {
319 int width = wp->w_width + width_inc;
320
321 if (width < 1)
322 width = 1;
323 wp->w_minwidth = width;
324 wp->w_maxwidth = width;
325 drag_start_col = mouse_col;
326 }
327
328 if (height_inc != 0)
329 {
330 int height = wp->w_height + height_inc;
331
332 if (height < 1)
333 height = 1;
334 wp->w_minheight = height;
335 wp->w_maxheight = height;
336 drag_start_row = mouse_row;
337 }
338
339 popup_adjust_position(wp);
340 return;
341 }
342
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000343 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200344 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200345 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
346 if (wp->w_wantline < 1)
347 wp->w_wantline = 1;
348 if (wp->w_wantline > Rows)
349 wp->w_wantline = Rows;
350 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
351 if (wp->w_wantcol < 1)
352 wp->w_wantcol = 1;
353 if (wp->w_wantcol > Columns)
354 wp->w_wantcol = Columns;
355
356 popup_adjust_position(wp);
357}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200358
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200359/*
360 * Set w_firstline to match the current "wp->w_topline".
361 */
362 void
363popup_set_firstline(win_T *wp)
364{
365 int height = wp->w_height;
366
367 wp->w_firstline = wp->w_topline;
368 popup_adjust_position(wp);
369
370 // we don't want the popup to get smaller, decrement the first line
371 // until it doesn't
372 while (wp->w_firstline > 1 && wp->w_height < height)
373 {
374 --wp->w_firstline;
375 popup_adjust_position(wp);
376 }
377}
378
379/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200380 * Return TRUE if the position is in the popup window scrollbar.
381 */
382 int
383popup_is_in_scrollbar(win_T *wp, int row, int col)
384{
385 return wp->w_has_scrollbar
386 && row >= wp->w_popup_border[0]
387 && row < popup_height(wp) - wp->w_popup_border[2]
388 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
389}
390
391
392/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200393 * Handle a click in a popup window, if it is in the scrollbar.
394 */
395 void
396popup_handle_scrollbar_click(win_T *wp, int row, int col)
397{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000398 if (!popup_is_in_scrollbar(wp, row, col))
399 return;
Bram Moolenaard28950f2022-05-29 14:13:04 +0100400
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000401 int height = popup_height(wp);
402 int new_topline = wp->w_topline;
403
404 if (row >= height / 2)
405 {
406 // Click in lower half, scroll down.
407 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
408 ++new_topline;
409 }
410 else if (wp->w_topline > 1)
411 // click on upper half, scroll up.
412 --new_topline;
413
414 if (new_topline == wp->w_topline)
415 return;
416
417 set_topline(wp, new_topline);
418 if (wp == curwin)
419 {
420 if (wp->w_cursor.lnum < wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200421 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000422 wp->w_cursor.lnum = wp->w_topline;
423 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200424 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000425 else if (wp->w_cursor.lnum >= wp->w_botline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200426 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000427 wp->w_cursor.lnum = wp->w_botline - 1;
428 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200429 }
430 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000431 popup_set_firstline(wp);
432 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200433}
434
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200435#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100436/*
437 * Add a timer to "wp" with "time".
438 * If "close" is true use popup_close(), otherwise popup_hide().
439 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200440 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100441popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200442{
443 char_u cbbuf[50];
444 char_u *ptr = cbbuf;
445 typval_T tv;
446
447 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100448 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
449 wp->w_id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000450 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) != OK)
451 return;
452
453 wp->w_popup_timer = create_timer(time, 0);
454 callback_T cb = get_callback(&tv);
455 if (cb.cb_name != NULL && !cb.cb_free_name)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200456 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000457 cb.cb_name = vim_strsave(cb.cb_name);
458 cb.cb_free_name = TRUE;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200459 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000460 wp->w_popup_timer->tr_callback = cb;
461 clear_tv(&tv);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200462}
463#endif
464
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100465 static poppos_T
466get_pos_entry(dict_T *d, int give_error)
467{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100468 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100469 int nr;
470
471 if (str == NULL)
472 return POPPOS_NONE;
473
K.Takataeeec2542021-06-02 13:28:16 +0200474 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100475 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
476 return poppos_entries[nr].pp_val;
477
478 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000479 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100480 return POPPOS_NONE;
481}
482
Bram Moolenaar17627312019-06-02 19:53:44 +0200483/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200484 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200485 */
486 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200487apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200488{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200489 int nr;
490 char_u *str;
491 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200492
Bram Moolenaard61efa52022-07-23 09:52:04 +0100493 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200494 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200496 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100497 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200498 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200500 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200501
502 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200503 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200504 wp->w_wantline = nr;
505 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200506 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200507 wp->w_wantcol = nr;
508
Bram Moolenaar55881332020-08-18 13:04:15 +0200509
Bram Moolenaard61efa52022-07-23 09:52:04 +0100510 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200511 if (nr != -1)
512 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200513
Bram Moolenaar12034e22019-08-25 22:25:02 +0200514 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100515 poppos_T ppt = get_pos_entry(d, TRUE);
516
517 if (ppt != POPPOS_NONE)
518 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200519 }
520
Bram Moolenaard61efa52022-07-23 09:52:04 +0100521 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200522 if (str != NULL)
523 {
524 wp->w_popup_prop_type = 0;
525 if (*str != NUL)
526 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100527 wp->w_popup_prop_win = curwin;
528 di = dict_find(d, (char_u *)"textpropwin", -1);
529 if (di != NULL)
530 {
531 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100532 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100533 wp->w_popup_prop_win = curwin;
534 }
535
536 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200537 if (nr <= 0)
538 nr = find_prop_type_id(str, NULL);
539 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000540 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200541 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200542 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200543 }
544 }
545
546 di = dict_find(d, (char_u *)"textpropid", -1);
547 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100548 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200549}
550
Bram Moolenaarb0992022020-01-30 14:55:42 +0100551/*
552 * Handle "moved" and "mousemoved" arguments.
553 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200554 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200555handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
556{
557 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
558 {
559 char_u *s = di->di_tv.vval.v_string;
560 int flags = 0;
561
562 if (STRCMP(s, "word") == 0)
563 flags = FIND_IDENT | FIND_STRING;
564 else if (STRCMP(s, "WORD") == 0)
565 flags = FIND_STRING;
566 else if (STRCMP(s, "expr") == 0)
567 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
568 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000569 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200570 if (flags != 0)
571 {
572 if (mousemoved)
573 set_mousemoved_columns(wp, flags);
574 else
575 set_moved_columns(wp, flags);
576 }
577 }
578 else if (di->di_tv.v_type == VAR_LIST
579 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200580 && (di->di_tv.vval.v_list->lv_len == 2
581 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200582 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200583 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100584 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200585 int mincol;
586 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200587
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200588 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100589 li = l->lv_first;
590 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200591 {
592 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
593
594 // Three numbers, might be from popup_getoptions().
595 if (mousemoved)
596 wp->w_popup_mouse_row = nr;
597 else
598 wp->w_popup_lnum = nr;
599 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100600 if (nr == 0)
601 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200602 }
603
604 mincol = tv_get_number(&li->li_tv);
605 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200606 if (mousemoved)
607 {
608 wp->w_popup_mouse_mincol = mincol;
609 wp->w_popup_mouse_maxcol = maxcol;
610 }
611 else
612 {
613 wp->w_popup_mincol = mincol;
614 wp->w_popup_maxcol = maxcol;
615 }
616 }
617 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000618 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200619}
620
621 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200622check_highlight(dict_T *dict, char *name, char_u **pval)
623{
624 dictitem_T *di;
625 char_u *str;
626
627 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000628 if (di == NULL)
629 return;
630
631 if (di->di_tv.v_type != VAR_STRING)
632 semsg(_(e_invalid_value_for_argument_str), name);
633 else
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200634 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000635 str = tv_get_string(&di->di_tv);
636 if (*str != NUL)
637 *pval = vim_strsave(str);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200638 }
639}
640
Bram Moolenaarae943152019-06-16 22:54:14 +0200641/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200642 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200643 */
644 static void
645popup_show_curline(win_T *wp)
646{
647 if (wp->w_cursor.lnum < wp->w_topline)
648 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200649 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200650 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200651 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200652 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200653 if (wp->w_topline < 1)
654 wp->w_topline = 1;
655 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
656 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200657 while (wp->w_topline < wp->w_cursor.lnum
658 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
Luuk van Baal32d701f2024-04-28 16:24:02 +0200659 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum,
660 wp->w_height + 1) > wp->w_height)
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200661 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200662 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200663
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200664 // Don't let "firstline" cause a scroll.
665 if (wp->w_firstline > 0)
666 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200667}
668
669/*
670 * Get the sign group name for window "wp".
671 * Returns a pointer to a static buffer, overwritten on the next call.
672 */
673 static char_u *
674popup_get_sign_name(win_T *wp)
675{
676 static char buf[30];
677
678 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
679 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200680}
681
682/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200683 * Highlight the line with the cursor.
684 * Also scrolls the text to put the cursor line in view.
685 */
686 static void
687popup_highlight_curline(win_T *wp)
688{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200689 int sign_id = 0;
690 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200691
Bram Moolenaar72570732019-11-30 14:21:53 +0100692 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200693
694 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
695 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200696 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200697
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200698 if (!sign_exists_by_name(sign_name))
699 {
700 char *linehl = "PopupSelected";
LemonBoyb975ddf2024-07-06 18:04:09 +0200701 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL,
702 NULL, SIGN_DEF_PRIO);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200703 }
704
Bram Moolenaar72570732019-11-30 14:21:53 +0100705 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200706 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100707 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200708 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200709 else
710 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200711 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200712}
713
714/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200715 * Shared between popup_create() and f_popup_setoptions().
716 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200717 static int
Bram Moolenaarae943152019-06-16 22:54:14 +0200718apply_general_options(win_T *wp, dict_T *dict)
719{
720 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200721 int nr;
722 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200723
Bram Moolenaarae943152019-06-16 22:54:14 +0200724 // TODO: flip
725
726 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200727 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200728 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100729 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200730 if (wp->w_firstline < 0)
731 wp->w_firstline = -1;
732 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200733
Bram Moolenaard61efa52022-07-23 09:52:04 +0100734 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200735 if (nr != -1)
736 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200737
Bram Moolenaard61efa52022-07-23 09:52:04 +0100738 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200739 if (str != NULL)
740 {
741 vim_free(wp->w_popup_title);
742 wp->w_popup_title = vim_strsave(str);
743 }
744
Bram Moolenaard61efa52022-07-23 09:52:04 +0100745 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200746 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200747 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200748
Bram Moolenaard61efa52022-07-23 09:52:04 +0100749 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200750 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200751 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200752 if (nr)
753 wp->w_popup_flags |= POPF_DRAG;
754 else
755 wp->w_popup_flags &= ~POPF_DRAG;
756 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100757 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000758 if (nr != -1)
759 {
760 if (nr)
761 wp->w_popup_flags |= POPF_DRAGALL;
762 else
763 wp->w_popup_flags &= ~POPF_DRAGALL;
764 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200765
Bram Moolenaard61efa52022-07-23 09:52:04 +0100766 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200767 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100768 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100769 if (nr)
770 wp->w_popup_flags |= POPF_POSINVERT;
771 else
772 wp->w_popup_flags &= ~POPF_POSINVERT;
773 }
774
Bram Moolenaard61efa52022-07-23 09:52:04 +0100775 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200776 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200777 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200778 if (nr)
779 wp->w_popup_flags |= POPF_RESIZE;
780 else
781 wp->w_popup_flags &= ~POPF_RESIZE;
782 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200783
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200784 di = dict_find(dict, (char_u *)"close", -1);
785 if (di != NULL)
786 {
787 int ok = TRUE;
788
789 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
790 {
791 char_u *s = di->di_tv.vval.v_string;
792
793 if (STRCMP(s, "none") == 0)
794 wp->w_popup_close = POPCLOSE_NONE;
795 else if (STRCMP(s, "button") == 0)
796 wp->w_popup_close = POPCLOSE_BUTTON;
797 else if (STRCMP(s, "click") == 0)
798 wp->w_popup_close = POPCLOSE_CLICK;
799 else
800 ok = FALSE;
801 }
802 else
803 ok = FALSE;
804 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000805 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200806 }
807
Bram Moolenaard61efa52022-07-23 09:52:04 +0100808 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200809 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000810 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200811 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
812 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000813#ifdef FEAT_TERMINAL
814 term_update_wincolor(wp);
815#endif
816 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200817
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200818 if (set_padding_border(dict, wp->w_popup_padding, "padding", 999) == FAIL ||
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200819 set_padding_border(dict, wp->w_popup_border, "border", 1) == FAIL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200820 return FAIL;
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200821
Bram Moolenaar790498b2019-06-01 22:15:29 +0200822 di = dict_find(dict, (char_u *)"borderhighlight", -1);
823 if (di != NULL)
824 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200825 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200826 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000827 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200828 return FAIL;
829 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200830 else
831 {
832 list_T *list = di->di_tv.vval.v_list;
833 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200834 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200835
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200836 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200837 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
838 ++i, li = li->li_next)
839 {
840 str = tv_get_string(&li->li_tv);
841 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100842 {
843 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200844 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100845 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200846 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200847 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
848 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100849 {
850 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200851 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200852 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100853 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200854 }
855 }
856
Bram Moolenaar790498b2019-06-01 22:15:29 +0200857 di = dict_find(dict, (char_u *)"borderchars", -1);
858 if (di != NULL)
859 {
860 if (di->di_tv.v_type != VAR_LIST)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200861 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000862 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200863 return FAIL;
864 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200865 else
866 {
867 list_T *list = di->di_tv.vval.v_list;
868 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200869 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200870
871 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100872 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200873 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200874 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
875 ++i, li = li->li_next)
876 {
877 str = tv_get_string(&li->li_tv);
878 if (*str != NUL)
879 wp->w_border_char[i] = mb_ptr2char(str);
880 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200881 if (list->lv_len == 1)
882 for (i = 1; i < 8; ++i)
883 wp->w_border_char[i] = wp->w_border_char[0];
884 if (list->lv_len == 2)
885 {
886 for (i = 4; i < 8; ++i)
887 wp->w_border_char[i] = wp->w_border_char[1];
888 for (i = 1; i < 4; ++i)
889 wp->w_border_char[i] = wp->w_border_char[0];
890 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200891 }
892 }
893 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200894
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200895 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
896 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
897
Bram Moolenaarae943152019-06-16 22:54:14 +0200898 di = dict_find(dict, (char_u *)"zindex", -1);
899 if (di != NULL)
900 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100901 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200902 if (wp->w_zindex < 1)
903 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
904 if (wp->w_zindex > 32000)
905 wp->w_zindex = 32000;
906 }
907
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200908 di = dict_find(dict, (char_u *)"mask", -1);
909 if (di != NULL)
910 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200911 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200912
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200913 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200914 {
915 listitem_T *li;
916
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200917 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200918 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200919 {
920 if (li->li_tv.v_type != VAR_LIST
921 || li->li_tv.vval.v_list == NULL
922 || li->li_tv.vval.v_list->lv_len != 4)
923 {
924 ok = FALSE;
925 break;
926 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100927 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200928 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200929 }
930 }
931 if (ok)
932 {
933 wp->w_popup_mask = di->di_tv.vval.v_list;
934 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200935 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200936 }
937 else
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200938 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000939 semsg(_(e_invalid_value_for_argument_str), "mask");
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200940 return FAIL;
941 }
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200942 }
943
Bram Moolenaarae943152019-06-16 22:54:14 +0200944#if defined(FEAT_TIMERS)
945 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100946 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200947 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100948 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200949#endif
950
Bram Moolenaar3397f742019-06-02 18:40:06 +0200951 di = dict_find(dict, (char_u *)"moved", -1);
952 if (di != NULL)
953 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200954 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200955 handle_moved_argument(wp, di, FALSE);
956 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200957
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200958 di = dict_find(dict, (char_u *)"mousemoved", -1);
959 if (di != NULL)
960 {
961 set_mousemoved_values(wp);
962 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200963 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200964
Bram Moolenaard61efa52022-07-23 09:52:04 +0100965 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100966 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200967 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100968 if (nr != 0)
969 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200970 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100971 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200972 }
973
Bram Moolenaarae943152019-06-16 22:54:14 +0200974 di = dict_find(dict, (char_u *)"filter", -1);
975 if (di != NULL)
976 {
977 callback_T callback = get_callback(&di->di_tv);
978
979 if (callback.cb_name != NULL)
980 {
981 free_callback(&wp->w_filter_cb);
982 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000983 if (callback.cb_free_name)
984 vim_free(callback.cb_name);
Bram Moolenaarae943152019-06-16 22:54:14 +0200985 }
986 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100987 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200988 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200989 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200990 if (nr)
991 wp->w_popup_flags |= POPF_MAPPING;
992 else
993 wp->w_popup_flags &= ~POPF_MAPPING;
994 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200995
Bram Moolenaard61efa52022-07-23 09:52:04 +0100996 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200997 if (str != NULL)
998 {
999 if (STRCMP(str, "a") == 0)
1000 wp->w_filter_mode = MODE_ALL;
1001 else
1002 wp->w_filter_mode = mode_str2flags(str);
1003 }
1004
Bram Moolenaarae943152019-06-16 22:54:14 +02001005 di = dict_find(dict, (char_u *)"callback", -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001006 if (di == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001007 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001008
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001009 callback_T callback = get_callback(&di->di_tv);
1010 if (callback.cb_name == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001011 return OK;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001012
1013 free_callback(&wp->w_close_cb);
1014 set_callback(&wp->w_close_cb, &callback);
1015 if (callback.cb_free_name)
1016 vim_free(callback.cb_name);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001017
1018 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001019}
1020
1021/*
1022 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001023 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +02001024 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001025 static int
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001026apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001027{
1028 int nr;
1029
1030 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001031
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001032 if (create)
1033 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001034 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1035
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001036 if (apply_general_options(wp, dict) == FAIL)
1037 return FAIL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001038
Bram Moolenaard61efa52022-07-23 09:52:04 +01001039 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001040 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001041 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001042
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001043 // when "firstline" and "cursorline" are both set and the cursor would be
1044 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001045 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1046 {
1047 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1048 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001049 else if (wp->w_cursor.lnum < wp->w_firstline
1050 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001051 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001052 wp->w_topline = wp->w_firstline;
1053 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001054 }
1055
Bram Moolenaar33796b32019-06-08 16:01:13 +02001056 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001057 popup_highlight_curline(wp);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001058
1059 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001060}
1061
1062/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001063 * Add lines to the popup from a list of strings.
1064 */
1065 static void
1066add_popup_strings(buf_T *buf, list_T *l)
1067{
1068 listitem_T *li;
1069 linenr_T lnum = 0;
1070 char_u *p;
1071
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001072 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001073 if (li->li_tv.v_type == VAR_STRING)
1074 {
1075 p = li->li_tv.vval.v_string;
1076 ml_append_buf(buf, lnum++,
1077 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1078 }
1079}
1080
1081/*
1082 * Add lines to the popup from a list of dictionaries.
1083 */
1084 static void
1085add_popup_dicts(buf_T *buf, list_T *l)
1086{
1087 listitem_T *li;
1088 listitem_T *pli;
1089 linenr_T lnum = 0;
1090 char_u *p;
1091 dict_T *dict;
1092
1093 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001094 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001095 {
1096 if (li->li_tv.v_type != VAR_DICT)
1097 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001098 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001099 return;
1100 }
1101 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001102 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001103 ml_append_buf(buf, lnum++,
1104 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1105 }
1106
1107 // add the text properties
1108 lnum = 1;
1109 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1110 {
1111 dictitem_T *di;
1112 list_T *plist;
1113
1114 dict = li->li_tv.vval.v_dict;
1115 di = dict_find(dict, (char_u *)"props", -1);
1116 if (di != NULL)
1117 {
1118 if (di->di_tv.v_type != VAR_LIST)
1119 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001120 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001121 return;
1122 }
1123 plist = di->di_tv.vval.v_list;
1124 if (plist != NULL)
1125 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001126 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001127 {
1128 if (pli->li_tv.v_type != VAR_DICT)
1129 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001130 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001131 return;
1132 }
1133 dict = pli->li_tv.vval.v_dict;
1134 if (dict != NULL)
1135 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001136 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001137
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001138 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001139 }
1140 }
1141 }
1142 }
1143 }
1144}
1145
1146/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001147 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1148 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001149 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001150popup_top_extra(win_T *wp)
1151{
1152 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1153
1154 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1155 return 1;
1156 return extra;
1157}
1158
1159/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001160 * Get the padding plus border at the left.
1161 */
1162 int
1163popup_left_extra(win_T *wp)
1164{
1165 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1166}
1167
1168/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001169 * Return the height of popup window "wp", including border and padding.
1170 */
1171 int
1172popup_height(win_T *wp)
1173{
1174 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001175 + popup_top_extra(wp)
1176 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001177}
1178
1179/*
1180 * Return the width of popup window "wp", including border, padding and
1181 * scrollbar.
1182 */
1183 int
1184popup_width(win_T *wp)
1185{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001186 // w_leftcol is how many columns of the core are left of the screen
1187 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001188 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001189 + popup_extra_width(wp)
1190 + wp->w_popup_rightoff;
1191}
1192
1193/*
1194 * Return the extra width of popup window "wp": border, padding and scrollbar.
1195 */
1196 int
1197popup_extra_width(win_T *wp)
1198{
1199 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1200 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1201 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001202}
1203
1204/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001205 * Adjust the position and size of the popup to fit on the screen.
1206 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001207 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001208popup_adjust_position(win_T *wp)
1209{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001210 linenr_T lnum;
1211 int wrapped = 0;
1212 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001213 int maxwidth_no_scrollbar;
1214 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001215 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001216 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001217 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001218 int center_vert = FALSE;
1219 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001220 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001221 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001222 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1223 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1224 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1225 int extra_height = top_extra + bot_extra;
1226 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001227 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001228 int org_winrow = wp->w_winrow;
1229 int org_wincol = wp->w_wincol;
1230 int org_width = wp->w_width;
1231 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001232 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001233 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001234 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001235 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001236 int wantline = wp->w_wantline; // adjusted for textprop
1237 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001238 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001239 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001240
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001241 wp->w_winrow = 0;
1242 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001243 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001244 wp->w_popup_leftoff = 0;
1245 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001246
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001247 // May need to update the "cursorline" highlighting, which may also change
1248 // "topline"
1249 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1250 popup_highlight_curline(wp);
1251
Bram Moolenaar12034e22019-08-25 22:25:02 +02001252 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1253 {
1254 win_T *prop_win = wp->w_popup_prop_win;
1255 textprop_T prop;
1256 linenr_T prop_lnum;
1257 pos_T pos;
1258 int screen_row;
1259 int screen_scol;
1260 int screen_ccol;
1261 int screen_ecol;
1262
1263 // Popup window is positioned relative to a text property.
1264 if (find_visible_prop(prop_win,
1265 wp->w_popup_prop_type, wp->w_popup_prop_id,
1266 &prop, &prop_lnum) == FAIL)
1267 {
1268 // Text property is no longer visible, hide the popup.
1269 // Unhiding the popup is done in check_popup_unhidden().
1270 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1271 {
1272 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001273 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001274 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001275 }
1276 return;
1277 }
1278
1279 // Compute the desired position from the position of the text
1280 // property. Use "wantline" and "wantcol" as offsets.
1281 pos.lnum = prop_lnum;
1282 pos.col = prop.tp_col;
1283 if (wp->w_popup_pos == POPPOS_TOPLEFT
1284 || wp->w_popup_pos == POPPOS_BOTLEFT)
1285 pos.col += prop.tp_len - 1;
1286 textpos2screenpos(prop_win, &pos, &screen_row,
1287 &screen_scol, &screen_ccol, &screen_ecol);
1288
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001289 if (screen_scol == 0)
1290 {
1291 // position is off screen, make the width zero to hide it.
1292 wp->w_width = 0;
1293 return;
1294 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001295 if (wp->w_popup_pos == POPPOS_TOPLEFT
1296 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1297 // below the text
1298 wantline = screen_row + wantline + 1;
1299 else
1300 // above the text
1301 wantline = screen_row + wantline - 1;
1302 center_vert = FALSE;
1303 if (wp->w_popup_pos == POPPOS_TOPLEFT
1304 || wp->w_popup_pos == POPPOS_BOTLEFT)
1305 // right of the text
1306 wantcol = screen_ecol + wantcol;
1307 else
1308 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001309 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001310 use_wantcol = TRUE;
1311 }
1312 else
1313 {
1314 // If no line was specified default to vertical centering.
1315 if (wantline == 0)
1316 center_vert = TRUE;
1317 else if (wantline < 0)
1318 // If "wantline" is negative it actually means zero.
1319 wantline = 0;
1320 if (wantcol < 0)
1321 // If "wantcol" is negative it actually means zero.
1322 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001323 }
1324
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001325 if (wp->w_popup_pos == POPPOS_CENTER)
1326 {
1327 // center after computing the size
1328 center_vert = TRUE;
1329 center_hor = TRUE;
1330 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001331 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001332 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001333 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1334 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001335 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001336 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001337 if (wp->w_winrow >= Rows)
1338 wp->w_winrow = Rows - 1;
1339 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001340 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001341 {
1342 // Assume that each buffer line takes one screen line, and one line
1343 // for the top border. First make sure cmdline_row is valid,
1344 // calling update_screen() will set it only later.
1345 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001346 wp->w_winrow = MAX(cmdline_row
1347 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001348 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001349
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001350 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001351 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001352 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1353 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001354 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001355 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001356 // Need to see at least one character after the decoration.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001357 if (wp->w_wincol > firstwin->w_wincol + topframe->fr_width - left_extra - 1)
1358 wp->w_wincol = firstwin->w_wincol + topframe->fr_width - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001359 }
1360 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001361
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001362 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001363 // When left aligned use the space available, but shift to the left when we
1364 // hit the right of the screen.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001365 maxspace = firstwin->w_wincol + topframe->fr_width - wp->w_wincol - left_extra;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001366 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001367 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001368 {
1369 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001370 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001371 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001372
1373 if (wp->w_p_nu || wp->w_p_rnu)
1374 margin_width = number_width(wp) + 1;
1375#ifdef FEAT_FOLDING
1376 margin_width += wp->w_p_fdc;
1377#endif
1378#ifdef FEAT_SIGNS
1379 if (signcolumn_on(wp))
1380 margin_width += 2;
1381#endif
1382 if (margin_width >= maxwidth)
1383 margin_width = maxwidth - 1;
1384
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001385 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001386 minheight = wp->w_minheight;
1387#ifdef FEAT_TERMINAL
1388 // A terminal popup initially does not have content, use a default minimal
1389 // width of 20 characters and height of 5 lines.
1390 if (wp->w_buffer->b_term != NULL)
1391 {
1392 if (minwidth == 0)
1393 minwidth = 20;
1394 if (minheight == 0)
1395 minheight = 5;
1396 }
1397#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001398
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001399 if (wp->w_maxheight > 0)
1400 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001401 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1402 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001403
Bram Moolenaar8d241042019-06-12 23:40:01 +02001404 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001405 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001406 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001407 if (wp->w_topline < 1)
1408 wp->w_topline = 1;
1409 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001410 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1411
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001412 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001413 // Use a minimum width of one, so that something shows when there is no
1414 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001415 // When "firstline" is -1 then start with the last buffer line and go
1416 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001417 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001418 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001419 if (wp->w_firstline < 0)
1420 lnum = wp->w_buffer->b_ml.ml_line_count;
1421 else
1422 lnum = wp->w_topline;
1423 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001424 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001425 int len;
1426 int w_width = wp->w_width;
1427
1428 // Count Tabs for what they are worth and compute the length based on
1429 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001430 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001431 if (wp->w_width < maxwidth)
1432 wp->w_width = maxwidth;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001433 len = linetabsize(wp, lnum);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001434 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001435
Boris Staletic13c11532024-12-19 20:22:19 +01001436 if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001437 && allow_adjust_left
1438 && (wp->w_popup_pos == POPPOS_TOPLEFT
1439 || wp->w_popup_pos == POPPOS_BOTLEFT))
1440 {
1441 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001442 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001443
Bram Moolenaar51c31312019-06-15 22:27:23 +02001444 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001445 {
1446 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001447
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001448 shift_by -= truncate_shift;
1449 }
1450
1451 wp->w_wincol -= shift_by;
1452 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001453 wp->w_width = maxwidth;
1454 }
Boris Staletic13c11532024-12-19 20:22:19 +01001455 if (wp->w_p_wrap)
1456 {
1457 while (len + margin_width > maxwidth)
1458 {
1459 ++wrapped;
1460 len -= maxwidth - margin_width;
1461 wp->w_width = maxwidth;
1462 used_maxwidth = TRUE;
1463 }
1464 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001465 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001466 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001467 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001468 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1469 wp->w_width = wp->w_maxwidth;
1470 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001471
1472 if (wp->w_firstline < 0)
1473 --lnum;
1474 else
1475 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001476
1477 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001478 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001479 && (wp->w_firstline >= 0
1480 ? lnum - wp->w_topline
1481 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001482 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001483 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001484 }
1485
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001486 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001487 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001488
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001489 wp->w_has_scrollbar = wp->w_want_scrollbar
1490 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001491#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001492 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1493 // Terminal window with running job never has a scrollbar, adjusts to
1494 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001495 wp->w_has_scrollbar = FALSE;
1496#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001497 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001498 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001499 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001500 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001501 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001502 // make space for the scrollbar if needed, when lines wrap and when
1503 // applying minwidth
1504 if (maxwidth + right_extra >= maxspace
1505 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1506 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001507 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001508
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001509 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1510 {
1511 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1512
1513 if (minwidth < title_len)
1514 minwidth = title_len;
1515 }
1516
1517 if (minwidth > 0 && wp->w_width < minwidth)
1518 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001519 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001520 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001521 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001522 // some columns cut off on the right
1523 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001524
1525 // If the window doesn't fit because 'minwidth' is set then the
1526 // scrollbar is at the far right of the screen, use the size without
1527 // the scrollbar.
1528 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1529 {
1530 int off = wp->w_width - maxwidth;
1531
1532 if (off > right_extra)
1533 extra_width -= right_extra;
1534 else
1535 extra_width -= off;
1536 wp->w_width = maxwidth_no_scrollbar;
1537 }
1538 else
1539 {
1540 wp->w_width = maxwidth;
1541
1542 // when adding a scrollbar below need to adjust the width
1543 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1544 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001545 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001546 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001547 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001548 wp->w_wincol = (firstwin->w_wincol + topframe->fr_width - wp->w_width - extra_width) / 2;
Bram Moolenaara730e552019-06-16 19:05:31 +02001549 if (wp->w_wincol < 0)
1550 wp->w_wincol = 0;
1551 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001552 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1553 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1554 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001555 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001556
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001557 // Right aligned: move to the right if needed.
1558 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001559 if (leftoff >= 0)
1560 wp->w_wincol = leftoff;
1561 else if (wp->w_popup_fixed)
1562 {
1563 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001564 // columns when displaying the window, minus left border and
1565 // padding.
1566 if (-leftoff > left_extra)
1567 wp->w_leftcol = -leftoff - left_extra;
1568 wp->w_width -= wp->w_leftcol;
1569 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001570 if (wp->w_width < 0)
1571 wp->w_width = 0;
1572 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001573 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001574
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001575 if (wp->w_p_wrap || (!wp->w_popup_fixed
1576 && (wp->w_popup_pos == POPPOS_TOPLEFT
1577 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001578 {
1579 int want_col = 0;
1580
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001581 // try to show the right border and any scrollbar
1582 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001583 if (want_col > 0 && wp->w_wincol > 0
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001584 && wp->w_wincol + want_col >= firstwin->w_wincol + topframe->fr_width)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001585 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001586 wp->w_wincol = firstwin->w_wincol + topframe->fr_width - want_col;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001587 if (wp->w_wincol < 0)
1588 wp->w_wincol = 0;
1589 }
1590 }
1591
Bram Moolenaar8d241042019-06-12 23:40:01 +02001592 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1593 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001594 if (minheight > 0 && wp->w_height < minheight)
1595 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001596 if (maxheight > 0 && wp->w_height > maxheight)
1597 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001598 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001599 if (wp->w_height > Rows - wp->w_winrow)
1600 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001601
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001602 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001603 {
1604 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1605 if (wp->w_winrow < 0)
1606 wp->w_winrow = 0;
1607 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001608 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001609 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001610 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001611 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001612 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001613 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001614 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1615 {
1616 // Bottom aligned but does not fit, and less space on the other
1617 // side or "posinvert" is off: reduce height.
1618 wp->w_winrow = 0;
1619 wp->w_height = wantline - extra_height;
1620 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001621 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001622 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001623 // Not enough space and more space on the other side: make top
1624 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001625 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001626 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001627 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001628 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001629 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1630 || wp->w_popup_pos == POPPOS_TOPLEFT)
1631 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001632 if (wp != popup_dragwin
1633 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001634 && wantline * 2 > Rows
1635 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001636 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001637 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001638 // make bottom aligned and recompute the height
1639 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001640 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001641 if (wp->w_winrow < 0)
1642 {
1643 wp->w_height += wp->w_winrow;
1644 wp->w_winrow = 0;
1645 }
1646 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001647 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001648 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001649 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001650 adjust_height_for_top_aligned = TRUE;
1651 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001652 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001653
1654 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1655 && wp->w_winrow + wp->w_height + extra_height > Rows)
1656 {
1657 // Bottom of the popup goes below the last line, reduce the height and
1658 // add a scrollbar.
1659 wp->w_height = Rows - wp->w_winrow - extra_height;
1660#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001661 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001662#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001663 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001664 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001665 if (width_with_scrollbar > 0)
1666 wp->w_width = width_with_scrollbar;
1667 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001668 }
1669
1670 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001671 if (wp->w_winrow >= Rows)
1672 wp->w_winrow = Rows - 1;
1673 else if (wp->w_winrow < 0)
1674 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001675
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001676 if (wp->w_wincol + wp->w_width > firstwin->w_wincol + topframe->fr_width)
1677 wp->w_wincol = firstwin->w_wincol + topframe->fr_width - wp->w_width;
1678 else if (wp->w_wincol < firstwin->w_wincol)
1679 wp->w_wincol = firstwin->w_wincol;
1680 if (wp->w_wincol < 0)
1681 wp->w_wincol = 0;
1682
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001683 if (wp->w_height != org_height)
1684 win_comp_scroll(wp);
1685
Bram Moolenaar17146962019-05-30 00:12:11 +02001686 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001687 if (win_valid(wp->w_popup_prop_win))
1688 {
1689 wp->w_popup_prop_changedtick =
1690 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1691 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1692 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001693
1694 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001695 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001696 if (org_winrow != wp->w_winrow
1697 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001698 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001699 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001700 || org_width != wp->w_width
1701 || org_height != wp->w_height)
1702 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001703 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001704 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1705 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001706 popup_mask_refresh = TRUE;
1707 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001708}
1709
Bram Moolenaar17627312019-06-02 19:53:44 +02001710typedef enum
1711{
1712 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001713 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001714 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001715 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001716 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001717 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001718 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001719 TYPE_PREVIEW, // preview window
1720 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001721} create_type_T;
1722
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001723/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001724 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1725 */
1726 static int
1727popup_is_notification(create_type_T type)
1728{
1729 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1730}
1731
1732/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001733 * Make "buf" empty and set the contents to "text".
1734 * Used by popup_create() and popup_settext().
1735 */
1736 static void
1737popup_set_buffer_text(buf_T *buf, typval_T text)
1738{
1739 int lnum;
1740
1741 // Clear the buffer, then replace the lines.
1742 curbuf = buf;
1743 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001744 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001745 curbuf = curwin->w_buffer;
1746
1747 // Add text to the buffer.
1748 if (text.v_type == VAR_STRING)
1749 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001750 char_u *s = text.vval.v_string;
1751
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001752 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001753 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001754 }
1755 else
1756 {
1757 list_T *l = text.vval.v_list;
1758
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001759 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001760 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001761 if (l->lv_first == &range_list_item)
1762 emsg(_(e_using_number_as_string));
1763 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001764 // list of strings
1765 add_popup_strings(buf, l);
1766 else
1767 // list of dictionaries
1768 add_popup_dicts(buf, l);
1769 }
1770 }
1771
1772 // delete the line that was in the empty buffer
1773 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001774 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001775 curbuf = curwin->w_buffer;
1776}
1777
1778/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001779 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1780 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001781 * Return FAIL if the parsing fails.
1782 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001783 static int
1784parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001785{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001786 char_u *p =
1787#ifdef FEAT_QUICKFIX
1788 !is_preview ? p_cpp :
1789#endif
1790 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001791
Bram Moolenaar258cef52019-08-21 17:29:29 +02001792 if (wp != NULL)
1793 wp->w_popup_flags &= ~POPF_INFO_MENU;
1794
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001795 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001796 {
1797 char_u *e, *dig;
1798 char_u *s = p;
1799 int x;
1800
1801 e = vim_strchr(p, ':');
1802 if (e == NULL || e[1] == NUL)
1803 return FAIL;
1804
1805 p = vim_strchr(e, ',');
1806 if (p == NULL)
1807 p = e + STRLEN(e);
1808 dig = e + 1;
1809 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001810
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001811 // Note: Keep this in sync with p_popup_option_values.
Bram Moolenaar79648732019-07-18 21:43:07 +02001812 if (STRNCMP(s, "height:", 7) == 0)
1813 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001814 if (dig != p)
1815 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001816 if (wp != NULL)
1817 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001818 if (is_preview)
1819 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001820 wp->w_maxheight = x;
1821 }
1822 }
1823 else if (STRNCMP(s, "width:", 6) == 0)
1824 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001825 if (dig != p)
1826 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001827 if (wp != NULL)
1828 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001829 if (is_preview)
1830 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001831 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001832 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001833 }
1834 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001835 else if (STRNCMP(s, "highlight:", 10) == 0)
1836 {
1837 if (wp != NULL)
1838 {
1839 int c = *p;
1840
1841 *p = NUL;
1842 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1843 s + 10, OPT_FREE|OPT_LOCAL, 0);
1844 *p = c;
1845 }
1846 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001847 else if (STRNCMP(s, "border:", 7) == 0)
1848 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001849 // Note: Keep this in sync with p_popup_option_border_values.
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001850 char_u *arg = s + 7;
1851 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1852 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1853 int i;
1854
1855 if (!on && !off)
1856 return FAIL;
1857 if (wp != NULL)
1858 {
1859 for (i = 0; i < 4; ++i)
1860 wp->w_popup_border[i] = on ? 1 : 0;
1861 if (off)
1862 // only show the X for close when there is a border
1863 wp->w_popup_close = POPCLOSE_NONE;
1864 }
1865 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001866 else if (STRNCMP(s, "align:", 6) == 0)
1867 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001868 // Note: Keep this in sync with p_popup_option_align_values.
Bram Moolenaar258cef52019-08-21 17:29:29 +02001869 char_u *arg = s + 6;
1870 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1871 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1872
1873 if (!menu && !item)
1874 return FAIL;
1875 if (wp != NULL && menu)
1876 wp->w_popup_flags |= POPF_INFO_MENU;
1877 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001878 else
1879 return FAIL;
1880 }
1881 return OK;
1882}
1883
1884/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001885 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1886 * is not NULL.
1887 * Return FAIL if the parsing fails.
1888 */
1889 int
1890parse_previewpopup(win_T *wp)
1891{
1892 return parse_popup_option(wp, TRUE);
1893}
1894
1895/*
1896 * Parse the 'completepopup' option and apply the values to window "wp" if it
1897 * is not NULL.
1898 * Return FAIL if the parsing fails.
1899 */
1900 int
1901parse_completepopup(win_T *wp)
1902{
1903 return parse_popup_option(wp, FALSE);
1904}
1905
1906/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001907 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001908 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001909 */
1910 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001911popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001912{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001913 poppos_T ppt = POPPOS_NONE;
1914
1915 if (d != NULL)
1916 ppt = get_pos_entry(d, FALSE);
1917
Bram Moolenaar79648732019-07-18 21:43:07 +02001918 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001919 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001920 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001921 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1922 }
1923 else
1924 {
1925 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1926 if (wp->w_wantline == 0) // cursor in first line
1927 {
1928 wp->w_wantline = 2;
1929 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1930 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1931 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001932 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001933
Bram Moolenaar79648732019-07-18 21:43:07 +02001934 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001935 if (wp->w_wantcol > Columns - width)
1936 {
1937 wp->w_wantcol = Columns - width;
1938 if (wp->w_wantcol < 1)
1939 wp->w_wantcol = 1;
1940 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001941
Bram Moolenaar79648732019-07-18 21:43:07 +02001942 popup_adjust_position(wp);
1943}
1944
1945/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001946 * Set w_wantline and w_wantcol for the a given screen position.
1947 * Caller must take care of running into the window border.
1948 */
1949 void
1950popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1951{
1952 wp->w_wantline = row;
1953 wp->w_wantcol = col;
1954 popup_adjust_position(wp);
1955}
1956
1957/*
1958 * Add a border and lef&right padding.
1959 */
1960 static void
1961add_border_left_right_padding(win_T *wp)
1962{
1963 int i;
1964
1965 for (i = 0; i < 4; ++i)
1966 {
1967 wp->w_popup_border[i] = 1;
1968 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1969 }
1970}
1971
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001972#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001973/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001974 * Return TRUE if there is any popup window with a terminal buffer.
1975 */
1976 static int
1977popup_terminal_exists(void)
1978{
1979 win_T *wp;
1980 tabpage_T *tp;
1981
1982 FOR_ALL_POPUPWINS(wp)
1983 if (wp->w_buffer->b_term != NULL)
1984 return TRUE;
1985 FOR_ALL_TABPAGES(tp)
1986 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1987 if (wp->w_buffer->b_term != NULL)
1988 return TRUE;
1989 return FALSE;
1990}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001991#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001992
1993/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001994 * Mark all popup windows in the current tab and global for redrawing.
1995 */
1996 void
1997popup_redraw_all(void)
1998{
1999 win_T *wp;
2000
2001 FOR_ALL_POPUPWINS(wp)
2002 wp->w_redr_type = UPD_NOT_VALID;
2003 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
2004 wp->w_redr_type = UPD_NOT_VALID;
2005}
2006
2007/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01002008 * Set the color for a notification window.
2009 */
2010 static void
2011popup_update_color(win_T *wp, create_type_T type)
2012{
2013 char *hiname = type == TYPE_MESSAGE_WIN
2014 ? "MessageWindow" : "PopupNotification";
Bram Moolenaar9198de32022-08-27 21:30:03 +01002015 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Yee Cheng Chine700dde2025-02-20 21:58:21 +01002016 (char_u *)hiname,
Bram Moolenaar9198de32022-08-27 21:30:03 +01002017 OPT_FREE|OPT_LOCAL, 0);
2018}
2019
2020/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002021 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002022 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02002023 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002024 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002025 */
Bram Moolenaara730e552019-06-16 19:05:31 +02002026 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02002027popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002028{
Bram Moolenaara3fce622019-06-20 02:31:49 +02002029 win_T *wp;
2030 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002031 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002032 int new_buffer;
2033 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002034 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002035 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002036
Bram Moolenaar79648732019-07-18 21:43:07 +02002037 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002038 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002039 if (in_vim9script()
2040 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2041 || check_for_dict_arg(argvars, 1) == FAIL))
2042 return NULL;
2043
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002044 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002045 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002046 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002047 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002048 if (buf == NULL)
2049 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002050 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002051 return NULL;
2052 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002053#ifdef FEAT_TERMINAL
2054 if (buf->b_term != NULL && popup_terminal_exists())
2055 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002056 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002057 return NULL;
2058 }
2059#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002060 }
2061 else if (!(argvars[0].v_type == VAR_STRING
2062 && argvars[0].vval.v_string != NULL)
2063 && !(argvars[0].v_type == VAR_LIST
2064 && argvars[0].vval.v_list != NULL))
2065 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002066 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002067 return NULL;
2068 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002069 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002070 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002071 d = argvars[1].vval.v_dict;
2072 }
2073
2074 if (d != NULL)
2075 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002076 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002077 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002078 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002079 tabnr = -1; // notifications are global by default
2080 else
2081 tabnr = 0;
2082 if (tabnr > 0)
2083 {
2084 tp = find_tabpage(tabnr);
2085 if (tp == NULL)
2086 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002087 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002088 return NULL;
2089 }
2090 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002091 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002092 else if (popup_is_notification(type))
2093 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002094
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002095 // Create the window and buffer.
2096 wp = win_alloc_popup_win();
2097 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002098 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002099 if (rettv != NULL)
2100 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002101 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002102 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002103
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104 if (buf != NULL)
2105 {
2106 // use existing buffer
2107 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002108 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002109 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002110 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002111 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002112 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002113 }
2114 else
2115 {
2116 // create a new buffer associated with the popup
2117 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002118 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002119 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002120 {
2121 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002122 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002123 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002124 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002125
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002126 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002127
Bram Moolenaar46451042019-08-24 15:50:46 +02002128 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002129 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002130 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002131 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002132 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002133 buf->b_p_ul = -1; // no undo
2134 buf->b_p_swf = FALSE; // no swap file
2135 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002136 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002137
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002138 // Avoid that 'buftype' is reset when this buffer is entered.
2139 buf->b_p_initialized = TRUE;
2140 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002141 wp->w_p_wrap = TRUE; // 'wrap' is default on
2142 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002143
Bram Moolenaara3fce622019-06-20 02:31:49 +02002144 if (tp != NULL)
2145 {
2146 // popup on specified tab page
2147 wp->w_next = tp->tp_first_popupwin;
2148 tp->tp_first_popupwin = wp;
2149 }
2150 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002151 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002152 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002153 wp->w_next = curtab->tp_first_popupwin;
2154 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002155 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002156 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002157 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002158 win_T *prev = first_popupwin;
2159
2160 // Global popup: add at the end, so that it gets displayed on top of
2161 // older ones with the same zindex. Matters for notifications.
2162 if (first_popupwin == NULL)
2163 first_popupwin = wp;
2164 else
2165 {
2166 while (prev->w_next != NULL)
2167 prev = prev->w_next;
2168 prev->w_next = wp;
2169 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002170 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002171
Bram Moolenaar79648732019-07-18 21:43:07 +02002172 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002173 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002174
Bram Moolenaar79648732019-07-18 21:43:07 +02002175 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002176 {
2177 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002178 }
2179 if (type == TYPE_ATCURSOR)
2180 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002181 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002182 set_moved_values(wp);
2183 set_moved_columns(wp, FIND_STRING);
2184 }
2185
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002186 if (type == TYPE_BEVAL)
2187 {
2188 wp->w_popup_pos = POPPOS_BOTLEFT;
2189
2190 // by default use the mouse position
2191 wp->w_wantline = mouse_row;
2192 if (wp->w_wantline <= 0) // mouse on first line
2193 {
2194 wp->w_wantline = 2;
2195 wp->w_popup_pos = POPPOS_TOPLEFT;
2196 }
2197 wp->w_wantcol = mouse_col + 1;
2198 set_mousemoved_values(wp);
2199 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2200 }
2201
Bram Moolenaar33796b32019-06-08 16:01:13 +02002202 // set default values
2203 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002204 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002205
Bram Moolenaar9198de32022-08-27 21:30:03 +01002206 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002207 {
2208 win_T *twp, *nextwin;
2209 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002210
2211 // Try to not overlap with another global popup. Guess we need 3
2212 // more screen lines than buffer lines.
2213 wp->w_wantline = 1;
2214 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2215 {
2216 nextwin = twp->w_next;
2217 if (twp != wp
2218 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2219 && twp->w_winrow <= wp->w_wantline - 1 + height
2220 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2221 {
2222 // move to below this popup and restart the loop to check for
2223 // overlap with other popups
2224 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2225 nextwin = first_popupwin;
2226 }
2227 }
2228 if (wp->w_wantline + height > Rows)
2229 {
2230 // can't avoid overlap, put on top in the hope that message goes
2231 // away soon.
2232 wp->w_wantline = 1;
2233 }
2234
2235 wp->w_wantcol = 10;
2236 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002237 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002238 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002239 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002240 for (i = 0; i < 4; ++i)
2241 wp->w_popup_border[i] = 1;
2242 wp->w_popup_padding[1] = 1;
2243 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002244
Bram Moolenaar9198de32022-08-27 21:30:03 +01002245 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002246 }
2247
Bram Moolenaara730e552019-06-16 19:05:31 +02002248 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002249 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002250 wp->w_popup_pos = POPPOS_CENTER;
2251 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002252 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002253 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002254 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002255 }
2256
Bram Moolenaara730e552019-06-16 19:05:31 +02002257 if (type == TYPE_MENU)
2258 {
2259 typval_T tv;
2260 callback_T callback;
2261
2262 tv.v_type = VAR_STRING;
2263 tv.vval.v_string = (char_u *)"popup_filter_menu";
2264 callback = get_callback(&tv);
2265 if (callback.cb_name != NULL)
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002266 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002267 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002268 if (callback.cb_free_name)
2269 vim_free(callback.cb_name);
2270 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002271
2272 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002273 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002274 }
2275
Bram Moolenaar79648732019-07-18 21:43:07 +02002276 if (type == TYPE_PREVIEW)
2277 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002278 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002279 wp->w_popup_close = POPCLOSE_BUTTON;
2280 for (i = 0; i < 4; ++i)
2281 wp->w_popup_border[i] = 1;
2282 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002283 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002284 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002285# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002286 if (type == TYPE_INFO)
2287 {
2288 wp->w_popup_pos = POPPOS_TOPLEFT;
2289 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2290 wp->w_popup_close = POPCLOSE_BUTTON;
2291 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002292 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002293 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002294# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002295
Bram Moolenaarae943152019-06-16 22:54:14 +02002296 for (i = 0; i < 4; ++i)
2297 VIM_CLEAR(wp->w_border_highlight[i]);
2298 for (i = 0; i < 8; ++i)
2299 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002300 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002301 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002302 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002303
Bram Moolenaar79648732019-07-18 21:43:07 +02002304 if (d != NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002305 {
Bram Moolenaar79648732019-07-18 21:43:07 +02002306 // Deal with options.
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002307 if (apply_options(wp, d, TRUE) == FAIL)
2308 {
2309 (void)popup_close(wp->w_id, FALSE);
2310 return NULL;
2311 }
2312 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002313
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002314#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002315 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2316 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002317#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002318
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002319 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002320
2321 wp->w_vsep_width = 0;
2322
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002323 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002324 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002325
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002326#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002327 // When running a terminal in the popup it becomes the current window.
2328 if (buf->b_term != NULL)
2329 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002330#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002331
Bram Moolenaara730e552019-06-16 19:05:31 +02002332 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002333}
2334
2335/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002336 * popup_clear()
2337 */
2338 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002339f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002340{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002341 int force = FALSE;
2342
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002343 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2344 return;
2345
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002346 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002347 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002348 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002349}
2350
2351/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002352 * popup_create({text}, {options})
2353 */
2354 void
2355f_popup_create(typval_T *argvars, typval_T *rettv)
2356{
Bram Moolenaar17627312019-06-02 19:53:44 +02002357 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002358}
2359
2360/*
2361 * popup_atcursor({text}, {options})
2362 */
2363 void
2364f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2365{
Bram Moolenaar17627312019-06-02 19:53:44 +02002366 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002367}
2368
2369/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002370 * popup_beval({text}, {options})
2371 */
2372 void
2373f_popup_beval(typval_T *argvars, typval_T *rettv)
2374{
2375 popup_create(argvars, rettv, TYPE_BEVAL);
2376}
2377
2378/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002379 * Invoke the close callback for window "wp" with value "result".
2380 * Careful: The callback may make "wp" invalid!
2381 */
2382 static void
2383invoke_popup_callback(win_T *wp, typval_T *result)
2384{
2385 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002386 typval_T argv[3];
2387
Christian Brabandt6701abf2023-11-19 10:52:50 +01002388 rettv.v_type = VAR_UNKNOWN;
2389
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002390 argv[0].v_type = VAR_NUMBER;
2391 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2392
2393 if (result != NULL && result->v_type != VAR_UNKNOWN)
2394 copy_tv(result, &argv[1]);
2395 else
2396 {
2397 argv[1].v_type = VAR_NUMBER;
2398 argv[1].vval.v_number = 0;
2399 }
2400
2401 argv[2].v_type = VAR_UNKNOWN;
2402
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002403 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002404 if (result != NULL)
2405 clear_tv(&argv[1]);
2406 clear_tv(&rettv);
2407}
2408
2409/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002410 * Make "prevwin" the current window, unless it's equal to "wp".
2411 * Otherwise make "firstwin" the current window.
2412 */
2413 static void
2414back_to_prevwin(win_T *wp)
2415{
2416 if (win_valid(prevwin) && wp != prevwin)
2417 win_enter(prevwin, FALSE);
2418 else
2419 win_enter(firstwin, FALSE);
2420}
2421
2422/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002423 * Close popup "wp" and invoke any close callback for it.
Christian Brabandt8e831052025-06-18 18:33:31 +02002424 * Careful: callback function might have freed the popup window already
Bram Moolenaar3397f742019-06-02 18:40:06 +02002425 */
2426 static void
2427popup_close_and_callback(win_T *wp, typval_T *arg)
2428{
Christian Brabandt8e831052025-06-18 18:33:31 +02002429 int id;
2430
2431 if (!win_valid(wp))
2432 return;
2433
2434 id = wp->w_id;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002435
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002436#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002437 if (wp == curwin && curbuf->b_term != NULL)
2438 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002439 win_T *owp;
2440
2441 // Closing popup window with a terminal: put focus back on the first
2442 // that works:
2443 // - another popup window with a terminal
2444 // - the previous window
2445 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002446 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002447 if (owp != curwin && owp->w_buffer->b_term != NULL)
2448 break;
2449 if (owp != NULL)
2450 win_enter(owp, FALSE);
2451 else
2452 {
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002453 FOR_ALL_POPUPWINS_IN_TAB(curtab, owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002454 if (owp != curwin && owp->w_buffer->b_term != NULL)
2455 break;
2456 if (owp != NULL)
2457 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002458 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002459 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002460 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002461 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002462#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002463
Bram Moolenaar49540192019-12-11 19:34:54 +01002464 // Just in case a check higher up is missing.
2465 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002466 {
2467 // To avoid getting stuck when win_execute() does something that causes
2468 // an error, stop calling the filter callback.
2469 free_callback(&wp->w_filter_cb);
2470
Bram Moolenaar49540192019-12-11 19:34:54 +01002471 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002472 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002473
Bram Moolenaarcee52202020-03-11 14:19:58 +01002474 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002475 if (wp->w_close_cb.cb_name != NULL)
2476 // Careful: This may make "wp" invalid.
2477 invoke_popup_callback(wp, arg);
2478
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002479 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002480 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002481}
2482
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002483 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002484popup_close_with_retval(win_T *wp, int retval)
2485{
2486 typval_T res;
2487
2488 res.v_type = VAR_NUMBER;
2489 res.vval.v_number = retval;
2490 popup_close_and_callback(wp, &res);
2491}
2492
Bram Moolenaar3397f742019-06-02 18:40:06 +02002493/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002494 * Close popup "wp" because of a mouse click.
2495 */
2496 void
2497popup_close_for_mouse_click(win_T *wp)
2498{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002499 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002500}
2501
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002502 static void
2503check_mouse_moved(win_T *wp, win_T *mouse_wp)
2504{
2505 // Close the popup when all if these are true:
2506 // - the mouse is not on this popup
2507 // - "mousemoved" was used
2508 // - the mouse is no longer on the same screen row or the mouse column is
2509 // outside of the relevant text
2510 if (wp != mouse_wp
2511 && wp->w_popup_mouse_row != 0
2512 && (wp->w_popup_mouse_row != mouse_row
2513 || mouse_col < wp->w_popup_mouse_mincol
2514 || mouse_col > wp->w_popup_mouse_maxcol))
2515 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002516 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002517 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002518 }
2519}
2520
2521/*
2522 * Called when the mouse moved: may close a popup with "mousemoved".
2523 */
2524 void
2525popup_handle_mouse_moved(void)
2526{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002527 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002528 win_T *mouse_wp;
2529 int row = mouse_row;
2530 int col = mouse_col;
2531
2532 // find the window where the mouse is in
2533 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2534
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002535 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2536 {
2537 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002538 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002539 }
2540 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2541 {
2542 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002543 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002544 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002545}
2546
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002547/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002548 * In a filter: check if the typed key is a mouse event that is used for
2549 * dragging the popup.
2550 */
2551 static void
2552filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2553{
2554 int row = mouse_row;
2555 int col = mouse_col;
2556
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002557 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002558 && is_mouse_key(c)
2559 && (wp == popup_dragwin
2560 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2561 // do not consume the key, allow for dragging the popup
2562 rettv->vval.v_number = 0;
2563}
2564
Bram Moolenaara730e552019-06-16 19:05:31 +02002565/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002566 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002567 */
2568 void
2569f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2570{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002571 int id;
2572 win_T *wp;
2573 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002574 typval_T res;
2575 int c;
2576 linenr_T old_lnum;
2577
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002578 if (in_vim9script()
2579 && (check_for_number_arg(argvars, 0) == FAIL
2580 || check_for_string_arg(argvars, 1) == FAIL))
2581 return;
2582
2583 id = tv_get_number(&argvars[0]);
2584 wp = win_id2wp(id);
2585 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002586 // If the popup has been closed do not consume the key.
2587 if (wp == NULL)
2588 return;
2589
2590 c = *key;
2591 if (c == K_SPECIAL && key[1] != NUL)
2592 c = TO_SPECIAL(key[1], key[2]);
2593
2594 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002595 rettv->v_type = VAR_BOOL;
2596 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002597 res.v_type = VAR_NUMBER;
2598
2599 old_lnum = wp->w_cursor.lnum;
Christian Brabandtbadeedd2023-08-13 19:25:28 +02002600 if (c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2601 {
2602 if (wp->w_cursor.lnum > 1)
2603 --wp->w_cursor.lnum;
2604 else
2605 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2606 }
2607 if (c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
2608 {
2609 if (wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2610 ++wp->w_cursor.lnum;
2611 else
2612 wp->w_cursor.lnum = 1;
2613 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002614 if (old_lnum != wp->w_cursor.lnum)
2615 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002616 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002617 return;
2618 }
2619
2620 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2621 {
2622 // Cancelled, invoke callback with -1
2623 res.vval.v_number = -1;
2624 popup_close_and_callback(wp, &res);
2625 return;
2626 }
2627 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2628 {
2629 // Invoke callback with current index.
2630 res.vval.v_number = wp->w_cursor.lnum;
2631 popup_close_and_callback(wp, &res);
2632 return;
2633 }
2634
2635 filter_handle_drag(wp, c, rettv);
2636}
2637
2638/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002639 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002640 */
2641 void
2642f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2643{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002644 int id;
2645 win_T *wp;
2646 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002647 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002648 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002649
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002650 if (in_vim9script()
2651 && (check_for_number_arg(argvars, 0) == FAIL
2652 || check_for_string_arg(argvars, 1) == FAIL))
2653 return;
2654
2655 id = tv_get_number(&argvars[0]);
2656 wp = win_id2wp(id);
2657 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002658 // If the popup has been closed don't consume the key.
2659 if (wp == NULL)
2660 return;
2661
Bram Moolenaara730e552019-06-16 19:05:31 +02002662 c = *key;
Ernie Raelb14c3252024-07-19 16:37:09 +02002663 if (c == CAR && need_wait_return)
2664 return;
Bram Moolenaara730e552019-06-16 19:05:31 +02002665 if (c == K_SPECIAL && key[1] != NUL)
2666 c = TO_SPECIAL(key[1], key[2]);
2667
Bram Moolenaara42d9452019-06-15 21:46:30 +02002668 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002669 rettv->v_type = VAR_BOOL;
2670 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002671
Bram Moolenaara730e552019-06-16 19:05:31 +02002672 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002673 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002674 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002675 res.vval.v_number = 0;
2676 else
2677 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002678 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002679 return;
2680 }
2681
2682 // Invoke callback
2683 res.v_type = VAR_NUMBER;
2684 popup_close_and_callback(wp, &res);
2685}
2686
2687/*
2688 * popup_dialog({text}, {options})
2689 */
2690 void
2691f_popup_dialog(typval_T *argvars, typval_T *rettv)
2692{
2693 popup_create(argvars, rettv, TYPE_DIALOG);
2694}
2695
2696/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002697 * popup_menu({text}, {options})
2698 */
2699 void
2700f_popup_menu(typval_T *argvars, typval_T *rettv)
2701{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002702 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002703}
2704
2705/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002706 * popup_notification({text}, {options})
2707 */
2708 void
2709f_popup_notification(typval_T *argvars, typval_T *rettv)
2710{
2711 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2712}
2713
2714/*
2715 * Find the popup window with window-ID "id".
2716 * If the popup window does not exist NULL is returned.
2717 * If the window is not a popup window, and error message is given.
2718 */
2719 static win_T *
2720find_popup_win(int id)
2721{
2722 win_T *wp = win_id2wp(id);
2723
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002724 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002725 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002726 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002727 return NULL;
2728 }
2729 return wp;
2730}
2731
2732/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002733 * popup_close({id})
2734 */
2735 void
2736f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2737{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002738 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002739 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002740
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002741 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2742 return;
2743
2744 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002745 if (
2746# ifdef FEAT_TERMINAL
2747 // if the popup contains a terminal it will become hidden
2748 curbuf->b_term == NULL &&
2749# endif
2750 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002751 return;
2752
2753 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002754 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002755 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002756}
2757
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002758 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002759popup_hide(win_T *wp)
2760{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002761#ifdef FEAT_TERMINAL
2762 if (error_if_term_popup_window())
2763 return;
2764#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002765 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2766 return;
2767
2768 wp->w_popup_flags |= POPF_HIDDEN;
2769 // Do not decrement b_nwindows, we still reference the buffer.
Christian Brabandtf00f4d92024-09-03 18:20:13 +02002770 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
2771 clear_cmdline = TRUE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002772 redraw_all_later(UPD_NOT_VALID);
2773 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002774}
2775
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002776/*
2777 * popup_hide({id})
2778 */
2779 void
2780f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2781{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002782 int id;
2783 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002784
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002785 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2786 return;
2787
2788 id = (int)tv_get_number(argvars);
2789 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002790 if (wp == NULL)
2791 return;
2792
2793 popup_hide(wp);
2794 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002795}
2796
2797 void
2798popup_show(win_T *wp)
2799{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002800 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2801 return;
2802
2803 wp->w_popup_flags &= ~POPF_HIDDEN;
2804 redraw_all_later(UPD_NOT_VALID);
2805 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002806}
2807
2808/*
2809 * popup_show({id})
2810 */
2811 void
2812f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2813{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002814 int id;
2815 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002816
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002817 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2818 return;
2819
2820 id = (int)tv_get_number(argvars);
2821 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002822 if (wp == NULL)
2823 return;
2824
2825 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2826 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002827#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002828 if (wp->w_popup_flags & POPF_INFO)
2829 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002830#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002831}
2832
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002833/*
2834 * popup_settext({id}, {text})
2835 */
2836 void
2837f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2838{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002839 int id;
2840 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002841
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002842 if (in_vim9script()
2843 && (check_for_number_arg(argvars, 0) == FAIL
2844 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2845 return;
2846
2847 id = (int)tv_get_number(&argvars[0]);
2848 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002849 if (wp == NULL)
2850 return;
2851
2852 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2853 return;
2854
2855 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2856 redraw_win_later(wp, UPD_NOT_VALID);
2857 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002858}
2859
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002860/*
2861 * popup_setbuf({id}, {bufnr})
2862 */
2863 void
2864f_popup_setbuf(typval_T *argvars, typval_T *rettv UNUSED)
2865{
2866 int id;
2867 win_T *wp;
2868 buf_T *buf;
2869
2870 rettv->v_type = VAR_BOOL;
2871 rettv->vval.v_number = VVAL_FALSE;
2872
2873 if (check_for_number_arg(argvars, 0) == FAIL
2874 || check_for_buffer_arg(argvars, 1) == FAIL)
2875 return;
2876
2877 id = (int)tv_get_number(&argvars[0]);
2878 wp = find_popup_win(id);
2879 if (wp == NULL)
2880 return;
2881
2882 buf = tv_get_buf_from_arg(&argvars[1]);
2883
2884 if (buf == NULL)
2885 return;
2886#ifdef FEAT_TERMINAL
2887 if (buf->b_term != NULL && popup_terminal_exists())
2888 {
2889 emsg(_(e_cannot_open_second_popup_with_terminal));
2890 return;
2891 }
2892#endif
2893
2894 if (wp->w_buffer != buf)
2895 {
2896 wp->w_buffer->b_nwindows--;
2897 win_init_popup_win(wp, buf);
2898 set_local_options_default(wp, FALSE);
2899 swap_exists_action = SEA_READONLY;
2900 buffer_ensure_loaded(buf);
2901 swap_exists_action = SEA_NONE;
2902 redraw_win_later(wp, UPD_NOT_VALID);
2903 popup_adjust_position(wp);
2904 }
2905 rettv->vval.v_number = VVAL_TRUE;
2906}
2907
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002908 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002909popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002910{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002911 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002912 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002913 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002914 clear_cmdline = TRUE;
2915 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002916
Bram Moolenaar43568642022-08-28 13:02:45 +01002917#ifdef HAS_MESSAGE_WINDOW
2918 if (wp == message_win)
2919 message_win = NULL;
2920#endif
2921
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002922 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002923 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002924}
2925
Bram Moolenaar82106932020-03-13 14:34:38 +01002926 static void
2927error_for_popup_window(void)
2928{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002929 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002930}
2931
2932 int
2933error_if_popup_window(int also_with_term UNUSED)
2934{
2935 // win_execute() may set "curwin" to a popup window temporarily, but many
2936 // commands are disallowed then. When a terminal runs in the popup most
2937 // things are allowed. When a terminal is finished it can be closed.
2938 if (WIN_IS_POPUP(curwin)
2939# ifdef FEAT_TERMINAL
2940 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002941# endif
2942 )
2943 {
2944 error_for_popup_window();
2945 return TRUE;
2946 }
2947 return FALSE;
2948}
2949
Bram Moolenaarec583842019-05-26 14:11:23 +02002950/*
2951 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002952 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002953 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002954 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002955 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002956popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002957{
2958 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002959 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002960 win_T *prev = NULL;
2961
Bram Moolenaarec583842019-05-26 14:11:23 +02002962 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002963 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002964 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002965 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002966 if (wp == curwin)
2967 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002968 if (!force)
2969 {
2970 error_for_popup_window();
2971 return FAIL;
2972 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002973 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002974 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002975 if (prev == NULL)
2976 first_popupwin = wp->w_next;
2977 else
2978 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002979 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002980 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002981 }
2982
Bram Moolenaarec583842019-05-26 14:11:23 +02002983 // go through tab-local popups
2984 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002985 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002986 return OK;
2987 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002988}
2989
2990/*
2991 * Close a popup window with Window-id "id" in tabpage "tp".
2992 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002993 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002994popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002995{
2996 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002997 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002998 win_T *prev = NULL;
2999
Bram Moolenaarec583842019-05-26 14:11:23 +02003000 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
3001 if (wp->w_id == id)
3002 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01003003 if (wp == curwin)
3004 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003005 if (!force)
3006 {
3007 error_for_popup_window();
3008 return FAIL;
3009 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003010 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01003011 }
Bram Moolenaarec583842019-05-26 14:11:23 +02003012 if (prev == NULL)
3013 *root = wp->w_next;
3014 else
3015 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02003016 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02003017 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02003018 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02003019 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003020}
3021
3022 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003023close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003024{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003025 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003026 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003027 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003028 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003029 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003030 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003031 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003032 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003033}
3034
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003035/*
3036 * popup_move({id}, {options})
3037 */
3038 void
3039f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
3040{
Bram Moolenaarae943152019-06-16 22:54:14 +02003041 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003042 int id;
3043 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003044
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003045 if (in_vim9script()
3046 && (check_for_number_arg(argvars, 0) == FAIL
3047 || check_for_dict_arg(argvars, 1) == FAIL))
3048 return;
3049
3050 id = (int)tv_get_number(argvars);
3051 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003052 if (wp == NULL)
3053 return; // invalid {id}
3054
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003055 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003056 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003057 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003058
Bram Moolenaarae943152019-06-16 22:54:14 +02003059 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003060
3061 if (wp->w_winrow + wp->w_height >= cmdline_row)
3062 clear_cmdline = TRUE;
3063 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003064}
3065
Bram Moolenaarbc133542019-05-29 20:26:46 +02003066/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003067 * popup_setoptions({id}, {options})
3068 */
3069 void
3070f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
3071{
3072 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003073 int id;
3074 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003075 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003076
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003077 if (in_vim9script()
3078 && (check_for_number_arg(argvars, 0) == FAIL
3079 || check_for_dict_arg(argvars, 1) == FAIL))
3080 return;
3081
3082 id = (int)tv_get_number(argvars);
3083 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02003084 if (wp == NULL)
3085 return; // invalid {id}
3086
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003087 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02003088 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003089 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003090 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003091
Christian Brabandtf6cdab32023-08-11 23:42:02 +02003092 (void)apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02003093
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003094 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003095 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003096 popup_adjust_position(wp);
3097}
3098
3099/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003100 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003101 */
3102 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003103f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003104{
3105 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003106 int id;
3107 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003108 int top_extra;
3109 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003110
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003111 if (rettv_dict_alloc(rettv) == FAIL)
3112 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003113
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003114 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3115 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003116
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003117 id = (int)tv_get_number(argvars);
3118 wp = find_popup_win(id);
3119 if (wp == NULL)
3120 return; // invalid {id}
3121 top_extra = popup_top_extra(wp);
3122 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003123
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003124 // we know how much space we need, avoid resizing halfway
3125 dict = rettv->vval.v_dict;
3126 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003127
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003128 dict_add_number(dict, "line", wp->w_winrow + 1);
3129 dict_add_number(dict, "col", wp->w_wincol + 1);
3130 dict_add_number(dict, "width", wp->w_width + left_extra
3131 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3132 dict_add_number(dict, "height", wp->w_height + top_extra
3133 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003134
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003135 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3136 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3137 dict_add_number(dict, "core_width", wp->w_width);
3138 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003139
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003140 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3141 dict_add_number(dict, "firstline", wp->w_topline);
3142 dict_add_number(dict, "lastline", wp->w_botline - 1);
3143 dict_add_number(dict, "visible",
3144 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3145
3146 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003147}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003148
3149/*
3150 * popup_list()
3151 */
3152 void
3153f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3154{
3155 win_T *wp;
3156 tabpage_T *tp;
3157
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003158 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003159 return;
3160 FOR_ALL_POPUPWINS(wp)
3161 list_append_number(rettv->vval.v_list, wp->w_id);
3162 FOR_ALL_TABPAGES(tp)
3163 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3164 list_append_number(rettv->vval.v_list, wp->w_id);
3165}
3166
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003167/*
3168 * popup_locate({row}, {col})
3169 */
3170 void
3171f_popup_locate(typval_T *argvars, typval_T *rettv)
3172{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003173 int row;
3174 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003175 win_T *wp;
3176
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003177 if (in_vim9script()
3178 && (check_for_number_arg(argvars, 0) == FAIL
3179 || check_for_number_arg(argvars, 1) == FAIL))
3180 return;
3181
3182 row = tv_get_number(&argvars[0]) - 1;
3183 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003184 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003185 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003186 rettv->vval.v_number = wp->w_id;
3187}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003188
3189/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003190 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3191 */
3192 static void
3193get_padding_border(dict_T *dict, int *array, char *name)
3194{
3195 list_T *list;
3196 int i;
3197
3198 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3199 return;
3200
3201 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003202 if (list == NULL)
3203 return;
3204
3205 dict_add_list(dict, name, list);
3206 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3207 for (i = 0; i < 4; ++i)
3208 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003209}
3210
3211/*
3212 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3213 */
3214 static void
3215get_borderhighlight(dict_T *dict, win_T *wp)
3216{
3217 list_T *list;
3218 int i;
3219
3220 for (i = 0; i < 4; ++i)
3221 if (wp->w_border_highlight[i] != NULL)
3222 break;
3223 if (i == 4)
3224 return;
3225
3226 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003227 if (list == NULL)
3228 return;
3229
3230 dict_add_list(dict, "borderhighlight", list);
3231 for (i = 0; i < 4; ++i)
3232 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003233}
3234
3235/*
3236 * For popup_getoptions(): add a "borderchars" entry to "dict".
3237 */
3238 static void
3239get_borderchars(dict_T *dict, win_T *wp)
3240{
3241 list_T *list;
3242 int i;
3243 char_u buf[NUMBUFLEN];
3244 int len;
3245
3246 for (i = 0; i < 8; ++i)
3247 if (wp->w_border_char[i] != 0)
3248 break;
3249 if (i == 8)
3250 return;
3251
3252 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003253 if (list == NULL)
3254 return;
3255
3256 dict_add_list(dict, "borderchars", list);
3257 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003258 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003259 len = mb_char2bytes(wp->w_border_char[i], buf);
3260 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003261 }
3262}
3263
3264/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003265 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003266 */
3267 static void
3268get_moved_list(dict_T *dict, win_T *wp)
3269{
3270 list_T *list;
3271
3272 list = list_alloc();
3273 if (list != NULL)
3274 {
3275 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003276 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003277 list_append_number(list, wp->w_popup_mincol);
3278 list_append_number(list, wp->w_popup_maxcol);
3279 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003280 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003281 if (list == NULL)
3282 return;
3283
3284 dict_add_list(dict, "mousemoved", list);
3285 list_append_number(list, wp->w_popup_mouse_row);
3286 list_append_number(list, wp->w_popup_mouse_mincol);
3287 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003288}
3289
3290/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003291 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003292 */
3293 void
3294f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3295{
3296 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003297 int id;
3298 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003299 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003300 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003301
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003302 if (rettv_dict_alloc(rettv) == FAIL)
3303 return;
3304
3305 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3306 return;
3307
3308 id = (int)tv_get_number(argvars);
3309 wp = find_popup_win(id);
3310 if (wp == NULL)
3311 return;
3312
3313 dict = rettv->vval.v_dict;
3314 dict_add_number(dict, "line", wp->w_wantline);
3315 dict_add_number(dict, "col", wp->w_wantcol);
3316 dict_add_number(dict, "minwidth", wp->w_minwidth);
3317 dict_add_number(dict, "minheight", wp->w_minheight);
3318 dict_add_number(dict, "maxheight", wp->w_maxheight);
3319 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3320 dict_add_number(dict, "firstline", wp->w_firstline);
3321 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3322 dict_add_number(dict, "zindex", wp->w_zindex);
3323 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3324 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003325 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003326 proptype_T *pt = text_prop_type_by_id(
3327 wp->w_popup_prop_win->w_buffer,
3328 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003329
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003330 if (pt != NULL)
3331 dict_add_string(dict, "textprop", pt->pt_name);
3332 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3333 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3334 }
3335 dict_add_string(dict, "title", wp->w_popup_title);
3336 dict_add_number(dict, "wrap", wp->w_p_wrap);
3337 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3338 dict_add_number(dict, "dragall",
3339 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3340 dict_add_number(dict, "mapping",
3341 (wp->w_popup_flags & POPF_MAPPING) != 0);
3342 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3343 dict_add_number(dict, "posinvert",
3344 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3345 dict_add_number(dict, "cursorline",
3346 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3347 dict_add_string(dict, "highlight", wp->w_p_wcr);
3348 if (wp->w_scrollbar_highlight != NULL)
3349 dict_add_string(dict, "scrollbarhighlight",
3350 wp->w_scrollbar_highlight);
3351 if (wp->w_thumb_highlight != NULL)
3352 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003353
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003354 // find the tabpage that holds this popup
3355 i = 1;
3356 FOR_ALL_TABPAGES(tp)
3357 {
3358 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003359
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003360 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3361 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003362 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003363 if (twp != NULL)
3364 break;
3365 ++i;
3366 }
3367 if (tp == NULL)
3368 i = -1; // must be global
3369 else if (tp == curtab)
3370 i = 0;
3371 dict_add_number(dict, "tabpage", i);
3372
3373 get_padding_border(dict, wp->w_popup_padding, "padding");
3374 get_padding_border(dict, wp->w_popup_border, "border");
3375 get_borderhighlight(dict, wp);
3376 get_borderchars(dict, wp);
3377 get_moved_list(dict, wp);
3378
3379 if (wp->w_filter_cb.cb_name != NULL)
3380 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3381 if (wp->w_close_cb.cb_name != NULL)
3382 dict_add_callback(dict, "callback", &wp->w_close_cb);
3383
3384 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3385 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3386 {
3387 dict_add_string(dict, "pos",
3388 (char_u *)poppos_entries[i].pp_name);
3389 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003390 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003391
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003392 dict_add_string(dict, "close", (char_u *)(
3393 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3394 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003395
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003396# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003397 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3398 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003399# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003400}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003401
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003402# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003403/*
3404 * Return TRUE if the current window is running a terminal in a popup window.
3405 * Return FALSE when the job has ended.
3406 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003407 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003408error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003409{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003410 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3411 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003412 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003413 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003414 return TRUE;
3415 }
3416 return FALSE;
3417}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003418# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003419
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003420/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003421 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003422 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003423 * Each calling function should use a different flag, see the list at
3424 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003425 */
3426 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003427popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003428{
3429 win_T *wp;
3430
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003431 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003432 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003433 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003434 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003435}
3436
3437/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003438 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003439 * Must have called popup_reset_handled() first.
3440 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3441 * popup with the highest zindex.
3442 */
3443 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003444find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003445{
3446 win_T *wp;
3447 win_T *found_wp;
3448 int found_zindex;
3449
3450 found_zindex = lowest ? INT_MAX : 0;
3451 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003452 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003453 if ((wp->w_popup_handled & handled_flag) == 0
3454 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003455 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003456 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003457 {
3458 found_zindex = wp->w_zindex;
3459 found_wp = wp;
3460 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003461 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003462 if ((wp->w_popup_handled & handled_flag) == 0
3463 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003464 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003465 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003466 {
3467 found_zindex = wp->w_zindex;
3468 found_wp = wp;
3469 }
3470
3471 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003472 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003473 return found_wp;
3474}
3475
3476/*
3477 * Invoke the filter callback for window "wp" with typed character "c".
3478 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003479 * Returns the return value of the filter or -1 for CTRL-C in the current
3480 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003481 * Careful: The filter may make "wp" invalid!
3482 */
3483 static int
3484invoke_popup_filter(win_T *wp, int c)
3485{
3486 int res;
3487 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003488 typval_T argv[3];
3489 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003490 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003491 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003492
Bram Moolenaara42d9452019-06-15 21:46:30 +02003493 // Emergency exit: CTRL-C closes the popup.
3494 if (c == Ctrl_C)
3495 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003496 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003497 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003498
3499 // Reset got_int to avoid the callback isn't called.
3500 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003501 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003502 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003503
3504 // If the popup is the current window it probably fails to close. Then
3505 // do not consume the key.
3506 if (was_curwin && wp == curwin)
3507 return -1;
3508 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003509 }
3510
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003511 argv[0].v_type = VAR_NUMBER;
3512 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3513
3514 // Convert the number to a string, so that the function can use:
3515 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003516 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003517 argv[1].v_type = VAR_STRING;
3518 argv[1].vval.v_string = vim_strsave(buf);
3519
3520 argv[2].v_type = VAR_UNKNOWN;
3521
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003522 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003523 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3524 {
3525 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003526 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003527 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003528 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003529 }
3530 else
3531 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003532 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3533 popup_highlight_curline(wp);
3534
Bram Moolenaar371806e2020-10-22 13:44:54 +02003535 // If an error message was given always return FALSE, so that keys are
3536 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003537 // If we get three errors in a row then close the popup. Decrement the
3538 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3539 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003540 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003541 {
3542 wp->w_filter_errors += 10;
3543 if (wp->w_filter_errors >= 30)
3544 popup_close_with_retval(wp, -1);
3545 res = FALSE;
3546 }
3547 else
3548 {
3549 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3550 --wp->w_filter_errors;
3551 res = tv_get_bool(&rettv);
3552 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003553 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003554
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003555 vim_free(argv[1].vval.v_string);
3556 clear_tv(&rettv);
3557 return res;
3558}
3559
3560/*
3561 * Called when "c" was typed: invoke popup filter callbacks.
3562 * Returns TRUE when the character was consumed,
3563 */
3564 int
3565popup_do_filter(int c)
3566{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003567 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003568 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003569 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003570 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003571 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003572 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003573
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003574#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003575 // Popup window with terminal always gets focus.
3576 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3577 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003578#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003579
Bram Moolenaar934470e2019-09-01 23:27:05 +02003580 if (recursive)
3581 return FALSE;
3582 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003583
Bram Moolenaarf6396232019-08-24 19:36:00 +02003584 if (c == K_LEFTMOUSE)
3585 {
3586 int row = mouse_row;
3587 int col = mouse_col;
3588
3589 wp = mouse_find_win(&row, &col, FIND_POPUP);
3590 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003591 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003592 }
3593
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003594 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003595 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003596 while (res == FALSE
3597 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003598 if (wp->w_filter_cb.cb_name != NULL
3599 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003600 res = invoke_popup_filter(wp, c);
3601
glepnir8a635292025-03-21 18:12:32 +01003602 // when Ctrl-C and no popup has been processed (res is still FALSE)
3603 // Try to find and close a popup that has no filter callback
3604 if (c == Ctrl_C && res == FALSE)
3605 {
3606 popup_reset_handled(POPUP_HANDLED_2);
3607 wp = find_next_popup(FALSE, POPUP_HANDLED_2);
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02003608 if (wp != NULL)
3609 {
glepnir8a635292025-03-21 18:12:32 +01003610 popup_close_with_retval(wp, -1);
3611 res = TRUE;
3612 }
3613 }
3614
3615
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003616 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003617 {
3618 int save_got_int = got_int;
3619
3620 // Reset got_int to avoid a function used in the statusline aborts.
3621 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003622 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003623 got_int |= save_got_int;
3624 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003625 recursive = FALSE;
3626 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003627
3628 // When interrupted return FALSE to avoid looping.
3629 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003630}
3631
Bram Moolenaar3397f742019-06-02 18:40:06 +02003632/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003633 * Return TRUE if there is a popup visible with a filter callback and the
3634 * "mapping" property off.
3635 */
3636 int
3637popup_no_mapping(void)
3638{
3639 int round;
3640 win_T *wp;
3641
3642 for (round = 1; round <= 2; ++round)
3643 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3644 wp != NULL; wp = wp->w_next)
3645 if (wp->w_filter_cb.cb_name != NULL
3646 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3647 return TRUE;
3648 return FALSE;
3649}
3650
3651/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003652 * Called when the cursor moved: check if any popup needs to be closed if the
3653 * cursor moved far enough.
3654 */
3655 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003656popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003657{
3658 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003659
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003660 popup_reset_handled(POPUP_HANDLED_3);
3661 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003662 if (wp->w_popup_curwin != NULL
3663 && (curwin != wp->w_popup_curwin
3664 || curwin->w_cursor.lnum != wp->w_popup_lnum
3665 || curwin->w_cursor.col < wp->w_popup_mincol
3666 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003667 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003668}
3669
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003670/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003671 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003672 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003673 static void
3674popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003675{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003676 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003677 char_u *cells;
3678 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003679
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003680 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3681 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003682 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003683 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003684 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003685 if (wp->w_popup_mask_cells != NULL
3686 && wp->w_popup_mask_height == height
3687 && wp->w_popup_mask_width == width)
3688 return; // cache is still valid
3689
3690 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003691 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003692 if (wp->w_popup_mask_cells == NULL)
3693 return;
3694 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003695
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003696 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003697 {
3698 int cols, cole;
3699 int lines, linee;
3700
3701 li = lio->li_tv.vval.v_list->lv_first;
3702 cols = tv_get_number(&li->li_tv);
3703 if (cols < 0)
3704 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003705 if (cols <= 0)
3706 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003707 li = li->li_next;
3708 cole = tv_get_number(&li->li_tv);
3709 if (cole < 0)
3710 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003711 if (cole > width)
3712 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003713 li = li->li_next;
3714 lines = tv_get_number(&li->li_tv);
3715 if (lines < 0)
3716 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003717 if (lines <= 0)
3718 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003719 li = li->li_next;
3720 linee = tv_get_number(&li->li_tv);
3721 if (linee < 0)
3722 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003723 if (linee > height)
3724 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003725
Bram Moolenaar4012d262020-12-29 11:57:46 +01003726 for (row = lines - 1; row < linee; ++row)
3727 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003728 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003729 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003730}
3731
3732/*
3733 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3734 * "col" and "line" are screen coordinates.
3735 */
3736 static int
3737popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3738{
3739 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3740 int line = screenline - wp->w_winrow;
3741
3742 return col >= 0 && col < width
3743 && line >= 0 && line < height
3744 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003745}
3746
3747/*
3748 * Set flags in popup_transparent[] for window "wp" to "val".
3749 */
3750 static void
3751update_popup_transparent(win_T *wp, int val)
3752{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003753 if (wp->w_popup_mask == NULL)
3754 return;
3755
3756 int width = popup_width(wp);
3757 int height = popup_height(wp);
3758 listitem_T *lio, *li;
3759 int cols, cole;
3760 int lines, linee;
3761 int col, line;
3762
3763 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003764 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003765 li = lio->li_tv.vval.v_list->lv_first;
3766 cols = tv_get_number(&li->li_tv);
3767 if (cols < 0)
3768 cols = width + cols + 1;
3769 li = li->li_next;
3770 cole = tv_get_number(&li->li_tv);
3771 if (cole < 0)
3772 cole = width + cole + 1;
3773 li = li->li_next;
3774 lines = tv_get_number(&li->li_tv);
3775 if (lines < 0)
3776 lines = height + lines + 1;
3777 li = li->li_next;
3778 linee = tv_get_number(&li->li_tv);
3779 if (linee < 0)
3780 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003781
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003782 --cols;
3783 cols -= wp->w_popup_leftoff;
3784 if (cols < 0)
3785 cols = 0;
3786 cole -= wp->w_popup_leftoff;
3787 --lines;
3788 if (lines < 0)
3789 lines = 0;
3790 for (line = lines; line < linee
3791 && line + wp->w_winrow < screen_Rows; ++line)
3792 for (col = cols; col < cole
3793 && col + wp->w_wincol < screen_Columns; ++col)
3794 popup_transparent[(line + wp->w_winrow) * screen_Columns
3795 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003796 }
3797}
3798
3799/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003800 * Only called when popup window "wp" is hidden: If the window is positioned
3801 * next to a text property, and it is now visible, then unhide the popup.
3802 * We don't check if visible popups become hidden, that is done in
3803 * popup_adjust_position().
3804 * Return TRUE if the popup became unhidden.
3805 */
3806 static int
3807check_popup_unhidden(win_T *wp)
3808{
3809 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3810 {
3811 textprop_T prop;
3812 linenr_T lnum;
3813
Bram Moolenaar27724252022-05-08 15:00:04 +01003814 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3815 && find_visible_prop(wp->w_popup_prop_win,
3816 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003817 &prop, &lnum) == OK)
3818 {
3819 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003820 wp->w_popup_prop_topline = 0; // force repositioning
3821 return TRUE;
3822 }
3823 }
3824 return FALSE;
3825}
3826
3827/*
3828 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3829 * That is when the buffer in the popup was changed, or the popup is following
3830 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003831 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003832 */
3833 static int
3834popup_need_position_adjust(win_T *wp)
3835{
3836 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3837 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003838 if (win_valid(wp->w_popup_prop_win)
3839 && (wp->w_popup_prop_changedtick
3840 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3841 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3842 return TRUE;
3843
3844 // May need to adjust the width if the cursor moved.
3845 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003846}
3847
3848/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003849 * Update "popup_mask" if needed.
3850 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003851 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003852 * Also marks window lines for redrawing.
3853 */
3854 void
3855may_update_popup_mask(int type)
3856{
3857 win_T *wp;
3858 short *mask;
3859 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003860 int redraw_all_popups = FALSE;
3861 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003862
3863 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003864 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3865 // basic (such as the screen size) must have changed.
3866 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003867 {
3868 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003869 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003870 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003871
3872 // Check if any popup window buffer has changed and if any popup connected
3873 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003874 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003875 if (wp->w_popup_flags & POPF_HIDDEN)
3876 popup_mask_refresh |= check_popup_unhidden(wp);
3877 else if (popup_need_position_adjust(wp))
3878 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003879 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003880 if (wp->w_popup_flags & POPF_HIDDEN)
3881 popup_mask_refresh |= check_popup_unhidden(wp);
3882 else if (popup_need_position_adjust(wp))
3883 popup_mask_refresh = TRUE;
3884
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003885 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003886 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003887
3888 // Need to update the mask, something has changed.
3889 popup_mask_refresh = FALSE;
3890 popup_mask_tab = curtab;
3891 popup_visible = FALSE;
3892
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003893 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003894 // If redrawing only what is needed, update "popup_mask_next" and then
3895 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003896 redrawing_all_win = TRUE;
3897 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003898 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003899 redrawing_all_win = FALSE;
3900 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003901 mask = popup_mask;
3902 else
3903 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003904 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003905
3906 // Find the window with the lowest zindex that hasn't been handled yet,
3907 // so that the window with a higher zindex overwrites the value in
3908 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003909 popup_reset_handled(POPUP_HANDLED_4);
3910 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003911 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003912 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003913 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003914
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003915 popup_visible = TRUE;
3916
Bram Moolenaar12034e22019-08-25 22:25:02 +02003917 // Recompute the position if the text changed. It may make the popup
3918 // hidden if it's attach to a text property that is no longer visible.
3919 if (redraw_all_popups || popup_need_position_adjust(wp))
3920 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003921 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003922 if (wp->w_popup_flags & POPF_HIDDEN)
3923 continue;
3924 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003925
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003926 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003927 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003928 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003929 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003930 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003931 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003932 col < wp->w_wincol + width - wp->w_popup_leftoff
3933 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003934 if (wp->w_zindex < POPUPMENU_ZINDEX
3935 && pum_visible()
3936 && pum_under_menu(line, col, FALSE))
3937 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3938 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003939 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003940 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003941 }
3942
3943 // Only check which lines are to be updated if not already
3944 // updating all lines.
3945 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003946 {
3947 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3948 win_T *prev_wp = NULL;
3949
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 for (line = 0; line < screen_Rows; ++line)
3951 {
3952 int col_done = 0;
3953
3954 for (col = 0; col < screen_Columns; ++col)
3955 {
3956 int off = line * screen_Columns + col;
3957
3958 if (popup_mask[off] != popup_mask_next[off])
3959 {
3960 popup_mask[off] = popup_mask_next[off];
3961
3962 if (line >= cmdline_row)
3963 {
3964 // the command line needs to be cleared if text below
3965 // the popup is now visible.
3966 if (!msg_scrolled && popup_mask_next[off] == 0)
3967 clear_cmdline = TRUE;
3968 }
3969 else if (col >= col_done)
3970 {
3971 linenr_T lnum;
3972 int line_cp = line;
3973 int col_cp = col;
3974
3975 // The screen position "line" / "col" needs to be
3976 // redrawn. Figure out what window that is and update
zeertzjqf2d90a32024-02-12 20:28:01 +01003977 // w_redraw_top and w_redraw_bot. Only needs to be
3978 // done once for each window line.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003979 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3980 if (wp != NULL)
3981 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003982#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003983 // A terminal window needs to be redrawn.
3984 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003985 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003986 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003987#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003988 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003989 if (wp != prev_wp)
3990 {
3991 vim_memset(plines_cache, 0,
3992 sizeof(int) * Rows);
3993 prev_wp = wp;
3994 }
3995
3996 if (line_cp >= wp->w_height)
3997 // In (or below) status line
3998 wp->w_redr_status = TRUE;
3999 else
4000 {
4001 // compute the position in the buffer line
4002 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00004003 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02004004 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02004005 redrawWinline(wp, lnum);
4006 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02004007 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004008
4009 // This line is going to be redrawn, no need to
4010 // check until the right side of the window.
4011 col_done = wp->w_wincol + wp->w_width - 1;
4012 }
4013 }
4014 }
4015 }
4016 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004017
4018 vim_free(plines_cache);
4019 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00004020
4021 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004022}
4023
4024/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02004025 * If the current window is a popup and something relevant changed, recompute
4026 * the position and size.
4027 */
4028 void
4029may_update_popup_position(void)
4030{
4031 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
4032 popup_adjust_position(curwin);
4033}
4034
4035/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004036 * Return a string of "len" spaces in IObuff.
4037 */
4038 static char_u *
4039get_spaces(int len)
4040{
4041 vim_memset(IObuff, ' ', (size_t)len);
4042 IObuff[len] = NUL;
4043 return IObuff;
4044}
4045
4046/*
4047 * Update popup windows. They are drawn on top of normal windows.
4048 * "win_update" is called for each popup window, lowest zindex first.
4049 */
4050 void
4051update_popups(void (*win_update)(win_T *wp))
4052{
4053 win_T *wp;
4054 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004055 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004056 int total_width;
4057 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004058 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004059 int popup_attr;
4060 int border_attr[4];
4061 int border_char[8];
4062 char_u buf[MB_MAXBYTES];
4063 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004064 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004065 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004066 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004067 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02004068 int sb_thumb_top = 0;
4069 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004070 int attr_scroll = 0;
4071 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004072
Bram Moolenaarebec3e22020-11-28 20:22:06 +01004073 // hide the cursor until redrawing is done.
4074 cursor_off();
4075
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004076 // Find the window with the lowest zindex that hasn't been updated yet,
4077 // so that the window with a higher zindex is drawn later, thus goes on
4078 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01004079 popup_reset_handled(POPUP_HANDLED_5);
4080 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004081 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004082 int title_len = 0;
4083 int title_wincol;
4084
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004085 // This drawing uses the zindex of the popup window, so that it's on
4086 // top of the text but doesn't draw when another popup with higher
4087 // zindex is on top of the character.
4088 screen_zindex = wp->w_zindex;
4089
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004090 // Set flags in popup_transparent[] for masked cells.
4091 update_popup_transparent(wp, 1);
4092
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004093 // adjust w_winrow and w_wincol for border and padding, since
4094 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004095 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02004096 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004097 - wp->w_popup_leftoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004098 if (wp->w_wincol + left_extra < 0)
4099 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004100 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004101 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004102
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004103 // Draw the popup text, unless it's off screen.
4104 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004105 {
Bram Moolenaar10476522020-09-24 22:57:31 +02004106 // May need to update the "cursorline" highlighting, which may also
4107 // change "topline"
4108 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4109 popup_highlight_curline(wp);
4110
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004111 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004112
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004113 // move the cursor into the visible lines, otherwise executing
4114 // commands with win_execute() may cause the text to jump.
4115 if (wp->w_cursor.lnum < wp->w_topline)
4116 wp->w_cursor.lnum = wp->w_topline;
4117 else if (wp->w_cursor.lnum >= wp->w_botline)
4118 wp->w_cursor.lnum = wp->w_botline - 1;
4119 }
4120
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004121 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004122 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004123
4124 // Add offset for border and padding if not done already.
4125 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4126 {
4127 wp->w_wcol += left_extra;
4128 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4129 }
4130 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004131 {
4132 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004133 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004134 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004135
Bram Moolenaareabddc42022-04-02 15:32:16 +01004136 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004137 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004138 popup_attr = get_wcr_attr(wp);
4139
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004140 if (wp->w_winrow + total_height > cmdline_row)
4141 wp->w_popup_flags |= POPF_ON_CMDLINE;
4142 else
4143 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4144
4145
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004146 // We can only use these line drawing characters when 'encoding' is
4147 // "utf-8" and 'ambiwidth' is "single".
4148 if (enc_utf8 && *p_ambw == 's')
4149 {
4150 border_char[0] = border_char[2] = 0x2550;
4151 border_char[1] = border_char[3] = 0x2551;
4152 border_char[4] = 0x2554;
4153 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004154 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4155 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004156 border_char[7] = 0x255a;
4157 }
4158 else
4159 {
4160 border_char[0] = border_char[2] = '-';
4161 border_char[1] = border_char[3] = '|';
4162 for (i = 4; i < 8; ++i)
4163 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004164 if (wp->w_popup_flags & POPF_RESIZE)
4165 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004166 }
4167 for (i = 0; i < 8; ++i)
4168 if (wp->w_border_char[i] != 0)
4169 border_char[i] = wp->w_border_char[i];
4170
4171 for (i = 0; i < 4; ++i)
4172 {
4173 border_attr[i] = popup_attr;
4174 if (wp->w_border_highlight[i] != NULL)
4175 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4176 }
4177
Bram Moolenaard91467f2020-11-21 12:42:09 +01004178 // Title goes on top of border or padding.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004179 title_wincol = wp->w_wincol + 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004180 if (wp->w_popup_title != NULL)
4181 {
rbtnnc6119412021-08-07 13:08:45 +02004182 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004183
Ralf Schandlbc869872021-05-28 14:12:14 +02004184 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004185 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004186 {
4187 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4188 char_u *title_text = alloc(title_byte_len + 1);
4189
4190 if (title_text != NULL)
4191 {
4192 trunc_string(wp->w_popup_title, title_text,
4193 total_width - 2, title_byte_len + 1);
4194 screen_puts(title_text, wp->w_winrow, title_wincol,
4195 wp->w_popup_border[0] > 0
4196 ? border_attr[0] : popup_attr);
4197 vim_free(title_text);
4198 }
4199
Bram Moolenaard91467f2020-11-21 12:42:09 +01004200 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004201 }
4202 else
4203 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4204 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004205 }
4206
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004207 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004208 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004209 if (wp->w_popup_border[0] > 0)
4210 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004211 // top border; do not draw over the title
4212 if (title_len > 0)
4213 {
4214 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4215 wincol < 0 ? 0 : wincol, title_wincol,
4216 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004217 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004218 border_char[0], border_attr[0]);
4219 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4220 title_wincol + title_len, wincol + total_width,
4221 border_char[0], border_char[0], border_attr[0]);
4222 }
4223 else
4224 {
4225 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4226 wincol < 0 ? 0 : wincol, wincol + total_width,
4227 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4228 ? border_char[4] : border_char[0],
4229 border_char[0], border_attr[0]);
4230 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004231 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004232 {
4233 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4234 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004235 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004236 }
4237 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004238 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4239 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004240
Bram Moolenaard529ba52019-07-02 23:13:53 +02004241 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4242 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004243 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004244 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004245 - wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004246 if (padcol < 0)
4247 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004248 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004249 padcol = 0;
4250 }
4251 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004252 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004253 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004254 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004255 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004256 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004257 // top padding and no border; do not draw over the title
4258 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004259 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004260 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004261 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004262 row += 1;
4263 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004264 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004265 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004266 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004267 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004268
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004269 // Compute scrollbar thumb position and size.
4270 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004271 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004272 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4273 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004274 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004275
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004276 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4277 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004278 if (wp->w_topline > 1 && sb_thumb_height == height)
4279 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004280 if (sb_thumb_height == 0)
4281 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004282 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004283 // it just fits, avoid divide by zero
4284 sb_thumb_top = 0;
4285 else
4286 sb_thumb_top = (wp->w_topline - 1
4287 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004288 * (wp->w_height - sb_thumb_height)
4289 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004290 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4291 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004292 last = total_height - top_off - wp->w_popup_border[2];
4293 if (sb_thumb_top >= last)
4294 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004295 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004296
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004297 if (wp->w_scrollbar_highlight != NULL)
4298 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4299 else
4300 attr_scroll = highlight_attr[HLF_PSB];
4301 if (wp->w_thumb_highlight != NULL)
4302 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4303 else
4304 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004305 }
4306
4307 for (i = wp->w_popup_border[0];
4308 i < total_height - wp->w_popup_border[2]; ++i)
4309 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004310 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004311 // left and right padding only needed next to the body
4312 int do_padding =
4313 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4314 && i < total_height - wp->w_popup_border[2]
4315 - wp->w_popup_padding[2];
4316
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004317 row = wp->w_winrow + i;
4318
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004319 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004320 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004321 {
4322 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004323 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004324 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004325 if (do_padding && wp->w_popup_padding[3] > 0)
4326 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004327 int col = wincol + wp->w_popup_border[3];
4328
Bram Moolenaard529ba52019-07-02 23:13:53 +02004329 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004330 pad_left = wp->w_popup_padding[3];
4331 if (col < 0)
4332 {
4333 pad_left += col;
4334 col = 0;
4335 }
4336 if (pad_left > 0)
4337 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4338 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004339 // scrollbar
4340 if (wp->w_has_scrollbar)
4341 {
4342 int line = i - top_off;
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004343 int scroll_col = wp->w_wincol + total_width - 1
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004344 - wp->w_popup_border[1];
4345
4346 if (line >= 0 && line < wp->w_height)
4347 screen_putchar(' ', row, scroll_col,
4348 line >= sb_thumb_top
4349 && line < sb_thumb_top + sb_thumb_height
4350 ? attr_thumb : attr_scroll);
4351 else
4352 screen_putchar(' ', row, scroll_col, popup_attr);
4353 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004354 // right border
4355 if (wp->w_popup_border[1] > 0)
4356 {
4357 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004358 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004359 }
4360 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004361 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004362 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004363 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004364 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4365 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004366 }
4367
4368 if (wp->w_popup_padding[2] > 0)
4369 {
4370 // bottom padding
4371 row = wp->w_winrow + wp->w_popup_border[0]
4372 + wp->w_popup_padding[0] + wp->w_height;
4373 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004374 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004375 }
4376
4377 if (wp->w_popup_border[2] > 0)
4378 {
4379 // bottom border
4380 row = wp->w_winrow + total_height - 1;
4381 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004382 wincol < 0 ? 0 : wincol,
4383 wincol + total_width,
4384 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004385 ? border_char[7] : border_char[2],
4386 border_char[2], border_attr[2]);
4387 if (wp->w_popup_border[1] > 0)
4388 {
4389 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004390 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004391 }
4392 }
4393
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004394 if (wp->w_popup_close == POPCLOSE_BUTTON)
4395 {
4396 // close button goes on top of anything at the top-right corner
4397 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004398 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004399 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4400 }
4401
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004402 update_popup_transparent(wp, 0);
4403
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004404 // Back to the normal zindex.
4405 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004406
4407#ifdef HAS_MESSAGE_WINDOW
4408 // if this was the message window popup may start the timer now
4409 may_start_message_win_timer(wp);
4410#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004411 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004412
4413#if defined(FEAT_SEARCH_EXTRA)
4414 // In case win_update() called start_search_hl().
4415 end_search_hl();
4416#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004417}
4418
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004419/*
4420 * Mark references in callbacks of one popup window.
4421 */
4422 static int
4423set_ref_in_one_popup(win_T *wp, int copyID)
4424{
4425 int abort = FALSE;
4426 typval_T tv;
4427
4428 if (wp->w_close_cb.cb_partial != NULL)
4429 {
4430 tv.v_type = VAR_PARTIAL;
4431 tv.vval.v_partial = wp->w_close_cb.cb_partial;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004432 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL, NULL);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004433 }
4434 if (wp->w_filter_cb.cb_partial != NULL)
4435 {
4436 tv.v_type = VAR_PARTIAL;
4437 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004438 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL, NULL);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004439 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004440 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004441 return abort;
4442}
4443
4444/*
4445 * Set reference in callbacks of popup windows.
4446 */
4447 int
4448set_ref_in_popups(int copyID)
4449{
4450 int abort = FALSE;
4451 win_T *wp;
4452 tabpage_T *tp;
4453
4454 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4455 abort = abort || set_ref_in_one_popup(wp, copyID);
4456
4457 FOR_ALL_TABPAGES(tp)
4458 {
4459 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4460 abort = abort || set_ref_in_one_popup(wp, copyID);
4461 if (abort)
4462 break;
4463 }
4464 return abort;
4465}
Bram Moolenaar79648732019-07-18 21:43:07 +02004466
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004467 int
4468popup_is_popup(win_T *wp)
4469{
4470 return wp->w_popup_flags != 0;
4471}
4472
4473#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004474/*
4475 * Find an existing popup used as the preview window, in the current tab page.
4476 * Return NULL if not found.
4477 */
4478 win_T *
4479popup_find_preview_window(void)
4480{
4481 win_T *wp;
4482
4483 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004484 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004485 if (wp->w_p_pvw)
4486 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004487 return NULL;
4488}
4489
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004490/*
4491 * Find an existing popup used as the info window, in the current tab page.
4492 * Return NULL if not found.
4493 */
4494 win_T *
4495popup_find_info_window(void)
4496{
4497 win_T *wp;
4498
4499 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004500 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004501 if (wp->w_popup_flags & POPF_INFO)
4502 return wp;
4503 return NULL;
4504}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004505#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004506
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004507 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004508f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4509{
4510#ifdef HAS_MESSAGE_WINDOW
4511 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4512#else
4513 rettv->vval.v_number = 0;
4514#endif
4515}
4516
4517 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004518f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4519{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004520#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004521 win_T *wp = popup_find_info_window();
4522
4523 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004524#else
4525 rettv->vval.v_number = 0;
4526#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004527}
4528
4529 void
4530f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004531{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004532#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004533 win_T *wp = popup_find_preview_window();
4534
4535 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004536#else
4537 rettv->vval.v_number = 0;
4538#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004539}
4540
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004541#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004542/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004543 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004544 * NOTE: this makes the popup the current window, so that the file can be
4545 * edited. However, it must not remain to be the current window, the caller
4546 * must make sure of that.
4547 */
4548 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004549popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004550{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004551 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004552
4553 if (wp == NULL)
4554 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004555 if (info)
4556 wp->w_popup_flags |= POPF_INFO;
4557 else
4558 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004559
4560 // Set the width to a reasonable value, so that w_topline can be computed.
4561 if (wp->w_minwidth > 0)
4562 wp->w_width = wp->w_minwidth;
4563 else if (wp->w_maxwidth > 0)
4564 wp->w_width = wp->w_maxwidth;
4565 else
4566 wp->w_width = curwin->w_width;
4567
4568 // Will switch to another buffer soon, dummy one can be wiped.
4569 wp->w_buffer->b_locked = FALSE;
4570
4571 win_enter(wp, FALSE);
4572 return OK;
4573}
4574
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004575/*
4576 * Close any preview popup.
4577 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004578 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004579popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004580{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004581 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004582
4583 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004584 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004585}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004586
4587/*
4588 * Hide the info popup.
4589 */
4590 void
4591popup_hide_info(void)
4592{
4593 win_T *wp = popup_find_info_window();
4594
4595 if (wp != NULL)
glepnirff159252025-03-01 16:17:00 +01004596 {
4597 popup_on_cmdline = wp->w_popup_flags & POPF_ON_CMDLINE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004598 popup_hide(wp);
glepnirff159252025-03-01 16:17:00 +01004599 }
4600}
4601
4602/*
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004603 * Close any info popup.
4604 */
4605 void
4606popup_close_info(void)
4607{
4608 win_T *wp = popup_find_info_window();
4609
4610 if (wp != NULL)
4611 popup_close_with_retval(wp, -1);
4612}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004613#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004614
John Marriott45377e22025-03-27 18:12:32 +01004615/*
4616 * Returns TRUE if a popup extends into the cmdline area.
4617 */
4618 int
4619popup_overlaps_cmdline(void)
4620{
4621 return popup_on_cmdline;
4622}
4623
Bram Moolenaar9198de32022-08-27 21:30:03 +01004624#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4625
Bram Moolenaar9198de32022-08-27 21:30:03 +01004626/*
4627 * Get the message window.
4628 * Returns NULL if something failed.
4629 */
4630 win_T *
4631popup_get_message_win(void)
4632{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004633 if (message_win != NULL)
4634 return message_win;
4635
4636 int i;
4637
4638 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4639
Bram Moolenaar9198de32022-08-27 21:30:03 +01004640 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004641 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004642
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004643 // use the full screen width
4644 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004645
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004646 // position at bottom of screen
4647 message_win->w_popup_pos = POPPOS_BOTTOM;
4648 message_win->w_wantcol = 1;
4649 message_win->w_minwidth = 9999;
4650 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004651
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004652 // no padding, border at the top
4653 for (i = 0; i < 4; ++i)
4654 message_win->w_popup_padding[i] = 0;
4655 for (i = 1; i < 4; ++i)
4656 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004657
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004658 if (message_win->w_popup_timer != NULL)
4659 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004660 return message_win;
4661}
4662
4663/*
4664 * If the message window is not visible: show it
4665 * If the message window is visible: reset the timeout
4666 */
4667 void
4668popup_show_message_win(void)
4669{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004670 if (message_win == NULL)
4671 return;
4672
4673 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004674 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004675 // the highlight may have changed.
4676 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4677 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004678 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004679 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004680}
4681
4682 static void
4683may_start_message_win_timer(win_T *wp)
4684{
4685 if (wp == message_win && start_message_win_timer)
4686 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004687 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004688 {
4689 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004690 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004691 message_win_time = 3000;
4692 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004693 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004694 }
4695}
4696
4697 int
4698popup_message_win_visible(void)
4699{
4700 return message_win != NULL
4701 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4702}
4703
4704/*
4705 * If the message window is visible: hide it.
4706 */
4707 void
4708popup_hide_message_win(void)
4709{
4710 if (message_win != NULL)
4711 popup_hide(message_win);
4712}
4713
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004714// Values saved in start_echowindow() and restored in end_echowindow()
4715static int save_msg_didout = FALSE;
4716static int save_msg_col = 0;
4717// Values saved in end_echowindow() and restored in start_echowindow()
4718static int ew_msg_didout = FALSE;
4719static int ew_msg_col = 0;
4720
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004721/*
4722 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004723 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004724 */
4725 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004726start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004727{
4728 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004729 save_msg_didout = msg_didout;
4730 save_msg_col = msg_col;
4731 msg_didout = ew_msg_didout;
4732 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004733 if (time_sec != 0)
4734 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004735}
4736
4737/*
4738 * Invoked after outputting a message for ":echowindow".
4739 */
4740 void
4741end_echowindow(void)
4742{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004743 in_echowindow = FALSE;
4744
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004745 if ((State & MODE_HITRETURN) == 0)
4746 // show the message window now
4747 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004748
4749 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004750 ew_msg_didout = TRUE;
4751 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4752 msg_didout = save_msg_didout;
4753 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004754}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004755#endif
4756
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004757/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004758 * Close any popup for a text property associated with "win".
4759 * Return TRUE if a popup was closed.
4760 */
4761 int
4762popup_win_closed(win_T *win)
4763{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004764 int round;
4765 win_T *wp;
4766 win_T *next;
4767 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004768
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004769 for (round = 1; round <= 2; ++round)
4770 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4771 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004772 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004773 next = wp->w_next;
4774 if (wp->w_popup_prop_win == win)
4775 {
4776 popup_close_with_retval(wp, -1);
4777 ret = TRUE;
4778 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004779 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004780 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004781}
4782
4783/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004784 * Set the title of the popup window to the file name.
4785 */
4786 void
4787popup_set_title(win_T *wp)
4788{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004789 if (wp->w_buffer->b_fname == NULL)
4790 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004791
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004792 char_u dirname[MAXPATHL];
4793 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004794
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004795 mch_dirname(dirname, MAXPATHL);
4796 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4797
4798 vim_free(wp->w_popup_title);
4799 len = STRLEN(wp->w_buffer->b_fname) + 3;
4800 wp->w_popup_title = alloc((int)len);
4801 if (wp->w_popup_title != NULL)
4802 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4803 wp->w_buffer->b_fname);
4804 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004805}
4806
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004807# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004808/*
4809 * If there is a preview window, update the title.
4810 * Used after changing directory.
4811 */
4812 void
4813popup_update_preview_title(void)
4814{
4815 win_T *wp = popup_find_preview_window();
4816
4817 if (wp != NULL)
4818 popup_set_title(wp);
4819}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004820# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004821
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004822#endif // FEAT_PROP_POPUP