blob: c84c079d36c02016196dd7ea22f14561af8b13e4 [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";
LemonBoyb975ddf2024-07-06 18:04:09 +0200699 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL, NULL,
700 NULL, SIGN_DEF_PRIO);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200701 }
702
Bram Moolenaar72570732019-11-30 14:21:53 +0100703 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200704 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100705 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200706 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200707 else
708 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200709 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200710}
711
712/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200713 * Shared between popup_create() and f_popup_setoptions().
714 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200715 static int
Bram Moolenaarae943152019-06-16 22:54:14 +0200716apply_general_options(win_T *wp, dict_T *dict)
717{
718 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200719 int nr;
720 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200721
Bram Moolenaarae943152019-06-16 22:54:14 +0200722 // TODO: flip
723
724 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200725 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200726 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100727 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200728 if (wp->w_firstline < 0)
729 wp->w_firstline = -1;
730 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200731
Bram Moolenaard61efa52022-07-23 09:52:04 +0100732 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200733 if (nr != -1)
734 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200735
Bram Moolenaard61efa52022-07-23 09:52:04 +0100736 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200737 if (str != NULL)
738 {
739 vim_free(wp->w_popup_title);
740 wp->w_popup_title = vim_strsave(str);
741 }
742
Bram Moolenaard61efa52022-07-23 09:52:04 +0100743 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200744 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200745 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200746
Bram Moolenaard61efa52022-07-23 09:52:04 +0100747 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200749 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200750 if (nr)
751 wp->w_popup_flags |= POPF_DRAG;
752 else
753 wp->w_popup_flags &= ~POPF_DRAG;
754 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100755 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000756 if (nr != -1)
757 {
758 if (nr)
759 wp->w_popup_flags |= POPF_DRAGALL;
760 else
761 wp->w_popup_flags &= ~POPF_DRAGALL;
762 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200763
Bram Moolenaard61efa52022-07-23 09:52:04 +0100764 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200765 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100766 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100767 if (nr)
768 wp->w_popup_flags |= POPF_POSINVERT;
769 else
770 wp->w_popup_flags &= ~POPF_POSINVERT;
771 }
772
Bram Moolenaard61efa52022-07-23 09:52:04 +0100773 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200774 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200775 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200776 if (nr)
777 wp->w_popup_flags |= POPF_RESIZE;
778 else
779 wp->w_popup_flags &= ~POPF_RESIZE;
780 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200781
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200782 di = dict_find(dict, (char_u *)"close", -1);
783 if (di != NULL)
784 {
785 int ok = TRUE;
786
787 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
788 {
789 char_u *s = di->di_tv.vval.v_string;
790
791 if (STRCMP(s, "none") == 0)
792 wp->w_popup_close = POPCLOSE_NONE;
793 else if (STRCMP(s, "button") == 0)
794 wp->w_popup_close = POPCLOSE_BUTTON;
795 else if (STRCMP(s, "click") == 0)
796 wp->w_popup_close = POPCLOSE_CLICK;
797 else
798 ok = FALSE;
799 }
800 else
801 ok = FALSE;
802 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000803 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200804 }
805
Bram Moolenaard61efa52022-07-23 09:52:04 +0100806 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200807 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000808 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200809 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
810 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000811#ifdef FEAT_TERMINAL
812 term_update_wincolor(wp);
813#endif
814 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200815
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200816 if (set_padding_border(dict, wp->w_popup_padding, "padding", 999) == FAIL ||
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +0200817 set_padding_border(dict, wp->w_popup_border, "border", 1) == FAIL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200818 return FAIL;
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200819
Bram Moolenaar790498b2019-06-01 22:15:29 +0200820 di = dict_find(dict, (char_u *)"borderhighlight", -1);
821 if (di != NULL)
822 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200823 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200824 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000825 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200826 return FAIL;
827 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200828 else
829 {
830 list_T *list = di->di_tv.vval.v_list;
831 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200832 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200833
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200834 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200835 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
836 ++i, li = li->li_next)
837 {
838 str = tv_get_string(&li->li_tv);
839 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100840 {
841 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200842 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100843 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200844 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200845 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
846 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100847 {
848 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200849 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200850 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100851 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200852 }
853 }
854
Bram Moolenaar790498b2019-06-01 22:15:29 +0200855 di = dict_find(dict, (char_u *)"borderchars", -1);
856 if (di != NULL)
857 {
858 if (di->di_tv.v_type != VAR_LIST)
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200859 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000860 emsg(_(e_list_required));
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200861 return FAIL;
862 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200863 else
864 {
865 list_T *list = di->di_tv.vval.v_list;
866 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200867 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200868
869 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100870 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200871 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200872 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
873 ++i, li = li->li_next)
874 {
875 str = tv_get_string(&li->li_tv);
876 if (*str != NUL)
877 wp->w_border_char[i] = mb_ptr2char(str);
878 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200879 if (list->lv_len == 1)
880 for (i = 1; i < 8; ++i)
881 wp->w_border_char[i] = wp->w_border_char[0];
882 if (list->lv_len == 2)
883 {
884 for (i = 4; i < 8; ++i)
885 wp->w_border_char[i] = wp->w_border_char[1];
886 for (i = 1; i < 4; ++i)
887 wp->w_border_char[i] = wp->w_border_char[0];
888 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200889 }
890 }
891 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200892
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200893 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
894 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
895
Bram Moolenaarae943152019-06-16 22:54:14 +0200896 di = dict_find(dict, (char_u *)"zindex", -1);
897 if (di != NULL)
898 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100899 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200900 if (wp->w_zindex < 1)
901 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
902 if (wp->w_zindex > 32000)
903 wp->w_zindex = 32000;
904 }
905
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200906 di = dict_find(dict, (char_u *)"mask", -1);
907 if (di != NULL)
908 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200909 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200911 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200912 {
913 listitem_T *li;
914
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200915 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200916 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200917 {
918 if (li->li_tv.v_type != VAR_LIST
919 || li->li_tv.vval.v_list == NULL
920 || li->li_tv.vval.v_list->lv_len != 4)
921 {
922 ok = FALSE;
923 break;
924 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100925 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200926 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200927 }
928 }
929 if (ok)
930 {
931 wp->w_popup_mask = di->di_tv.vval.v_list;
932 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200933 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200934 }
935 else
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200936 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000937 semsg(_(e_invalid_value_for_argument_str), "mask");
Christian Brabandtf6cdab32023-08-11 23:42:02 +0200938 return FAIL;
939 }
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200940 }
941
Bram Moolenaarae943152019-06-16 22:54:14 +0200942#if defined(FEAT_TIMERS)
943 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100944 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200945 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100946 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200947#endif
948
Bram Moolenaar3397f742019-06-02 18:40:06 +0200949 di = dict_find(dict, (char_u *)"moved", -1);
950 if (di != NULL)
951 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200952 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200953 handle_moved_argument(wp, di, FALSE);
954 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200955
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200956 di = dict_find(dict, (char_u *)"mousemoved", -1);
957 if (di != NULL)
958 {
959 set_mousemoved_values(wp);
960 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200961 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200962
Bram Moolenaard61efa52022-07-23 09:52:04 +0100963 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100964 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200965 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100966 if (nr != 0)
967 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200968 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100969 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200970 }
971
Bram Moolenaarae943152019-06-16 22:54:14 +0200972 di = dict_find(dict, (char_u *)"filter", -1);
973 if (di != NULL)
974 {
975 callback_T callback = get_callback(&di->di_tv);
976
977 if (callback.cb_name != NULL)
978 {
979 free_callback(&wp->w_filter_cb);
980 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000981 if (callback.cb_free_name)
982 vim_free(callback.cb_name);
Bram Moolenaarae943152019-06-16 22:54:14 +0200983 }
984 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100985 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200986 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200987 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200988 if (nr)
989 wp->w_popup_flags |= POPF_MAPPING;
990 else
991 wp->w_popup_flags &= ~POPF_MAPPING;
992 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200993
Bram Moolenaard61efa52022-07-23 09:52:04 +0100994 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200995 if (str != NULL)
996 {
997 if (STRCMP(str, "a") == 0)
998 wp->w_filter_mode = MODE_ALL;
999 else
1000 wp->w_filter_mode = mode_str2flags(str);
1001 }
1002
Bram Moolenaarae943152019-06-16 22:54:14 +02001003 di = dict_find(dict, (char_u *)"callback", -1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001004 if (di == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001005 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001006
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001007 callback_T callback = get_callback(&di->di_tv);
1008 if (callback.cb_name == NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001009 return OK;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001010
1011 free_callback(&wp->w_close_cb);
1012 set_callback(&wp->w_close_cb, &callback);
1013 if (callback.cb_free_name)
1014 vim_free(callback.cb_name);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001015
1016 return OK;
Bram Moolenaarae943152019-06-16 22:54:14 +02001017}
1018
1019/*
1020 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001021 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +02001022 */
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001023 static int
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001024apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001025{
1026 int nr;
1027
1028 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001029
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001030 if (create)
1031 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001032 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1033
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001034 if (apply_general_options(wp, dict) == FAIL)
1035 return FAIL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001036
Bram Moolenaard61efa52022-07-23 09:52:04 +01001037 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001038 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001039 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001040
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001041 // when "firstline" and "cursorline" are both set and the cursor would be
1042 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001043 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1044 {
1045 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1046 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001047 else if (wp->w_cursor.lnum < wp->w_firstline
1048 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001049 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001050 wp->w_topline = wp->w_firstline;
1051 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001052 }
1053
Bram Moolenaar33796b32019-06-08 16:01:13 +02001054 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001055 popup_highlight_curline(wp);
Christian Brabandtf6cdab32023-08-11 23:42:02 +02001056
1057 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001058}
1059
1060/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001061 * Add lines to the popup from a list of strings.
1062 */
1063 static void
1064add_popup_strings(buf_T *buf, list_T *l)
1065{
1066 listitem_T *li;
1067 linenr_T lnum = 0;
1068 char_u *p;
1069
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001070 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001071 if (li->li_tv.v_type == VAR_STRING)
1072 {
1073 p = li->li_tv.vval.v_string;
1074 ml_append_buf(buf, lnum++,
1075 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1076 }
1077}
1078
1079/*
1080 * Add lines to the popup from a list of dictionaries.
1081 */
1082 static void
1083add_popup_dicts(buf_T *buf, list_T *l)
1084{
1085 listitem_T *li;
1086 listitem_T *pli;
1087 linenr_T lnum = 0;
1088 char_u *p;
1089 dict_T *dict;
1090
1091 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001092 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001093 {
1094 if (li->li_tv.v_type != VAR_DICT)
1095 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001096 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001097 return;
1098 }
1099 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001100 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001101 ml_append_buf(buf, lnum++,
1102 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1103 }
1104
1105 // add the text properties
1106 lnum = 1;
1107 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1108 {
1109 dictitem_T *di;
1110 list_T *plist;
1111
1112 dict = li->li_tv.vval.v_dict;
1113 di = dict_find(dict, (char_u *)"props", -1);
1114 if (di != NULL)
1115 {
1116 if (di->di_tv.v_type != VAR_LIST)
1117 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001118 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001119 return;
1120 }
1121 plist = di->di_tv.vval.v_list;
1122 if (plist != NULL)
1123 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001124 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001125 {
1126 if (pli->li_tv.v_type != VAR_DICT)
1127 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001128 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001129 return;
1130 }
1131 dict = pli->li_tv.vval.v_dict;
1132 if (dict != NULL)
1133 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001134 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001135
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001136 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001137 }
1138 }
1139 }
1140 }
1141 }
1142}
1143
1144/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001145 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1146 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001147 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001148popup_top_extra(win_T *wp)
1149{
1150 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1151
1152 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1153 return 1;
1154 return extra;
1155}
1156
1157/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001158 * Get the padding plus border at the left.
1159 */
1160 int
1161popup_left_extra(win_T *wp)
1162{
1163 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1164}
1165
1166/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001167 * Return the height of popup window "wp", including border and padding.
1168 */
1169 int
1170popup_height(win_T *wp)
1171{
1172 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001173 + popup_top_extra(wp)
1174 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001175}
1176
1177/*
1178 * Return the width of popup window "wp", including border, padding and
1179 * scrollbar.
1180 */
1181 int
1182popup_width(win_T *wp)
1183{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001184 // w_leftcol is how many columns of the core are left of the screen
1185 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001186 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001187 + popup_extra_width(wp)
1188 + wp->w_popup_rightoff;
1189}
1190
1191/*
1192 * Return the extra width of popup window "wp": border, padding and scrollbar.
1193 */
1194 int
1195popup_extra_width(win_T *wp)
1196{
1197 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1198 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1199 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001200}
1201
1202/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001203 * Adjust the position and size of the popup to fit on the screen.
1204 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001205 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001206popup_adjust_position(win_T *wp)
1207{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001208 linenr_T lnum;
1209 int wrapped = 0;
1210 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001211 int maxwidth_no_scrollbar;
1212 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001213 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001214 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001215 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001216 int center_vert = FALSE;
1217 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001218 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001219 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001220 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1221 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1222 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1223 int extra_height = top_extra + bot_extra;
1224 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001225 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001226 int org_winrow = wp->w_winrow;
1227 int org_wincol = wp->w_wincol;
1228 int org_width = wp->w_width;
1229 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001230 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001231 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001232 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001233 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001234 int wantline = wp->w_wantline; // adjusted for textprop
1235 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001236 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001237 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001238
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001239 wp->w_winrow = 0;
1240 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001241 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001242 wp->w_popup_leftoff = 0;
1243 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001244
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001245 // May need to update the "cursorline" highlighting, which may also change
1246 // "topline"
1247 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1248 popup_highlight_curline(wp);
1249
Bram Moolenaar12034e22019-08-25 22:25:02 +02001250 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1251 {
1252 win_T *prop_win = wp->w_popup_prop_win;
1253 textprop_T prop;
1254 linenr_T prop_lnum;
1255 pos_T pos;
1256 int screen_row;
1257 int screen_scol;
1258 int screen_ccol;
1259 int screen_ecol;
1260
1261 // Popup window is positioned relative to a text property.
1262 if (find_visible_prop(prop_win,
1263 wp->w_popup_prop_type, wp->w_popup_prop_id,
1264 &prop, &prop_lnum) == FAIL)
1265 {
1266 // Text property is no longer visible, hide the popup.
1267 // Unhiding the popup is done in check_popup_unhidden().
1268 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1269 {
1270 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001271 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001272 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001273 }
1274 return;
1275 }
1276
1277 // Compute the desired position from the position of the text
1278 // property. Use "wantline" and "wantcol" as offsets.
1279 pos.lnum = prop_lnum;
1280 pos.col = prop.tp_col;
1281 if (wp->w_popup_pos == POPPOS_TOPLEFT
1282 || wp->w_popup_pos == POPPOS_BOTLEFT)
1283 pos.col += prop.tp_len - 1;
1284 textpos2screenpos(prop_win, &pos, &screen_row,
1285 &screen_scol, &screen_ccol, &screen_ecol);
1286
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001287 if (screen_scol == 0)
1288 {
1289 // position is off screen, make the width zero to hide it.
1290 wp->w_width = 0;
1291 return;
1292 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001293 if (wp->w_popup_pos == POPPOS_TOPLEFT
1294 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1295 // below the text
1296 wantline = screen_row + wantline + 1;
1297 else
1298 // above the text
1299 wantline = screen_row + wantline - 1;
1300 center_vert = FALSE;
1301 if (wp->w_popup_pos == POPPOS_TOPLEFT
1302 || wp->w_popup_pos == POPPOS_BOTLEFT)
1303 // right of the text
1304 wantcol = screen_ecol + wantcol;
1305 else
1306 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001307 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001308 use_wantcol = TRUE;
1309 }
1310 else
1311 {
1312 // If no line was specified default to vertical centering.
1313 if (wantline == 0)
1314 center_vert = TRUE;
1315 else if (wantline < 0)
1316 // If "wantline" is negative it actually means zero.
1317 wantline = 0;
1318 if (wantcol < 0)
1319 // If "wantcol" is negative it actually means zero.
1320 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001321 }
1322
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001323 if (wp->w_popup_pos == POPPOS_CENTER)
1324 {
1325 // center after computing the size
1326 center_vert = TRUE;
1327 center_hor = TRUE;
1328 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001329 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001330 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001331 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1332 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001333 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001334 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001335 if (wp->w_winrow >= Rows)
1336 wp->w_winrow = Rows - 1;
1337 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001338 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001339 {
1340 // Assume that each buffer line takes one screen line, and one line
1341 // for the top border. First make sure cmdline_row is valid,
1342 // calling update_screen() will set it only later.
1343 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001344 wp->w_winrow = MAX(cmdline_row
1345 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001346 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001347
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001348 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001349 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001350 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1351 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001352 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001353 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001354 // Need to see at least one character after the decoration.
1355 if (wp->w_wincol > Columns - left_extra - 1)
1356 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001357 }
1358 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001359
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001360 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001361 // When left aligned use the space available, but shift to the left when we
1362 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001363 maxspace = Columns - wp->w_wincol - left_extra;
1364 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001365 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001366 {
1367 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001369 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001370
1371 if (wp->w_p_nu || wp->w_p_rnu)
1372 margin_width = number_width(wp) + 1;
1373#ifdef FEAT_FOLDING
1374 margin_width += wp->w_p_fdc;
1375#endif
1376#ifdef FEAT_SIGNS
1377 if (signcolumn_on(wp))
1378 margin_width += 2;
1379#endif
1380 if (margin_width >= maxwidth)
1381 margin_width = maxwidth - 1;
1382
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001383 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001384 minheight = wp->w_minheight;
1385#ifdef FEAT_TERMINAL
1386 // A terminal popup initially does not have content, use a default minimal
1387 // width of 20 characters and height of 5 lines.
1388 if (wp->w_buffer->b_term != NULL)
1389 {
1390 if (minwidth == 0)
1391 minwidth = 20;
1392 if (minheight == 0)
1393 minheight = 5;
1394 }
1395#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001396
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001397 if (wp->w_maxheight > 0)
1398 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001399 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1400 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001401
Bram Moolenaar8d241042019-06-12 23:40:01 +02001402 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001403 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001404 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001405 if (wp->w_topline < 1)
1406 wp->w_topline = 1;
1407 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001408 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1409
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001410 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001411 // Use a minimum width of one, so that something shows when there is no
1412 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001413 // When "firstline" is -1 then start with the last buffer line and go
1414 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001415 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001416 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001417 if (wp->w_firstline < 0)
1418 lnum = wp->w_buffer->b_ml.ml_line_count;
1419 else
1420 lnum = wp->w_topline;
1421 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001422 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001423 int len;
1424 int w_width = wp->w_width;
1425
1426 // Count Tabs for what they are worth and compute the length based on
1427 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001428 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001429 if (wp->w_width < maxwidth)
1430 wp->w_width = maxwidth;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001431 len = linetabsize(wp, lnum);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001432 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001433
Boris Staletic13c11532024-12-19 20:22:19 +01001434 if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001435 && allow_adjust_left
1436 && (wp->w_popup_pos == POPPOS_TOPLEFT
1437 || wp->w_popup_pos == POPPOS_BOTLEFT))
1438 {
1439 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001440 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001441
Bram Moolenaar51c31312019-06-15 22:27:23 +02001442 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001443 {
1444 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001445
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001446 shift_by -= truncate_shift;
1447 }
1448
1449 wp->w_wincol -= shift_by;
1450 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001451 wp->w_width = maxwidth;
1452 }
Boris Staletic13c11532024-12-19 20:22:19 +01001453 if (wp->w_p_wrap)
1454 {
1455 while (len + margin_width > maxwidth)
1456 {
1457 ++wrapped;
1458 len -= maxwidth - margin_width;
1459 wp->w_width = maxwidth;
1460 used_maxwidth = TRUE;
1461 }
1462 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001463 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001464 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001465 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001466 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1467 wp->w_width = wp->w_maxwidth;
1468 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001469
1470 if (wp->w_firstline < 0)
1471 --lnum;
1472 else
1473 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001474
1475 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001476 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001477 && (wp->w_firstline >= 0
1478 ? lnum - wp->w_topline
1479 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001480 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001481 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001482 }
1483
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001484 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001485 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001486
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001487 wp->w_has_scrollbar = wp->w_want_scrollbar
1488 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001489#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001490 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1491 // Terminal window with running job never has a scrollbar, adjusts to
1492 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001493 wp->w_has_scrollbar = FALSE;
1494#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001495 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001496 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001497 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001498 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001499 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001500 // make space for the scrollbar if needed, when lines wrap and when
1501 // applying minwidth
1502 if (maxwidth + right_extra >= maxspace
1503 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1504 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001505 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001506
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001507 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1508 {
1509 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1510
1511 if (minwidth < title_len)
1512 minwidth = title_len;
1513 }
1514
1515 if (minwidth > 0 && wp->w_width < minwidth)
1516 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001517 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001518 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001519 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001520 // some columns cut off on the right
1521 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001522
1523 // If the window doesn't fit because 'minwidth' is set then the
1524 // scrollbar is at the far right of the screen, use the size without
1525 // the scrollbar.
1526 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1527 {
1528 int off = wp->w_width - maxwidth;
1529
1530 if (off > right_extra)
1531 extra_width -= right_extra;
1532 else
1533 extra_width -= off;
1534 wp->w_width = maxwidth_no_scrollbar;
1535 }
1536 else
1537 {
1538 wp->w_width = maxwidth;
1539
1540 // when adding a scrollbar below need to adjust the width
1541 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1542 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001543 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001544 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001545 {
1546 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1547 if (wp->w_wincol < 0)
1548 wp->w_wincol = 0;
1549 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001550 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1551 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1552 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001553 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001554
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001555 // Right aligned: move to the right if needed.
1556 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001557 if (leftoff >= 0)
1558 wp->w_wincol = leftoff;
1559 else if (wp->w_popup_fixed)
1560 {
1561 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001562 // columns when displaying the window, minus left border and
1563 // padding.
1564 if (-leftoff > left_extra)
1565 wp->w_leftcol = -leftoff - left_extra;
1566 wp->w_width -= wp->w_leftcol;
1567 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001568 if (wp->w_width < 0)
1569 wp->w_width = 0;
1570 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001571 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001572
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001573 if (wp->w_p_wrap || (!wp->w_popup_fixed
1574 && (wp->w_popup_pos == POPPOS_TOPLEFT
1575 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001576 {
1577 int want_col = 0;
1578
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001579 // try to show the right border and any scrollbar
1580 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001581 if (want_col > 0 && wp->w_wincol > 0
1582 && wp->w_wincol + want_col >= Columns)
1583 {
1584 wp->w_wincol = Columns - want_col;
1585 if (wp->w_wincol < 0)
1586 wp->w_wincol = 0;
1587 }
1588 }
1589
Bram Moolenaar8d241042019-06-12 23:40:01 +02001590 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1591 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001592 if (minheight > 0 && wp->w_height < minheight)
1593 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001594 if (maxheight > 0 && wp->w_height > maxheight)
1595 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001596 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001597 if (wp->w_height > Rows - wp->w_winrow)
1598 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001599
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001600 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001601 {
1602 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1603 if (wp->w_winrow < 0)
1604 wp->w_winrow = 0;
1605 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001606 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001607 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001608 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001609 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001610 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001611 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001612 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1613 {
1614 // Bottom aligned but does not fit, and less space on the other
1615 // side or "posinvert" is off: reduce height.
1616 wp->w_winrow = 0;
1617 wp->w_height = wantline - extra_height;
1618 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001619 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001620 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001621 // Not enough space and more space on the other side: make top
1622 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001623 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001624 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001625 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001626 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001627 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1628 || wp->w_popup_pos == POPPOS_TOPLEFT)
1629 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001630 if (wp != popup_dragwin
1631 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001632 && wantline * 2 > Rows
1633 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001634 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001635 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001636 // make bottom aligned and recompute the height
1637 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001638 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001639 if (wp->w_winrow < 0)
1640 {
1641 wp->w_height += wp->w_winrow;
1642 wp->w_winrow = 0;
1643 }
1644 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001645 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001646 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001647 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001648 adjust_height_for_top_aligned = TRUE;
1649 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001650 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001651
1652 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1653 && wp->w_winrow + wp->w_height + extra_height > Rows)
1654 {
1655 // Bottom of the popup goes below the last line, reduce the height and
1656 // add a scrollbar.
1657 wp->w_height = Rows - wp->w_winrow - extra_height;
1658#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001659 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001660#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001661 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001662 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001663 if (width_with_scrollbar > 0)
1664 wp->w_width = width_with_scrollbar;
1665 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001666 }
1667
1668 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001669 if (wp->w_winrow >= Rows)
1670 wp->w_winrow = Rows - 1;
1671 else if (wp->w_winrow < 0)
1672 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001673
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001674 if (wp->w_height != org_height)
1675 win_comp_scroll(wp);
1676
Bram Moolenaar17146962019-05-30 00:12:11 +02001677 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001678 if (win_valid(wp->w_popup_prop_win))
1679 {
1680 wp->w_popup_prop_changedtick =
1681 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1682 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1683 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001684
1685 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001686 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001687 if (org_winrow != wp->w_winrow
1688 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001689 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001690 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001691 || org_width != wp->w_width
1692 || org_height != wp->w_height)
1693 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001694 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001695 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1696 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001697 popup_mask_refresh = TRUE;
1698 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001699}
1700
Bram Moolenaar17627312019-06-02 19:53:44 +02001701typedef enum
1702{
1703 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001704 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001705 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001706 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001707 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001708 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001709 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001710 TYPE_PREVIEW, // preview window
1711 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001712} create_type_T;
1713
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001714/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001715 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1716 */
1717 static int
1718popup_is_notification(create_type_T type)
1719{
1720 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1721}
1722
1723/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001724 * Make "buf" empty and set the contents to "text".
1725 * Used by popup_create() and popup_settext().
1726 */
1727 static void
1728popup_set_buffer_text(buf_T *buf, typval_T text)
1729{
1730 int lnum;
1731
1732 // Clear the buffer, then replace the lines.
1733 curbuf = buf;
1734 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001735 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001736 curbuf = curwin->w_buffer;
1737
1738 // Add text to the buffer.
1739 if (text.v_type == VAR_STRING)
1740 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001741 char_u *s = text.vval.v_string;
1742
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001743 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001744 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001745 }
1746 else
1747 {
1748 list_T *l = text.vval.v_list;
1749
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001750 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001751 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001752 if (l->lv_first == &range_list_item)
1753 emsg(_(e_using_number_as_string));
1754 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001755 // list of strings
1756 add_popup_strings(buf, l);
1757 else
1758 // list of dictionaries
1759 add_popup_dicts(buf, l);
1760 }
1761 }
1762
1763 // delete the line that was in the empty buffer
1764 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001765 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001766 curbuf = curwin->w_buffer;
1767}
1768
1769/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001770 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1771 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001772 * Return FAIL if the parsing fails.
1773 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001774 static int
1775parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001776{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001777 char_u *p =
1778#ifdef FEAT_QUICKFIX
1779 !is_preview ? p_cpp :
1780#endif
1781 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001782
Bram Moolenaar258cef52019-08-21 17:29:29 +02001783 if (wp != NULL)
1784 wp->w_popup_flags &= ~POPF_INFO_MENU;
1785
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001786 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001787 {
1788 char_u *e, *dig;
1789 char_u *s = p;
1790 int x;
1791
1792 e = vim_strchr(p, ':');
1793 if (e == NULL || e[1] == NUL)
1794 return FAIL;
1795
1796 p = vim_strchr(e, ',');
1797 if (p == NULL)
1798 p = e + STRLEN(e);
1799 dig = e + 1;
1800 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001801
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001802 // Note: Keep this in sync with p_popup_option_values.
Bram Moolenaar79648732019-07-18 21:43:07 +02001803 if (STRNCMP(s, "height:", 7) == 0)
1804 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001805 if (dig != p)
1806 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001807 if (wp != NULL)
1808 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001809 if (is_preview)
1810 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001811 wp->w_maxheight = x;
1812 }
1813 }
1814 else if (STRNCMP(s, "width:", 6) == 0)
1815 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001816 if (dig != p)
1817 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001818 if (wp != NULL)
1819 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001820 if (is_preview)
1821 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001822 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001823 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001824 }
1825 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001826 else if (STRNCMP(s, "highlight:", 10) == 0)
1827 {
1828 if (wp != NULL)
1829 {
1830 int c = *p;
1831
1832 *p = NUL;
1833 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1834 s + 10, OPT_FREE|OPT_LOCAL, 0);
1835 *p = c;
1836 }
1837 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001838 else if (STRNCMP(s, "border:", 7) == 0)
1839 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001840 // Note: Keep this in sync with p_popup_option_border_values.
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001841 char_u *arg = s + 7;
1842 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1843 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1844 int i;
1845
1846 if (!on && !off)
1847 return FAIL;
1848 if (wp != NULL)
1849 {
1850 for (i = 0; i < 4; ++i)
1851 wp->w_popup_border[i] = on ? 1 : 0;
1852 if (off)
1853 // only show the X for close when there is a border
1854 wp->w_popup_close = POPCLOSE_NONE;
1855 }
1856 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001857 else if (STRNCMP(s, "align:", 6) == 0)
1858 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001859 // Note: Keep this in sync with p_popup_option_align_values.
Bram Moolenaar258cef52019-08-21 17:29:29 +02001860 char_u *arg = s + 6;
1861 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1862 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1863
1864 if (!menu && !item)
1865 return FAIL;
1866 if (wp != NULL && menu)
1867 wp->w_popup_flags |= POPF_INFO_MENU;
1868 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001869 else
1870 return FAIL;
1871 }
1872 return OK;
1873}
1874
1875/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001876 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1877 * is not NULL.
1878 * Return FAIL if the parsing fails.
1879 */
1880 int
1881parse_previewpopup(win_T *wp)
1882{
1883 return parse_popup_option(wp, TRUE);
1884}
1885
1886/*
1887 * Parse the 'completepopup' option and apply the values to window "wp" if it
1888 * is not NULL.
1889 * Return FAIL if the parsing fails.
1890 */
1891 int
1892parse_completepopup(win_T *wp)
1893{
1894 return parse_popup_option(wp, FALSE);
1895}
1896
1897/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001898 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001899 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001900 */
1901 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001902popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001903{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001904 poppos_T ppt = POPPOS_NONE;
1905
1906 if (d != NULL)
1907 ppt = get_pos_entry(d, FALSE);
1908
Bram Moolenaar79648732019-07-18 21:43:07 +02001909 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001910 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001911 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001912 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1913 }
1914 else
1915 {
1916 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1917 if (wp->w_wantline == 0) // cursor in first line
1918 {
1919 wp->w_wantline = 2;
1920 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1921 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1922 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001923 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001924
Bram Moolenaar79648732019-07-18 21:43:07 +02001925 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001926 if (wp->w_wantcol > Columns - width)
1927 {
1928 wp->w_wantcol = Columns - width;
1929 if (wp->w_wantcol < 1)
1930 wp->w_wantcol = 1;
1931 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001932
Bram Moolenaar79648732019-07-18 21:43:07 +02001933 popup_adjust_position(wp);
1934}
1935
1936/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001937 * Set w_wantline and w_wantcol for the a given screen position.
1938 * Caller must take care of running into the window border.
1939 */
1940 void
1941popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1942{
1943 wp->w_wantline = row;
1944 wp->w_wantcol = col;
1945 popup_adjust_position(wp);
1946}
1947
1948/*
1949 * Add a border and lef&right padding.
1950 */
1951 static void
1952add_border_left_right_padding(win_T *wp)
1953{
1954 int i;
1955
1956 for (i = 0; i < 4; ++i)
1957 {
1958 wp->w_popup_border[i] = 1;
1959 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1960 }
1961}
1962
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001963#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001964/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001965 * Return TRUE if there is any popup window with a terminal buffer.
1966 */
1967 static int
1968popup_terminal_exists(void)
1969{
1970 win_T *wp;
1971 tabpage_T *tp;
1972
1973 FOR_ALL_POPUPWINS(wp)
1974 if (wp->w_buffer->b_term != NULL)
1975 return TRUE;
1976 FOR_ALL_TABPAGES(tp)
1977 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1978 if (wp->w_buffer->b_term != NULL)
1979 return TRUE;
1980 return FALSE;
1981}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001982#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001983
1984/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001985 * Mark all popup windows in the current tab and global for redrawing.
1986 */
1987 void
1988popup_redraw_all(void)
1989{
1990 win_T *wp;
1991
1992 FOR_ALL_POPUPWINS(wp)
1993 wp->w_redr_type = UPD_NOT_VALID;
1994 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1995 wp->w_redr_type = UPD_NOT_VALID;
1996}
1997
1998/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001999 * Set the color for a notification window.
2000 */
2001 static void
2002popup_update_color(win_T *wp, create_type_T type)
2003{
2004 char *hiname = type == TYPE_MESSAGE_WIN
2005 ? "MessageWindow" : "PopupNotification";
Bram Moolenaar9198de32022-08-27 21:30:03 +01002006 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Yee Cheng Chine700dde2025-02-20 21:58:21 +01002007 (char_u *)hiname,
Bram Moolenaar9198de32022-08-27 21:30:03 +01002008 OPT_FREE|OPT_LOCAL, 0);
2009}
2010
2011/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002012 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002013 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02002014 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002015 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002016 */
Bram Moolenaara730e552019-06-16 19:05:31 +02002017 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02002018popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002019{
Bram Moolenaara3fce622019-06-20 02:31:49 +02002020 win_T *wp;
2021 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002022 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002023 int new_buffer;
2024 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002025 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002026 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002027
Bram Moolenaar79648732019-07-18 21:43:07 +02002028 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002029 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002030 if (in_vim9script()
2031 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2032 || check_for_dict_arg(argvars, 1) == FAIL))
2033 return NULL;
2034
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002035 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002036 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002037 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002038 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002039 if (buf == NULL)
2040 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002041 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002042 return NULL;
2043 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002044#ifdef FEAT_TERMINAL
2045 if (buf->b_term != NULL && popup_terminal_exists())
2046 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002047 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002048 return NULL;
2049 }
2050#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002051 }
2052 else if (!(argvars[0].v_type == VAR_STRING
2053 && argvars[0].vval.v_string != NULL)
2054 && !(argvars[0].v_type == VAR_LIST
2055 && argvars[0].vval.v_list != NULL))
2056 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002057 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002058 return NULL;
2059 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002060 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002061 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002062 d = argvars[1].vval.v_dict;
2063 }
2064
2065 if (d != NULL)
2066 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002067 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002068 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002069 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002070 tabnr = -1; // notifications are global by default
2071 else
2072 tabnr = 0;
2073 if (tabnr > 0)
2074 {
2075 tp = find_tabpage(tabnr);
2076 if (tp == NULL)
2077 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002078 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002079 return NULL;
2080 }
2081 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002082 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002083 else if (popup_is_notification(type))
2084 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002085
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002086 // Create the window and buffer.
2087 wp = win_alloc_popup_win();
2088 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002089 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002090 if (rettv != NULL)
2091 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002092 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002093 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002094
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002095 if (buf != NULL)
2096 {
2097 // use existing buffer
2098 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002099 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002100 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002101 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002102 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002103 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104 }
2105 else
2106 {
2107 // create a new buffer associated with the popup
2108 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002109 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002110 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002111 {
2112 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002113 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002114 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002115 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002116
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002117 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002118
Bram Moolenaar46451042019-08-24 15:50:46 +02002119 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002120 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002122 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002123 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002124 buf->b_p_ul = -1; // no undo
2125 buf->b_p_swf = FALSE; // no swap file
2126 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002127 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002128
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002129 // Avoid that 'buftype' is reset when this buffer is entered.
2130 buf->b_p_initialized = TRUE;
2131 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002132 wp->w_p_wrap = TRUE; // 'wrap' is default on
2133 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002134
Bram Moolenaara3fce622019-06-20 02:31:49 +02002135 if (tp != NULL)
2136 {
2137 // popup on specified tab page
2138 wp->w_next = tp->tp_first_popupwin;
2139 tp->tp_first_popupwin = wp;
2140 }
2141 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002142 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002143 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002144 wp->w_next = curtab->tp_first_popupwin;
2145 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002146 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002147 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002148 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002149 win_T *prev = first_popupwin;
2150
2151 // Global popup: add at the end, so that it gets displayed on top of
2152 // older ones with the same zindex. Matters for notifications.
2153 if (first_popupwin == NULL)
2154 first_popupwin = wp;
2155 else
2156 {
2157 while (prev->w_next != NULL)
2158 prev = prev->w_next;
2159 prev->w_next = wp;
2160 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002161 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002162
Bram Moolenaar79648732019-07-18 21:43:07 +02002163 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002164 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002165
Bram Moolenaar79648732019-07-18 21:43:07 +02002166 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002167 {
2168 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002169 }
2170 if (type == TYPE_ATCURSOR)
2171 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002172 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002173 set_moved_values(wp);
2174 set_moved_columns(wp, FIND_STRING);
2175 }
2176
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002177 if (type == TYPE_BEVAL)
2178 {
2179 wp->w_popup_pos = POPPOS_BOTLEFT;
2180
2181 // by default use the mouse position
2182 wp->w_wantline = mouse_row;
2183 if (wp->w_wantline <= 0) // mouse on first line
2184 {
2185 wp->w_wantline = 2;
2186 wp->w_popup_pos = POPPOS_TOPLEFT;
2187 }
2188 wp->w_wantcol = mouse_col + 1;
2189 set_mousemoved_values(wp);
2190 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2191 }
2192
Bram Moolenaar33796b32019-06-08 16:01:13 +02002193 // set default values
2194 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002195 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002196
Bram Moolenaar9198de32022-08-27 21:30:03 +01002197 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002198 {
2199 win_T *twp, *nextwin;
2200 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002201
2202 // Try to not overlap with another global popup. Guess we need 3
2203 // more screen lines than buffer lines.
2204 wp->w_wantline = 1;
2205 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2206 {
2207 nextwin = twp->w_next;
2208 if (twp != wp
2209 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2210 && twp->w_winrow <= wp->w_wantline - 1 + height
2211 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2212 {
2213 // move to below this popup and restart the loop to check for
2214 // overlap with other popups
2215 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2216 nextwin = first_popupwin;
2217 }
2218 }
2219 if (wp->w_wantline + height > Rows)
2220 {
2221 // can't avoid overlap, put on top in the hope that message goes
2222 // away soon.
2223 wp->w_wantline = 1;
2224 }
2225
2226 wp->w_wantcol = 10;
2227 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002228 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002229 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002230 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002231 for (i = 0; i < 4; ++i)
2232 wp->w_popup_border[i] = 1;
2233 wp->w_popup_padding[1] = 1;
2234 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002235
Bram Moolenaar9198de32022-08-27 21:30:03 +01002236 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002237 }
2238
Bram Moolenaara730e552019-06-16 19:05:31 +02002239 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002240 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002241 wp->w_popup_pos = POPPOS_CENTER;
2242 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002243 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002244 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002245 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002246 }
2247
Bram Moolenaara730e552019-06-16 19:05:31 +02002248 if (type == TYPE_MENU)
2249 {
2250 typval_T tv;
2251 callback_T callback;
2252
2253 tv.v_type = VAR_STRING;
2254 tv.vval.v_string = (char_u *)"popup_filter_menu";
2255 callback = get_callback(&tv);
2256 if (callback.cb_name != NULL)
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002257 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002258 set_callback(&wp->w_filter_cb, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00002259 if (callback.cb_free_name)
2260 vim_free(callback.cb_name);
2261 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002262
2263 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002264 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002265 }
2266
Bram Moolenaar79648732019-07-18 21:43:07 +02002267 if (type == TYPE_PREVIEW)
2268 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002269 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002270 wp->w_popup_close = POPCLOSE_BUTTON;
2271 for (i = 0; i < 4; ++i)
2272 wp->w_popup_border[i] = 1;
2273 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002274 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002275 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002276# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002277 if (type == TYPE_INFO)
2278 {
2279 wp->w_popup_pos = POPPOS_TOPLEFT;
2280 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2281 wp->w_popup_close = POPCLOSE_BUTTON;
2282 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002283 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002284 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002285# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002286
Bram Moolenaarae943152019-06-16 22:54:14 +02002287 for (i = 0; i < 4; ++i)
2288 VIM_CLEAR(wp->w_border_highlight[i]);
2289 for (i = 0; i < 8; ++i)
2290 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002291 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002292 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002293 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002294
Bram Moolenaar79648732019-07-18 21:43:07 +02002295 if (d != NULL)
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002296 {
Bram Moolenaar79648732019-07-18 21:43:07 +02002297 // Deal with options.
Christian Brabandtf6cdab32023-08-11 23:42:02 +02002298 if (apply_options(wp, d, TRUE) == FAIL)
2299 {
2300 (void)popup_close(wp->w_id, FALSE);
2301 return NULL;
2302 }
2303 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002304
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002305#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002306 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2307 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002308#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002309
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002310 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002311
2312 wp->w_vsep_width = 0;
2313
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002314 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002315 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002316
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002317#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002318 // When running a terminal in the popup it becomes the current window.
2319 if (buf->b_term != NULL)
2320 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002321#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002322
Bram Moolenaara730e552019-06-16 19:05:31 +02002323 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002324}
2325
2326/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002327 * popup_clear()
2328 */
2329 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002330f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002331{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002332 int force = FALSE;
2333
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002334 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2335 return;
2336
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002337 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002338 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002339 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002340}
2341
2342/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002343 * popup_create({text}, {options})
2344 */
2345 void
2346f_popup_create(typval_T *argvars, typval_T *rettv)
2347{
Bram Moolenaar17627312019-06-02 19:53:44 +02002348 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002349}
2350
2351/*
2352 * popup_atcursor({text}, {options})
2353 */
2354 void
2355f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2356{
Bram Moolenaar17627312019-06-02 19:53:44 +02002357 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002358}
2359
2360/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002361 * popup_beval({text}, {options})
2362 */
2363 void
2364f_popup_beval(typval_T *argvars, typval_T *rettv)
2365{
2366 popup_create(argvars, rettv, TYPE_BEVAL);
2367}
2368
2369/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002370 * Invoke the close callback for window "wp" with value "result".
2371 * Careful: The callback may make "wp" invalid!
2372 */
2373 static void
2374invoke_popup_callback(win_T *wp, typval_T *result)
2375{
2376 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002377 typval_T argv[3];
2378
Christian Brabandt6701abf2023-11-19 10:52:50 +01002379 rettv.v_type = VAR_UNKNOWN;
2380
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002381 argv[0].v_type = VAR_NUMBER;
2382 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2383
2384 if (result != NULL && result->v_type != VAR_UNKNOWN)
2385 copy_tv(result, &argv[1]);
2386 else
2387 {
2388 argv[1].v_type = VAR_NUMBER;
2389 argv[1].vval.v_number = 0;
2390 }
2391
2392 argv[2].v_type = VAR_UNKNOWN;
2393
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002394 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002395 if (result != NULL)
2396 clear_tv(&argv[1]);
2397 clear_tv(&rettv);
2398}
2399
2400/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002401 * Make "prevwin" the current window, unless it's equal to "wp".
2402 * Otherwise make "firstwin" the current window.
2403 */
2404 static void
2405back_to_prevwin(win_T *wp)
2406{
2407 if (win_valid(prevwin) && wp != prevwin)
2408 win_enter(prevwin, FALSE);
2409 else
2410 win_enter(firstwin, FALSE);
2411}
2412
2413/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002414 * Close popup "wp" and invoke any close callback for it.
2415 */
2416 static void
2417popup_close_and_callback(win_T *wp, typval_T *arg)
2418{
2419 int id = wp->w_id;
2420
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002421#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002422 if (wp == curwin && curbuf->b_term != NULL)
2423 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002424 win_T *owp;
2425
2426 // Closing popup window with a terminal: put focus back on the first
2427 // that works:
2428 // - another popup window with a terminal
2429 // - the previous window
2430 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002431 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002432 if (owp != curwin && owp->w_buffer->b_term != NULL)
2433 break;
2434 if (owp != NULL)
2435 win_enter(owp, FALSE);
2436 else
2437 {
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002438 FOR_ALL_POPUPWINS_IN_TAB(curtab, owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002439 if (owp != curwin && owp->w_buffer->b_term != NULL)
2440 break;
2441 if (owp != NULL)
2442 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002443 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002444 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002445 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002446 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002447#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002448
Bram Moolenaar49540192019-12-11 19:34:54 +01002449 // Just in case a check higher up is missing.
2450 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002451 {
2452 // To avoid getting stuck when win_execute() does something that causes
2453 // an error, stop calling the filter callback.
2454 free_callback(&wp->w_filter_cb);
2455
Bram Moolenaar49540192019-12-11 19:34:54 +01002456 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002457 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002458
Bram Moolenaarcee52202020-03-11 14:19:58 +01002459 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002460 if (wp->w_close_cb.cb_name != NULL)
2461 // Careful: This may make "wp" invalid.
2462 invoke_popup_callback(wp, arg);
2463
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002464 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002465 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002466}
2467
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002468 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002469popup_close_with_retval(win_T *wp, int retval)
2470{
2471 typval_T res;
2472
2473 res.v_type = VAR_NUMBER;
2474 res.vval.v_number = retval;
2475 popup_close_and_callback(wp, &res);
2476}
2477
Bram Moolenaar3397f742019-06-02 18:40:06 +02002478/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002479 * Close popup "wp" because of a mouse click.
2480 */
2481 void
2482popup_close_for_mouse_click(win_T *wp)
2483{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002484 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002485}
2486
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002487 static void
2488check_mouse_moved(win_T *wp, win_T *mouse_wp)
2489{
2490 // Close the popup when all if these are true:
2491 // - the mouse is not on this popup
2492 // - "mousemoved" was used
2493 // - the mouse is no longer on the same screen row or the mouse column is
2494 // outside of the relevant text
2495 if (wp != mouse_wp
2496 && wp->w_popup_mouse_row != 0
2497 && (wp->w_popup_mouse_row != mouse_row
2498 || mouse_col < wp->w_popup_mouse_mincol
2499 || mouse_col > wp->w_popup_mouse_maxcol))
2500 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002501 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002502 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002503 }
2504}
2505
2506/*
2507 * Called when the mouse moved: may close a popup with "mousemoved".
2508 */
2509 void
2510popup_handle_mouse_moved(void)
2511{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002512 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002513 win_T *mouse_wp;
2514 int row = mouse_row;
2515 int col = mouse_col;
2516
2517 // find the window where the mouse is in
2518 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2519
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002520 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2521 {
2522 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002523 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002524 }
2525 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2526 {
2527 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002528 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002529 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002530}
2531
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002532/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002533 * In a filter: check if the typed key is a mouse event that is used for
2534 * dragging the popup.
2535 */
2536 static void
2537filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2538{
2539 int row = mouse_row;
2540 int col = mouse_col;
2541
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002542 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002543 && is_mouse_key(c)
2544 && (wp == popup_dragwin
2545 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2546 // do not consume the key, allow for dragging the popup
2547 rettv->vval.v_number = 0;
2548}
2549
Bram Moolenaara730e552019-06-16 19:05:31 +02002550/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002551 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002552 */
2553 void
2554f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2555{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002556 int id;
2557 win_T *wp;
2558 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002559 typval_T res;
2560 int c;
2561 linenr_T old_lnum;
2562
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002563 if (in_vim9script()
2564 && (check_for_number_arg(argvars, 0) == FAIL
2565 || check_for_string_arg(argvars, 1) == FAIL))
2566 return;
2567
2568 id = tv_get_number(&argvars[0]);
2569 wp = win_id2wp(id);
2570 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002571 // If the popup has been closed do not consume the key.
2572 if (wp == NULL)
2573 return;
2574
2575 c = *key;
2576 if (c == K_SPECIAL && key[1] != NUL)
2577 c = TO_SPECIAL(key[1], key[2]);
2578
2579 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002580 rettv->v_type = VAR_BOOL;
2581 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002582 res.v_type = VAR_NUMBER;
2583
2584 old_lnum = wp->w_cursor.lnum;
Christian Brabandtbadeedd2023-08-13 19:25:28 +02002585 if (c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2586 {
2587 if (wp->w_cursor.lnum > 1)
2588 --wp->w_cursor.lnum;
2589 else
2590 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2591 }
2592 if (c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
2593 {
2594 if (wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2595 ++wp->w_cursor.lnum;
2596 else
2597 wp->w_cursor.lnum = 1;
2598 }
Bram Moolenaara730e552019-06-16 19:05:31 +02002599 if (old_lnum != wp->w_cursor.lnum)
2600 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002601 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002602 return;
2603 }
2604
2605 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2606 {
2607 // Cancelled, invoke callback with -1
2608 res.vval.v_number = -1;
2609 popup_close_and_callback(wp, &res);
2610 return;
2611 }
2612 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2613 {
2614 // Invoke callback with current index.
2615 res.vval.v_number = wp->w_cursor.lnum;
2616 popup_close_and_callback(wp, &res);
2617 return;
2618 }
2619
2620 filter_handle_drag(wp, c, rettv);
2621}
2622
2623/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002624 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002625 */
2626 void
2627f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2628{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002629 int id;
2630 win_T *wp;
2631 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002632 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002633 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002634
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002635 if (in_vim9script()
2636 && (check_for_number_arg(argvars, 0) == FAIL
2637 || check_for_string_arg(argvars, 1) == FAIL))
2638 return;
2639
2640 id = tv_get_number(&argvars[0]);
2641 wp = win_id2wp(id);
2642 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002643 // If the popup has been closed don't consume the key.
2644 if (wp == NULL)
2645 return;
2646
Bram Moolenaara730e552019-06-16 19:05:31 +02002647 c = *key;
Ernie Raelb14c3252024-07-19 16:37:09 +02002648 if (c == CAR && need_wait_return)
2649 return;
Bram Moolenaara730e552019-06-16 19:05:31 +02002650 if (c == K_SPECIAL && key[1] != NUL)
2651 c = TO_SPECIAL(key[1], key[2]);
2652
Bram Moolenaara42d9452019-06-15 21:46:30 +02002653 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002654 rettv->v_type = VAR_BOOL;
2655 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002656
Bram Moolenaara730e552019-06-16 19:05:31 +02002657 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002658 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002659 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002660 res.vval.v_number = 0;
2661 else
2662 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002663 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002664 return;
2665 }
2666
2667 // Invoke callback
2668 res.v_type = VAR_NUMBER;
2669 popup_close_and_callback(wp, &res);
2670}
2671
2672/*
2673 * popup_dialog({text}, {options})
2674 */
2675 void
2676f_popup_dialog(typval_T *argvars, typval_T *rettv)
2677{
2678 popup_create(argvars, rettv, TYPE_DIALOG);
2679}
2680
2681/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002682 * popup_menu({text}, {options})
2683 */
2684 void
2685f_popup_menu(typval_T *argvars, typval_T *rettv)
2686{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002687 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002688}
2689
2690/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002691 * popup_notification({text}, {options})
2692 */
2693 void
2694f_popup_notification(typval_T *argvars, typval_T *rettv)
2695{
2696 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2697}
2698
2699/*
2700 * Find the popup window with window-ID "id".
2701 * If the popup window does not exist NULL is returned.
2702 * If the window is not a popup window, and error message is given.
2703 */
2704 static win_T *
2705find_popup_win(int id)
2706{
2707 win_T *wp = win_id2wp(id);
2708
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002709 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002710 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002711 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002712 return NULL;
2713 }
2714 return wp;
2715}
2716
2717/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002718 * popup_close({id})
2719 */
2720 void
2721f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2722{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002723 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002724 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002725
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002726 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2727 return;
2728
2729 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002730 if (
2731# ifdef FEAT_TERMINAL
2732 // if the popup contains a terminal it will become hidden
2733 curbuf->b_term == NULL &&
2734# endif
2735 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002736 return;
2737
2738 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002739 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002740 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002741}
2742
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002743 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002744popup_hide(win_T *wp)
2745{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002746#ifdef FEAT_TERMINAL
2747 if (error_if_term_popup_window())
2748 return;
2749#endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002750 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
2751 return;
2752
2753 wp->w_popup_flags |= POPF_HIDDEN;
2754 // Do not decrement b_nwindows, we still reference the buffer.
Christian Brabandtf00f4d92024-09-03 18:20:13 +02002755 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
2756 clear_cmdline = TRUE;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002757 redraw_all_later(UPD_NOT_VALID);
2758 popup_mask_refresh = TRUE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002759}
2760
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002761/*
2762 * popup_hide({id})
2763 */
2764 void
2765f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2766{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002767 int id;
2768 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002769
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002770 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2771 return;
2772
2773 id = (int)tv_get_number(argvars);
2774 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002775 if (wp == NULL)
2776 return;
2777
2778 popup_hide(wp);
2779 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002780}
2781
2782 void
2783popup_show(win_T *wp)
2784{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002785 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2786 return;
2787
2788 wp->w_popup_flags &= ~POPF_HIDDEN;
2789 redraw_all_later(UPD_NOT_VALID);
2790 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002791}
2792
2793/*
2794 * popup_show({id})
2795 */
2796 void
2797f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2798{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002799 int id;
2800 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002801
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002802 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2803 return;
2804
2805 id = (int)tv_get_number(argvars);
2806 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002807 if (wp == NULL)
2808 return;
2809
2810 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
2811 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002812#ifdef FEAT_QUICKFIX
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002813 if (wp->w_popup_flags & POPF_INFO)
2814 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002815#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002816}
2817
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002818/*
2819 * popup_settext({id}, {text})
2820 */
2821 void
2822f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2823{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002824 int id;
2825 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002826
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002827 if (in_vim9script()
2828 && (check_for_number_arg(argvars, 0) == FAIL
2829 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2830 return;
2831
2832 id = (int)tv_get_number(&argvars[0]);
2833 wp = find_popup_win(id);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002834 if (wp == NULL)
2835 return;
2836
2837 if (check_for_string_or_list_arg(argvars, 1) == FAIL)
2838 return;
2839
2840 popup_set_buffer_text(wp->w_buffer, argvars[1]);
2841 redraw_win_later(wp, UPD_NOT_VALID);
2842 popup_adjust_position(wp);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002843}
2844
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002845/*
2846 * popup_setbuf({id}, {bufnr})
2847 */
2848 void
2849f_popup_setbuf(typval_T *argvars, typval_T *rettv UNUSED)
2850{
2851 int id;
2852 win_T *wp;
2853 buf_T *buf;
2854
2855 rettv->v_type = VAR_BOOL;
2856 rettv->vval.v_number = VVAL_FALSE;
2857
2858 if (check_for_number_arg(argvars, 0) == FAIL
2859 || check_for_buffer_arg(argvars, 1) == FAIL)
2860 return;
2861
2862 id = (int)tv_get_number(&argvars[0]);
2863 wp = find_popup_win(id);
2864 if (wp == NULL)
2865 return;
2866
2867 buf = tv_get_buf_from_arg(&argvars[1]);
2868
2869 if (buf == NULL)
2870 return;
2871#ifdef FEAT_TERMINAL
2872 if (buf->b_term != NULL && popup_terminal_exists())
2873 {
2874 emsg(_(e_cannot_open_second_popup_with_terminal));
2875 return;
2876 }
2877#endif
2878
2879 if (wp->w_buffer != buf)
2880 {
2881 wp->w_buffer->b_nwindows--;
2882 win_init_popup_win(wp, buf);
2883 set_local_options_default(wp, FALSE);
2884 swap_exists_action = SEA_READONLY;
2885 buffer_ensure_loaded(buf);
2886 swap_exists_action = SEA_NONE;
2887 redraw_win_later(wp, UPD_NOT_VALID);
2888 popup_adjust_position(wp);
2889 }
2890 rettv->vval.v_number = VVAL_TRUE;
2891}
2892
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002893 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002894popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002895{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002896 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002897 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002898 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002899 clear_cmdline = TRUE;
2900 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002901
Bram Moolenaar43568642022-08-28 13:02:45 +01002902#ifdef HAS_MESSAGE_WINDOW
2903 if (wp == message_win)
2904 message_win = NULL;
2905#endif
2906
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002907 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002908 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002909}
2910
Bram Moolenaar82106932020-03-13 14:34:38 +01002911 static void
2912error_for_popup_window(void)
2913{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002914 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002915}
2916
2917 int
2918error_if_popup_window(int also_with_term UNUSED)
2919{
2920 // win_execute() may set "curwin" to a popup window temporarily, but many
2921 // commands are disallowed then. When a terminal runs in the popup most
2922 // things are allowed. When a terminal is finished it can be closed.
2923 if (WIN_IS_POPUP(curwin)
2924# ifdef FEAT_TERMINAL
2925 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002926# endif
2927 )
2928 {
2929 error_for_popup_window();
2930 return TRUE;
2931 }
2932 return FALSE;
2933}
2934
Bram Moolenaarec583842019-05-26 14:11:23 +02002935/*
2936 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002937 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002938 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002939 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002940 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002941popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002942{
2943 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002944 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002945 win_T *prev = NULL;
2946
Bram Moolenaarec583842019-05-26 14:11:23 +02002947 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002948 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002949 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002950 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002951 if (wp == curwin)
2952 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002953 if (!force)
2954 {
2955 error_for_popup_window();
2956 return FAIL;
2957 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002958 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002959 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002960 if (prev == NULL)
2961 first_popupwin = wp->w_next;
2962 else
2963 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002964 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002965 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002966 }
2967
Bram Moolenaarec583842019-05-26 14:11:23 +02002968 // go through tab-local popups
2969 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002970 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002971 return OK;
2972 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002973}
2974
2975/*
2976 * Close a popup window with Window-id "id" in tabpage "tp".
2977 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002978 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002979popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002980{
2981 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002982 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002983 win_T *prev = NULL;
2984
Bram Moolenaarec583842019-05-26 14:11:23 +02002985 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2986 if (wp->w_id == id)
2987 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002988 if (wp == curwin)
2989 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002990 if (!force)
2991 {
2992 error_for_popup_window();
2993 return FAIL;
2994 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002995 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002996 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002997 if (prev == NULL)
2998 *root = wp->w_next;
2999 else
3000 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02003001 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02003002 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02003003 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02003004 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003005}
3006
3007 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003008close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003009{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003010 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003011 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003012 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003013 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003014 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003015 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02003016 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02003017 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003018}
3019
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003020/*
3021 * popup_move({id}, {options})
3022 */
3023 void
3024f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
3025{
Bram Moolenaarae943152019-06-16 22:54:14 +02003026 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003027 int id;
3028 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003029
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003030 if (in_vim9script()
3031 && (check_for_number_arg(argvars, 0) == FAIL
3032 || check_for_dict_arg(argvars, 1) == FAIL))
3033 return;
3034
3035 id = (int)tv_get_number(argvars);
3036 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003037 if (wp == NULL)
3038 return; // invalid {id}
3039
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003040 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003041 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003042 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003043
Bram Moolenaarae943152019-06-16 22:54:14 +02003044 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003045
3046 if (wp->w_winrow + wp->w_height >= cmdline_row)
3047 clear_cmdline = TRUE;
3048 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003049}
3050
Bram Moolenaarbc133542019-05-29 20:26:46 +02003051/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003052 * popup_setoptions({id}, {options})
3053 */
3054 void
3055f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
3056{
3057 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003058 int id;
3059 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003060 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003061
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02003062 if (in_vim9script()
3063 && (check_for_number_arg(argvars, 0) == FAIL
3064 || check_for_dict_arg(argvars, 1) == FAIL))
3065 return;
3066
3067 id = (int)tv_get_number(argvars);
3068 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02003069 if (wp == NULL)
3070 return; // invalid {id}
3071
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01003072 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02003073 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02003074 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003075 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02003076
Christian Brabandtf6cdab32023-08-11 23:42:02 +02003077 (void)apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02003078
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003079 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003080 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02003081 popup_adjust_position(wp);
3082}
3083
3084/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003085 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02003086 */
3087 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003088f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02003089{
3090 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003091 int id;
3092 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003093 int top_extra;
3094 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003095
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003096 if (rettv_dict_alloc(rettv) == FAIL)
3097 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003098
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003099 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3100 return;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003101
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003102 id = (int)tv_get_number(argvars);
3103 wp = find_popup_win(id);
3104 if (wp == NULL)
3105 return; // invalid {id}
3106 top_extra = popup_top_extra(wp);
3107 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003108
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003109 // we know how much space we need, avoid resizing halfway
3110 dict = rettv->vval.v_dict;
3111 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003112
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003113 dict_add_number(dict, "line", wp->w_winrow + 1);
3114 dict_add_number(dict, "col", wp->w_wincol + 1);
3115 dict_add_number(dict, "width", wp->w_width + left_extra
3116 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3117 dict_add_number(dict, "height", wp->w_height + top_extra
3118 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003119
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003120 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3121 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3122 dict_add_number(dict, "core_width", wp->w_width);
3123 dict_add_number(dict, "core_height", wp->w_height);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003124
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003125 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
3126 dict_add_number(dict, "firstline", wp->w_topline);
3127 dict_add_number(dict, "lastline", wp->w_botline - 1);
3128 dict_add_number(dict, "visible",
3129 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
3130
3131 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003132}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003133
3134/*
3135 * popup_list()
3136 */
3137 void
3138f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3139{
3140 win_T *wp;
3141 tabpage_T *tp;
3142
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003143 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003144 return;
3145 FOR_ALL_POPUPWINS(wp)
3146 list_append_number(rettv->vval.v_list, wp->w_id);
3147 FOR_ALL_TABPAGES(tp)
3148 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3149 list_append_number(rettv->vval.v_list, wp->w_id);
3150}
3151
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003152/*
3153 * popup_locate({row}, {col})
3154 */
3155 void
3156f_popup_locate(typval_T *argvars, typval_T *rettv)
3157{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003158 int row;
3159 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003160 win_T *wp;
3161
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003162 if (in_vim9script()
3163 && (check_for_number_arg(argvars, 0) == FAIL
3164 || check_for_number_arg(argvars, 1) == FAIL))
3165 return;
3166
3167 row = tv_get_number(&argvars[0]) - 1;
3168 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003169 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003170 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003171 rettv->vval.v_number = wp->w_id;
3172}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003173
3174/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003175 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3176 */
3177 static void
3178get_padding_border(dict_T *dict, int *array, char *name)
3179{
3180 list_T *list;
3181 int i;
3182
3183 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3184 return;
3185
3186 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003187 if (list == NULL)
3188 return;
3189
3190 dict_add_list(dict, name, list);
3191 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3192 for (i = 0; i < 4; ++i)
3193 list_append_number(list, array[i]);
Bram Moolenaarae943152019-06-16 22:54:14 +02003194}
3195
3196/*
3197 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3198 */
3199 static void
3200get_borderhighlight(dict_T *dict, win_T *wp)
3201{
3202 list_T *list;
3203 int i;
3204
3205 for (i = 0; i < 4; ++i)
3206 if (wp->w_border_highlight[i] != NULL)
3207 break;
3208 if (i == 4)
3209 return;
3210
3211 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003212 if (list == NULL)
3213 return;
3214
3215 dict_add_list(dict, "borderhighlight", list);
3216 for (i = 0; i < 4; ++i)
3217 list_append_string(list, wp->w_border_highlight[i], -1);
Bram Moolenaarae943152019-06-16 22:54:14 +02003218}
3219
3220/*
3221 * For popup_getoptions(): add a "borderchars" entry to "dict".
3222 */
3223 static void
3224get_borderchars(dict_T *dict, win_T *wp)
3225{
3226 list_T *list;
3227 int i;
3228 char_u buf[NUMBUFLEN];
3229 int len;
3230
3231 for (i = 0; i < 8; ++i)
3232 if (wp->w_border_char[i] != 0)
3233 break;
3234 if (i == 8)
3235 return;
3236
3237 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003238 if (list == NULL)
3239 return;
3240
3241 dict_add_list(dict, "borderchars", list);
3242 for (i = 0; i < 8; ++i)
Bram Moolenaarae943152019-06-16 22:54:14 +02003243 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003244 len = mb_char2bytes(wp->w_border_char[i], buf);
3245 list_append_string(list, buf, len);
Bram Moolenaarae943152019-06-16 22:54:14 +02003246 }
3247}
3248
3249/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003250 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003251 */
3252 static void
3253get_moved_list(dict_T *dict, win_T *wp)
3254{
3255 list_T *list;
3256
3257 list = list_alloc();
3258 if (list != NULL)
3259 {
3260 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003261 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003262 list_append_number(list, wp->w_popup_mincol);
3263 list_append_number(list, wp->w_popup_maxcol);
3264 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003265 list = list_alloc();
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003266 if (list == NULL)
3267 return;
3268
3269 dict_add_list(dict, "mousemoved", list);
3270 list_append_number(list, wp->w_popup_mouse_row);
3271 list_append_number(list, wp->w_popup_mouse_mincol);
3272 list_append_number(list, wp->w_popup_mouse_maxcol);
Bram Moolenaarae943152019-06-16 22:54:14 +02003273}
3274
3275/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003276 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003277 */
3278 void
3279f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3280{
3281 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003282 int id;
3283 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003284 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003285 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003286
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003287 if (rettv_dict_alloc(rettv) == FAIL)
3288 return;
3289
3290 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3291 return;
3292
3293 id = (int)tv_get_number(argvars);
3294 wp = find_popup_win(id);
3295 if (wp == NULL)
3296 return;
3297
3298 dict = rettv->vval.v_dict;
3299 dict_add_number(dict, "line", wp->w_wantline);
3300 dict_add_number(dict, "col", wp->w_wantcol);
3301 dict_add_number(dict, "minwidth", wp->w_minwidth);
3302 dict_add_number(dict, "minheight", wp->w_minheight);
3303 dict_add_number(dict, "maxheight", wp->w_maxheight);
3304 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
3305 dict_add_number(dict, "firstline", wp->w_firstline);
3306 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
3307 dict_add_number(dict, "zindex", wp->w_zindex);
3308 dict_add_number(dict, "fixed", wp->w_popup_fixed);
3309 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003310 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003311 proptype_T *pt = text_prop_type_by_id(
3312 wp->w_popup_prop_win->w_buffer,
3313 wp->w_popup_prop_type);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003314
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003315 if (pt != NULL)
3316 dict_add_string(dict, "textprop", pt->pt_name);
3317 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3318 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3319 }
3320 dict_add_string(dict, "title", wp->w_popup_title);
3321 dict_add_number(dict, "wrap", wp->w_p_wrap);
3322 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
3323 dict_add_number(dict, "dragall",
3324 (wp->w_popup_flags & POPF_DRAGALL) != 0);
3325 dict_add_number(dict, "mapping",
3326 (wp->w_popup_flags & POPF_MAPPING) != 0);
3327 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
3328 dict_add_number(dict, "posinvert",
3329 (wp->w_popup_flags & POPF_POSINVERT) != 0);
3330 dict_add_number(dict, "cursorline",
3331 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
3332 dict_add_string(dict, "highlight", wp->w_p_wcr);
3333 if (wp->w_scrollbar_highlight != NULL)
3334 dict_add_string(dict, "scrollbarhighlight",
3335 wp->w_scrollbar_highlight);
3336 if (wp->w_thumb_highlight != NULL)
3337 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003338
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003339 // find the tabpage that holds this popup
3340 i = 1;
3341 FOR_ALL_TABPAGES(tp)
3342 {
3343 win_T *twp;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003344
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003345 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3346 if (twp->w_id == id)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003347 break;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003348 if (twp != NULL)
3349 break;
3350 ++i;
3351 }
3352 if (tp == NULL)
3353 i = -1; // must be global
3354 else if (tp == curtab)
3355 i = 0;
3356 dict_add_number(dict, "tabpage", i);
3357
3358 get_padding_border(dict, wp->w_popup_padding, "padding");
3359 get_padding_border(dict, wp->w_popup_border, "border");
3360 get_borderhighlight(dict, wp);
3361 get_borderchars(dict, wp);
3362 get_moved_list(dict, wp);
3363
3364 if (wp->w_filter_cb.cb_name != NULL)
3365 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3366 if (wp->w_close_cb.cb_name != NULL)
3367 dict_add_callback(dict, "callback", &wp->w_close_cb);
3368
3369 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
3370 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3371 {
3372 dict_add_string(dict, "pos",
3373 (char_u *)poppos_entries[i].pp_name);
3374 break;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003375 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02003376
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003377 dict_add_string(dict, "close", (char_u *)(
3378 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3379 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003380
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003381# if defined(FEAT_TIMERS)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003382 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3383 ? (long)wp->w_popup_timer->tr_interval : 0L);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003384# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003385}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003386
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003387# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003388/*
3389 * Return TRUE if the current window is running a terminal in a popup window.
3390 * Return FALSE when the job has ended.
3391 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003392 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003393error_if_term_popup_window(void)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003394{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003395 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3396 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003397 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003398 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003399 return TRUE;
3400 }
3401 return FALSE;
3402}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003403# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003404
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003405/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003406 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003407 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003408 * Each calling function should use a different flag, see the list at
3409 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003410 */
3411 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003412popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003413{
3414 win_T *wp;
3415
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003416 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003417 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003418 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003419 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003420}
3421
3422/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003423 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003424 * Must have called popup_reset_handled() first.
3425 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3426 * popup with the highest zindex.
3427 */
3428 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003429find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003430{
3431 win_T *wp;
3432 win_T *found_wp;
3433 int found_zindex;
3434
3435 found_zindex = lowest ? INT_MAX : 0;
3436 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003437 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003438 if ((wp->w_popup_handled & handled_flag) == 0
3439 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003440 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003441 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003442 {
3443 found_zindex = wp->w_zindex;
3444 found_wp = wp;
3445 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003446 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003447 if ((wp->w_popup_handled & handled_flag) == 0
3448 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003449 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003450 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003451 {
3452 found_zindex = wp->w_zindex;
3453 found_wp = wp;
3454 }
3455
3456 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003457 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003458 return found_wp;
3459}
3460
3461/*
3462 * Invoke the filter callback for window "wp" with typed character "c".
3463 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003464 * Returns the return value of the filter or -1 for CTRL-C in the current
3465 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003466 * Careful: The filter may make "wp" invalid!
3467 */
3468 static int
3469invoke_popup_filter(win_T *wp, int c)
3470{
3471 int res;
3472 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003473 typval_T argv[3];
3474 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003475 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003476 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003477
Bram Moolenaara42d9452019-06-15 21:46:30 +02003478 // Emergency exit: CTRL-C closes the popup.
3479 if (c == Ctrl_C)
3480 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003481 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003482 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003483
3484 // Reset got_int to avoid the callback isn't called.
3485 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003486 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003487 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003488
3489 // If the popup is the current window it probably fails to close. Then
3490 // do not consume the key.
3491 if (was_curwin && wp == curwin)
3492 return -1;
3493 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003494 }
3495
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003496 argv[0].v_type = VAR_NUMBER;
3497 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3498
3499 // Convert the number to a string, so that the function can use:
3500 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003501 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003502 argv[1].v_type = VAR_STRING;
3503 argv[1].vval.v_string = vim_strsave(buf);
3504
3505 argv[2].v_type = VAR_UNKNOWN;
3506
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003507 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003508 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3509 {
3510 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003511 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003512 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003513 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003514 }
3515 else
3516 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003517 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3518 popup_highlight_curline(wp);
3519
Bram Moolenaar371806e2020-10-22 13:44:54 +02003520 // If an error message was given always return FALSE, so that keys are
3521 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003522 // If we get three errors in a row then close the popup. Decrement the
3523 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3524 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003525 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003526 {
3527 wp->w_filter_errors += 10;
3528 if (wp->w_filter_errors >= 30)
3529 popup_close_with_retval(wp, -1);
3530 res = FALSE;
3531 }
3532 else
3533 {
3534 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3535 --wp->w_filter_errors;
3536 res = tv_get_bool(&rettv);
3537 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003538 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003539
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003540 vim_free(argv[1].vval.v_string);
3541 clear_tv(&rettv);
3542 return res;
3543}
3544
3545/*
3546 * Called when "c" was typed: invoke popup filter callbacks.
3547 * Returns TRUE when the character was consumed,
3548 */
3549 int
3550popup_do_filter(int c)
3551{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003552 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003553 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003554 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003555 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003556 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003557 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003558
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003559#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003560 // Popup window with terminal always gets focus.
3561 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3562 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003563#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003564
Bram Moolenaar934470e2019-09-01 23:27:05 +02003565 if (recursive)
3566 return FALSE;
3567 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003568
Bram Moolenaarf6396232019-08-24 19:36:00 +02003569 if (c == K_LEFTMOUSE)
3570 {
3571 int row = mouse_row;
3572 int col = mouse_col;
3573
3574 wp = mouse_find_win(&row, &col, FIND_POPUP);
3575 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003576 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003577 }
3578
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003579 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003580 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003581 while (res == FALSE
3582 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003583 if (wp->w_filter_cb.cb_name != NULL
3584 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003585 res = invoke_popup_filter(wp, c);
3586
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003587 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003588 {
3589 int save_got_int = got_int;
3590
3591 // Reset got_int to avoid a function used in the statusline aborts.
3592 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003593 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003594 got_int |= save_got_int;
3595 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003596 recursive = FALSE;
3597 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003598
3599 // When interrupted return FALSE to avoid looping.
3600 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003601}
3602
Bram Moolenaar3397f742019-06-02 18:40:06 +02003603/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003604 * Return TRUE if there is a popup visible with a filter callback and the
3605 * "mapping" property off.
3606 */
3607 int
3608popup_no_mapping(void)
3609{
3610 int round;
3611 win_T *wp;
3612
3613 for (round = 1; round <= 2; ++round)
3614 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3615 wp != NULL; wp = wp->w_next)
3616 if (wp->w_filter_cb.cb_name != NULL
3617 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3618 return TRUE;
3619 return FALSE;
3620}
3621
3622/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003623 * Called when the cursor moved: check if any popup needs to be closed if the
3624 * cursor moved far enough.
3625 */
3626 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003627popup_check_cursor_pos(void)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003628{
3629 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003630
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003631 popup_reset_handled(POPUP_HANDLED_3);
3632 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003633 if (wp->w_popup_curwin != NULL
3634 && (curwin != wp->w_popup_curwin
3635 || curwin->w_cursor.lnum != wp->w_popup_lnum
3636 || curwin->w_cursor.col < wp->w_popup_mincol
3637 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003638 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003639}
3640
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003641/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003642 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003643 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003644 static void
3645popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003646{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003647 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003648 char_u *cells;
3649 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003650
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003651 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3652 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003653 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003654 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003655 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003656 if (wp->w_popup_mask_cells != NULL
3657 && wp->w_popup_mask_height == height
3658 && wp->w_popup_mask_width == width)
3659 return; // cache is still valid
3660
3661 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003662 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003663 if (wp->w_popup_mask_cells == NULL)
3664 return;
3665 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003666
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003667 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003668 {
3669 int cols, cole;
3670 int lines, linee;
3671
3672 li = lio->li_tv.vval.v_list->lv_first;
3673 cols = tv_get_number(&li->li_tv);
3674 if (cols < 0)
3675 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003676 if (cols <= 0)
3677 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003678 li = li->li_next;
3679 cole = tv_get_number(&li->li_tv);
3680 if (cole < 0)
3681 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003682 if (cole > width)
3683 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003684 li = li->li_next;
3685 lines = tv_get_number(&li->li_tv);
3686 if (lines < 0)
3687 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003688 if (lines <= 0)
3689 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003690 li = li->li_next;
3691 linee = tv_get_number(&li->li_tv);
3692 if (linee < 0)
3693 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003694 if (linee > height)
3695 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003696
Bram Moolenaar4012d262020-12-29 11:57:46 +01003697 for (row = lines - 1; row < linee; ++row)
3698 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003699 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003700 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003701}
3702
3703/*
3704 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3705 * "col" and "line" are screen coordinates.
3706 */
3707 static int
3708popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3709{
3710 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3711 int line = screenline - wp->w_winrow;
3712
3713 return col >= 0 && col < width
3714 && line >= 0 && line < height
3715 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003716}
3717
3718/*
3719 * Set flags in popup_transparent[] for window "wp" to "val".
3720 */
3721 static void
3722update_popup_transparent(win_T *wp, int val)
3723{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003724 if (wp->w_popup_mask == NULL)
3725 return;
3726
3727 int width = popup_width(wp);
3728 int height = popup_height(wp);
3729 listitem_T *lio, *li;
3730 int cols, cole;
3731 int lines, linee;
3732 int col, line;
3733
3734 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003735 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003736 li = lio->li_tv.vval.v_list->lv_first;
3737 cols = tv_get_number(&li->li_tv);
3738 if (cols < 0)
3739 cols = width + cols + 1;
3740 li = li->li_next;
3741 cole = tv_get_number(&li->li_tv);
3742 if (cole < 0)
3743 cole = width + cole + 1;
3744 li = li->li_next;
3745 lines = tv_get_number(&li->li_tv);
3746 if (lines < 0)
3747 lines = height + lines + 1;
3748 li = li->li_next;
3749 linee = tv_get_number(&li->li_tv);
3750 if (linee < 0)
3751 linee = height + linee + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003752
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00003753 --cols;
3754 cols -= wp->w_popup_leftoff;
3755 if (cols < 0)
3756 cols = 0;
3757 cole -= wp->w_popup_leftoff;
3758 --lines;
3759 if (lines < 0)
3760 lines = 0;
3761 for (line = lines; line < linee
3762 && line + wp->w_winrow < screen_Rows; ++line)
3763 for (col = cols; col < cole
3764 && col + wp->w_wincol < screen_Columns; ++col)
3765 popup_transparent[(line + wp->w_winrow) * screen_Columns
3766 + col + wp->w_wincol] = val;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003767 }
3768}
3769
3770/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003771 * Only called when popup window "wp" is hidden: If the window is positioned
3772 * next to a text property, and it is now visible, then unhide the popup.
3773 * We don't check if visible popups become hidden, that is done in
3774 * popup_adjust_position().
3775 * Return TRUE if the popup became unhidden.
3776 */
3777 static int
3778check_popup_unhidden(win_T *wp)
3779{
3780 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3781 {
3782 textprop_T prop;
3783 linenr_T lnum;
3784
Bram Moolenaar27724252022-05-08 15:00:04 +01003785 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3786 && find_visible_prop(wp->w_popup_prop_win,
3787 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003788 &prop, &lnum) == OK)
3789 {
3790 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003791 wp->w_popup_prop_topline = 0; // force repositioning
3792 return TRUE;
3793 }
3794 }
3795 return FALSE;
3796}
3797
3798/*
3799 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3800 * That is when the buffer in the popup was changed, or the popup is following
3801 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003802 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003803 */
3804 static int
3805popup_need_position_adjust(win_T *wp)
3806{
3807 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3808 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003809 if (win_valid(wp->w_popup_prop_win)
3810 && (wp->w_popup_prop_changedtick
3811 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3812 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3813 return TRUE;
3814
3815 // May need to adjust the width if the cursor moved.
3816 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003817}
3818
3819/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003820 * Update "popup_mask" if needed.
3821 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003822 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003823 * Also marks window lines for redrawing.
3824 */
3825 void
3826may_update_popup_mask(int type)
3827{
3828 win_T *wp;
3829 short *mask;
3830 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003831 int redraw_all_popups = FALSE;
3832 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003833
3834 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003835 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3836 // basic (such as the screen size) must have changed.
3837 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003838 {
3839 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003840 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003841 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003842
3843 // Check if any popup window buffer has changed and if any popup connected
3844 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003845 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003846 if (wp->w_popup_flags & POPF_HIDDEN)
3847 popup_mask_refresh |= check_popup_unhidden(wp);
3848 else if (popup_need_position_adjust(wp))
3849 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003850 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003851 if (wp->w_popup_flags & POPF_HIDDEN)
3852 popup_mask_refresh |= check_popup_unhidden(wp);
3853 else if (popup_need_position_adjust(wp))
3854 popup_mask_refresh = TRUE;
3855
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003856 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003857 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003858
3859 // Need to update the mask, something has changed.
3860 popup_mask_refresh = FALSE;
3861 popup_mask_tab = curtab;
3862 popup_visible = FALSE;
3863
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003864 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003865 // If redrawing only what is needed, update "popup_mask_next" and then
3866 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003867 redrawing_all_win = TRUE;
3868 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003869 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003870 redrawing_all_win = FALSE;
3871 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003872 mask = popup_mask;
3873 else
3874 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003875 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003876
3877 // Find the window with the lowest zindex that hasn't been handled yet,
3878 // so that the window with a higher zindex overwrites the value in
3879 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003880 popup_reset_handled(POPUP_HANDLED_4);
3881 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003883 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003884 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003885
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003886 popup_visible = TRUE;
3887
Bram Moolenaar12034e22019-08-25 22:25:02 +02003888 // Recompute the position if the text changed. It may make the popup
3889 // hidden if it's attach to a text property that is no longer visible.
3890 if (redraw_all_popups || popup_need_position_adjust(wp))
3891 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003892 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003893 if (wp->w_popup_flags & POPF_HIDDEN)
3894 continue;
3895 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003897 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003898 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003899 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003900 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003901 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003902 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003903 col < wp->w_wincol + width - wp->w_popup_leftoff
3904 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003905 if (wp->w_zindex < POPUPMENU_ZINDEX
3906 && pum_visible()
3907 && pum_under_menu(line, col, FALSE))
3908 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3909 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003910 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003911 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003912 }
3913
3914 // Only check which lines are to be updated if not already
3915 // updating all lines.
3916 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003917 {
3918 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3919 win_T *prev_wp = NULL;
3920
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003921 for (line = 0; line < screen_Rows; ++line)
3922 {
3923 int col_done = 0;
3924
3925 for (col = 0; col < screen_Columns; ++col)
3926 {
3927 int off = line * screen_Columns + col;
3928
3929 if (popup_mask[off] != popup_mask_next[off])
3930 {
3931 popup_mask[off] = popup_mask_next[off];
3932
3933 if (line >= cmdline_row)
3934 {
3935 // the command line needs to be cleared if text below
3936 // the popup is now visible.
3937 if (!msg_scrolled && popup_mask_next[off] == 0)
3938 clear_cmdline = TRUE;
3939 }
3940 else if (col >= col_done)
3941 {
3942 linenr_T lnum;
3943 int line_cp = line;
3944 int col_cp = col;
3945
3946 // The screen position "line" / "col" needs to be
3947 // redrawn. Figure out what window that is and update
zeertzjqf2d90a32024-02-12 20:28:01 +01003948 // w_redraw_top and w_redraw_bot. Only needs to be
3949 // done once for each window line.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3951 if (wp != NULL)
3952 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003953#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003954 // A terminal window needs to be redrawn.
3955 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003956 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003957 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003958#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003959 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003960 if (wp != prev_wp)
3961 {
3962 vim_memset(plines_cache, 0,
3963 sizeof(int) * Rows);
3964 prev_wp = wp;
3965 }
3966
3967 if (line_cp >= wp->w_height)
3968 // In (or below) status line
3969 wp->w_redr_status = TRUE;
3970 else
3971 {
3972 // compute the position in the buffer line
3973 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003974 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003975 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003976 redrawWinline(wp, lnum);
3977 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003978 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003979
3980 // This line is going to be redrawn, no need to
3981 // check until the right side of the window.
3982 col_done = wp->w_wincol + wp->w_width - 1;
3983 }
3984 }
3985 }
3986 }
3987 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003988
3989 vim_free(plines_cache);
3990 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003991
3992 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003993}
3994
3995/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003996 * If the current window is a popup and something relevant changed, recompute
3997 * the position and size.
3998 */
3999 void
4000may_update_popup_position(void)
4001{
4002 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
4003 popup_adjust_position(curwin);
4004}
4005
4006/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004007 * Return a string of "len" spaces in IObuff.
4008 */
4009 static char_u *
4010get_spaces(int len)
4011{
4012 vim_memset(IObuff, ' ', (size_t)len);
4013 IObuff[len] = NUL;
4014 return IObuff;
4015}
4016
4017/*
4018 * Update popup windows. They are drawn on top of normal windows.
4019 * "win_update" is called for each popup window, lowest zindex first.
4020 */
4021 void
4022update_popups(void (*win_update)(win_T *wp))
4023{
4024 win_T *wp;
4025 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004026 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004027 int total_width;
4028 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004029 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004030 int popup_attr;
4031 int border_attr[4];
4032 int border_char[8];
4033 char_u buf[MB_MAXBYTES];
4034 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004035 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004036 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004037 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004038 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02004039 int sb_thumb_top = 0;
4040 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004041 int attr_scroll = 0;
4042 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004043
Bram Moolenaarebec3e22020-11-28 20:22:06 +01004044 // hide the cursor until redrawing is done.
4045 cursor_off();
4046
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004047 // Find the window with the lowest zindex that hasn't been updated yet,
4048 // so that the window with a higher zindex is drawn later, thus goes on
4049 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01004050 popup_reset_handled(POPUP_HANDLED_5);
4051 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004052 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004053 int title_len = 0;
4054 int title_wincol;
4055
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004056 // This drawing uses the zindex of the popup window, so that it's on
4057 // top of the text but doesn't draw when another popup with higher
4058 // zindex is on top of the character.
4059 screen_zindex = wp->w_zindex;
4060
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004061 // Set flags in popup_transparent[] for masked cells.
4062 update_popup_transparent(wp, 1);
4063
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004064 // adjust w_winrow and w_wincol for border and padding, since
4065 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004066 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02004067 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
4068 - wp->w_popup_leftoff;
4069 if (wp->w_wincol + left_extra < 0)
4070 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004071 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004072 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004073
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004074 // Draw the popup text, unless it's off screen.
4075 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004076 {
Bram Moolenaar10476522020-09-24 22:57:31 +02004077 // May need to update the "cursorline" highlighting, which may also
4078 // change "topline"
4079 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
4080 popup_highlight_curline(wp);
4081
Bram Moolenaarc2a43162019-06-26 01:03:53 +02004082 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004083
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02004084 // move the cursor into the visible lines, otherwise executing
4085 // commands with win_execute() may cause the text to jump.
4086 if (wp->w_cursor.lnum < wp->w_topline)
4087 wp->w_cursor.lnum = wp->w_topline;
4088 else if (wp->w_cursor.lnum >= wp->w_botline)
4089 wp->w_cursor.lnum = wp->w_botline - 1;
4090 }
4091
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004092 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004093 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004094
4095 // Add offset for border and padding if not done already.
4096 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4097 {
4098 wp->w_wcol += left_extra;
4099 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4100 }
4101 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004102 {
4103 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004104 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004105 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004106
Bram Moolenaareabddc42022-04-02 15:32:16 +01004107 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004108 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004109 popup_attr = get_wcr_attr(wp);
4110
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004111 if (wp->w_winrow + total_height > cmdline_row)
4112 wp->w_popup_flags |= POPF_ON_CMDLINE;
4113 else
4114 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4115
4116
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004117 // We can only use these line drawing characters when 'encoding' is
4118 // "utf-8" and 'ambiwidth' is "single".
4119 if (enc_utf8 && *p_ambw == 's')
4120 {
4121 border_char[0] = border_char[2] = 0x2550;
4122 border_char[1] = border_char[3] = 0x2551;
4123 border_char[4] = 0x2554;
4124 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004125 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4126 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004127 border_char[7] = 0x255a;
4128 }
4129 else
4130 {
4131 border_char[0] = border_char[2] = '-';
4132 border_char[1] = border_char[3] = '|';
4133 for (i = 4; i < 8; ++i)
4134 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004135 if (wp->w_popup_flags & POPF_RESIZE)
4136 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004137 }
4138 for (i = 0; i < 8; ++i)
4139 if (wp->w_border_char[i] != 0)
4140 border_char[i] = wp->w_border_char[i];
4141
4142 for (i = 0; i < 4; ++i)
4143 {
4144 border_attr[i] = popup_attr;
4145 if (wp->w_border_highlight[i] != NULL)
4146 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4147 }
4148
Bram Moolenaard91467f2020-11-21 12:42:09 +01004149 // Title goes on top of border or padding.
4150 title_wincol = wp->w_wincol + 1;
4151 if (wp->w_popup_title != NULL)
4152 {
rbtnnc6119412021-08-07 13:08:45 +02004153 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004154
Ralf Schandlbc869872021-05-28 14:12:14 +02004155 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004156 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004157 {
4158 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4159 char_u *title_text = alloc(title_byte_len + 1);
4160
4161 if (title_text != NULL)
4162 {
4163 trunc_string(wp->w_popup_title, title_text,
4164 total_width - 2, title_byte_len + 1);
4165 screen_puts(title_text, wp->w_winrow, title_wincol,
4166 wp->w_popup_border[0] > 0
4167 ? border_attr[0] : popup_attr);
4168 vim_free(title_text);
4169 }
4170
Bram Moolenaard91467f2020-11-21 12:42:09 +01004171 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004172 }
4173 else
4174 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4175 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004176 }
4177
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004178 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004179 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004180 if (wp->w_popup_border[0] > 0)
4181 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004182 // top border; do not draw over the title
4183 if (title_len > 0)
4184 {
4185 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4186 wincol < 0 ? 0 : wincol, title_wincol,
4187 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004188 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004189 border_char[0], border_attr[0]);
4190 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4191 title_wincol + title_len, wincol + total_width,
4192 border_char[0], border_char[0], border_attr[0]);
4193 }
4194 else
4195 {
4196 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4197 wincol < 0 ? 0 : wincol, wincol + total_width,
4198 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4199 ? border_char[4] : border_char[0],
4200 border_char[0], border_attr[0]);
4201 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004202 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004203 {
4204 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4205 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004206 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004207 }
4208 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004209 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4210 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004211
Bram Moolenaard529ba52019-07-02 23:13:53 +02004212 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4213 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004214 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004215 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004216 - wp->w_has_scrollbar;
4217 if (padcol < 0)
4218 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004219 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004220 padcol = 0;
4221 }
4222 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004223 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004224 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004225 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004226 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004227 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004228 // top padding and no border; do not draw over the title
4229 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004230 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004231 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004232 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004233 row += 1;
4234 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004235 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004236 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004237 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004238 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004239
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004240 // Compute scrollbar thumb position and size.
4241 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004242 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004243 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4244 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004245 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004246
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004247 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4248 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004249 if (wp->w_topline > 1 && sb_thumb_height == height)
4250 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004251 if (sb_thumb_height == 0)
4252 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004253 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004254 // it just fits, avoid divide by zero
4255 sb_thumb_top = 0;
4256 else
4257 sb_thumb_top = (wp->w_topline - 1
4258 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004259 * (wp->w_height - sb_thumb_height)
4260 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004261 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4262 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004263 last = total_height - top_off - wp->w_popup_border[2];
4264 if (sb_thumb_top >= last)
4265 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004266 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004267
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004268 if (wp->w_scrollbar_highlight != NULL)
4269 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4270 else
4271 attr_scroll = highlight_attr[HLF_PSB];
4272 if (wp->w_thumb_highlight != NULL)
4273 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4274 else
4275 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004276 }
4277
4278 for (i = wp->w_popup_border[0];
4279 i < total_height - wp->w_popup_border[2]; ++i)
4280 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004281 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004282 // left and right padding only needed next to the body
4283 int do_padding =
4284 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4285 && i < total_height - wp->w_popup_border[2]
4286 - wp->w_popup_padding[2];
4287
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004288 row = wp->w_winrow + i;
4289
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004290 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004291 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004292 {
4293 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004294 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004295 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004296 if (do_padding && wp->w_popup_padding[3] > 0)
4297 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004298 int col = wincol + wp->w_popup_border[3];
4299
Bram Moolenaard529ba52019-07-02 23:13:53 +02004300 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004301 pad_left = wp->w_popup_padding[3];
4302 if (col < 0)
4303 {
4304 pad_left += col;
4305 col = 0;
4306 }
4307 if (pad_left > 0)
4308 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4309 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004310 // scrollbar
4311 if (wp->w_has_scrollbar)
4312 {
4313 int line = i - top_off;
4314 int scroll_col = wp->w_wincol + total_width - 1
4315 - wp->w_popup_border[1];
4316
4317 if (line >= 0 && line < wp->w_height)
4318 screen_putchar(' ', row, scroll_col,
4319 line >= sb_thumb_top
4320 && line < sb_thumb_top + sb_thumb_height
4321 ? attr_thumb : attr_scroll);
4322 else
4323 screen_putchar(' ', row, scroll_col, popup_attr);
4324 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004325 // right border
4326 if (wp->w_popup_border[1] > 0)
4327 {
4328 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004329 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004330 }
4331 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004332 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004333 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004334 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004335 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4336 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004337 }
4338
4339 if (wp->w_popup_padding[2] > 0)
4340 {
4341 // bottom padding
4342 row = wp->w_winrow + wp->w_popup_border[0]
4343 + wp->w_popup_padding[0] + wp->w_height;
4344 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004345 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004346 }
4347
4348 if (wp->w_popup_border[2] > 0)
4349 {
4350 // bottom border
4351 row = wp->w_winrow + total_height - 1;
4352 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004353 wincol < 0 ? 0 : wincol,
4354 wincol + total_width,
4355 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004356 ? border_char[7] : border_char[2],
4357 border_char[2], border_attr[2]);
4358 if (wp->w_popup_border[1] > 0)
4359 {
4360 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004361 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004362 }
4363 }
4364
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004365 if (wp->w_popup_close == POPCLOSE_BUTTON)
4366 {
4367 // close button goes on top of anything at the top-right corner
4368 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004369 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004370 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4371 }
4372
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004373 update_popup_transparent(wp, 0);
4374
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004375 // Back to the normal zindex.
4376 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004377
4378#ifdef HAS_MESSAGE_WINDOW
4379 // if this was the message window popup may start the timer now
4380 may_start_message_win_timer(wp);
4381#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004382 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004383
4384#if defined(FEAT_SEARCH_EXTRA)
4385 // In case win_update() called start_search_hl().
4386 end_search_hl();
4387#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004388}
4389
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004390/*
4391 * Mark references in callbacks of one popup window.
4392 */
4393 static int
4394set_ref_in_one_popup(win_T *wp, int copyID)
4395{
4396 int abort = FALSE;
4397 typval_T tv;
4398
4399 if (wp->w_close_cb.cb_partial != NULL)
4400 {
4401 tv.v_type = VAR_PARTIAL;
4402 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4403 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4404 }
4405 if (wp->w_filter_cb.cb_partial != NULL)
4406 {
4407 tv.v_type = VAR_PARTIAL;
4408 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4409 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4410 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004411 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004412 return abort;
4413}
4414
4415/*
4416 * Set reference in callbacks of popup windows.
4417 */
4418 int
4419set_ref_in_popups(int copyID)
4420{
4421 int abort = FALSE;
4422 win_T *wp;
4423 tabpage_T *tp;
4424
4425 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4426 abort = abort || set_ref_in_one_popup(wp, copyID);
4427
4428 FOR_ALL_TABPAGES(tp)
4429 {
4430 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4431 abort = abort || set_ref_in_one_popup(wp, copyID);
4432 if (abort)
4433 break;
4434 }
4435 return abort;
4436}
Bram Moolenaar79648732019-07-18 21:43:07 +02004437
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004438 int
4439popup_is_popup(win_T *wp)
4440{
4441 return wp->w_popup_flags != 0;
4442}
4443
4444#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004445/*
4446 * Find an existing popup used as the preview window, in the current tab page.
4447 * Return NULL if not found.
4448 */
4449 win_T *
4450popup_find_preview_window(void)
4451{
4452 win_T *wp;
4453
4454 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004455 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004456 if (wp->w_p_pvw)
4457 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004458 return NULL;
4459}
4460
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004461/*
4462 * Find an existing popup used as the info window, in the current tab page.
4463 * Return NULL if not found.
4464 */
4465 win_T *
4466popup_find_info_window(void)
4467{
4468 win_T *wp;
4469
4470 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004471 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004472 if (wp->w_popup_flags & POPF_INFO)
4473 return wp;
4474 return NULL;
4475}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004476#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004477
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004478 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004479f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4480{
4481#ifdef HAS_MESSAGE_WINDOW
4482 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4483#else
4484 rettv->vval.v_number = 0;
4485#endif
4486}
4487
4488 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004489f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4490{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004491#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004492 win_T *wp = popup_find_info_window();
4493
4494 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004495#else
4496 rettv->vval.v_number = 0;
4497#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004498}
4499
4500 void
4501f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004502{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004503#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004504 win_T *wp = popup_find_preview_window();
4505
4506 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004507#else
4508 rettv->vval.v_number = 0;
4509#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004510}
4511
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004512#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004513/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004514 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004515 * NOTE: this makes the popup the current window, so that the file can be
4516 * edited. However, it must not remain to be the current window, the caller
4517 * must make sure of that.
4518 */
4519 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004520popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004521{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004522 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004523
4524 if (wp == NULL)
4525 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004526 if (info)
4527 wp->w_popup_flags |= POPF_INFO;
4528 else
4529 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004530
4531 // Set the width to a reasonable value, so that w_topline can be computed.
4532 if (wp->w_minwidth > 0)
4533 wp->w_width = wp->w_minwidth;
4534 else if (wp->w_maxwidth > 0)
4535 wp->w_width = wp->w_maxwidth;
4536 else
4537 wp->w_width = curwin->w_width;
4538
4539 // Will switch to another buffer soon, dummy one can be wiped.
4540 wp->w_buffer->b_locked = FALSE;
4541
4542 win_enter(wp, FALSE);
4543 return OK;
4544}
4545
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004546/*
4547 * Close any preview popup.
4548 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004549 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004550popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004551{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004552 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004553
4554 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004555 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004556}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004557
4558/*
4559 * Hide the info popup.
4560 */
4561 void
4562popup_hide_info(void)
4563{
4564 win_T *wp = popup_find_info_window();
4565
4566 if (wp != NULL)
4567 popup_hide(wp);
4568}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004569
4570/*
4571 * Close any info popup.
4572 */
4573 void
4574popup_close_info(void)
4575{
4576 win_T *wp = popup_find_info_window();
4577
4578 if (wp != NULL)
4579 popup_close_with_retval(wp, -1);
4580}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004581#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004582
Bram Moolenaar9198de32022-08-27 21:30:03 +01004583#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4584
Bram Moolenaar9198de32022-08-27 21:30:03 +01004585/*
4586 * Get the message window.
4587 * Returns NULL if something failed.
4588 */
4589 win_T *
4590popup_get_message_win(void)
4591{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004592 if (message_win != NULL)
4593 return message_win;
4594
4595 int i;
4596
4597 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4598
Bram Moolenaar9198de32022-08-27 21:30:03 +01004599 if (message_win == NULL)
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004600 return NULL;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004601
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004602 // use the full screen width
4603 message_win->w_width = Columns;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004604
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004605 // position at bottom of screen
4606 message_win->w_popup_pos = POPPOS_BOTTOM;
4607 message_win->w_wantcol = 1;
4608 message_win->w_minwidth = 9999;
4609 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004610
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004611 // no padding, border at the top
4612 for (i = 0; i < 4; ++i)
4613 message_win->w_popup_padding[i] = 0;
4614 for (i = 1; i < 4; ++i)
4615 message_win->w_popup_border[i] = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004616
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004617 if (message_win->w_popup_timer != NULL)
4618 message_win->w_popup_timer->tr_keep = TRUE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004619 return message_win;
4620}
4621
4622/*
4623 * If the message window is not visible: show it
4624 * If the message window is visible: reset the timeout
4625 */
4626 void
4627popup_show_message_win(void)
4628{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004629 if (message_win == NULL)
4630 return;
4631
4632 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004633 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004634 // the highlight may have changed.
4635 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4636 popup_show(message_win);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004637 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004638 start_message_win_timer = TRUE;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004639}
4640
4641 static void
4642may_start_message_win_timer(win_T *wp)
4643{
4644 if (wp == message_win && start_message_win_timer)
4645 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004646 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004647 {
4648 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004649 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004650 message_win_time = 3000;
4651 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004652 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004653 }
4654}
4655
4656 int
4657popup_message_win_visible(void)
4658{
4659 return message_win != NULL
4660 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4661}
4662
4663/*
4664 * If the message window is visible: hide it.
4665 */
4666 void
4667popup_hide_message_win(void)
4668{
4669 if (message_win != NULL)
4670 popup_hide(message_win);
4671}
4672
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004673// Values saved in start_echowindow() and restored in end_echowindow()
4674static int save_msg_didout = FALSE;
4675static int save_msg_col = 0;
4676// Values saved in end_echowindow() and restored in start_echowindow()
4677static int ew_msg_didout = FALSE;
4678static int ew_msg_col = 0;
4679
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004680/*
4681 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004682 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004683 */
4684 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004685start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004686{
4687 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004688 save_msg_didout = msg_didout;
4689 save_msg_col = msg_col;
4690 msg_didout = ew_msg_didout;
4691 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004692 if (time_sec != 0)
4693 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004694}
4695
4696/*
4697 * Invoked after outputting a message for ":echowindow".
4698 */
4699 void
4700end_echowindow(void)
4701{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004702 in_echowindow = FALSE;
4703
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004704 if ((State & MODE_HITRETURN) == 0)
4705 // show the message window now
4706 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004707
4708 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004709 ew_msg_didout = TRUE;
4710 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4711 msg_didout = save_msg_didout;
4712 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004713}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004714#endif
4715
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004716/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004717 * Close any popup for a text property associated with "win".
4718 * Return TRUE if a popup was closed.
4719 */
4720 int
4721popup_win_closed(win_T *win)
4722{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004723 int round;
4724 win_T *wp;
4725 win_T *next;
4726 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004727
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004728 for (round = 1; round <= 2; ++round)
4729 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4730 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004731 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004732 next = wp->w_next;
4733 if (wp->w_popup_prop_win == win)
4734 {
4735 popup_close_with_retval(wp, -1);
4736 ret = TRUE;
4737 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004738 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004739 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004740}
4741
4742/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004743 * Set the title of the popup window to the file name.
4744 */
4745 void
4746popup_set_title(win_T *wp)
4747{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004748 if (wp->w_buffer->b_fname == NULL)
4749 return;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004750
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004751 char_u dirname[MAXPATHL];
4752 size_t len;
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004753
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004754 mch_dirname(dirname, MAXPATHL);
4755 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4756
4757 vim_free(wp->w_popup_title);
4758 len = STRLEN(wp->w_buffer->b_fname) + 3;
4759 wp->w_popup_title = alloc((int)len);
4760 if (wp->w_popup_title != NULL)
4761 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4762 wp->w_buffer->b_fname);
4763 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004764}
4765
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004766# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004767/*
4768 * If there is a preview window, update the title.
4769 * Used after changing directory.
4770 */
4771 void
4772popup_update_preview_title(void)
4773{
4774 win_T *wp = popup_find_preview_window();
4775
4776 if (wp != NULL)
4777 popup_set_title(wp);
4778}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004779# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004780
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004781#endif // FEAT_PROP_POPUP