blob: 8c567173f9e69cec58766833f727d8352b57da0c [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar43568642022-08-28 13:02:45 +010031#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +010032// Window used for ":echowindow"
Bram Moolenaar43568642022-08-28 13:02:45 +010033static win_T *message_win = NULL;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +010034
Bram Moolenaarbdc09a12022-10-07 14:31:45 +010035// Time used for the next ":echowindow" message in msec.
36static int message_win_time = 3000;
37
Bram Moolenaarcf0995d2022-09-11 21:36:17 +010038// Flag set when a message is added to the message window, timer is started
39// when the message window is drawn. This might be after pressing Enter at the
40// hit-enter prompt.
41static int start_message_win_timer = FALSE;
42
43static void may_start_message_win_timer(win_T *wp);
Bram Moolenaar43568642022-08-28 13:02:45 +010044#endif
45
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020046static void popup_adjust_position(win_T *wp);
47
Bram Moolenaar4d784b22019-05-25 19:51:39 +020048/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020049 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020051 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020052 */
53 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020054popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020055{
56 dictitem_T *di;
57 char_u *val;
58 char_u *s;
59 char_u *endp;
60 int n = 0;
61
62 di = dict_find(dict, key, -1);
63 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020064 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020065
66 val = tv_get_string(&di->di_tv);
67 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020068 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020069
70 setcursor_mayforce(TRUE);
71 s = val + 6;
72 if (*s != NUL)
73 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020074 endp = s;
75 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
76 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 if (endp != NULL && *skipwhite(endp) != NUL)
78 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020079 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020080 return 0;
81 }
82 }
83
84 if (STRCMP(key, "line") == 0)
85 n = screen_screenrow() + 1 + n;
86 else // "col"
87 n = screen_screencol() + 1 + n;
88
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020089 // Zero means "not set", use -1 instead.
90 if (n == 0)
91 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020092 return n;
93}
94
Christian Brabandtf6cdab32023-08-11 23:42:02 +020095 static int
Bram Moolenaarae943152019-06-16 22:54:14 +020096set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020097{
98 dictitem_T *di;
99
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000101 if (di == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200102 return OK;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000104 if (di->di_tv.v_type != VAR_LIST)
105 {
106 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200107 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000108 }
109
110 list_T *list = di->di_tv.vval.v_list;
111 listitem_T *li;
112 int i;
113 int nr;
114
115 for (i = 0; i < 4; ++i)
116 array[i] = 1;
117 if (list == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200118 return OK;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000119
120 CHECK_LIST_MATERIALIZE(list);
121 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
122 ++i, li = li->li_next)
123 {
124 nr = (int)tv_get_number(&li->li_tv);
125 if (nr >= 0)
126 array[i] = nr > max_val ? max_val : nr;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200127 }
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200128
129 return OK;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200130}
131
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200132/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200133 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200134 */
135 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200136set_moved_values(win_T *wp)
137{
138 wp->w_popup_curwin = curwin;
139 wp->w_popup_lnum = curwin->w_cursor.lnum;
140 wp->w_popup_mincol = curwin->w_cursor.col;
141 wp->w_popup_maxcol = curwin->w_cursor.col;
142}
143
144/*
145 * Used when popup options contain "moved" with "word" or "WORD".
146 */
147 static void
148set_moved_columns(win_T *wp, int flags)
149{
150 char_u *ptr;
151 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
152
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000153 if (len <= 0)
154 return;
155
156 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
157 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
Bram Moolenaar17627312019-06-02 19:53:44 +0200158}
159
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200160/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200161 * Used when popup options contain "mousemoved": set default moved values.
162 */
163 static void
164set_mousemoved_values(win_T *wp)
165{
166 wp->w_popup_mouse_row = mouse_row;
167 wp->w_popup_mouse_mincol = mouse_col;
168 wp->w_popup_mouse_maxcol = mouse_col;
169}
170
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000171 static void
172update_popup_uses_mouse_move(void)
173{
174 popup_uses_mouse_move = FALSE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000175 if (!popup_visible)
176 return;
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000177
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000178 win_T *wp;
179
180 FOR_ALL_POPUPWINS(wp)
181 if (wp->w_popup_mouse_row != 0)
182 {
183 popup_uses_mouse_move = TRUE;
184 return;
185 }
186 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
187 if (wp->w_popup_mouse_row != 0)
188 {
189 popup_uses_mouse_move = TRUE;
190 return;
191 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000192}
193
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200194/*
195 * Used when popup options contain "moved" with "word" or "WORD".
196 */
197 static void
198set_mousemoved_columns(win_T *wp, int flags)
199{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200200 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200201 char_u *text;
202 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 pos_T pos;
204 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205
206 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000207 &textwp, &pos.lnum, &text, NULL, &col) != OK)
208 return;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200209
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000210 // convert text column to mouse column
211 pos.col = col;
212 pos.coladd = 0;
213 getvcol(textwp, &pos, &mcol, NULL, NULL);
214 wp->w_popup_mouse_mincol = mcol;
215
216 pos.col = col + (colnr_T)STRLEN(text) - 1;
217 getvcol(textwp, &pos, NULL, NULL, &mcol);
218 wp->w_popup_mouse_maxcol = mcol;
219 vim_free(text);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200220}
221
222/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200223 * Return TRUE if "row"/"col" is on the border of the popup.
224 * The values are relative to the top-left corner.
225 */
226 int
227popup_on_border(win_T *wp, int row, int col)
228{
229 return (row == 0 && wp->w_popup_border[0] > 0)
230 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
231 || (col == 0 && wp->w_popup_border[3] > 0)
232 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
233}
234
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200235/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200236 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
237 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200238 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200239 * Caller should check the left mouse button was clicked.
240 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200241 */
242 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200243popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200244{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200245 if (wp->w_popup_close == POPCLOSE_BUTTON
246 && row == 0 && col == popup_width(wp) - 1)
247 {
248 popup_close_for_mouse_click(wp);
249 return TRUE;
250 }
251 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200252}
253
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254// Values set when dragging a popup window starts.
255static int drag_start_row;
256static int drag_start_col;
257static int drag_start_wantline;
258static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200259static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200260
261/*
262 * Mouse down on border of popup window: start dragging it.
263 * Uses mouse_col and mouse_row.
264 */
265 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200266popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200267{
268 drag_start_row = mouse_row;
269 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200270 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200271 drag_start_wantline = wp->w_winrow + 1;
272 else
273 drag_start_wantline = wp->w_wantline;
274 if (wp->w_wantcol == 0)
275 drag_start_wantcol = wp->w_wincol + 1;
276 else
277 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200278
279 // Stop centering the popup
280 if (wp->w_popup_pos == POPPOS_CENTER)
281 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282
283 drag_on_resize_handle = wp->w_popup_border[1] > 0
284 && wp->w_popup_border[2] > 0
285 && row == popup_height(wp) - 1
286 && col == popup_width(wp) - 1;
287
288 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
289 {
290 if (wp->w_popup_pos == POPPOS_TOPRIGHT
291 || wp->w_popup_pos == POPPOS_BOTRIGHT)
292 wp->w_wantcol = wp->w_wincol + 1;
293 if (wp->w_popup_pos == POPPOS_BOTLEFT)
294 wp->w_wantline = wp->w_winrow + 1;
295 wp->w_popup_pos = POPPOS_TOPLEFT;
296 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200297}
298
299/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200300 * Mouse moved while dragging a popup window: adjust the window popup position
301 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200302 */
303 void
304popup_drag(win_T *wp)
305{
306 // The popup may be closed before dragging stops.
307 if (!win_valid_popup(wp))
308 return;
309
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200310 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
311 {
312 int width_inc = mouse_col - drag_start_col;
313 int height_inc = mouse_row - drag_start_row;
314
315 if (width_inc != 0)
316 {
317 int width = wp->w_width + width_inc;
318
319 if (width < 1)
320 width = 1;
321 wp->w_minwidth = width;
322 wp->w_maxwidth = width;
323 drag_start_col = mouse_col;
324 }
325
326 if (height_inc != 0)
327 {
328 int height = wp->w_height + height_inc;
329
330 if (height < 1)
331 height = 1;
332 wp->w_minheight = height;
333 wp->w_maxheight = height;
334 drag_start_row = mouse_row;
335 }
336
337 popup_adjust_position(wp);
338 return;
339 }
340
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000341 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200342 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200343 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
344 if (wp->w_wantline < 1)
345 wp->w_wantline = 1;
346 if (wp->w_wantline > Rows)
347 wp->w_wantline = Rows;
348 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
349 if (wp->w_wantcol < 1)
350 wp->w_wantcol = 1;
351 if (wp->w_wantcol > Columns)
352 wp->w_wantcol = Columns;
353
354 popup_adjust_position(wp);
355}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200356
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200357/*
358 * Set w_firstline to match the current "wp->w_topline".
359 */
360 void
361popup_set_firstline(win_T *wp)
362{
363 int height = wp->w_height;
364
365 wp->w_firstline = wp->w_topline;
366 popup_adjust_position(wp);
367
368 // we don't want the popup to get smaller, decrement the first line
369 // until it doesn't
370 while (wp->w_firstline > 1 && wp->w_height < height)
371 {
372 --wp->w_firstline;
373 popup_adjust_position(wp);
374 }
375}
376
377/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200378 * Return TRUE if the position is in the popup window scrollbar.
379 */
380 int
381popup_is_in_scrollbar(win_T *wp, int row, int col)
382{
383 return wp->w_has_scrollbar
384 && row >= wp->w_popup_border[0]
385 && row < popup_height(wp) - wp->w_popup_border[2]
386 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
387}
388
389
390/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200391 * Handle a click in a popup window, if it is in the scrollbar.
392 */
393 void
394popup_handle_scrollbar_click(win_T *wp, int row, int col)
395{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000396 if (!popup_is_in_scrollbar(wp, row, col))
397 return;
Bram Moolenaard28950f2022-05-29 14:13:04 +0100398
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000399 int height = popup_height(wp);
400 int new_topline = wp->w_topline;
401
402 if (row >= height / 2)
403 {
404 // Click in lower half, scroll down.
405 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
406 ++new_topline;
407 }
408 else if (wp->w_topline > 1)
409 // click on upper half, scroll up.
410 --new_topline;
411
412 if (new_topline == wp->w_topline)
413 return;
414
415 set_topline(wp, new_topline);
416 if (wp == curwin)
417 {
418 if (wp->w_cursor.lnum < wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200419 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000420 wp->w_cursor.lnum = wp->w_topline;
421 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200422 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000423 else if (wp->w_cursor.lnum >= wp->w_botline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200424 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000425 wp->w_cursor.lnum = wp->w_botline - 1;
426 check_cursor();
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200427 }
428 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000429 popup_set_firstline(wp);
430 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200431}
432
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200433#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100434/*
435 * Add a timer to "wp" with "time".
436 * If "close" is true use popup_close(), otherwise popup_hide().
437 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200438 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100439popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200440{
441 char_u cbbuf[50];
442 char_u *ptr = cbbuf;
443 typval_T tv;
444
445 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100446 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
447 wp->w_id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000448 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) != OK)
449 return;
450
451 wp->w_popup_timer = create_timer(time, 0);
452 callback_T cb = get_callback(&tv);
453 if (cb.cb_name != NULL && !cb.cb_free_name)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200454 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000455 cb.cb_name = vim_strsave(cb.cb_name);
456 cb.cb_free_name = TRUE;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200457 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000458 wp->w_popup_timer->tr_callback = cb;
459 clear_tv(&tv);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200460}
461#endif
462
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100463 static poppos_T
464get_pos_entry(dict_T *d, int give_error)
465{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100466 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100467 int nr;
468
469 if (str == NULL)
470 return POPPOS_NONE;
471
K.Takataeeec2542021-06-02 13:28:16 +0200472 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100473 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
474 return poppos_entries[nr].pp_val;
475
476 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000477 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100478 return POPPOS_NONE;
479}
480
Bram Moolenaar17627312019-06-02 19:53:44 +0200481/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200482 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200483 */
484 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200485apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200486{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200487 int nr;
488 char_u *str;
489 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200490
Bram Moolenaard61efa52022-07-23 09:52:04 +0100491 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200492 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100493 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200494 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200496 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100497 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200498 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200499
500 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200501 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200502 wp->w_wantline = nr;
503 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200504 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200505 wp->w_wantcol = nr;
506
Bram Moolenaar55881332020-08-18 13:04:15 +0200507
Bram Moolenaard61efa52022-07-23 09:52:04 +0100508 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200509 if (nr != -1)
510 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200511
Bram Moolenaar12034e22019-08-25 22:25:02 +0200512 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100513 poppos_T ppt = get_pos_entry(d, TRUE);
514
515 if (ppt != POPPOS_NONE)
516 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200517 }
518
Bram Moolenaard61efa52022-07-23 09:52:04 +0100519 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200520 if (str != NULL)
521 {
522 wp->w_popup_prop_type = 0;
523 if (*str != NUL)
524 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100525 wp->w_popup_prop_win = curwin;
526 di = dict_find(d, (char_u *)"textpropwin", -1);
527 if (di != NULL)
528 {
529 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100530 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100531 wp->w_popup_prop_win = curwin;
532 }
533
534 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200535 if (nr <= 0)
536 nr = find_prop_type_id(str, NULL);
537 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000538 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200539 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200540 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200541 }
542 }
543
544 di = dict_find(d, (char_u *)"textpropid", -1);
545 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100546 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200547}
548
Bram Moolenaarb0992022020-01-30 14:55:42 +0100549/*
550 * Handle "moved" and "mousemoved" arguments.
551 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200552 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200553handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
554{
555 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
556 {
557 char_u *s = di->di_tv.vval.v_string;
558 int flags = 0;
559
560 if (STRCMP(s, "word") == 0)
561 flags = FIND_IDENT | FIND_STRING;
562 else if (STRCMP(s, "WORD") == 0)
563 flags = FIND_STRING;
564 else if (STRCMP(s, "expr") == 0)
565 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
566 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000567 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200568 if (flags != 0)
569 {
570 if (mousemoved)
571 set_mousemoved_columns(wp, flags);
572 else
573 set_moved_columns(wp, flags);
574 }
575 }
576 else if (di->di_tv.v_type == VAR_LIST
577 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200578 && (di->di_tv.vval.v_list->lv_len == 2
579 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200580 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200581 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100582 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200583 int mincol;
584 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200585
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200586 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100587 li = l->lv_first;
588 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200589 {
590 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
591
592 // Three numbers, might be from popup_getoptions().
593 if (mousemoved)
594 wp->w_popup_mouse_row = nr;
595 else
596 wp->w_popup_lnum = nr;
597 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100598 if (nr == 0)
599 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200600 }
601
602 mincol = tv_get_number(&li->li_tv);
603 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200604 if (mousemoved)
605 {
606 wp->w_popup_mouse_mincol = mincol;
607 wp->w_popup_mouse_maxcol = maxcol;
608 }
609 else
610 {
611 wp->w_popup_mincol = mincol;
612 wp->w_popup_maxcol = maxcol;
613 }
614 }
615 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000616 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200617}
618
619 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200620check_highlight(dict_T *dict, char *name, char_u **pval)
621{
622 dictitem_T *di;
623 char_u *str;
624
625 di = dict_find(dict, (char_u *)name, -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000626 if (di == NULL)
627 return;
628
629 if (di->di_tv.v_type != VAR_STRING)
630 semsg(_(e_invalid_value_for_argument_str), name);
631 else
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200632 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000633 str = tv_get_string(&di->di_tv);
634 if (*str != NUL)
635 *pval = vim_strsave(str);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200636 }
637}
638
Bram Moolenaarae943152019-06-16 22:54:14 +0200639/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200640 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200641 */
642 static void
643popup_show_curline(win_T *wp)
644{
645 if (wp->w_cursor.lnum < wp->w_topline)
646 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200647 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200648 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200649 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200650 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200651 if (wp->w_topline < 1)
652 wp->w_topline = 1;
653 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
654 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200655 while (wp->w_topline < wp->w_cursor.lnum
656 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
Luuk van Baal32d701f2024-04-28 16:24:02 +0200657 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum,
658 wp->w_height + 1) > wp->w_height)
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200659 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200660 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200661
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200662 // Don't let "firstline" cause a scroll.
663 if (wp->w_firstline > 0)
664 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200665}
666
667/*
668 * Get the sign group name for window "wp".
669 * Returns a pointer to a static buffer, overwritten on the next call.
670 */
671 static char_u *
672popup_get_sign_name(win_T *wp)
673{
674 static char buf[30];
675
676 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
677 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200678}
679
680/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200681 * Highlight the line with the cursor.
682 * Also scrolls the text to put the cursor line in view.
683 */
684 static void
685popup_highlight_curline(win_T *wp)
686{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200687 int sign_id = 0;
688 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200689
Bram Moolenaar72570732019-11-30 14:21:53 +0100690 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200691
692 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
693 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200694 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200695
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200696 if (!sign_exists_by_name(sign_name))
697 {
698 char *linehl = "PopupSelected";
699
700 if (syn_name2id((char_u *)linehl) == 0)
701 linehl = "PmenuSel";
LemonBoyb975ddf2024-07-06 18:04:09 +0200702 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL,
703 NULL, SIGN_DEF_PRIO);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200704 }
705
Bram Moolenaar72570732019-11-30 14:21:53 +0100706 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200707 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100708 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200709 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200710 else
711 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200712 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200713}
714
715/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200716 * Shared between popup_create() and f_popup_setoptions().
717 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200718 static int
Bram Moolenaarae943152019-06-16 22:54:14 +0200719apply_general_options(win_T *wp, dict_T *dict)
720{
721 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200722 int nr;
723 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200724
Bram Moolenaarae943152019-06-16 22:54:14 +0200725 // TODO: flip
726
727 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200728 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200729 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100730 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200731 if (wp->w_firstline < 0)
732 wp->w_firstline = -1;
733 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200734
Bram Moolenaard61efa52022-07-23 09:52:04 +0100735 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200736 if (nr != -1)
737 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200738
Bram Moolenaard61efa52022-07-23 09:52:04 +0100739 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200740 if (str != NULL)
741 {
742 vim_free(wp->w_popup_title);
743 wp->w_popup_title = vim_strsave(str);
744 }
745
Bram Moolenaard61efa52022-07-23 09:52:04 +0100746 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200747 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200748 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200749
Bram Moolenaard61efa52022-07-23 09:52:04 +0100750 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200751 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200752 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200753 if (nr)
754 wp->w_popup_flags |= POPF_DRAG;
755 else
756 wp->w_popup_flags &= ~POPF_DRAG;
757 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100758 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000759 if (nr != -1)
760 {
761 if (nr)
762 wp->w_popup_flags |= POPF_DRAGALL;
763 else
764 wp->w_popup_flags &= ~POPF_DRAGALL;
765 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200766
Bram Moolenaard61efa52022-07-23 09:52:04 +0100767 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200768 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100769 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100770 if (nr)
771 wp->w_popup_flags |= POPF_POSINVERT;
772 else
773 wp->w_popup_flags &= ~POPF_POSINVERT;
774 }
775
Bram Moolenaard61efa52022-07-23 09:52:04 +0100776 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200777 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200778 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200779 if (nr)
780 wp->w_popup_flags |= POPF_RESIZE;
781 else
782 wp->w_popup_flags &= ~POPF_RESIZE;
783 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200784
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200785 di = dict_find(dict, (char_u *)"close", -1);
786 if (di != NULL)
787 {
788 int ok = TRUE;
789
790 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
791 {
792 char_u *s = di->di_tv.vval.v_string;
793
794 if (STRCMP(s, "none") == 0)
795 wp->w_popup_close = POPCLOSE_NONE;
796 else if (STRCMP(s, "button") == 0)
797 wp->w_popup_close = POPCLOSE_BUTTON;
798 else if (STRCMP(s, "click") == 0)
799 wp->w_popup_close = POPCLOSE_CLICK;
800 else
801 ok = FALSE;
802 }
803 else
804 ok = FALSE;
805 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000806 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200807 }
808
Bram Moolenaard61efa52022-07-23 09:52:04 +0100809 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200810 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000811 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200812 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
813 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000814#ifdef FEAT_TERMINAL
815 term_update_wincolor(wp);
816#endif
817 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200818
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200819 if (set_padding_border(dict, wp->w_popup_padding, "padding", 999) == FAIL ||
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200820 set_padding_border(dict, wp->w_popup_border, "border", 1) == FAIL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200821 return FAIL;
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200822
Bram Moolenaar790498b2019-06-01 22:15:29 +0200823 di = dict_find(dict, (char_u *)"borderhighlight", -1);
824 if (di != NULL)
825 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200826 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200827 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000828 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200829 return FAIL;
830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 else
832 {
833 list_T *list = di->di_tv.vval.v_list;
834 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200835 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200836
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200837 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200838 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
839 ++i, li = li->li_next)
840 {
841 str = tv_get_string(&li->li_tv);
842 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100843 {
844 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200845 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100846 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200847 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200848 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
849 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100850 {
851 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200852 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200853 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100854 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200855 }
856 }
857
Bram Moolenaar790498b2019-06-01 22:15:29 +0200858 di = dict_find(dict, (char_u *)"borderchars", -1);
859 if (di != NULL)
860 {
861 if (di->di_tv.v_type != VAR_LIST)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200862 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000863 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200864 return FAIL;
865 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200866 else
867 {
868 list_T *list = di->di_tv.vval.v_list;
869 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200870 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200871
872 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100873 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200874 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200875 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
876 ++i, li = li->li_next)
877 {
878 str = tv_get_string(&li->li_tv);
879 if (*str != NUL)
880 wp->w_border_char[i] = mb_ptr2char(str);
881 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200882 if (list->lv_len == 1)
883 for (i = 1; i < 8; ++i)
884 wp->w_border_char[i] = wp->w_border_char[0];
885 if (list->lv_len == 2)
886 {
887 for (i = 4; i < 8; ++i)
888 wp->w_border_char[i] = wp->w_border_char[1];
889 for (i = 1; i < 4; ++i)
890 wp->w_border_char[i] = wp->w_border_char[0];
891 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200892 }
893 }
894 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200895
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200896 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
897 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
898
Bram Moolenaarae943152019-06-16 22:54:14 +0200899 di = dict_find(dict, (char_u *)"zindex", -1);
900 if (di != NULL)
901 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100902 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200903 if (wp->w_zindex < 1)
904 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
905 if (wp->w_zindex > 32000)
906 wp->w_zindex = 32000;
907 }
908
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200909 di = dict_find(dict, (char_u *)"mask", -1);
910 if (di != NULL)
911 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200912 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200914 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200915 {
916 listitem_T *li;
917
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200918 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200919 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200920 {
921 if (li->li_tv.v_type != VAR_LIST
922 || li->li_tv.vval.v_list == NULL
923 || li->li_tv.vval.v_list->lv_len != 4)
924 {
925 ok = FALSE;
926 break;
927 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100928 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200929 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200930 }
931 }
932 if (ok)
933 {
934 wp->w_popup_mask = di->di_tv.vval.v_list;
935 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200936 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200937 }
938 else
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200939 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000940 semsg(_(e_invalid_value_for_argument_str), "mask");
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200941 return FAIL;
942 }
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200943 }
944
Bram Moolenaarae943152019-06-16 22:54:14 +0200945#if defined(FEAT_TIMERS)
946 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100947 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200948 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100949 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200950#endif
951
Bram Moolenaar3397f742019-06-02 18:40:06 +0200952 di = dict_find(dict, (char_u *)"moved", -1);
953 if (di != NULL)
954 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200955 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200956 handle_moved_argument(wp, di, FALSE);
957 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200958
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200959 di = dict_find(dict, (char_u *)"mousemoved", -1);
960 if (di != NULL)
961 {
962 set_mousemoved_values(wp);
963 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200964 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200965
Bram Moolenaard61efa52022-07-23 09:52:04 +0100966 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100967 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200968 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100969 if (nr != 0)
970 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200971 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100972 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200973 }
974
Bram Moolenaarae943152019-06-16 22:54:14 +0200975 di = dict_find(dict, (char_u *)"filter", -1);
976 if (di != NULL)
977 {
978 callback_T callback = get_callback(&di->di_tv);
979
980 if (callback.cb_name != NULL)
981 {
982 free_callback(&wp->w_filter_cb);
983 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000984 if (callback.cb_free_name)
985 vim_free(callback.cb_name);
Bram Moolenaarae943152019-06-16 22:54:14 +0200986 }
987 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100988 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200989 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200990 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200991 if (nr)
992 wp->w_popup_flags |= POPF_MAPPING;
993 else
994 wp->w_popup_flags &= ~POPF_MAPPING;
995 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200996
Bram Moolenaard61efa52022-07-23 09:52:04 +0100997 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200998 if (str != NULL)
999 {
1000 if (STRCMP(str, "a") == 0)
1001 wp->w_filter_mode = MODE_ALL;
1002 else
1003 wp->w_filter_mode = mode_str2flags(str);
1004 }
1005
Bram Moolenaarae943152019-06-16 22:54:14 +02001006 di = dict_find(dict, (char_u *)"callback", -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001007 if (di == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001008 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001009
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001010 callback_T callback = get_callback(&di->di_tv);
1011 if (callback.cb_name == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001012 return OK;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001013
1014 free_callback(&wp->w_close_cb);
1015 set_callback(&wp->w_close_cb, &callback);
1016 if (callback.cb_free_name)
1017 vim_free(callback.cb_name);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001018
1019 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001020}
1021
1022/*
1023 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001024 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +02001025 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001026 static int
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001027apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001028{
1029 int nr;
1030
1031 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001032
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001033 if (create)
1034 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001035 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1036
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001037 if (apply_general_options(wp, dict) == FAIL)
1038 return FAIL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001039
Bram Moolenaard61efa52022-07-23 09:52:04 +01001040 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001041 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001042 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001043
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001044 // when "firstline" and "cursorline" are both set and the cursor would be
1045 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001046 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1047 {
1048 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1049 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001050 else if (wp->w_cursor.lnum < wp->w_firstline
1051 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001052 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001053 wp->w_topline = wp->w_firstline;
1054 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001055 }
1056
Bram Moolenaar33796b32019-06-08 16:01:13 +02001057 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001058 popup_highlight_curline(wp);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001059
1060 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001061}
1062
1063/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001064 * Add lines to the popup from a list of strings.
1065 */
1066 static void
1067add_popup_strings(buf_T *buf, list_T *l)
1068{
1069 listitem_T *li;
1070 linenr_T lnum = 0;
1071 char_u *p;
1072
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001073 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001074 if (li->li_tv.v_type == VAR_STRING)
1075 {
1076 p = li->li_tv.vval.v_string;
1077 ml_append_buf(buf, lnum++,
1078 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1079 }
1080}
1081
1082/*
1083 * Add lines to the popup from a list of dictionaries.
1084 */
1085 static void
1086add_popup_dicts(buf_T *buf, list_T *l)
1087{
1088 listitem_T *li;
1089 listitem_T *pli;
1090 linenr_T lnum = 0;
1091 char_u *p;
1092 dict_T *dict;
1093
1094 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001095 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001096 {
1097 if (li->li_tv.v_type != VAR_DICT)
1098 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001099 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100 return;
1101 }
1102 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001103 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001104 ml_append_buf(buf, lnum++,
1105 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1106 }
1107
1108 // add the text properties
1109 lnum = 1;
1110 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1111 {
1112 dictitem_T *di;
1113 list_T *plist;
1114
1115 dict = li->li_tv.vval.v_dict;
1116 di = dict_find(dict, (char_u *)"props", -1);
1117 if (di != NULL)
1118 {
1119 if (di->di_tv.v_type != VAR_LIST)
1120 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001121 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001122 return;
1123 }
1124 plist = di->di_tv.vval.v_list;
1125 if (plist != NULL)
1126 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001127 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001128 {
1129 if (pli->li_tv.v_type != VAR_DICT)
1130 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001131 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001132 return;
1133 }
1134 dict = pli->li_tv.vval.v_dict;
1135 if (dict != NULL)
1136 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001137 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001138
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001139 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001140 }
1141 }
1142 }
1143 }
1144 }
1145}
1146
1147/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001148 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1149 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001150 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001151popup_top_extra(win_T *wp)
1152{
1153 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1154
1155 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1156 return 1;
1157 return extra;
1158}
1159
1160/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001161 * Get the padding plus border at the left.
1162 */
1163 int
1164popup_left_extra(win_T *wp)
1165{
1166 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1167}
1168
1169/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001170 * Return the height of popup window "wp", including border and padding.
1171 */
1172 int
1173popup_height(win_T *wp)
1174{
1175 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001176 + popup_top_extra(wp)
1177 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001178}
1179
1180/*
1181 * Return the width of popup window "wp", including border, padding and
1182 * scrollbar.
1183 */
1184 int
1185popup_width(win_T *wp)
1186{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001187 // w_leftcol is how many columns of the core are left of the screen
1188 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001189 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001190 + popup_extra_width(wp)
1191 + wp->w_popup_rightoff;
1192}
1193
1194/*
1195 * Return the extra width of popup window "wp": border, padding and scrollbar.
1196 */
1197 int
1198popup_extra_width(win_T *wp)
1199{
1200 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1201 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1202 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001203}
1204
1205/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001206 * Adjust the position and size of the popup to fit on the screen.
1207 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001208 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001209popup_adjust_position(win_T *wp)
1210{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001211 linenr_T lnum;
1212 int wrapped = 0;
1213 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001214 int maxwidth_no_scrollbar;
1215 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001216 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001217 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001218 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001219 int center_vert = FALSE;
1220 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001221 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001222 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001223 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1224 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1225 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1226 int extra_height = top_extra + bot_extra;
1227 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001228 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001229 int org_winrow = wp->w_winrow;
1230 int org_wincol = wp->w_wincol;
1231 int org_width = wp->w_width;
1232 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001233 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001234 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001235 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001236 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001237 int wantline = wp->w_wantline; // adjusted for textprop
1238 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001239 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001240 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001241
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001242 wp->w_winrow = 0;
1243 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001244 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001245 wp->w_popup_leftoff = 0;
1246 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001247
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001248 // May need to update the "cursorline" highlighting, which may also change
1249 // "topline"
1250 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1251 popup_highlight_curline(wp);
1252
Bram Moolenaar12034e22019-08-25 22:25:02 +02001253 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1254 {
1255 win_T *prop_win = wp->w_popup_prop_win;
1256 textprop_T prop;
1257 linenr_T prop_lnum;
1258 pos_T pos;
1259 int screen_row;
1260 int screen_scol;
1261 int screen_ccol;
1262 int screen_ecol;
1263
1264 // Popup window is positioned relative to a text property.
1265 if (find_visible_prop(prop_win,
1266 wp->w_popup_prop_type, wp->w_popup_prop_id,
1267 &prop, &prop_lnum) == FAIL)
1268 {
1269 // Text property is no longer visible, hide the popup.
1270 // Unhiding the popup is done in check_popup_unhidden().
1271 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1272 {
1273 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001274 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001275 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001276 }
1277 return;
1278 }
1279
1280 // Compute the desired position from the position of the text
1281 // property. Use "wantline" and "wantcol" as offsets.
1282 pos.lnum = prop_lnum;
1283 pos.col = prop.tp_col;
1284 if (wp->w_popup_pos == POPPOS_TOPLEFT
1285 || wp->w_popup_pos == POPPOS_BOTLEFT)
1286 pos.col += prop.tp_len - 1;
1287 textpos2screenpos(prop_win, &pos, &screen_row,
1288 &screen_scol, &screen_ccol, &screen_ecol);
1289
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001290 if (screen_scol == 0)
1291 {
1292 // position is off screen, make the width zero to hide it.
1293 wp->w_width = 0;
1294 return;
1295 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001296 if (wp->w_popup_pos == POPPOS_TOPLEFT
1297 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1298 // below the text
1299 wantline = screen_row + wantline + 1;
1300 else
1301 // above the text
1302 wantline = screen_row + wantline - 1;
1303 center_vert = FALSE;
1304 if (wp->w_popup_pos == POPPOS_TOPLEFT
1305 || wp->w_popup_pos == POPPOS_BOTLEFT)
1306 // right of the text
1307 wantcol = screen_ecol + wantcol;
1308 else
1309 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001310 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001311 use_wantcol = TRUE;
1312 }
1313 else
1314 {
1315 // If no line was specified default to vertical centering.
1316 if (wantline == 0)
1317 center_vert = TRUE;
1318 else if (wantline < 0)
1319 // If "wantline" is negative it actually means zero.
1320 wantline = 0;
1321 if (wantcol < 0)
1322 // If "wantcol" is negative it actually means zero.
1323 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001324 }
1325
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001326 if (wp->w_popup_pos == POPPOS_CENTER)
1327 {
1328 // center after computing the size
1329 center_vert = TRUE;
1330 center_hor = TRUE;
1331 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001332 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001333 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001334 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1335 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001336 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001337 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001338 if (wp->w_winrow >= Rows)
1339 wp->w_winrow = Rows - 1;
1340 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001341 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001342 {
1343 // Assume that each buffer line takes one screen line, and one line
1344 // for the top border. First make sure cmdline_row is valid,
1345 // calling update_screen() will set it only later.
1346 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001347 wp->w_winrow = MAX(cmdline_row
1348 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001349 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001350
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001351 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001352 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001353 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1354 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001355 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001356 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001357 // Need to see at least one character after the decoration.
1358 if (wp->w_wincol > Columns - left_extra - 1)
1359 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001360 }
1361 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001362
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001363 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001364 // When left aligned use the space available, but shift to the left when we
1365 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001366 maxspace = Columns - wp->w_wincol - left_extra;
1367 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001369 {
1370 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001371 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001372 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001373
1374 if (wp->w_p_nu || wp->w_p_rnu)
1375 margin_width = number_width(wp) + 1;
1376#ifdef FEAT_FOLDING
1377 margin_width += wp->w_p_fdc;
1378#endif
1379#ifdef FEAT_SIGNS
1380 if (signcolumn_on(wp))
1381 margin_width += 2;
1382#endif
1383 if (margin_width >= maxwidth)
1384 margin_width = maxwidth - 1;
1385
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001386 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001387 minheight = wp->w_minheight;
1388#ifdef FEAT_TERMINAL
1389 // A terminal popup initially does not have content, use a default minimal
1390 // width of 20 characters and height of 5 lines.
1391 if (wp->w_buffer->b_term != NULL)
1392 {
1393 if (minwidth == 0)
1394 minwidth = 20;
1395 if (minheight == 0)
1396 minheight = 5;
1397 }
1398#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001399
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001400 if (wp->w_maxheight > 0)
1401 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001402 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1403 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001404
Bram Moolenaar8d241042019-06-12 23:40:01 +02001405 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001406 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001407 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001408 if (wp->w_topline < 1)
1409 wp->w_topline = 1;
1410 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001411 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1412
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001413 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001414 // Use a minimum width of one, so that something shows when there is no
1415 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001416 // When "firstline" is -1 then start with the last buffer line and go
1417 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001418 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001419 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001420 if (wp->w_firstline < 0)
1421 lnum = wp->w_buffer->b_ml.ml_line_count;
1422 else
1423 lnum = wp->w_topline;
1424 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001425 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001426 int len;
1427 int w_width = wp->w_width;
1428
1429 // Count Tabs for what they are worth and compute the length based on
1430 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001431 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001432 if (wp->w_width < maxwidth)
1433 wp->w_width = maxwidth;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001434 len = linetabsize(wp, lnum);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001435 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001436
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001437 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001438 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001439 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001440 {
1441 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001442 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001443 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001444 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001445 }
1446 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001447 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001448 && allow_adjust_left
1449 && (wp->w_popup_pos == POPPOS_TOPLEFT
1450 || wp->w_popup_pos == POPPOS_BOTLEFT))
1451 {
1452 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001453 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001454
Bram Moolenaar51c31312019-06-15 22:27:23 +02001455 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001456 {
1457 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001458
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001459 len -= truncate_shift;
1460 shift_by -= truncate_shift;
1461 }
1462
1463 wp->w_wincol -= shift_by;
1464 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001465 wp->w_width = maxwidth;
1466 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001467 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001468 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001469 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001470 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1471 wp->w_width = wp->w_maxwidth;
1472 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001473
1474 if (wp->w_firstline < 0)
1475 --lnum;
1476 else
1477 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001478
1479 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001480 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001481 && (wp->w_firstline >= 0
1482 ? lnum - wp->w_topline
1483 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001484 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001485 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001486 }
1487
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001488 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001489 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001490
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001491 wp->w_has_scrollbar = wp->w_want_scrollbar
1492 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001493#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001494 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1495 // Terminal window with running job never has a scrollbar, adjusts to
1496 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001497 wp->w_has_scrollbar = FALSE;
1498#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001499 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001500 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001501 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001502 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001503 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001504 // make space for the scrollbar if needed, when lines wrap and when
1505 // applying minwidth
1506 if (maxwidth + right_extra >= maxspace
1507 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1508 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001509 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001510
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001511 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1512 {
1513 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1514
1515 if (minwidth < title_len)
1516 minwidth = title_len;
1517 }
1518
1519 if (minwidth > 0 && wp->w_width < minwidth)
1520 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001521 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001522 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001523 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001524 // some columns cut off on the right
1525 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001526
1527 // If the window doesn't fit because 'minwidth' is set then the
1528 // scrollbar is at the far right of the screen, use the size without
1529 // the scrollbar.
1530 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1531 {
1532 int off = wp->w_width - maxwidth;
1533
1534 if (off > right_extra)
1535 extra_width -= right_extra;
1536 else
1537 extra_width -= off;
1538 wp->w_width = maxwidth_no_scrollbar;
1539 }
1540 else
1541 {
1542 wp->w_width = maxwidth;
1543
1544 // when adding a scrollbar below need to adjust the width
1545 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1546 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001547 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001548 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001549 {
1550 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1551 if (wp->w_wincol < 0)
1552 wp->w_wincol = 0;
1553 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001554 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1555 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1556 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001557 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001558
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001559 // Right aligned: move to the right if needed.
1560 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001561 if (leftoff >= 0)
1562 wp->w_wincol = leftoff;
1563 else if (wp->w_popup_fixed)
1564 {
1565 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001566 // columns when displaying the window, minus left border and
1567 // padding.
1568 if (-leftoff > left_extra)
1569 wp->w_leftcol = -leftoff - left_extra;
1570 wp->w_width -= wp->w_leftcol;
1571 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001572 if (wp->w_width < 0)
1573 wp->w_width = 0;
1574 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001575 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001576
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001577 if (wp->w_p_wrap || (!wp->w_popup_fixed
1578 && (wp->w_popup_pos == POPPOS_TOPLEFT
1579 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001580 {
1581 int want_col = 0;
1582
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001583 // try to show the right border and any scrollbar
1584 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001585 if (want_col > 0 && wp->w_wincol > 0
1586 && wp->w_wincol + want_col >= Columns)
1587 {
1588 wp->w_wincol = Columns - want_col;
1589 if (wp->w_wincol < 0)
1590 wp->w_wincol = 0;
1591 }
1592 }
1593
Bram Moolenaar8d241042019-06-12 23:40:01 +02001594 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1595 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001596 if (minheight > 0 && wp->w_height < minheight)
1597 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001598 if (maxheight > 0 && wp->w_height > maxheight)
1599 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001600 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001601 if (wp->w_height > Rows - wp->w_winrow)
1602 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001603
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001604 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 {
1606 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1607 if (wp->w_winrow < 0)
1608 wp->w_winrow = 0;
1609 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001610 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001611 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001612 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001613 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001614 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001615 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001616 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1617 {
1618 // Bottom aligned but does not fit, and less space on the other
1619 // side or "posinvert" is off: reduce height.
1620 wp->w_winrow = 0;
1621 wp->w_height = wantline - extra_height;
1622 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001623 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001624 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001625 // Not enough space and more space on the other side: make top
1626 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001627 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001628 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001629 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001630 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001631 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1632 || wp->w_popup_pos == POPPOS_TOPLEFT)
1633 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001634 if (wp != popup_dragwin
1635 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001636 && wantline * 2 > Rows
1637 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001638 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001639 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001640 // make bottom aligned and recompute the height
1641 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001642 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001643 if (wp->w_winrow < 0)
1644 {
1645 wp->w_height += wp->w_winrow;
1646 wp->w_winrow = 0;
1647 }
1648 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001649 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001650 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001651 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001652 adjust_height_for_top_aligned = TRUE;
1653 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001654 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001655
1656 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1657 && wp->w_winrow + wp->w_height + extra_height > Rows)
1658 {
1659 // Bottom of the popup goes below the last line, reduce the height and
1660 // add a scrollbar.
1661 wp->w_height = Rows - wp->w_winrow - extra_height;
1662#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001663 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001664#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001665 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001666 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001667 if (width_with_scrollbar > 0)
1668 wp->w_width = width_with_scrollbar;
1669 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001670 }
1671
1672 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001673 if (wp->w_winrow >= Rows)
1674 wp->w_winrow = Rows - 1;
1675 else if (wp->w_winrow < 0)
1676 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001677
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001678 if (wp->w_height != org_height)
1679 win_comp_scroll(wp);
1680
Bram Moolenaar17146962019-05-30 00:12:11 +02001681 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001682 if (win_valid(wp->w_popup_prop_win))
1683 {
1684 wp->w_popup_prop_changedtick =
1685 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1686 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1687 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001688
1689 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001690 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001691 if (org_winrow != wp->w_winrow
1692 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001693 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001694 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001695 || org_width != wp->w_width
1696 || org_height != wp->w_height)
1697 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001698 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001699 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1700 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001701 popup_mask_refresh = TRUE;
1702 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001703}
1704
Bram Moolenaar17627312019-06-02 19:53:44 +02001705typedef enum
1706{
1707 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001708 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001709 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001710 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001711 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001712 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001713 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001714 TYPE_PREVIEW, // preview window
1715 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001716} create_type_T;
1717
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001718/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001719 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1720 */
1721 static int
1722popup_is_notification(create_type_T type)
1723{
1724 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1725}
1726
1727/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001728 * Make "buf" empty and set the contents to "text".
1729 * Used by popup_create() and popup_settext().
1730 */
1731 static void
1732popup_set_buffer_text(buf_T *buf, typval_T text)
1733{
1734 int lnum;
1735
1736 // Clear the buffer, then replace the lines.
1737 curbuf = buf;
1738 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001739 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001740 curbuf = curwin->w_buffer;
1741
1742 // Add text to the buffer.
1743 if (text.v_type == VAR_STRING)
1744 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001745 char_u *s = text.vval.v_string;
1746
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001747 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001748 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001749 }
1750 else
1751 {
1752 list_T *l = text.vval.v_list;
1753
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001754 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001755 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001756 if (l->lv_first == &range_list_item)
1757 emsg(_(e_using_number_as_string));
1758 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001759 // list of strings
1760 add_popup_strings(buf, l);
1761 else
1762 // list of dictionaries
1763 add_popup_dicts(buf, l);
1764 }
1765 }
1766
1767 // delete the line that was in the empty buffer
1768 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001769 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001770 curbuf = curwin->w_buffer;
1771}
1772
1773/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001774 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1775 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 * Return FAIL if the parsing fails.
1777 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001778 static int
1779parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001780{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001781 char_u *p =
1782#ifdef FEAT_QUICKFIX
1783 !is_preview ? p_cpp :
1784#endif
1785 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001786
Bram Moolenaar258cef52019-08-21 17:29:29 +02001787 if (wp != NULL)
1788 wp->w_popup_flags &= ~POPF_INFO_MENU;
1789
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001790 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001791 {
1792 char_u *e, *dig;
1793 char_u *s = p;
1794 int x;
1795
1796 e = vim_strchr(p, ':');
1797 if (e == NULL || e[1] == NUL)
1798 return FAIL;
1799
1800 p = vim_strchr(e, ',');
1801 if (p == NULL)
1802 p = e + STRLEN(e);
1803 dig = e + 1;
1804 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001805
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001806 // Note: Keep this in sync with p_popup_option_values.
Bram Moolenaar79648732019-07-18 21:43:07 +02001807 if (STRNCMP(s, "height:", 7) == 0)
1808 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001809 if (dig != p)
1810 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001811 if (wp != NULL)
1812 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001813 if (is_preview)
1814 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001815 wp->w_maxheight = x;
1816 }
1817 }
1818 else if (STRNCMP(s, "width:", 6) == 0)
1819 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001820 if (dig != p)
1821 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001822 if (wp != NULL)
1823 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001824 if (is_preview)
1825 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001826 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001827 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001828 }
1829 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001830 else if (STRNCMP(s, "highlight:", 10) == 0)
1831 {
1832 if (wp != NULL)
1833 {
1834 int c = *p;
1835
1836 *p = NUL;
1837 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1838 s + 10, OPT_FREE|OPT_LOCAL, 0);
1839 *p = c;
1840 }
1841 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001842 else if (STRNCMP(s, "border:", 7) == 0)
1843 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001844 // Note: Keep this in sync with p_popup_option_border_values.
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001845 char_u *arg = s + 7;
1846 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1847 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1848 int i;
1849
1850 if (!on && !off)
1851 return FAIL;
1852 if (wp != NULL)
1853 {
1854 for (i = 0; i < 4; ++i)
1855 wp->w_popup_border[i] = on ? 1 : 0;
1856 if (off)
1857 // only show the X for close when there is a border
1858 wp->w_popup_close = POPCLOSE_NONE;
1859 }
1860 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001861 else if (STRNCMP(s, "align:", 6) == 0)
1862 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001863 // Note: Keep this in sync with p_popup_option_align_values.
Bram Moolenaar258cef52019-08-21 17:29:29 +02001864 char_u *arg = s + 6;
1865 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1866 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1867
1868 if (!menu && !item)
1869 return FAIL;
1870 if (wp != NULL && menu)
1871 wp->w_popup_flags |= POPF_INFO_MENU;
1872 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 else
1874 return FAIL;
1875 }
1876 return OK;
1877}
1878
1879/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001880 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1881 * is not NULL.
1882 * Return FAIL if the parsing fails.
1883 */
1884 int
1885parse_previewpopup(win_T *wp)
1886{
1887 return parse_popup_option(wp, TRUE);
1888}
1889
1890/*
1891 * Parse the 'completepopup' option and apply the values to window "wp" if it
1892 * is not NULL.
1893 * Return FAIL if the parsing fails.
1894 */
1895 int
1896parse_completepopup(win_T *wp)
1897{
1898 return parse_popup_option(wp, FALSE);
1899}
1900
1901/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001902 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001903 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001904 */
1905 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001906popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001907{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001908 poppos_T ppt = POPPOS_NONE;
1909
1910 if (d != NULL)
1911 ppt = get_pos_entry(d, FALSE);
1912
Bram Moolenaar79648732019-07-18 21:43:07 +02001913 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001914 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001915 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001916 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1917 }
1918 else
1919 {
1920 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1921 if (wp->w_wantline == 0) // cursor in first line
1922 {
1923 wp->w_wantline = 2;
1924 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1925 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1926 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001927 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001928
Bram Moolenaar79648732019-07-18 21:43:07 +02001929 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001930 if (wp->w_wantcol > Columns - width)
1931 {
1932 wp->w_wantcol = Columns - width;
1933 if (wp->w_wantcol < 1)
1934 wp->w_wantcol = 1;
1935 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001936
Bram Moolenaar79648732019-07-18 21:43:07 +02001937 popup_adjust_position(wp);
1938}
1939
1940/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001941 * Set w_wantline and w_wantcol for the a given screen position.
1942 * Caller must take care of running into the window border.
1943 */
1944 void
1945popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1946{
1947 wp->w_wantline = row;
1948 wp->w_wantcol = col;
1949 popup_adjust_position(wp);
1950}
1951
1952/*
1953 * Add a border and lef&right padding.
1954 */
1955 static void
1956add_border_left_right_padding(win_T *wp)
1957{
1958 int i;
1959
1960 for (i = 0; i < 4; ++i)
1961 {
1962 wp->w_popup_border[i] = 1;
1963 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1964 }
1965}
1966
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001967#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001968/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001969 * Return TRUE if there is any popup window with a terminal buffer.
1970 */
1971 static int
1972popup_terminal_exists(void)
1973{
1974 win_T *wp;
1975 tabpage_T *tp;
1976
1977 FOR_ALL_POPUPWINS(wp)
1978 if (wp->w_buffer->b_term != NULL)
1979 return TRUE;
1980 FOR_ALL_TABPAGES(tp)
1981 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1982 if (wp->w_buffer->b_term != NULL)
1983 return TRUE;
1984 return FALSE;
1985}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001986#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001987
1988/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001989 * Mark all popup windows in the current tab and global for redrawing.
1990 */
1991 void
1992popup_redraw_all(void)
1993{
1994 win_T *wp;
1995
1996 FOR_ALL_POPUPWINS(wp)
1997 wp->w_redr_type = UPD_NOT_VALID;
1998 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1999 wp->w_redr_type = UPD_NOT_VALID;
2000}
2001
2002/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01002003 * Set the color for a notification window.
2004 */
2005 static void
2006popup_update_color(win_T *wp, create_type_T type)
2007{
2008 char *hiname = type == TYPE_MESSAGE_WIN
2009 ? "MessageWindow" : "PopupNotification";
2010 int nr = syn_name2id((char_u *)hiname);
2011
2012 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
2013 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
2014 OPT_FREE|OPT_LOCAL, 0);
2015}
2016
2017/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002019 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02002020 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002021 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002022 */
Bram Moolenaara730e552019-06-16 19:05:31 +02002023 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02002024popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002025{
Bram Moolenaara3fce622019-06-20 02:31:49 +02002026 win_T *wp;
2027 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002028 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002029 int new_buffer;
2030 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002031 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002032 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002033
Bram Moolenaar79648732019-07-18 21:43:07 +02002034 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002035 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002036 if (in_vim9script()
2037 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2038 || check_for_dict_arg(argvars, 1) == FAIL))
2039 return NULL;
2040
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002041 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002042 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002043 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002044 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002045 if (buf == NULL)
2046 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002047 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002048 return NULL;
2049 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002050#ifdef FEAT_TERMINAL
2051 if (buf->b_term != NULL && popup_terminal_exists())
2052 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002053 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002054 return NULL;
2055 }
2056#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002057 }
2058 else if (!(argvars[0].v_type == VAR_STRING
2059 && argvars[0].vval.v_string != NULL)
2060 && !(argvars[0].v_type == VAR_LIST
2061 && argvars[0].vval.v_list != NULL))
2062 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002063 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002064 return NULL;
2065 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002066 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002067 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002068 d = argvars[1].vval.v_dict;
2069 }
2070
2071 if (d != NULL)
2072 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002073 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002074 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002075 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002076 tabnr = -1; // notifications are global by default
2077 else
2078 tabnr = 0;
2079 if (tabnr > 0)
2080 {
2081 tp = find_tabpage(tabnr);
2082 if (tp == NULL)
2083 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002084 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002085 return NULL;
2086 }
2087 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002088 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002089 else if (popup_is_notification(type))
2090 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002091
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002092 // Create the window and buffer.
2093 wp = win_alloc_popup_win();
2094 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002095 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002096 if (rettv != NULL)
2097 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002098 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002099 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002100
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002101 if (buf != NULL)
2102 {
2103 // use existing buffer
2104 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002105 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002106 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002107 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002108 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002109 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002110 }
2111 else
2112 {
2113 // create a new buffer associated with the popup
2114 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002115 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002116 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002117 {
2118 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002119 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002120 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002121 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002122
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002123 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002124
Bram Moolenaar46451042019-08-24 15:50:46 +02002125 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002126 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002127 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002128 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002129 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002130 buf->b_p_ul = -1; // no undo
2131 buf->b_p_swf = FALSE; // no swap file
2132 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002133 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002134
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002135 // Avoid that 'buftype' is reset when this buffer is entered.
2136 buf->b_p_initialized = TRUE;
2137 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002138 wp->w_p_wrap = TRUE; // 'wrap' is default on
2139 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002140
Bram Moolenaara3fce622019-06-20 02:31:49 +02002141 if (tp != NULL)
2142 {
2143 // popup on specified tab page
2144 wp->w_next = tp->tp_first_popupwin;
2145 tp->tp_first_popupwin = wp;
2146 }
2147 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002148 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002149 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002150 wp->w_next = curtab->tp_first_popupwin;
2151 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002152 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002153 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002154 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002155 win_T *prev = first_popupwin;
2156
2157 // Global popup: add at the end, so that it gets displayed on top of
2158 // older ones with the same zindex. Matters for notifications.
2159 if (first_popupwin == NULL)
2160 first_popupwin = wp;
2161 else
2162 {
2163 while (prev->w_next != NULL)
2164 prev = prev->w_next;
2165 prev->w_next = wp;
2166 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002167 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002168
Bram Moolenaar79648732019-07-18 21:43:07 +02002169 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002170 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002171
Bram Moolenaar79648732019-07-18 21:43:07 +02002172 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002173 {
2174 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002175 }
2176 if (type == TYPE_ATCURSOR)
2177 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002178 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002179 set_moved_values(wp);
2180 set_moved_columns(wp, FIND_STRING);
2181 }
2182
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002183 if (type == TYPE_BEVAL)
2184 {
2185 wp->w_popup_pos = POPPOS_BOTLEFT;
2186
2187 // by default use the mouse position
2188 wp->w_wantline = mouse_row;
2189 if (wp->w_wantline <= 0) // mouse on first line
2190 {
2191 wp->w_wantline = 2;
2192 wp->w_popup_pos = POPPOS_TOPLEFT;
2193 }
2194 wp->w_wantcol = mouse_col + 1;
2195 set_mousemoved_values(wp);
2196 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2197 }
2198
Bram Moolenaar33796b32019-06-08 16:01:13 +02002199 // set default values
2200 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002201 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002202
Bram Moolenaar9198de32022-08-27 21:30:03 +01002203 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002204 {
2205 win_T *twp, *nextwin;
2206 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002207
2208 // Try to not overlap with another global popup. Guess we need 3
2209 // more screen lines than buffer lines.
2210 wp->w_wantline = 1;
2211 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2212 {
2213 nextwin = twp->w_next;
2214 if (twp != wp
2215 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2216 && twp->w_winrow <= wp->w_wantline - 1 + height
2217 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2218 {
2219 // move to below this popup and restart the loop to check for
2220 // overlap with other popups
2221 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2222 nextwin = first_popupwin;
2223 }
2224 }
2225 if (wp->w_wantline + height > Rows)
2226 {
2227 // can't avoid overlap, put on top in the hope that message goes
2228 // away soon.
2229 wp->w_wantline = 1;
2230 }
2231
2232 wp->w_wantcol = 10;
2233 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002234 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002235 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002236 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002237 for (i = 0; i < 4; ++i)
2238 wp->w_popup_border[i] = 1;
2239 wp->w_popup_padding[1] = 1;
2240 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002241
Bram Moolenaar9198de32022-08-27 21:30:03 +01002242 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002243 }
2244
Bram Moolenaara730e552019-06-16 19:05:31 +02002245 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002246 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002247 wp->w_popup_pos = POPPOS_CENTER;
2248 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002249 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002250 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002251 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002252 }
2253
Bram Moolenaara730e552019-06-16 19:05:31 +02002254 if (type == TYPE_MENU)
2255 {
2256 typval_T tv;
2257 callback_T callback;
2258
2259 tv.v_type = VAR_STRING;
2260 tv.vval.v_string = (char_u *)"popup_filter_menu";
2261 callback = get_callback(&tv);
2262 if (callback.cb_name != NULL)
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002263 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002264 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002265 if (callback.cb_free_name)
2266 vim_free(callback.cb_name);
2267 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002268
2269 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002270 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002271 }
2272
Bram Moolenaar79648732019-07-18 21:43:07 +02002273 if (type == TYPE_PREVIEW)
2274 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002275 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002276 wp->w_popup_close = POPCLOSE_BUTTON;
2277 for (i = 0; i < 4; ++i)
2278 wp->w_popup_border[i] = 1;
2279 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002280 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002281 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002282# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002283 if (type == TYPE_INFO)
2284 {
2285 wp->w_popup_pos = POPPOS_TOPLEFT;
2286 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2287 wp->w_popup_close = POPCLOSE_BUTTON;
2288 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002289 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002290 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002291# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002292
Bram Moolenaarae943152019-06-16 22:54:14 +02002293 for (i = 0; i < 4; ++i)
2294 VIM_CLEAR(wp->w_border_highlight[i]);
2295 for (i = 0; i < 8; ++i)
2296 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002297 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002298 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002299 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002300
Bram Moolenaar79648732019-07-18 21:43:07 +02002301 if (d != NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002302 {
Bram Moolenaar79648732019-07-18 21:43:07 +02002303 // Deal with options.
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002304 if (apply_options(wp, d, TRUE) == FAIL)
2305 {
2306 (void)popup_close(wp->w_id, FALSE);
2307 return NULL;
2308 }
2309 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002310
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002311#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002312 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2313 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002314#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002315
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002316 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002317
2318 wp->w_vsep_width = 0;
2319
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002320 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002321 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002322
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002323#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002324 // When running a terminal in the popup it becomes the current window.
2325 if (buf->b_term != NULL)
2326 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002327#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002328
Bram Moolenaara730e552019-06-16 19:05:31 +02002329 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002330}
2331
2332/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002333 * popup_clear()
2334 */
2335 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002336f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002337{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002338 int force = FALSE;
2339
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002340 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2341 return;
2342
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002343 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002344 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002345 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002346}
2347
2348/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002349 * popup_create({text}, {options})
2350 */
2351 void
2352f_popup_create(typval_T *argvars, typval_T *rettv)
2353{
Bram Moolenaar17627312019-06-02 19:53:44 +02002354 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002355}
2356
2357/*
2358 * popup_atcursor({text}, {options})
2359 */
2360 void
2361f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2362{
Bram Moolenaar17627312019-06-02 19:53:44 +02002363 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002364}
2365
2366/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002367 * popup_beval({text}, {options})
2368 */
2369 void
2370f_popup_beval(typval_T *argvars, typval_T *rettv)
2371{
2372 popup_create(argvars, rettv, TYPE_BEVAL);
2373}
2374
2375/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002376 * Invoke the close callback for window "wp" with value "result".
2377 * Careful: The callback may make "wp" invalid!
2378 */
2379 static void
2380invoke_popup_callback(win_T *wp, typval_T *result)
2381{
2382 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002383 typval_T argv[3];
2384
Christian Brabandt6701abf2023-11-19 10:52:50 +01002385 rettv.v_type = VAR_UNKNOWN;
2386
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002387 argv[0].v_type = VAR_NUMBER;
2388 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2389
2390 if (result != NULL && result->v_type != VAR_UNKNOWN)
2391 copy_tv(result, &argv[1]);
2392 else
2393 {
2394 argv[1].v_type = VAR_NUMBER;
2395 argv[1].vval.v_number = 0;
2396 }
2397
2398 argv[2].v_type = VAR_UNKNOWN;
2399
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002400 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002401 if (result != NULL)
2402 clear_tv(&argv[1]);
2403 clear_tv(&rettv);
2404}
2405
2406/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002407 * Make "prevwin" the current window, unless it's equal to "wp".
2408 * Otherwise make "firstwin" the current window.
2409 */
2410 static void
2411back_to_prevwin(win_T *wp)
2412{
2413 if (win_valid(prevwin) && wp != prevwin)
2414 win_enter(prevwin, FALSE);
2415 else
2416 win_enter(firstwin, FALSE);
2417}
2418
2419/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002420 * Close popup "wp" and invoke any close callback for it.
2421 */
2422 static void
2423popup_close_and_callback(win_T *wp, typval_T *arg)
2424{
2425 int id = wp->w_id;
2426
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002427#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002428 if (wp == curwin && curbuf->b_term != NULL)
2429 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002430 win_T *owp;
2431
2432 // Closing popup window with a terminal: put focus back on the first
2433 // that works:
2434 // - another popup window with a terminal
2435 // - the previous window
2436 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002437 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002438 if (owp != curwin && owp->w_buffer->b_term != NULL)
2439 break;
2440 if (owp != NULL)
2441 win_enter(owp, FALSE);
2442 else
2443 {
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002444 FOR_ALL_POPUPWINS_IN_TAB(curtab, owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002445 if (owp != curwin && owp->w_buffer->b_term != NULL)
2446 break;
2447 if (owp != NULL)
2448 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002449 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002450 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002451 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002452 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002453#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002454
Bram Moolenaar49540192019-12-11 19:34:54 +01002455 // Just in case a check higher up is missing.
2456 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002457 {
2458 // To avoid getting stuck when win_execute() does something that causes
2459 // an error, stop calling the filter callback.
2460 free_callback(&wp->w_filter_cb);
2461
Bram Moolenaar49540192019-12-11 19:34:54 +01002462 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002463 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002464
Bram Moolenaarcee52202020-03-11 14:19:58 +01002465 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002466 if (wp->w_close_cb.cb_name != NULL)
2467 // Careful: This may make "wp" invalid.
2468 invoke_popup_callback(wp, arg);
2469
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002470 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002471 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002472}
2473
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002474 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002475popup_close_with_retval(win_T *wp, int retval)
2476{
2477 typval_T res;
2478
2479 res.v_type = VAR_NUMBER;
2480 res.vval.v_number = retval;
2481 popup_close_and_callback(wp, &res);
2482}
2483
Bram Moolenaar3397f742019-06-02 18:40:06 +02002484/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002485 * Close popup "wp" because of a mouse click.
2486 */
2487 void
2488popup_close_for_mouse_click(win_T *wp)
2489{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002490 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002491}
2492
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002493 static void
2494check_mouse_moved(win_T *wp, win_T *mouse_wp)
2495{
2496 // Close the popup when all if these are true:
2497 // - the mouse is not on this popup
2498 // - "mousemoved" was used
2499 // - the mouse is no longer on the same screen row or the mouse column is
2500 // outside of the relevant text
2501 if (wp != mouse_wp
2502 && wp->w_popup_mouse_row != 0
2503 && (wp->w_popup_mouse_row != mouse_row
2504 || mouse_col < wp->w_popup_mouse_mincol
2505 || mouse_col > wp->w_popup_mouse_maxcol))
2506 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002507 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002508 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002509 }
2510}
2511
2512/*
2513 * Called when the mouse moved: may close a popup with "mousemoved".
2514 */
2515 void
2516popup_handle_mouse_moved(void)
2517{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002518 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002519 win_T *mouse_wp;
2520 int row = mouse_row;
2521 int col = mouse_col;
2522
2523 // find the window where the mouse is in
2524 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2525
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002526 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2527 {
2528 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002529 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002530 }
2531 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2532 {
2533 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002534 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002535 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002536}
2537
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002538/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002539 * In a filter: check if the typed key is a mouse event that is used for
2540 * dragging the popup.
2541 */
2542 static void
2543filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2544{
2545 int row = mouse_row;
2546 int col = mouse_col;
2547
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002548 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002549 && is_mouse_key(c)
2550 && (wp == popup_dragwin
2551 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2552 // do not consume the key, allow for dragging the popup
2553 rettv->vval.v_number = 0;
2554}
2555
Bram Moolenaara730e552019-06-16 19:05:31 +02002556/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002557 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002558 */
2559 void
2560f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2561{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002562 int id;
2563 win_T *wp;
2564 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002565 typval_T res;
2566 int c;
2567 linenr_T old_lnum;
2568
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002569 if (in_vim9script()
2570 && (check_for_number_arg(argvars, 0) == FAIL
2571 || check_for_string_arg(argvars, 1) == FAIL))
2572 return;
2573
2574 id = tv_get_number(&argvars[0]);
2575 wp = win_id2wp(id);
2576 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002577 // If the popup has been closed do not consume the key.
2578 if (wp == NULL)
2579 return;
2580
2581 c = *key;
2582 if (c == K_SPECIAL && key[1] != NUL)
2583 c = TO_SPECIAL(key[1], key[2]);
2584
2585 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002586 rettv->v_type = VAR_BOOL;
2587 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002588 res.v_type = VAR_NUMBER;
2589
2590 old_lnum = wp->w_cursor.lnum;
Christian Brabandtbadeedd2023-08-13 19:25:28 +02002591 if (c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2592 {
2593 if (wp->w_cursor.lnum > 1)
2594 --wp->w_cursor.lnum;
2595 else
2596 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2597 }
2598 if (c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
2599 {
2600 if (wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2601 ++wp->w_cursor.lnum;
2602 else
2603 wp->w_cursor.lnum = 1;
2604 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002605 if (old_lnum != wp->w_cursor.lnum)
2606 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002607 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002608 return;
2609 }
2610
2611 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2612 {
2613 // Cancelled, invoke callback with -1
2614 res.vval.v_number = -1;
2615 popup_close_and_callback(wp, &res);
2616 return;
2617 }
2618 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2619 {
2620 // Invoke callback with current index.
2621 res.vval.v_number = wp->w_cursor.lnum;
2622 popup_close_and_callback(wp, &res);
2623 return;
2624 }
2625
2626 filter_handle_drag(wp, c, rettv);
2627}
2628
2629/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002630 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002631 */
2632 void
2633f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2634{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002635 int id;
2636 win_T *wp;
2637 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002638 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002639 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002640
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002641 if (in_vim9script()
2642 && (check_for_number_arg(argvars, 0) == FAIL
2643 || check_for_string_arg(argvars, 1) == FAIL))
2644 return;
2645
2646 id = tv_get_number(&argvars[0]);
2647 wp = win_id2wp(id);
2648 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002649 // If the popup has been closed don't consume the key.
2650 if (wp == NULL)
2651 return;
2652
Bram Moolenaara730e552019-06-16 19:05:31 +02002653 c = *key;
Ernie Raelb14c3252024-07-19 16:37:09 +02002654 if (c == CAR && need_wait_return)
2655 return;
Bram Moolenaara730e552019-06-16 19:05:31 +02002656 if (c == K_SPECIAL && key[1] != NUL)
2657 c = TO_SPECIAL(key[1], key[2]);
2658
Bram Moolenaara42d9452019-06-15 21:46:30 +02002659 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002660 rettv->v_type = VAR_BOOL;
2661 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002662
Bram Moolenaara730e552019-06-16 19:05:31 +02002663 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002664 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002665 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002666 res.vval.v_number = 0;
2667 else
2668 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002669 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002670 return;
2671 }
2672
2673 // Invoke callback
2674 res.v_type = VAR_NUMBER;
2675 popup_close_and_callback(wp, &res);
2676}
2677
2678/*
2679 * popup_dialog({text}, {options})
2680 */
2681 void
2682f_popup_dialog(typval_T *argvars, typval_T *rettv)
2683{
2684 popup_create(argvars, rettv, TYPE_DIALOG);
2685}
2686
2687/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002688 * popup_menu({text}, {options})
2689 */
2690 void
2691f_popup_menu(typval_T *argvars, typval_T *rettv)
2692{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002693 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002694}
2695
2696/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002697 * popup_notification({text}, {options})
2698 */
2699 void
2700f_popup_notification(typval_T *argvars, typval_T *rettv)
2701{
2702 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2703}
2704
2705/*
2706 * Find the popup window with window-ID "id".
2707 * If the popup window does not exist NULL is returned.
2708 * If the window is not a popup window, and error message is given.
2709 */
2710 static win_T *
2711find_popup_win(int id)
2712{
2713 win_T *wp = win_id2wp(id);
2714
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002715 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002716 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002717 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002718 return NULL;
2719 }
2720 return wp;
2721}
2722
2723/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002724 * popup_close({id})
2725 */
2726 void
2727f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2728{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002729 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002730 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002731
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002732 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2733 return;
2734
2735 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002736 if (
2737# ifdef FEAT_TERMINAL
2738 // if the popup contains a terminal it will become hidden
2739 curbuf->b_term == NULL &&
2740# endif
2741 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002742 return;
2743
2744 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002745 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002746 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002747}
2748
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002749 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002750popup_hide(win_T *wp)
2751{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002752#ifdef FEAT_TERMINAL
2753 if (error_if_term_popup_window())
2754 return;
2755#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002756 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2757 return;
2758
2759 wp->w_popup_flags |= POPF_HIDDEN;
2760 // Do not decrement b_nwindows, we still reference the buffer.
Christian Brabandtf00f4d92024-09-03 18:20:13 +02002761 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
2762 clear_cmdline = TRUE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002763 redraw_all_later(UPD_NOT_VALID);
2764 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002765}
2766
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002767/*
2768 * popup_hide({id})
2769 */
2770 void
2771f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2772{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002773 int id;
2774 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002775
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002776 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2777 return;
2778
2779 id = (int)tv_get_number(argvars);
2780 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002781 if (wp == NULL)
2782 return;
2783
2784 popup_hide(wp);
2785 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002786}
2787
2788 void
2789popup_show(win_T *wp)
2790{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002791 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2792 return;
2793
2794 wp->w_popup_flags &= ~POPF_HIDDEN;
2795 redraw_all_later(UPD_NOT_VALID);
2796 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002797}
2798
2799/*
2800 * popup_show({id})
2801 */
2802 void
2803f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2804{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002805 int id;
2806 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002807
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002808 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2809 return;
2810
2811 id = (int)tv_get_number(argvars);
2812 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002813 if (wp == NULL)
2814 return;
2815
2816 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2817 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002818#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002819 if (wp->w_popup_flags & POPF_INFO)
2820 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002821#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002822}
2823
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002824/*
2825 * popup_settext({id}, {text})
2826 */
2827 void
2828f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2829{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002830 int id;
2831 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002832
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002833 if (in_vim9script()
2834 && (check_for_number_arg(argvars, 0) == FAIL
2835 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2836 return;
2837
2838 id = (int)tv_get_number(&argvars[0]);
2839 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002840 if (wp == NULL)
2841 return;
2842
2843 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2844 return;
2845
2846 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2847 redraw_win_later(wp, UPD_NOT_VALID);
2848 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002849}
2850
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002851/*
2852 * popup_setbuf({id}, {bufnr})
2853 */
2854 void
2855f_popup_setbuf(typval_T *argvars, typval_T *rettv UNUSED)
2856{
2857 int id;
2858 win_T *wp;
2859 buf_T *buf;
2860
2861 rettv->v_type = VAR_BOOL;
2862 rettv->vval.v_number = VVAL_FALSE;
2863
2864 if (check_for_number_arg(argvars, 0) == FAIL
2865 || check_for_buffer_arg(argvars, 1) == FAIL)
2866 return;
2867
2868 id = (int)tv_get_number(&argvars[0]);
2869 wp = find_popup_win(id);
2870 if (wp == NULL)
2871 return;
2872
2873 buf = tv_get_buf_from_arg(&argvars[1]);
2874
2875 if (buf == NULL)
2876 return;
2877#ifdef FEAT_TERMINAL
2878 if (buf->b_term != NULL && popup_terminal_exists())
2879 {
2880 emsg(_(e_cannot_open_second_popup_with_terminal));
2881 return;
2882 }
2883#endif
2884
2885 if (wp->w_buffer != buf)
2886 {
2887 wp->w_buffer->b_nwindows--;
2888 win_init_popup_win(wp, buf);
2889 set_local_options_default(wp, FALSE);
2890 swap_exists_action = SEA_READONLY;
2891 buffer_ensure_loaded(buf);
2892 swap_exists_action = SEA_NONE;
2893 redraw_win_later(wp, UPD_NOT_VALID);
2894 popup_adjust_position(wp);
2895 }
2896 rettv->vval.v_number = VVAL_TRUE;
2897}
2898
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002899 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002900popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002901{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002902 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002903 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002904 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002905 clear_cmdline = TRUE;
2906 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002907
Bram Moolenaar43568642022-08-28 13:02:45 +01002908#ifdef HAS_MESSAGE_WINDOW
2909 if (wp == message_win)
2910 message_win = NULL;
2911#endif
2912
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002913 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002914 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002915}
2916
Bram Moolenaar82106932020-03-13 14:34:38 +01002917 static void
2918error_for_popup_window(void)
2919{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002920 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002921}
2922
2923 int
2924error_if_popup_window(int also_with_term UNUSED)
2925{
2926 // win_execute() may set "curwin" to a popup window temporarily, but many
2927 // commands are disallowed then. When a terminal runs in the popup most
2928 // things are allowed. When a terminal is finished it can be closed.
2929 if (WIN_IS_POPUP(curwin)
2930# ifdef FEAT_TERMINAL
2931 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002932# endif
2933 )
2934 {
2935 error_for_popup_window();
2936 return TRUE;
2937 }
2938 return FALSE;
2939}
2940
Bram Moolenaarec583842019-05-26 14:11:23 +02002941/*
2942 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002943 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002944 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002945 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002946 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002947popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002948{
2949 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002950 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002951 win_T *prev = NULL;
2952
Bram Moolenaarec583842019-05-26 14:11:23 +02002953 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002954 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002955 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002956 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002957 if (wp == curwin)
2958 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002959 if (!force)
2960 {
2961 error_for_popup_window();
2962 return FAIL;
2963 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002964 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002965 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002966 if (prev == NULL)
2967 first_popupwin = wp->w_next;
2968 else
2969 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002970 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002971 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002972 }
2973
Bram Moolenaarec583842019-05-26 14:11:23 +02002974 // go through tab-local popups
2975 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002976 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002977 return OK;
2978 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002979}
2980
2981/*
2982 * Close a popup window with Window-id "id" in tabpage "tp".
2983 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002984 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002985popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002986{
2987 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002988 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002989 win_T *prev = NULL;
2990
Bram Moolenaarec583842019-05-26 14:11:23 +02002991 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2992 if (wp->w_id == id)
2993 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002994 if (wp == curwin)
2995 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002996 if (!force)
2997 {
2998 error_for_popup_window();
2999 return FAIL;
3000 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003001 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01003002 }
Bram Moolenaarec583842019-05-26 14:11:23 +02003003 if (prev == NULL)
3004 *root = wp->w_next;
3005 else
3006 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02003007 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02003008 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02003009 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02003010 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003011}
3012
3013 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003014close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003015{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003016 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003017 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003018 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003019 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003020 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003021 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003022 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003023 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003024}
3025
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003026/*
3027 * popup_move({id}, {options})
3028 */
3029 void
3030f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
3031{
Bram Moolenaarae943152019-06-16 22:54:14 +02003032 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003033 int id;
3034 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003035
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003036 if (in_vim9script()
3037 && (check_for_number_arg(argvars, 0) == FAIL
3038 || check_for_dict_arg(argvars, 1) == FAIL))
3039 return;
3040
3041 id = (int)tv_get_number(argvars);
3042 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003043 if (wp == NULL)
3044 return; // invalid {id}
3045
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003046 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003047 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003048 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003049
Bram Moolenaarae943152019-06-16 22:54:14 +02003050 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003051
3052 if (wp->w_winrow + wp->w_height >= cmdline_row)
3053 clear_cmdline = TRUE;
3054 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003055}
3056
Bram Moolenaarbc133542019-05-29 20:26:46 +02003057/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003058 * popup_setoptions({id}, {options})
3059 */
3060 void
3061f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
3062{
3063 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003064 int id;
3065 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003066 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003067
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003068 if (in_vim9script()
3069 && (check_for_number_arg(argvars, 0) == FAIL
3070 || check_for_dict_arg(argvars, 1) == FAIL))
3071 return;
3072
3073 id = (int)tv_get_number(argvars);
3074 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02003075 if (wp == NULL)
3076 return; // invalid {id}
3077
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003078 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02003079 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003080 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003081 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003082
Christian Brabandtf6cdab32023-08-11 23:42:02 +02003083 (void)apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02003084
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003085 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003086 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003087 popup_adjust_position(wp);
3088}
3089
3090/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003091 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003092 */
3093 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003094f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003095{
3096 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003097 int id;
3098 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003099 int top_extra;
3100 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003101
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003102 if (rettv_dict_alloc(rettv) == FAIL)
3103 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003104
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003105 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3106 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003107
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003108 id = (int)tv_get_number(argvars);
3109 wp = find_popup_win(id);
3110 if (wp == NULL)
3111 return; // invalid {id}
3112 top_extra = popup_top_extra(wp);
3113 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003114
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003115 // we know how much space we need, avoid resizing halfway
3116 dict = rettv->vval.v_dict;
3117 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003118
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003119 dict_add_number(dict, "line", wp->w_winrow + 1);
3120 dict_add_number(dict, "col", wp->w_wincol + 1);
3121 dict_add_number(dict, "width", wp->w_width + left_extra
3122 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3123 dict_add_number(dict, "height", wp->w_height + top_extra
3124 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003125
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003126 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3127 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3128 dict_add_number(dict, "core_width", wp->w_width);
3129 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003130
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003131 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3132 dict_add_number(dict, "firstline", wp->w_topline);
3133 dict_add_number(dict, "lastline", wp->w_botline - 1);
3134 dict_add_number(dict, "visible",
3135 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3136
3137 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003138}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003139
3140/*
3141 * popup_list()
3142 */
3143 void
3144f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3145{
3146 win_T *wp;
3147 tabpage_T *tp;
3148
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003149 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003150 return;
3151 FOR_ALL_POPUPWINS(wp)
3152 list_append_number(rettv->vval.v_list, wp->w_id);
3153 FOR_ALL_TABPAGES(tp)
3154 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3155 list_append_number(rettv->vval.v_list, wp->w_id);
3156}
3157
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003158/*
3159 * popup_locate({row}, {col})
3160 */
3161 void
3162f_popup_locate(typval_T *argvars, typval_T *rettv)
3163{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003164 int row;
3165 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003166 win_T *wp;
3167
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003168 if (in_vim9script()
3169 && (check_for_number_arg(argvars, 0) == FAIL
3170 || check_for_number_arg(argvars, 1) == FAIL))
3171 return;
3172
3173 row = tv_get_number(&argvars[0]) - 1;
3174 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003175 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003176 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003177 rettv->vval.v_number = wp->w_id;
3178}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003179
3180/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003181 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3182 */
3183 static void
3184get_padding_border(dict_T *dict, int *array, char *name)
3185{
3186 list_T *list;
3187 int i;
3188
3189 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3190 return;
3191
3192 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003193 if (list == NULL)
3194 return;
3195
3196 dict_add_list(dict, name, list);
3197 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3198 for (i = 0; i < 4; ++i)
3199 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003200}
3201
3202/*
3203 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3204 */
3205 static void
3206get_borderhighlight(dict_T *dict, win_T *wp)
3207{
3208 list_T *list;
3209 int i;
3210
3211 for (i = 0; i < 4; ++i)
3212 if (wp->w_border_highlight[i] != NULL)
3213 break;
3214 if (i == 4)
3215 return;
3216
3217 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003218 if (list == NULL)
3219 return;
3220
3221 dict_add_list(dict, "borderhighlight", list);
3222 for (i = 0; i < 4; ++i)
3223 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003224}
3225
3226/*
3227 * For popup_getoptions(): add a "borderchars" entry to "dict".
3228 */
3229 static void
3230get_borderchars(dict_T *dict, win_T *wp)
3231{
3232 list_T *list;
3233 int i;
3234 char_u buf[NUMBUFLEN];
3235 int len;
3236
3237 for (i = 0; i < 8; ++i)
3238 if (wp->w_border_char[i] != 0)
3239 break;
3240 if (i == 8)
3241 return;
3242
3243 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003244 if (list == NULL)
3245 return;
3246
3247 dict_add_list(dict, "borderchars", list);
3248 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003249 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003250 len = mb_char2bytes(wp->w_border_char[i], buf);
3251 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003252 }
3253}
3254
3255/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003256 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003257 */
3258 static void
3259get_moved_list(dict_T *dict, win_T *wp)
3260{
3261 list_T *list;
3262
3263 list = list_alloc();
3264 if (list != NULL)
3265 {
3266 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003267 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003268 list_append_number(list, wp->w_popup_mincol);
3269 list_append_number(list, wp->w_popup_maxcol);
3270 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003271 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003272 if (list == NULL)
3273 return;
3274
3275 dict_add_list(dict, "mousemoved", list);
3276 list_append_number(list, wp->w_popup_mouse_row);
3277 list_append_number(list, wp->w_popup_mouse_mincol);
3278 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003279}
3280
3281/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003282 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003283 */
3284 void
3285f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3286{
3287 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003288 int id;
3289 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003290 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003291 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003292
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003293 if (rettv_dict_alloc(rettv) == FAIL)
3294 return;
3295
3296 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3297 return;
3298
3299 id = (int)tv_get_number(argvars);
3300 wp = find_popup_win(id);
3301 if (wp == NULL)
3302 return;
3303
3304 dict = rettv->vval.v_dict;
3305 dict_add_number(dict, "line", wp->w_wantline);
3306 dict_add_number(dict, "col", wp->w_wantcol);
3307 dict_add_number(dict, "minwidth", wp->w_minwidth);
3308 dict_add_number(dict, "minheight", wp->w_minheight);
3309 dict_add_number(dict, "maxheight", wp->w_maxheight);
3310 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3311 dict_add_number(dict, "firstline", wp->w_firstline);
3312 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3313 dict_add_number(dict, "zindex", wp->w_zindex);
3314 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3315 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003316 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003317 proptype_T *pt = text_prop_type_by_id(
3318 wp->w_popup_prop_win->w_buffer,
3319 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003320
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003321 if (pt != NULL)
3322 dict_add_string(dict, "textprop", pt->pt_name);
3323 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3324 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3325 }
3326 dict_add_string(dict, "title", wp->w_popup_title);
3327 dict_add_number(dict, "wrap", wp->w_p_wrap);
3328 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3329 dict_add_number(dict, "dragall",
3330 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3331 dict_add_number(dict, "mapping",
3332 (wp->w_popup_flags & POPF_MAPPING) != 0);
3333 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3334 dict_add_number(dict, "posinvert",
3335 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3336 dict_add_number(dict, "cursorline",
3337 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3338 dict_add_string(dict, "highlight", wp->w_p_wcr);
3339 if (wp->w_scrollbar_highlight != NULL)
3340 dict_add_string(dict, "scrollbarhighlight",
3341 wp->w_scrollbar_highlight);
3342 if (wp->w_thumb_highlight != NULL)
3343 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003344
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003345 // find the tabpage that holds this popup
3346 i = 1;
3347 FOR_ALL_TABPAGES(tp)
3348 {
3349 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003350
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003351 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3352 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003353 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003354 if (twp != NULL)
3355 break;
3356 ++i;
3357 }
3358 if (tp == NULL)
3359 i = -1; // must be global
3360 else if (tp == curtab)
3361 i = 0;
3362 dict_add_number(dict, "tabpage", i);
3363
3364 get_padding_border(dict, wp->w_popup_padding, "padding");
3365 get_padding_border(dict, wp->w_popup_border, "border");
3366 get_borderhighlight(dict, wp);
3367 get_borderchars(dict, wp);
3368 get_moved_list(dict, wp);
3369
3370 if (wp->w_filter_cb.cb_name != NULL)
3371 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3372 if (wp->w_close_cb.cb_name != NULL)
3373 dict_add_callback(dict, "callback", &wp->w_close_cb);
3374
3375 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3376 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3377 {
3378 dict_add_string(dict, "pos",
3379 (char_u *)poppos_entries[i].pp_name);
3380 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003381 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003382
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003383 dict_add_string(dict, "close", (char_u *)(
3384 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3385 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003386
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003387# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003388 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3389 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003390# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003391}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003392
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003393# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003394/*
3395 * Return TRUE if the current window is running a terminal in a popup window.
3396 * Return FALSE when the job has ended.
3397 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003398 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003399error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003400{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003401 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3402 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003403 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003404 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003405 return TRUE;
3406 }
3407 return FALSE;
3408}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003409# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003410
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003411/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003412 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003413 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003414 * Each calling function should use a different flag, see the list at
3415 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003416 */
3417 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003418popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003419{
3420 win_T *wp;
3421
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003422 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003423 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003424 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003425 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003426}
3427
3428/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003429 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003430 * Must have called popup_reset_handled() first.
3431 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3432 * popup with the highest zindex.
3433 */
3434 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003435find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003436{
3437 win_T *wp;
3438 win_T *found_wp;
3439 int found_zindex;
3440
3441 found_zindex = lowest ? INT_MAX : 0;
3442 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003443 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003444 if ((wp->w_popup_handled & handled_flag) == 0
3445 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003446 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003447 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003448 {
3449 found_zindex = wp->w_zindex;
3450 found_wp = wp;
3451 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003452 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003453 if ((wp->w_popup_handled & handled_flag) == 0
3454 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003455 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003456 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003457 {
3458 found_zindex = wp->w_zindex;
3459 found_wp = wp;
3460 }
3461
3462 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003463 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003464 return found_wp;
3465}
3466
3467/*
3468 * Invoke the filter callback for window "wp" with typed character "c".
3469 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003470 * Returns the return value of the filter or -1 for CTRL-C in the current
3471 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003472 * Careful: The filter may make "wp" invalid!
3473 */
3474 static int
3475invoke_popup_filter(win_T *wp, int c)
3476{
3477 int res;
3478 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003479 typval_T argv[3];
3480 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003481 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003482 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003483
Bram Moolenaara42d9452019-06-15 21:46:30 +02003484 // Emergency exit: CTRL-C closes the popup.
3485 if (c == Ctrl_C)
3486 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003487 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003488 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003489
3490 // Reset got_int to avoid the callback isn't called.
3491 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003492 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003493 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003494
3495 // If the popup is the current window it probably fails to close. Then
3496 // do not consume the key.
3497 if (was_curwin && wp == curwin)
3498 return -1;
3499 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003500 }
3501
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003502 argv[0].v_type = VAR_NUMBER;
3503 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3504
3505 // Convert the number to a string, so that the function can use:
3506 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003507 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003508 argv[1].v_type = VAR_STRING;
3509 argv[1].vval.v_string = vim_strsave(buf);
3510
3511 argv[2].v_type = VAR_UNKNOWN;
3512
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003513 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003514 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3515 {
3516 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003517 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003518 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003519 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003520 }
3521 else
3522 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003523 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3524 popup_highlight_curline(wp);
3525
Bram Moolenaar371806e2020-10-22 13:44:54 +02003526 // If an error message was given always return FALSE, so that keys are
3527 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003528 // If we get three errors in a row then close the popup. Decrement the
3529 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3530 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003531 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003532 {
3533 wp->w_filter_errors += 10;
3534 if (wp->w_filter_errors >= 30)
3535 popup_close_with_retval(wp, -1);
3536 res = FALSE;
3537 }
3538 else
3539 {
3540 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3541 --wp->w_filter_errors;
3542 res = tv_get_bool(&rettv);
3543 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003544 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003545
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003546 vim_free(argv[1].vval.v_string);
3547 clear_tv(&rettv);
3548 return res;
3549}
3550
3551/*
3552 * Called when "c" was typed: invoke popup filter callbacks.
3553 * Returns TRUE when the character was consumed,
3554 */
3555 int
3556popup_do_filter(int c)
3557{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003558 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003559 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003560 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003561 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003562 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003563 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003564
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003565#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003566 // Popup window with terminal always gets focus.
3567 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3568 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003569#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003570
Bram Moolenaar934470e2019-09-01 23:27:05 +02003571 if (recursive)
3572 return FALSE;
3573 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003574
Bram Moolenaarf6396232019-08-24 19:36:00 +02003575 if (c == K_LEFTMOUSE)
3576 {
3577 int row = mouse_row;
3578 int col = mouse_col;
3579
3580 wp = mouse_find_win(&row, &col, FIND_POPUP);
3581 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003582 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003583 }
3584
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003585 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003586 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003587 while (res == FALSE
3588 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003589 if (wp->w_filter_cb.cb_name != NULL
3590 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003591 res = invoke_popup_filter(wp, c);
3592
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003593 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003594 {
3595 int save_got_int = got_int;
3596
3597 // Reset got_int to avoid a function used in the statusline aborts.
3598 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003599 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003600 got_int |= save_got_int;
3601 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003602 recursive = FALSE;
3603 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003604
3605 // When interrupted return FALSE to avoid looping.
3606 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003607}
3608
Bram Moolenaar3397f742019-06-02 18:40:06 +02003609/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003610 * Return TRUE if there is a popup visible with a filter callback and the
3611 * "mapping" property off.
3612 */
3613 int
3614popup_no_mapping(void)
3615{
3616 int round;
3617 win_T *wp;
3618
3619 for (round = 1; round <= 2; ++round)
3620 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3621 wp != NULL; wp = wp->w_next)
3622 if (wp->w_filter_cb.cb_name != NULL
3623 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3624 return TRUE;
3625 return FALSE;
3626}
3627
3628/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003629 * Called when the cursor moved: check if any popup needs to be closed if the
3630 * cursor moved far enough.
3631 */
3632 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003633popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003634{
3635 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003636
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003637 popup_reset_handled(POPUP_HANDLED_3);
3638 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003639 if (wp->w_popup_curwin != NULL
3640 && (curwin != wp->w_popup_curwin
3641 || curwin->w_cursor.lnum != wp->w_popup_lnum
3642 || curwin->w_cursor.col < wp->w_popup_mincol
3643 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003644 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003645}
3646
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003647/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003648 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003649 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003650 static void
3651popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003652{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003653 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003654 char_u *cells;
3655 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003656
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003657 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3658 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003659 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003660 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003661 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003662 if (wp->w_popup_mask_cells != NULL
3663 && wp->w_popup_mask_height == height
3664 && wp->w_popup_mask_width == width)
3665 return; // cache is still valid
3666
3667 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003668 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003669 if (wp->w_popup_mask_cells == NULL)
3670 return;
3671 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003672
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003673 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003674 {
3675 int cols, cole;
3676 int lines, linee;
3677
3678 li = lio->li_tv.vval.v_list->lv_first;
3679 cols = tv_get_number(&li->li_tv);
3680 if (cols < 0)
3681 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003682 if (cols <= 0)
3683 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003684 li = li->li_next;
3685 cole = tv_get_number(&li->li_tv);
3686 if (cole < 0)
3687 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003688 if (cole > width)
3689 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003690 li = li->li_next;
3691 lines = tv_get_number(&li->li_tv);
3692 if (lines < 0)
3693 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003694 if (lines <= 0)
3695 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003696 li = li->li_next;
3697 linee = tv_get_number(&li->li_tv);
3698 if (linee < 0)
3699 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003700 if (linee > height)
3701 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003702
Bram Moolenaar4012d262020-12-29 11:57:46 +01003703 for (row = lines - 1; row < linee; ++row)
3704 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003705 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003706 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003707}
3708
3709/*
3710 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3711 * "col" and "line" are screen coordinates.
3712 */
3713 static int
3714popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3715{
3716 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3717 int line = screenline - wp->w_winrow;
3718
3719 return col >= 0 && col < width
3720 && line >= 0 && line < height
3721 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003722}
3723
3724/*
3725 * Set flags in popup_transparent[] for window "wp" to "val".
3726 */
3727 static void
3728update_popup_transparent(win_T *wp, int val)
3729{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003730 if (wp->w_popup_mask == NULL)
3731 return;
3732
3733 int width = popup_width(wp);
3734 int height = popup_height(wp);
3735 listitem_T *lio, *li;
3736 int cols, cole;
3737 int lines, linee;
3738 int col, line;
3739
3740 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003741 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003742 li = lio->li_tv.vval.v_list->lv_first;
3743 cols = tv_get_number(&li->li_tv);
3744 if (cols < 0)
3745 cols = width + cols + 1;
3746 li = li->li_next;
3747 cole = tv_get_number(&li->li_tv);
3748 if (cole < 0)
3749 cole = width + cole + 1;
3750 li = li->li_next;
3751 lines = tv_get_number(&li->li_tv);
3752 if (lines < 0)
3753 lines = height + lines + 1;
3754 li = li->li_next;
3755 linee = tv_get_number(&li->li_tv);
3756 if (linee < 0)
3757 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003758
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003759 --cols;
3760 cols -= wp->w_popup_leftoff;
3761 if (cols < 0)
3762 cols = 0;
3763 cole -= wp->w_popup_leftoff;
3764 --lines;
3765 if (lines < 0)
3766 lines = 0;
3767 for (line = lines; line < linee
3768 && line + wp->w_winrow < screen_Rows; ++line)
3769 for (col = cols; col < cole
3770 && col + wp->w_wincol < screen_Columns; ++col)
3771 popup_transparent[(line + wp->w_winrow) * screen_Columns
3772 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003773 }
3774}
3775
3776/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003777 * Only called when popup window "wp" is hidden: If the window is positioned
3778 * next to a text property, and it is now visible, then unhide the popup.
3779 * We don't check if visible popups become hidden, that is done in
3780 * popup_adjust_position().
3781 * Return TRUE if the popup became unhidden.
3782 */
3783 static int
3784check_popup_unhidden(win_T *wp)
3785{
3786 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3787 {
3788 textprop_T prop;
3789 linenr_T lnum;
3790
Bram Moolenaar27724252022-05-08 15:00:04 +01003791 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3792 && find_visible_prop(wp->w_popup_prop_win,
3793 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003794 &prop, &lnum) == OK)
3795 {
3796 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003797 wp->w_popup_prop_topline = 0; // force repositioning
3798 return TRUE;
3799 }
3800 }
3801 return FALSE;
3802}
3803
3804/*
3805 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3806 * That is when the buffer in the popup was changed, or the popup is following
3807 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003808 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003809 */
3810 static int
3811popup_need_position_adjust(win_T *wp)
3812{
3813 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3814 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003815 if (win_valid(wp->w_popup_prop_win)
3816 && (wp->w_popup_prop_changedtick
3817 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3818 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3819 return TRUE;
3820
3821 // May need to adjust the width if the cursor moved.
3822 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003823}
3824
3825/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003826 * Update "popup_mask" if needed.
3827 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003828 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003829 * Also marks window lines for redrawing.
3830 */
3831 void
3832may_update_popup_mask(int type)
3833{
3834 win_T *wp;
3835 short *mask;
3836 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003837 int redraw_all_popups = FALSE;
3838 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003839
3840 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003841 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3842 // basic (such as the screen size) must have changed.
3843 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003844 {
3845 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003846 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003847 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003848
3849 // Check if any popup window buffer has changed and if any popup connected
3850 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003851 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003852 if (wp->w_popup_flags & POPF_HIDDEN)
3853 popup_mask_refresh |= check_popup_unhidden(wp);
3854 else if (popup_need_position_adjust(wp))
3855 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003856 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003857 if (wp->w_popup_flags & POPF_HIDDEN)
3858 popup_mask_refresh |= check_popup_unhidden(wp);
3859 else if (popup_need_position_adjust(wp))
3860 popup_mask_refresh = TRUE;
3861
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003862 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003863 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003864
3865 // Need to update the mask, something has changed.
3866 popup_mask_refresh = FALSE;
3867 popup_mask_tab = curtab;
3868 popup_visible = FALSE;
3869
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003870 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003871 // If redrawing only what is needed, update "popup_mask_next" and then
3872 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003873 redrawing_all_win = TRUE;
3874 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003875 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003876 redrawing_all_win = FALSE;
3877 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003878 mask = popup_mask;
3879 else
3880 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003881 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882
3883 // Find the window with the lowest zindex that hasn't been handled yet,
3884 // so that the window with a higher zindex overwrites the value in
3885 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003886 popup_reset_handled(POPUP_HANDLED_4);
3887 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003888 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003889 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003890 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003891
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003892 popup_visible = TRUE;
3893
Bram Moolenaar12034e22019-08-25 22:25:02 +02003894 // Recompute the position if the text changed. It may make the popup
3895 // hidden if it's attach to a text property that is no longer visible.
3896 if (redraw_all_popups || popup_need_position_adjust(wp))
3897 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003898 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003899 if (wp->w_popup_flags & POPF_HIDDEN)
3900 continue;
3901 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003902
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003903 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003904 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003905 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003906 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003907 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003908 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003909 col < wp->w_wincol + width - wp->w_popup_leftoff
3910 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003911 if (wp->w_zindex < POPUPMENU_ZINDEX
3912 && pum_visible()
3913 && pum_under_menu(line, col, FALSE))
3914 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3915 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003916 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003917 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003918 }
3919
3920 // Only check which lines are to be updated if not already
3921 // updating all lines.
3922 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003923 {
3924 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3925 win_T *prev_wp = NULL;
3926
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003927 for (line = 0; line < screen_Rows; ++line)
3928 {
3929 int col_done = 0;
3930
3931 for (col = 0; col < screen_Columns; ++col)
3932 {
3933 int off = line * screen_Columns + col;
3934
3935 if (popup_mask[off] != popup_mask_next[off])
3936 {
3937 popup_mask[off] = popup_mask_next[off];
3938
3939 if (line >= cmdline_row)
3940 {
3941 // the command line needs to be cleared if text below
3942 // the popup is now visible.
3943 if (!msg_scrolled && popup_mask_next[off] == 0)
3944 clear_cmdline = TRUE;
3945 }
3946 else if (col >= col_done)
3947 {
3948 linenr_T lnum;
3949 int line_cp = line;
3950 int col_cp = col;
3951
3952 // The screen position "line" / "col" needs to be
3953 // redrawn. Figure out what window that is and update
zeertzjqf2d90a32024-02-12 20:28:01 +01003954 // w_redraw_top and w_redraw_bot. Only needs to be
3955 // done once for each window line.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003956 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3957 if (wp != NULL)
3958 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003959#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003960 // A terminal window needs to be redrawn.
3961 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003962 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003963 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003964#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003965 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003966 if (wp != prev_wp)
3967 {
3968 vim_memset(plines_cache, 0,
3969 sizeof(int) * Rows);
3970 prev_wp = wp;
3971 }
3972
3973 if (line_cp >= wp->w_height)
3974 // In (or below) status line
3975 wp->w_redr_status = TRUE;
3976 else
3977 {
3978 // compute the position in the buffer line
3979 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003980 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003981 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003982 redrawWinline(wp, lnum);
3983 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003984 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003985
3986 // This line is going to be redrawn, no need to
3987 // check until the right side of the window.
3988 col_done = wp->w_wincol + wp->w_width - 1;
3989 }
3990 }
3991 }
3992 }
3993 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003994
3995 vim_free(plines_cache);
3996 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003997
3998 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003999}
4000
4001/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02004002 * If the current window is a popup and something relevant changed, recompute
4003 * the position and size.
4004 */
4005 void
4006may_update_popup_position(void)
4007{
4008 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
4009 popup_adjust_position(curwin);
4010}
4011
4012/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004013 * Return a string of "len" spaces in IObuff.
4014 */
4015 static char_u *
4016get_spaces(int len)
4017{
4018 vim_memset(IObuff, ' ', (size_t)len);
4019 IObuff[len] = NUL;
4020 return IObuff;
4021}
4022
4023/*
4024 * Update popup windows. They are drawn on top of normal windows.
4025 * "win_update" is called for each popup window, lowest zindex first.
4026 */
4027 void
4028update_popups(void (*win_update)(win_T *wp))
4029{
4030 win_T *wp;
4031 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004032 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004033 int total_width;
4034 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004035 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004036 int popup_attr;
4037 int border_attr[4];
4038 int border_char[8];
4039 char_u buf[MB_MAXBYTES];
4040 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004041 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004042 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004043 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004044 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02004045 int sb_thumb_top = 0;
4046 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004047 int attr_scroll = 0;
4048 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004049
Bram Moolenaarebec3e22020-11-28 20:22:06 +01004050 // hide the cursor until redrawing is done.
4051 cursor_off();
4052
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004053 // Find the window with the lowest zindex that hasn't been updated yet,
4054 // so that the window with a higher zindex is drawn later, thus goes on
4055 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01004056 popup_reset_handled(POPUP_HANDLED_5);
4057 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004058 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004059 int title_len = 0;
4060 int title_wincol;
4061
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004062 // This drawing uses the zindex of the popup window, so that it's on
4063 // top of the text but doesn't draw when another popup with higher
4064 // zindex is on top of the character.
4065 screen_zindex = wp->w_zindex;
4066
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004067 // Set flags in popup_transparent[] for masked cells.
4068 update_popup_transparent(wp, 1);
4069
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004070 // adjust w_winrow and w_wincol for border and padding, since
4071 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004072 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02004073 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
4074 - wp->w_popup_leftoff;
4075 if (wp->w_wincol + left_extra < 0)
4076 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004077 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004078 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004079
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004080 // Draw the popup text, unless it's off screen.
4081 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004082 {
Bram Moolenaar10476522020-09-24 22:57:31 +02004083 // May need to update the "cursorline" highlighting, which may also
4084 // change "topline"
4085 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4086 popup_highlight_curline(wp);
4087
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004088 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004089
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004090 // move the cursor into the visible lines, otherwise executing
4091 // commands with win_execute() may cause the text to jump.
4092 if (wp->w_cursor.lnum < wp->w_topline)
4093 wp->w_cursor.lnum = wp->w_topline;
4094 else if (wp->w_cursor.lnum >= wp->w_botline)
4095 wp->w_cursor.lnum = wp->w_botline - 1;
4096 }
4097
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004098 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004099 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004100
4101 // Add offset for border and padding if not done already.
4102 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4103 {
4104 wp->w_wcol += left_extra;
4105 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4106 }
4107 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004108 {
4109 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004110 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004111 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004112
Bram Moolenaareabddc42022-04-02 15:32:16 +01004113 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004114 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004115 popup_attr = get_wcr_attr(wp);
4116
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004117 if (wp->w_winrow + total_height > cmdline_row)
4118 wp->w_popup_flags |= POPF_ON_CMDLINE;
4119 else
4120 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4121
4122
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004123 // We can only use these line drawing characters when 'encoding' is
4124 // "utf-8" and 'ambiwidth' is "single".
4125 if (enc_utf8 && *p_ambw == 's')
4126 {
4127 border_char[0] = border_char[2] = 0x2550;
4128 border_char[1] = border_char[3] = 0x2551;
4129 border_char[4] = 0x2554;
4130 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004131 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4132 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004133 border_char[7] = 0x255a;
4134 }
4135 else
4136 {
4137 border_char[0] = border_char[2] = '-';
4138 border_char[1] = border_char[3] = '|';
4139 for (i = 4; i < 8; ++i)
4140 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004141 if (wp->w_popup_flags & POPF_RESIZE)
4142 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004143 }
4144 for (i = 0; i < 8; ++i)
4145 if (wp->w_border_char[i] != 0)
4146 border_char[i] = wp->w_border_char[i];
4147
4148 for (i = 0; i < 4; ++i)
4149 {
4150 border_attr[i] = popup_attr;
4151 if (wp->w_border_highlight[i] != NULL)
4152 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4153 }
4154
Bram Moolenaard91467f2020-11-21 12:42:09 +01004155 // Title goes on top of border or padding.
4156 title_wincol = wp->w_wincol + 1;
4157 if (wp->w_popup_title != NULL)
4158 {
rbtnnc6119412021-08-07 13:08:45 +02004159 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004160
Ralf Schandlbc869872021-05-28 14:12:14 +02004161 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004162 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004163 {
4164 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4165 char_u *title_text = alloc(title_byte_len + 1);
4166
4167 if (title_text != NULL)
4168 {
4169 trunc_string(wp->w_popup_title, title_text,
4170 total_width - 2, title_byte_len + 1);
4171 screen_puts(title_text, wp->w_winrow, title_wincol,
4172 wp->w_popup_border[0] > 0
4173 ? border_attr[0] : popup_attr);
4174 vim_free(title_text);
4175 }
4176
Bram Moolenaard91467f2020-11-21 12:42:09 +01004177 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004178 }
4179 else
4180 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4181 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004182 }
4183
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004184 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004185 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004186 if (wp->w_popup_border[0] > 0)
4187 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004188 // top border; do not draw over the title
4189 if (title_len > 0)
4190 {
4191 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4192 wincol < 0 ? 0 : wincol, title_wincol,
4193 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004194 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004195 border_char[0], border_attr[0]);
4196 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4197 title_wincol + title_len, wincol + total_width,
4198 border_char[0], border_char[0], border_attr[0]);
4199 }
4200 else
4201 {
4202 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4203 wincol < 0 ? 0 : wincol, wincol + total_width,
4204 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4205 ? border_char[4] : border_char[0],
4206 border_char[0], border_attr[0]);
4207 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004208 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004209 {
4210 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4211 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004212 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004213 }
4214 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004215 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4216 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004217
Bram Moolenaard529ba52019-07-02 23:13:53 +02004218 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4219 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004220 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004221 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004222 - wp->w_has_scrollbar;
4223 if (padcol < 0)
4224 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004225 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004226 padcol = 0;
4227 }
4228 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004229 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004230 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004231 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004232 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004233 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004234 // top padding and no border; do not draw over the title
4235 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004236 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004237 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004238 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004239 row += 1;
4240 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004241 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004242 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004243 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004244 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004245
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004246 // Compute scrollbar thumb position and size.
4247 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004248 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004249 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4250 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004251 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004252
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004253 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4254 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004255 if (wp->w_topline > 1 && sb_thumb_height == height)
4256 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004257 if (sb_thumb_height == 0)
4258 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004259 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004260 // it just fits, avoid divide by zero
4261 sb_thumb_top = 0;
4262 else
4263 sb_thumb_top = (wp->w_topline - 1
4264 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004265 * (wp->w_height - sb_thumb_height)
4266 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004267 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4268 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004269 last = total_height - top_off - wp->w_popup_border[2];
4270 if (sb_thumb_top >= last)
4271 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004272 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004273
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004274 if (wp->w_scrollbar_highlight != NULL)
4275 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4276 else
4277 attr_scroll = highlight_attr[HLF_PSB];
4278 if (wp->w_thumb_highlight != NULL)
4279 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4280 else
4281 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004282 }
4283
4284 for (i = wp->w_popup_border[0];
4285 i < total_height - wp->w_popup_border[2]; ++i)
4286 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004287 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004288 // left and right padding only needed next to the body
4289 int do_padding =
4290 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4291 && i < total_height - wp->w_popup_border[2]
4292 - wp->w_popup_padding[2];
4293
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004294 row = wp->w_winrow + i;
4295
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004296 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004297 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004298 {
4299 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004300 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004301 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004302 if (do_padding && wp->w_popup_padding[3] > 0)
4303 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004304 int col = wincol + wp->w_popup_border[3];
4305
Bram Moolenaard529ba52019-07-02 23:13:53 +02004306 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004307 pad_left = wp->w_popup_padding[3];
4308 if (col < 0)
4309 {
4310 pad_left += col;
4311 col = 0;
4312 }
4313 if (pad_left > 0)
4314 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4315 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004316 // scrollbar
4317 if (wp->w_has_scrollbar)
4318 {
4319 int line = i - top_off;
4320 int scroll_col = wp->w_wincol + total_width - 1
4321 - wp->w_popup_border[1];
4322
4323 if (line >= 0 && line < wp->w_height)
4324 screen_putchar(' ', row, scroll_col,
4325 line >= sb_thumb_top
4326 && line < sb_thumb_top + sb_thumb_height
4327 ? attr_thumb : attr_scroll);
4328 else
4329 screen_putchar(' ', row, scroll_col, popup_attr);
4330 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004331 // right border
4332 if (wp->w_popup_border[1] > 0)
4333 {
4334 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004335 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004336 }
4337 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004338 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004339 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004340 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004341 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4342 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004343 }
4344
4345 if (wp->w_popup_padding[2] > 0)
4346 {
4347 // bottom padding
4348 row = wp->w_winrow + wp->w_popup_border[0]
4349 + wp->w_popup_padding[0] + wp->w_height;
4350 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004351 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004352 }
4353
4354 if (wp->w_popup_border[2] > 0)
4355 {
4356 // bottom border
4357 row = wp->w_winrow + total_height - 1;
4358 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004359 wincol < 0 ? 0 : wincol,
4360 wincol + total_width,
4361 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004362 ? border_char[7] : border_char[2],
4363 border_char[2], border_attr[2]);
4364 if (wp->w_popup_border[1] > 0)
4365 {
4366 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004367 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004368 }
4369 }
4370
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004371 if (wp->w_popup_close == POPCLOSE_BUTTON)
4372 {
4373 // close button goes on top of anything at the top-right corner
4374 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004375 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004376 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4377 }
4378
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004379 update_popup_transparent(wp, 0);
4380
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004381 // Back to the normal zindex.
4382 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004383
4384#ifdef HAS_MESSAGE_WINDOW
4385 // if this was the message window popup may start the timer now
4386 may_start_message_win_timer(wp);
4387#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004388 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004389
4390#if defined(FEAT_SEARCH_EXTRA)
4391 // In case win_update() called start_search_hl().
4392 end_search_hl();
4393#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004394}
4395
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004396/*
4397 * Mark references in callbacks of one popup window.
4398 */
4399 static int
4400set_ref_in_one_popup(win_T *wp, int copyID)
4401{
4402 int abort = FALSE;
4403 typval_T tv;
4404
4405 if (wp->w_close_cb.cb_partial != NULL)
4406 {
4407 tv.v_type = VAR_PARTIAL;
4408 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4409 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4410 }
4411 if (wp->w_filter_cb.cb_partial != NULL)
4412 {
4413 tv.v_type = VAR_PARTIAL;
4414 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4415 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4416 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004417 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004418 return abort;
4419}
4420
4421/*
4422 * Set reference in callbacks of popup windows.
4423 */
4424 int
4425set_ref_in_popups(int copyID)
4426{
4427 int abort = FALSE;
4428 win_T *wp;
4429 tabpage_T *tp;
4430
4431 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4432 abort = abort || set_ref_in_one_popup(wp, copyID);
4433
4434 FOR_ALL_TABPAGES(tp)
4435 {
4436 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4437 abort = abort || set_ref_in_one_popup(wp, copyID);
4438 if (abort)
4439 break;
4440 }
4441 return abort;
4442}
Bram Moolenaar79648732019-07-18 21:43:07 +02004443
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004444 int
4445popup_is_popup(win_T *wp)
4446{
4447 return wp->w_popup_flags != 0;
4448}
4449
4450#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004451/*
4452 * Find an existing popup used as the preview window, in the current tab page.
4453 * Return NULL if not found.
4454 */
4455 win_T *
4456popup_find_preview_window(void)
4457{
4458 win_T *wp;
4459
4460 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004461 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004462 if (wp->w_p_pvw)
4463 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004464 return NULL;
4465}
4466
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004467/*
4468 * Find an existing popup used as the info window, in the current tab page.
4469 * Return NULL if not found.
4470 */
4471 win_T *
4472popup_find_info_window(void)
4473{
4474 win_T *wp;
4475
4476 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004477 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004478 if (wp->w_popup_flags & POPF_INFO)
4479 return wp;
4480 return NULL;
4481}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004482#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004483
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004484 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004485f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4486{
4487#ifdef HAS_MESSAGE_WINDOW
4488 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4489#else
4490 rettv->vval.v_number = 0;
4491#endif
4492}
4493
4494 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004495f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4496{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004497#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004498 win_T *wp = popup_find_info_window();
4499
4500 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004501#else
4502 rettv->vval.v_number = 0;
4503#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004504}
4505
4506 void
4507f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004508{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004509#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004510 win_T *wp = popup_find_preview_window();
4511
4512 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004513#else
4514 rettv->vval.v_number = 0;
4515#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004516}
4517
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004518#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004519/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004520 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004521 * NOTE: this makes the popup the current window, so that the file can be
4522 * edited. However, it must not remain to be the current window, the caller
4523 * must make sure of that.
4524 */
4525 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004526popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004527{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004528 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004529
4530 if (wp == NULL)
4531 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004532 if (info)
4533 wp->w_popup_flags |= POPF_INFO;
4534 else
4535 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004536
4537 // Set the width to a reasonable value, so that w_topline can be computed.
4538 if (wp->w_minwidth > 0)
4539 wp->w_width = wp->w_minwidth;
4540 else if (wp->w_maxwidth > 0)
4541 wp->w_width = wp->w_maxwidth;
4542 else
4543 wp->w_width = curwin->w_width;
4544
4545 // Will switch to another buffer soon, dummy one can be wiped.
4546 wp->w_buffer->b_locked = FALSE;
4547
4548 win_enter(wp, FALSE);
4549 return OK;
4550}
4551
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004552/*
4553 * Close any preview popup.
4554 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004555 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004556popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004557{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004558 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004559
4560 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004561 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004562}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004563
4564/*
4565 * Hide the info popup.
4566 */
4567 void
4568popup_hide_info(void)
4569{
4570 win_T *wp = popup_find_info_window();
4571
4572 if (wp != NULL)
4573 popup_hide(wp);
4574}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004575
4576/*
4577 * Close any info popup.
4578 */
4579 void
4580popup_close_info(void)
4581{
4582 win_T *wp = popup_find_info_window();
4583
4584 if (wp != NULL)
4585 popup_close_with_retval(wp, -1);
4586}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004587#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004588
Bram Moolenaar9198de32022-08-27 21:30:03 +01004589#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4590
Bram Moolenaar9198de32022-08-27 21:30:03 +01004591/*
4592 * Get the message window.
4593 * Returns NULL if something failed.
4594 */
4595 win_T *
4596popup_get_message_win(void)
4597{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004598 if (message_win != NULL)
4599 return message_win;
4600
4601 int i;
4602
4603 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4604
Bram Moolenaar9198de32022-08-27 21:30:03 +01004605 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004606 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004607
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004608 // use the full screen width
4609 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004610
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004611 // position at bottom of screen
4612 message_win->w_popup_pos = POPPOS_BOTTOM;
4613 message_win->w_wantcol = 1;
4614 message_win->w_minwidth = 9999;
4615 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004616
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004617 // no padding, border at the top
4618 for (i = 0; i < 4; ++i)
4619 message_win->w_popup_padding[i] = 0;
4620 for (i = 1; i < 4; ++i)
4621 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004622
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004623 if (message_win->w_popup_timer != NULL)
4624 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004625 return message_win;
4626}
4627
4628/*
4629 * If the message window is not visible: show it
4630 * If the message window is visible: reset the timeout
4631 */
4632 void
4633popup_show_message_win(void)
4634{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004635 if (message_win == NULL)
4636 return;
4637
4638 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004639 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004640 // the highlight may have changed.
4641 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4642 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004643 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004644 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004645}
4646
4647 static void
4648may_start_message_win_timer(win_T *wp)
4649{
4650 if (wp == message_win && start_message_win_timer)
4651 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004652 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004653 {
4654 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004655 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004656 message_win_time = 3000;
4657 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004658 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004659 }
4660}
4661
4662 int
4663popup_message_win_visible(void)
4664{
4665 return message_win != NULL
4666 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4667}
4668
4669/*
4670 * If the message window is visible: hide it.
4671 */
4672 void
4673popup_hide_message_win(void)
4674{
4675 if (message_win != NULL)
4676 popup_hide(message_win);
4677}
4678
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004679// Values saved in start_echowindow() and restored in end_echowindow()
4680static int save_msg_didout = FALSE;
4681static int save_msg_col = 0;
4682// Values saved in end_echowindow() and restored in start_echowindow()
4683static int ew_msg_didout = FALSE;
4684static int ew_msg_col = 0;
4685
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004686/*
4687 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004688 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004689 */
4690 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004691start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004692{
4693 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004694 save_msg_didout = msg_didout;
4695 save_msg_col = msg_col;
4696 msg_didout = ew_msg_didout;
4697 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004698 if (time_sec != 0)
4699 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004700}
4701
4702/*
4703 * Invoked after outputting a message for ":echowindow".
4704 */
4705 void
4706end_echowindow(void)
4707{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004708 in_echowindow = FALSE;
4709
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004710 if ((State & MODE_HITRETURN) == 0)
4711 // show the message window now
4712 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004713
4714 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004715 ew_msg_didout = TRUE;
4716 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4717 msg_didout = save_msg_didout;
4718 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004719}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004720#endif
4721
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004722/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004723 * Close any popup for a text property associated with "win".
4724 * Return TRUE if a popup was closed.
4725 */
4726 int
4727popup_win_closed(win_T *win)
4728{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004729 int round;
4730 win_T *wp;
4731 win_T *next;
4732 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004733
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004734 for (round = 1; round <= 2; ++round)
4735 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4736 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004737 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004738 next = wp->w_next;
4739 if (wp->w_popup_prop_win == win)
4740 {
4741 popup_close_with_retval(wp, -1);
4742 ret = TRUE;
4743 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004744 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004745 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004746}
4747
4748/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004749 * Set the title of the popup window to the file name.
4750 */
4751 void
4752popup_set_title(win_T *wp)
4753{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004754 if (wp->w_buffer->b_fname == NULL)
4755 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004756
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004757 char_u dirname[MAXPATHL];
4758 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004759
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004760 mch_dirname(dirname, MAXPATHL);
4761 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4762
4763 vim_free(wp->w_popup_title);
4764 len = STRLEN(wp->w_buffer->b_fname) + 3;
4765 wp->w_popup_title = alloc((int)len);
4766 if (wp->w_popup_title != NULL)
4767 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4768 wp->w_buffer->b_fname);
4769 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004770}
4771
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004772# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004773/*
4774 * If there is a preview window, update the title.
4775 * Used after changing directory.
4776 */
4777 void
4778popup_update_preview_title(void)
4779{
4780 win_T *wp = popup_find_preview_window();
4781
4782 if (wp != NULL)
4783 popup_set_title(wp);
4784}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004785# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004786
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004787#endif // FEAT_PROP_POPUP