blob: 2032492a9108351155ebbdea431bad34680c1655 [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
glepnirff159252025-03-01 16:17:00 +010043static int popup_on_cmdline = FALSE;
44
Bram Moolenaarcf0995d2022-09-11 21:36:17 +010045static void may_start_message_win_timer(win_T *wp);
Bram Moolenaar43568642022-08-28 13:02:45 +010046#endif
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"
89 n = screen_screencol() + 1 + n;
90
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.
1357 if (wp->w_wincol > Columns - left_extra - 1)
1358 wp->w_wincol = Columns - 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.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001365 maxspace = Columns - wp->w_wincol - left_extra;
1366 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 {
1548 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1549 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
1584 && wp->w_wincol + want_col >= Columns)
1585 {
1586 wp->w_wincol = Columns - want_col;
1587 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
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001676 if (wp->w_height != org_height)
1677 win_comp_scroll(wp);
1678
Bram Moolenaar17146962019-05-30 00:12:11 +02001679 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001680 if (win_valid(wp->w_popup_prop_win))
1681 {
1682 wp->w_popup_prop_changedtick =
1683 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1684 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1685 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001686
1687 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001688 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001689 if (org_winrow != wp->w_winrow
1690 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001691 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001692 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001693 || org_width != wp->w_width
1694 || org_height != wp->w_height)
1695 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001696 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001697 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1698 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001699 popup_mask_refresh = TRUE;
1700 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001701}
1702
Bram Moolenaar17627312019-06-02 19:53:44 +02001703typedef enum
1704{
1705 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001706 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001707 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001708 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001709 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001710 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001711 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001712 TYPE_PREVIEW, // preview window
1713 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001714} create_type_T;
1715
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001716/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001717 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1718 */
1719 static int
1720popup_is_notification(create_type_T type)
1721{
1722 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1723}
1724
1725/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001726 * Make "buf" empty and set the contents to "text".
1727 * Used by popup_create() and popup_settext().
1728 */
1729 static void
1730popup_set_buffer_text(buf_T *buf, typval_T text)
1731{
1732 int lnum;
1733
1734 // Clear the buffer, then replace the lines.
1735 curbuf = buf;
1736 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001737 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001738 curbuf = curwin->w_buffer;
1739
1740 // Add text to the buffer.
1741 if (text.v_type == VAR_STRING)
1742 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001743 char_u *s = text.vval.v_string;
1744
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001745 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001746 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001747 }
1748 else
1749 {
1750 list_T *l = text.vval.v_list;
1751
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001752 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001753 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001754 if (l->lv_first == &range_list_item)
1755 emsg(_(e_using_number_as_string));
1756 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001757 // list of strings
1758 add_popup_strings(buf, l);
1759 else
1760 // list of dictionaries
1761 add_popup_dicts(buf, l);
1762 }
1763 }
1764
1765 // delete the line that was in the empty buffer
1766 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001767 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001768 curbuf = curwin->w_buffer;
1769}
1770
1771/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001772 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1773 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001774 * Return FAIL if the parsing fails.
1775 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001776 static int
1777parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001778{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001779 char_u *p =
1780#ifdef FEAT_QUICKFIX
1781 !is_preview ? p_cpp :
1782#endif
1783 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001784
Bram Moolenaar258cef52019-08-21 17:29:29 +02001785 if (wp != NULL)
1786 wp->w_popup_flags &= ~POPF_INFO_MENU;
1787
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001788 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001789 {
1790 char_u *e, *dig;
1791 char_u *s = p;
1792 int x;
1793
1794 e = vim_strchr(p, ':');
1795 if (e == NULL || e[1] == NUL)
1796 return FAIL;
1797
1798 p = vim_strchr(e, ',');
1799 if (p == NULL)
1800 p = e + STRLEN(e);
1801 dig = e + 1;
1802 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001803
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001804 // Note: Keep this in sync with p_popup_option_values.
Bram Moolenaar79648732019-07-18 21:43:07 +02001805 if (STRNCMP(s, "height:", 7) == 0)
1806 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001807 if (dig != p)
1808 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001809 if (wp != NULL)
1810 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001811 if (is_preview)
1812 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001813 wp->w_maxheight = x;
1814 }
1815 }
1816 else if (STRNCMP(s, "width:", 6) == 0)
1817 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001818 if (dig != p)
1819 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001820 if (wp != NULL)
1821 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001822 if (is_preview)
1823 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001824 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001825 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001826 }
1827 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001828 else if (STRNCMP(s, "highlight:", 10) == 0)
1829 {
1830 if (wp != NULL)
1831 {
1832 int c = *p;
1833
1834 *p = NUL;
1835 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1836 s + 10, OPT_FREE|OPT_LOCAL, 0);
1837 *p = c;
1838 }
1839 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001840 else if (STRNCMP(s, "border:", 7) == 0)
1841 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001842 // Note: Keep this in sync with p_popup_option_border_values.
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001843 char_u *arg = s + 7;
1844 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1845 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1846 int i;
1847
1848 if (!on && !off)
1849 return FAIL;
1850 if (wp != NULL)
1851 {
1852 for (i = 0; i < 4; ++i)
1853 wp->w_popup_border[i] = on ? 1 : 0;
1854 if (off)
1855 // only show the X for close when there is a border
1856 wp->w_popup_close = POPCLOSE_NONE;
1857 }
1858 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001859 else if (STRNCMP(s, "align:", 6) == 0)
1860 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001861 // Note: Keep this in sync with p_popup_option_align_values.
Bram Moolenaar258cef52019-08-21 17:29:29 +02001862 char_u *arg = s + 6;
1863 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1864 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1865
1866 if (!menu && !item)
1867 return FAIL;
1868 if (wp != NULL && menu)
1869 wp->w_popup_flags |= POPF_INFO_MENU;
1870 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001871 else
1872 return FAIL;
1873 }
1874 return OK;
1875}
1876
1877/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001878 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1879 * is not NULL.
1880 * Return FAIL if the parsing fails.
1881 */
1882 int
1883parse_previewpopup(win_T *wp)
1884{
1885 return parse_popup_option(wp, TRUE);
1886}
1887
1888/*
1889 * Parse the 'completepopup' option and apply the values to window "wp" if it
1890 * is not NULL.
1891 * Return FAIL if the parsing fails.
1892 */
1893 int
1894parse_completepopup(win_T *wp)
1895{
1896 return parse_popup_option(wp, FALSE);
1897}
1898
1899/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001900 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001901 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001902 */
1903 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001904popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001905{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001906 poppos_T ppt = POPPOS_NONE;
1907
1908 if (d != NULL)
1909 ppt = get_pos_entry(d, FALSE);
1910
Bram Moolenaar79648732019-07-18 21:43:07 +02001911 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001912 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001913 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001914 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1915 }
1916 else
1917 {
1918 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1919 if (wp->w_wantline == 0) // cursor in first line
1920 {
1921 wp->w_wantline = 2;
1922 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1923 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1924 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001925 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001926
Bram Moolenaar79648732019-07-18 21:43:07 +02001927 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001928 if (wp->w_wantcol > Columns - width)
1929 {
1930 wp->w_wantcol = Columns - width;
1931 if (wp->w_wantcol < 1)
1932 wp->w_wantcol = 1;
1933 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001934
Bram Moolenaar79648732019-07-18 21:43:07 +02001935 popup_adjust_position(wp);
1936}
1937
1938/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001939 * Set w_wantline and w_wantcol for the a given screen position.
1940 * Caller must take care of running into the window border.
1941 */
1942 void
1943popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1944{
1945 wp->w_wantline = row;
1946 wp->w_wantcol = col;
1947 popup_adjust_position(wp);
1948}
1949
1950/*
1951 * Add a border and lef&right padding.
1952 */
1953 static void
1954add_border_left_right_padding(win_T *wp)
1955{
1956 int i;
1957
1958 for (i = 0; i < 4; ++i)
1959 {
1960 wp->w_popup_border[i] = 1;
1961 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1962 }
1963}
1964
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001965#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001966/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001967 * Return TRUE if there is any popup window with a terminal buffer.
1968 */
1969 static int
1970popup_terminal_exists(void)
1971{
1972 win_T *wp;
1973 tabpage_T *tp;
1974
1975 FOR_ALL_POPUPWINS(wp)
1976 if (wp->w_buffer->b_term != NULL)
1977 return TRUE;
1978 FOR_ALL_TABPAGES(tp)
1979 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1980 if (wp->w_buffer->b_term != NULL)
1981 return TRUE;
1982 return FALSE;
1983}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001984#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001985
1986/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001987 * Mark all popup windows in the current tab and global for redrawing.
1988 */
1989 void
1990popup_redraw_all(void)
1991{
1992 win_T *wp;
1993
1994 FOR_ALL_POPUPWINS(wp)
1995 wp->w_redr_type = UPD_NOT_VALID;
1996 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1997 wp->w_redr_type = UPD_NOT_VALID;
1998}
1999
2000/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01002001 * Set the color for a notification window.
2002 */
2003 static void
2004popup_update_color(win_T *wp, create_type_T type)
2005{
2006 char *hiname = type == TYPE_MESSAGE_WIN
2007 ? "MessageWindow" : "PopupNotification";
Bram Moolenaar9198de32022-08-27 21:30:03 +01002008 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Yee Cheng Chine700dde2025-02-20 21:58:21 +01002009 (char_u *)hiname,
Bram Moolenaar9198de32022-08-27 21:30:03 +01002010 OPT_FREE|OPT_LOCAL, 0);
2011}
2012
2013/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002014 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002015 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02002016 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002017 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018 */
Bram Moolenaara730e552019-06-16 19:05:31 +02002019 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02002020popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002021{
Bram Moolenaara3fce622019-06-20 02:31:49 +02002022 win_T *wp;
2023 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002024 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002025 int new_buffer;
2026 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002027 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002028 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002029
Bram Moolenaar79648732019-07-18 21:43:07 +02002030 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002031 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002032 if (in_vim9script()
2033 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2034 || check_for_dict_arg(argvars, 1) == FAIL))
2035 return NULL;
2036
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002037 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002038 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002039 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002040 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002041 if (buf == NULL)
2042 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002043 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002044 return NULL;
2045 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002046#ifdef FEAT_TERMINAL
2047 if (buf->b_term != NULL && popup_terminal_exists())
2048 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002049 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002050 return NULL;
2051 }
2052#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002053 }
2054 else if (!(argvars[0].v_type == VAR_STRING
2055 && argvars[0].vval.v_string != NULL)
2056 && !(argvars[0].v_type == VAR_LIST
2057 && argvars[0].vval.v_list != NULL))
2058 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002059 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002060 return NULL;
2061 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002062 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002063 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002064 d = argvars[1].vval.v_dict;
2065 }
2066
2067 if (d != NULL)
2068 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002069 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002070 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002071 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002072 tabnr = -1; // notifications are global by default
2073 else
2074 tabnr = 0;
2075 if (tabnr > 0)
2076 {
2077 tp = find_tabpage(tabnr);
2078 if (tp == NULL)
2079 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002080 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002081 return NULL;
2082 }
2083 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002084 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002085 else if (popup_is_notification(type))
2086 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002087
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002088 // Create the window and buffer.
2089 wp = win_alloc_popup_win();
2090 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002091 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002092 if (rettv != NULL)
2093 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002094 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002095 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002096
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002097 if (buf != NULL)
2098 {
2099 // use existing buffer
2100 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002101 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002102 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002103 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002105 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002106 }
2107 else
2108 {
2109 // create a new buffer associated with the popup
2110 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002111 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002112 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002113 {
2114 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002115 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002116 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002117 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002118
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002119 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002120
Bram Moolenaar46451042019-08-24 15:50:46 +02002121 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002122 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002123 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002124 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002125 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002126 buf->b_p_ul = -1; // no undo
2127 buf->b_p_swf = FALSE; // no swap file
2128 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002129 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002130
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002131 // Avoid that 'buftype' is reset when this buffer is entered.
2132 buf->b_p_initialized = TRUE;
2133 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002134 wp->w_p_wrap = TRUE; // 'wrap' is default on
2135 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002136
Bram Moolenaara3fce622019-06-20 02:31:49 +02002137 if (tp != NULL)
2138 {
2139 // popup on specified tab page
2140 wp->w_next = tp->tp_first_popupwin;
2141 tp->tp_first_popupwin = wp;
2142 }
2143 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002144 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002145 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002146 wp->w_next = curtab->tp_first_popupwin;
2147 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002148 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002149 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002150 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002151 win_T *prev = first_popupwin;
2152
2153 // Global popup: add at the end, so that it gets displayed on top of
2154 // older ones with the same zindex. Matters for notifications.
2155 if (first_popupwin == NULL)
2156 first_popupwin = wp;
2157 else
2158 {
2159 while (prev->w_next != NULL)
2160 prev = prev->w_next;
2161 prev->w_next = wp;
2162 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002163 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002164
Bram Moolenaar79648732019-07-18 21:43:07 +02002165 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002166 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002167
Bram Moolenaar79648732019-07-18 21:43:07 +02002168 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002169 {
2170 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002171 }
2172 if (type == TYPE_ATCURSOR)
2173 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002174 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002175 set_moved_values(wp);
2176 set_moved_columns(wp, FIND_STRING);
2177 }
2178
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002179 if (type == TYPE_BEVAL)
2180 {
2181 wp->w_popup_pos = POPPOS_BOTLEFT;
2182
2183 // by default use the mouse position
2184 wp->w_wantline = mouse_row;
2185 if (wp->w_wantline <= 0) // mouse on first line
2186 {
2187 wp->w_wantline = 2;
2188 wp->w_popup_pos = POPPOS_TOPLEFT;
2189 }
2190 wp->w_wantcol = mouse_col + 1;
2191 set_mousemoved_values(wp);
2192 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2193 }
2194
Bram Moolenaar33796b32019-06-08 16:01:13 +02002195 // set default values
2196 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002197 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002198
Bram Moolenaar9198de32022-08-27 21:30:03 +01002199 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002200 {
2201 win_T *twp, *nextwin;
2202 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002203
2204 // Try to not overlap with another global popup. Guess we need 3
2205 // more screen lines than buffer lines.
2206 wp->w_wantline = 1;
2207 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2208 {
2209 nextwin = twp->w_next;
2210 if (twp != wp
2211 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2212 && twp->w_winrow <= wp->w_wantline - 1 + height
2213 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2214 {
2215 // move to below this popup and restart the loop to check for
2216 // overlap with other popups
2217 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2218 nextwin = first_popupwin;
2219 }
2220 }
2221 if (wp->w_wantline + height > Rows)
2222 {
2223 // can't avoid overlap, put on top in the hope that message goes
2224 // away soon.
2225 wp->w_wantline = 1;
2226 }
2227
2228 wp->w_wantcol = 10;
2229 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002230 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002231 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002232 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002233 for (i = 0; i < 4; ++i)
2234 wp->w_popup_border[i] = 1;
2235 wp->w_popup_padding[1] = 1;
2236 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002237
Bram Moolenaar9198de32022-08-27 21:30:03 +01002238 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002239 }
2240
Bram Moolenaara730e552019-06-16 19:05:31 +02002241 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002242 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002243 wp->w_popup_pos = POPPOS_CENTER;
2244 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002245 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002246 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002247 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002248 }
2249
Bram Moolenaara730e552019-06-16 19:05:31 +02002250 if (type == TYPE_MENU)
2251 {
2252 typval_T tv;
2253 callback_T callback;
2254
2255 tv.v_type = VAR_STRING;
2256 tv.vval.v_string = (char_u *)"popup_filter_menu";
2257 callback = get_callback(&tv);
2258 if (callback.cb_name != NULL)
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002259 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002260 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002261 if (callback.cb_free_name)
2262 vim_free(callback.cb_name);
2263 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002264
2265 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002266 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002267 }
2268
Bram Moolenaar79648732019-07-18 21:43:07 +02002269 if (type == TYPE_PREVIEW)
2270 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002271 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002272 wp->w_popup_close = POPCLOSE_BUTTON;
2273 for (i = 0; i < 4; ++i)
2274 wp->w_popup_border[i] = 1;
2275 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002276 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002277 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002278# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002279 if (type == TYPE_INFO)
2280 {
2281 wp->w_popup_pos = POPPOS_TOPLEFT;
2282 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2283 wp->w_popup_close = POPCLOSE_BUTTON;
2284 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002285 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002286 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002287# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002288
Bram Moolenaarae943152019-06-16 22:54:14 +02002289 for (i = 0; i < 4; ++i)
2290 VIM_CLEAR(wp->w_border_highlight[i]);
2291 for (i = 0; i < 8; ++i)
2292 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002293 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002294 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002295 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002296
Bram Moolenaar79648732019-07-18 21:43:07 +02002297 if (d != NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002298 {
Bram Moolenaar79648732019-07-18 21:43:07 +02002299 // Deal with options.
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002300 if (apply_options(wp, d, TRUE) == FAIL)
2301 {
2302 (void)popup_close(wp->w_id, FALSE);
2303 return NULL;
2304 }
2305 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002306
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002307#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002308 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2309 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002310#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002311
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002312 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002313
2314 wp->w_vsep_width = 0;
2315
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002316 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002317 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002318
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002319#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002320 // When running a terminal in the popup it becomes the current window.
2321 if (buf->b_term != NULL)
2322 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002323#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002324
Bram Moolenaara730e552019-06-16 19:05:31 +02002325 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002326}
2327
2328/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002329 * popup_clear()
2330 */
2331 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002332f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002333{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002334 int force = FALSE;
2335
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002336 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2337 return;
2338
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002339 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002340 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002341 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002342}
2343
2344/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002345 * popup_create({text}, {options})
2346 */
2347 void
2348f_popup_create(typval_T *argvars, typval_T *rettv)
2349{
Bram Moolenaar17627312019-06-02 19:53:44 +02002350 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002351}
2352
2353/*
2354 * popup_atcursor({text}, {options})
2355 */
2356 void
2357f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2358{
Bram Moolenaar17627312019-06-02 19:53:44 +02002359 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002360}
2361
2362/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002363 * popup_beval({text}, {options})
2364 */
2365 void
2366f_popup_beval(typval_T *argvars, typval_T *rettv)
2367{
2368 popup_create(argvars, rettv, TYPE_BEVAL);
2369}
2370
2371/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002372 * Invoke the close callback for window "wp" with value "result".
2373 * Careful: The callback may make "wp" invalid!
2374 */
2375 static void
2376invoke_popup_callback(win_T *wp, typval_T *result)
2377{
2378 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002379 typval_T argv[3];
2380
Christian Brabandt6701abf2023-11-19 10:52:50 +01002381 rettv.v_type = VAR_UNKNOWN;
2382
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002383 argv[0].v_type = VAR_NUMBER;
2384 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2385
2386 if (result != NULL && result->v_type != VAR_UNKNOWN)
2387 copy_tv(result, &argv[1]);
2388 else
2389 {
2390 argv[1].v_type = VAR_NUMBER;
2391 argv[1].vval.v_number = 0;
2392 }
2393
2394 argv[2].v_type = VAR_UNKNOWN;
2395
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002396 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002397 if (result != NULL)
2398 clear_tv(&argv[1]);
2399 clear_tv(&rettv);
2400}
2401
2402/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002403 * Make "prevwin" the current window, unless it's equal to "wp".
2404 * Otherwise make "firstwin" the current window.
2405 */
2406 static void
2407back_to_prevwin(win_T *wp)
2408{
2409 if (win_valid(prevwin) && wp != prevwin)
2410 win_enter(prevwin, FALSE);
2411 else
2412 win_enter(firstwin, FALSE);
2413}
2414
2415/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002416 * Close popup "wp" and invoke any close callback for it.
2417 */
2418 static void
2419popup_close_and_callback(win_T *wp, typval_T *arg)
2420{
2421 int id = wp->w_id;
2422
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002423#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002424 if (wp == curwin && curbuf->b_term != NULL)
2425 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002426 win_T *owp;
2427
2428 // Closing popup window with a terminal: put focus back on the first
2429 // that works:
2430 // - another popup window with a terminal
2431 // - the previous window
2432 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002433 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002434 if (owp != curwin && owp->w_buffer->b_term != NULL)
2435 break;
2436 if (owp != NULL)
2437 win_enter(owp, FALSE);
2438 else
2439 {
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002440 FOR_ALL_POPUPWINS_IN_TAB(curtab, 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);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002445 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002446 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002447 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002448 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002449#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002450
Bram Moolenaar49540192019-12-11 19:34:54 +01002451 // Just in case a check higher up is missing.
2452 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002453 {
2454 // To avoid getting stuck when win_execute() does something that causes
2455 // an error, stop calling the filter callback.
2456 free_callback(&wp->w_filter_cb);
2457
Bram Moolenaar49540192019-12-11 19:34:54 +01002458 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002459 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002460
Bram Moolenaarcee52202020-03-11 14:19:58 +01002461 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002462 if (wp->w_close_cb.cb_name != NULL)
2463 // Careful: This may make "wp" invalid.
2464 invoke_popup_callback(wp, arg);
2465
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002466 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002467 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002468}
2469
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002470 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002471popup_close_with_retval(win_T *wp, int retval)
2472{
2473 typval_T res;
2474
2475 res.v_type = VAR_NUMBER;
2476 res.vval.v_number = retval;
2477 popup_close_and_callback(wp, &res);
2478}
2479
Bram Moolenaar3397f742019-06-02 18:40:06 +02002480/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002481 * Close popup "wp" because of a mouse click.
2482 */
2483 void
2484popup_close_for_mouse_click(win_T *wp)
2485{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002486 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002487}
2488
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002489 static void
2490check_mouse_moved(win_T *wp, win_T *mouse_wp)
2491{
2492 // Close the popup when all if these are true:
2493 // - the mouse is not on this popup
2494 // - "mousemoved" was used
2495 // - the mouse is no longer on the same screen row or the mouse column is
2496 // outside of the relevant text
2497 if (wp != mouse_wp
2498 && wp->w_popup_mouse_row != 0
2499 && (wp->w_popup_mouse_row != mouse_row
2500 || mouse_col < wp->w_popup_mouse_mincol
2501 || mouse_col > wp->w_popup_mouse_maxcol))
2502 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002503 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002504 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002505 }
2506}
2507
2508/*
2509 * Called when the mouse moved: may close a popup with "mousemoved".
2510 */
2511 void
2512popup_handle_mouse_moved(void)
2513{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002514 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002515 win_T *mouse_wp;
2516 int row = mouse_row;
2517 int col = mouse_col;
2518
2519 // find the window where the mouse is in
2520 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2521
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002522 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2523 {
2524 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002525 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002526 }
2527 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2528 {
2529 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002530 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002531 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002532}
2533
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002534/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002535 * In a filter: check if the typed key is a mouse event that is used for
2536 * dragging the popup.
2537 */
2538 static void
2539filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2540{
2541 int row = mouse_row;
2542 int col = mouse_col;
2543
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002544 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002545 && is_mouse_key(c)
2546 && (wp == popup_dragwin
2547 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2548 // do not consume the key, allow for dragging the popup
2549 rettv->vval.v_number = 0;
2550}
2551
Bram Moolenaara730e552019-06-16 19:05:31 +02002552/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002553 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002554 */
2555 void
2556f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2557{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002558 int id;
2559 win_T *wp;
2560 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002561 typval_T res;
2562 int c;
2563 linenr_T old_lnum;
2564
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002565 if (in_vim9script()
2566 && (check_for_number_arg(argvars, 0) == FAIL
2567 || check_for_string_arg(argvars, 1) == FAIL))
2568 return;
2569
2570 id = tv_get_number(&argvars[0]);
2571 wp = win_id2wp(id);
2572 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002573 // If the popup has been closed do not consume the key.
2574 if (wp == NULL)
2575 return;
2576
2577 c = *key;
2578 if (c == K_SPECIAL && key[1] != NUL)
2579 c = TO_SPECIAL(key[1], key[2]);
2580
2581 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002582 rettv->v_type = VAR_BOOL;
2583 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002584 res.v_type = VAR_NUMBER;
2585
2586 old_lnum = wp->w_cursor.lnum;
Christian Brabandtbadeedd2023-08-13 19:25:28 +02002587 if (c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2588 {
2589 if (wp->w_cursor.lnum > 1)
2590 --wp->w_cursor.lnum;
2591 else
2592 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2593 }
2594 if (c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
2595 {
2596 if (wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2597 ++wp->w_cursor.lnum;
2598 else
2599 wp->w_cursor.lnum = 1;
2600 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002601 if (old_lnum != wp->w_cursor.lnum)
2602 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002603 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002604 return;
2605 }
2606
2607 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2608 {
2609 // Cancelled, invoke callback with -1
2610 res.vval.v_number = -1;
2611 popup_close_and_callback(wp, &res);
2612 return;
2613 }
2614 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2615 {
2616 // Invoke callback with current index.
2617 res.vval.v_number = wp->w_cursor.lnum;
2618 popup_close_and_callback(wp, &res);
2619 return;
2620 }
2621
2622 filter_handle_drag(wp, c, rettv);
2623}
2624
2625/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002626 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002627 */
2628 void
2629f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2630{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002631 int id;
2632 win_T *wp;
2633 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002634 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002635 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002636
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002637 if (in_vim9script()
2638 && (check_for_number_arg(argvars, 0) == FAIL
2639 || check_for_string_arg(argvars, 1) == FAIL))
2640 return;
2641
2642 id = tv_get_number(&argvars[0]);
2643 wp = win_id2wp(id);
2644 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002645 // If the popup has been closed don't consume the key.
2646 if (wp == NULL)
2647 return;
2648
Bram Moolenaara730e552019-06-16 19:05:31 +02002649 c = *key;
Ernie Raelb14c3252024-07-19 16:37:09 +02002650 if (c == CAR && need_wait_return)
2651 return;
Bram Moolenaara730e552019-06-16 19:05:31 +02002652 if (c == K_SPECIAL && key[1] != NUL)
2653 c = TO_SPECIAL(key[1], key[2]);
2654
Bram Moolenaara42d9452019-06-15 21:46:30 +02002655 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002656 rettv->v_type = VAR_BOOL;
2657 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002658
Bram Moolenaara730e552019-06-16 19:05:31 +02002659 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002660 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002661 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002662 res.vval.v_number = 0;
2663 else
2664 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002665 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002666 return;
2667 }
2668
2669 // Invoke callback
2670 res.v_type = VAR_NUMBER;
2671 popup_close_and_callback(wp, &res);
2672}
2673
2674/*
2675 * popup_dialog({text}, {options})
2676 */
2677 void
2678f_popup_dialog(typval_T *argvars, typval_T *rettv)
2679{
2680 popup_create(argvars, rettv, TYPE_DIALOG);
2681}
2682
2683/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002684 * popup_menu({text}, {options})
2685 */
2686 void
2687f_popup_menu(typval_T *argvars, typval_T *rettv)
2688{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002689 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002690}
2691
2692/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002693 * popup_notification({text}, {options})
2694 */
2695 void
2696f_popup_notification(typval_T *argvars, typval_T *rettv)
2697{
2698 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2699}
2700
2701/*
2702 * Find the popup window with window-ID "id".
2703 * If the popup window does not exist NULL is returned.
2704 * If the window is not a popup window, and error message is given.
2705 */
2706 static win_T *
2707find_popup_win(int id)
2708{
2709 win_T *wp = win_id2wp(id);
2710
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002711 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002712 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002713 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002714 return NULL;
2715 }
2716 return wp;
2717}
2718
2719/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002720 * popup_close({id})
2721 */
2722 void
2723f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2724{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002725 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002726 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002727
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002728 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2729 return;
2730
2731 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002732 if (
2733# ifdef FEAT_TERMINAL
2734 // if the popup contains a terminal it will become hidden
2735 curbuf->b_term == NULL &&
2736# endif
2737 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002738 return;
2739
2740 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002741 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002742 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002743}
2744
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002745 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002746popup_hide(win_T *wp)
2747{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002748#ifdef FEAT_TERMINAL
2749 if (error_if_term_popup_window())
2750 return;
2751#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002752 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2753 return;
2754
2755 wp->w_popup_flags |= POPF_HIDDEN;
2756 // Do not decrement b_nwindows, we still reference the buffer.
Christian Brabandtf00f4d92024-09-03 18:20:13 +02002757 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
2758 clear_cmdline = TRUE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002759 redraw_all_later(UPD_NOT_VALID);
2760 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002761}
2762
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002763/*
2764 * popup_hide({id})
2765 */
2766 void
2767f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2768{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002769 int id;
2770 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002771
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002772 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2773 return;
2774
2775 id = (int)tv_get_number(argvars);
2776 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002777 if (wp == NULL)
2778 return;
2779
2780 popup_hide(wp);
2781 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002782}
2783
2784 void
2785popup_show(win_T *wp)
2786{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002787 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2788 return;
2789
2790 wp->w_popup_flags &= ~POPF_HIDDEN;
2791 redraw_all_later(UPD_NOT_VALID);
2792 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002793}
2794
2795/*
2796 * popup_show({id})
2797 */
2798 void
2799f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2800{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002801 int id;
2802 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002803
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002804 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2805 return;
2806
2807 id = (int)tv_get_number(argvars);
2808 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002809 if (wp == NULL)
2810 return;
2811
2812 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2813 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002814#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002815 if (wp->w_popup_flags & POPF_INFO)
2816 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002817#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002818}
2819
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002820/*
2821 * popup_settext({id}, {text})
2822 */
2823 void
2824f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2825{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002826 int id;
2827 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002828
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002829 if (in_vim9script()
2830 && (check_for_number_arg(argvars, 0) == FAIL
2831 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2832 return;
2833
2834 id = (int)tv_get_number(&argvars[0]);
2835 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002836 if (wp == NULL)
2837 return;
2838
2839 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2840 return;
2841
2842 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2843 redraw_win_later(wp, UPD_NOT_VALID);
2844 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002845}
2846
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002847/*
2848 * popup_setbuf({id}, {bufnr})
2849 */
2850 void
2851f_popup_setbuf(typval_T *argvars, typval_T *rettv UNUSED)
2852{
2853 int id;
2854 win_T *wp;
2855 buf_T *buf;
2856
2857 rettv->v_type = VAR_BOOL;
2858 rettv->vval.v_number = VVAL_FALSE;
2859
2860 if (check_for_number_arg(argvars, 0) == FAIL
2861 || check_for_buffer_arg(argvars, 1) == FAIL)
2862 return;
2863
2864 id = (int)tv_get_number(&argvars[0]);
2865 wp = find_popup_win(id);
2866 if (wp == NULL)
2867 return;
2868
2869 buf = tv_get_buf_from_arg(&argvars[1]);
2870
2871 if (buf == NULL)
2872 return;
2873#ifdef FEAT_TERMINAL
2874 if (buf->b_term != NULL && popup_terminal_exists())
2875 {
2876 emsg(_(e_cannot_open_second_popup_with_terminal));
2877 return;
2878 }
2879#endif
2880
2881 if (wp->w_buffer != buf)
2882 {
2883 wp->w_buffer->b_nwindows--;
2884 win_init_popup_win(wp, buf);
2885 set_local_options_default(wp, FALSE);
2886 swap_exists_action = SEA_READONLY;
2887 buffer_ensure_loaded(buf);
2888 swap_exists_action = SEA_NONE;
2889 redraw_win_later(wp, UPD_NOT_VALID);
2890 popup_adjust_position(wp);
2891 }
2892 rettv->vval.v_number = VVAL_TRUE;
2893}
2894
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002895 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002896popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002897{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002898 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002899 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002900 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002901 clear_cmdline = TRUE;
2902 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002903
Bram Moolenaar43568642022-08-28 13:02:45 +01002904#ifdef HAS_MESSAGE_WINDOW
2905 if (wp == message_win)
2906 message_win = NULL;
2907#endif
2908
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002909 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002910 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002911}
2912
Bram Moolenaar82106932020-03-13 14:34:38 +01002913 static void
2914error_for_popup_window(void)
2915{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002916 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002917}
2918
2919 int
2920error_if_popup_window(int also_with_term UNUSED)
2921{
2922 // win_execute() may set "curwin" to a popup window temporarily, but many
2923 // commands are disallowed then. When a terminal runs in the popup most
2924 // things are allowed. When a terminal is finished it can be closed.
2925 if (WIN_IS_POPUP(curwin)
2926# ifdef FEAT_TERMINAL
2927 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002928# endif
2929 )
2930 {
2931 error_for_popup_window();
2932 return TRUE;
2933 }
2934 return FALSE;
2935}
2936
Bram Moolenaarec583842019-05-26 14:11:23 +02002937/*
2938 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002939 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002940 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002941 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002942 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002943popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002944{
2945 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002946 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002947 win_T *prev = NULL;
2948
Bram Moolenaarec583842019-05-26 14:11:23 +02002949 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002950 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002951 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002952 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002953 if (wp == curwin)
2954 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002955 if (!force)
2956 {
2957 error_for_popup_window();
2958 return FAIL;
2959 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002960 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002961 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002962 if (prev == NULL)
2963 first_popupwin = wp->w_next;
2964 else
2965 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002966 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002967 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002968 }
2969
Bram Moolenaarec583842019-05-26 14:11:23 +02002970 // go through tab-local popups
2971 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002972 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002973 return OK;
2974 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002975}
2976
2977/*
2978 * Close a popup window with Window-id "id" in tabpage "tp".
2979 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002980 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002981popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002982{
2983 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002984 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002985 win_T *prev = NULL;
2986
Bram Moolenaarec583842019-05-26 14:11:23 +02002987 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2988 if (wp->w_id == id)
2989 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002990 if (wp == curwin)
2991 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002992 if (!force)
2993 {
2994 error_for_popup_window();
2995 return FAIL;
2996 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002997 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002998 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002999 if (prev == NULL)
3000 *root = wp->w_next;
3001 else
3002 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02003003 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02003004 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02003005 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02003006 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003007}
3008
3009 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003010close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003011{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003012 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003013 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003014 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003015 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003016 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003017 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003018 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003019 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003020}
3021
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003022/*
3023 * popup_move({id}, {options})
3024 */
3025 void
3026f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
3027{
Bram Moolenaarae943152019-06-16 22:54:14 +02003028 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003029 int id;
3030 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003031
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003032 if (in_vim9script()
3033 && (check_for_number_arg(argvars, 0) == FAIL
3034 || check_for_dict_arg(argvars, 1) == FAIL))
3035 return;
3036
3037 id = (int)tv_get_number(argvars);
3038 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003039 if (wp == NULL)
3040 return; // invalid {id}
3041
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003042 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003043 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003044 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003045
Bram Moolenaarae943152019-06-16 22:54:14 +02003046 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003047
3048 if (wp->w_winrow + wp->w_height >= cmdline_row)
3049 clear_cmdline = TRUE;
3050 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003051}
3052
Bram Moolenaarbc133542019-05-29 20:26:46 +02003053/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003054 * popup_setoptions({id}, {options})
3055 */
3056 void
3057f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
3058{
3059 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003060 int id;
3061 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003062 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003063
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003064 if (in_vim9script()
3065 && (check_for_number_arg(argvars, 0) == FAIL
3066 || check_for_dict_arg(argvars, 1) == FAIL))
3067 return;
3068
3069 id = (int)tv_get_number(argvars);
3070 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02003071 if (wp == NULL)
3072 return; // invalid {id}
3073
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003074 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02003075 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003076 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003077 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003078
Christian Brabandtf6cdab32023-08-11 23:42:02 +02003079 (void)apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02003080
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003081 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003082 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003083 popup_adjust_position(wp);
3084}
3085
3086/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003087 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003088 */
3089 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003090f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003091{
3092 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003093 int id;
3094 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003095 int top_extra;
3096 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003097
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003098 if (rettv_dict_alloc(rettv) == FAIL)
3099 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003100
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003101 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3102 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003103
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003104 id = (int)tv_get_number(argvars);
3105 wp = find_popup_win(id);
3106 if (wp == NULL)
3107 return; // invalid {id}
3108 top_extra = popup_top_extra(wp);
3109 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003110
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003111 // we know how much space we need, avoid resizing halfway
3112 dict = rettv->vval.v_dict;
3113 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003114
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003115 dict_add_number(dict, "line", wp->w_winrow + 1);
3116 dict_add_number(dict, "col", wp->w_wincol + 1);
3117 dict_add_number(dict, "width", wp->w_width + left_extra
3118 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3119 dict_add_number(dict, "height", wp->w_height + top_extra
3120 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003121
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003122 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3123 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3124 dict_add_number(dict, "core_width", wp->w_width);
3125 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003126
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003127 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3128 dict_add_number(dict, "firstline", wp->w_topline);
3129 dict_add_number(dict, "lastline", wp->w_botline - 1);
3130 dict_add_number(dict, "visible",
3131 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3132
3133 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003134}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003135
3136/*
3137 * popup_list()
3138 */
3139 void
3140f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3141{
3142 win_T *wp;
3143 tabpage_T *tp;
3144
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003145 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003146 return;
3147 FOR_ALL_POPUPWINS(wp)
3148 list_append_number(rettv->vval.v_list, wp->w_id);
3149 FOR_ALL_TABPAGES(tp)
3150 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3151 list_append_number(rettv->vval.v_list, wp->w_id);
3152}
3153
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003154/*
3155 * popup_locate({row}, {col})
3156 */
3157 void
3158f_popup_locate(typval_T *argvars, typval_T *rettv)
3159{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003160 int row;
3161 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003162 win_T *wp;
3163
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003164 if (in_vim9script()
3165 && (check_for_number_arg(argvars, 0) == FAIL
3166 || check_for_number_arg(argvars, 1) == FAIL))
3167 return;
3168
3169 row = tv_get_number(&argvars[0]) - 1;
3170 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003171 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003172 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003173 rettv->vval.v_number = wp->w_id;
3174}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003175
3176/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003177 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3178 */
3179 static void
3180get_padding_border(dict_T *dict, int *array, char *name)
3181{
3182 list_T *list;
3183 int i;
3184
3185 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3186 return;
3187
3188 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003189 if (list == NULL)
3190 return;
3191
3192 dict_add_list(dict, name, list);
3193 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3194 for (i = 0; i < 4; ++i)
3195 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003196}
3197
3198/*
3199 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3200 */
3201 static void
3202get_borderhighlight(dict_T *dict, win_T *wp)
3203{
3204 list_T *list;
3205 int i;
3206
3207 for (i = 0; i < 4; ++i)
3208 if (wp->w_border_highlight[i] != NULL)
3209 break;
3210 if (i == 4)
3211 return;
3212
3213 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003214 if (list == NULL)
3215 return;
3216
3217 dict_add_list(dict, "borderhighlight", list);
3218 for (i = 0; i < 4; ++i)
3219 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003220}
3221
3222/*
3223 * For popup_getoptions(): add a "borderchars" entry to "dict".
3224 */
3225 static void
3226get_borderchars(dict_T *dict, win_T *wp)
3227{
3228 list_T *list;
3229 int i;
3230 char_u buf[NUMBUFLEN];
3231 int len;
3232
3233 for (i = 0; i < 8; ++i)
3234 if (wp->w_border_char[i] != 0)
3235 break;
3236 if (i == 8)
3237 return;
3238
3239 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003240 if (list == NULL)
3241 return;
3242
3243 dict_add_list(dict, "borderchars", list);
3244 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003245 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003246 len = mb_char2bytes(wp->w_border_char[i], buf);
3247 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003248 }
3249}
3250
3251/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003252 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003253 */
3254 static void
3255get_moved_list(dict_T *dict, win_T *wp)
3256{
3257 list_T *list;
3258
3259 list = list_alloc();
3260 if (list != NULL)
3261 {
3262 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003263 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003264 list_append_number(list, wp->w_popup_mincol);
3265 list_append_number(list, wp->w_popup_maxcol);
3266 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003267 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003268 if (list == NULL)
3269 return;
3270
3271 dict_add_list(dict, "mousemoved", list);
3272 list_append_number(list, wp->w_popup_mouse_row);
3273 list_append_number(list, wp->w_popup_mouse_mincol);
3274 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003275}
3276
3277/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003278 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003279 */
3280 void
3281f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3282{
3283 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003284 int id;
3285 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003286 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003287 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003288
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003289 if (rettv_dict_alloc(rettv) == FAIL)
3290 return;
3291
3292 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3293 return;
3294
3295 id = (int)tv_get_number(argvars);
3296 wp = find_popup_win(id);
3297 if (wp == NULL)
3298 return;
3299
3300 dict = rettv->vval.v_dict;
3301 dict_add_number(dict, "line", wp->w_wantline);
3302 dict_add_number(dict, "col", wp->w_wantcol);
3303 dict_add_number(dict, "minwidth", wp->w_minwidth);
3304 dict_add_number(dict, "minheight", wp->w_minheight);
3305 dict_add_number(dict, "maxheight", wp->w_maxheight);
3306 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3307 dict_add_number(dict, "firstline", wp->w_firstline);
3308 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3309 dict_add_number(dict, "zindex", wp->w_zindex);
3310 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3311 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003312 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003313 proptype_T *pt = text_prop_type_by_id(
3314 wp->w_popup_prop_win->w_buffer,
3315 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003316
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003317 if (pt != NULL)
3318 dict_add_string(dict, "textprop", pt->pt_name);
3319 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3320 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3321 }
3322 dict_add_string(dict, "title", wp->w_popup_title);
3323 dict_add_number(dict, "wrap", wp->w_p_wrap);
3324 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3325 dict_add_number(dict, "dragall",
3326 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3327 dict_add_number(dict, "mapping",
3328 (wp->w_popup_flags & POPF_MAPPING) != 0);
3329 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3330 dict_add_number(dict, "posinvert",
3331 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3332 dict_add_number(dict, "cursorline",
3333 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3334 dict_add_string(dict, "highlight", wp->w_p_wcr);
3335 if (wp->w_scrollbar_highlight != NULL)
3336 dict_add_string(dict, "scrollbarhighlight",
3337 wp->w_scrollbar_highlight);
3338 if (wp->w_thumb_highlight != NULL)
3339 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003340
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003341 // find the tabpage that holds this popup
3342 i = 1;
3343 FOR_ALL_TABPAGES(tp)
3344 {
3345 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003346
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003347 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3348 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003349 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003350 if (twp != NULL)
3351 break;
3352 ++i;
3353 }
3354 if (tp == NULL)
3355 i = -1; // must be global
3356 else if (tp == curtab)
3357 i = 0;
3358 dict_add_number(dict, "tabpage", i);
3359
3360 get_padding_border(dict, wp->w_popup_padding, "padding");
3361 get_padding_border(dict, wp->w_popup_border, "border");
3362 get_borderhighlight(dict, wp);
3363 get_borderchars(dict, wp);
3364 get_moved_list(dict, wp);
3365
3366 if (wp->w_filter_cb.cb_name != NULL)
3367 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3368 if (wp->w_close_cb.cb_name != NULL)
3369 dict_add_callback(dict, "callback", &wp->w_close_cb);
3370
3371 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3372 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3373 {
3374 dict_add_string(dict, "pos",
3375 (char_u *)poppos_entries[i].pp_name);
3376 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003377 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003378
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003379 dict_add_string(dict, "close", (char_u *)(
3380 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3381 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003382
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003383# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003384 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3385 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003386# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003387}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003388
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003389# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003390/*
3391 * Return TRUE if the current window is running a terminal in a popup window.
3392 * Return FALSE when the job has ended.
3393 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003394 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003395error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003396{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003397 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3398 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003399 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003400 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003401 return TRUE;
3402 }
3403 return FALSE;
3404}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003405# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003406
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003407/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003408 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003409 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003410 * Each calling function should use a different flag, see the list at
3411 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003412 */
3413 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003414popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003415{
3416 win_T *wp;
3417
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003418 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003419 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003420 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003421 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003422}
3423
3424/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003425 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003426 * Must have called popup_reset_handled() first.
3427 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3428 * popup with the highest zindex.
3429 */
3430 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003431find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003432{
3433 win_T *wp;
3434 win_T *found_wp;
3435 int found_zindex;
3436
3437 found_zindex = lowest ? INT_MAX : 0;
3438 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003439 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003440 if ((wp->w_popup_handled & handled_flag) == 0
3441 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003442 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003443 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003444 {
3445 found_zindex = wp->w_zindex;
3446 found_wp = wp;
3447 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003448 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003449 if ((wp->w_popup_handled & handled_flag) == 0
3450 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003451 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003452 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003453 {
3454 found_zindex = wp->w_zindex;
3455 found_wp = wp;
3456 }
3457
3458 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003459 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003460 return found_wp;
3461}
3462
3463/*
3464 * Invoke the filter callback for window "wp" with typed character "c".
3465 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003466 * Returns the return value of the filter or -1 for CTRL-C in the current
3467 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003468 * Careful: The filter may make "wp" invalid!
3469 */
3470 static int
3471invoke_popup_filter(win_T *wp, int c)
3472{
3473 int res;
3474 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003475 typval_T argv[3];
3476 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003477 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003478 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003479
Bram Moolenaara42d9452019-06-15 21:46:30 +02003480 // Emergency exit: CTRL-C closes the popup.
3481 if (c == Ctrl_C)
3482 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003483 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003484 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003485
3486 // Reset got_int to avoid the callback isn't called.
3487 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003488 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003489 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003490
3491 // If the popup is the current window it probably fails to close. Then
3492 // do not consume the key.
3493 if (was_curwin && wp == curwin)
3494 return -1;
3495 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003496 }
3497
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003498 argv[0].v_type = VAR_NUMBER;
3499 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3500
3501 // Convert the number to a string, so that the function can use:
3502 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003503 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003504 argv[1].v_type = VAR_STRING;
3505 argv[1].vval.v_string = vim_strsave(buf);
3506
3507 argv[2].v_type = VAR_UNKNOWN;
3508
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003509 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003510 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3511 {
3512 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003513 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003514 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003515 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003516 }
3517 else
3518 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003519 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3520 popup_highlight_curline(wp);
3521
Bram Moolenaar371806e2020-10-22 13:44:54 +02003522 // If an error message was given always return FALSE, so that keys are
3523 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003524 // If we get three errors in a row then close the popup. Decrement the
3525 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3526 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003527 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003528 {
3529 wp->w_filter_errors += 10;
3530 if (wp->w_filter_errors >= 30)
3531 popup_close_with_retval(wp, -1);
3532 res = FALSE;
3533 }
3534 else
3535 {
3536 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3537 --wp->w_filter_errors;
3538 res = tv_get_bool(&rettv);
3539 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003540 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003541
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003542 vim_free(argv[1].vval.v_string);
3543 clear_tv(&rettv);
3544 return res;
3545}
3546
3547/*
3548 * Called when "c" was typed: invoke popup filter callbacks.
3549 * Returns TRUE when the character was consumed,
3550 */
3551 int
3552popup_do_filter(int c)
3553{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003554 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003555 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003556 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003557 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003558 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003559 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003560
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003561#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003562 // Popup window with terminal always gets focus.
3563 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3564 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003565#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003566
Bram Moolenaar934470e2019-09-01 23:27:05 +02003567 if (recursive)
3568 return FALSE;
3569 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003570
Bram Moolenaarf6396232019-08-24 19:36:00 +02003571 if (c == K_LEFTMOUSE)
3572 {
3573 int row = mouse_row;
3574 int col = mouse_col;
3575
3576 wp = mouse_find_win(&row, &col, FIND_POPUP);
3577 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003578 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003579 }
3580
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003581 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003582 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003583 while (res == FALSE
3584 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003585 if (wp->w_filter_cb.cb_name != NULL
3586 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003587 res = invoke_popup_filter(wp, c);
3588
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003589 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003590 {
3591 int save_got_int = got_int;
3592
3593 // Reset got_int to avoid a function used in the statusline aborts.
3594 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003595 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003596 got_int |= save_got_int;
3597 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003598 recursive = FALSE;
3599 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003600
3601 // When interrupted return FALSE to avoid looping.
3602 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003603}
3604
Bram Moolenaar3397f742019-06-02 18:40:06 +02003605/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003606 * Return TRUE if there is a popup visible with a filter callback and the
3607 * "mapping" property off.
3608 */
3609 int
3610popup_no_mapping(void)
3611{
3612 int round;
3613 win_T *wp;
3614
3615 for (round = 1; round <= 2; ++round)
3616 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3617 wp != NULL; wp = wp->w_next)
3618 if (wp->w_filter_cb.cb_name != NULL
3619 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3620 return TRUE;
3621 return FALSE;
3622}
3623
3624/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003625 * Called when the cursor moved: check if any popup needs to be closed if the
3626 * cursor moved far enough.
3627 */
3628 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003629popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003630{
3631 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003632
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003633 popup_reset_handled(POPUP_HANDLED_3);
3634 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003635 if (wp->w_popup_curwin != NULL
3636 && (curwin != wp->w_popup_curwin
3637 || curwin->w_cursor.lnum != wp->w_popup_lnum
3638 || curwin->w_cursor.col < wp->w_popup_mincol
3639 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003640 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003641}
3642
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003643/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003644 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003645 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003646 static void
3647popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003648{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003649 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003650 char_u *cells;
3651 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003652
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003653 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3654 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003655 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003656 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003657 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003658 if (wp->w_popup_mask_cells != NULL
3659 && wp->w_popup_mask_height == height
3660 && wp->w_popup_mask_width == width)
3661 return; // cache is still valid
3662
3663 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003664 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003665 if (wp->w_popup_mask_cells == NULL)
3666 return;
3667 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003668
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003669 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003670 {
3671 int cols, cole;
3672 int lines, linee;
3673
3674 li = lio->li_tv.vval.v_list->lv_first;
3675 cols = tv_get_number(&li->li_tv);
3676 if (cols < 0)
3677 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003678 if (cols <= 0)
3679 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003680 li = li->li_next;
3681 cole = tv_get_number(&li->li_tv);
3682 if (cole < 0)
3683 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003684 if (cole > width)
3685 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003686 li = li->li_next;
3687 lines = tv_get_number(&li->li_tv);
3688 if (lines < 0)
3689 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003690 if (lines <= 0)
3691 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003692 li = li->li_next;
3693 linee = tv_get_number(&li->li_tv);
3694 if (linee < 0)
3695 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003696 if (linee > height)
3697 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003698
Bram Moolenaar4012d262020-12-29 11:57:46 +01003699 for (row = lines - 1; row < linee; ++row)
3700 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003701 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003702 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003703}
3704
3705/*
3706 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3707 * "col" and "line" are screen coordinates.
3708 */
3709 static int
3710popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3711{
3712 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3713 int line = screenline - wp->w_winrow;
3714
3715 return col >= 0 && col < width
3716 && line >= 0 && line < height
3717 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003718}
3719
3720/*
3721 * Set flags in popup_transparent[] for window "wp" to "val".
3722 */
3723 static void
3724update_popup_transparent(win_T *wp, int val)
3725{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003726 if (wp->w_popup_mask == NULL)
3727 return;
3728
3729 int width = popup_width(wp);
3730 int height = popup_height(wp);
3731 listitem_T *lio, *li;
3732 int cols, cole;
3733 int lines, linee;
3734 int col, line;
3735
3736 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003737 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003738 li = lio->li_tv.vval.v_list->lv_first;
3739 cols = tv_get_number(&li->li_tv);
3740 if (cols < 0)
3741 cols = width + cols + 1;
3742 li = li->li_next;
3743 cole = tv_get_number(&li->li_tv);
3744 if (cole < 0)
3745 cole = width + cole + 1;
3746 li = li->li_next;
3747 lines = tv_get_number(&li->li_tv);
3748 if (lines < 0)
3749 lines = height + lines + 1;
3750 li = li->li_next;
3751 linee = tv_get_number(&li->li_tv);
3752 if (linee < 0)
3753 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003754
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003755 --cols;
3756 cols -= wp->w_popup_leftoff;
3757 if (cols < 0)
3758 cols = 0;
3759 cole -= wp->w_popup_leftoff;
3760 --lines;
3761 if (lines < 0)
3762 lines = 0;
3763 for (line = lines; line < linee
3764 && line + wp->w_winrow < screen_Rows; ++line)
3765 for (col = cols; col < cole
3766 && col + wp->w_wincol < screen_Columns; ++col)
3767 popup_transparent[(line + wp->w_winrow) * screen_Columns
3768 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003769 }
3770}
3771
3772/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003773 * Only called when popup window "wp" is hidden: If the window is positioned
3774 * next to a text property, and it is now visible, then unhide the popup.
3775 * We don't check if visible popups become hidden, that is done in
3776 * popup_adjust_position().
3777 * Return TRUE if the popup became unhidden.
3778 */
3779 static int
3780check_popup_unhidden(win_T *wp)
3781{
3782 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3783 {
3784 textprop_T prop;
3785 linenr_T lnum;
3786
Bram Moolenaar27724252022-05-08 15:00:04 +01003787 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3788 && find_visible_prop(wp->w_popup_prop_win,
3789 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003790 &prop, &lnum) == OK)
3791 {
3792 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003793 wp->w_popup_prop_topline = 0; // force repositioning
3794 return TRUE;
3795 }
3796 }
3797 return FALSE;
3798}
3799
3800/*
3801 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3802 * That is when the buffer in the popup was changed, or the popup is following
3803 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003804 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003805 */
3806 static int
3807popup_need_position_adjust(win_T *wp)
3808{
3809 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3810 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003811 if (win_valid(wp->w_popup_prop_win)
3812 && (wp->w_popup_prop_changedtick
3813 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3814 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3815 return TRUE;
3816
3817 // May need to adjust the width if the cursor moved.
3818 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003819}
3820
3821/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003822 * Update "popup_mask" if needed.
3823 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003824 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003825 * Also marks window lines for redrawing.
3826 */
3827 void
3828may_update_popup_mask(int type)
3829{
3830 win_T *wp;
3831 short *mask;
3832 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003833 int redraw_all_popups = FALSE;
3834 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003835
3836 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003837 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3838 // basic (such as the screen size) must have changed.
3839 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003840 {
3841 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003842 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003843 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003844
3845 // Check if any popup window buffer has changed and if any popup connected
3846 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003847 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003848 if (wp->w_popup_flags & POPF_HIDDEN)
3849 popup_mask_refresh |= check_popup_unhidden(wp);
3850 else if (popup_need_position_adjust(wp))
3851 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003852 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003853 if (wp->w_popup_flags & POPF_HIDDEN)
3854 popup_mask_refresh |= check_popup_unhidden(wp);
3855 else if (popup_need_position_adjust(wp))
3856 popup_mask_refresh = TRUE;
3857
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003858 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003859 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003860
3861 // Need to update the mask, something has changed.
3862 popup_mask_refresh = FALSE;
3863 popup_mask_tab = curtab;
3864 popup_visible = FALSE;
3865
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003866 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003867 // If redrawing only what is needed, update "popup_mask_next" and then
3868 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003869 redrawing_all_win = TRUE;
3870 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003871 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003872 redrawing_all_win = FALSE;
3873 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003874 mask = popup_mask;
3875 else
3876 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003877 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003878
3879 // Find the window with the lowest zindex that hasn't been handled yet,
3880 // so that the window with a higher zindex overwrites the value in
3881 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003882 popup_reset_handled(POPUP_HANDLED_4);
3883 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003884 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003885 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003886 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003887
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003888 popup_visible = TRUE;
3889
Bram Moolenaar12034e22019-08-25 22:25:02 +02003890 // Recompute the position if the text changed. It may make the popup
3891 // hidden if it's attach to a text property that is no longer visible.
3892 if (redraw_all_popups || popup_need_position_adjust(wp))
3893 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003894 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003895 if (wp->w_popup_flags & POPF_HIDDEN)
3896 continue;
3897 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003898
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003899 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003900 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003901 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003902 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003903 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003904 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003905 col < wp->w_wincol + width - wp->w_popup_leftoff
3906 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003907 if (wp->w_zindex < POPUPMENU_ZINDEX
3908 && pum_visible()
3909 && pum_under_menu(line, col, FALSE))
3910 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3911 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003912 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003913 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003914 }
3915
3916 // Only check which lines are to be updated if not already
3917 // updating all lines.
3918 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003919 {
3920 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3921 win_T *prev_wp = NULL;
3922
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003923 for (line = 0; line < screen_Rows; ++line)
3924 {
3925 int col_done = 0;
3926
3927 for (col = 0; col < screen_Columns; ++col)
3928 {
3929 int off = line * screen_Columns + col;
3930
3931 if (popup_mask[off] != popup_mask_next[off])
3932 {
3933 popup_mask[off] = popup_mask_next[off];
3934
3935 if (line >= cmdline_row)
3936 {
3937 // the command line needs to be cleared if text below
3938 // the popup is now visible.
3939 if (!msg_scrolled && popup_mask_next[off] == 0)
3940 clear_cmdline = TRUE;
3941 }
3942 else if (col >= col_done)
3943 {
3944 linenr_T lnum;
3945 int line_cp = line;
3946 int col_cp = col;
3947
3948 // The screen position "line" / "col" needs to be
3949 // redrawn. Figure out what window that is and update
zeertzjqf2d90a32024-02-12 20:28:01 +01003950 // w_redraw_top and w_redraw_bot. Only needs to be
3951 // done once for each window line.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003952 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3953 if (wp != NULL)
3954 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003955#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003956 // A terminal window needs to be redrawn.
3957 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003958 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003959 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003960#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003961 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003962 if (wp != prev_wp)
3963 {
3964 vim_memset(plines_cache, 0,
3965 sizeof(int) * Rows);
3966 prev_wp = wp;
3967 }
3968
3969 if (line_cp >= wp->w_height)
3970 // In (or below) status line
3971 wp->w_redr_status = TRUE;
3972 else
3973 {
3974 // compute the position in the buffer line
3975 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003976 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003977 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003978 redrawWinline(wp, lnum);
3979 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003980 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003981
3982 // This line is going to be redrawn, no need to
3983 // check until the right side of the window.
3984 col_done = wp->w_wincol + wp->w_width - 1;
3985 }
3986 }
3987 }
3988 }
3989 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003990
3991 vim_free(plines_cache);
3992 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003993
3994 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003995}
3996
3997/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003998 * If the current window is a popup and something relevant changed, recompute
3999 * the position and size.
4000 */
4001 void
4002may_update_popup_position(void)
4003{
4004 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
4005 popup_adjust_position(curwin);
4006}
4007
4008/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004009 * Return a string of "len" spaces in IObuff.
4010 */
4011 static char_u *
4012get_spaces(int len)
4013{
4014 vim_memset(IObuff, ' ', (size_t)len);
4015 IObuff[len] = NUL;
4016 return IObuff;
4017}
4018
4019/*
4020 * Update popup windows. They are drawn on top of normal windows.
4021 * "win_update" is called for each popup window, lowest zindex first.
4022 */
4023 void
4024update_popups(void (*win_update)(win_T *wp))
4025{
4026 win_T *wp;
4027 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004028 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004029 int total_width;
4030 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004031 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004032 int popup_attr;
4033 int border_attr[4];
4034 int border_char[8];
4035 char_u buf[MB_MAXBYTES];
4036 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004037 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004038 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004039 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004040 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02004041 int sb_thumb_top = 0;
4042 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004043 int attr_scroll = 0;
4044 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004045
Bram Moolenaarebec3e22020-11-28 20:22:06 +01004046 // hide the cursor until redrawing is done.
4047 cursor_off();
4048
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004049 // Find the window with the lowest zindex that hasn't been updated yet,
4050 // so that the window with a higher zindex is drawn later, thus goes on
4051 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01004052 popup_reset_handled(POPUP_HANDLED_5);
4053 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004054 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 int title_len = 0;
4056 int title_wincol;
4057
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004058 // This drawing uses the zindex of the popup window, so that it's on
4059 // top of the text but doesn't draw when another popup with higher
4060 // zindex is on top of the character.
4061 screen_zindex = wp->w_zindex;
4062
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004063 // Set flags in popup_transparent[] for masked cells.
4064 update_popup_transparent(wp, 1);
4065
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004066 // adjust w_winrow and w_wincol for border and padding, since
4067 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004068 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02004069 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
4070 - wp->w_popup_leftoff;
4071 if (wp->w_wincol + left_extra < 0)
4072 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004073 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004074 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004075
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004076 // Draw the popup text, unless it's off screen.
4077 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004078 {
Bram Moolenaar10476522020-09-24 22:57:31 +02004079 // May need to update the "cursorline" highlighting, which may also
4080 // change "topline"
4081 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4082 popup_highlight_curline(wp);
4083
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004084 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004085
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004086 // move the cursor into the visible lines, otherwise executing
4087 // commands with win_execute() may cause the text to jump.
4088 if (wp->w_cursor.lnum < wp->w_topline)
4089 wp->w_cursor.lnum = wp->w_topline;
4090 else if (wp->w_cursor.lnum >= wp->w_botline)
4091 wp->w_cursor.lnum = wp->w_botline - 1;
4092 }
4093
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 Moolenaar6a076442020-11-15 20:32:58 +01004096
4097 // Add offset for border and padding if not done already.
4098 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4099 {
4100 wp->w_wcol += left_extra;
4101 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4102 }
4103 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004104 {
4105 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004106 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004107 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108
Bram Moolenaareabddc42022-04-02 15:32:16 +01004109 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004110 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004111 popup_attr = get_wcr_attr(wp);
4112
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004113 if (wp->w_winrow + total_height > cmdline_row)
4114 wp->w_popup_flags |= POPF_ON_CMDLINE;
4115 else
4116 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4117
4118
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004119 // We can only use these line drawing characters when 'encoding' is
4120 // "utf-8" and 'ambiwidth' is "single".
4121 if (enc_utf8 && *p_ambw == 's')
4122 {
4123 border_char[0] = border_char[2] = 0x2550;
4124 border_char[1] = border_char[3] = 0x2551;
4125 border_char[4] = 0x2554;
4126 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004127 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4128 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004129 border_char[7] = 0x255a;
4130 }
4131 else
4132 {
4133 border_char[0] = border_char[2] = '-';
4134 border_char[1] = border_char[3] = '|';
4135 for (i = 4; i < 8; ++i)
4136 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004137 if (wp->w_popup_flags & POPF_RESIZE)
4138 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004139 }
4140 for (i = 0; i < 8; ++i)
4141 if (wp->w_border_char[i] != 0)
4142 border_char[i] = wp->w_border_char[i];
4143
4144 for (i = 0; i < 4; ++i)
4145 {
4146 border_attr[i] = popup_attr;
4147 if (wp->w_border_highlight[i] != NULL)
4148 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4149 }
4150
Bram Moolenaard91467f2020-11-21 12:42:09 +01004151 // Title goes on top of border or padding.
4152 title_wincol = wp->w_wincol + 1;
4153 if (wp->w_popup_title != NULL)
4154 {
rbtnnc6119412021-08-07 13:08:45 +02004155 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004156
Ralf Schandlbc869872021-05-28 14:12:14 +02004157 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004158 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004159 {
4160 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4161 char_u *title_text = alloc(title_byte_len + 1);
4162
4163 if (title_text != NULL)
4164 {
4165 trunc_string(wp->w_popup_title, title_text,
4166 total_width - 2, title_byte_len + 1);
4167 screen_puts(title_text, wp->w_winrow, title_wincol,
4168 wp->w_popup_border[0] > 0
4169 ? border_attr[0] : popup_attr);
4170 vim_free(title_text);
4171 }
4172
Bram Moolenaard91467f2020-11-21 12:42:09 +01004173 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004174 }
4175 else
4176 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4177 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004178 }
4179
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004180 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004181 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004182 if (wp->w_popup_border[0] > 0)
4183 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004184 // top border; do not draw over the title
4185 if (title_len > 0)
4186 {
4187 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4188 wincol < 0 ? 0 : wincol, title_wincol,
4189 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004190 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004191 border_char[0], border_attr[0]);
4192 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4193 title_wincol + title_len, wincol + total_width,
4194 border_char[0], border_char[0], border_attr[0]);
4195 }
4196 else
4197 {
4198 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4199 wincol < 0 ? 0 : wincol, wincol + total_width,
4200 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4201 ? border_char[4] : border_char[0],
4202 border_char[0], border_attr[0]);
4203 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004204 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004205 {
4206 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4207 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004208 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004209 }
4210 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004211 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4212 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004213
Bram Moolenaard529ba52019-07-02 23:13:53 +02004214 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4215 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004216 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004217 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004218 - wp->w_has_scrollbar;
4219 if (padcol < 0)
4220 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004221 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004222 padcol = 0;
4223 }
4224 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004225 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004226 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004227 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004228 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004229 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004230 // top padding and no border; do not draw over the title
4231 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004232 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004233 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004234 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004235 row += 1;
4236 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004237 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004238 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004239 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004240 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004241
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004242 // Compute scrollbar thumb position and size.
4243 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004244 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004245 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4246 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004247 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004248
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004249 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4250 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004251 if (wp->w_topline > 1 && sb_thumb_height == height)
4252 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004253 if (sb_thumb_height == 0)
4254 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004255 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004256 // it just fits, avoid divide by zero
4257 sb_thumb_top = 0;
4258 else
4259 sb_thumb_top = (wp->w_topline - 1
4260 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004261 * (wp->w_height - sb_thumb_height)
4262 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004263 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4264 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004265 last = total_height - top_off - wp->w_popup_border[2];
4266 if (sb_thumb_top >= last)
4267 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004268 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004269
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004270 if (wp->w_scrollbar_highlight != NULL)
4271 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4272 else
4273 attr_scroll = highlight_attr[HLF_PSB];
4274 if (wp->w_thumb_highlight != NULL)
4275 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4276 else
4277 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004278 }
4279
4280 for (i = wp->w_popup_border[0];
4281 i < total_height - wp->w_popup_border[2]; ++i)
4282 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004283 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004284 // left and right padding only needed next to the body
4285 int do_padding =
4286 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4287 && i < total_height - wp->w_popup_border[2]
4288 - wp->w_popup_padding[2];
4289
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004290 row = wp->w_winrow + i;
4291
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004292 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004293 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004294 {
4295 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004296 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004297 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004298 if (do_padding && wp->w_popup_padding[3] > 0)
4299 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004300 int col = wincol + wp->w_popup_border[3];
4301
Bram Moolenaard529ba52019-07-02 23:13:53 +02004302 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004303 pad_left = wp->w_popup_padding[3];
4304 if (col < 0)
4305 {
4306 pad_left += col;
4307 col = 0;
4308 }
4309 if (pad_left > 0)
4310 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4311 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004312 // scrollbar
4313 if (wp->w_has_scrollbar)
4314 {
4315 int line = i - top_off;
4316 int scroll_col = wp->w_wincol + total_width - 1
4317 - wp->w_popup_border[1];
4318
4319 if (line >= 0 && line < wp->w_height)
4320 screen_putchar(' ', row, scroll_col,
4321 line >= sb_thumb_top
4322 && line < sb_thumb_top + sb_thumb_height
4323 ? attr_thumb : attr_scroll);
4324 else
4325 screen_putchar(' ', row, scroll_col, popup_attr);
4326 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004327 // right border
4328 if (wp->w_popup_border[1] > 0)
4329 {
4330 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004331 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004332 }
4333 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004334 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004335 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004336 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004337 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4338 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004339 }
4340
4341 if (wp->w_popup_padding[2] > 0)
4342 {
4343 // bottom padding
4344 row = wp->w_winrow + wp->w_popup_border[0]
4345 + wp->w_popup_padding[0] + wp->w_height;
4346 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004347 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004348 }
4349
4350 if (wp->w_popup_border[2] > 0)
4351 {
4352 // bottom border
4353 row = wp->w_winrow + total_height - 1;
4354 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004355 wincol < 0 ? 0 : wincol,
4356 wincol + total_width,
4357 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004358 ? border_char[7] : border_char[2],
4359 border_char[2], border_attr[2]);
4360 if (wp->w_popup_border[1] > 0)
4361 {
4362 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004363 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004364 }
4365 }
4366
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004367 if (wp->w_popup_close == POPCLOSE_BUTTON)
4368 {
4369 // close button goes on top of anything at the top-right corner
4370 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004371 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004372 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4373 }
4374
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004375 update_popup_transparent(wp, 0);
4376
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004377 // Back to the normal zindex.
4378 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004379
4380#ifdef HAS_MESSAGE_WINDOW
4381 // if this was the message window popup may start the timer now
4382 may_start_message_win_timer(wp);
4383#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004384 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004385
4386#if defined(FEAT_SEARCH_EXTRA)
4387 // In case win_update() called start_search_hl().
4388 end_search_hl();
4389#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004390}
4391
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004392/*
4393 * Mark references in callbacks of one popup window.
4394 */
4395 static int
4396set_ref_in_one_popup(win_T *wp, int copyID)
4397{
4398 int abort = FALSE;
4399 typval_T tv;
4400
4401 if (wp->w_close_cb.cb_partial != NULL)
4402 {
4403 tv.v_type = VAR_PARTIAL;
4404 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4405 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4406 }
4407 if (wp->w_filter_cb.cb_partial != NULL)
4408 {
4409 tv.v_type = VAR_PARTIAL;
4410 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4411 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4412 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004413 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004414 return abort;
4415}
4416
4417/*
4418 * Set reference in callbacks of popup windows.
4419 */
4420 int
4421set_ref_in_popups(int copyID)
4422{
4423 int abort = FALSE;
4424 win_T *wp;
4425 tabpage_T *tp;
4426
4427 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4428 abort = abort || set_ref_in_one_popup(wp, copyID);
4429
4430 FOR_ALL_TABPAGES(tp)
4431 {
4432 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4433 abort = abort || set_ref_in_one_popup(wp, copyID);
4434 if (abort)
4435 break;
4436 }
4437 return abort;
4438}
Bram Moolenaar79648732019-07-18 21:43:07 +02004439
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004440 int
4441popup_is_popup(win_T *wp)
4442{
4443 return wp->w_popup_flags != 0;
4444}
4445
4446#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004447/*
4448 * Find an existing popup used as the preview window, in the current tab page.
4449 * Return NULL if not found.
4450 */
4451 win_T *
4452popup_find_preview_window(void)
4453{
4454 win_T *wp;
4455
4456 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004457 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004458 if (wp->w_p_pvw)
4459 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004460 return NULL;
4461}
4462
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004463/*
4464 * Find an existing popup used as the info window, in the current tab page.
4465 * Return NULL if not found.
4466 */
4467 win_T *
4468popup_find_info_window(void)
4469{
4470 win_T *wp;
4471
4472 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004473 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004474 if (wp->w_popup_flags & POPF_INFO)
4475 return wp;
4476 return NULL;
4477}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004478#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004479
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004480 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004481f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4482{
4483#ifdef HAS_MESSAGE_WINDOW
4484 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4485#else
4486 rettv->vval.v_number = 0;
4487#endif
4488}
4489
4490 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004491f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4492{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004493#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004494 win_T *wp = popup_find_info_window();
4495
4496 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004497#else
4498 rettv->vval.v_number = 0;
4499#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004500}
4501
4502 void
4503f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004504{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004505#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004506 win_T *wp = popup_find_preview_window();
4507
4508 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004509#else
4510 rettv->vval.v_number = 0;
4511#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004512}
4513
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004514#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004515/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004516 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004517 * NOTE: this makes the popup the current window, so that the file can be
4518 * edited. However, it must not remain to be the current window, the caller
4519 * must make sure of that.
4520 */
4521 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004522popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004523{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004524 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004525
4526 if (wp == NULL)
4527 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004528 if (info)
4529 wp->w_popup_flags |= POPF_INFO;
4530 else
4531 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004532
4533 // Set the width to a reasonable value, so that w_topline can be computed.
4534 if (wp->w_minwidth > 0)
4535 wp->w_width = wp->w_minwidth;
4536 else if (wp->w_maxwidth > 0)
4537 wp->w_width = wp->w_maxwidth;
4538 else
4539 wp->w_width = curwin->w_width;
4540
4541 // Will switch to another buffer soon, dummy one can be wiped.
4542 wp->w_buffer->b_locked = FALSE;
4543
4544 win_enter(wp, FALSE);
4545 return OK;
4546}
4547
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004548/*
4549 * Close any preview popup.
4550 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004551 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004552popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004553{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004554 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004555
4556 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004557 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004558}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004559
4560/*
4561 * Hide the info popup.
4562 */
4563 void
4564popup_hide_info(void)
4565{
4566 win_T *wp = popup_find_info_window();
4567
4568 if (wp != NULL)
glepnirff159252025-03-01 16:17:00 +01004569 {
4570 popup_on_cmdline = wp->w_popup_flags & POPF_ON_CMDLINE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004571 popup_hide(wp);
glepnirff159252025-03-01 16:17:00 +01004572 }
4573}
4574
4575/*
4576 * Returns TRUE if a popup extends into the cmdline area.
4577 */
4578 int
4579popup_overlaps_cmdline(void)
4580{
4581 return popup_on_cmdline;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004582}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004583
4584/*
4585 * Close any info popup.
4586 */
4587 void
4588popup_close_info(void)
4589{
4590 win_T *wp = popup_find_info_window();
4591
4592 if (wp != NULL)
4593 popup_close_with_retval(wp, -1);
4594}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004595#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004596
Bram Moolenaar9198de32022-08-27 21:30:03 +01004597#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4598
Bram Moolenaar9198de32022-08-27 21:30:03 +01004599/*
4600 * Get the message window.
4601 * Returns NULL if something failed.
4602 */
4603 win_T *
4604popup_get_message_win(void)
4605{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004606 if (message_win != NULL)
4607 return message_win;
4608
4609 int i;
4610
4611 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4612
Bram Moolenaar9198de32022-08-27 21:30:03 +01004613 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004614 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004615
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004616 // use the full screen width
4617 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004618
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004619 // position at bottom of screen
4620 message_win->w_popup_pos = POPPOS_BOTTOM;
4621 message_win->w_wantcol = 1;
4622 message_win->w_minwidth = 9999;
4623 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004624
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004625 // no padding, border at the top
4626 for (i = 0; i < 4; ++i)
4627 message_win->w_popup_padding[i] = 0;
4628 for (i = 1; i < 4; ++i)
4629 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004630
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004631 if (message_win->w_popup_timer != NULL)
4632 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004633 return message_win;
4634}
4635
4636/*
4637 * If the message window is not visible: show it
4638 * If the message window is visible: reset the timeout
4639 */
4640 void
4641popup_show_message_win(void)
4642{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004643 if (message_win == NULL)
4644 return;
4645
4646 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004647 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004648 // the highlight may have changed.
4649 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4650 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004651 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004652 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004653}
4654
4655 static void
4656may_start_message_win_timer(win_T *wp)
4657{
4658 if (wp == message_win && start_message_win_timer)
4659 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004660 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004661 {
4662 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004663 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004664 message_win_time = 3000;
4665 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004666 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004667 }
4668}
4669
4670 int
4671popup_message_win_visible(void)
4672{
4673 return message_win != NULL
4674 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4675}
4676
4677/*
4678 * If the message window is visible: hide it.
4679 */
4680 void
4681popup_hide_message_win(void)
4682{
4683 if (message_win != NULL)
4684 popup_hide(message_win);
4685}
4686
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004687// Values saved in start_echowindow() and restored in end_echowindow()
4688static int save_msg_didout = FALSE;
4689static int save_msg_col = 0;
4690// Values saved in end_echowindow() and restored in start_echowindow()
4691static int ew_msg_didout = FALSE;
4692static int ew_msg_col = 0;
4693
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004694/*
4695 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004696 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004697 */
4698 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004699start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004700{
4701 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004702 save_msg_didout = msg_didout;
4703 save_msg_col = msg_col;
4704 msg_didout = ew_msg_didout;
4705 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004706 if (time_sec != 0)
4707 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004708}
4709
4710/*
4711 * Invoked after outputting a message for ":echowindow".
4712 */
4713 void
4714end_echowindow(void)
4715{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004716 in_echowindow = FALSE;
4717
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004718 if ((State & MODE_HITRETURN) == 0)
4719 // show the message window now
4720 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004721
4722 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004723 ew_msg_didout = TRUE;
4724 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4725 msg_didout = save_msg_didout;
4726 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004727}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004728#endif
4729
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004730/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004731 * Close any popup for a text property associated with "win".
4732 * Return TRUE if a popup was closed.
4733 */
4734 int
4735popup_win_closed(win_T *win)
4736{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004737 int round;
4738 win_T *wp;
4739 win_T *next;
4740 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004741
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004742 for (round = 1; round <= 2; ++round)
4743 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4744 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004745 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004746 next = wp->w_next;
4747 if (wp->w_popup_prop_win == win)
4748 {
4749 popup_close_with_retval(wp, -1);
4750 ret = TRUE;
4751 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004752 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004753 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004754}
4755
4756/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004757 * Set the title of the popup window to the file name.
4758 */
4759 void
4760popup_set_title(win_T *wp)
4761{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004762 if (wp->w_buffer->b_fname == NULL)
4763 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004764
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004765 char_u dirname[MAXPATHL];
4766 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004767
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004768 mch_dirname(dirname, MAXPATHL);
4769 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4770
4771 vim_free(wp->w_popup_title);
4772 len = STRLEN(wp->w_buffer->b_fname) + 3;
4773 wp->w_popup_title = alloc((int)len);
4774 if (wp->w_popup_title != NULL)
4775 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4776 wp->w_buffer->b_fname);
4777 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004778}
4779
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004780# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004781/*
4782 * If there is a preview window, update the title.
4783 * Used after changing directory.
4784 */
4785 void
4786popup_update_preview_title(void)
4787{
4788 win_T *wp = popup_find_preview_window();
4789
4790 if (wp != NULL)
4791 popup_set_title(wp);
4792}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004793# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004794
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004795#endif // FEAT_PROP_POPUP