blob: 9bc31746ec031ac99285f1d2064ed405bae67b06 [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 Moolenaarc9121f72022-10-14 20:09:04 +01001406 len = linetabsize(wp, lnum);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001407 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001408
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001409 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001410 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001411 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001412 {
1413 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001414 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001416 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001417 }
1418 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001419 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001420 && allow_adjust_left
1421 && (wp->w_popup_pos == POPPOS_TOPLEFT
1422 || wp->w_popup_pos == POPPOS_BOTLEFT))
1423 {
1424 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001425 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001426
Bram Moolenaar51c31312019-06-15 22:27:23 +02001427 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001428 {
1429 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001430
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001431 len -= truncate_shift;
1432 shift_by -= truncate_shift;
1433 }
1434
1435 wp->w_wincol -= shift_by;
1436 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001437 wp->w_width = maxwidth;
1438 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001439 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001440 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001441 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001442 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1443 wp->w_width = wp->w_maxwidth;
1444 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001445
1446 if (wp->w_firstline < 0)
1447 --lnum;
1448 else
1449 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001450
1451 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001452 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001453 && (wp->w_firstline >= 0
1454 ? lnum - wp->w_topline
1455 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001456 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001457 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001458 }
1459
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001460 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001461 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001462
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001463 wp->w_has_scrollbar = wp->w_want_scrollbar
1464 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001465#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001466 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1467 // Terminal window with running job never has a scrollbar, adjusts to
1468 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001469 wp->w_has_scrollbar = FALSE;
1470#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001471 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001472 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001473 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001474 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001475 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001476 // make space for the scrollbar if needed, when lines wrap and when
1477 // applying minwidth
1478 if (maxwidth + right_extra >= maxspace
1479 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1480 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001481 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001482
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001483 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1484 {
1485 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1486
1487 if (minwidth < title_len)
1488 minwidth = title_len;
1489 }
1490
1491 if (minwidth > 0 && wp->w_width < minwidth)
1492 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001493 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001494 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001495 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001496 // some columns cut off on the right
1497 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001498
1499 // If the window doesn't fit because 'minwidth' is set then the
1500 // scrollbar is at the far right of the screen, use the size without
1501 // the scrollbar.
1502 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1503 {
1504 int off = wp->w_width - maxwidth;
1505
1506 if (off > right_extra)
1507 extra_width -= right_extra;
1508 else
1509 extra_width -= off;
1510 wp->w_width = maxwidth_no_scrollbar;
1511 }
1512 else
1513 {
1514 wp->w_width = maxwidth;
1515
1516 // when adding a scrollbar below need to adjust the width
1517 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1518 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001519 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001520 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001521 {
1522 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1523 if (wp->w_wincol < 0)
1524 wp->w_wincol = 0;
1525 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001526 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1527 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1528 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001529 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001530
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001531 // Right aligned: move to the right if needed.
1532 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001533 if (leftoff >= 0)
1534 wp->w_wincol = leftoff;
1535 else if (wp->w_popup_fixed)
1536 {
1537 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001538 // columns when displaying the window, minus left border and
1539 // padding.
1540 if (-leftoff > left_extra)
1541 wp->w_leftcol = -leftoff - left_extra;
1542 wp->w_width -= wp->w_leftcol;
1543 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001544 if (wp->w_width < 0)
1545 wp->w_width = 0;
1546 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001547 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001548
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001549 if (wp->w_p_wrap || (!wp->w_popup_fixed
1550 && (wp->w_popup_pos == POPPOS_TOPLEFT
1551 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001552 {
1553 int want_col = 0;
1554
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001555 // try to show the right border and any scrollbar
1556 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001557 if (want_col > 0 && wp->w_wincol > 0
1558 && wp->w_wincol + want_col >= Columns)
1559 {
1560 wp->w_wincol = Columns - want_col;
1561 if (wp->w_wincol < 0)
1562 wp->w_wincol = 0;
1563 }
1564 }
1565
Bram Moolenaar8d241042019-06-12 23:40:01 +02001566 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1567 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001568 if (minheight > 0 && wp->w_height < minheight)
1569 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001570 if (maxheight > 0 && wp->w_height > maxheight)
1571 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001572 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001573 if (wp->w_height > Rows - wp->w_winrow)
1574 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001575
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001576 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001577 {
1578 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1579 if (wp->w_winrow < 0)
1580 wp->w_winrow = 0;
1581 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001582 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001583 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001584 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001585 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001586 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001587 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001588 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1589 {
1590 // Bottom aligned but does not fit, and less space on the other
1591 // side or "posinvert" is off: reduce height.
1592 wp->w_winrow = 0;
1593 wp->w_height = wantline - extra_height;
1594 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001595 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001596 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001597 // Not enough space and more space on the other side: make top
1598 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001599 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001600 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001601 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001602 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001603 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1604 || wp->w_popup_pos == POPPOS_TOPLEFT)
1605 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001606 if (wp != popup_dragwin
1607 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001608 && wantline * 2 > Rows
1609 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001610 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001611 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001612 // make bottom aligned and recompute the height
1613 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001614 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001615 if (wp->w_winrow < 0)
1616 {
1617 wp->w_height += wp->w_winrow;
1618 wp->w_winrow = 0;
1619 }
1620 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001621 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001622 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001623 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001624 adjust_height_for_top_aligned = TRUE;
1625 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001626 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001627
1628 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1629 && wp->w_winrow + wp->w_height + extra_height > Rows)
1630 {
1631 // Bottom of the popup goes below the last line, reduce the height and
1632 // add a scrollbar.
1633 wp->w_height = Rows - wp->w_winrow - extra_height;
1634#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001635 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001636#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001637 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001638 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001639 if (width_with_scrollbar > 0)
1640 wp->w_width = width_with_scrollbar;
1641 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001642 }
1643
1644 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001645 if (wp->w_winrow >= Rows)
1646 wp->w_winrow = Rows - 1;
1647 else if (wp->w_winrow < 0)
1648 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001649
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001650 if (wp->w_height != org_height)
1651 win_comp_scroll(wp);
1652
Bram Moolenaar17146962019-05-30 00:12:11 +02001653 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001654 if (win_valid(wp->w_popup_prop_win))
1655 {
1656 wp->w_popup_prop_changedtick =
1657 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1658 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1659 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001660
1661 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001662 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001663 if (org_winrow != wp->w_winrow
1664 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001665 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001666 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001667 || org_width != wp->w_width
1668 || org_height != wp->w_height)
1669 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001670 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001671 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1672 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001673 popup_mask_refresh = TRUE;
1674 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001675}
1676
Bram Moolenaar17627312019-06-02 19:53:44 +02001677typedef enum
1678{
1679 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001680 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001681 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001682 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001683 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001684 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001685 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001686 TYPE_PREVIEW, // preview window
1687 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001688} create_type_T;
1689
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001690/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001691 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1692 */
1693 static int
1694popup_is_notification(create_type_T type)
1695{
1696 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1697}
1698
1699/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001700 * Make "buf" empty and set the contents to "text".
1701 * Used by popup_create() and popup_settext().
1702 */
1703 static void
1704popup_set_buffer_text(buf_T *buf, typval_T text)
1705{
1706 int lnum;
1707
1708 // Clear the buffer, then replace the lines.
1709 curbuf = buf;
1710 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001711 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001712 curbuf = curwin->w_buffer;
1713
1714 // Add text to the buffer.
1715 if (text.v_type == VAR_STRING)
1716 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001717 char_u *s = text.vval.v_string;
1718
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001719 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001720 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001721 }
1722 else
1723 {
1724 list_T *l = text.vval.v_list;
1725
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001726 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001727 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001728 if (l->lv_first == &range_list_item)
1729 emsg(_(e_using_number_as_string));
1730 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001731 // list of strings
1732 add_popup_strings(buf, l);
1733 else
1734 // list of dictionaries
1735 add_popup_dicts(buf, l);
1736 }
1737 }
1738
1739 // delete the line that was in the empty buffer
1740 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001741 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001742 curbuf = curwin->w_buffer;
1743}
1744
1745/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001746 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1747 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001748 * Return FAIL if the parsing fails.
1749 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001750 static int
1751parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001752{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001753 char_u *p =
1754#ifdef FEAT_QUICKFIX
1755 !is_preview ? p_cpp :
1756#endif
1757 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001758
Bram Moolenaar258cef52019-08-21 17:29:29 +02001759 if (wp != NULL)
1760 wp->w_popup_flags &= ~POPF_INFO_MENU;
1761
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001762 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001763 {
1764 char_u *e, *dig;
1765 char_u *s = p;
1766 int x;
1767
1768 e = vim_strchr(p, ':');
1769 if (e == NULL || e[1] == NUL)
1770 return FAIL;
1771
1772 p = vim_strchr(e, ',');
1773 if (p == NULL)
1774 p = e + STRLEN(e);
1775 dig = e + 1;
1776 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001777
1778 if (STRNCMP(s, "height:", 7) == 0)
1779 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001780 if (dig != p)
1781 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001782 if (wp != NULL)
1783 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001784 if (is_preview)
1785 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001786 wp->w_maxheight = x;
1787 }
1788 }
1789 else if (STRNCMP(s, "width:", 6) == 0)
1790 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001791 if (dig != p)
1792 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001793 if (wp != NULL)
1794 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001795 if (is_preview)
1796 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001797 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001798 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001799 }
1800 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001801 else if (STRNCMP(s, "highlight:", 10) == 0)
1802 {
1803 if (wp != NULL)
1804 {
1805 int c = *p;
1806
1807 *p = NUL;
1808 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1809 s + 10, OPT_FREE|OPT_LOCAL, 0);
1810 *p = c;
1811 }
1812 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001813 else if (STRNCMP(s, "border:", 7) == 0)
1814 {
1815 char_u *arg = s + 7;
1816 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1817 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1818 int i;
1819
1820 if (!on && !off)
1821 return FAIL;
1822 if (wp != NULL)
1823 {
1824 for (i = 0; i < 4; ++i)
1825 wp->w_popup_border[i] = on ? 1 : 0;
1826 if (off)
1827 // only show the X for close when there is a border
1828 wp->w_popup_close = POPCLOSE_NONE;
1829 }
1830 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001831 else if (STRNCMP(s, "align:", 6) == 0)
1832 {
1833 char_u *arg = s + 6;
1834 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1835 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1836
1837 if (!menu && !item)
1838 return FAIL;
1839 if (wp != NULL && menu)
1840 wp->w_popup_flags |= POPF_INFO_MENU;
1841 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001842 else
1843 return FAIL;
1844 }
1845 return OK;
1846}
1847
1848/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001849 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1850 * is not NULL.
1851 * Return FAIL if the parsing fails.
1852 */
1853 int
1854parse_previewpopup(win_T *wp)
1855{
1856 return parse_popup_option(wp, TRUE);
1857}
1858
1859/*
1860 * Parse the 'completepopup' option and apply the values to window "wp" if it
1861 * is not NULL.
1862 * Return FAIL if the parsing fails.
1863 */
1864 int
1865parse_completepopup(win_T *wp)
1866{
1867 return parse_popup_option(wp, FALSE);
1868}
1869
1870/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001871 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001872 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 */
1874 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001875popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001876{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001877 poppos_T ppt = POPPOS_NONE;
1878
1879 if (d != NULL)
1880 ppt = get_pos_entry(d, FALSE);
1881
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001883 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001885 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1886 }
1887 else
1888 {
1889 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1890 if (wp->w_wantline == 0) // cursor in first line
1891 {
1892 wp->w_wantline = 2;
1893 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1894 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1895 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001896 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001897
Bram Moolenaar79648732019-07-18 21:43:07 +02001898 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001899 if (wp->w_wantcol > Columns - width)
1900 {
1901 wp->w_wantcol = Columns - width;
1902 if (wp->w_wantcol < 1)
1903 wp->w_wantcol = 1;
1904 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001905
Bram Moolenaar79648732019-07-18 21:43:07 +02001906 popup_adjust_position(wp);
1907}
1908
1909/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001910 * Set w_wantline and w_wantcol for the a given screen position.
1911 * Caller must take care of running into the window border.
1912 */
1913 void
1914popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1915{
1916 wp->w_wantline = row;
1917 wp->w_wantcol = col;
1918 popup_adjust_position(wp);
1919}
1920
1921/*
1922 * Add a border and lef&right padding.
1923 */
1924 static void
1925add_border_left_right_padding(win_T *wp)
1926{
1927 int i;
1928
1929 for (i = 0; i < 4; ++i)
1930 {
1931 wp->w_popup_border[i] = 1;
1932 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1933 }
1934}
1935
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001936#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001937/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001938 * Return TRUE if there is any popup window with a terminal buffer.
1939 */
1940 static int
1941popup_terminal_exists(void)
1942{
1943 win_T *wp;
1944 tabpage_T *tp;
1945
1946 FOR_ALL_POPUPWINS(wp)
1947 if (wp->w_buffer->b_term != NULL)
1948 return TRUE;
1949 FOR_ALL_TABPAGES(tp)
1950 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1951 if (wp->w_buffer->b_term != NULL)
1952 return TRUE;
1953 return FALSE;
1954}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001955#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001956
1957/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001958 * Mark all popup windows in the current tab and global for redrawing.
1959 */
1960 void
1961popup_redraw_all(void)
1962{
1963 win_T *wp;
1964
1965 FOR_ALL_POPUPWINS(wp)
1966 wp->w_redr_type = UPD_NOT_VALID;
1967 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1968 wp->w_redr_type = UPD_NOT_VALID;
1969}
1970
1971/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001972 * Set the color for a notification window.
1973 */
1974 static void
1975popup_update_color(win_T *wp, create_type_T type)
1976{
1977 char *hiname = type == TYPE_MESSAGE_WIN
1978 ? "MessageWindow" : "PopupNotification";
1979 int nr = syn_name2id((char_u *)hiname);
1980
1981 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1982 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1983 OPT_FREE|OPT_LOCAL, 0);
1984}
1985
1986/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001987 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001988 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001989 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001990 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001991 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001992 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001993popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001994{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001995 win_T *wp;
1996 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001997 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001998 int new_buffer;
1999 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002000 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002001 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002002
Bram Moolenaar79648732019-07-18 21:43:07 +02002003 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002004 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002005 if (in_vim9script()
2006 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2007 || check_for_dict_arg(argvars, 1) == FAIL))
2008 return NULL;
2009
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002010 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002011 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002012 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002013 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002014 if (buf == NULL)
2015 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002016 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002017 return NULL;
2018 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002019#ifdef FEAT_TERMINAL
2020 if (buf->b_term != NULL && popup_terminal_exists())
2021 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002022 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002023 return NULL;
2024 }
2025#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002026 }
2027 else if (!(argvars[0].v_type == VAR_STRING
2028 && argvars[0].vval.v_string != NULL)
2029 && !(argvars[0].v_type == VAR_LIST
2030 && argvars[0].vval.v_list != NULL))
2031 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002032 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002033 return NULL;
2034 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002035 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002036 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002037 d = argvars[1].vval.v_dict;
2038 }
2039
2040 if (d != NULL)
2041 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002042 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002043 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002044 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002045 tabnr = -1; // notifications are global by default
2046 else
2047 tabnr = 0;
2048 if (tabnr > 0)
2049 {
2050 tp = find_tabpage(tabnr);
2051 if (tp == NULL)
2052 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002053 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002054 return NULL;
2055 }
2056 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002057 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002058 else if (popup_is_notification(type))
2059 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002060
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002061 // Create the window and buffer.
2062 wp = win_alloc_popup_win();
2063 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002064 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002065 if (rettv != NULL)
2066 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002067 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002068 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002069
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002070 if (buf != NULL)
2071 {
2072 // use existing buffer
2073 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002074 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002075 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002076 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002078 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002079 }
2080 else
2081 {
2082 // create a new buffer associated with the popup
2083 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002084 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002085 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002086 {
2087 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002088 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002089 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002090 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002091
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002092 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002093
Bram Moolenaar46451042019-08-24 15:50:46 +02002094 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002095 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002096 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002097 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002098 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002099 buf->b_p_ul = -1; // no undo
2100 buf->b_p_swf = FALSE; // no swap file
2101 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002102 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002103
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104 // Avoid that 'buftype' is reset when this buffer is entered.
2105 buf->b_p_initialized = TRUE;
2106 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002107 wp->w_p_wrap = TRUE; // 'wrap' is default on
2108 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002109
Bram Moolenaara3fce622019-06-20 02:31:49 +02002110 if (tp != NULL)
2111 {
2112 // popup on specified tab page
2113 wp->w_next = tp->tp_first_popupwin;
2114 tp->tp_first_popupwin = wp;
2115 }
2116 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002117 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002118 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002119 wp->w_next = curtab->tp_first_popupwin;
2120 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002122 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002123 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002124 win_T *prev = first_popupwin;
2125
2126 // Global popup: add at the end, so that it gets displayed on top of
2127 // older ones with the same zindex. Matters for notifications.
2128 if (first_popupwin == NULL)
2129 first_popupwin = wp;
2130 else
2131 {
2132 while (prev->w_next != NULL)
2133 prev = prev->w_next;
2134 prev->w_next = wp;
2135 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002136 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002137
Bram Moolenaar79648732019-07-18 21:43:07 +02002138 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002139 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002140
Bram Moolenaar79648732019-07-18 21:43:07 +02002141 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002142 {
2143 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002144 }
2145 if (type == TYPE_ATCURSOR)
2146 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002147 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002148 set_moved_values(wp);
2149 set_moved_columns(wp, FIND_STRING);
2150 }
2151
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002152 if (type == TYPE_BEVAL)
2153 {
2154 wp->w_popup_pos = POPPOS_BOTLEFT;
2155
2156 // by default use the mouse position
2157 wp->w_wantline = mouse_row;
2158 if (wp->w_wantline <= 0) // mouse on first line
2159 {
2160 wp->w_wantline = 2;
2161 wp->w_popup_pos = POPPOS_TOPLEFT;
2162 }
2163 wp->w_wantcol = mouse_col + 1;
2164 set_mousemoved_values(wp);
2165 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2166 }
2167
Bram Moolenaar33796b32019-06-08 16:01:13 +02002168 // set default values
2169 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002170 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002171
Bram Moolenaar9198de32022-08-27 21:30:03 +01002172 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002173 {
2174 win_T *twp, *nextwin;
2175 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002176
2177 // Try to not overlap with another global popup. Guess we need 3
2178 // more screen lines than buffer lines.
2179 wp->w_wantline = 1;
2180 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2181 {
2182 nextwin = twp->w_next;
2183 if (twp != wp
2184 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2185 && twp->w_winrow <= wp->w_wantline - 1 + height
2186 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2187 {
2188 // move to below this popup and restart the loop to check for
2189 // overlap with other popups
2190 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2191 nextwin = first_popupwin;
2192 }
2193 }
2194 if (wp->w_wantline + height > Rows)
2195 {
2196 // can't avoid overlap, put on top in the hope that message goes
2197 // away soon.
2198 wp->w_wantline = 1;
2199 }
2200
2201 wp->w_wantcol = 10;
2202 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002203 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002204 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002205 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002206 for (i = 0; i < 4; ++i)
2207 wp->w_popup_border[i] = 1;
2208 wp->w_popup_padding[1] = 1;
2209 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002210
Bram Moolenaar9198de32022-08-27 21:30:03 +01002211 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002212 }
2213
Bram Moolenaara730e552019-06-16 19:05:31 +02002214 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002215 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002216 wp->w_popup_pos = POPPOS_CENTER;
2217 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002218 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002219 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002220 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002221 }
2222
Bram Moolenaara730e552019-06-16 19:05:31 +02002223 if (type == TYPE_MENU)
2224 {
2225 typval_T tv;
2226 callback_T callback;
2227
2228 tv.v_type = VAR_STRING;
2229 tv.vval.v_string = (char_u *)"popup_filter_menu";
2230 callback = get_callback(&tv);
2231 if (callback.cb_name != NULL)
2232 set_callback(&wp->w_filter_cb, &callback);
2233
2234 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002235 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002236 }
2237
Bram Moolenaar79648732019-07-18 21:43:07 +02002238 if (type == TYPE_PREVIEW)
2239 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002240 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002241 wp->w_popup_close = POPCLOSE_BUTTON;
2242 for (i = 0; i < 4; ++i)
2243 wp->w_popup_border[i] = 1;
2244 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002245 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002246 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002247# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002248 if (type == TYPE_INFO)
2249 {
2250 wp->w_popup_pos = POPPOS_TOPLEFT;
2251 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2252 wp->w_popup_close = POPCLOSE_BUTTON;
2253 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002254 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002255 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002256# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002257
Bram Moolenaarae943152019-06-16 22:54:14 +02002258 for (i = 0; i < 4; ++i)
2259 VIM_CLEAR(wp->w_border_highlight[i]);
2260 for (i = 0; i < 8; ++i)
2261 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002262 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002263 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002264 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002265
Bram Moolenaar79648732019-07-18 21:43:07 +02002266 if (d != NULL)
2267 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002268 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002269
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002270#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002271 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2272 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002273#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002274
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002275 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002276
2277 wp->w_vsep_width = 0;
2278
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002279 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002280 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002281
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002282#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002283 // When running a terminal in the popup it becomes the current window.
2284 if (buf->b_term != NULL)
2285 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002286#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002287
Bram Moolenaara730e552019-06-16 19:05:31 +02002288 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002289}
2290
2291/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002292 * popup_clear()
2293 */
2294 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002295f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002296{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002297 int force = FALSE;
2298
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002299 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2300 return;
2301
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002302 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002303 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002304 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002305}
2306
2307/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002308 * popup_create({text}, {options})
2309 */
2310 void
2311f_popup_create(typval_T *argvars, typval_T *rettv)
2312{
Bram Moolenaar17627312019-06-02 19:53:44 +02002313 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002314}
2315
2316/*
2317 * popup_atcursor({text}, {options})
2318 */
2319 void
2320f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2321{
Bram Moolenaar17627312019-06-02 19:53:44 +02002322 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002323}
2324
2325/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002326 * popup_beval({text}, {options})
2327 */
2328 void
2329f_popup_beval(typval_T *argvars, typval_T *rettv)
2330{
2331 popup_create(argvars, rettv, TYPE_BEVAL);
2332}
2333
2334/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002335 * Invoke the close callback for window "wp" with value "result".
2336 * Careful: The callback may make "wp" invalid!
2337 */
2338 static void
2339invoke_popup_callback(win_T *wp, typval_T *result)
2340{
2341 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002342 typval_T argv[3];
2343
2344 argv[0].v_type = VAR_NUMBER;
2345 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2346
2347 if (result != NULL && result->v_type != VAR_UNKNOWN)
2348 copy_tv(result, &argv[1]);
2349 else
2350 {
2351 argv[1].v_type = VAR_NUMBER;
2352 argv[1].vval.v_number = 0;
2353 }
2354
2355 argv[2].v_type = VAR_UNKNOWN;
2356
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002357 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002358 if (result != NULL)
2359 clear_tv(&argv[1]);
2360 clear_tv(&rettv);
2361}
2362
2363/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002364 * Make "prevwin" the current window, unless it's equal to "wp".
2365 * Otherwise make "firstwin" the current window.
2366 */
2367 static void
2368back_to_prevwin(win_T *wp)
2369{
2370 if (win_valid(prevwin) && wp != prevwin)
2371 win_enter(prevwin, FALSE);
2372 else
2373 win_enter(firstwin, FALSE);
2374}
2375
2376/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002377 * Close popup "wp" and invoke any close callback for it.
2378 */
2379 static void
2380popup_close_and_callback(win_T *wp, typval_T *arg)
2381{
2382 int id = wp->w_id;
2383
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002384#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002385 if (wp == curwin && curbuf->b_term != NULL)
2386 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002387 win_T *owp;
2388
2389 // Closing popup window with a terminal: put focus back on the first
2390 // that works:
2391 // - another popup window with a terminal
2392 // - the previous window
2393 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002394 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002395 if (owp != curwin && owp->w_buffer->b_term != NULL)
2396 break;
2397 if (owp != NULL)
2398 win_enter(owp, FALSE);
2399 else
2400 {
2401 for (owp = curtab->tp_first_popupwin; owp != NULL;
2402 owp = owp->w_next)
2403 if (owp != curwin && owp->w_buffer->b_term != NULL)
2404 break;
2405 if (owp != NULL)
2406 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002407 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002408 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002409 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002410 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002411#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002412
Bram Moolenaar49540192019-12-11 19:34:54 +01002413 // Just in case a check higher up is missing.
2414 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002415 {
2416 // To avoid getting stuck when win_execute() does something that causes
2417 // an error, stop calling the filter callback.
2418 free_callback(&wp->w_filter_cb);
2419
Bram Moolenaar49540192019-12-11 19:34:54 +01002420 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002421 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002422
Bram Moolenaarcee52202020-03-11 14:19:58 +01002423 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002424 if (wp->w_close_cb.cb_name != NULL)
2425 // Careful: This may make "wp" invalid.
2426 invoke_popup_callback(wp, arg);
2427
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002428 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002429 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002430}
2431
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002432 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002433popup_close_with_retval(win_T *wp, int retval)
2434{
2435 typval_T res;
2436
2437 res.v_type = VAR_NUMBER;
2438 res.vval.v_number = retval;
2439 popup_close_and_callback(wp, &res);
2440}
2441
Bram Moolenaar3397f742019-06-02 18:40:06 +02002442/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002443 * Close popup "wp" because of a mouse click.
2444 */
2445 void
2446popup_close_for_mouse_click(win_T *wp)
2447{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002448 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002449}
2450
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002451 static void
2452check_mouse_moved(win_T *wp, win_T *mouse_wp)
2453{
2454 // Close the popup when all if these are true:
2455 // - the mouse is not on this popup
2456 // - "mousemoved" was used
2457 // - the mouse is no longer on the same screen row or the mouse column is
2458 // outside of the relevant text
2459 if (wp != mouse_wp
2460 && wp->w_popup_mouse_row != 0
2461 && (wp->w_popup_mouse_row != mouse_row
2462 || mouse_col < wp->w_popup_mouse_mincol
2463 || mouse_col > wp->w_popup_mouse_maxcol))
2464 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002465 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002466 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002467 }
2468}
2469
2470/*
2471 * Called when the mouse moved: may close a popup with "mousemoved".
2472 */
2473 void
2474popup_handle_mouse_moved(void)
2475{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002476 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002477 win_T *mouse_wp;
2478 int row = mouse_row;
2479 int col = mouse_col;
2480
2481 // find the window where the mouse is in
2482 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2483
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002484 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2485 {
2486 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002487 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002488 }
2489 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2490 {
2491 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002492 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002493 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002494}
2495
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002496/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002497 * In a filter: check if the typed key is a mouse event that is used for
2498 * dragging the popup.
2499 */
2500 static void
2501filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2502{
2503 int row = mouse_row;
2504 int col = mouse_col;
2505
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002506 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002507 && is_mouse_key(c)
2508 && (wp == popup_dragwin
2509 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2510 // do not consume the key, allow for dragging the popup
2511 rettv->vval.v_number = 0;
2512}
2513
Bram Moolenaara730e552019-06-16 19:05:31 +02002514/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002515 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002516 */
2517 void
2518f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2519{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002520 int id;
2521 win_T *wp;
2522 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002523 typval_T res;
2524 int c;
2525 linenr_T old_lnum;
2526
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002527 if (in_vim9script()
2528 && (check_for_number_arg(argvars, 0) == FAIL
2529 || check_for_string_arg(argvars, 1) == FAIL))
2530 return;
2531
2532 id = tv_get_number(&argvars[0]);
2533 wp = win_id2wp(id);
2534 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002535 // If the popup has been closed do not consume the key.
2536 if (wp == NULL)
2537 return;
2538
2539 c = *key;
2540 if (c == K_SPECIAL && key[1] != NUL)
2541 c = TO_SPECIAL(key[1], key[2]);
2542
2543 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002544 rettv->v_type = VAR_BOOL;
2545 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002546 res.v_type = VAR_NUMBER;
2547
2548 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002549 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2550 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002551 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002552 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002553 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2554 ++wp->w_cursor.lnum;
2555 if (old_lnum != wp->w_cursor.lnum)
2556 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002557 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002558 return;
2559 }
2560
2561 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2562 {
2563 // Cancelled, invoke callback with -1
2564 res.vval.v_number = -1;
2565 popup_close_and_callback(wp, &res);
2566 return;
2567 }
2568 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2569 {
2570 // Invoke callback with current index.
2571 res.vval.v_number = wp->w_cursor.lnum;
2572 popup_close_and_callback(wp, &res);
2573 return;
2574 }
2575
2576 filter_handle_drag(wp, c, rettv);
2577}
2578
2579/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002580 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002581 */
2582 void
2583f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2584{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002585 int id;
2586 win_T *wp;
2587 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002588 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002589 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002590
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002591 if (in_vim9script()
2592 && (check_for_number_arg(argvars, 0) == FAIL
2593 || check_for_string_arg(argvars, 1) == FAIL))
2594 return;
2595
2596 id = tv_get_number(&argvars[0]);
2597 wp = win_id2wp(id);
2598 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002599 // If the popup has been closed don't consume the key.
2600 if (wp == NULL)
2601 return;
2602
Bram Moolenaara730e552019-06-16 19:05:31 +02002603 c = *key;
2604 if (c == K_SPECIAL && key[1] != NUL)
2605 c = TO_SPECIAL(key[1], key[2]);
2606
Bram Moolenaara42d9452019-06-15 21:46:30 +02002607 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002608 rettv->v_type = VAR_BOOL;
2609 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002610
Bram Moolenaara730e552019-06-16 19:05:31 +02002611 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002612 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002613 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002614 res.vval.v_number = 0;
2615 else
2616 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002617 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002618 return;
2619 }
2620
2621 // Invoke callback
2622 res.v_type = VAR_NUMBER;
2623 popup_close_and_callback(wp, &res);
2624}
2625
2626/*
2627 * popup_dialog({text}, {options})
2628 */
2629 void
2630f_popup_dialog(typval_T *argvars, typval_T *rettv)
2631{
2632 popup_create(argvars, rettv, TYPE_DIALOG);
2633}
2634
2635/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002636 * popup_menu({text}, {options})
2637 */
2638 void
2639f_popup_menu(typval_T *argvars, typval_T *rettv)
2640{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002641 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002642}
2643
2644/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002645 * popup_notification({text}, {options})
2646 */
2647 void
2648f_popup_notification(typval_T *argvars, typval_T *rettv)
2649{
2650 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2651}
2652
2653/*
2654 * Find the popup window with window-ID "id".
2655 * If the popup window does not exist NULL is returned.
2656 * If the window is not a popup window, and error message is given.
2657 */
2658 static win_T *
2659find_popup_win(int id)
2660{
2661 win_T *wp = win_id2wp(id);
2662
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002663 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002664 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002665 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002666 return NULL;
2667 }
2668 return wp;
2669}
2670
2671/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002672 * popup_close({id})
2673 */
2674 void
2675f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2676{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002677 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002678 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002679
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002680 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2681 return;
2682
2683 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002684 if (
2685# ifdef FEAT_TERMINAL
2686 // if the popup contains a terminal it will become hidden
2687 curbuf->b_term == NULL &&
2688# endif
2689 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002690 return;
2691
2692 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002693 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002694 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002695}
2696
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002697 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002698popup_hide(win_T *wp)
2699{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002700#ifdef FEAT_TERMINAL
2701 if (error_if_term_popup_window())
2702 return;
2703#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002704 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2705 {
2706 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002707 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002708 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002709 popup_mask_refresh = TRUE;
2710 }
2711}
2712
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002713/*
2714 * popup_hide({id})
2715 */
2716 void
2717f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2718{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002719 int id;
2720 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002721
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002722 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2723 return;
2724
2725 id = (int)tv_get_number(argvars);
2726 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002727 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002728 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002729 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002730 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2731 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002732}
2733
2734 void
2735popup_show(win_T *wp)
2736{
2737 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002738 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002739 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002740 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002741 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002742 }
2743}
2744
2745/*
2746 * popup_show({id})
2747 */
2748 void
2749f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2750{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002751 int id;
2752 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002753
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002754 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2755 return;
2756
2757 id = (int)tv_get_number(argvars);
2758 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002759 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002760 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002761 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002762 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002763#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002764 if (wp->w_popup_flags & POPF_INFO)
2765 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002766#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002767 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002768}
2769
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002770/*
2771 * popup_settext({id}, {text})
2772 */
2773 void
2774f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2775{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002776 int id;
2777 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002778
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002779 if (in_vim9script()
2780 && (check_for_number_arg(argvars, 0) == FAIL
2781 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2782 return;
2783
2784 id = (int)tv_get_number(&argvars[0]);
2785 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002786 if (wp != NULL)
2787 {
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002788 if (check_for_string_or_list_arg(argvars, 1) != FAIL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002789 {
2790 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002791 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002792 popup_adjust_position(wp);
2793 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002794 }
2795}
2796
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002797 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002798popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002799{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002800 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002801 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002802 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002803 clear_cmdline = TRUE;
2804 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002805
Bram Moolenaar43568642022-08-28 13:02:45 +01002806#ifdef HAS_MESSAGE_WINDOW
2807 if (wp == message_win)
2808 message_win = NULL;
2809#endif
2810
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002811 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002812 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002813}
2814
Bram Moolenaar82106932020-03-13 14:34:38 +01002815 static void
2816error_for_popup_window(void)
2817{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002818 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002819}
2820
2821 int
2822error_if_popup_window(int also_with_term UNUSED)
2823{
2824 // win_execute() may set "curwin" to a popup window temporarily, but many
2825 // commands are disallowed then. When a terminal runs in the popup most
2826 // things are allowed. When a terminal is finished it can be closed.
2827 if (WIN_IS_POPUP(curwin)
2828# ifdef FEAT_TERMINAL
2829 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002830# endif
2831 )
2832 {
2833 error_for_popup_window();
2834 return TRUE;
2835 }
2836 return FALSE;
2837}
2838
Bram Moolenaarec583842019-05-26 14:11:23 +02002839/*
2840 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002841 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002842 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002843 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002844 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002845popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002846{
2847 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002848 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002849 win_T *prev = NULL;
2850
Bram Moolenaarec583842019-05-26 14:11:23 +02002851 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002852 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002853 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002854 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002855 if (wp == curwin)
2856 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002857 if (!force)
2858 {
2859 error_for_popup_window();
2860 return FAIL;
2861 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002862 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002863 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002864 if (prev == NULL)
2865 first_popupwin = wp->w_next;
2866 else
2867 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002868 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002869 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002870 }
2871
Bram Moolenaarec583842019-05-26 14:11:23 +02002872 // go through tab-local popups
2873 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002874 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002875 return OK;
2876 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002877}
2878
2879/*
2880 * Close a popup window with Window-id "id" in tabpage "tp".
2881 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002882 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002883popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002884{
2885 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002886 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002887 win_T *prev = NULL;
2888
Bram Moolenaarec583842019-05-26 14:11:23 +02002889 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2890 if (wp->w_id == id)
2891 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002892 if (wp == curwin)
2893 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002894 if (!force)
2895 {
2896 error_for_popup_window();
2897 return FAIL;
2898 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002899 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002900 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002901 if (prev == NULL)
2902 *root = wp->w_next;
2903 else
2904 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002905 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002906 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002907 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002908 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002909}
2910
2911 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002912close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002913{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002914 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002915 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002916 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002917 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002918 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002919 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002920 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002921 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002922}
2923
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002924/*
2925 * popup_move({id}, {options})
2926 */
2927 void
2928f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2929{
Bram Moolenaarae943152019-06-16 22:54:14 +02002930 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002931 int id;
2932 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002933
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002934 if (in_vim9script()
2935 && (check_for_number_arg(argvars, 0) == FAIL
2936 || check_for_dict_arg(argvars, 1) == FAIL))
2937 return;
2938
2939 id = (int)tv_get_number(argvars);
2940 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002941 if (wp == NULL)
2942 return; // invalid {id}
2943
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002944 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002945 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002946 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002947
Bram Moolenaarae943152019-06-16 22:54:14 +02002948 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002949
2950 if (wp->w_winrow + wp->w_height >= cmdline_row)
2951 clear_cmdline = TRUE;
2952 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002953}
2954
Bram Moolenaarbc133542019-05-29 20:26:46 +02002955/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002956 * popup_setoptions({id}, {options})
2957 */
2958 void
2959f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2960{
2961 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002962 int id;
2963 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002964 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002965
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002966 if (in_vim9script()
2967 && (check_for_number_arg(argvars, 0) == FAIL
2968 || check_for_dict_arg(argvars, 1) == FAIL))
2969 return;
2970
2971 id = (int)tv_get_number(argvars);
2972 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002973 if (wp == NULL)
2974 return; // invalid {id}
2975
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002976 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002977 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002978 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002979 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002980
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002981 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002982
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002983 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002984 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002985 popup_adjust_position(wp);
2986}
2987
2988/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002989 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002990 */
2991 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002992f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002993{
2994 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002995 int id;
2996 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002997 int top_extra;
2998 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002999
3000 if (rettv_dict_alloc(rettv) == OK)
3001 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003002 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3003 return;
3004
3005 id = (int)tv_get_number(argvars);
3006 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02003007 if (wp == NULL)
3008 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003009 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003010 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
3011
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003012 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02003013 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003014 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003015
Bram Moolenaarbc133542019-05-29 20:26:46 +02003016 dict_add_number(dict, "line", wp->w_winrow + 1);
3017 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003018 dict_add_number(dict, "width", wp->w_width + left_extra
3019 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3020 dict_add_number(dict, "height", wp->w_height + top_extra
3021 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003022
3023 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3024 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3025 dict_add_number(dict, "core_width", wp->w_width);
3026 dict_add_number(dict, "core_height", wp->w_height);
3027
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003028 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003029 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003030 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003031 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003032 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003033
3034 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003035 }
3036}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003037
3038/*
3039 * popup_list()
3040 */
3041 void
3042f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3043{
3044 win_T *wp;
3045 tabpage_T *tp;
3046
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003047 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003048 return;
3049 FOR_ALL_POPUPWINS(wp)
3050 list_append_number(rettv->vval.v_list, wp->w_id);
3051 FOR_ALL_TABPAGES(tp)
3052 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3053 list_append_number(rettv->vval.v_list, wp->w_id);
3054}
3055
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003056/*
3057 * popup_locate({row}, {col})
3058 */
3059 void
3060f_popup_locate(typval_T *argvars, typval_T *rettv)
3061{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003062 int row;
3063 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003064 win_T *wp;
3065
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003066 if (in_vim9script()
3067 && (check_for_number_arg(argvars, 0) == FAIL
3068 || check_for_number_arg(argvars, 1) == FAIL))
3069 return;
3070
3071 row = tv_get_number(&argvars[0]) - 1;
3072 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003073 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003074 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003075 rettv->vval.v_number = wp->w_id;
3076}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003077
3078/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003079 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3080 */
3081 static void
3082get_padding_border(dict_T *dict, int *array, char *name)
3083{
3084 list_T *list;
3085 int i;
3086
3087 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3088 return;
3089
3090 list = list_alloc();
3091 if (list != NULL)
3092 {
3093 dict_add_list(dict, name, list);
3094 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3095 for (i = 0; i < 4; ++i)
3096 list_append_number(list, array[i]);
3097 }
3098}
3099
3100/*
3101 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3102 */
3103 static void
3104get_borderhighlight(dict_T *dict, win_T *wp)
3105{
3106 list_T *list;
3107 int i;
3108
3109 for (i = 0; i < 4; ++i)
3110 if (wp->w_border_highlight[i] != NULL)
3111 break;
3112 if (i == 4)
3113 return;
3114
3115 list = list_alloc();
3116 if (list != NULL)
3117 {
3118 dict_add_list(dict, "borderhighlight", list);
3119 for (i = 0; i < 4; ++i)
3120 list_append_string(list, wp->w_border_highlight[i], -1);
3121 }
3122}
3123
3124/*
3125 * For popup_getoptions(): add a "borderchars" entry to "dict".
3126 */
3127 static void
3128get_borderchars(dict_T *dict, win_T *wp)
3129{
3130 list_T *list;
3131 int i;
3132 char_u buf[NUMBUFLEN];
3133 int len;
3134
3135 for (i = 0; i < 8; ++i)
3136 if (wp->w_border_char[i] != 0)
3137 break;
3138 if (i == 8)
3139 return;
3140
3141 list = list_alloc();
3142 if (list != NULL)
3143 {
3144 dict_add_list(dict, "borderchars", list);
3145 for (i = 0; i < 8; ++i)
3146 {
3147 len = mb_char2bytes(wp->w_border_char[i], buf);
3148 list_append_string(list, buf, len);
3149 }
3150 }
3151}
3152
3153/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003154 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003155 */
3156 static void
3157get_moved_list(dict_T *dict, win_T *wp)
3158{
3159 list_T *list;
3160
3161 list = list_alloc();
3162 if (list != NULL)
3163 {
3164 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003165 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003166 list_append_number(list, wp->w_popup_mincol);
3167 list_append_number(list, wp->w_popup_maxcol);
3168 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003169 list = list_alloc();
3170 if (list != NULL)
3171 {
3172 dict_add_list(dict, "mousemoved", list);
3173 list_append_number(list, wp->w_popup_mouse_row);
3174 list_append_number(list, wp->w_popup_mouse_mincol);
3175 list_append_number(list, wp->w_popup_mouse_maxcol);
3176 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003177}
3178
3179/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003180 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003181 */
3182 void
3183f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3184{
3185 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003186 int id;
3187 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003188 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003189 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003190
3191 if (rettv_dict_alloc(rettv) == OK)
3192 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003193 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3194 return;
3195
3196 id = (int)tv_get_number(argvars);
3197 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003198 if (wp == NULL)
3199 return;
3200
3201 dict = rettv->vval.v_dict;
3202 dict_add_number(dict, "line", wp->w_wantline);
3203 dict_add_number(dict, "col", wp->w_wantcol);
3204 dict_add_number(dict, "minwidth", wp->w_minwidth);
3205 dict_add_number(dict, "minheight", wp->w_minheight);
3206 dict_add_number(dict, "maxheight", wp->w_maxheight);
3207 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003208 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003209 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003210 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003211 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003212 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003213 {
3214 proptype_T *pt = text_prop_type_by_id(
3215 wp->w_popup_prop_win->w_buffer,
3216 wp->w_popup_prop_type);
3217
3218 if (pt != NULL)
3219 dict_add_string(dict, "textprop", pt->pt_name);
3220 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3221 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3222 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003223 dict_add_string(dict, "title", wp->w_popup_title);
3224 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003225 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003226 dict_add_number(dict, "dragall",
3227 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003228 dict_add_number(dict, "mapping",
3229 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003230 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003231 dict_add_number(dict, "posinvert",
3232 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003233 dict_add_number(dict, "cursorline",
3234 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003235 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003236 if (wp->w_scrollbar_highlight != NULL)
3237 dict_add_string(dict, "scrollbarhighlight",
3238 wp->w_scrollbar_highlight);
3239 if (wp->w_thumb_highlight != NULL)
3240 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003241
Bram Moolenaara3fce622019-06-20 02:31:49 +02003242 // find the tabpage that holds this popup
3243 i = 1;
3244 FOR_ALL_TABPAGES(tp)
3245 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003246 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003247
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003248 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3249 if (twp->w_id == id)
3250 break;
3251 if (twp != NULL)
3252 break;
3253 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003254 }
3255 if (tp == NULL)
3256 i = -1; // must be global
3257 else if (tp == curtab)
3258 i = 0;
3259 dict_add_number(dict, "tabpage", i);
3260
Bram Moolenaarae943152019-06-16 22:54:14 +02003261 get_padding_border(dict, wp->w_popup_padding, "padding");
3262 get_padding_border(dict, wp->w_popup_border, "border");
3263 get_borderhighlight(dict, wp);
3264 get_borderchars(dict, wp);
3265 get_moved_list(dict, wp);
3266
3267 if (wp->w_filter_cb.cb_name != NULL)
3268 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3269 if (wp->w_close_cb.cb_name != NULL)
3270 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003271
K.Takataeeec2542021-06-02 13:28:16 +02003272 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003273 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3274 {
3275 dict_add_string(dict, "pos",
3276 (char_u *)poppos_entries[i].pp_name);
3277 break;
3278 }
3279
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003280 dict_add_string(dict, "close", (char_u *)(
3281 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3282 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3283
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003284# if defined(FEAT_TIMERS)
3285 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3286 ? (long)wp->w_popup_timer->tr_interval : 0L);
3287# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003288 }
3289}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003290
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003291# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003292/*
3293 * Return TRUE if the current window is running a terminal in a popup window.
3294 * Return FALSE when the job has ended.
3295 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003296 int
3297error_if_term_popup_window()
3298{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003299 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3300 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003301 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003302 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003303 return TRUE;
3304 }
3305 return FALSE;
3306}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003307# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003308
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003309/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003310 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003311 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003312 * Each calling function should use a different flag, see the list at
3313 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003314 */
3315 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003316popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003317{
3318 win_T *wp;
3319
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003320 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003321 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003322 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003323 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003324}
3325
3326/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003327 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003328 * Must have called popup_reset_handled() first.
3329 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3330 * popup with the highest zindex.
3331 */
3332 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003333find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003334{
3335 win_T *wp;
3336 win_T *found_wp;
3337 int found_zindex;
3338
3339 found_zindex = lowest ? INT_MAX : 0;
3340 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003341 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003342 if ((wp->w_popup_handled & handled_flag) == 0
3343 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003344 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003345 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003346 {
3347 found_zindex = wp->w_zindex;
3348 found_wp = wp;
3349 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003350 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003351 if ((wp->w_popup_handled & handled_flag) == 0
3352 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003353 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003354 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003355 {
3356 found_zindex = wp->w_zindex;
3357 found_wp = wp;
3358 }
3359
3360 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003361 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003362 return found_wp;
3363}
3364
3365/*
3366 * Invoke the filter callback for window "wp" with typed character "c".
3367 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003368 * Returns the return value of the filter or -1 for CTRL-C in the current
3369 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003370 * Careful: The filter may make "wp" invalid!
3371 */
3372 static int
3373invoke_popup_filter(win_T *wp, int c)
3374{
3375 int res;
3376 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003377 typval_T argv[3];
3378 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003379 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003380 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003381
Bram Moolenaara42d9452019-06-15 21:46:30 +02003382 // Emergency exit: CTRL-C closes the popup.
3383 if (c == Ctrl_C)
3384 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003385 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003386 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003387
3388 // Reset got_int to avoid the callback isn't called.
3389 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003390 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003391 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003392
3393 // If the popup is the current window it probably fails to close. Then
3394 // do not consume the key.
3395 if (was_curwin && wp == curwin)
3396 return -1;
3397 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003398 }
3399
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003400 argv[0].v_type = VAR_NUMBER;
3401 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3402
3403 // Convert the number to a string, so that the function can use:
3404 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003405 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003406 argv[1].v_type = VAR_STRING;
3407 argv[1].vval.v_string = vim_strsave(buf);
3408
3409 argv[2].v_type = VAR_UNKNOWN;
3410
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003411 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003412 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3413 {
3414 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003415 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003416 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003417 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003418 }
3419 else
3420 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003421 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3422 popup_highlight_curline(wp);
3423
Bram Moolenaar371806e2020-10-22 13:44:54 +02003424 // If an error message was given always return FALSE, so that keys are
3425 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003426 // If we get three errors in a row then close the popup. Decrement the
3427 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3428 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003429 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003430 {
3431 wp->w_filter_errors += 10;
3432 if (wp->w_filter_errors >= 30)
3433 popup_close_with_retval(wp, -1);
3434 res = FALSE;
3435 }
3436 else
3437 {
3438 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3439 --wp->w_filter_errors;
3440 res = tv_get_bool(&rettv);
3441 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003442 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003443
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003444 vim_free(argv[1].vval.v_string);
3445 clear_tv(&rettv);
3446 return res;
3447}
3448
3449/*
3450 * Called when "c" was typed: invoke popup filter callbacks.
3451 * Returns TRUE when the character was consumed,
3452 */
3453 int
3454popup_do_filter(int c)
3455{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003456 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003457 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003458 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003459 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003460 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003461 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003462
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003463#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003464 // Popup window with terminal always gets focus.
3465 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3466 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003467#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003468
Bram Moolenaar934470e2019-09-01 23:27:05 +02003469 if (recursive)
3470 return FALSE;
3471 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003472
Bram Moolenaarf6396232019-08-24 19:36:00 +02003473 if (c == K_LEFTMOUSE)
3474 {
3475 int row = mouse_row;
3476 int col = mouse_col;
3477
3478 wp = mouse_find_win(&row, &col, FIND_POPUP);
3479 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003480 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003481 }
3482
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003483 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003484 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003485 while (res == FALSE
3486 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003487 if (wp->w_filter_cb.cb_name != NULL
3488 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003489 res = invoke_popup_filter(wp, c);
3490
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003491 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003492 {
3493 int save_got_int = got_int;
3494
3495 // Reset got_int to avoid a function used in the statusline aborts.
3496 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003497 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003498 got_int |= save_got_int;
3499 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003500 recursive = FALSE;
3501 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003502
3503 // When interrupted return FALSE to avoid looping.
3504 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003505}
3506
Bram Moolenaar3397f742019-06-02 18:40:06 +02003507/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003508 * Return TRUE if there is a popup visible with a filter callback and the
3509 * "mapping" property off.
3510 */
3511 int
3512popup_no_mapping(void)
3513{
3514 int round;
3515 win_T *wp;
3516
3517 for (round = 1; round <= 2; ++round)
3518 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3519 wp != NULL; wp = wp->w_next)
3520 if (wp->w_filter_cb.cb_name != NULL
3521 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3522 return TRUE;
3523 return FALSE;
3524}
3525
3526/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003527 * Called when the cursor moved: check if any popup needs to be closed if the
3528 * cursor moved far enough.
3529 */
3530 void
3531popup_check_cursor_pos()
3532{
3533 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003534
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003535 popup_reset_handled(POPUP_HANDLED_3);
3536 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003537 if (wp->w_popup_curwin != NULL
3538 && (curwin != wp->w_popup_curwin
3539 || curwin->w_cursor.lnum != wp->w_popup_lnum
3540 || curwin->w_cursor.col < wp->w_popup_mincol
3541 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003542 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003543}
3544
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003545/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003546 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003547 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003548 static void
3549popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003550{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003551 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003552 char_u *cells;
3553 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003554
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003555 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3556 {
3557 vim_free(wp->w_popup_mask_cells);
3558 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003559 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003560 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003561 if (wp->w_popup_mask_cells != NULL
3562 && wp->w_popup_mask_height == height
3563 && wp->w_popup_mask_width == width)
3564 return; // cache is still valid
3565
3566 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003567 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003568 if (wp->w_popup_mask_cells == NULL)
3569 return;
3570 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003571
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003572 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003573 {
3574 int cols, cole;
3575 int lines, linee;
3576
3577 li = lio->li_tv.vval.v_list->lv_first;
3578 cols = tv_get_number(&li->li_tv);
3579 if (cols < 0)
3580 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003581 if (cols <= 0)
3582 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003583 li = li->li_next;
3584 cole = tv_get_number(&li->li_tv);
3585 if (cole < 0)
3586 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003587 if (cole > width)
3588 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003589 li = li->li_next;
3590 lines = tv_get_number(&li->li_tv);
3591 if (lines < 0)
3592 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003593 if (lines <= 0)
3594 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003595 li = li->li_next;
3596 linee = tv_get_number(&li->li_tv);
3597 if (linee < 0)
3598 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003599 if (linee > height)
3600 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003601
Bram Moolenaar4012d262020-12-29 11:57:46 +01003602 for (row = lines - 1; row < linee; ++row)
3603 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003604 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003605 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003606}
3607
3608/*
3609 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3610 * "col" and "line" are screen coordinates.
3611 */
3612 static int
3613popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3614{
3615 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3616 int line = screenline - wp->w_winrow;
3617
3618 return col >= 0 && col < width
3619 && line >= 0 && line < height
3620 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003621}
3622
3623/*
3624 * Set flags in popup_transparent[] for window "wp" to "val".
3625 */
3626 static void
3627update_popup_transparent(win_T *wp, int val)
3628{
3629 if (wp->w_popup_mask != NULL)
3630 {
3631 int width = popup_width(wp);
3632 int height = popup_height(wp);
3633 listitem_T *lio, *li;
3634 int cols, cole;
3635 int lines, linee;
3636 int col, line;
3637
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003638 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003639 {
3640 li = lio->li_tv.vval.v_list->lv_first;
3641 cols = tv_get_number(&li->li_tv);
3642 if (cols < 0)
3643 cols = width + cols + 1;
3644 li = li->li_next;
3645 cole = tv_get_number(&li->li_tv);
3646 if (cole < 0)
3647 cole = width + cole + 1;
3648 li = li->li_next;
3649 lines = tv_get_number(&li->li_tv);
3650 if (lines < 0)
3651 lines = height + lines + 1;
3652 li = li->li_next;
3653 linee = tv_get_number(&li->li_tv);
3654 if (linee < 0)
3655 linee = height + linee + 1;
3656
3657 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003658 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003659 if (cols < 0)
3660 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003661 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003662 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003663 if (lines < 0)
3664 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003665 for (line = lines; line < linee
3666 && line + wp->w_winrow < screen_Rows; ++line)
3667 for (col = cols; col < cole
3668 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003669 popup_transparent[(line + wp->w_winrow) * screen_Columns
3670 + col + wp->w_wincol] = val;
3671 }
3672 }
3673}
3674
3675/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003676 * Only called when popup window "wp" is hidden: If the window is positioned
3677 * next to a text property, and it is now visible, then unhide the popup.
3678 * We don't check if visible popups become hidden, that is done in
3679 * popup_adjust_position().
3680 * Return TRUE if the popup became unhidden.
3681 */
3682 static int
3683check_popup_unhidden(win_T *wp)
3684{
3685 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3686 {
3687 textprop_T prop;
3688 linenr_T lnum;
3689
Bram Moolenaar27724252022-05-08 15:00:04 +01003690 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3691 && find_visible_prop(wp->w_popup_prop_win,
3692 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003693 &prop, &lnum) == OK)
3694 {
3695 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003696 wp->w_popup_prop_topline = 0; // force repositioning
3697 return TRUE;
3698 }
3699 }
3700 return FALSE;
3701}
3702
3703/*
3704 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3705 * That is when the buffer in the popup was changed, or the popup is following
3706 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003707 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003708 */
3709 static int
3710popup_need_position_adjust(win_T *wp)
3711{
3712 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3713 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003714 if (win_valid(wp->w_popup_prop_win)
3715 && (wp->w_popup_prop_changedtick
3716 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3717 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3718 return TRUE;
3719
3720 // May need to adjust the width if the cursor moved.
3721 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003722}
3723
3724/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003725 * Update "popup_mask" if needed.
3726 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003727 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003728 * Also marks window lines for redrawing.
3729 */
3730 void
3731may_update_popup_mask(int type)
3732{
3733 win_T *wp;
3734 short *mask;
3735 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003736 int redraw_all_popups = FALSE;
3737 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003738
3739 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003740 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3741 // basic (such as the screen size) must have changed.
3742 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003743 {
3744 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003745 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003746 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003747
3748 // Check if any popup window buffer has changed and if any popup connected
3749 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003750 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003751 if (wp->w_popup_flags & POPF_HIDDEN)
3752 popup_mask_refresh |= check_popup_unhidden(wp);
3753 else if (popup_need_position_adjust(wp))
3754 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003755 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003756 if (wp->w_popup_flags & POPF_HIDDEN)
3757 popup_mask_refresh |= check_popup_unhidden(wp);
3758 else if (popup_need_position_adjust(wp))
3759 popup_mask_refresh = TRUE;
3760
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003761 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003762 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003763
3764 // Need to update the mask, something has changed.
3765 popup_mask_refresh = FALSE;
3766 popup_mask_tab = curtab;
3767 popup_visible = FALSE;
3768
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003769 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003770 // If redrawing only what is needed, update "popup_mask_next" and then
3771 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003772 redrawing_all_win = TRUE;
3773 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003774 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003775 redrawing_all_win = FALSE;
3776 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003777 mask = popup_mask;
3778 else
3779 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003780 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003781
3782 // Find the window with the lowest zindex that hasn't been handled yet,
3783 // so that the window with a higher zindex overwrites the value in
3784 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003785 popup_reset_handled(POPUP_HANDLED_4);
3786 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003787 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003788 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003789 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003790
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003791 popup_visible = TRUE;
3792
Bram Moolenaar12034e22019-08-25 22:25:02 +02003793 // Recompute the position if the text changed. It may make the popup
3794 // hidden if it's attach to a text property that is no longer visible.
3795 if (redraw_all_popups || popup_need_position_adjust(wp))
3796 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003797 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003798 if (wp->w_popup_flags & POPF_HIDDEN)
3799 continue;
3800 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003801
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003802 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003803 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003804 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003805 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003806 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003807 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003808 col < wp->w_wincol + width - wp->w_popup_leftoff
3809 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003810 if (wp->w_zindex < POPUPMENU_ZINDEX
3811 && pum_visible()
3812 && pum_under_menu(line, col, FALSE))
3813 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3814 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003815 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003816 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003817 }
3818
3819 // Only check which lines are to be updated if not already
3820 // updating all lines.
3821 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003822 {
3823 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3824 win_T *prev_wp = NULL;
3825
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003826 for (line = 0; line < screen_Rows; ++line)
3827 {
3828 int col_done = 0;
3829
3830 for (col = 0; col < screen_Columns; ++col)
3831 {
3832 int off = line * screen_Columns + col;
3833
3834 if (popup_mask[off] != popup_mask_next[off])
3835 {
3836 popup_mask[off] = popup_mask_next[off];
3837
3838 if (line >= cmdline_row)
3839 {
3840 // the command line needs to be cleared if text below
3841 // the popup is now visible.
3842 if (!msg_scrolled && popup_mask_next[off] == 0)
3843 clear_cmdline = TRUE;
3844 }
3845 else if (col >= col_done)
3846 {
3847 linenr_T lnum;
3848 int line_cp = line;
3849 int col_cp = col;
3850
3851 // The screen position "line" / "col" needs to be
3852 // redrawn. Figure out what window that is and update
3853 // w_redraw_top and w_redr_bot. Only needs to be done
3854 // once for each window line.
3855 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3856 if (wp != NULL)
3857 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003858#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003859 // A terminal window needs to be redrawn.
3860 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003861 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003862 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003863#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003864 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003865 if (wp != prev_wp)
3866 {
3867 vim_memset(plines_cache, 0,
3868 sizeof(int) * Rows);
3869 prev_wp = wp;
3870 }
3871
3872 if (line_cp >= wp->w_height)
3873 // In (or below) status line
3874 wp->w_redr_status = TRUE;
3875 else
3876 {
3877 // compute the position in the buffer line
3878 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003879 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003880 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003881 redrawWinline(wp, lnum);
3882 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003883 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003884
3885 // This line is going to be redrawn, no need to
3886 // check until the right side of the window.
3887 col_done = wp->w_wincol + wp->w_width - 1;
3888 }
3889 }
3890 }
3891 }
3892 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003893
3894 vim_free(plines_cache);
3895 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003896
3897 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003898}
3899
3900/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003901 * If the current window is a popup and something relevant changed, recompute
3902 * the position and size.
3903 */
3904 void
3905may_update_popup_position(void)
3906{
3907 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3908 popup_adjust_position(curwin);
3909}
3910
3911/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003912 * Return a string of "len" spaces in IObuff.
3913 */
3914 static char_u *
3915get_spaces(int len)
3916{
3917 vim_memset(IObuff, ' ', (size_t)len);
3918 IObuff[len] = NUL;
3919 return IObuff;
3920}
3921
3922/*
3923 * Update popup windows. They are drawn on top of normal windows.
3924 * "win_update" is called for each popup window, lowest zindex first.
3925 */
3926 void
3927update_popups(void (*win_update)(win_T *wp))
3928{
3929 win_T *wp;
3930 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003931 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003932 int total_width;
3933 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003934 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003935 int popup_attr;
3936 int border_attr[4];
3937 int border_char[8];
3938 char_u buf[MB_MAXBYTES];
3939 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003940 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003941 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003942 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003943 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003944 int sb_thumb_top = 0;
3945 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003946 int attr_scroll = 0;
3947 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003948
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003949 // hide the cursor until redrawing is done.
3950 cursor_off();
3951
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003952 // Find the window with the lowest zindex that hasn't been updated yet,
3953 // so that the window with a higher zindex is drawn later, thus goes on
3954 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003955 popup_reset_handled(POPUP_HANDLED_5);
3956 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003957 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003958 int title_len = 0;
3959 int title_wincol;
3960
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003961 // This drawing uses the zindex of the popup window, so that it's on
3962 // top of the text but doesn't draw when another popup with higher
3963 // zindex is on top of the character.
3964 screen_zindex = wp->w_zindex;
3965
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003966 // Set flags in popup_transparent[] for masked cells.
3967 update_popup_transparent(wp, 1);
3968
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003969 // adjust w_winrow and w_wincol for border and padding, since
3970 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003971 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003972 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3973 - wp->w_popup_leftoff;
3974 if (wp->w_wincol + left_extra < 0)
3975 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003976 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003977 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003978
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003979 // Draw the popup text, unless it's off screen.
3980 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003981 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003982 // May need to update the "cursorline" highlighting, which may also
3983 // change "topline"
3984 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3985 popup_highlight_curline(wp);
3986
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003987 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003988
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003989 // move the cursor into the visible lines, otherwise executing
3990 // commands with win_execute() may cause the text to jump.
3991 if (wp->w_cursor.lnum < wp->w_topline)
3992 wp->w_cursor.lnum = wp->w_topline;
3993 else if (wp->w_cursor.lnum >= wp->w_botline)
3994 wp->w_cursor.lnum = wp->w_botline - 1;
3995 }
3996
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003997 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003998 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003999
4000 // Add offset for border and padding if not done already.
4001 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4002 {
4003 wp->w_wcol += left_extra;
4004 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4005 }
4006 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004007 {
4008 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004009 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004010 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004011
Bram Moolenaareabddc42022-04-02 15:32:16 +01004012 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004013 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004014 popup_attr = get_wcr_attr(wp);
4015
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004016 if (wp->w_winrow + total_height > cmdline_row)
4017 wp->w_popup_flags |= POPF_ON_CMDLINE;
4018 else
4019 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4020
4021
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004022 // We can only use these line drawing characters when 'encoding' is
4023 // "utf-8" and 'ambiwidth' is "single".
4024 if (enc_utf8 && *p_ambw == 's')
4025 {
4026 border_char[0] = border_char[2] = 0x2550;
4027 border_char[1] = border_char[3] = 0x2551;
4028 border_char[4] = 0x2554;
4029 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004030 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4031 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004032 border_char[7] = 0x255a;
4033 }
4034 else
4035 {
4036 border_char[0] = border_char[2] = '-';
4037 border_char[1] = border_char[3] = '|';
4038 for (i = 4; i < 8; ++i)
4039 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004040 if (wp->w_popup_flags & POPF_RESIZE)
4041 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004042 }
4043 for (i = 0; i < 8; ++i)
4044 if (wp->w_border_char[i] != 0)
4045 border_char[i] = wp->w_border_char[i];
4046
4047 for (i = 0; i < 4; ++i)
4048 {
4049 border_attr[i] = popup_attr;
4050 if (wp->w_border_highlight[i] != NULL)
4051 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4052 }
4053
Bram Moolenaard91467f2020-11-21 12:42:09 +01004054 // Title goes on top of border or padding.
4055 title_wincol = wp->w_wincol + 1;
4056 if (wp->w_popup_title != NULL)
4057 {
rbtnnc6119412021-08-07 13:08:45 +02004058 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004059
Ralf Schandlbc869872021-05-28 14:12:14 +02004060 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004061 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004062 {
4063 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4064 char_u *title_text = alloc(title_byte_len + 1);
4065
4066 if (title_text != NULL)
4067 {
4068 trunc_string(wp->w_popup_title, title_text,
4069 total_width - 2, title_byte_len + 1);
4070 screen_puts(title_text, wp->w_winrow, title_wincol,
4071 wp->w_popup_border[0] > 0
4072 ? border_attr[0] : popup_attr);
4073 vim_free(title_text);
4074 }
4075
Bram Moolenaard91467f2020-11-21 12:42:09 +01004076 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004077 }
4078 else
4079 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4080 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004081 }
4082
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004083 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004084 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004085 if (wp->w_popup_border[0] > 0)
4086 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004087 // top border; do not draw over the title
4088 if (title_len > 0)
4089 {
4090 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4091 wincol < 0 ? 0 : wincol, title_wincol,
4092 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004093 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004094 border_char[0], border_attr[0]);
4095 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4096 title_wincol + title_len, wincol + total_width,
4097 border_char[0], border_char[0], border_attr[0]);
4098 }
4099 else
4100 {
4101 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4102 wincol < 0 ? 0 : wincol, wincol + total_width,
4103 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4104 ? border_char[4] : border_char[0],
4105 border_char[0], border_attr[0]);
4106 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004107 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004108 {
4109 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4110 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004111 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004112 }
4113 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004114 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4115 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004116
Bram Moolenaard529ba52019-07-02 23:13:53 +02004117 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4118 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004119 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004120 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004121 - wp->w_has_scrollbar;
4122 if (padcol < 0)
4123 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004124 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004125 padcol = 0;
4126 }
4127 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004128 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004129 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004130 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004131 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004132 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004133 // top padding and no border; do not draw over the title
4134 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004135 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004136 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004137 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004138 row += 1;
4139 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004140 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004141 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004142 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004143 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004144
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004145 // Compute scrollbar thumb position and size.
4146 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004147 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004148 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4149 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004150 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004151
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004152 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4153 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004154 if (wp->w_topline > 1 && sb_thumb_height == height)
4155 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004156 if (sb_thumb_height == 0)
4157 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004158 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004159 // it just fits, avoid divide by zero
4160 sb_thumb_top = 0;
4161 else
4162 sb_thumb_top = (wp->w_topline - 1
4163 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004164 * (wp->w_height - sb_thumb_height)
4165 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004166 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4167 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004168 last = total_height - top_off - wp->w_popup_border[2];
4169 if (sb_thumb_top >= last)
4170 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004171 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004172
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004173 if (wp->w_scrollbar_highlight != NULL)
4174 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4175 else
4176 attr_scroll = highlight_attr[HLF_PSB];
4177 if (wp->w_thumb_highlight != NULL)
4178 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4179 else
4180 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004181 }
4182
4183 for (i = wp->w_popup_border[0];
4184 i < total_height - wp->w_popup_border[2]; ++i)
4185 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004186 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004187 // left and right padding only needed next to the body
4188 int do_padding =
4189 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4190 && i < total_height - wp->w_popup_border[2]
4191 - wp->w_popup_padding[2];
4192
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004193 row = wp->w_winrow + i;
4194
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004195 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004196 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004197 {
4198 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004199 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004200 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004201 if (do_padding && wp->w_popup_padding[3] > 0)
4202 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004203 int col = wincol + wp->w_popup_border[3];
4204
Bram Moolenaard529ba52019-07-02 23:13:53 +02004205 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004206 pad_left = wp->w_popup_padding[3];
4207 if (col < 0)
4208 {
4209 pad_left += col;
4210 col = 0;
4211 }
4212 if (pad_left > 0)
4213 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4214 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004215 // scrollbar
4216 if (wp->w_has_scrollbar)
4217 {
4218 int line = i - top_off;
4219 int scroll_col = wp->w_wincol + total_width - 1
4220 - wp->w_popup_border[1];
4221
4222 if (line >= 0 && line < wp->w_height)
4223 screen_putchar(' ', row, scroll_col,
4224 line >= sb_thumb_top
4225 && line < sb_thumb_top + sb_thumb_height
4226 ? attr_thumb : attr_scroll);
4227 else
4228 screen_putchar(' ', row, scroll_col, popup_attr);
4229 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004230 // right border
4231 if (wp->w_popup_border[1] > 0)
4232 {
4233 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004234 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004235 }
4236 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004237 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004238 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004239 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004240 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4241 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004242 }
4243
4244 if (wp->w_popup_padding[2] > 0)
4245 {
4246 // bottom padding
4247 row = wp->w_winrow + wp->w_popup_border[0]
4248 + wp->w_popup_padding[0] + wp->w_height;
4249 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004250 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004251 }
4252
4253 if (wp->w_popup_border[2] > 0)
4254 {
4255 // bottom border
4256 row = wp->w_winrow + total_height - 1;
4257 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004258 wincol < 0 ? 0 : wincol,
4259 wincol + total_width,
4260 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004261 ? border_char[7] : border_char[2],
4262 border_char[2], border_attr[2]);
4263 if (wp->w_popup_border[1] > 0)
4264 {
4265 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004266 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004267 }
4268 }
4269
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004270 if (wp->w_popup_close == POPCLOSE_BUTTON)
4271 {
4272 // close button goes on top of anything at the top-right corner
4273 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004274 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004275 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4276 }
4277
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004278 update_popup_transparent(wp, 0);
4279
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004280 // Back to the normal zindex.
4281 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004282
4283#ifdef HAS_MESSAGE_WINDOW
4284 // if this was the message window popup may start the timer now
4285 may_start_message_win_timer(wp);
4286#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004287 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004288
4289#if defined(FEAT_SEARCH_EXTRA)
4290 // In case win_update() called start_search_hl().
4291 end_search_hl();
4292#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004293}
4294
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004295/*
4296 * Mark references in callbacks of one popup window.
4297 */
4298 static int
4299set_ref_in_one_popup(win_T *wp, int copyID)
4300{
4301 int abort = FALSE;
4302 typval_T tv;
4303
4304 if (wp->w_close_cb.cb_partial != NULL)
4305 {
4306 tv.v_type = VAR_PARTIAL;
4307 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4308 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4309 }
4310 if (wp->w_filter_cb.cb_partial != NULL)
4311 {
4312 tv.v_type = VAR_PARTIAL;
4313 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4314 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4315 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004316 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004317 return abort;
4318}
4319
4320/*
4321 * Set reference in callbacks of popup windows.
4322 */
4323 int
4324set_ref_in_popups(int copyID)
4325{
4326 int abort = FALSE;
4327 win_T *wp;
4328 tabpage_T *tp;
4329
4330 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4331 abort = abort || set_ref_in_one_popup(wp, copyID);
4332
4333 FOR_ALL_TABPAGES(tp)
4334 {
4335 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4336 abort = abort || set_ref_in_one_popup(wp, copyID);
4337 if (abort)
4338 break;
4339 }
4340 return abort;
4341}
Bram Moolenaar79648732019-07-18 21:43:07 +02004342
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004343 int
4344popup_is_popup(win_T *wp)
4345{
4346 return wp->w_popup_flags != 0;
4347}
4348
4349#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004350/*
4351 * Find an existing popup used as the preview window, in the current tab page.
4352 * Return NULL if not found.
4353 */
4354 win_T *
4355popup_find_preview_window(void)
4356{
4357 win_T *wp;
4358
4359 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004360 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004361 if (wp->w_p_pvw)
4362 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004363 return NULL;
4364}
4365
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004366/*
4367 * Find an existing popup used as the info window, in the current tab page.
4368 * Return NULL if not found.
4369 */
4370 win_T *
4371popup_find_info_window(void)
4372{
4373 win_T *wp;
4374
4375 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004376 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004377 if (wp->w_popup_flags & POPF_INFO)
4378 return wp;
4379 return NULL;
4380}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004381#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004382
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004383 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004384f_popup_findecho(typval_T *argvars UNUSED, typval_T *rettv)
4385{
4386#ifdef HAS_MESSAGE_WINDOW
4387 rettv->vval.v_number = message_win == NULL ? 0 : message_win->w_id;
4388#else
4389 rettv->vval.v_number = 0;
4390#endif
4391}
4392
4393 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004394f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4395{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004396#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004397 win_T *wp = popup_find_info_window();
4398
4399 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004400#else
4401 rettv->vval.v_number = 0;
4402#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004403}
4404
4405 void
4406f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004407{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004408#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004409 win_T *wp = popup_find_preview_window();
4410
4411 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004412#else
4413 rettv->vval.v_number = 0;
4414#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004415}
4416
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004417#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004418/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004419 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004420 * NOTE: this makes the popup the current window, so that the file can be
4421 * edited. However, it must not remain to be the current window, the caller
4422 * must make sure of that.
4423 */
4424 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004425popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004426{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004427 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004428
4429 if (wp == NULL)
4430 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004431 if (info)
4432 wp->w_popup_flags |= POPF_INFO;
4433 else
4434 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004435
4436 // Set the width to a reasonable value, so that w_topline can be computed.
4437 if (wp->w_minwidth > 0)
4438 wp->w_width = wp->w_minwidth;
4439 else if (wp->w_maxwidth > 0)
4440 wp->w_width = wp->w_maxwidth;
4441 else
4442 wp->w_width = curwin->w_width;
4443
4444 // Will switch to another buffer soon, dummy one can be wiped.
4445 wp->w_buffer->b_locked = FALSE;
4446
4447 win_enter(wp, FALSE);
4448 return OK;
4449}
4450
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004451/*
4452 * Close any preview popup.
4453 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004454 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004455popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004456{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004457 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004458
4459 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004460 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004461}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004462
4463/*
4464 * Hide the info popup.
4465 */
4466 void
4467popup_hide_info(void)
4468{
4469 win_T *wp = popup_find_info_window();
4470
4471 if (wp != NULL)
4472 popup_hide(wp);
4473}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004474
4475/*
4476 * Close any info popup.
4477 */
4478 void
4479popup_close_info(void)
4480{
4481 win_T *wp = popup_find_info_window();
4482
4483 if (wp != NULL)
4484 popup_close_with_retval(wp, -1);
4485}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004486#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004487
Bram Moolenaar9198de32022-08-27 21:30:03 +01004488#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4489
Bram Moolenaar9198de32022-08-27 21:30:03 +01004490/*
4491 * Get the message window.
4492 * Returns NULL if something failed.
4493 */
4494 win_T *
4495popup_get_message_win(void)
4496{
4497 if (message_win == NULL)
4498 {
4499 int i;
4500
4501 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4502
4503 if (message_win == NULL)
4504 return NULL;
4505
4506 // use the full screen width
4507 message_win->w_width = Columns;
4508
4509 // position at bottom of screen
4510 message_win->w_popup_pos = POPPOS_BOTTOM;
4511 message_win->w_wantcol = 1;
4512 message_win->w_minwidth = 9999;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01004513 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004514
4515 // no padding, border at the top
4516 for (i = 0; i < 4; ++i)
4517 message_win->w_popup_padding[i] = 0;
4518 for (i = 1; i < 4; ++i)
4519 message_win->w_popup_border[i] = 0;
4520
4521 if (message_win->w_popup_timer != NULL)
4522 message_win->w_popup_timer->tr_keep = TRUE;
4523 }
4524 return message_win;
4525}
4526
4527/*
4528 * If the message window is not visible: show it
4529 * If the message window is visible: reset the timeout
4530 */
4531 void
4532popup_show_message_win(void)
4533{
4534 if (message_win != NULL)
4535 {
4536 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4537 {
4538 // the highlight may have changed.
4539 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4540 popup_show(message_win);
4541 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004542 start_message_win_timer = TRUE;
4543 }
4544}
4545
4546 static void
4547may_start_message_win_timer(win_T *wp)
4548{
4549 if (wp == message_win && start_message_win_timer)
4550 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004551 if (message_win->w_popup_timer != NULL)
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004552 {
4553 message_win->w_popup_timer->tr_interval = message_win_time;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004554 timer_start(message_win->w_popup_timer);
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004555 message_win_time = 3000;
4556 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004557 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004558 }
4559}
4560
4561 int
4562popup_message_win_visible(void)
4563{
4564 return message_win != NULL
4565 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4566}
4567
4568/*
4569 * If the message window is visible: hide it.
4570 */
4571 void
4572popup_hide_message_win(void)
4573{
4574 if (message_win != NULL)
4575 popup_hide(message_win);
4576}
4577
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004578// Values saved in start_echowindow() and restored in end_echowindow()
4579static int save_msg_didout = FALSE;
4580static int save_msg_col = 0;
4581// Values saved in end_echowindow() and restored in start_echowindow()
4582static int ew_msg_didout = FALSE;
4583static int ew_msg_col = 0;
4584
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004585/*
4586 * Invoked before outputting a message for ":echowindow".
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004587 * "time_sec" is the display time, zero means using the default 3 sec.
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004588 */
4589 void
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004590start_echowindow(int time_sec)
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004591{
4592 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004593 save_msg_didout = msg_didout;
4594 save_msg_col = msg_col;
4595 msg_didout = ew_msg_didout;
4596 msg_col = ew_msg_col;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01004597 if (time_sec != 0)
4598 message_win_time = time_sec * 1000;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004599}
4600
4601/*
4602 * Invoked after outputting a message for ":echowindow".
4603 */
4604 void
4605end_echowindow(void)
4606{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004607 in_echowindow = FALSE;
4608
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004609 if ((State & MODE_HITRETURN) == 0)
4610 // show the message window now
4611 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004612
4613 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004614 ew_msg_didout = TRUE;
4615 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4616 msg_didout = save_msg_didout;
4617 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004618}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004619#endif
4620
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004621/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004622 * Close any popup for a text property associated with "win".
4623 * Return TRUE if a popup was closed.
4624 */
4625 int
4626popup_win_closed(win_T *win)
4627{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004628 int round;
4629 win_T *wp;
4630 win_T *next;
4631 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004632
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004633 for (round = 1; round <= 2; ++round)
4634 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4635 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004636 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004637 next = wp->w_next;
4638 if (wp->w_popup_prop_win == win)
4639 {
4640 popup_close_with_retval(wp, -1);
4641 ret = TRUE;
4642 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004643 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004644 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004645}
4646
4647/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004648 * Set the title of the popup window to the file name.
4649 */
4650 void
4651popup_set_title(win_T *wp)
4652{
4653 if (wp->w_buffer->b_fname != NULL)
4654 {
4655 char_u dirname[MAXPATHL];
4656 size_t len;
4657
4658 mch_dirname(dirname, MAXPATHL);
4659 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4660
4661 vim_free(wp->w_popup_title);
4662 len = STRLEN(wp->w_buffer->b_fname) + 3;
4663 wp->w_popup_title = alloc((int)len);
4664 if (wp->w_popup_title != NULL)
4665 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4666 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004667 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004668 }
4669}
4670
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004671# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004672/*
4673 * If there is a preview window, update the title.
4674 * Used after changing directory.
4675 */
4676 void
4677popup_update_preview_title(void)
4678{
4679 win_T *wp = popup_find_preview_window();
4680
4681 if (wp != NULL)
4682 popup_set_title(wp);
4683}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004684# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004685
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004686#endif // FEAT_PROP_POPUP