blob: f9cbcef48e9ee5a282c4f5c8aed0549ebef54fd7 [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
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020095 static void
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);
101 if (di != NULL)
102 {
103 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000104 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105 else
106 {
107 list_T *list = di->di_tv.vval.v_list;
108 listitem_T *li;
109 int i;
110 int nr;
111
112 for (i = 0; i < 4; ++i)
113 array[i] = 1;
114 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100115 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200116 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200117 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
118 ++i, li = li->li_next)
119 {
120 nr = (int)tv_get_number(&li->li_tv);
121 if (nr >= 0)
122 array[i] = nr > max_val ? max_val : nr;
123 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100124 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200125 }
126 }
127}
128
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200129/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200130 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200131 */
132 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200133set_moved_values(win_T *wp)
134{
135 wp->w_popup_curwin = curwin;
136 wp->w_popup_lnum = curwin->w_cursor.lnum;
137 wp->w_popup_mincol = curwin->w_cursor.col;
138 wp->w_popup_maxcol = curwin->w_cursor.col;
139}
140
141/*
142 * Used when popup options contain "moved" with "word" or "WORD".
143 */
144 static void
145set_moved_columns(win_T *wp, int flags)
146{
147 char_u *ptr;
148 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
149
150 if (len > 0)
151 {
152 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
153 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
154 }
155}
156
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200157/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200158 * Used when popup options contain "mousemoved": set default moved values.
159 */
160 static void
161set_mousemoved_values(win_T *wp)
162{
163 wp->w_popup_mouse_row = mouse_row;
164 wp->w_popup_mouse_mincol = mouse_col;
165 wp->w_popup_mouse_maxcol = mouse_col;
166}
167
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000168 static void
169update_popup_uses_mouse_move(void)
170{
171 popup_uses_mouse_move = FALSE;
172 if (popup_visible)
173 {
174 win_T *wp;
175
176 FOR_ALL_POPUPWINS(wp)
177 if (wp->w_popup_mouse_row != 0)
178 {
179 popup_uses_mouse_move = TRUE;
180 return;
181 }
182 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
183 if (wp->w_popup_mouse_row != 0)
184 {
185 popup_uses_mouse_move = TRUE;
186 return;
187 }
188 }
189}
190
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200191/*
192 * Used when popup options contain "moved" with "word" or "WORD".
193 */
194 static void
195set_mousemoved_columns(win_T *wp, int flags)
196{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200197 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200198 char_u *text;
199 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200200 pos_T pos;
201 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200202
203 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200204 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200206 // convert text column to mouse column
207 pos.col = col;
208 pos.coladd = 0;
209 getvcol(textwp, &pos, &mcol, NULL, NULL);
210 wp->w_popup_mouse_mincol = mcol;
211
Bram Moolenaar10727682019-07-12 13:59:20 +0200212 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200213 getvcol(textwp, &pos, NULL, NULL, &mcol);
214 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200215 vim_free(text);
216 }
217}
218
219/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200220 * Return TRUE if "row"/"col" is on the border of the popup.
221 * The values are relative to the top-left corner.
222 */
223 int
224popup_on_border(win_T *wp, int row, int col)
225{
226 return (row == 0 && wp->w_popup_border[0] > 0)
227 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
228 || (col == 0 && wp->w_popup_border[3] > 0)
229 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
230}
231
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200232/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200233 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
234 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200235 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200236 * Caller should check the left mouse button was clicked.
237 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200238 */
239 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200240popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200241{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200242 if (wp->w_popup_close == POPCLOSE_BUTTON
243 && row == 0 && col == popup_width(wp) - 1)
244 {
245 popup_close_for_mouse_click(wp);
246 return TRUE;
247 }
248 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200249}
250
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200251// Values set when dragging a popup window starts.
252static int drag_start_row;
253static int drag_start_col;
254static int drag_start_wantline;
255static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200256static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200257
258/*
259 * Mouse down on border of popup window: start dragging it.
260 * Uses mouse_col and mouse_row.
261 */
262 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200263popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200264{
265 drag_start_row = mouse_row;
266 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200267 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200268 drag_start_wantline = wp->w_winrow + 1;
269 else
270 drag_start_wantline = wp->w_wantline;
271 if (wp->w_wantcol == 0)
272 drag_start_wantcol = wp->w_wincol + 1;
273 else
274 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200275
276 // Stop centering the popup
277 if (wp->w_popup_pos == POPPOS_CENTER)
278 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200279
280 drag_on_resize_handle = wp->w_popup_border[1] > 0
281 && wp->w_popup_border[2] > 0
282 && row == popup_height(wp) - 1
283 && col == popup_width(wp) - 1;
284
285 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
286 {
287 if (wp->w_popup_pos == POPPOS_TOPRIGHT
288 || wp->w_popup_pos == POPPOS_BOTRIGHT)
289 wp->w_wantcol = wp->w_wincol + 1;
290 if (wp->w_popup_pos == POPPOS_BOTLEFT)
291 wp->w_wantline = wp->w_winrow + 1;
292 wp->w_popup_pos = POPPOS_TOPLEFT;
293 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200294}
295
296/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200297 * Mouse moved while dragging a popup window: adjust the window popup position
298 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200299 */
300 void
301popup_drag(win_T *wp)
302{
303 // The popup may be closed before dragging stops.
304 if (!win_valid_popup(wp))
305 return;
306
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200307 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
308 {
309 int width_inc = mouse_col - drag_start_col;
310 int height_inc = mouse_row - drag_start_row;
311
312 if (width_inc != 0)
313 {
314 int width = wp->w_width + width_inc;
315
316 if (width < 1)
317 width = 1;
318 wp->w_minwidth = width;
319 wp->w_maxwidth = width;
320 drag_start_col = mouse_col;
321 }
322
323 if (height_inc != 0)
324 {
325 int height = wp->w_height + height_inc;
326
327 if (height < 1)
328 height = 1;
329 wp->w_minheight = height;
330 wp->w_maxheight = height;
331 drag_start_row = mouse_row;
332 }
333
334 popup_adjust_position(wp);
335 return;
336 }
337
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000338 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200339 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200340 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
341 if (wp->w_wantline < 1)
342 wp->w_wantline = 1;
343 if (wp->w_wantline > Rows)
344 wp->w_wantline = Rows;
345 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
346 if (wp->w_wantcol < 1)
347 wp->w_wantcol = 1;
348 if (wp->w_wantcol > Columns)
349 wp->w_wantcol = Columns;
350
351 popup_adjust_position(wp);
352}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200353
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200354/*
355 * Set w_firstline to match the current "wp->w_topline".
356 */
357 void
358popup_set_firstline(win_T *wp)
359{
360 int height = wp->w_height;
361
362 wp->w_firstline = wp->w_topline;
363 popup_adjust_position(wp);
364
365 // we don't want the popup to get smaller, decrement the first line
366 // until it doesn't
367 while (wp->w_firstline > 1 && wp->w_height < height)
368 {
369 --wp->w_firstline;
370 popup_adjust_position(wp);
371 }
372}
373
374/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200375 * Return TRUE if the position is in the popup window scrollbar.
376 */
377 int
378popup_is_in_scrollbar(win_T *wp, int row, int col)
379{
380 return wp->w_has_scrollbar
381 && row >= wp->w_popup_border[0]
382 && row < popup_height(wp) - wp->w_popup_border[2]
383 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
384}
385
386
387/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200388 * Handle a click in a popup window, if it is in the scrollbar.
389 */
390 void
391popup_handle_scrollbar_click(win_T *wp, int row, int col)
392{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200393 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200394 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100395 int height = popup_height(wp);
396 int new_topline = wp->w_topline;
397
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200398 if (row >= height / 2)
399 {
400 // Click in lower half, scroll down.
401 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100402 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200403 }
404 else if (wp->w_topline > 1)
405 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100406 --new_topline;
407 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200408 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100409 set_topline(wp, new_topline);
410 if (wp == curwin)
411 {
412 if (wp->w_cursor.lnum < wp->w_topline)
413 {
414 wp->w_cursor.lnum = wp->w_topline;
415 check_cursor();
416 }
417 else if (wp->w_cursor.lnum >= wp->w_botline)
418 {
419 wp->w_cursor.lnum = wp->w_botline - 1;
420 check_cursor();
421 }
422 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200423 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100424 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200425 }
426 }
427}
428
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200429#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100430/*
431 * Add a timer to "wp" with "time".
432 * If "close" is true use popup_close(), otherwise popup_hide().
433 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200434 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100435popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200436{
437 char_u cbbuf[50];
438 char_u *ptr = cbbuf;
439 typval_T tv;
440
441 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100442 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
443 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200444 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200445 {
446 wp->w_popup_timer = create_timer(time, 0);
447 wp->w_popup_timer->tr_callback = get_callback(&tv);
448 clear_tv(&tv);
449 }
450}
451#endif
452
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100453 static poppos_T
454get_pos_entry(dict_T *d, int give_error)
455{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100456 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100457 int nr;
458
459 if (str == NULL)
460 return POPPOS_NONE;
461
K.Takataeeec2542021-06-02 13:28:16 +0200462 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100463 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
464 return poppos_entries[nr].pp_val;
465
466 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100468 return POPPOS_NONE;
469}
470
Bram Moolenaar17627312019-06-02 19:53:44 +0200471/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200472 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200473 */
474 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200475apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200476{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200477 int nr;
478 char_u *str;
479 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200480
Bram Moolenaard61efa52022-07-23 09:52:04 +0100481 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200482 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100483 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200484 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100485 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200486 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100487 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200488 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200489
490 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200491 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 wp->w_wantline = nr;
493 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200494 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200495 wp->w_wantcol = nr;
496
Bram Moolenaar55881332020-08-18 13:04:15 +0200497
Bram Moolenaard61efa52022-07-23 09:52:04 +0100498 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200499 if (nr != -1)
500 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200501
Bram Moolenaar12034e22019-08-25 22:25:02 +0200502 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100503 poppos_T ppt = get_pos_entry(d, TRUE);
504
505 if (ppt != POPPOS_NONE)
506 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200507 }
508
Bram Moolenaard61efa52022-07-23 09:52:04 +0100509 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200510 if (str != NULL)
511 {
512 wp->w_popup_prop_type = 0;
513 if (*str != NUL)
514 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100515 wp->w_popup_prop_win = curwin;
516 di = dict_find(d, (char_u *)"textpropwin", -1);
517 if (di != NULL)
518 {
519 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100520 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100521 wp->w_popup_prop_win = curwin;
522 }
523
524 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200525 if (nr <= 0)
526 nr = find_prop_type_id(str, NULL);
527 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000528 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200529 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200530 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200531 }
532 }
533
534 di = dict_find(d, (char_u *)"textpropid", -1);
535 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100536 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200537}
538
Bram Moolenaarb0992022020-01-30 14:55:42 +0100539/*
540 * Handle "moved" and "mousemoved" arguments.
541 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200542 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200543handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
544{
545 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
546 {
547 char_u *s = di->di_tv.vval.v_string;
548 int flags = 0;
549
550 if (STRCMP(s, "word") == 0)
551 flags = FIND_IDENT | FIND_STRING;
552 else if (STRCMP(s, "WORD") == 0)
553 flags = FIND_STRING;
554 else if (STRCMP(s, "expr") == 0)
555 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
556 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000557 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200558 if (flags != 0)
559 {
560 if (mousemoved)
561 set_mousemoved_columns(wp, flags);
562 else
563 set_moved_columns(wp, flags);
564 }
565 }
566 else if (di->di_tv.v_type == VAR_LIST
567 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200568 && (di->di_tv.vval.v_list->lv_len == 2
569 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200570 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200571 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100572 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200573 int mincol;
574 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200575
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200576 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100577 li = l->lv_first;
578 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200579 {
580 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
581
582 // Three numbers, might be from popup_getoptions().
583 if (mousemoved)
584 wp->w_popup_mouse_row = nr;
585 else
586 wp->w_popup_lnum = nr;
587 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100588 if (nr == 0)
589 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200590 }
591
592 mincol = tv_get_number(&li->li_tv);
593 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200594 if (mousemoved)
595 {
596 wp->w_popup_mouse_mincol = mincol;
597 wp->w_popup_mouse_maxcol = maxcol;
598 }
599 else
600 {
601 wp->w_popup_mincol = mincol;
602 wp->w_popup_maxcol = maxcol;
603 }
604 }
605 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000606 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200607}
608
609 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200610check_highlight(dict_T *dict, char *name, char_u **pval)
611{
612 dictitem_T *di;
613 char_u *str;
614
615 di = dict_find(dict, (char_u *)name, -1);
616 if (di != NULL)
617 {
618 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000619 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200620 else
621 {
622 str = tv_get_string(&di->di_tv);
623 if (*str != NUL)
624 *pval = vim_strsave(str);
625 }
626 }
627}
628
Bram Moolenaarae943152019-06-16 22:54:14 +0200629/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200630 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200631 */
632 static void
633popup_show_curline(win_T *wp)
634{
635 if (wp->w_cursor.lnum < wp->w_topline)
636 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200637 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200638 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200639 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200640 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200641 if (wp->w_topline < 1)
642 wp->w_topline = 1;
643 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
644 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200645 while (wp->w_topline < wp->w_cursor.lnum
646 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
647 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
648 > wp->w_height)
649 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200650 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200651
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200652 // Don't let "firstline" cause a scroll.
653 if (wp->w_firstline > 0)
654 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200655}
656
657/*
658 * Get the sign group name for window "wp".
659 * Returns a pointer to a static buffer, overwritten on the next call.
660 */
661 static char_u *
662popup_get_sign_name(win_T *wp)
663{
664 static char buf[30];
665
666 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
667 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200668}
669
670/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200671 * Highlight the line with the cursor.
672 * Also scrolls the text to put the cursor line in view.
673 */
674 static void
675popup_highlight_curline(win_T *wp)
676{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200677 int sign_id = 0;
678 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200679
Bram Moolenaar72570732019-11-30 14:21:53 +0100680 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200681
682 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
683 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200684 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200685
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200686 if (!sign_exists_by_name(sign_name))
687 {
688 char *linehl = "PopupSelected";
689
690 if (syn_name2id((char_u *)linehl) == 0)
691 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100692 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
693 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200694 }
695
Bram Moolenaar72570732019-11-30 14:21:53 +0100696 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200697 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100698 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200699 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200700 else
701 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200702 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200703}
704
705/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200706 * Shared between popup_create() and f_popup_setoptions().
707 */
708 static void
709apply_general_options(win_T *wp, dict_T *dict)
710{
711 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200712 int nr;
713 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200714
Bram Moolenaarae943152019-06-16 22:54:14 +0200715 // TODO: flip
716
717 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200718 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200719 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100720 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200721 if (wp->w_firstline < 0)
722 wp->w_firstline = -1;
723 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200724
Bram Moolenaard61efa52022-07-23 09:52:04 +0100725 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200726 if (nr != -1)
727 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200728
Bram Moolenaard61efa52022-07-23 09:52:04 +0100729 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200730 if (str != NULL)
731 {
732 vim_free(wp->w_popup_title);
733 wp->w_popup_title = vim_strsave(str);
734 }
735
Bram Moolenaard61efa52022-07-23 09:52:04 +0100736 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200737 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200738 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200739
Bram Moolenaard61efa52022-07-23 09:52:04 +0100740 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200741 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200742 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200743 if (nr)
744 wp->w_popup_flags |= POPF_DRAG;
745 else
746 wp->w_popup_flags &= ~POPF_DRAG;
747 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100748 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000749 if (nr != -1)
750 {
751 if (nr)
752 wp->w_popup_flags |= POPF_DRAGALL;
753 else
754 wp->w_popup_flags &= ~POPF_DRAGALL;
755 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200756
Bram Moolenaard61efa52022-07-23 09:52:04 +0100757 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200758 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100759 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100760 if (nr)
761 wp->w_popup_flags |= POPF_POSINVERT;
762 else
763 wp->w_popup_flags &= ~POPF_POSINVERT;
764 }
765
Bram Moolenaard61efa52022-07-23 09:52:04 +0100766 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200767 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200768 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200769 if (nr)
770 wp->w_popup_flags |= POPF_RESIZE;
771 else
772 wp->w_popup_flags &= ~POPF_RESIZE;
773 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200774
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200775 di = dict_find(dict, (char_u *)"close", -1);
776 if (di != NULL)
777 {
778 int ok = TRUE;
779
780 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
781 {
782 char_u *s = di->di_tv.vval.v_string;
783
784 if (STRCMP(s, "none") == 0)
785 wp->w_popup_close = POPCLOSE_NONE;
786 else if (STRCMP(s, "button") == 0)
787 wp->w_popup_close = POPCLOSE_BUTTON;
788 else if (STRCMP(s, "click") == 0)
789 wp->w_popup_close = POPCLOSE_CLICK;
790 else
791 ok = FALSE;
792 }
793 else
794 ok = FALSE;
795 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000796 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200797 }
798
Bram Moolenaard61efa52022-07-23 09:52:04 +0100799 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200800 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000801 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200802 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
803 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000804#ifdef FEAT_TERMINAL
805 term_update_wincolor(wp);
806#endif
807 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200808
Bram Moolenaarae943152019-06-16 22:54:14 +0200809 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
810 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200811
Bram Moolenaar790498b2019-06-01 22:15:29 +0200812 di = dict_find(dict, (char_u *)"borderhighlight", -1);
813 if (di != NULL)
814 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200815 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000816 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200817 else
818 {
819 list_T *list = di->di_tv.vval.v_list;
820 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200821 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200822
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200823 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200824 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
825 ++i, li = li->li_next)
826 {
827 str = tv_get_string(&li->li_tv);
828 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100829 {
830 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200831 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100832 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200833 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200834 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
835 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100836 {
837 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200838 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200839 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100840 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200841 }
842 }
843
Bram Moolenaar790498b2019-06-01 22:15:29 +0200844 di = dict_find(dict, (char_u *)"borderchars", -1);
845 if (di != NULL)
846 {
847 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000848 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200849 else
850 {
851 list_T *list = di->di_tv.vval.v_list;
852 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200853 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200854
855 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100856 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200857 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200858 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
859 ++i, li = li->li_next)
860 {
861 str = tv_get_string(&li->li_tv);
862 if (*str != NUL)
863 wp->w_border_char[i] = mb_ptr2char(str);
864 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200865 if (list->lv_len == 1)
866 for (i = 1; i < 8; ++i)
867 wp->w_border_char[i] = wp->w_border_char[0];
868 if (list->lv_len == 2)
869 {
870 for (i = 4; i < 8; ++i)
871 wp->w_border_char[i] = wp->w_border_char[1];
872 for (i = 1; i < 4; ++i)
873 wp->w_border_char[i] = wp->w_border_char[0];
874 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200875 }
876 }
877 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200878
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200879 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
880 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
881
Bram Moolenaarae943152019-06-16 22:54:14 +0200882 di = dict_find(dict, (char_u *)"zindex", -1);
883 if (di != NULL)
884 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100885 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200886 if (wp->w_zindex < 1)
887 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
888 if (wp->w_zindex > 32000)
889 wp->w_zindex = 32000;
890 }
891
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200892 di = dict_find(dict, (char_u *)"mask", -1);
893 if (di != NULL)
894 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200895 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200896
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200897 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200898 {
899 listitem_T *li;
900
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200901 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200902 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200903 {
904 if (li->li_tv.v_type != VAR_LIST
905 || li->li_tv.vval.v_list == NULL
906 || li->li_tv.vval.v_list->lv_len != 4)
907 {
908 ok = FALSE;
909 break;
910 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100911 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200912 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200913 }
914 }
915 if (ok)
916 {
917 wp->w_popup_mask = di->di_tv.vval.v_list;
918 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200919 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200920 }
921 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000922 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200923 }
924
Bram Moolenaarae943152019-06-16 22:54:14 +0200925#if defined(FEAT_TIMERS)
926 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100927 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200928 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100929 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200930#endif
931
Bram Moolenaar3397f742019-06-02 18:40:06 +0200932 di = dict_find(dict, (char_u *)"moved", -1);
933 if (di != NULL)
934 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200935 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200936 handle_moved_argument(wp, di, FALSE);
937 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200938
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200939 di = dict_find(dict, (char_u *)"mousemoved", -1);
940 if (di != NULL)
941 {
942 set_mousemoved_values(wp);
943 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200944 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200945
Bram Moolenaard61efa52022-07-23 09:52:04 +0100946 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100947 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200948 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100949 if (nr != 0)
950 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200951 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100952 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200953 }
954
Bram Moolenaarae943152019-06-16 22:54:14 +0200955 di = dict_find(dict, (char_u *)"filter", -1);
956 if (di != NULL)
957 {
958 callback_T callback = get_callback(&di->di_tv);
959
960 if (callback.cb_name != NULL)
961 {
962 free_callback(&wp->w_filter_cb);
963 set_callback(&wp->w_filter_cb, &callback);
964 }
965 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100966 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200967 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200968 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200969 if (nr)
970 wp->w_popup_flags |= POPF_MAPPING;
971 else
972 wp->w_popup_flags &= ~POPF_MAPPING;
973 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200974
Bram Moolenaard61efa52022-07-23 09:52:04 +0100975 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200976 if (str != NULL)
977 {
978 if (STRCMP(str, "a") == 0)
979 wp->w_filter_mode = MODE_ALL;
980 else
981 wp->w_filter_mode = mode_str2flags(str);
982 }
983
Bram Moolenaarae943152019-06-16 22:54:14 +0200984 di = dict_find(dict, (char_u *)"callback", -1);
985 if (di != NULL)
986 {
987 callback_T callback = get_callback(&di->di_tv);
988
989 if (callback.cb_name != NULL)
990 {
991 free_callback(&wp->w_close_cb);
992 set_callback(&wp->w_close_cb, &callback);
993 }
994 }
995}
996
997/*
998 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200999 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +02001000 */
1001 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001002apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001003{
1004 int nr;
1005
1006 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001007
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001008 if (create)
1009 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001010 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1011
Bram Moolenaarae943152019-06-16 22:54:14 +02001012 apply_general_options(wp, dict);
1013
Bram Moolenaard61efa52022-07-23 09:52:04 +01001014 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001015 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001016 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001017
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001018 // when "firstline" and "cursorline" are both set and the cursor would be
1019 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001020 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1021 {
1022 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1023 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001024 else if (wp->w_cursor.lnum < wp->w_firstline
1025 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001026 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001027 wp->w_topline = wp->w_firstline;
1028 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001029 }
1030
Bram Moolenaar33796b32019-06-08 16:01:13 +02001031 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001032 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001033}
1034
1035/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001036 * Add lines to the popup from a list of strings.
1037 */
1038 static void
1039add_popup_strings(buf_T *buf, list_T *l)
1040{
1041 listitem_T *li;
1042 linenr_T lnum = 0;
1043 char_u *p;
1044
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001045 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001046 if (li->li_tv.v_type == VAR_STRING)
1047 {
1048 p = li->li_tv.vval.v_string;
1049 ml_append_buf(buf, lnum++,
1050 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1051 }
1052}
1053
1054/*
1055 * Add lines to the popup from a list of dictionaries.
1056 */
1057 static void
1058add_popup_dicts(buf_T *buf, list_T *l)
1059{
1060 listitem_T *li;
1061 listitem_T *pli;
1062 linenr_T lnum = 0;
1063 char_u *p;
1064 dict_T *dict;
1065
1066 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001067 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001068 {
1069 if (li->li_tv.v_type != VAR_DICT)
1070 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001071 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001072 return;
1073 }
1074 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001075 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001076 ml_append_buf(buf, lnum++,
1077 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1078 }
1079
1080 // add the text properties
1081 lnum = 1;
1082 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1083 {
1084 dictitem_T *di;
1085 list_T *plist;
1086
1087 dict = li->li_tv.vval.v_dict;
1088 di = dict_find(dict, (char_u *)"props", -1);
1089 if (di != NULL)
1090 {
1091 if (di->di_tv.v_type != VAR_LIST)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001094 return;
1095 }
1096 plist = di->di_tv.vval.v_list;
1097 if (plist != NULL)
1098 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001099 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001100 {
1101 if (pli->li_tv.v_type != VAR_DICT)
1102 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001103 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001104 return;
1105 }
1106 dict = pli->li_tv.vval.v_dict;
1107 if (dict != NULL)
1108 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001109 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001110
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001111 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001112 }
1113 }
1114 }
1115 }
1116 }
1117}
1118
1119/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001120 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1121 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001122 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001123popup_top_extra(win_T *wp)
1124{
1125 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1126
1127 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1128 return 1;
1129 return extra;
1130}
1131
1132/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001133 * Get the padding plus border at the left.
1134 */
1135 int
1136popup_left_extra(win_T *wp)
1137{
1138 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1139}
1140
1141/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001142 * Return the height of popup window "wp", including border and padding.
1143 */
1144 int
1145popup_height(win_T *wp)
1146{
1147 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001148 + popup_top_extra(wp)
1149 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001150}
1151
1152/*
1153 * Return the width of popup window "wp", including border, padding and
1154 * scrollbar.
1155 */
1156 int
1157popup_width(win_T *wp)
1158{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001159 // w_leftcol is how many columns of the core are left of the screen
1160 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001161 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001162 + popup_extra_width(wp)
1163 + wp->w_popup_rightoff;
1164}
1165
1166/*
1167 * Return the extra width of popup window "wp": border, padding and scrollbar.
1168 */
1169 int
1170popup_extra_width(win_T *wp)
1171{
1172 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1173 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1174 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001175}
1176
1177/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001178 * Adjust the position and size of the popup to fit on the screen.
1179 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001180 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001181popup_adjust_position(win_T *wp)
1182{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001183 linenr_T lnum;
1184 int wrapped = 0;
1185 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001186 int maxwidth_no_scrollbar;
1187 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001188 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001189 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001190 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001191 int center_vert = FALSE;
1192 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001193 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001194 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001195 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1196 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1197 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1198 int extra_height = top_extra + bot_extra;
1199 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001200 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001201 int org_winrow = wp->w_winrow;
1202 int org_wincol = wp->w_wincol;
1203 int org_width = wp->w_width;
1204 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001205 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001206 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001207 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001208 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001209 int wantline = wp->w_wantline; // adjusted for textprop
1210 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001211 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001212 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001213
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001214 wp->w_winrow = 0;
1215 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001216 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001217 wp->w_popup_leftoff = 0;
1218 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001219
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001220 // May need to update the "cursorline" highlighting, which may also change
1221 // "topline"
1222 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1223 popup_highlight_curline(wp);
1224
Bram Moolenaar12034e22019-08-25 22:25:02 +02001225 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1226 {
1227 win_T *prop_win = wp->w_popup_prop_win;
1228 textprop_T prop;
1229 linenr_T prop_lnum;
1230 pos_T pos;
1231 int screen_row;
1232 int screen_scol;
1233 int screen_ccol;
1234 int screen_ecol;
1235
1236 // Popup window is positioned relative to a text property.
1237 if (find_visible_prop(prop_win,
1238 wp->w_popup_prop_type, wp->w_popup_prop_id,
1239 &prop, &prop_lnum) == FAIL)
1240 {
1241 // Text property is no longer visible, hide the popup.
1242 // Unhiding the popup is done in check_popup_unhidden().
1243 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1244 {
1245 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001246 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001247 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001248 }
1249 return;
1250 }
1251
1252 // Compute the desired position from the position of the text
1253 // property. Use "wantline" and "wantcol" as offsets.
1254 pos.lnum = prop_lnum;
1255 pos.col = prop.tp_col;
1256 if (wp->w_popup_pos == POPPOS_TOPLEFT
1257 || wp->w_popup_pos == POPPOS_BOTLEFT)
1258 pos.col += prop.tp_len - 1;
1259 textpos2screenpos(prop_win, &pos, &screen_row,
1260 &screen_scol, &screen_ccol, &screen_ecol);
1261
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001262 if (screen_scol == 0)
1263 {
1264 // position is off screen, make the width zero to hide it.
1265 wp->w_width = 0;
1266 return;
1267 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001268 if (wp->w_popup_pos == POPPOS_TOPLEFT
1269 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1270 // below the text
1271 wantline = screen_row + wantline + 1;
1272 else
1273 // above the text
1274 wantline = screen_row + wantline - 1;
1275 center_vert = FALSE;
1276 if (wp->w_popup_pos == POPPOS_TOPLEFT
1277 || wp->w_popup_pos == POPPOS_BOTLEFT)
1278 // right of the text
1279 wantcol = screen_ecol + wantcol;
1280 else
1281 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001282 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001283 use_wantcol = TRUE;
1284 }
1285 else
1286 {
1287 // If no line was specified default to vertical centering.
1288 if (wantline == 0)
1289 center_vert = TRUE;
1290 else if (wantline < 0)
1291 // If "wantline" is negative it actually means zero.
1292 wantline = 0;
1293 if (wantcol < 0)
1294 // If "wantcol" is negative it actually means zero.
1295 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001296 }
1297
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001298 if (wp->w_popup_pos == POPPOS_CENTER)
1299 {
1300 // center after computing the size
1301 center_vert = TRUE;
1302 center_hor = TRUE;
1303 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001304 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001305 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001306 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1307 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001308 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001309 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001310 if (wp->w_winrow >= Rows)
1311 wp->w_winrow = Rows - 1;
1312 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001313 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001314 {
1315 // Assume that each buffer line takes one screen line, and one line
1316 // for the top border. First make sure cmdline_row is valid,
1317 // calling update_screen() will set it only later.
1318 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001319 wp->w_winrow = MAX(cmdline_row
1320 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001321 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001322
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001323 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001324 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001325 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1326 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001327 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001328 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001329 // Need to see at least one character after the decoration.
1330 if (wp->w_wincol > Columns - left_extra - 1)
1331 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001332 }
1333 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001334
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001335 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001336 // When left aligned use the space available, but shift to the left when we
1337 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001338 maxspace = Columns - wp->w_wincol - left_extra;
1339 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001340 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001341 {
1342 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001343 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001344 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001345
1346 if (wp->w_p_nu || wp->w_p_rnu)
1347 margin_width = number_width(wp) + 1;
1348#ifdef FEAT_FOLDING
1349 margin_width += wp->w_p_fdc;
1350#endif
1351#ifdef FEAT_SIGNS
1352 if (signcolumn_on(wp))
1353 margin_width += 2;
1354#endif
1355 if (margin_width >= maxwidth)
1356 margin_width = maxwidth - 1;
1357
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001358 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001359 minheight = wp->w_minheight;
1360#ifdef FEAT_TERMINAL
1361 // A terminal popup initially does not have content, use a default minimal
1362 // width of 20 characters and height of 5 lines.
1363 if (wp->w_buffer->b_term != NULL)
1364 {
1365 if (minwidth == 0)
1366 minwidth = 20;
1367 if (minheight == 0)
1368 minheight = 5;
1369 }
1370#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001371
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001372 if (wp->w_maxheight > 0)
1373 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001374 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1375 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001376
Bram Moolenaar8d241042019-06-12 23:40:01 +02001377 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001378 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001379 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001380 if (wp->w_topline < 1)
1381 wp->w_topline = 1;
1382 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001383 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1384
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001385 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001386 // Use a minimum width of one, so that something shows when there is no
1387 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001388 // When "firstline" is -1 then start with the last buffer line and go
1389 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001390 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001391 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001392 if (wp->w_firstline < 0)
1393 lnum = wp->w_buffer->b_ml.ml_line_count;
1394 else
1395 lnum = wp->w_topline;
1396 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001397 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001398 int len;
1399 int w_width = wp->w_width;
1400
1401 // Count Tabs for what they are worth and compute the length based on
1402 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001403 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001404 if (wp->w_width < maxwidth)
1405 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001406 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001407 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001408 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001409
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001410 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001411 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001412 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001413 {
1414 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001415 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001416 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001417 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001418 }
1419 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001420 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001421 && allow_adjust_left
1422 && (wp->w_popup_pos == POPPOS_TOPLEFT
1423 || wp->w_popup_pos == POPPOS_BOTLEFT))
1424 {
1425 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001426 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001427
Bram Moolenaar51c31312019-06-15 22:27:23 +02001428 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001429 {
1430 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001431
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001432 len -= truncate_shift;
1433 shift_by -= truncate_shift;
1434 }
1435
1436 wp->w_wincol -= shift_by;
1437 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001438 wp->w_width = maxwidth;
1439 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001440 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001441 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001442 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001443 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1444 wp->w_width = wp->w_maxwidth;
1445 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001446
1447 if (wp->w_firstline < 0)
1448 --lnum;
1449 else
1450 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001451
1452 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001453 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001454 && (wp->w_firstline >= 0
1455 ? lnum - wp->w_topline
1456 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001457 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001458 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001459 }
1460
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001461 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001462 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001463
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001464 wp->w_has_scrollbar = wp->w_want_scrollbar
1465 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001466#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001467 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1468 // Terminal window with running job never has a scrollbar, adjusts to
1469 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001470 wp->w_has_scrollbar = FALSE;
1471#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001472 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001473 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001474 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001475 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001476 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001477 // make space for the scrollbar if needed, when lines wrap and when
1478 // applying minwidth
1479 if (maxwidth + right_extra >= maxspace
1480 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1481 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001482 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001483
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001484 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1485 {
1486 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1487
1488 if (minwidth < title_len)
1489 minwidth = title_len;
1490 }
1491
1492 if (minwidth > 0 && wp->w_width < minwidth)
1493 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001494 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001495 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001496 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001497 // some columns cut off on the right
1498 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001499
1500 // If the window doesn't fit because 'minwidth' is set then the
1501 // scrollbar is at the far right of the screen, use the size without
1502 // the scrollbar.
1503 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1504 {
1505 int off = wp->w_width - maxwidth;
1506
1507 if (off > right_extra)
1508 extra_width -= right_extra;
1509 else
1510 extra_width -= off;
1511 wp->w_width = maxwidth_no_scrollbar;
1512 }
1513 else
1514 {
1515 wp->w_width = maxwidth;
1516
1517 // when adding a scrollbar below need to adjust the width
1518 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1519 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001520 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001521 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001522 {
1523 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1524 if (wp->w_wincol < 0)
1525 wp->w_wincol = 0;
1526 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001527 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1528 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1529 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001530 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001531
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001532 // Right aligned: move to the right if needed.
1533 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001534 if (leftoff >= 0)
1535 wp->w_wincol = leftoff;
1536 else if (wp->w_popup_fixed)
1537 {
1538 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001539 // columns when displaying the window, minus left border and
1540 // padding.
1541 if (-leftoff > left_extra)
1542 wp->w_leftcol = -leftoff - left_extra;
1543 wp->w_width -= wp->w_leftcol;
1544 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001545 if (wp->w_width < 0)
1546 wp->w_width = 0;
1547 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001548 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001549
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001550 if (wp->w_p_wrap || (!wp->w_popup_fixed
1551 && (wp->w_popup_pos == POPPOS_TOPLEFT
1552 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001553 {
1554 int want_col = 0;
1555
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001556 // try to show the right border and any scrollbar
1557 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001558 if (want_col > 0 && wp->w_wincol > 0
1559 && wp->w_wincol + want_col >= Columns)
1560 {
1561 wp->w_wincol = Columns - want_col;
1562 if (wp->w_wincol < 0)
1563 wp->w_wincol = 0;
1564 }
1565 }
1566
Bram Moolenaar8d241042019-06-12 23:40:01 +02001567 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1568 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001569 if (minheight > 0 && wp->w_height < minheight)
1570 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001571 if (maxheight > 0 && wp->w_height > maxheight)
1572 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001573 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001574 if (wp->w_height > Rows - wp->w_winrow)
1575 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001576
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001577 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001578 {
1579 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1580 if (wp->w_winrow < 0)
1581 wp->w_winrow = 0;
1582 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001583 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001584 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001585 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001586 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001587 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001588 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001589 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1590 {
1591 // Bottom aligned but does not fit, and less space on the other
1592 // side or "posinvert" is off: reduce height.
1593 wp->w_winrow = 0;
1594 wp->w_height = wantline - extra_height;
1595 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001596 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001597 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001598 // Not enough space and more space on the other side: make top
1599 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001600 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001601 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001602 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001603 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001604 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1605 || wp->w_popup_pos == POPPOS_TOPLEFT)
1606 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001607 if (wp != popup_dragwin
1608 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001609 && wantline * 2 > Rows
1610 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001611 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001612 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001613 // make bottom aligned and recompute the height
1614 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001615 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001616 if (wp->w_winrow < 0)
1617 {
1618 wp->w_height += wp->w_winrow;
1619 wp->w_winrow = 0;
1620 }
1621 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001622 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001623 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001624 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001625 adjust_height_for_top_aligned = TRUE;
1626 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001627 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001628
1629 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1630 && wp->w_winrow + wp->w_height + extra_height > Rows)
1631 {
1632 // Bottom of the popup goes below the last line, reduce the height and
1633 // add a scrollbar.
1634 wp->w_height = Rows - wp->w_winrow - extra_height;
1635#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001636 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001637#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001638 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001639 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001640 if (width_with_scrollbar > 0)
1641 wp->w_width = width_with_scrollbar;
1642 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001643 }
1644
1645 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001646 if (wp->w_winrow >= Rows)
1647 wp->w_winrow = Rows - 1;
1648 else if (wp->w_winrow < 0)
1649 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001650
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001651 if (wp->w_height != org_height)
1652 win_comp_scroll(wp);
1653
Bram Moolenaar17146962019-05-30 00:12:11 +02001654 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001655 if (win_valid(wp->w_popup_prop_win))
1656 {
1657 wp->w_popup_prop_changedtick =
1658 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1659 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1660 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001661
1662 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001663 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001664 if (org_winrow != wp->w_winrow
1665 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001666 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001667 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001668 || org_width != wp->w_width
1669 || org_height != wp->w_height)
1670 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001671 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001672 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1673 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001674 popup_mask_refresh = TRUE;
1675 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001676}
1677
Bram Moolenaar17627312019-06-02 19:53:44 +02001678typedef enum
1679{
1680 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001681 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001682 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001683 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001684 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001685 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001686 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001687 TYPE_PREVIEW, // preview window
1688 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001689} create_type_T;
1690
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001691/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001692 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1693 */
1694 static int
1695popup_is_notification(create_type_T type)
1696{
1697 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1698}
1699
1700/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001701 * Make "buf" empty and set the contents to "text".
1702 * Used by popup_create() and popup_settext().
1703 */
1704 static void
1705popup_set_buffer_text(buf_T *buf, typval_T text)
1706{
1707 int lnum;
1708
1709 // Clear the buffer, then replace the lines.
1710 curbuf = buf;
1711 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001712 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001713 curbuf = curwin->w_buffer;
1714
1715 // Add text to the buffer.
1716 if (text.v_type == VAR_STRING)
1717 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001718 char_u *s = text.vval.v_string;
1719
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001720 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001721 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001722 }
1723 else
1724 {
1725 list_T *l = text.vval.v_list;
1726
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001727 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001728 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001729 if (l->lv_first == &range_list_item)
1730 emsg(_(e_using_number_as_string));
1731 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001732 // list of strings
1733 add_popup_strings(buf, l);
1734 else
1735 // list of dictionaries
1736 add_popup_dicts(buf, l);
1737 }
1738 }
1739
1740 // delete the line that was in the empty buffer
1741 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001742 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001743 curbuf = curwin->w_buffer;
1744}
1745
1746/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001747 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1748 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001749 * Return FAIL if the parsing fails.
1750 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001751 static int
1752parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001753{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001754 char_u *p =
1755#ifdef FEAT_QUICKFIX
1756 !is_preview ? p_cpp :
1757#endif
1758 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001759
Bram Moolenaar258cef52019-08-21 17:29:29 +02001760 if (wp != NULL)
1761 wp->w_popup_flags &= ~POPF_INFO_MENU;
1762
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001763 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001764 {
1765 char_u *e, *dig;
1766 char_u *s = p;
1767 int x;
1768
1769 e = vim_strchr(p, ':');
1770 if (e == NULL || e[1] == NUL)
1771 return FAIL;
1772
1773 p = vim_strchr(e, ',');
1774 if (p == NULL)
1775 p = e + STRLEN(e);
1776 dig = e + 1;
1777 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001778
1779 if (STRNCMP(s, "height:", 7) == 0)
1780 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001781 if (dig != p)
1782 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001783 if (wp != NULL)
1784 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001785 if (is_preview)
1786 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001787 wp->w_maxheight = x;
1788 }
1789 }
1790 else if (STRNCMP(s, "width:", 6) == 0)
1791 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001792 if (dig != p)
1793 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001794 if (wp != NULL)
1795 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001796 if (is_preview)
1797 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001798 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001799 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001800 }
1801 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001802 else if (STRNCMP(s, "highlight:", 10) == 0)
1803 {
1804 if (wp != NULL)
1805 {
1806 int c = *p;
1807
1808 *p = NUL;
1809 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1810 s + 10, OPT_FREE|OPT_LOCAL, 0);
1811 *p = c;
1812 }
1813 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001814 else if (STRNCMP(s, "border:", 7) == 0)
1815 {
1816 char_u *arg = s + 7;
1817 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1818 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1819 int i;
1820
1821 if (!on && !off)
1822 return FAIL;
1823 if (wp != NULL)
1824 {
1825 for (i = 0; i < 4; ++i)
1826 wp->w_popup_border[i] = on ? 1 : 0;
1827 if (off)
1828 // only show the X for close when there is a border
1829 wp->w_popup_close = POPCLOSE_NONE;
1830 }
1831 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001832 else if (STRNCMP(s, "align:", 6) == 0)
1833 {
1834 char_u *arg = s + 6;
1835 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1836 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1837
1838 if (!menu && !item)
1839 return FAIL;
1840 if (wp != NULL && menu)
1841 wp->w_popup_flags |= POPF_INFO_MENU;
1842 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001843 else
1844 return FAIL;
1845 }
1846 return OK;
1847}
1848
1849/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001850 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1851 * is not NULL.
1852 * Return FAIL if the parsing fails.
1853 */
1854 int
1855parse_previewpopup(win_T *wp)
1856{
1857 return parse_popup_option(wp, TRUE);
1858}
1859
1860/*
1861 * Parse the 'completepopup' option and apply the values to window "wp" if it
1862 * is not NULL.
1863 * Return FAIL if the parsing fails.
1864 */
1865 int
1866parse_completepopup(win_T *wp)
1867{
1868 return parse_popup_option(wp, FALSE);
1869}
1870
1871/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001872 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001873 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001874 */
1875 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001876popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001877{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001878 poppos_T ppt = POPPOS_NONE;
1879
1880 if (d != NULL)
1881 ppt = get_pos_entry(d, FALSE);
1882
Bram Moolenaar79648732019-07-18 21:43:07 +02001883 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001884 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001885 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001886 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1887 }
1888 else
1889 {
1890 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1891 if (wp->w_wantline == 0) // cursor in first line
1892 {
1893 wp->w_wantline = 2;
1894 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1895 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1896 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001897 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001898
Bram Moolenaar79648732019-07-18 21:43:07 +02001899 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001900 if (wp->w_wantcol > Columns - width)
1901 {
1902 wp->w_wantcol = Columns - width;
1903 if (wp->w_wantcol < 1)
1904 wp->w_wantcol = 1;
1905 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001906
Bram Moolenaar79648732019-07-18 21:43:07 +02001907 popup_adjust_position(wp);
1908}
1909
1910/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001911 * Set w_wantline and w_wantcol for the a given screen position.
1912 * Caller must take care of running into the window border.
1913 */
1914 void
1915popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1916{
1917 wp->w_wantline = row;
1918 wp->w_wantcol = col;
1919 popup_adjust_position(wp);
1920}
1921
1922/*
1923 * Add a border and lef&right padding.
1924 */
1925 static void
1926add_border_left_right_padding(win_T *wp)
1927{
1928 int i;
1929
1930 for (i = 0; i < 4; ++i)
1931 {
1932 wp->w_popup_border[i] = 1;
1933 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1934 }
1935}
1936
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001937#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001938/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001939 * Return TRUE if there is any popup window with a terminal buffer.
1940 */
1941 static int
1942popup_terminal_exists(void)
1943{
1944 win_T *wp;
1945 tabpage_T *tp;
1946
1947 FOR_ALL_POPUPWINS(wp)
1948 if (wp->w_buffer->b_term != NULL)
1949 return TRUE;
1950 FOR_ALL_TABPAGES(tp)
1951 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1952 if (wp->w_buffer->b_term != NULL)
1953 return TRUE;
1954 return FALSE;
1955}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001956#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001957
1958/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001959 * Mark all popup windows in the current tab and global for redrawing.
1960 */
1961 void
1962popup_redraw_all(void)
1963{
1964 win_T *wp;
1965
1966 FOR_ALL_POPUPWINS(wp)
1967 wp->w_redr_type = UPD_NOT_VALID;
1968 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1969 wp->w_redr_type = UPD_NOT_VALID;
1970}
1971
1972/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001973 * Set the color for a notification window.
1974 */
1975 static void
1976popup_update_color(win_T *wp, create_type_T type)
1977{
1978 char *hiname = type == TYPE_MESSAGE_WIN
1979 ? "MessageWindow" : "PopupNotification";
1980 int nr = syn_name2id((char_u *)hiname);
1981
1982 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1983 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1984 OPT_FREE|OPT_LOCAL, 0);
1985}
1986
1987/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001988 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001989 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001990 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001991 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001992 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001993 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001994popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001995{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001996 win_T *wp;
1997 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001999 int new_buffer;
2000 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002002 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002003
Bram Moolenaar79648732019-07-18 21:43:07 +02002004 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002005 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002006 if (in_vim9script()
2007 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2008 || check_for_dict_arg(argvars, 1) == FAIL))
2009 return NULL;
2010
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002011 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002012 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002013 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002014 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002015 if (buf == NULL)
2016 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002017 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002018 return NULL;
2019 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002020#ifdef FEAT_TERMINAL
2021 if (buf->b_term != NULL && popup_terminal_exists())
2022 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002023 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002024 return NULL;
2025 }
2026#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002027 }
2028 else if (!(argvars[0].v_type == VAR_STRING
2029 && argvars[0].vval.v_string != NULL)
2030 && !(argvars[0].v_type == VAR_LIST
2031 && argvars[0].vval.v_list != NULL))
2032 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002033 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002034 return NULL;
2035 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002036 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002037 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002038 d = argvars[1].vval.v_dict;
2039 }
2040
2041 if (d != NULL)
2042 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002043 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002044 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002045 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002046 tabnr = -1; // notifications are global by default
2047 else
2048 tabnr = 0;
2049 if (tabnr > 0)
2050 {
2051 tp = find_tabpage(tabnr);
2052 if (tp == NULL)
2053 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002054 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002055 return NULL;
2056 }
2057 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002058 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002059 else if (popup_is_notification(type))
2060 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002061
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002062 // Create the window and buffer.
2063 wp = win_alloc_popup_win();
2064 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002065 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002066 if (rettv != NULL)
2067 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002068 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002069 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002070
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002071 if (buf != NULL)
2072 {
2073 // use existing buffer
2074 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002075 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002076 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002077 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002078 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002079 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002080 }
2081 else
2082 {
2083 // create a new buffer associated with the popup
2084 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002085 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002086 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002087 {
2088 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002089 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002090 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002091 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002092
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002093 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002094
Bram Moolenaar46451042019-08-24 15:50:46 +02002095 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002096 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002097 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002098 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002099 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002100 buf->b_p_ul = -1; // no undo
2101 buf->b_p_swf = FALSE; // no swap file
2102 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002103 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002104
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002105 // Avoid that 'buftype' is reset when this buffer is entered.
2106 buf->b_p_initialized = TRUE;
2107 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002108 wp->w_p_wrap = TRUE; // 'wrap' is default on
2109 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002110
Bram Moolenaara3fce622019-06-20 02:31:49 +02002111 if (tp != NULL)
2112 {
2113 // popup on specified tab page
2114 wp->w_next = tp->tp_first_popupwin;
2115 tp->tp_first_popupwin = wp;
2116 }
2117 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002118 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002119 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002120 wp->w_next = curtab->tp_first_popupwin;
2121 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002122 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002123 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002124 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002125 win_T *prev = first_popupwin;
2126
2127 // Global popup: add at the end, so that it gets displayed on top of
2128 // older ones with the same zindex. Matters for notifications.
2129 if (first_popupwin == NULL)
2130 first_popupwin = wp;
2131 else
2132 {
2133 while (prev->w_next != NULL)
2134 prev = prev->w_next;
2135 prev->w_next = wp;
2136 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002137 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002138
Bram Moolenaar79648732019-07-18 21:43:07 +02002139 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002140 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002141
Bram Moolenaar79648732019-07-18 21:43:07 +02002142 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002143 {
2144 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002145 }
2146 if (type == TYPE_ATCURSOR)
2147 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002148 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002149 set_moved_values(wp);
2150 set_moved_columns(wp, FIND_STRING);
2151 }
2152
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002153 if (type == TYPE_BEVAL)
2154 {
2155 wp->w_popup_pos = POPPOS_BOTLEFT;
2156
2157 // by default use the mouse position
2158 wp->w_wantline = mouse_row;
2159 if (wp->w_wantline <= 0) // mouse on first line
2160 {
2161 wp->w_wantline = 2;
2162 wp->w_popup_pos = POPPOS_TOPLEFT;
2163 }
2164 wp->w_wantcol = mouse_col + 1;
2165 set_mousemoved_values(wp);
2166 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2167 }
2168
Bram Moolenaar33796b32019-06-08 16:01:13 +02002169 // set default values
2170 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002171 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002172
Bram Moolenaar9198de32022-08-27 21:30:03 +01002173 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002174 {
2175 win_T *twp, *nextwin;
2176 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002177
2178 // Try to not overlap with another global popup. Guess we need 3
2179 // more screen lines than buffer lines.
2180 wp->w_wantline = 1;
2181 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2182 {
2183 nextwin = twp->w_next;
2184 if (twp != wp
2185 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2186 && twp->w_winrow <= wp->w_wantline - 1 + height
2187 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2188 {
2189 // move to below this popup and restart the loop to check for
2190 // overlap with other popups
2191 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2192 nextwin = first_popupwin;
2193 }
2194 }
2195 if (wp->w_wantline + height > Rows)
2196 {
2197 // can't avoid overlap, put on top in the hope that message goes
2198 // away soon.
2199 wp->w_wantline = 1;
2200 }
2201
2202 wp->w_wantcol = 10;
2203 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002204 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002205 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002206 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002207 for (i = 0; i < 4; ++i)
2208 wp->w_popup_border[i] = 1;
2209 wp->w_popup_padding[1] = 1;
2210 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002211
Bram Moolenaar9198de32022-08-27 21:30:03 +01002212 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002213 }
2214
Bram Moolenaara730e552019-06-16 19:05:31 +02002215 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002216 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002217 wp->w_popup_pos = POPPOS_CENTER;
2218 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002219 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002220 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002221 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002222 }
2223
Bram Moolenaara730e552019-06-16 19:05:31 +02002224 if (type == TYPE_MENU)
2225 {
2226 typval_T tv;
2227 callback_T callback;
2228
2229 tv.v_type = VAR_STRING;
2230 tv.vval.v_string = (char_u *)"popup_filter_menu";
2231 callback = get_callback(&tv);
2232 if (callback.cb_name != NULL)
2233 set_callback(&wp->w_filter_cb, &callback);
2234
2235 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002236 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002237 }
2238
Bram Moolenaar79648732019-07-18 21:43:07 +02002239 if (type == TYPE_PREVIEW)
2240 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002241 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002242 wp->w_popup_close = POPCLOSE_BUTTON;
2243 for (i = 0; i < 4; ++i)
2244 wp->w_popup_border[i] = 1;
2245 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002246 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002247 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002248# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002249 if (type == TYPE_INFO)
2250 {
2251 wp->w_popup_pos = POPPOS_TOPLEFT;
2252 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2253 wp->w_popup_close = POPCLOSE_BUTTON;
2254 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002255 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002256 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002257# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002258
Bram Moolenaarae943152019-06-16 22:54:14 +02002259 for (i = 0; i < 4; ++i)
2260 VIM_CLEAR(wp->w_border_highlight[i]);
2261 for (i = 0; i < 8; ++i)
2262 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002263 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002264 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002265 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002266
Bram Moolenaar79648732019-07-18 21:43:07 +02002267 if (d != NULL)
2268 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002269 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002270
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002271#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002272 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2273 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002274#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002275
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002276 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002277
2278 wp->w_vsep_width = 0;
2279
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002280 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002281 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002282
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002283#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002284 // When running a terminal in the popup it becomes the current window.
2285 if (buf->b_term != NULL)
2286 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002287#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002288
Bram Moolenaara730e552019-06-16 19:05:31 +02002289 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002290}
2291
2292/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002293 * popup_clear()
2294 */
2295 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002296f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002297{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002298 int force = FALSE;
2299
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002300 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2301 return;
2302
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002303 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002304 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002305 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002306}
2307
2308/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002309 * popup_create({text}, {options})
2310 */
2311 void
2312f_popup_create(typval_T *argvars, typval_T *rettv)
2313{
Bram Moolenaar17627312019-06-02 19:53:44 +02002314 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002315}
2316
2317/*
2318 * popup_atcursor({text}, {options})
2319 */
2320 void
2321f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2322{
Bram Moolenaar17627312019-06-02 19:53:44 +02002323 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002324}
2325
2326/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002327 * popup_beval({text}, {options})
2328 */
2329 void
2330f_popup_beval(typval_T *argvars, typval_T *rettv)
2331{
2332 popup_create(argvars, rettv, TYPE_BEVAL);
2333}
2334
2335/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002336 * Invoke the close callback for window "wp" with value "result".
2337 * Careful: The callback may make "wp" invalid!
2338 */
2339 static void
2340invoke_popup_callback(win_T *wp, typval_T *result)
2341{
2342 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002343 typval_T argv[3];
2344
2345 argv[0].v_type = VAR_NUMBER;
2346 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2347
2348 if (result != NULL && result->v_type != VAR_UNKNOWN)
2349 copy_tv(result, &argv[1]);
2350 else
2351 {
2352 argv[1].v_type = VAR_NUMBER;
2353 argv[1].vval.v_number = 0;
2354 }
2355
2356 argv[2].v_type = VAR_UNKNOWN;
2357
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002358 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002359 if (result != NULL)
2360 clear_tv(&argv[1]);
2361 clear_tv(&rettv);
2362}
2363
2364/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002365 * Make "prevwin" the current window, unless it's equal to "wp".
2366 * Otherwise make "firstwin" the current window.
2367 */
2368 static void
2369back_to_prevwin(win_T *wp)
2370{
2371 if (win_valid(prevwin) && wp != prevwin)
2372 win_enter(prevwin, FALSE);
2373 else
2374 win_enter(firstwin, FALSE);
2375}
2376
2377/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002378 * Close popup "wp" and invoke any close callback for it.
2379 */
2380 static void
2381popup_close_and_callback(win_T *wp, typval_T *arg)
2382{
2383 int id = wp->w_id;
2384
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002385#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002386 if (wp == curwin && curbuf->b_term != NULL)
2387 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002388 win_T *owp;
2389
2390 // Closing popup window with a terminal: put focus back on the first
2391 // that works:
2392 // - another popup window with a terminal
2393 // - the previous window
2394 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002395 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002396 if (owp != curwin && owp->w_buffer->b_term != NULL)
2397 break;
2398 if (owp != NULL)
2399 win_enter(owp, FALSE);
2400 else
2401 {
2402 for (owp = curtab->tp_first_popupwin; owp != NULL;
2403 owp = owp->w_next)
2404 if (owp != curwin && owp->w_buffer->b_term != NULL)
2405 break;
2406 if (owp != NULL)
2407 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002408 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002409 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002410 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002411 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002412#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002413
Bram Moolenaar49540192019-12-11 19:34:54 +01002414 // Just in case a check higher up is missing.
2415 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002416 {
2417 // To avoid getting stuck when win_execute() does something that causes
2418 // an error, stop calling the filter callback.
2419 free_callback(&wp->w_filter_cb);
2420
Bram Moolenaar49540192019-12-11 19:34:54 +01002421 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002422 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002423
Bram Moolenaarcee52202020-03-11 14:19:58 +01002424 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002425 if (wp->w_close_cb.cb_name != NULL)
2426 // Careful: This may make "wp" invalid.
2427 invoke_popup_callback(wp, arg);
2428
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002429 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002430 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002431}
2432
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002433 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002434popup_close_with_retval(win_T *wp, int retval)
2435{
2436 typval_T res;
2437
2438 res.v_type = VAR_NUMBER;
2439 res.vval.v_number = retval;
2440 popup_close_and_callback(wp, &res);
2441}
2442
Bram Moolenaar3397f742019-06-02 18:40:06 +02002443/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002444 * Close popup "wp" because of a mouse click.
2445 */
2446 void
2447popup_close_for_mouse_click(win_T *wp)
2448{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002449 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002450}
2451
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002452 static void
2453check_mouse_moved(win_T *wp, win_T *mouse_wp)
2454{
2455 // Close the popup when all if these are true:
2456 // - the mouse is not on this popup
2457 // - "mousemoved" was used
2458 // - the mouse is no longer on the same screen row or the mouse column is
2459 // outside of the relevant text
2460 if (wp != mouse_wp
2461 && wp->w_popup_mouse_row != 0
2462 && (wp->w_popup_mouse_row != mouse_row
2463 || mouse_col < wp->w_popup_mouse_mincol
2464 || mouse_col > wp->w_popup_mouse_maxcol))
2465 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002466 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002467 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002468 }
2469}
2470
2471/*
2472 * Called when the mouse moved: may close a popup with "mousemoved".
2473 */
2474 void
2475popup_handle_mouse_moved(void)
2476{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002477 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002478 win_T *mouse_wp;
2479 int row = mouse_row;
2480 int col = mouse_col;
2481
2482 // find the window where the mouse is in
2483 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2484
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002485 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2486 {
2487 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002488 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002489 }
2490 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2491 {
2492 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002493 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002494 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002495}
2496
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002497/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002498 * In a filter: check if the typed key is a mouse event that is used for
2499 * dragging the popup.
2500 */
2501 static void
2502filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2503{
2504 int row = mouse_row;
2505 int col = mouse_col;
2506
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002507 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002508 && is_mouse_key(c)
2509 && (wp == popup_dragwin
2510 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2511 // do not consume the key, allow for dragging the popup
2512 rettv->vval.v_number = 0;
2513}
2514
Bram Moolenaara730e552019-06-16 19:05:31 +02002515/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002516 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002517 */
2518 void
2519f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2520{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002521 int id;
2522 win_T *wp;
2523 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002524 typval_T res;
2525 int c;
2526 linenr_T old_lnum;
2527
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002528 if (in_vim9script()
2529 && (check_for_number_arg(argvars, 0) == FAIL
2530 || check_for_string_arg(argvars, 1) == FAIL))
2531 return;
2532
2533 id = tv_get_number(&argvars[0]);
2534 wp = win_id2wp(id);
2535 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002536 // If the popup has been closed do not consume the key.
2537 if (wp == NULL)
2538 return;
2539
2540 c = *key;
2541 if (c == K_SPECIAL && key[1] != NUL)
2542 c = TO_SPECIAL(key[1], key[2]);
2543
2544 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002545 rettv->v_type = VAR_BOOL;
2546 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002547 res.v_type = VAR_NUMBER;
2548
2549 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002550 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2551 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002552 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002553 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002554 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2555 ++wp->w_cursor.lnum;
2556 if (old_lnum != wp->w_cursor.lnum)
2557 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002558 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002559 return;
2560 }
2561
2562 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2563 {
2564 // Cancelled, invoke callback with -1
2565 res.vval.v_number = -1;
2566 popup_close_and_callback(wp, &res);
2567 return;
2568 }
2569 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2570 {
2571 // Invoke callback with current index.
2572 res.vval.v_number = wp->w_cursor.lnum;
2573 popup_close_and_callback(wp, &res);
2574 return;
2575 }
2576
2577 filter_handle_drag(wp, c, rettv);
2578}
2579
2580/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002581 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002582 */
2583 void
2584f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2585{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002586 int id;
2587 win_T *wp;
2588 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002589 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002590 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002591
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002592 if (in_vim9script()
2593 && (check_for_number_arg(argvars, 0) == FAIL
2594 || check_for_string_arg(argvars, 1) == FAIL))
2595 return;
2596
2597 id = tv_get_number(&argvars[0]);
2598 wp = win_id2wp(id);
2599 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002600 // If the popup has been closed don't consume the key.
2601 if (wp == NULL)
2602 return;
2603
Bram Moolenaara730e552019-06-16 19:05:31 +02002604 c = *key;
2605 if (c == K_SPECIAL && key[1] != NUL)
2606 c = TO_SPECIAL(key[1], key[2]);
2607
Bram Moolenaara42d9452019-06-15 21:46:30 +02002608 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002609 rettv->v_type = VAR_BOOL;
2610 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002611
Bram Moolenaara730e552019-06-16 19:05:31 +02002612 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002613 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002614 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002615 res.vval.v_number = 0;
2616 else
2617 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002618 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002619 return;
2620 }
2621
2622 // Invoke callback
2623 res.v_type = VAR_NUMBER;
2624 popup_close_and_callback(wp, &res);
2625}
2626
2627/*
2628 * popup_dialog({text}, {options})
2629 */
2630 void
2631f_popup_dialog(typval_T *argvars, typval_T *rettv)
2632{
2633 popup_create(argvars, rettv, TYPE_DIALOG);
2634}
2635
2636/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002637 * popup_menu({text}, {options})
2638 */
2639 void
2640f_popup_menu(typval_T *argvars, typval_T *rettv)
2641{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002642 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002643}
2644
2645/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002646 * popup_notification({text}, {options})
2647 */
2648 void
2649f_popup_notification(typval_T *argvars, typval_T *rettv)
2650{
2651 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2652}
2653
2654/*
2655 * Find the popup window with window-ID "id".
2656 * If the popup window does not exist NULL is returned.
2657 * If the window is not a popup window, and error message is given.
2658 */
2659 static win_T *
2660find_popup_win(int id)
2661{
2662 win_T *wp = win_id2wp(id);
2663
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002664 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002665 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002666 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002667 return NULL;
2668 }
2669 return wp;
2670}
2671
2672/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002673 * popup_close({id})
2674 */
2675 void
2676f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2677{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002678 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002679 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002680
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002681 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2682 return;
2683
2684 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002685 if (
2686# ifdef FEAT_TERMINAL
2687 // if the popup contains a terminal it will become hidden
2688 curbuf->b_term == NULL &&
2689# endif
2690 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002691 return;
2692
2693 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002694 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002695 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002696}
2697
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002698 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002699popup_hide(win_T *wp)
2700{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002701#ifdef FEAT_TERMINAL
2702 if (error_if_term_popup_window())
2703 return;
2704#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002705 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2706 {
2707 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002708 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002709 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002710 popup_mask_refresh = TRUE;
2711 }
2712}
2713
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002714/*
2715 * popup_hide({id})
2716 */
2717 void
2718f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2719{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002720 int id;
2721 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002722
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002723 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2724 return;
2725
2726 id = (int)tv_get_number(argvars);
2727 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002728 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002729 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002730 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002731 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2732 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002733}
2734
2735 void
2736popup_show(win_T *wp)
2737{
2738 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002739 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002740 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002741 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002742 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002743 }
2744}
2745
2746/*
2747 * popup_show({id})
2748 */
2749 void
2750f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2751{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002752 int id;
2753 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002754
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002755 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2756 return;
2757
2758 id = (int)tv_get_number(argvars);
2759 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002760 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002761 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002762 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002763 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002764#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002765 if (wp->w_popup_flags & POPF_INFO)
2766 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002767#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002768 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002769}
2770
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002771/*
2772 * popup_settext({id}, {text})
2773 */
2774 void
2775f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2776{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002777 int id;
2778 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002779
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002780 if (in_vim9script()
2781 && (check_for_number_arg(argvars, 0) == FAIL
2782 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2783 return;
2784
2785 id = (int)tv_get_number(&argvars[0]);
2786 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002787 if (wp != NULL)
2788 {
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002789 if (check_for_string_or_list_arg(argvars, 1) != FAIL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002790 {
2791 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002792 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002793 popup_adjust_position(wp);
2794 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002795 }
2796}
2797
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002798 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002799popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002800{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002801 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002802 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002803 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002804 clear_cmdline = TRUE;
2805 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002806
Bram Moolenaar43568642022-08-28 13:02:45 +01002807#ifdef HAS_MESSAGE_WINDOW
2808 if (wp == message_win)
2809 message_win = NULL;
2810#endif
2811
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002812 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002813 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002814}
2815
Bram Moolenaar82106932020-03-13 14:34:38 +01002816 static void
2817error_for_popup_window(void)
2818{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002819 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002820}
2821
2822 int
2823error_if_popup_window(int also_with_term UNUSED)
2824{
2825 // win_execute() may set "curwin" to a popup window temporarily, but many
2826 // commands are disallowed then. When a terminal runs in the popup most
2827 // things are allowed. When a terminal is finished it can be closed.
2828 if (WIN_IS_POPUP(curwin)
2829# ifdef FEAT_TERMINAL
2830 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002831# endif
2832 )
2833 {
2834 error_for_popup_window();
2835 return TRUE;
2836 }
2837 return FALSE;
2838}
2839
Bram Moolenaarec583842019-05-26 14:11:23 +02002840/*
2841 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002842 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002843 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002844 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002845 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002846popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002847{
2848 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002849 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002850 win_T *prev = NULL;
2851
Bram Moolenaarec583842019-05-26 14:11:23 +02002852 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002853 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002854 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002855 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002856 if (wp == curwin)
2857 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002858 if (!force)
2859 {
2860 error_for_popup_window();
2861 return FAIL;
2862 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002863 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002864 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002865 if (prev == NULL)
2866 first_popupwin = wp->w_next;
2867 else
2868 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002869 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002870 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002871 }
2872
Bram Moolenaarec583842019-05-26 14:11:23 +02002873 // go through tab-local popups
2874 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002875 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002876 return OK;
2877 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002878}
2879
2880/*
2881 * Close a popup window with Window-id "id" in tabpage "tp".
2882 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002883 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002884popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002885{
2886 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002887 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002888 win_T *prev = NULL;
2889
Bram Moolenaarec583842019-05-26 14:11:23 +02002890 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2891 if (wp->w_id == id)
2892 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002893 if (wp == curwin)
2894 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002895 if (!force)
2896 {
2897 error_for_popup_window();
2898 return FAIL;
2899 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002900 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002901 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002902 if (prev == NULL)
2903 *root = wp->w_next;
2904 else
2905 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002906 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002907 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002908 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002909 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002910}
2911
2912 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002913close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002914{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002915 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002916 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002917 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002918 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002919 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002920 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002921 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002922 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002923}
2924
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002925/*
2926 * popup_move({id}, {options})
2927 */
2928 void
2929f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2930{
Bram Moolenaarae943152019-06-16 22:54:14 +02002931 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002932 int id;
2933 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002934
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002935 if (in_vim9script()
2936 && (check_for_number_arg(argvars, 0) == FAIL
2937 || check_for_dict_arg(argvars, 1) == FAIL))
2938 return;
2939
2940 id = (int)tv_get_number(argvars);
2941 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002942 if (wp == NULL)
2943 return; // invalid {id}
2944
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002945 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002946 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002947 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002948
Bram Moolenaarae943152019-06-16 22:54:14 +02002949 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002950
2951 if (wp->w_winrow + wp->w_height >= cmdline_row)
2952 clear_cmdline = TRUE;
2953 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002954}
2955
Bram Moolenaarbc133542019-05-29 20:26:46 +02002956/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002957 * popup_setoptions({id}, {options})
2958 */
2959 void
2960f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2961{
2962 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002963 int id;
2964 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002965 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002966
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002967 if (in_vim9script()
2968 && (check_for_number_arg(argvars, 0) == FAIL
2969 || check_for_dict_arg(argvars, 1) == FAIL))
2970 return;
2971
2972 id = (int)tv_get_number(argvars);
2973 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002974 if (wp == NULL)
2975 return; // invalid {id}
2976
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002977 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002978 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002979 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002980 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002981
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002982 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002983
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002984 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002985 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002986 popup_adjust_position(wp);
2987}
2988
2989/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002990 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002991 */
2992 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002993f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002994{
2995 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002996 int id;
2997 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002998 int top_extra;
2999 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02003000
3001 if (rettv_dict_alloc(rettv) == OK)
3002 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003003 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3004 return;
3005
3006 id = (int)tv_get_number(argvars);
3007 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02003008 if (wp == NULL)
3009 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003010 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003011 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
3012
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003013 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02003014 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003015 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003016
Bram Moolenaarbc133542019-05-29 20:26:46 +02003017 dict_add_number(dict, "line", wp->w_winrow + 1);
3018 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003019 dict_add_number(dict, "width", wp->w_width + left_extra
3020 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3021 dict_add_number(dict, "height", wp->w_height + top_extra
3022 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003023
3024 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3025 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3026 dict_add_number(dict, "core_width", wp->w_width);
3027 dict_add_number(dict, "core_height", wp->w_height);
3028
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003029 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003030 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003031 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003032 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003033 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003034
3035 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003036 }
3037}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003038
3039/*
3040 * popup_list()
3041 */
3042 void
3043f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3044{
3045 win_T *wp;
3046 tabpage_T *tp;
3047
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003048 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003049 return;
3050 FOR_ALL_POPUPWINS(wp)
3051 list_append_number(rettv->vval.v_list, wp->w_id);
3052 FOR_ALL_TABPAGES(tp)
3053 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3054 list_append_number(rettv->vval.v_list, wp->w_id);
3055}
3056
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003057/*
3058 * popup_locate({row}, {col})
3059 */
3060 void
3061f_popup_locate(typval_T *argvars, typval_T *rettv)
3062{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003063 int row;
3064 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003065 win_T *wp;
3066
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003067 if (in_vim9script()
3068 && (check_for_number_arg(argvars, 0) == FAIL
3069 || check_for_number_arg(argvars, 1) == FAIL))
3070 return;
3071
3072 row = tv_get_number(&argvars[0]) - 1;
3073 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003074 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003075 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003076 rettv->vval.v_number = wp->w_id;
3077}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003078
3079/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003080 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3081 */
3082 static void
3083get_padding_border(dict_T *dict, int *array, char *name)
3084{
3085 list_T *list;
3086 int i;
3087
3088 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3089 return;
3090
3091 list = list_alloc();
3092 if (list != NULL)
3093 {
3094 dict_add_list(dict, name, list);
3095 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3096 for (i = 0; i < 4; ++i)
3097 list_append_number(list, array[i]);
3098 }
3099}
3100
3101/*
3102 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3103 */
3104 static void
3105get_borderhighlight(dict_T *dict, win_T *wp)
3106{
3107 list_T *list;
3108 int i;
3109
3110 for (i = 0; i < 4; ++i)
3111 if (wp->w_border_highlight[i] != NULL)
3112 break;
3113 if (i == 4)
3114 return;
3115
3116 list = list_alloc();
3117 if (list != NULL)
3118 {
3119 dict_add_list(dict, "borderhighlight", list);
3120 for (i = 0; i < 4; ++i)
3121 list_append_string(list, wp->w_border_highlight[i], -1);
3122 }
3123}
3124
3125/*
3126 * For popup_getoptions(): add a "borderchars" entry to "dict".
3127 */
3128 static void
3129get_borderchars(dict_T *dict, win_T *wp)
3130{
3131 list_T *list;
3132 int i;
3133 char_u buf[NUMBUFLEN];
3134 int len;
3135
3136 for (i = 0; i < 8; ++i)
3137 if (wp->w_border_char[i] != 0)
3138 break;
3139 if (i == 8)
3140 return;
3141
3142 list = list_alloc();
3143 if (list != NULL)
3144 {
3145 dict_add_list(dict, "borderchars", list);
3146 for (i = 0; i < 8; ++i)
3147 {
3148 len = mb_char2bytes(wp->w_border_char[i], buf);
3149 list_append_string(list, buf, len);
3150 }
3151 }
3152}
3153
3154/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003155 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003156 */
3157 static void
3158get_moved_list(dict_T *dict, win_T *wp)
3159{
3160 list_T *list;
3161
3162 list = list_alloc();
3163 if (list != NULL)
3164 {
3165 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003166 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003167 list_append_number(list, wp->w_popup_mincol);
3168 list_append_number(list, wp->w_popup_maxcol);
3169 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003170 list = list_alloc();
3171 if (list != NULL)
3172 {
3173 dict_add_list(dict, "mousemoved", list);
3174 list_append_number(list, wp->w_popup_mouse_row);
3175 list_append_number(list, wp->w_popup_mouse_mincol);
3176 list_append_number(list, wp->w_popup_mouse_maxcol);
3177 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003178}
3179
3180/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003181 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003182 */
3183 void
3184f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3185{
3186 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003187 int id;
3188 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003189 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003190 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003191
3192 if (rettv_dict_alloc(rettv) == OK)
3193 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003194 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3195 return;
3196
3197 id = (int)tv_get_number(argvars);
3198 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003199 if (wp == NULL)
3200 return;
3201
3202 dict = rettv->vval.v_dict;
3203 dict_add_number(dict, "line", wp->w_wantline);
3204 dict_add_number(dict, "col", wp->w_wantcol);
3205 dict_add_number(dict, "minwidth", wp->w_minwidth);
3206 dict_add_number(dict, "minheight", wp->w_minheight);
3207 dict_add_number(dict, "maxheight", wp->w_maxheight);
3208 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003209 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003210 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003211 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003212 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003213 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003214 {
3215 proptype_T *pt = text_prop_type_by_id(
3216 wp->w_popup_prop_win->w_buffer,
3217 wp->w_popup_prop_type);
3218
3219 if (pt != NULL)
3220 dict_add_string(dict, "textprop", pt->pt_name);
3221 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3222 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3223 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003224 dict_add_string(dict, "title", wp->w_popup_title);
3225 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003226 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003227 dict_add_number(dict, "dragall",
3228 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003229 dict_add_number(dict, "mapping",
3230 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003231 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003232 dict_add_number(dict, "posinvert",
3233 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003234 dict_add_number(dict, "cursorline",
3235 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003236 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003237 if (wp->w_scrollbar_highlight != NULL)
3238 dict_add_string(dict, "scrollbarhighlight",
3239 wp->w_scrollbar_highlight);
3240 if (wp->w_thumb_highlight != NULL)
3241 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003242
Bram Moolenaara3fce622019-06-20 02:31:49 +02003243 // find the tabpage that holds this popup
3244 i = 1;
3245 FOR_ALL_TABPAGES(tp)
3246 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003247 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003248
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003249 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3250 if (twp->w_id == id)
3251 break;
3252 if (twp != NULL)
3253 break;
3254 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003255 }
3256 if (tp == NULL)
3257 i = -1; // must be global
3258 else if (tp == curtab)
3259 i = 0;
3260 dict_add_number(dict, "tabpage", i);
3261
Bram Moolenaarae943152019-06-16 22:54:14 +02003262 get_padding_border(dict, wp->w_popup_padding, "padding");
3263 get_padding_border(dict, wp->w_popup_border, "border");
3264 get_borderhighlight(dict, wp);
3265 get_borderchars(dict, wp);
3266 get_moved_list(dict, wp);
3267
3268 if (wp->w_filter_cb.cb_name != NULL)
3269 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3270 if (wp->w_close_cb.cb_name != NULL)
3271 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003272
K.Takataeeec2542021-06-02 13:28:16 +02003273 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003274 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3275 {
3276 dict_add_string(dict, "pos",
3277 (char_u *)poppos_entries[i].pp_name);
3278 break;
3279 }
3280
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003281 dict_add_string(dict, "close", (char_u *)(
3282 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3283 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3284
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003285# if defined(FEAT_TIMERS)
3286 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3287 ? (long)wp->w_popup_timer->tr_interval : 0L);
3288# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003289 }
3290}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003291
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003292# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003293/*
3294 * Return TRUE if the current window is running a terminal in a popup window.
3295 * Return FALSE when the job has ended.
3296 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003297 int
3298error_if_term_popup_window()
3299{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003300 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3301 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003302 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003303 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003304 return TRUE;
3305 }
3306 return FALSE;
3307}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003308# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003309
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003310/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003311 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003312 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003313 * Each calling function should use a different flag, see the list at
3314 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003315 */
3316 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003317popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003318{
3319 win_T *wp;
3320
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003321 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003322 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003323 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003324 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003325}
3326
3327/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003328 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003329 * Must have called popup_reset_handled() first.
3330 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3331 * popup with the highest zindex.
3332 */
3333 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003334find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003335{
3336 win_T *wp;
3337 win_T *found_wp;
3338 int found_zindex;
3339
3340 found_zindex = lowest ? INT_MAX : 0;
3341 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003342 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003343 if ((wp->w_popup_handled & handled_flag) == 0
3344 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003345 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003346 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003347 {
3348 found_zindex = wp->w_zindex;
3349 found_wp = wp;
3350 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003351 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003352 if ((wp->w_popup_handled & handled_flag) == 0
3353 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003354 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003355 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003356 {
3357 found_zindex = wp->w_zindex;
3358 found_wp = wp;
3359 }
3360
3361 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003362 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003363 return found_wp;
3364}
3365
3366/*
3367 * Invoke the filter callback for window "wp" with typed character "c".
3368 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003369 * Returns the return value of the filter or -1 for CTRL-C in the current
3370 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003371 * Careful: The filter may make "wp" invalid!
3372 */
3373 static int
3374invoke_popup_filter(win_T *wp, int c)
3375{
3376 int res;
3377 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003378 typval_T argv[3];
3379 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003380 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003381 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003382
Bram Moolenaara42d9452019-06-15 21:46:30 +02003383 // Emergency exit: CTRL-C closes the popup.
3384 if (c == Ctrl_C)
3385 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003386 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003387 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003388
3389 // Reset got_int to avoid the callback isn't called.
3390 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003391 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003392 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003393
3394 // If the popup is the current window it probably fails to close. Then
3395 // do not consume the key.
3396 if (was_curwin && wp == curwin)
3397 return -1;
3398 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003399 }
3400
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003401 argv[0].v_type = VAR_NUMBER;
3402 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3403
3404 // Convert the number to a string, so that the function can use:
3405 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003406 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003407 argv[1].v_type = VAR_STRING;
3408 argv[1].vval.v_string = vim_strsave(buf);
3409
3410 argv[2].v_type = VAR_UNKNOWN;
3411
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003412 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003413 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3414 {
3415 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003416 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003417 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003418 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003419 }
3420 else
3421 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003422 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3423 popup_highlight_curline(wp);
3424
Bram Moolenaar371806e2020-10-22 13:44:54 +02003425 // If an error message was given always return FALSE, so that keys are
3426 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003427 // If we get three errors in a row then close the popup. Decrement the
3428 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3429 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003430 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003431 {
3432 wp->w_filter_errors += 10;
3433 if (wp->w_filter_errors >= 30)
3434 popup_close_with_retval(wp, -1);
3435 res = FALSE;
3436 }
3437 else
3438 {
3439 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3440 --wp->w_filter_errors;
3441 res = tv_get_bool(&rettv);
3442 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003443 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003444
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003445 vim_free(argv[1].vval.v_string);
3446 clear_tv(&rettv);
3447 return res;
3448}
3449
3450/*
3451 * Called when "c" was typed: invoke popup filter callbacks.
3452 * Returns TRUE when the character was consumed,
3453 */
3454 int
3455popup_do_filter(int c)
3456{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003457 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003458 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003459 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003460 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003461 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003462 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003463
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003464#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003465 // Popup window with terminal always gets focus.
3466 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3467 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003468#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003469
Bram Moolenaar934470e2019-09-01 23:27:05 +02003470 if (recursive)
3471 return FALSE;
3472 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003473
Bram Moolenaarf6396232019-08-24 19:36:00 +02003474 if (c == K_LEFTMOUSE)
3475 {
3476 int row = mouse_row;
3477 int col = mouse_col;
3478
3479 wp = mouse_find_win(&row, &col, FIND_POPUP);
3480 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003481 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003482 }
3483
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003484 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003485 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003486 while (res == FALSE
3487 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003488 if (wp->w_filter_cb.cb_name != NULL
3489 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003490 res = invoke_popup_filter(wp, c);
3491
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003492 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003493 {
3494 int save_got_int = got_int;
3495
3496 // Reset got_int to avoid a function used in the statusline aborts.
3497 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003498 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003499 got_int |= save_got_int;
3500 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003501 recursive = FALSE;
3502 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003503
3504 // When interrupted return FALSE to avoid looping.
3505 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003506}
3507
Bram Moolenaar3397f742019-06-02 18:40:06 +02003508/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003509 * Return TRUE if there is a popup visible with a filter callback and the
3510 * "mapping" property off.
3511 */
3512 int
3513popup_no_mapping(void)
3514{
3515 int round;
3516 win_T *wp;
3517
3518 for (round = 1; round <= 2; ++round)
3519 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3520 wp != NULL; wp = wp->w_next)
3521 if (wp->w_filter_cb.cb_name != NULL
3522 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3523 return TRUE;
3524 return FALSE;
3525}
3526
3527/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003528 * Called when the cursor moved: check if any popup needs to be closed if the
3529 * cursor moved far enough.
3530 */
3531 void
3532popup_check_cursor_pos()
3533{
3534 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003535
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003536 popup_reset_handled(POPUP_HANDLED_3);
3537 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003538 if (wp->w_popup_curwin != NULL
3539 && (curwin != wp->w_popup_curwin
3540 || curwin->w_cursor.lnum != wp->w_popup_lnum
3541 || curwin->w_cursor.col < wp->w_popup_mincol
3542 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003543 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003544}
3545
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003546/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003547 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003548 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003549 static void
3550popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003551{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003552 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003553 char_u *cells;
3554 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003555
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003556 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3557 {
3558 vim_free(wp->w_popup_mask_cells);
3559 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003560 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003561 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003562 if (wp->w_popup_mask_cells != NULL
3563 && wp->w_popup_mask_height == height
3564 && wp->w_popup_mask_width == width)
3565 return; // cache is still valid
3566
3567 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003568 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003569 if (wp->w_popup_mask_cells == NULL)
3570 return;
3571 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003572
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003573 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003574 {
3575 int cols, cole;
3576 int lines, linee;
3577
3578 li = lio->li_tv.vval.v_list->lv_first;
3579 cols = tv_get_number(&li->li_tv);
3580 if (cols < 0)
3581 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003582 if (cols <= 0)
3583 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003584 li = li->li_next;
3585 cole = tv_get_number(&li->li_tv);
3586 if (cole < 0)
3587 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003588 if (cole > width)
3589 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003590 li = li->li_next;
3591 lines = tv_get_number(&li->li_tv);
3592 if (lines < 0)
3593 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003594 if (lines <= 0)
3595 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003596 li = li->li_next;
3597 linee = tv_get_number(&li->li_tv);
3598 if (linee < 0)
3599 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003600 if (linee > height)
3601 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003602
Bram Moolenaar4012d262020-12-29 11:57:46 +01003603 for (row = lines - 1; row < linee; ++row)
3604 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003605 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003606 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003607}
3608
3609/*
3610 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3611 * "col" and "line" are screen coordinates.
3612 */
3613 static int
3614popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3615{
3616 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3617 int line = screenline - wp->w_winrow;
3618
3619 return col >= 0 && col < width
3620 && line >= 0 && line < height
3621 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003622}
3623
3624/*
3625 * Set flags in popup_transparent[] for window "wp" to "val".
3626 */
3627 static void
3628update_popup_transparent(win_T *wp, int val)
3629{
3630 if (wp->w_popup_mask != NULL)
3631 {
3632 int width = popup_width(wp);
3633 int height = popup_height(wp);
3634 listitem_T *lio, *li;
3635 int cols, cole;
3636 int lines, linee;
3637 int col, line;
3638
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003639 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003640 {
3641 li = lio->li_tv.vval.v_list->lv_first;
3642 cols = tv_get_number(&li->li_tv);
3643 if (cols < 0)
3644 cols = width + cols + 1;
3645 li = li->li_next;
3646 cole = tv_get_number(&li->li_tv);
3647 if (cole < 0)
3648 cole = width + cole + 1;
3649 li = li->li_next;
3650 lines = tv_get_number(&li->li_tv);
3651 if (lines < 0)
3652 lines = height + lines + 1;
3653 li = li->li_next;
3654 linee = tv_get_number(&li->li_tv);
3655 if (linee < 0)
3656 linee = height + linee + 1;
3657
3658 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003659 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003660 if (cols < 0)
3661 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003662 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003663 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003664 if (lines < 0)
3665 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003666 for (line = lines; line < linee
3667 && line + wp->w_winrow < screen_Rows; ++line)
3668 for (col = cols; col < cole
3669 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003670 popup_transparent[(line + wp->w_winrow) * screen_Columns
3671 + col + wp->w_wincol] = val;
3672 }
3673 }
3674}
3675
3676/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003677 * Only called when popup window "wp" is hidden: If the window is positioned
3678 * next to a text property, and it is now visible, then unhide the popup.
3679 * We don't check if visible popups become hidden, that is done in
3680 * popup_adjust_position().
3681 * Return TRUE if the popup became unhidden.
3682 */
3683 static int
3684check_popup_unhidden(win_T *wp)
3685{
3686 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3687 {
3688 textprop_T prop;
3689 linenr_T lnum;
3690
Bram Moolenaar27724252022-05-08 15:00:04 +01003691 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3692 && find_visible_prop(wp->w_popup_prop_win,
3693 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003694 &prop, &lnum) == OK)
3695 {
3696 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003697 wp->w_popup_prop_topline = 0; // force repositioning
3698 return TRUE;
3699 }
3700 }
3701 return FALSE;
3702}
3703
3704/*
3705 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3706 * That is when the buffer in the popup was changed, or the popup is following
3707 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003708 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003709 */
3710 static int
3711popup_need_position_adjust(win_T *wp)
3712{
3713 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3714 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003715 if (win_valid(wp->w_popup_prop_win)
3716 && (wp->w_popup_prop_changedtick
3717 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3718 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3719 return TRUE;
3720
3721 // May need to adjust the width if the cursor moved.
3722 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003723}
3724
3725/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003726 * Update "popup_mask" if needed.
3727 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003728 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003729 * Also marks window lines for redrawing.
3730 */
3731 void
3732may_update_popup_mask(int type)
3733{
3734 win_T *wp;
3735 short *mask;
3736 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003737 int redraw_all_popups = FALSE;
3738 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003739
3740 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003741 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3742 // basic (such as the screen size) must have changed.
3743 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003744 {
3745 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003746 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003747 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003748
3749 // Check if any popup window buffer has changed and if any popup connected
3750 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003751 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003752 if (wp->w_popup_flags & POPF_HIDDEN)
3753 popup_mask_refresh |= check_popup_unhidden(wp);
3754 else if (popup_need_position_adjust(wp))
3755 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003756 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003757 if (wp->w_popup_flags & POPF_HIDDEN)
3758 popup_mask_refresh |= check_popup_unhidden(wp);
3759 else if (popup_need_position_adjust(wp))
3760 popup_mask_refresh = TRUE;
3761
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003762 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003763 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003764
3765 // Need to update the mask, something has changed.
3766 popup_mask_refresh = FALSE;
3767 popup_mask_tab = curtab;
3768 popup_visible = FALSE;
3769
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003770 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003771 // If redrawing only what is needed, update "popup_mask_next" and then
3772 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003773 redrawing_all_win = TRUE;
3774 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003775 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003776 redrawing_all_win = FALSE;
3777 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003778 mask = popup_mask;
3779 else
3780 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003781 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003782
3783 // Find the window with the lowest zindex that hasn't been handled yet,
3784 // so that the window with a higher zindex overwrites the value in
3785 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003786 popup_reset_handled(POPUP_HANDLED_4);
3787 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003788 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003789 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003790 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003791
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003792 popup_visible = TRUE;
3793
Bram Moolenaar12034e22019-08-25 22:25:02 +02003794 // Recompute the position if the text changed. It may make the popup
3795 // hidden if it's attach to a text property that is no longer visible.
3796 if (redraw_all_popups || popup_need_position_adjust(wp))
3797 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003798 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003799 if (wp->w_popup_flags & POPF_HIDDEN)
3800 continue;
3801 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003802
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003803 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003804 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003805 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003806 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003807 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003808 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003809 col < wp->w_wincol + width - wp->w_popup_leftoff
3810 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003811 if (wp->w_zindex < POPUPMENU_ZINDEX
3812 && pum_visible()
3813 && pum_under_menu(line, col, FALSE))
3814 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3815 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003816 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003817 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003818 }
3819
3820 // Only check which lines are to be updated if not already
3821 // updating all lines.
3822 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003823 {
3824 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3825 win_T *prev_wp = NULL;
3826
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003827 for (line = 0; line < screen_Rows; ++line)
3828 {
3829 int col_done = 0;
3830
3831 for (col = 0; col < screen_Columns; ++col)
3832 {
3833 int off = line * screen_Columns + col;
3834
3835 if (popup_mask[off] != popup_mask_next[off])
3836 {
3837 popup_mask[off] = popup_mask_next[off];
3838
3839 if (line >= cmdline_row)
3840 {
3841 // the command line needs to be cleared if text below
3842 // the popup is now visible.
3843 if (!msg_scrolled && popup_mask_next[off] == 0)
3844 clear_cmdline = TRUE;
3845 }
3846 else if (col >= col_done)
3847 {
3848 linenr_T lnum;
3849 int line_cp = line;
3850 int col_cp = col;
3851
3852 // The screen position "line" / "col" needs to be
3853 // redrawn. Figure out what window that is and update
3854 // w_redraw_top and w_redr_bot. Only needs to be done
3855 // once for each window line.
3856 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3857 if (wp != NULL)
3858 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003859#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003860 // A terminal window needs to be redrawn.
3861 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003862 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003863 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003864#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003865 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003866 if (wp != prev_wp)
3867 {
3868 vim_memset(plines_cache, 0,
3869 sizeof(int) * Rows);
3870 prev_wp = wp;
3871 }
3872
3873 if (line_cp >= wp->w_height)
3874 // In (or below) status line
3875 wp->w_redr_status = TRUE;
3876 else
3877 {
3878 // compute the position in the buffer line
3879 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003880 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003881 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003882 redrawWinline(wp, lnum);
3883 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003884 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003885
3886 // This line is going to be redrawn, no need to
3887 // check until the right side of the window.
3888 col_done = wp->w_wincol + wp->w_width - 1;
3889 }
3890 }
3891 }
3892 }
3893 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003894
3895 vim_free(plines_cache);
3896 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003897
3898 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003899}
3900
3901/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003902 * If the current window is a popup and something relevant changed, recompute
3903 * the position and size.
3904 */
3905 void
3906may_update_popup_position(void)
3907{
3908 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3909 popup_adjust_position(curwin);
3910}
3911
3912/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003913 * Return a string of "len" spaces in IObuff.
3914 */
3915 static char_u *
3916get_spaces(int len)
3917{
3918 vim_memset(IObuff, ' ', (size_t)len);
3919 IObuff[len] = NUL;
3920 return IObuff;
3921}
3922
3923/*
3924 * Update popup windows. They are drawn on top of normal windows.
3925 * "win_update" is called for each popup window, lowest zindex first.
3926 */
3927 void
3928update_popups(void (*win_update)(win_T *wp))
3929{
3930 win_T *wp;
3931 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003932 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003933 int total_width;
3934 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003935 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003936 int popup_attr;
3937 int border_attr[4];
3938 int border_char[8];
3939 char_u buf[MB_MAXBYTES];
3940 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003941 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003942 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003943 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003944 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003945 int sb_thumb_top = 0;
3946 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003947 int attr_scroll = 0;
3948 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003949
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003950 // hide the cursor until redrawing is done.
3951 cursor_off();
3952
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003953 // Find the window with the lowest zindex that hasn't been updated yet,
3954 // so that the window with a higher zindex is drawn later, thus goes on
3955 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003956 popup_reset_handled(POPUP_HANDLED_5);
3957 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003958 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003959 int title_len = 0;
3960 int title_wincol;
3961
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003962 // This drawing uses the zindex of the popup window, so that it's on
3963 // top of the text but doesn't draw when another popup with higher
3964 // zindex is on top of the character.
3965 screen_zindex = wp->w_zindex;
3966
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003967 // Set flags in popup_transparent[] for masked cells.
3968 update_popup_transparent(wp, 1);
3969
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003970 // adjust w_winrow and w_wincol for border and padding, since
3971 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003972 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003973 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3974 - wp->w_popup_leftoff;
3975 if (wp->w_wincol + left_extra < 0)
3976 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003977 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003978 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003979
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003980 // Draw the popup text, unless it's off screen.
3981 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003982 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003983 // May need to update the "cursorline" highlighting, which may also
3984 // change "topline"
3985 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3986 popup_highlight_curline(wp);
3987
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003988 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003989
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003990 // move the cursor into the visible lines, otherwise executing
3991 // commands with win_execute() may cause the text to jump.
3992 if (wp->w_cursor.lnum < wp->w_topline)
3993 wp->w_cursor.lnum = wp->w_topline;
3994 else if (wp->w_cursor.lnum >= wp->w_botline)
3995 wp->w_cursor.lnum = wp->w_botline - 1;
3996 }
3997
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003998 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003999 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004000
4001 // Add offset for border and padding if not done already.
4002 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4003 {
4004 wp->w_wcol += left_extra;
4005 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4006 }
4007 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004008 {
4009 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004010 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004011 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004012
Bram Moolenaareabddc42022-04-02 15:32:16 +01004013 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004014 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004015 popup_attr = get_wcr_attr(wp);
4016
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004017 if (wp->w_winrow + total_height > cmdline_row)
4018 wp->w_popup_flags |= POPF_ON_CMDLINE;
4019 else
4020 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4021
4022
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004023 // We can only use these line drawing characters when 'encoding' is
4024 // "utf-8" and 'ambiwidth' is "single".
4025 if (enc_utf8 && *p_ambw == 's')
4026 {
4027 border_char[0] = border_char[2] = 0x2550;
4028 border_char[1] = border_char[3] = 0x2551;
4029 border_char[4] = 0x2554;
4030 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004031 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4032 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004033 border_char[7] = 0x255a;
4034 }
4035 else
4036 {
4037 border_char[0] = border_char[2] = '-';
4038 border_char[1] = border_char[3] = '|';
4039 for (i = 4; i < 8; ++i)
4040 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004041 if (wp->w_popup_flags & POPF_RESIZE)
4042 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004043 }
4044 for (i = 0; i < 8; ++i)
4045 if (wp->w_border_char[i] != 0)
4046 border_char[i] = wp->w_border_char[i];
4047
4048 for (i = 0; i < 4; ++i)
4049 {
4050 border_attr[i] = popup_attr;
4051 if (wp->w_border_highlight[i] != NULL)
4052 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4053 }
4054
Bram Moolenaard91467f2020-11-21 12:42:09 +01004055 // Title goes on top of border or padding.
4056 title_wincol = wp->w_wincol + 1;
4057 if (wp->w_popup_title != NULL)
4058 {
rbtnnc6119412021-08-07 13:08:45 +02004059 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004060
Ralf Schandlbc869872021-05-28 14:12:14 +02004061 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004062 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004063 {
4064 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4065 char_u *title_text = alloc(title_byte_len + 1);
4066
4067 if (title_text != NULL)
4068 {
4069 trunc_string(wp->w_popup_title, title_text,
4070 total_width - 2, title_byte_len + 1);
4071 screen_puts(title_text, wp->w_winrow, title_wincol,
4072 wp->w_popup_border[0] > 0
4073 ? border_attr[0] : popup_attr);
4074 vim_free(title_text);
4075 }
4076
Bram Moolenaard91467f2020-11-21 12:42:09 +01004077 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004078 }
4079 else
4080 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4081 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004082 }
4083
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004084 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004085 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004086 if (wp->w_popup_border[0] > 0)
4087 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004088 // top border; do not draw over the title
4089 if (title_len > 0)
4090 {
4091 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4092 wincol < 0 ? 0 : wincol, title_wincol,
4093 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004094 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004095 border_char[0], border_attr[0]);
4096 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4097 title_wincol + title_len, wincol + total_width,
4098 border_char[0], border_char[0], border_attr[0]);
4099 }
4100 else
4101 {
4102 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4103 wincol < 0 ? 0 : wincol, wincol + total_width,
4104 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4105 ? border_char[4] : border_char[0],
4106 border_char[0], border_attr[0]);
4107 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004108 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004109 {
4110 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4111 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004112 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004113 }
4114 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004115 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4116 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004117
Bram Moolenaard529ba52019-07-02 23:13:53 +02004118 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4119 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004120 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004121 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004122 - wp->w_has_scrollbar;
4123 if (padcol < 0)
4124 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004125 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004126 padcol = 0;
4127 }
4128 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004129 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004130 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004131 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004132 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004133 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004134 // top padding and no border; do not draw over the title
4135 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004136 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004137 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004138 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004139 row += 1;
4140 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004141 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004142 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004143 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004144 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004145
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004146 // Compute scrollbar thumb position and size.
4147 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004148 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004149 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4150 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004151 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004152
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004153 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4154 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004155 if (wp->w_topline > 1 && sb_thumb_height == height)
4156 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004157 if (sb_thumb_height == 0)
4158 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004159 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004160 // it just fits, avoid divide by zero
4161 sb_thumb_top = 0;
4162 else
4163 sb_thumb_top = (wp->w_topline - 1
4164 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004165 * (wp->w_height - sb_thumb_height)
4166 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004167 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4168 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004169 last = total_height - top_off - wp->w_popup_border[2];
4170 if (sb_thumb_top >= last)
4171 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004172 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004173
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004174 if (wp->w_scrollbar_highlight != NULL)
4175 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4176 else
4177 attr_scroll = highlight_attr[HLF_PSB];
4178 if (wp->w_thumb_highlight != NULL)
4179 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4180 else
4181 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004182 }
4183
4184 for (i = wp->w_popup_border[0];
4185 i < total_height - wp->w_popup_border[2]; ++i)
4186 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004187 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004188 // left and right padding only needed next to the body
4189 int do_padding =
4190 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4191 && i < total_height - wp->w_popup_border[2]
4192 - wp->w_popup_padding[2];
4193
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004194 row = wp->w_winrow + i;
4195
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004196 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004197 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004198 {
4199 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004200 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004201 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004202 if (do_padding && wp->w_popup_padding[3] > 0)
4203 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004204 int col = wincol + wp->w_popup_border[3];
4205
Bram Moolenaard529ba52019-07-02 23:13:53 +02004206 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004207 pad_left = wp->w_popup_padding[3];
4208 if (col < 0)
4209 {
4210 pad_left += col;
4211 col = 0;
4212 }
4213 if (pad_left > 0)
4214 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4215 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004216 // scrollbar
4217 if (wp->w_has_scrollbar)
4218 {
4219 int line = i - top_off;
4220 int scroll_col = wp->w_wincol + total_width - 1
4221 - wp->w_popup_border[1];
4222
4223 if (line >= 0 && line < wp->w_height)
4224 screen_putchar(' ', row, scroll_col,
4225 line >= sb_thumb_top
4226 && line < sb_thumb_top + sb_thumb_height
4227 ? attr_thumb : attr_scroll);
4228 else
4229 screen_putchar(' ', row, scroll_col, popup_attr);
4230 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004231 // right border
4232 if (wp->w_popup_border[1] > 0)
4233 {
4234 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004235 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004236 }
4237 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004238 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004239 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004240 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004241 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4242 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004243 }
4244
4245 if (wp->w_popup_padding[2] > 0)
4246 {
4247 // bottom padding
4248 row = wp->w_winrow + wp->w_popup_border[0]
4249 + wp->w_popup_padding[0] + wp->w_height;
4250 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004251 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004252 }
4253
4254 if (wp->w_popup_border[2] > 0)
4255 {
4256 // bottom border
4257 row = wp->w_winrow + total_height - 1;
4258 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004259 wincol < 0 ? 0 : wincol,
4260 wincol + total_width,
4261 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004262 ? border_char[7] : border_char[2],
4263 border_char[2], border_attr[2]);
4264 if (wp->w_popup_border[1] > 0)
4265 {
4266 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004267 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004268 }
4269 }
4270
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004271 if (wp->w_popup_close == POPCLOSE_BUTTON)
4272 {
4273 // close button goes on top of anything at the top-right corner
4274 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004275 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004276 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4277 }
4278
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004279 update_popup_transparent(wp, 0);
4280
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004281 // Back to the normal zindex.
4282 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004283
4284#ifdef HAS_MESSAGE_WINDOW
4285 // if this was the message window popup may start the timer now
4286 may_start_message_win_timer(wp);
4287#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004288 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004289
4290#if defined(FEAT_SEARCH_EXTRA)
4291 // In case win_update() called start_search_hl().
4292 end_search_hl();
4293#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004294}
4295
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004296/*
4297 * Mark references in callbacks of one popup window.
4298 */
4299 static int
4300set_ref_in_one_popup(win_T *wp, int copyID)
4301{
4302 int abort = FALSE;
4303 typval_T tv;
4304
4305 if (wp->w_close_cb.cb_partial != NULL)
4306 {
4307 tv.v_type = VAR_PARTIAL;
4308 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4309 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4310 }
4311 if (wp->w_filter_cb.cb_partial != NULL)
4312 {
4313 tv.v_type = VAR_PARTIAL;
4314 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4315 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4316 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004317 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004318 return abort;
4319}
4320
4321/*
4322 * Set reference in callbacks of popup windows.
4323 */
4324 int
4325set_ref_in_popups(int copyID)
4326{
4327 int abort = FALSE;
4328 win_T *wp;
4329 tabpage_T *tp;
4330
4331 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4332 abort = abort || set_ref_in_one_popup(wp, copyID);
4333
4334 FOR_ALL_TABPAGES(tp)
4335 {
4336 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4337 abort = abort || set_ref_in_one_popup(wp, copyID);
4338 if (abort)
4339 break;
4340 }
4341 return abort;
4342}
Bram Moolenaar79648732019-07-18 21:43:07 +02004343
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004344 int
4345popup_is_popup(win_T *wp)
4346{
4347 return wp->w_popup_flags != 0;
4348}
4349
4350#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004351/*
4352 * Find an existing popup used as the preview window, in the current tab page.
4353 * Return NULL if not found.
4354 */
4355 win_T *
4356popup_find_preview_window(void)
4357{
4358 win_T *wp;
4359
4360 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004361 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004362 if (wp->w_p_pvw)
4363 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004364 return NULL;
4365}
4366
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004367/*
4368 * Find an existing popup used as the info window, in the current tab page.
4369 * Return NULL if not found.
4370 */
4371 win_T *
4372popup_find_info_window(void)
4373{
4374 win_T *wp;
4375
4376 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004377 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004378 if (wp->w_popup_flags & POPF_INFO)
4379 return wp;
4380 return NULL;
4381}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004382#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004383
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004384 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004385f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4386{
4387#ifdef HAS_MESSAGE_WINDOW
4388 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4389#else
4390 rettv->vval.v_number = 0;
4391#endif
4392}
4393
4394 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004395f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4396{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004397#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004398 win_T *wp = popup_find_info_window();
4399
4400 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004401#else
4402 rettv->vval.v_number = 0;
4403#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004404}
4405
4406 void
4407f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004408{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004409#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004410 win_T *wp = popup_find_preview_window();
4411
4412 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004413#else
4414 rettv->vval.v_number = 0;
4415#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004416}
4417
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004418#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004419/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004420 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004421 * NOTE: this makes the popup the current window, so that the file can be
4422 * edited. However, it must not remain to be the current window, the caller
4423 * must make sure of that.
4424 */
4425 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004426popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004427{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004428 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004429
4430 if (wp == NULL)
4431 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004432 if (info)
4433 wp->w_popup_flags |= POPF_INFO;
4434 else
4435 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004436
4437 // Set the width to a reasonable value, so that w_topline can be computed.
4438 if (wp->w_minwidth > 0)
4439 wp->w_width = wp->w_minwidth;
4440 else if (wp->w_maxwidth > 0)
4441 wp->w_width = wp->w_maxwidth;
4442 else
4443 wp->w_width = curwin->w_width;
4444
4445 // Will switch to another buffer soon, dummy one can be wiped.
4446 wp->w_buffer->b_locked = FALSE;
4447
4448 win_enter(wp, FALSE);
4449 return OK;
4450}
4451
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004452/*
4453 * Close any preview popup.
4454 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004455 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004456popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004457{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004458 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004459
4460 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004461 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004462}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004463
4464/*
4465 * Hide the info popup.
4466 */
4467 void
4468popup_hide_info(void)
4469{
4470 win_T *wp = popup_find_info_window();
4471
4472 if (wp != NULL)
4473 popup_hide(wp);
4474}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004475
4476/*
4477 * Close any info popup.
4478 */
4479 void
4480popup_close_info(void)
4481{
4482 win_T *wp = popup_find_info_window();
4483
4484 if (wp != NULL)
4485 popup_close_with_retval(wp, -1);
4486}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004487#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004488
Bram Moolenaar9198de32022-08-27 21:30:03 +01004489#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4490
Bram Moolenaar9198de32022-08-27 21:30:03 +01004491/*
4492 * Get the message window.
4493 * Returns NULL if something failed.
4494 */
4495 win_T *
4496popup_get_message_win(void)
4497{
4498 if (message_win == NULL)
4499 {
4500 int i;
4501
4502 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4503
4504 if (message_win == NULL)
4505 return NULL;
4506
4507 // use the full screen width
4508 message_win->w_width = Columns;
4509
4510 // position at bottom of screen
4511 message_win->w_popup_pos = POPPOS_BOTTOM;
4512 message_win->w_wantcol = 1;
4513 message_win->w_minwidth = 9999;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01004514 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004515
4516 // no padding, border at the top
4517 for (i = 0; i < 4; ++i)
4518 message_win->w_popup_padding[i] = 0;
4519 for (i = 1; i < 4; ++i)
4520 message_win->w_popup_border[i] = 0;
4521
4522 if (message_win->w_popup_timer != NULL)
4523 message_win->w_popup_timer->tr_keep = TRUE;
4524 }
4525 return message_win;
4526}
4527
4528/*
4529 * If the message window is not visible: show it
4530 * If the message window is visible: reset the timeout
4531 */
4532 void
4533popup_show_message_win(void)
4534{
4535 if (message_win != NULL)
4536 {
4537 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4538 {
4539 // the highlight may have changed.
4540 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4541 popup_show(message_win);
4542 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004543 start_message_win_timer = TRUE;
4544 }
4545}
4546
4547 static void
4548may_start_message_win_timer(win_T *wp)
4549{
4550 if (wp == message_win && start_message_win_timer)
4551 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004552 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004553 {
4554 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004555 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004556 message_win_time = 3000;
4557 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004558 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004559 }
4560}
4561
4562 int
4563popup_message_win_visible(void)
4564{
4565 return message_win != NULL
4566 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4567}
4568
4569/*
4570 * If the message window is visible: hide it.
4571 */
4572 void
4573popup_hide_message_win(void)
4574{
4575 if (message_win != NULL)
4576 popup_hide(message_win);
4577}
4578
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004579// Values saved in start_echowindow() and restored in end_echowindow()
4580static int save_msg_didout = FALSE;
4581static int save_msg_col = 0;
4582// Values saved in end_echowindow() and restored in start_echowindow()
4583static int ew_msg_didout = FALSE;
4584static int ew_msg_col = 0;
4585
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004586/*
4587 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004588 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004589 */
4590 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004591start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004592{
4593 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004594 save_msg_didout = msg_didout;
4595 save_msg_col = msg_col;
4596 msg_didout = ew_msg_didout;
4597 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004598 if (time_sec != 0)
4599 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004600}
4601
4602/*
4603 * Invoked after outputting a message for ":echowindow".
4604 */
4605 void
4606end_echowindow(void)
4607{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004608 in_echowindow = FALSE;
4609
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004610 if ((State & MODE_HITRETURN) == 0)
4611 // show the message window now
4612 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004613
4614 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004615 ew_msg_didout = TRUE;
4616 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4617 msg_didout = save_msg_didout;
4618 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004619}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004620#endif
4621
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004622/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004623 * Close any popup for a text property associated with "win".
4624 * Return TRUE if a popup was closed.
4625 */
4626 int
4627popup_win_closed(win_T *win)
4628{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004629 int round;
4630 win_T *wp;
4631 win_T *next;
4632 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004633
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004634 for (round = 1; round <= 2; ++round)
4635 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4636 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004637 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004638 next = wp->w_next;
4639 if (wp->w_popup_prop_win == win)
4640 {
4641 popup_close_with_retval(wp, -1);
4642 ret = TRUE;
4643 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004644 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004645 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004646}
4647
4648/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004649 * Set the title of the popup window to the file name.
4650 */
4651 void
4652popup_set_title(win_T *wp)
4653{
4654 if (wp->w_buffer->b_fname != NULL)
4655 {
4656 char_u dirname[MAXPATHL];
4657 size_t len;
4658
4659 mch_dirname(dirname, MAXPATHL);
4660 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4661
4662 vim_free(wp->w_popup_title);
4663 len = STRLEN(wp->w_buffer->b_fname) + 3;
4664 wp->w_popup_title = alloc((int)len);
4665 if (wp->w_popup_title != NULL)
4666 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4667 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004668 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004669 }
4670}
4671
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004672# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004673/*
4674 * If there is a preview window, update the title.
4675 * Used after changing directory.
4676 */
4677 void
4678popup_update_preview_title(void)
4679{
4680 win_T *wp = popup_find_preview_window();
4681
4682 if (wp != NULL)
4683 popup_set_title(wp);
4684}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004685# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004686
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004687#endif // FEAT_PROP_POPUP