blob: 199ffaf8df65e03843a4ef0f018e7763e1ffdf28 [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.
2424 */
2425 static void
2426popup_close_and_callback(win_T *wp, typval_T *arg)
2427{
2428 int id = wp->w_id;
2429
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002430#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002431 if (wp == curwin && curbuf->b_term != NULL)
2432 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002433 win_T *owp;
2434
2435 // Closing popup window with a terminal: put focus back on the first
2436 // that works:
2437 // - another popup window with a terminal
2438 // - the previous window
2439 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002440 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002441 if (owp != curwin && owp->w_buffer->b_term != NULL)
2442 break;
2443 if (owp != NULL)
2444 win_enter(owp, FALSE);
2445 else
2446 {
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002447 FOR_ALL_POPUPWINS_IN_TAB(curtab, owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002448 if (owp != curwin && owp->w_buffer->b_term != NULL)
2449 break;
2450 if (owp != NULL)
2451 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002452 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002453 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002454 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002455 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002456#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002457
Bram Moolenaar49540192019-12-11 19:34:54 +01002458 // Just in case a check higher up is missing.
2459 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002460 {
2461 // To avoid getting stuck when win_execute() does something that causes
2462 // an error, stop calling the filter callback.
2463 free_callback(&wp->w_filter_cb);
2464
Bram Moolenaar49540192019-12-11 19:34:54 +01002465 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002466 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002467
Bram Moolenaarcee52202020-03-11 14:19:58 +01002468 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002469 if (wp->w_close_cb.cb_name != NULL)
2470 // Careful: This may make "wp" invalid.
2471 invoke_popup_callback(wp, arg);
2472
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002473 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002474 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002475}
2476
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002477 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002478popup_close_with_retval(win_T *wp, int retval)
2479{
2480 typval_T res;
2481
2482 res.v_type = VAR_NUMBER;
2483 res.vval.v_number = retval;
2484 popup_close_and_callback(wp, &res);
2485}
2486
Bram Moolenaar3397f742019-06-02 18:40:06 +02002487/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002488 * Close popup "wp" because of a mouse click.
2489 */
2490 void
2491popup_close_for_mouse_click(win_T *wp)
2492{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002493 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002494}
2495
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002496 static void
2497check_mouse_moved(win_T *wp, win_T *mouse_wp)
2498{
2499 // Close the popup when all if these are true:
2500 // - the mouse is not on this popup
2501 // - "mousemoved" was used
2502 // - the mouse is no longer on the same screen row or the mouse column is
2503 // outside of the relevant text
2504 if (wp != mouse_wp
2505 && wp->w_popup_mouse_row != 0
2506 && (wp->w_popup_mouse_row != mouse_row
2507 || mouse_col < wp->w_popup_mouse_mincol
2508 || mouse_col > wp->w_popup_mouse_maxcol))
2509 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002510 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002511 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002512 }
2513}
2514
2515/*
2516 * Called when the mouse moved: may close a popup with "mousemoved".
2517 */
2518 void
2519popup_handle_mouse_moved(void)
2520{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002521 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002522 win_T *mouse_wp;
2523 int row = mouse_row;
2524 int col = mouse_col;
2525
2526 // find the window where the mouse is in
2527 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2528
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002529 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2530 {
2531 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002532 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002533 }
2534 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2535 {
2536 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002537 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002538 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002539}
2540
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002541/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002542 * In a filter: check if the typed key is a mouse event that is used for
2543 * dragging the popup.
2544 */
2545 static void
2546filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2547{
2548 int row = mouse_row;
2549 int col = mouse_col;
2550
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002551 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002552 && is_mouse_key(c)
2553 && (wp == popup_dragwin
2554 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2555 // do not consume the key, allow for dragging the popup
2556 rettv->vval.v_number = 0;
2557}
2558
Bram Moolenaara730e552019-06-16 19:05:31 +02002559/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002560 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002561 */
2562 void
2563f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2564{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002565 int id;
2566 win_T *wp;
2567 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002568 typval_T res;
2569 int c;
2570 linenr_T old_lnum;
2571
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002572 if (in_vim9script()
2573 && (check_for_number_arg(argvars, 0) == FAIL
2574 || check_for_string_arg(argvars, 1) == FAIL))
2575 return;
2576
2577 id = tv_get_number(&argvars[0]);
2578 wp = win_id2wp(id);
2579 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002580 // If the popup has been closed do not consume the key.
2581 if (wp == NULL)
2582 return;
2583
2584 c = *key;
2585 if (c == K_SPECIAL && key[1] != NUL)
2586 c = TO_SPECIAL(key[1], key[2]);
2587
2588 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002589 rettv->v_type = VAR_BOOL;
2590 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002591 res.v_type = VAR_NUMBER;
2592
2593 old_lnum = wp->w_cursor.lnum;
Christian Brabandtbadeedd2023-08-13 19:25:28 +02002594 if (c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2595 {
2596 if (wp->w_cursor.lnum > 1)
2597 --wp->w_cursor.lnum;
2598 else
2599 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2600 }
2601 if (c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
2602 {
2603 if (wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2604 ++wp->w_cursor.lnum;
2605 else
2606 wp->w_cursor.lnum = 1;
2607 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002608 if (old_lnum != wp->w_cursor.lnum)
2609 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002610 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002611 return;
2612 }
2613
2614 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2615 {
2616 // Cancelled, invoke callback with -1
2617 res.vval.v_number = -1;
2618 popup_close_and_callback(wp, &res);
2619 return;
2620 }
2621 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2622 {
2623 // Invoke callback with current index.
2624 res.vval.v_number = wp->w_cursor.lnum;
2625 popup_close_and_callback(wp, &res);
2626 return;
2627 }
2628
2629 filter_handle_drag(wp, c, rettv);
2630}
2631
2632/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002633 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002634 */
2635 void
2636f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2637{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002638 int id;
2639 win_T *wp;
2640 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002641 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002642 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002643
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002644 if (in_vim9script()
2645 && (check_for_number_arg(argvars, 0) == FAIL
2646 || check_for_string_arg(argvars, 1) == FAIL))
2647 return;
2648
2649 id = tv_get_number(&argvars[0]);
2650 wp = win_id2wp(id);
2651 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002652 // If the popup has been closed don't consume the key.
2653 if (wp == NULL)
2654 return;
2655
Bram Moolenaara730e552019-06-16 19:05:31 +02002656 c = *key;
Ernie Raelb14c3252024-07-19 16:37:09 +02002657 if (c == CAR && need_wait_return)
2658 return;
Bram Moolenaara730e552019-06-16 19:05:31 +02002659 if (c == K_SPECIAL && key[1] != NUL)
2660 c = TO_SPECIAL(key[1], key[2]);
2661
Bram Moolenaara42d9452019-06-15 21:46:30 +02002662 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002663 rettv->v_type = VAR_BOOL;
2664 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002665
Bram Moolenaara730e552019-06-16 19:05:31 +02002666 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002667 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002668 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002669 res.vval.v_number = 0;
2670 else
2671 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002672 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002673 return;
2674 }
2675
2676 // Invoke callback
2677 res.v_type = VAR_NUMBER;
2678 popup_close_and_callback(wp, &res);
2679}
2680
2681/*
2682 * popup_dialog({text}, {options})
2683 */
2684 void
2685f_popup_dialog(typval_T *argvars, typval_T *rettv)
2686{
2687 popup_create(argvars, rettv, TYPE_DIALOG);
2688}
2689
2690/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002691 * popup_menu({text}, {options})
2692 */
2693 void
2694f_popup_menu(typval_T *argvars, typval_T *rettv)
2695{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002696 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002697}
2698
2699/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002700 * popup_notification({text}, {options})
2701 */
2702 void
2703f_popup_notification(typval_T *argvars, typval_T *rettv)
2704{
2705 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2706}
2707
2708/*
2709 * Find the popup window with window-ID "id".
2710 * If the popup window does not exist NULL is returned.
2711 * If the window is not a popup window, and error message is given.
2712 */
2713 static win_T *
2714find_popup_win(int id)
2715{
2716 win_T *wp = win_id2wp(id);
2717
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002718 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002719 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002720 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002721 return NULL;
2722 }
2723 return wp;
2724}
2725
2726/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002727 * popup_close({id})
2728 */
2729 void
2730f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2731{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002732 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002733 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002734
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002735 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2736 return;
2737
2738 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002739 if (
2740# ifdef FEAT_TERMINAL
2741 // if the popup contains a terminal it will become hidden
2742 curbuf->b_term == NULL &&
2743# endif
2744 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002745 return;
2746
2747 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002748 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002749 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002750}
2751
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002752 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002753popup_hide(win_T *wp)
2754{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002755#ifdef FEAT_TERMINAL
2756 if (error_if_term_popup_window())
2757 return;
2758#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002759 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2760 return;
2761
2762 wp->w_popup_flags |= POPF_HIDDEN;
2763 // Do not decrement b_nwindows, we still reference the buffer.
Christian Brabandtf00f4d92024-09-03 18:20:13 +02002764 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
2765 clear_cmdline = TRUE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002766 redraw_all_later(UPD_NOT_VALID);
2767 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002768}
2769
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002770/*
2771 * popup_hide({id})
2772 */
2773 void
2774f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2775{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002776 int id;
2777 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002778
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002779 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2780 return;
2781
2782 id = (int)tv_get_number(argvars);
2783 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002784 if (wp == NULL)
2785 return;
2786
2787 popup_hide(wp);
2788 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002789}
2790
2791 void
2792popup_show(win_T *wp)
2793{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002794 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2795 return;
2796
2797 wp->w_popup_flags &= ~POPF_HIDDEN;
2798 redraw_all_later(UPD_NOT_VALID);
2799 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002800}
2801
2802/*
2803 * popup_show({id})
2804 */
2805 void
2806f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2807{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002808 int id;
2809 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002810
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002811 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2812 return;
2813
2814 id = (int)tv_get_number(argvars);
2815 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002816 if (wp == NULL)
2817 return;
2818
2819 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2820 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002821#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002822 if (wp->w_popup_flags & POPF_INFO)
2823 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002824#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002825}
2826
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002827/*
2828 * popup_settext({id}, {text})
2829 */
2830 void
2831f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2832{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002833 int id;
2834 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002835
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002836 if (in_vim9script()
2837 && (check_for_number_arg(argvars, 0) == FAIL
2838 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2839 return;
2840
2841 id = (int)tv_get_number(&argvars[0]);
2842 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002843 if (wp == NULL)
2844 return;
2845
2846 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2847 return;
2848
2849 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2850 redraw_win_later(wp, UPD_NOT_VALID);
2851 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002852}
2853
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002854/*
2855 * popup_setbuf({id}, {bufnr})
2856 */
2857 void
2858f_popup_setbuf(typval_T *argvars, typval_T *rettv UNUSED)
2859{
2860 int id;
2861 win_T *wp;
2862 buf_T *buf;
2863
2864 rettv->v_type = VAR_BOOL;
2865 rettv->vval.v_number = VVAL_FALSE;
2866
2867 if (check_for_number_arg(argvars, 0) == FAIL
2868 || check_for_buffer_arg(argvars, 1) == FAIL)
2869 return;
2870
2871 id = (int)tv_get_number(&argvars[0]);
2872 wp = find_popup_win(id);
2873 if (wp == NULL)
2874 return;
2875
2876 buf = tv_get_buf_from_arg(&argvars[1]);
2877
2878 if (buf == NULL)
2879 return;
2880#ifdef FEAT_TERMINAL
2881 if (buf->b_term != NULL && popup_terminal_exists())
2882 {
2883 emsg(_(e_cannot_open_second_popup_with_terminal));
2884 return;
2885 }
2886#endif
2887
2888 if (wp->w_buffer != buf)
2889 {
2890 wp->w_buffer->b_nwindows--;
2891 win_init_popup_win(wp, buf);
2892 set_local_options_default(wp, FALSE);
2893 swap_exists_action = SEA_READONLY;
2894 buffer_ensure_loaded(buf);
2895 swap_exists_action = SEA_NONE;
2896 redraw_win_later(wp, UPD_NOT_VALID);
2897 popup_adjust_position(wp);
2898 }
2899 rettv->vval.v_number = VVAL_TRUE;
2900}
2901
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002902 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002903popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002904{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002905 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002906 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002907 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002908 clear_cmdline = TRUE;
2909 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002910
Bram Moolenaar43568642022-08-28 13:02:45 +01002911#ifdef HAS_MESSAGE_WINDOW
2912 if (wp == message_win)
2913 message_win = NULL;
2914#endif
2915
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002916 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002917 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002918}
2919
Bram Moolenaar82106932020-03-13 14:34:38 +01002920 static void
2921error_for_popup_window(void)
2922{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002923 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002924}
2925
2926 int
2927error_if_popup_window(int also_with_term UNUSED)
2928{
2929 // win_execute() may set "curwin" to a popup window temporarily, but many
2930 // commands are disallowed then. When a terminal runs in the popup most
2931 // things are allowed. When a terminal is finished it can be closed.
2932 if (WIN_IS_POPUP(curwin)
2933# ifdef FEAT_TERMINAL
2934 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002935# endif
2936 )
2937 {
2938 error_for_popup_window();
2939 return TRUE;
2940 }
2941 return FALSE;
2942}
2943
Bram Moolenaarec583842019-05-26 14:11:23 +02002944/*
2945 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002946 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002947 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002948 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002949 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002950popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002951{
2952 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002953 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002954 win_T *prev = NULL;
2955
Bram Moolenaarec583842019-05-26 14:11:23 +02002956 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002957 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002958 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002959 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002960 if (wp == curwin)
2961 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002962 if (!force)
2963 {
2964 error_for_popup_window();
2965 return FAIL;
2966 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002967 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002968 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002969 if (prev == NULL)
2970 first_popupwin = wp->w_next;
2971 else
2972 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002973 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002974 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002975 }
2976
Bram Moolenaarec583842019-05-26 14:11:23 +02002977 // go through tab-local popups
2978 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002979 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002980 return OK;
2981 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002982}
2983
2984/*
2985 * Close a popup window with Window-id "id" in tabpage "tp".
2986 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002987 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002988popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002989{
2990 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002991 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002992 win_T *prev = NULL;
2993
Bram Moolenaarec583842019-05-26 14:11:23 +02002994 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2995 if (wp->w_id == id)
2996 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002997 if (wp == curwin)
2998 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002999 if (!force)
3000 {
3001 error_for_popup_window();
3002 return FAIL;
3003 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003004 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01003005 }
Bram Moolenaarec583842019-05-26 14:11:23 +02003006 if (prev == NULL)
3007 *root = wp->w_next;
3008 else
3009 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02003010 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02003011 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02003012 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02003013 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003014}
3015
3016 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003017close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003018{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003019 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003020 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003021 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003022 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003023 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003024 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003025 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003026 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003027}
3028
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003029/*
3030 * popup_move({id}, {options})
3031 */
3032 void
3033f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
3034{
Bram Moolenaarae943152019-06-16 22:54:14 +02003035 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003036 int id;
3037 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003038
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003039 if (in_vim9script()
3040 && (check_for_number_arg(argvars, 0) == FAIL
3041 || check_for_dict_arg(argvars, 1) == FAIL))
3042 return;
3043
3044 id = (int)tv_get_number(argvars);
3045 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003046 if (wp == NULL)
3047 return; // invalid {id}
3048
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003049 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003050 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003051 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003052
Bram Moolenaarae943152019-06-16 22:54:14 +02003053 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003054
3055 if (wp->w_winrow + wp->w_height >= cmdline_row)
3056 clear_cmdline = TRUE;
3057 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003058}
3059
Bram Moolenaarbc133542019-05-29 20:26:46 +02003060/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003061 * popup_setoptions({id}, {options})
3062 */
3063 void
3064f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
3065{
3066 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003067 int id;
3068 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003069 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003070
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003071 if (in_vim9script()
3072 && (check_for_number_arg(argvars, 0) == FAIL
3073 || check_for_dict_arg(argvars, 1) == FAIL))
3074 return;
3075
3076 id = (int)tv_get_number(argvars);
3077 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02003078 if (wp == NULL)
3079 return; // invalid {id}
3080
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003081 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02003082 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003083 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003084 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003085
Christian Brabandtf6cdab32023-08-11 23:42:02 +02003086 (void)apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02003087
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003088 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003089 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003090 popup_adjust_position(wp);
3091}
3092
3093/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003094 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003095 */
3096 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003097f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003098{
3099 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003100 int id;
3101 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003102 int top_extra;
3103 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003104
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003105 if (rettv_dict_alloc(rettv) == FAIL)
3106 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003107
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003108 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3109 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003110
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003111 id = (int)tv_get_number(argvars);
3112 wp = find_popup_win(id);
3113 if (wp == NULL)
3114 return; // invalid {id}
3115 top_extra = popup_top_extra(wp);
3116 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003117
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003118 // we know how much space we need, avoid resizing halfway
3119 dict = rettv->vval.v_dict;
3120 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003121
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003122 dict_add_number(dict, "line", wp->w_winrow + 1);
3123 dict_add_number(dict, "col", wp->w_wincol + 1);
3124 dict_add_number(dict, "width", wp->w_width + left_extra
3125 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3126 dict_add_number(dict, "height", wp->w_height + top_extra
3127 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003128
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003129 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3130 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3131 dict_add_number(dict, "core_width", wp->w_width);
3132 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003133
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003134 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3135 dict_add_number(dict, "firstline", wp->w_topline);
3136 dict_add_number(dict, "lastline", wp->w_botline - 1);
3137 dict_add_number(dict, "visible",
3138 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3139
3140 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003141}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003142
3143/*
3144 * popup_list()
3145 */
3146 void
3147f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3148{
3149 win_T *wp;
3150 tabpage_T *tp;
3151
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003152 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003153 return;
3154 FOR_ALL_POPUPWINS(wp)
3155 list_append_number(rettv->vval.v_list, wp->w_id);
3156 FOR_ALL_TABPAGES(tp)
3157 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3158 list_append_number(rettv->vval.v_list, wp->w_id);
3159}
3160
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003161/*
3162 * popup_locate({row}, {col})
3163 */
3164 void
3165f_popup_locate(typval_T *argvars, typval_T *rettv)
3166{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003167 int row;
3168 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003169 win_T *wp;
3170
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003171 if (in_vim9script()
3172 && (check_for_number_arg(argvars, 0) == FAIL
3173 || check_for_number_arg(argvars, 1) == FAIL))
3174 return;
3175
3176 row = tv_get_number(&argvars[0]) - 1;
3177 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003178 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003179 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003180 rettv->vval.v_number = wp->w_id;
3181}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003182
3183/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003184 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3185 */
3186 static void
3187get_padding_border(dict_T *dict, int *array, char *name)
3188{
3189 list_T *list;
3190 int i;
3191
3192 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3193 return;
3194
3195 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003196 if (list == NULL)
3197 return;
3198
3199 dict_add_list(dict, name, list);
3200 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3201 for (i = 0; i < 4; ++i)
3202 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003203}
3204
3205/*
3206 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3207 */
3208 static void
3209get_borderhighlight(dict_T *dict, win_T *wp)
3210{
3211 list_T *list;
3212 int i;
3213
3214 for (i = 0; i < 4; ++i)
3215 if (wp->w_border_highlight[i] != NULL)
3216 break;
3217 if (i == 4)
3218 return;
3219
3220 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003221 if (list == NULL)
3222 return;
3223
3224 dict_add_list(dict, "borderhighlight", list);
3225 for (i = 0; i < 4; ++i)
3226 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003227}
3228
3229/*
3230 * For popup_getoptions(): add a "borderchars" entry to "dict".
3231 */
3232 static void
3233get_borderchars(dict_T *dict, win_T *wp)
3234{
3235 list_T *list;
3236 int i;
3237 char_u buf[NUMBUFLEN];
3238 int len;
3239
3240 for (i = 0; i < 8; ++i)
3241 if (wp->w_border_char[i] != 0)
3242 break;
3243 if (i == 8)
3244 return;
3245
3246 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003247 if (list == NULL)
3248 return;
3249
3250 dict_add_list(dict, "borderchars", list);
3251 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003252 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003253 len = mb_char2bytes(wp->w_border_char[i], buf);
3254 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003255 }
3256}
3257
3258/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003259 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003260 */
3261 static void
3262get_moved_list(dict_T *dict, win_T *wp)
3263{
3264 list_T *list;
3265
3266 list = list_alloc();
3267 if (list != NULL)
3268 {
3269 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003270 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003271 list_append_number(list, wp->w_popup_mincol);
3272 list_append_number(list, wp->w_popup_maxcol);
3273 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003274 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003275 if (list == NULL)
3276 return;
3277
3278 dict_add_list(dict, "mousemoved", list);
3279 list_append_number(list, wp->w_popup_mouse_row);
3280 list_append_number(list, wp->w_popup_mouse_mincol);
3281 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003282}
3283
3284/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003285 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003286 */
3287 void
3288f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3289{
3290 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003291 int id;
3292 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003293 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003294 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003295
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003296 if (rettv_dict_alloc(rettv) == FAIL)
3297 return;
3298
3299 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3300 return;
3301
3302 id = (int)tv_get_number(argvars);
3303 wp = find_popup_win(id);
3304 if (wp == NULL)
3305 return;
3306
3307 dict = rettv->vval.v_dict;
3308 dict_add_number(dict, "line", wp->w_wantline);
3309 dict_add_number(dict, "col", wp->w_wantcol);
3310 dict_add_number(dict, "minwidth", wp->w_minwidth);
3311 dict_add_number(dict, "minheight", wp->w_minheight);
3312 dict_add_number(dict, "maxheight", wp->w_maxheight);
3313 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3314 dict_add_number(dict, "firstline", wp->w_firstline);
3315 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3316 dict_add_number(dict, "zindex", wp->w_zindex);
3317 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3318 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003319 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003320 proptype_T *pt = text_prop_type_by_id(
3321 wp->w_popup_prop_win->w_buffer,
3322 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003323
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003324 if (pt != NULL)
3325 dict_add_string(dict, "textprop", pt->pt_name);
3326 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3327 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3328 }
3329 dict_add_string(dict, "title", wp->w_popup_title);
3330 dict_add_number(dict, "wrap", wp->w_p_wrap);
3331 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3332 dict_add_number(dict, "dragall",
3333 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3334 dict_add_number(dict, "mapping",
3335 (wp->w_popup_flags & POPF_MAPPING) != 0);
3336 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3337 dict_add_number(dict, "posinvert",
3338 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3339 dict_add_number(dict, "cursorline",
3340 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3341 dict_add_string(dict, "highlight", wp->w_p_wcr);
3342 if (wp->w_scrollbar_highlight != NULL)
3343 dict_add_string(dict, "scrollbarhighlight",
3344 wp->w_scrollbar_highlight);
3345 if (wp->w_thumb_highlight != NULL)
3346 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003347
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003348 // find the tabpage that holds this popup
3349 i = 1;
3350 FOR_ALL_TABPAGES(tp)
3351 {
3352 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003353
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003354 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3355 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003356 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003357 if (twp != NULL)
3358 break;
3359 ++i;
3360 }
3361 if (tp == NULL)
3362 i = -1; // must be global
3363 else if (tp == curtab)
3364 i = 0;
3365 dict_add_number(dict, "tabpage", i);
3366
3367 get_padding_border(dict, wp->w_popup_padding, "padding");
3368 get_padding_border(dict, wp->w_popup_border, "border");
3369 get_borderhighlight(dict, wp);
3370 get_borderchars(dict, wp);
3371 get_moved_list(dict, wp);
3372
3373 if (wp->w_filter_cb.cb_name != NULL)
3374 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3375 if (wp->w_close_cb.cb_name != NULL)
3376 dict_add_callback(dict, "callback", &wp->w_close_cb);
3377
3378 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3379 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3380 {
3381 dict_add_string(dict, "pos",
3382 (char_u *)poppos_entries[i].pp_name);
3383 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003384 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003385
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003386 dict_add_string(dict, "close", (char_u *)(
3387 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3388 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003389
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003390# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003391 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3392 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003393# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003394}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003395
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003396# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003397/*
3398 * Return TRUE if the current window is running a terminal in a popup window.
3399 * Return FALSE when the job has ended.
3400 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003401 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003402error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003403{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003404 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3405 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003406 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003407 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003408 return TRUE;
3409 }
3410 return FALSE;
3411}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003412# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003413
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003414/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003415 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003416 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003417 * Each calling function should use a different flag, see the list at
3418 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003419 */
3420 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003421popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003422{
3423 win_T *wp;
3424
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003425 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003426 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003427 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003428 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003429}
3430
3431/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003432 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003433 * Must have called popup_reset_handled() first.
3434 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3435 * popup with the highest zindex.
3436 */
3437 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003438find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003439{
3440 win_T *wp;
3441 win_T *found_wp;
3442 int found_zindex;
3443
3444 found_zindex = lowest ? INT_MAX : 0;
3445 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003446 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003447 if ((wp->w_popup_handled & handled_flag) == 0
3448 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003449 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003450 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003451 {
3452 found_zindex = wp->w_zindex;
3453 found_wp = wp;
3454 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003455 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003456 if ((wp->w_popup_handled & handled_flag) == 0
3457 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003458 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003459 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003460 {
3461 found_zindex = wp->w_zindex;
3462 found_wp = wp;
3463 }
3464
3465 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003466 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003467 return found_wp;
3468}
3469
3470/*
3471 * Invoke the filter callback for window "wp" with typed character "c".
3472 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003473 * Returns the return value of the filter or -1 for CTRL-C in the current
3474 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003475 * Careful: The filter may make "wp" invalid!
3476 */
3477 static int
3478invoke_popup_filter(win_T *wp, int c)
3479{
3480 int res;
3481 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003482 typval_T argv[3];
3483 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003484 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003485 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003486
Bram Moolenaara42d9452019-06-15 21:46:30 +02003487 // Emergency exit: CTRL-C closes the popup.
3488 if (c == Ctrl_C)
3489 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003490 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003491 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003492
3493 // Reset got_int to avoid the callback isn't called.
3494 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003495 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003496 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003497
3498 // If the popup is the current window it probably fails to close. Then
3499 // do not consume the key.
3500 if (was_curwin && wp == curwin)
3501 return -1;
3502 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003503 }
3504
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003505 argv[0].v_type = VAR_NUMBER;
3506 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3507
3508 // Convert the number to a string, so that the function can use:
3509 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003510 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003511 argv[1].v_type = VAR_STRING;
3512 argv[1].vval.v_string = vim_strsave(buf);
3513
3514 argv[2].v_type = VAR_UNKNOWN;
3515
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003516 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003517 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3518 {
3519 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003520 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003521 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003522 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003523 }
3524 else
3525 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003526 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3527 popup_highlight_curline(wp);
3528
Bram Moolenaar371806e2020-10-22 13:44:54 +02003529 // If an error message was given always return FALSE, so that keys are
3530 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003531 // If we get three errors in a row then close the popup. Decrement the
3532 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3533 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003534 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003535 {
3536 wp->w_filter_errors += 10;
3537 if (wp->w_filter_errors >= 30)
3538 popup_close_with_retval(wp, -1);
3539 res = FALSE;
3540 }
3541 else
3542 {
3543 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3544 --wp->w_filter_errors;
3545 res = tv_get_bool(&rettv);
3546 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003547 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003548
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003549 vim_free(argv[1].vval.v_string);
3550 clear_tv(&rettv);
3551 return res;
3552}
3553
3554/*
3555 * Called when "c" was typed: invoke popup filter callbacks.
3556 * Returns TRUE when the character was consumed,
3557 */
3558 int
3559popup_do_filter(int c)
3560{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003561 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003562 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003563 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003564 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003565 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003566 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003567
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003568#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003569 // Popup window with terminal always gets focus.
3570 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3571 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003572#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003573
Bram Moolenaar934470e2019-09-01 23:27:05 +02003574 if (recursive)
3575 return FALSE;
3576 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003577
Bram Moolenaarf6396232019-08-24 19:36:00 +02003578 if (c == K_LEFTMOUSE)
3579 {
3580 int row = mouse_row;
3581 int col = mouse_col;
3582
3583 wp = mouse_find_win(&row, &col, FIND_POPUP);
3584 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003585 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003586 }
3587
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003588 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003589 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003590 while (res == FALSE
3591 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003592 if (wp->w_filter_cb.cb_name != NULL
3593 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003594 res = invoke_popup_filter(wp, c);
3595
glepnir8a635292025-03-21 18:12:32 +01003596 // when Ctrl-C and no popup has been processed (res is still FALSE)
3597 // Try to find and close a popup that has no filter callback
3598 if (c == Ctrl_C && res == FALSE)
3599 {
3600 popup_reset_handled(POPUP_HANDLED_2);
3601 wp = find_next_popup(FALSE, POPUP_HANDLED_2);
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02003602 if (wp != NULL)
3603 {
glepnir8a635292025-03-21 18:12:32 +01003604 popup_close_with_retval(wp, -1);
3605 res = TRUE;
3606 }
3607 }
3608
3609
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003610 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003611 {
3612 int save_got_int = got_int;
3613
3614 // Reset got_int to avoid a function used in the statusline aborts.
3615 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003616 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003617 got_int |= save_got_int;
3618 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003619 recursive = FALSE;
3620 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003621
3622 // When interrupted return FALSE to avoid looping.
3623 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003624}
3625
Bram Moolenaar3397f742019-06-02 18:40:06 +02003626/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003627 * Return TRUE if there is a popup visible with a filter callback and the
3628 * "mapping" property off.
3629 */
3630 int
3631popup_no_mapping(void)
3632{
3633 int round;
3634 win_T *wp;
3635
3636 for (round = 1; round <= 2; ++round)
3637 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3638 wp != NULL; wp = wp->w_next)
3639 if (wp->w_filter_cb.cb_name != NULL
3640 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3641 return TRUE;
3642 return FALSE;
3643}
3644
3645/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003646 * Called when the cursor moved: check if any popup needs to be closed if the
3647 * cursor moved far enough.
3648 */
3649 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003650popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003651{
3652 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003653
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003654 popup_reset_handled(POPUP_HANDLED_3);
3655 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003656 if (wp->w_popup_curwin != NULL
3657 && (curwin != wp->w_popup_curwin
3658 || curwin->w_cursor.lnum != wp->w_popup_lnum
3659 || curwin->w_cursor.col < wp->w_popup_mincol
3660 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003661 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003662}
3663
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003664/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003665 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003666 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003667 static void
3668popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003669{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003670 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003671 char_u *cells;
3672 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003673
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003674 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3675 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003676 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003677 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003678 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003679 if (wp->w_popup_mask_cells != NULL
3680 && wp->w_popup_mask_height == height
3681 && wp->w_popup_mask_width == width)
3682 return; // cache is still valid
3683
3684 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003685 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003686 if (wp->w_popup_mask_cells == NULL)
3687 return;
3688 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003689
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003690 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003691 {
3692 int cols, cole;
3693 int lines, linee;
3694
3695 li = lio->li_tv.vval.v_list->lv_first;
3696 cols = tv_get_number(&li->li_tv);
3697 if (cols < 0)
3698 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003699 if (cols <= 0)
3700 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003701 li = li->li_next;
3702 cole = tv_get_number(&li->li_tv);
3703 if (cole < 0)
3704 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003705 if (cole > width)
3706 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003707 li = li->li_next;
3708 lines = tv_get_number(&li->li_tv);
3709 if (lines < 0)
3710 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003711 if (lines <= 0)
3712 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003713 li = li->li_next;
3714 linee = tv_get_number(&li->li_tv);
3715 if (linee < 0)
3716 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003717 if (linee > height)
3718 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003719
Bram Moolenaar4012d262020-12-29 11:57:46 +01003720 for (row = lines - 1; row < linee; ++row)
3721 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003722 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003723 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003724}
3725
3726/*
3727 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3728 * "col" and "line" are screen coordinates.
3729 */
3730 static int
3731popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3732{
3733 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3734 int line = screenline - wp->w_winrow;
3735
3736 return col >= 0 && col < width
3737 && line >= 0 && line < height
3738 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003739}
3740
3741/*
3742 * Set flags in popup_transparent[] for window "wp" to "val".
3743 */
3744 static void
3745update_popup_transparent(win_T *wp, int val)
3746{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003747 if (wp->w_popup_mask == NULL)
3748 return;
3749
3750 int width = popup_width(wp);
3751 int height = popup_height(wp);
3752 listitem_T *lio, *li;
3753 int cols, cole;
3754 int lines, linee;
3755 int col, line;
3756
3757 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003758 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003759 li = lio->li_tv.vval.v_list->lv_first;
3760 cols = tv_get_number(&li->li_tv);
3761 if (cols < 0)
3762 cols = width + cols + 1;
3763 li = li->li_next;
3764 cole = tv_get_number(&li->li_tv);
3765 if (cole < 0)
3766 cole = width + cole + 1;
3767 li = li->li_next;
3768 lines = tv_get_number(&li->li_tv);
3769 if (lines < 0)
3770 lines = height + lines + 1;
3771 li = li->li_next;
3772 linee = tv_get_number(&li->li_tv);
3773 if (linee < 0)
3774 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003775
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003776 --cols;
3777 cols -= wp->w_popup_leftoff;
3778 if (cols < 0)
3779 cols = 0;
3780 cole -= wp->w_popup_leftoff;
3781 --lines;
3782 if (lines < 0)
3783 lines = 0;
3784 for (line = lines; line < linee
3785 && line + wp->w_winrow < screen_Rows; ++line)
3786 for (col = cols; col < cole
3787 && col + wp->w_wincol < screen_Columns; ++col)
3788 popup_transparent[(line + wp->w_winrow) * screen_Columns
3789 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003790 }
3791}
3792
3793/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003794 * Only called when popup window "wp" is hidden: If the window is positioned
3795 * next to a text property, and it is now visible, then unhide the popup.
3796 * We don't check if visible popups become hidden, that is done in
3797 * popup_adjust_position().
3798 * Return TRUE if the popup became unhidden.
3799 */
3800 static int
3801check_popup_unhidden(win_T *wp)
3802{
3803 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3804 {
3805 textprop_T prop;
3806 linenr_T lnum;
3807
Bram Moolenaar27724252022-05-08 15:00:04 +01003808 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3809 && find_visible_prop(wp->w_popup_prop_win,
3810 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003811 &prop, &lnum) == OK)
3812 {
3813 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003814 wp->w_popup_prop_topline = 0; // force repositioning
3815 return TRUE;
3816 }
3817 }
3818 return FALSE;
3819}
3820
3821/*
3822 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3823 * That is when the buffer in the popup was changed, or the popup is following
3824 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003825 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003826 */
3827 static int
3828popup_need_position_adjust(win_T *wp)
3829{
3830 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3831 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003832 if (win_valid(wp->w_popup_prop_win)
3833 && (wp->w_popup_prop_changedtick
3834 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3835 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3836 return TRUE;
3837
3838 // May need to adjust the width if the cursor moved.
3839 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003840}
3841
3842/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003843 * Update "popup_mask" if needed.
3844 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003845 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003846 * Also marks window lines for redrawing.
3847 */
3848 void
3849may_update_popup_mask(int type)
3850{
3851 win_T *wp;
3852 short *mask;
3853 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003854 int redraw_all_popups = FALSE;
3855 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003856
3857 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003858 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3859 // basic (such as the screen size) must have changed.
3860 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003861 {
3862 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003863 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003864 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003865
3866 // Check if any popup window buffer has changed and if any popup connected
3867 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003868 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003869 if (wp->w_popup_flags & POPF_HIDDEN)
3870 popup_mask_refresh |= check_popup_unhidden(wp);
3871 else if (popup_need_position_adjust(wp))
3872 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003873 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003874 if (wp->w_popup_flags & POPF_HIDDEN)
3875 popup_mask_refresh |= check_popup_unhidden(wp);
3876 else if (popup_need_position_adjust(wp))
3877 popup_mask_refresh = TRUE;
3878
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003879 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003880 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003881
3882 // Need to update the mask, something has changed.
3883 popup_mask_refresh = FALSE;
3884 popup_mask_tab = curtab;
3885 popup_visible = FALSE;
3886
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003887 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003888 // If redrawing only what is needed, update "popup_mask_next" and then
3889 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003890 redrawing_all_win = TRUE;
3891 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003892 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003893 redrawing_all_win = FALSE;
3894 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003895 mask = popup_mask;
3896 else
3897 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003898 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899
3900 // Find the window with the lowest zindex that hasn't been handled yet,
3901 // so that the window with a higher zindex overwrites the value in
3902 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003903 popup_reset_handled(POPUP_HANDLED_4);
3904 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003905 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003906 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003907 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003908
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003909 popup_visible = TRUE;
3910
Bram Moolenaar12034e22019-08-25 22:25:02 +02003911 // Recompute the position if the text changed. It may make the popup
3912 // hidden if it's attach to a text property that is no longer visible.
3913 if (redraw_all_popups || popup_need_position_adjust(wp))
3914 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003915 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003916 if (wp->w_popup_flags & POPF_HIDDEN)
3917 continue;
3918 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003919
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003920 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003921 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003922 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003923 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003924 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003925 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003926 col < wp->w_wincol + width - wp->w_popup_leftoff
3927 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003928 if (wp->w_zindex < POPUPMENU_ZINDEX
3929 && pum_visible()
3930 && pum_under_menu(line, col, FALSE))
3931 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3932 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003933 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003934 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 }
3936
3937 // Only check which lines are to be updated if not already
3938 // updating all lines.
3939 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003940 {
3941 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3942 win_T *prev_wp = NULL;
3943
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003944 for (line = 0; line < screen_Rows; ++line)
3945 {
3946 int col_done = 0;
3947
3948 for (col = 0; col < screen_Columns; ++col)
3949 {
3950 int off = line * screen_Columns + col;
3951
3952 if (popup_mask[off] != popup_mask_next[off])
3953 {
3954 popup_mask[off] = popup_mask_next[off];
3955
3956 if (line >= cmdline_row)
3957 {
3958 // the command line needs to be cleared if text below
3959 // the popup is now visible.
3960 if (!msg_scrolled && popup_mask_next[off] == 0)
3961 clear_cmdline = TRUE;
3962 }
3963 else if (col >= col_done)
3964 {
3965 linenr_T lnum;
3966 int line_cp = line;
3967 int col_cp = col;
3968
3969 // The screen position "line" / "col" needs to be
3970 // redrawn. Figure out what window that is and update
zeertzjqf2d90a32024-02-12 20:28:01 +01003971 // w_redraw_top and w_redraw_bot. Only needs to be
3972 // done once for each window line.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003973 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3974 if (wp != NULL)
3975 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003976#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003977 // A terminal window needs to be redrawn.
3978 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003979 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003980 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003981#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003982 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003983 if (wp != prev_wp)
3984 {
3985 vim_memset(plines_cache, 0,
3986 sizeof(int) * Rows);
3987 prev_wp = wp;
3988 }
3989
3990 if (line_cp >= wp->w_height)
3991 // In (or below) status line
3992 wp->w_redr_status = TRUE;
3993 else
3994 {
3995 // compute the position in the buffer line
3996 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003997 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003998 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003999 redrawWinline(wp, lnum);
4000 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02004001 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004002
4003 // This line is going to be redrawn, no need to
4004 // check until the right side of the window.
4005 col_done = wp->w_wincol + wp->w_width - 1;
4006 }
4007 }
4008 }
4009 }
4010 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004011
4012 vim_free(plines_cache);
4013 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00004014
4015 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004016}
4017
4018/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02004019 * If the current window is a popup and something relevant changed, recompute
4020 * the position and size.
4021 */
4022 void
4023may_update_popup_position(void)
4024{
4025 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
4026 popup_adjust_position(curwin);
4027}
4028
4029/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004030 * Return a string of "len" spaces in IObuff.
4031 */
4032 static char_u *
4033get_spaces(int len)
4034{
4035 vim_memset(IObuff, ' ', (size_t)len);
4036 IObuff[len] = NUL;
4037 return IObuff;
4038}
4039
4040/*
4041 * Update popup windows. They are drawn on top of normal windows.
4042 * "win_update" is called for each popup window, lowest zindex first.
4043 */
4044 void
4045update_popups(void (*win_update)(win_T *wp))
4046{
4047 win_T *wp;
4048 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004049 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004050 int total_width;
4051 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004052 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004053 int popup_attr;
4054 int border_attr[4];
4055 int border_char[8];
4056 char_u buf[MB_MAXBYTES];
4057 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004058 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004059 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004060 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004061 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02004062 int sb_thumb_top = 0;
4063 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004064 int attr_scroll = 0;
4065 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004066
Bram Moolenaarebec3e22020-11-28 20:22:06 +01004067 // hide the cursor until redrawing is done.
4068 cursor_off();
4069
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004070 // Find the window with the lowest zindex that hasn't been updated yet,
4071 // so that the window with a higher zindex is drawn later, thus goes on
4072 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01004073 popup_reset_handled(POPUP_HANDLED_5);
4074 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004075 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004076 int title_len = 0;
4077 int title_wincol;
4078
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004079 // This drawing uses the zindex of the popup window, so that it's on
4080 // top of the text but doesn't draw when another popup with higher
4081 // zindex is on top of the character.
4082 screen_zindex = wp->w_zindex;
4083
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004084 // Set flags in popup_transparent[] for masked cells.
4085 update_popup_transparent(wp, 1);
4086
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004087 // adjust w_winrow and w_wincol for border and padding, since
4088 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004089 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02004090 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004091 - wp->w_popup_leftoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004092 if (wp->w_wincol + left_extra < 0)
4093 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004094 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004095 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004096
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004097 // Draw the popup text, unless it's off screen.
4098 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004099 {
Bram Moolenaar10476522020-09-24 22:57:31 +02004100 // May need to update the "cursorline" highlighting, which may also
4101 // change "topline"
4102 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4103 popup_highlight_curline(wp);
4104
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004105 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004106
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004107 // move the cursor into the visible lines, otherwise executing
4108 // commands with win_execute() may cause the text to jump.
4109 if (wp->w_cursor.lnum < wp->w_topline)
4110 wp->w_cursor.lnum = wp->w_topline;
4111 else if (wp->w_cursor.lnum >= wp->w_botline)
4112 wp->w_cursor.lnum = wp->w_botline - 1;
4113 }
4114
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004116 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004117
4118 // Add offset for border and padding if not done already.
4119 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4120 {
4121 wp->w_wcol += left_extra;
4122 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4123 }
4124 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004125 {
4126 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004127 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004128 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004129
Bram Moolenaareabddc42022-04-02 15:32:16 +01004130 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004131 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004132 popup_attr = get_wcr_attr(wp);
4133
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004134 if (wp->w_winrow + total_height > cmdline_row)
4135 wp->w_popup_flags |= POPF_ON_CMDLINE;
4136 else
4137 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4138
4139
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004140 // We can only use these line drawing characters when 'encoding' is
4141 // "utf-8" and 'ambiwidth' is "single".
4142 if (enc_utf8 && *p_ambw == 's')
4143 {
4144 border_char[0] = border_char[2] = 0x2550;
4145 border_char[1] = border_char[3] = 0x2551;
4146 border_char[4] = 0x2554;
4147 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004148 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4149 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004150 border_char[7] = 0x255a;
4151 }
4152 else
4153 {
4154 border_char[0] = border_char[2] = '-';
4155 border_char[1] = border_char[3] = '|';
4156 for (i = 4; i < 8; ++i)
4157 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004158 if (wp->w_popup_flags & POPF_RESIZE)
4159 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004160 }
4161 for (i = 0; i < 8; ++i)
4162 if (wp->w_border_char[i] != 0)
4163 border_char[i] = wp->w_border_char[i];
4164
4165 for (i = 0; i < 4; ++i)
4166 {
4167 border_attr[i] = popup_attr;
4168 if (wp->w_border_highlight[i] != NULL)
4169 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4170 }
4171
Bram Moolenaard91467f2020-11-21 12:42:09 +01004172 // Title goes on top of border or padding.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004173 title_wincol = wp->w_wincol + 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004174 if (wp->w_popup_title != NULL)
4175 {
rbtnnc6119412021-08-07 13:08:45 +02004176 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004177
Ralf Schandlbc869872021-05-28 14:12:14 +02004178 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004179 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004180 {
4181 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4182 char_u *title_text = alloc(title_byte_len + 1);
4183
4184 if (title_text != NULL)
4185 {
4186 trunc_string(wp->w_popup_title, title_text,
4187 total_width - 2, title_byte_len + 1);
4188 screen_puts(title_text, wp->w_winrow, title_wincol,
4189 wp->w_popup_border[0] > 0
4190 ? border_attr[0] : popup_attr);
4191 vim_free(title_text);
4192 }
4193
Bram Moolenaard91467f2020-11-21 12:42:09 +01004194 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004195 }
4196 else
4197 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4198 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004199 }
4200
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004201 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004202 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004203 if (wp->w_popup_border[0] > 0)
4204 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004205 // top border; do not draw over the title
4206 if (title_len > 0)
4207 {
4208 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4209 wincol < 0 ? 0 : wincol, title_wincol,
4210 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004211 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004212 border_char[0], border_attr[0]);
4213 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4214 title_wincol + title_len, wincol + total_width,
4215 border_char[0], border_char[0], border_attr[0]);
4216 }
4217 else
4218 {
4219 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4220 wincol < 0 ? 0 : wincol, wincol + total_width,
4221 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4222 ? border_char[4] : border_char[0],
4223 border_char[0], border_attr[0]);
4224 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004225 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004226 {
4227 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4228 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004229 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004230 }
4231 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004232 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4233 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004234
Bram Moolenaard529ba52019-07-02 23:13:53 +02004235 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4236 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004237 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004238 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004239 - wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004240 if (padcol < 0)
4241 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004242 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004243 padcol = 0;
4244 }
4245 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004246 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004247 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004248 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004249 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004250 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004251 // top padding and no border; do not draw over the title
4252 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004253 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004254 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004255 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004256 row += 1;
4257 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004258 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004259 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004260 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004261 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004262
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004263 // Compute scrollbar thumb position and size.
4264 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004265 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004266 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4267 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004268 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004269
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004270 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4271 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004272 if (wp->w_topline > 1 && sb_thumb_height == height)
4273 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004274 if (sb_thumb_height == 0)
4275 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004276 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004277 // it just fits, avoid divide by zero
4278 sb_thumb_top = 0;
4279 else
4280 sb_thumb_top = (wp->w_topline - 1
4281 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004282 * (wp->w_height - sb_thumb_height)
4283 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004284 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4285 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004286 last = total_height - top_off - wp->w_popup_border[2];
4287 if (sb_thumb_top >= last)
4288 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004289 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004290
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004291 if (wp->w_scrollbar_highlight != NULL)
4292 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4293 else
4294 attr_scroll = highlight_attr[HLF_PSB];
4295 if (wp->w_thumb_highlight != NULL)
4296 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4297 else
4298 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004299 }
4300
4301 for (i = wp->w_popup_border[0];
4302 i < total_height - wp->w_popup_border[2]; ++i)
4303 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004304 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004305 // left and right padding only needed next to the body
4306 int do_padding =
4307 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4308 && i < total_height - wp->w_popup_border[2]
4309 - wp->w_popup_padding[2];
4310
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004311 row = wp->w_winrow + i;
4312
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004313 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004314 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004315 {
4316 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004317 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004318 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004319 if (do_padding && wp->w_popup_padding[3] > 0)
4320 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004321 int col = wincol + wp->w_popup_border[3];
4322
Bram Moolenaard529ba52019-07-02 23:13:53 +02004323 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004324 pad_left = wp->w_popup_padding[3];
4325 if (col < 0)
4326 {
4327 pad_left += col;
4328 col = 0;
4329 }
4330 if (pad_left > 0)
4331 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4332 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004333 // scrollbar
4334 if (wp->w_has_scrollbar)
4335 {
4336 int line = i - top_off;
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004337 int scroll_col = wp->w_wincol + total_width - 1
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004338 - wp->w_popup_border[1];
4339
4340 if (line >= 0 && line < wp->w_height)
4341 screen_putchar(' ', row, scroll_col,
4342 line >= sb_thumb_top
4343 && line < sb_thumb_top + sb_thumb_height
4344 ? attr_thumb : attr_scroll);
4345 else
4346 screen_putchar(' ', row, scroll_col, popup_attr);
4347 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004348 // right border
4349 if (wp->w_popup_border[1] > 0)
4350 {
4351 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004352 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004353 }
4354 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004355 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004356 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004357 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004358 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4359 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004360 }
4361
4362 if (wp->w_popup_padding[2] > 0)
4363 {
4364 // bottom padding
4365 row = wp->w_winrow + wp->w_popup_border[0]
4366 + wp->w_popup_padding[0] + wp->w_height;
4367 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004368 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004369 }
4370
4371 if (wp->w_popup_border[2] > 0)
4372 {
4373 // bottom border
4374 row = wp->w_winrow + total_height - 1;
4375 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004376 wincol < 0 ? 0 : wincol,
4377 wincol + total_width,
4378 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004379 ? border_char[7] : border_char[2],
4380 border_char[2], border_attr[2]);
4381 if (wp->w_popup_border[1] > 0)
4382 {
4383 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004384 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004385 }
4386 }
4387
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004388 if (wp->w_popup_close == POPCLOSE_BUTTON)
4389 {
4390 // close button goes on top of anything at the top-right corner
4391 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004392 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004393 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4394 }
4395
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004396 update_popup_transparent(wp, 0);
4397
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004398 // Back to the normal zindex.
4399 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004400
4401#ifdef HAS_MESSAGE_WINDOW
4402 // if this was the message window popup may start the timer now
4403 may_start_message_win_timer(wp);
4404#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004405 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004406
4407#if defined(FEAT_SEARCH_EXTRA)
4408 // In case win_update() called start_search_hl().
4409 end_search_hl();
4410#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004411}
4412
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004413/*
4414 * Mark references in callbacks of one popup window.
4415 */
4416 static int
4417set_ref_in_one_popup(win_T *wp, int copyID)
4418{
4419 int abort = FALSE;
4420 typval_T tv;
4421
4422 if (wp->w_close_cb.cb_partial != NULL)
4423 {
4424 tv.v_type = VAR_PARTIAL;
4425 tv.vval.v_partial = wp->w_close_cb.cb_partial;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004426 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL, NULL);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004427 }
4428 if (wp->w_filter_cb.cb_partial != NULL)
4429 {
4430 tv.v_type = VAR_PARTIAL;
4431 tv.vval.v_partial = wp->w_filter_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 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004434 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004435 return abort;
4436}
4437
4438/*
4439 * Set reference in callbacks of popup windows.
4440 */
4441 int
4442set_ref_in_popups(int copyID)
4443{
4444 int abort = FALSE;
4445 win_T *wp;
4446 tabpage_T *tp;
4447
4448 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4449 abort = abort || set_ref_in_one_popup(wp, copyID);
4450
4451 FOR_ALL_TABPAGES(tp)
4452 {
4453 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4454 abort = abort || set_ref_in_one_popup(wp, copyID);
4455 if (abort)
4456 break;
4457 }
4458 return abort;
4459}
Bram Moolenaar79648732019-07-18 21:43:07 +02004460
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004461 int
4462popup_is_popup(win_T *wp)
4463{
4464 return wp->w_popup_flags != 0;
4465}
4466
4467#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004468/*
4469 * Find an existing popup used as the preview window, in the current tab page.
4470 * Return NULL if not found.
4471 */
4472 win_T *
4473popup_find_preview_window(void)
4474{
4475 win_T *wp;
4476
4477 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004478 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004479 if (wp->w_p_pvw)
4480 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004481 return NULL;
4482}
4483
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004484/*
4485 * Find an existing popup used as the info window, in the current tab page.
4486 * Return NULL if not found.
4487 */
4488 win_T *
4489popup_find_info_window(void)
4490{
4491 win_T *wp;
4492
4493 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004494 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004495 if (wp->w_popup_flags & POPF_INFO)
4496 return wp;
4497 return NULL;
4498}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004499#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004500
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004501 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004502f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4503{
4504#ifdef HAS_MESSAGE_WINDOW
4505 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4506#else
4507 rettv->vval.v_number = 0;
4508#endif
4509}
4510
4511 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004512f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4513{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004514#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004515 win_T *wp = popup_find_info_window();
4516
4517 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004518#else
4519 rettv->vval.v_number = 0;
4520#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004521}
4522
4523 void
4524f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004525{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004526#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004527 win_T *wp = popup_find_preview_window();
4528
4529 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004530#else
4531 rettv->vval.v_number = 0;
4532#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004533}
4534
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004535#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004536/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004537 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004538 * NOTE: this makes the popup the current window, so that the file can be
4539 * edited. However, it must not remain to be the current window, the caller
4540 * must make sure of that.
4541 */
4542 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004543popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004544{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004545 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004546
4547 if (wp == NULL)
4548 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004549 if (info)
4550 wp->w_popup_flags |= POPF_INFO;
4551 else
4552 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004553
4554 // Set the width to a reasonable value, so that w_topline can be computed.
4555 if (wp->w_minwidth > 0)
4556 wp->w_width = wp->w_minwidth;
4557 else if (wp->w_maxwidth > 0)
4558 wp->w_width = wp->w_maxwidth;
4559 else
4560 wp->w_width = curwin->w_width;
4561
4562 // Will switch to another buffer soon, dummy one can be wiped.
4563 wp->w_buffer->b_locked = FALSE;
4564
4565 win_enter(wp, FALSE);
4566 return OK;
4567}
4568
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004569/*
4570 * Close any preview popup.
4571 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004572 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004573popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004574{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004575 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004576
4577 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004578 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004579}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004580
4581/*
4582 * Hide the info popup.
4583 */
4584 void
4585popup_hide_info(void)
4586{
4587 win_T *wp = popup_find_info_window();
4588
4589 if (wp != NULL)
glepnirff159252025-03-01 16:17:00 +01004590 {
4591 popup_on_cmdline = wp->w_popup_flags & POPF_ON_CMDLINE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004592 popup_hide(wp);
glepnirff159252025-03-01 16:17:00 +01004593 }
4594}
4595
4596/*
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004597 * Close any info popup.
4598 */
4599 void
4600popup_close_info(void)
4601{
4602 win_T *wp = popup_find_info_window();
4603
4604 if (wp != NULL)
4605 popup_close_with_retval(wp, -1);
4606}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004607#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004608
John Marriott45377e22025-03-27 18:12:32 +01004609/*
4610 * Returns TRUE if a popup extends into the cmdline area.
4611 */
4612 int
4613popup_overlaps_cmdline(void)
4614{
4615 return popup_on_cmdline;
4616}
4617
Bram Moolenaar9198de32022-08-27 21:30:03 +01004618#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4619
Bram Moolenaar9198de32022-08-27 21:30:03 +01004620/*
4621 * Get the message window.
4622 * Returns NULL if something failed.
4623 */
4624 win_T *
4625popup_get_message_win(void)
4626{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004627 if (message_win != NULL)
4628 return message_win;
4629
4630 int i;
4631
4632 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4633
Bram Moolenaar9198de32022-08-27 21:30:03 +01004634 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004635 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004636
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004637 // use the full screen width
4638 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004639
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004640 // position at bottom of screen
4641 message_win->w_popup_pos = POPPOS_BOTTOM;
4642 message_win->w_wantcol = 1;
4643 message_win->w_minwidth = 9999;
4644 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004645
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004646 // no padding, border at the top
4647 for (i = 0; i < 4; ++i)
4648 message_win->w_popup_padding[i] = 0;
4649 for (i = 1; i < 4; ++i)
4650 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004651
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004652 if (message_win->w_popup_timer != NULL)
4653 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004654 return message_win;
4655}
4656
4657/*
4658 * If the message window is not visible: show it
4659 * If the message window is visible: reset the timeout
4660 */
4661 void
4662popup_show_message_win(void)
4663{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004664 if (message_win == NULL)
4665 return;
4666
4667 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004668 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004669 // the highlight may have changed.
4670 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4671 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004672 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004673 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004674}
4675
4676 static void
4677may_start_message_win_timer(win_T *wp)
4678{
4679 if (wp == message_win && start_message_win_timer)
4680 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004681 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004682 {
4683 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004684 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004685 message_win_time = 3000;
4686 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004687 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004688 }
4689}
4690
4691 int
4692popup_message_win_visible(void)
4693{
4694 return message_win != NULL
4695 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4696}
4697
4698/*
4699 * If the message window is visible: hide it.
4700 */
4701 void
4702popup_hide_message_win(void)
4703{
4704 if (message_win != NULL)
4705 popup_hide(message_win);
4706}
4707
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004708// Values saved in start_echowindow() and restored in end_echowindow()
4709static int save_msg_didout = FALSE;
4710static int save_msg_col = 0;
4711// Values saved in end_echowindow() and restored in start_echowindow()
4712static int ew_msg_didout = FALSE;
4713static int ew_msg_col = 0;
4714
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004715/*
4716 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004717 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004718 */
4719 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004720start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004721{
4722 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004723 save_msg_didout = msg_didout;
4724 save_msg_col = msg_col;
4725 msg_didout = ew_msg_didout;
4726 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004727 if (time_sec != 0)
4728 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004729}
4730
4731/*
4732 * Invoked after outputting a message for ":echowindow".
4733 */
4734 void
4735end_echowindow(void)
4736{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004737 in_echowindow = FALSE;
4738
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004739 if ((State & MODE_HITRETURN) == 0)
4740 // show the message window now
4741 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004742
4743 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004744 ew_msg_didout = TRUE;
4745 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4746 msg_didout = save_msg_didout;
4747 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004748}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004749#endif
4750
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004751/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004752 * Close any popup for a text property associated with "win".
4753 * Return TRUE if a popup was closed.
4754 */
4755 int
4756popup_win_closed(win_T *win)
4757{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004758 int round;
4759 win_T *wp;
4760 win_T *next;
4761 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004762
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004763 for (round = 1; round <= 2; ++round)
4764 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4765 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004766 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004767 next = wp->w_next;
4768 if (wp->w_popup_prop_win == win)
4769 {
4770 popup_close_with_retval(wp, -1);
4771 ret = TRUE;
4772 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004773 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004774 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004775}
4776
4777/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004778 * Set the title of the popup window to the file name.
4779 */
4780 void
4781popup_set_title(win_T *wp)
4782{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004783 if (wp->w_buffer->b_fname == NULL)
4784 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004785
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004786 char_u dirname[MAXPATHL];
4787 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004788
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004789 mch_dirname(dirname, MAXPATHL);
4790 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4791
4792 vim_free(wp->w_popup_title);
4793 len = STRLEN(wp->w_buffer->b_fname) + 3;
4794 wp->w_popup_title = alloc((int)len);
4795 if (wp->w_popup_title != NULL)
4796 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4797 wp->w_buffer->b_fname);
4798 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004799}
4800
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004801# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004802/*
4803 * If there is a preview window, update the title.
4804 * Used after changing directory.
4805 */
4806 void
4807popup_update_preview_title(void)
4808{
4809 win_T *wp = popup_find_preview_window();
4810
4811 if (wp != NULL)
4812 popup_set_title(wp);
4813}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004814# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004815
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004816#endif // FEAT_PROP_POPUP