blob: aae546566efc556bd5539aa414a0370ca5b366c1 [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
35// Flag set when a message is added to the message window, timer is started
36// when the message window is drawn. This might be after pressing Enter at the
37// hit-enter prompt.
38static int start_message_win_timer = FALSE;
39
40static void may_start_message_win_timer(win_T *wp);
Bram Moolenaar43568642022-08-28 13:02:45 +010041#endif
42
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static void popup_adjust_position(win_T *wp);
44
Bram Moolenaar4d784b22019-05-25 19:51:39 +020045/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020046 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020047 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020048 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020049 */
50 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020051popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020052{
53 dictitem_T *di;
54 char_u *val;
55 char_u *s;
56 char_u *endp;
57 int n = 0;
58
59 di = dict_find(dict, key, -1);
60 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020061 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062
63 val = tv_get_string(&di->di_tv);
64 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020065 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020066
67 setcursor_mayforce(TRUE);
68 s = val + 6;
69 if (*s != NUL)
70 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020071 endp = s;
72 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
73 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020074 if (endp != NULL && *skipwhite(endp) != NUL)
75 {
Bram Moolenaar108010a2021-06-27 22:03:33 +020076 semsg(_(e_invalid_expression_str), val);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return 0;
78 }
79 }
80
81 if (STRCMP(key, "line") == 0)
82 n = screen_screenrow() + 1 + n;
83 else // "col"
84 n = screen_screencol() + 1 + n;
85
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020086 // Zero means "not set", use -1 instead.
87 if (n == 0)
88 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020089 return n;
90}
91
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020092 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020093set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020094{
95 dictitem_T *di;
96
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020097 di = dict_find(dict, (char_u *)name, -1);
98 if (di != NULL)
99 {
100 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000101 emsg(_(e_list_required));
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102 else
103 {
104 list_T *list = di->di_tv.vval.v_list;
105 listitem_T *li;
106 int i;
107 int nr;
108
109 for (i = 0; i < 4; ++i)
110 array[i] = 1;
111 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100112 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200113 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200114 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
115 ++i, li = li->li_next)
116 {
117 nr = (int)tv_get_number(&li->li_tv);
118 if (nr >= 0)
119 array[i] = nr > max_val ? max_val : nr;
120 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100121 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200122 }
123 }
124}
125
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200126/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200127 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200128 */
129 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200130set_moved_values(win_T *wp)
131{
132 wp->w_popup_curwin = curwin;
133 wp->w_popup_lnum = curwin->w_cursor.lnum;
134 wp->w_popup_mincol = curwin->w_cursor.col;
135 wp->w_popup_maxcol = curwin->w_cursor.col;
136}
137
138/*
139 * Used when popup options contain "moved" with "word" or "WORD".
140 */
141 static void
142set_moved_columns(win_T *wp, int flags)
143{
144 char_u *ptr;
145 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
146
147 if (len > 0)
148 {
149 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
150 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
151 }
152}
153
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200154/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200155 * Used when popup options contain "mousemoved": set default moved values.
156 */
157 static void
158set_mousemoved_values(win_T *wp)
159{
160 wp->w_popup_mouse_row = mouse_row;
161 wp->w_popup_mouse_mincol = mouse_col;
162 wp->w_popup_mouse_maxcol = mouse_col;
163}
164
Bram Moolenaarf8e43f62022-03-24 15:15:15 +0000165 static void
166update_popup_uses_mouse_move(void)
167{
168 popup_uses_mouse_move = FALSE;
169 if (popup_visible)
170 {
171 win_T *wp;
172
173 FOR_ALL_POPUPWINS(wp)
174 if (wp->w_popup_mouse_row != 0)
175 {
176 popup_uses_mouse_move = TRUE;
177 return;
178 }
179 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
180 if (wp->w_popup_mouse_row != 0)
181 {
182 popup_uses_mouse_move = TRUE;
183 return;
184 }
185 }
186}
187
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188/*
189 * Used when popup options contain "moved" with "word" or "WORD".
190 */
191 static void
192set_mousemoved_columns(win_T *wp, int flags)
193{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 char_u *text;
196 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200197 pos_T pos;
198 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200199
200 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200201 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200202 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 // convert text column to mouse column
204 pos.col = col;
205 pos.coladd = 0;
206 getvcol(textwp, &pos, &mcol, NULL, NULL);
207 wp->w_popup_mouse_mincol = mcol;
208
Bram Moolenaar10727682019-07-12 13:59:20 +0200209 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200210 getvcol(textwp, &pos, NULL, NULL, &mcol);
211 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200212 vim_free(text);
213 }
214}
215
216/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200217 * Return TRUE if "row"/"col" is on the border of the popup.
218 * The values are relative to the top-left corner.
219 */
220 int
221popup_on_border(win_T *wp, int row, int col)
222{
223 return (row == 0 && wp->w_popup_border[0] > 0)
224 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
225 || (col == 0 && wp->w_popup_border[3] > 0)
226 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
227}
228
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200229/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200230 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
231 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200232 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200233 * Caller should check the left mouse button was clicked.
234 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200235 */
236 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200237popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200238{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200239 if (wp->w_popup_close == POPCLOSE_BUTTON
240 && row == 0 && col == popup_width(wp) - 1)
241 {
242 popup_close_for_mouse_click(wp);
243 return TRUE;
244 }
245 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200246}
247
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200248// Values set when dragging a popup window starts.
249static int drag_start_row;
250static int drag_start_col;
251static int drag_start_wantline;
252static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200253static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200254
255/*
256 * Mouse down on border of popup window: start dragging it.
257 * Uses mouse_col and mouse_row.
258 */
259 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200260popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261{
262 drag_start_row = mouse_row;
263 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200264 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200265 drag_start_wantline = wp->w_winrow + 1;
266 else
267 drag_start_wantline = wp->w_wantline;
268 if (wp->w_wantcol == 0)
269 drag_start_wantcol = wp->w_wincol + 1;
270 else
271 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200272
273 // Stop centering the popup
274 if (wp->w_popup_pos == POPPOS_CENTER)
275 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200276
277 drag_on_resize_handle = wp->w_popup_border[1] > 0
278 && wp->w_popup_border[2] > 0
279 && row == popup_height(wp) - 1
280 && col == popup_width(wp) - 1;
281
282 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
283 {
284 if (wp->w_popup_pos == POPPOS_TOPRIGHT
285 || wp->w_popup_pos == POPPOS_BOTRIGHT)
286 wp->w_wantcol = wp->w_wincol + 1;
287 if (wp->w_popup_pos == POPPOS_BOTLEFT)
288 wp->w_wantline = wp->w_winrow + 1;
289 wp->w_popup_pos = POPPOS_TOPLEFT;
290 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200291}
292
293/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200294 * Mouse moved while dragging a popup window: adjust the window popup position
295 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200296 */
297 void
298popup_drag(win_T *wp)
299{
300 // The popup may be closed before dragging stops.
301 if (!win_valid_popup(wp))
302 return;
303
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200304 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
305 {
306 int width_inc = mouse_col - drag_start_col;
307 int height_inc = mouse_row - drag_start_row;
308
309 if (width_inc != 0)
310 {
311 int width = wp->w_width + width_inc;
312
313 if (width < 1)
314 width = 1;
315 wp->w_minwidth = width;
316 wp->w_maxwidth = width;
317 drag_start_col = mouse_col;
318 }
319
320 if (height_inc != 0)
321 {
322 int height = wp->w_height + height_inc;
323
324 if (height < 1)
325 height = 1;
326 wp->w_minheight = height;
327 wp->w_maxheight = height;
328 drag_start_row = mouse_row;
329 }
330
331 popup_adjust_position(wp);
332 return;
333 }
334
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000335 if (!(wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL)))
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200336 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200337 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
338 if (wp->w_wantline < 1)
339 wp->w_wantline = 1;
340 if (wp->w_wantline > Rows)
341 wp->w_wantline = Rows;
342 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
343 if (wp->w_wantcol < 1)
344 wp->w_wantcol = 1;
345 if (wp->w_wantcol > Columns)
346 wp->w_wantcol = Columns;
347
348 popup_adjust_position(wp);
349}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200350
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200351/*
352 * Set w_firstline to match the current "wp->w_topline".
353 */
354 void
355popup_set_firstline(win_T *wp)
356{
357 int height = wp->w_height;
358
359 wp->w_firstline = wp->w_topline;
360 popup_adjust_position(wp);
361
362 // we don't want the popup to get smaller, decrement the first line
363 // until it doesn't
364 while (wp->w_firstline > 1 && wp->w_height < height)
365 {
366 --wp->w_firstline;
367 popup_adjust_position(wp);
368 }
369}
370
371/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200372 * Return TRUE if the position is in the popup window scrollbar.
373 */
374 int
375popup_is_in_scrollbar(win_T *wp, int row, int col)
376{
377 return wp->w_has_scrollbar
378 && row >= wp->w_popup_border[0]
379 && row < popup_height(wp) - wp->w_popup_border[2]
380 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
381}
382
383
384/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200385 * Handle a click in a popup window, if it is in the scrollbar.
386 */
387 void
388popup_handle_scrollbar_click(win_T *wp, int row, int col)
389{
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200390 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200391 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100392 int height = popup_height(wp);
393 int new_topline = wp->w_topline;
394
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200395 if (row >= height / 2)
396 {
397 // Click in lower half, scroll down.
398 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaard28950f2022-05-29 14:13:04 +0100399 ++new_topline;
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200400 }
401 else if (wp->w_topline > 1)
402 // click on upper half, scroll up.
Bram Moolenaard28950f2022-05-29 14:13:04 +0100403 --new_topline;
404 if (new_topline != wp->w_topline)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200405 {
Bram Moolenaard28950f2022-05-29 14:13:04 +0100406 set_topline(wp, new_topline);
407 if (wp == curwin)
408 {
409 if (wp->w_cursor.lnum < wp->w_topline)
410 {
411 wp->w_cursor.lnum = wp->w_topline;
412 check_cursor();
413 }
414 else if (wp->w_cursor.lnum >= wp->w_botline)
415 {
416 wp->w_cursor.lnum = wp->w_botline - 1;
417 check_cursor();
418 }
419 }
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200420 popup_set_firstline(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100421 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200422 }
423 }
424}
425
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200426#if defined(FEAT_TIMERS)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100427/*
428 * Add a timer to "wp" with "time".
429 * If "close" is true use popup_close(), otherwise popup_hide().
430 */
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200431 static void
Bram Moolenaar9198de32022-08-27 21:30:03 +0100432popup_add_timeout(win_T *wp, int time, int close)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200433{
434 char_u cbbuf[50];
435 char_u *ptr = cbbuf;
436 typval_T tv;
437
438 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
Bram Moolenaar9198de32022-08-27 21:30:03 +0100439 close ? "(_) => popup_close(%d)" : "(_) => popup_hide(%d)",
440 wp->w_id);
Bram Moolenaar9bb0dad2021-07-19 22:19:29 +0200441 if (get_lambda_tv_and_compile(&ptr, &tv, FALSE, &EVALARG_EVALUATE) == OK)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200442 {
443 wp->w_popup_timer = create_timer(time, 0);
444 wp->w_popup_timer->tr_callback = get_callback(&tv);
445 clear_tv(&tv);
446 }
447}
448#endif
449
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100450 static poppos_T
451get_pos_entry(dict_T *d, int give_error)
452{
Bram Moolenaard61efa52022-07-23 09:52:04 +0100453 char_u *str = dict_get_string(d, "pos", FALSE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100454 int nr;
455
456 if (str == NULL)
457 return POPPOS_NONE;
458
K.Takataeeec2542021-06-02 13:28:16 +0200459 for (nr = 0; nr < (int)ARRAY_LENGTH(poppos_entries); ++nr)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100460 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
461 return poppos_entries[nr].pp_val;
462
463 if (give_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000464 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100465 return POPPOS_NONE;
466}
467
Bram Moolenaar17627312019-06-02 19:53:44 +0200468/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200469 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200470 */
471 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200472apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200473{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200474 int nr;
475 char_u *str;
476 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200477
Bram Moolenaard61efa52022-07-23 09:52:04 +0100478 if ((nr = dict_get_number_def(d, "minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200479 wp->w_minwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100480 if ((nr = dict_get_number_def(d, "minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200481 wp->w_minheight = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100482 if ((nr = dict_get_number_def(d, "maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200483 wp->w_maxwidth = nr;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100484 if ((nr = dict_get_number_def(d, "maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200485 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200486
487 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200488 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200489 wp->w_wantline = nr;
490 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200491 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200492 wp->w_wantcol = nr;
493
Bram Moolenaar55881332020-08-18 13:04:15 +0200494
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 nr = dict_get_bool(d, "fixed", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200496 if (nr != -1)
497 wp->w_popup_fixed = nr != 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200498
Bram Moolenaar12034e22019-08-25 22:25:02 +0200499 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100500 poppos_T ppt = get_pos_entry(d, TRUE);
501
502 if (ppt != POPPOS_NONE)
503 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200504 }
505
Bram Moolenaard61efa52022-07-23 09:52:04 +0100506 str = dict_get_string(d, "textprop", FALSE);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200507 if (str != NULL)
508 {
509 wp->w_popup_prop_type = 0;
510 if (*str != NUL)
511 {
Bram Moolenaara37cb552019-11-16 20:03:31 +0100512 wp->w_popup_prop_win = curwin;
513 di = dict_find(d, (char_u *)"textpropwin", -1);
514 if (di != NULL)
515 {
516 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaarefa19232021-02-06 14:59:27 +0100517 if (!win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaara37cb552019-11-16 20:03:31 +0100518 wp->w_popup_prop_win = curwin;
519 }
520
521 nr = find_prop_type_id(str, wp->w_popup_prop_win->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200522 if (nr <= 0)
523 nr = find_prop_type_id(str, NULL);
524 if (nr <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000525 semsg(_(e_invalid_argument_str), str);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200526 else
Bram Moolenaar12034e22019-08-25 22:25:02 +0200527 wp->w_popup_prop_type = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200528 }
529 }
530
531 di = dict_find(d, (char_u *)"textpropid", -1);
532 if (di != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +0100533 wp->w_popup_prop_id = dict_get_number(d, "textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200534}
535
Bram Moolenaarb0992022020-01-30 14:55:42 +0100536/*
537 * Handle "moved" and "mousemoved" arguments.
538 */
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200539 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200540handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
541{
542 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
543 {
544 char_u *s = di->di_tv.vval.v_string;
545 int flags = 0;
546
547 if (STRCMP(s, "word") == 0)
548 flags = FIND_IDENT | FIND_STRING;
549 else if (STRCMP(s, "WORD") == 0)
550 flags = FIND_STRING;
551 else if (STRCMP(s, "expr") == 0)
552 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
553 else if (STRCMP(s, "any") != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000554 semsg(_(e_invalid_argument_str), s);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200555 if (flags != 0)
556 {
557 if (mousemoved)
558 set_mousemoved_columns(wp, flags);
559 else
560 set_moved_columns(wp, flags);
561 }
562 }
563 else if (di->di_tv.v_type == VAR_LIST
564 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200565 && (di->di_tv.vval.v_list->lv_len == 2
566 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200567 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200568 list_T *l = di->di_tv.vval.v_list;
Bram Moolenaarb0992022020-01-30 14:55:42 +0100569 listitem_T *li;
Bram Moolenaara1396152019-10-20 18:46:05 +0200570 int mincol;
571 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200572
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200573 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarb0992022020-01-30 14:55:42 +0100574 li = l->lv_first;
575 if (l->lv_len == 3)
Bram Moolenaara1396152019-10-20 18:46:05 +0200576 {
577 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
578
579 // Three numbers, might be from popup_getoptions().
580 if (mousemoved)
581 wp->w_popup_mouse_row = nr;
582 else
583 wp->w_popup_lnum = nr;
584 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100585 if (nr == 0)
586 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200587 }
588
589 mincol = tv_get_number(&li->li_tv);
590 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200591 if (mousemoved)
592 {
593 wp->w_popup_mouse_mincol = mincol;
594 wp->w_popup_mouse_maxcol = maxcol;
595 }
596 else
597 {
598 wp->w_popup_mincol = mincol;
599 wp->w_popup_maxcol = maxcol;
600 }
601 }
602 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000603 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200604}
605
606 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200607check_highlight(dict_T *dict, char *name, char_u **pval)
608{
609 dictitem_T *di;
610 char_u *str;
611
612 di = dict_find(dict, (char_u *)name, -1);
613 if (di != NULL)
614 {
615 if (di->di_tv.v_type != VAR_STRING)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000616 semsg(_(e_invalid_value_for_argument_str), name);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200617 else
618 {
619 str = tv_get_string(&di->di_tv);
620 if (*str != NUL)
621 *pval = vim_strsave(str);
622 }
623 }
624}
625
Bram Moolenaarae943152019-06-16 22:54:14 +0200626/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200627 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200628 */
629 static void
630popup_show_curline(win_T *wp)
631{
632 if (wp->w_cursor.lnum < wp->w_topline)
633 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200634 else if (wp->w_cursor.lnum >= wp->w_botline
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200635 && (wp->w_valid & VALID_BOTLINE))
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200636 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200637 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200638 if (wp->w_topline < 1)
639 wp->w_topline = 1;
640 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
641 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200642 while (wp->w_topline < wp->w_cursor.lnum
643 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
644 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
645 > wp->w_height)
646 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200647 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200648
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200649 // Don't let "firstline" cause a scroll.
650 if (wp->w_firstline > 0)
651 wp->w_firstline = wp->w_topline;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200652}
653
654/*
655 * Get the sign group name for window "wp".
656 * Returns a pointer to a static buffer, overwritten on the next call.
657 */
658 static char_u *
659popup_get_sign_name(win_T *wp)
660{
661 static char buf[30];
662
663 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
664 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200665}
666
667/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200668 * Highlight the line with the cursor.
669 * Also scrolls the text to put the cursor line in view.
670 */
671 static void
672popup_highlight_curline(win_T *wp)
673{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200674 int sign_id = 0;
675 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200676
Bram Moolenaar72570732019-11-30 14:21:53 +0100677 buf_delete_signs(wp->w_buffer, (char_u *)"PopUpMenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200678
679 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
680 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200681 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200682
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200683 if (!sign_exists_by_name(sign_name))
684 {
685 char *linehl = "PopupSelected";
686
687 if (syn_name2id((char_u *)linehl) == 0)
688 linehl = "PmenuSel";
Bram Moolenaar9198de32022-08-27 21:30:03 +0100689 sign_define_by_name(sign_name, NULL, (char_u *)linehl,
690 NULL, NULL, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200691 }
692
Bram Moolenaar72570732019-11-30 14:21:53 +0100693 sign_place(&sign_id, (char_u *)"PopUpMenu", sign_name,
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200694 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100695 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200696 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200697 else
698 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200699 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200700}
701
702/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200703 * Shared between popup_create() and f_popup_setoptions().
704 */
705 static void
706apply_general_options(win_T *wp, dict_T *dict)
707{
708 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200709 int nr;
710 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200711
Bram Moolenaarae943152019-06-16 22:54:14 +0200712 // TODO: flip
713
714 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200715 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200716 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100717 wp->w_firstline = dict_get_number(dict, "firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200718 if (wp->w_firstline < 0)
719 wp->w_firstline = -1;
720 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200721
Bram Moolenaard61efa52022-07-23 09:52:04 +0100722 nr = dict_get_bool(dict, "scrollbar", -1);
Bram Moolenaar6c542f72020-09-27 21:16:45 +0200723 if (nr != -1)
724 wp->w_want_scrollbar = nr;
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200725
Bram Moolenaard61efa52022-07-23 09:52:04 +0100726 str = dict_get_string(dict, "title", FALSE);
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200727 if (str != NULL)
728 {
729 vim_free(wp->w_popup_title);
730 wp->w_popup_title = vim_strsave(str);
731 }
732
Bram Moolenaard61efa52022-07-23 09:52:04 +0100733 nr = dict_get_bool(dict, "wrap", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200734 if (nr != -1)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200735 wp->w_p_wrap = nr != 0;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200736
Bram Moolenaard61efa52022-07-23 09:52:04 +0100737 nr = dict_get_bool(dict, "drag", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200738 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200739 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200740 if (nr)
741 wp->w_popup_flags |= POPF_DRAG;
742 else
743 wp->w_popup_flags &= ~POPF_DRAG;
744 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100745 nr = dict_get_bool(dict, "dragall", -1);
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000746 if (nr != -1)
747 {
748 if (nr)
749 wp->w_popup_flags |= POPF_DRAGALL;
750 else
751 wp->w_popup_flags &= ~POPF_DRAGALL;
752 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200753
Bram Moolenaard61efa52022-07-23 09:52:04 +0100754 nr = dict_get_bool(dict, "posinvert", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200755 if (nr != -1)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100756 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100757 if (nr)
758 wp->w_popup_flags |= POPF_POSINVERT;
759 else
760 wp->w_popup_flags &= ~POPF_POSINVERT;
761 }
762
Bram Moolenaard61efa52022-07-23 09:52:04 +0100763 nr = dict_get_bool(dict, "resize", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200764 if (nr != -1)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200765 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200766 if (nr)
767 wp->w_popup_flags |= POPF_RESIZE;
768 else
769 wp->w_popup_flags &= ~POPF_RESIZE;
770 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200771
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200772 di = dict_find(dict, (char_u *)"close", -1);
773 if (di != NULL)
774 {
775 int ok = TRUE;
776
777 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
778 {
779 char_u *s = di->di_tv.vval.v_string;
780
781 if (STRCMP(s, "none") == 0)
782 wp->w_popup_close = POPCLOSE_NONE;
783 else if (STRCMP(s, "button") == 0)
784 wp->w_popup_close = POPCLOSE_BUTTON;
785 else if (STRCMP(s, "click") == 0)
786 wp->w_popup_close = POPCLOSE_CLICK;
787 else
788 ok = FALSE;
789 }
790 else
791 ok = FALSE;
792 if (!ok)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000793 semsg(_(e_invalid_value_for_argument_str_str), "close", tv_get_string(&di->di_tv));
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200794 }
795
Bram Moolenaard61efa52022-07-23 09:52:04 +0100796 str = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200797 if (str != NULL)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000798 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200799 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
800 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000801#ifdef FEAT_TERMINAL
802 term_update_wincolor(wp);
803#endif
804 }
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200805
Bram Moolenaarae943152019-06-16 22:54:14 +0200806 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
807 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200808
Bram Moolenaar790498b2019-06-01 22:15:29 +0200809 di = dict_find(dict, (char_u *)"borderhighlight", -1);
810 if (di != NULL)
811 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200812 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000813 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200814 else
815 {
816 list_T *list = di->di_tv.vval.v_list;
817 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200818 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200819
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200820 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200821 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
822 ++i, li = li->li_next)
823 {
824 str = tv_get_string(&li->li_tv);
825 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100826 {
827 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200828 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100829 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200830 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200831 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
832 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100833 {
834 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200835 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200836 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100837 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200838 }
839 }
840
Bram Moolenaar790498b2019-06-01 22:15:29 +0200841 di = dict_find(dict, (char_u *)"borderchars", -1);
842 if (di != NULL)
843 {
844 if (di->di_tv.v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000845 emsg(_(e_list_required));
Bram Moolenaar790498b2019-06-01 22:15:29 +0200846 else
847 {
848 list_T *list = di->di_tv.vval.v_list;
849 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200850 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200851
852 if (list != NULL)
Bram Moolenaarb0992022020-01-30 14:55:42 +0100853 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200854 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200855 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
856 ++i, li = li->li_next)
857 {
858 str = tv_get_string(&li->li_tv);
859 if (*str != NUL)
860 wp->w_border_char[i] = mb_ptr2char(str);
861 }
Bram Moolenaar8d4ed112020-04-04 14:50:32 +0200862 if (list->lv_len == 1)
863 for (i = 1; i < 8; ++i)
864 wp->w_border_char[i] = wp->w_border_char[0];
865 if (list->lv_len == 2)
866 {
867 for (i = 4; i < 8; ++i)
868 wp->w_border_char[i] = wp->w_border_char[1];
869 for (i = 1; i < 4; ++i)
870 wp->w_border_char[i] = wp->w_border_char[0];
871 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200872 }
873 }
874 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200875
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200876 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
877 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
878
Bram Moolenaarae943152019-06-16 22:54:14 +0200879 di = dict_find(dict, (char_u *)"zindex", -1);
880 if (di != NULL)
881 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100882 wp->w_zindex = dict_get_number(dict, "zindex");
Bram Moolenaarae943152019-06-16 22:54:14 +0200883 if (wp->w_zindex < 1)
884 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
885 if (wp->w_zindex > 32000)
886 wp->w_zindex = 32000;
887 }
888
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200889 di = dict_find(dict, (char_u *)"mask", -1);
890 if (di != NULL)
891 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200892 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200893
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200894 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200895 {
896 listitem_T *li;
897
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200898 ok = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200899 FOR_ALL_LIST_ITEMS(di->di_tv.vval.v_list, li)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200900 {
901 if (li->li_tv.v_type != VAR_LIST
902 || li->li_tv.vval.v_list == NULL
903 || li->li_tv.vval.v_list->lv_len != 4)
904 {
905 ok = FALSE;
906 break;
907 }
Bram Moolenaarb0992022020-01-30 14:55:42 +0100908 else
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200909 CHECK_LIST_MATERIALIZE(li->li_tv.vval.v_list);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200910 }
911 }
912 if (ok)
913 {
914 wp->w_popup_mask = di->di_tv.vval.v_list;
915 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200916 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200917 }
918 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000919 semsg(_(e_invalid_value_for_argument_str), "mask");
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200920 }
921
Bram Moolenaarae943152019-06-16 22:54:14 +0200922#if defined(FEAT_TIMERS)
923 // Add timer to close the popup after some time.
Bram Moolenaard61efa52022-07-23 09:52:04 +0100924 nr = dict_get_number(dict, "time");
Bram Moolenaarae943152019-06-16 22:54:14 +0200925 if (nr > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +0100926 popup_add_timeout(wp, nr, TRUE);
Bram Moolenaarae943152019-06-16 22:54:14 +0200927#endif
928
Bram Moolenaar3397f742019-06-02 18:40:06 +0200929 di = dict_find(dict, (char_u *)"moved", -1);
930 if (di != NULL)
931 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200932 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200933 handle_moved_argument(wp, di, FALSE);
934 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200935
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200936 di = dict_find(dict, (char_u *)"mousemoved", -1);
937 if (di != NULL)
938 {
939 set_mousemoved_values(wp);
940 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200941 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200942
Bram Moolenaard61efa52022-07-23 09:52:04 +0100943 nr = dict_get_bool(dict, "cursorline", -1);
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100944 if (nr != -1)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200945 {
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100946 if (nr != 0)
947 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200948 else
Bram Moolenaar6bfc4752021-02-21 23:12:18 +0100949 wp->w_popup_flags &= ~POPF_CURSORLINE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200950 }
951
Bram Moolenaarae943152019-06-16 22:54:14 +0200952 di = dict_find(dict, (char_u *)"filter", -1);
953 if (di != NULL)
954 {
955 callback_T callback = get_callback(&di->di_tv);
956
957 if (callback.cb_name != NULL)
958 {
959 free_callback(&wp->w_filter_cb);
960 set_callback(&wp->w_filter_cb, &callback);
961 }
962 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100963 nr = dict_get_bool(dict, "mapping", -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200964 if (nr != -1)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200965 {
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200966 if (nr)
967 wp->w_popup_flags |= POPF_MAPPING;
968 else
969 wp->w_popup_flags &= ~POPF_MAPPING;
970 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200971
Bram Moolenaard61efa52022-07-23 09:52:04 +0100972 str = dict_get_string(dict, "filtermode", FALSE);
Bram Moolenaar581ba392019-09-03 22:08:33 +0200973 if (str != NULL)
974 {
975 if (STRCMP(str, "a") == 0)
976 wp->w_filter_mode = MODE_ALL;
977 else
978 wp->w_filter_mode = mode_str2flags(str);
979 }
980
Bram Moolenaarae943152019-06-16 22:54:14 +0200981 di = dict_find(dict, (char_u *)"callback", -1);
982 if (di != NULL)
983 {
984 callback_T callback = get_callback(&di->di_tv);
985
986 if (callback.cb_name != NULL)
987 {
988 free_callback(&wp->w_close_cb);
989 set_callback(&wp->w_close_cb, &callback);
990 }
991 }
992}
993
994/*
995 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200996 * "create" is TRUE when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200997 */
998 static void
Bram Moolenaar3697c9b2020-09-26 22:03:00 +0200999apply_options(win_T *wp, dict_T *dict, int create)
Bram Moolenaarae943152019-06-16 22:54:14 +02001000{
1001 int nr;
1002
1003 apply_move_options(wp, dict);
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001004
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001005 if (create)
1006 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
Bram Moolenaar6d585f42020-07-26 22:20:54 +02001007 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
1008
Bram Moolenaarae943152019-06-16 22:54:14 +02001009 apply_general_options(wp, dict);
1010
Bram Moolenaard61efa52022-07-23 09:52:04 +01001011 nr = dict_get_bool(dict, "hidden", FALSE);
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001012 if (nr > 0)
Bram Moolenaar27724252022-05-08 15:00:04 +01001013 wp->w_popup_flags |= POPF_HIDDEN | POPF_HIDDEN_FORCE;
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001014
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001015 // when "firstline" and "cursorline" are both set and the cursor would be
1016 // above or below the displayed lines, move the cursor to "firstline".
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001017 if (wp->w_firstline > 0 && (wp->w_popup_flags & POPF_CURSORLINE))
1018 {
1019 if (wp->w_firstline > wp->w_buffer->b_ml.ml_line_count)
1020 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001021 else if (wp->w_cursor.lnum < wp->w_firstline
1022 || wp->w_cursor.lnum >= wp->w_firstline + wp->w_height)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001023 wp->w_cursor.lnum = wp->w_firstline;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001024 wp->w_topline = wp->w_firstline;
1025 wp->w_valid &= ~VALID_BOTLINE;
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02001026 }
1027
Bram Moolenaar33796b32019-06-08 16:01:13 +02001028 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001029 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001030}
1031
1032/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001033 * Add lines to the popup from a list of strings.
1034 */
1035 static void
1036add_popup_strings(buf_T *buf, list_T *l)
1037{
1038 listitem_T *li;
1039 linenr_T lnum = 0;
1040 char_u *p;
1041
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001042 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001043 if (li->li_tv.v_type == VAR_STRING)
1044 {
1045 p = li->li_tv.vval.v_string;
1046 ml_append_buf(buf, lnum++,
1047 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1048 }
1049}
1050
1051/*
1052 * Add lines to the popup from a list of dictionaries.
1053 */
1054 static void
1055add_popup_dicts(buf_T *buf, list_T *l)
1056{
1057 listitem_T *li;
1058 listitem_T *pli;
1059 linenr_T lnum = 0;
1060 char_u *p;
1061 dict_T *dict;
1062
1063 // first add the text lines
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001064 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001065 {
1066 if (li->li_tv.v_type != VAR_DICT)
1067 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001068 semsg(_(e_argument_1_list_item_nr_dictionary_required), lnum + 1);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001069 return;
1070 }
1071 dict = li->li_tv.vval.v_dict;
Bram Moolenaard61efa52022-07-23 09:52:04 +01001072 p = dict == NULL ? NULL : dict_get_string(dict, "text", FALSE);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001073 ml_append_buf(buf, lnum++,
1074 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
1075 }
1076
1077 // add the text properties
1078 lnum = 1;
1079 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1080 {
1081 dictitem_T *di;
1082 list_T *plist;
1083
1084 dict = li->li_tv.vval.v_dict;
1085 di = dict_find(dict, (char_u *)"props", -1);
1086 if (di != NULL)
1087 {
1088 if (di->di_tv.v_type != VAR_LIST)
1089 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001090 emsg(_(e_list_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001091 return;
1092 }
1093 plist = di->di_tv.vval.v_list;
1094 if (plist != NULL)
1095 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001096 FOR_ALL_LIST_ITEMS(plist, pli)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001097 {
1098 if (pli->li_tv.v_type != VAR_DICT)
1099 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001100 emsg(_(e_dictionary_required));
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001101 return;
1102 }
1103 dict = pli->li_tv.vval.v_dict;
1104 if (dict != NULL)
1105 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001106 int col = dict_get_number(dict, "col");
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001107
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001108 prop_add_common(lnum, col, dict, buf, NULL);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02001109 }
1110 }
1111 }
1112 }
1113 }
1114}
1115
1116/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001117 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1118 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001119 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001120popup_top_extra(win_T *wp)
1121{
1122 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1123
1124 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1125 return 1;
1126 return extra;
1127}
1128
1129/*
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001130 * Get the padding plus border at the left.
1131 */
1132 int
1133popup_left_extra(win_T *wp)
1134{
1135 return wp->w_popup_border[3] + wp->w_popup_padding[3];
1136}
1137
1138/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001139 * Return the height of popup window "wp", including border and padding.
1140 */
1141 int
1142popup_height(win_T *wp)
1143{
1144 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001145 + popup_top_extra(wp)
1146 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001147}
1148
1149/*
1150 * Return the width of popup window "wp", including border, padding and
1151 * scrollbar.
1152 */
1153 int
1154popup_width(win_T *wp)
1155{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001156 // w_leftcol is how many columns of the core are left of the screen
1157 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001158 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001159 + popup_extra_width(wp)
1160 + wp->w_popup_rightoff;
1161}
1162
1163/*
1164 * Return the extra width of popup window "wp": border, padding and scrollbar.
1165 */
1166 int
1167popup_extra_width(win_T *wp)
1168{
1169 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1170 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1171 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001172}
1173
1174/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001175 * Adjust the position and size of the popup to fit on the screen.
1176 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001177 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001178popup_adjust_position(win_T *wp)
1179{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001180 linenr_T lnum;
1181 int wrapped = 0;
1182 int maxwidth;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001183 int maxwidth_no_scrollbar;
1184 int width_with_scrollbar = 0;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001185 int used_maxwidth = FALSE;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001186 int margin_width = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001187 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001188 int center_vert = FALSE;
1189 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001190 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001191 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001192 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1193 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1194 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1195 int extra_height = top_extra + bot_extra;
1196 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001197 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001198 int org_winrow = wp->w_winrow;
1199 int org_wincol = wp->w_wincol;
1200 int org_width = wp->w_width;
1201 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001202 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001203 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaar19398262020-03-14 15:28:08 +01001204 int minwidth, minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001205 int maxheight = Rows;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001206 int wantline = wp->w_wantline; // adjusted for textprop
1207 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001208 int use_wantcol = wantcol != 0;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001209 int adjust_height_for_top_aligned = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001210
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001211 wp->w_winrow = 0;
1212 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001213 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001214 wp->w_popup_leftoff = 0;
1215 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001216
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001217 // May need to update the "cursorline" highlighting, which may also change
1218 // "topline"
1219 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1220 popup_highlight_curline(wp);
1221
Bram Moolenaar12034e22019-08-25 22:25:02 +02001222 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1223 {
1224 win_T *prop_win = wp->w_popup_prop_win;
1225 textprop_T prop;
1226 linenr_T prop_lnum;
1227 pos_T pos;
1228 int screen_row;
1229 int screen_scol;
1230 int screen_ccol;
1231 int screen_ecol;
1232
1233 // Popup window is positioned relative to a text property.
1234 if (find_visible_prop(prop_win,
1235 wp->w_popup_prop_type, wp->w_popup_prop_id,
1236 &prop, &prop_lnum) == FAIL)
1237 {
1238 // Text property is no longer visible, hide the popup.
1239 // Unhiding the popup is done in check_popup_unhidden().
1240 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1241 {
1242 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001243 if (win_valid(wp->w_popup_prop_win))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001244 redraw_win_later(wp->w_popup_prop_win, UPD_SOME_VALID);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001245 }
1246 return;
1247 }
1248
1249 // Compute the desired position from the position of the text
1250 // property. Use "wantline" and "wantcol" as offsets.
1251 pos.lnum = prop_lnum;
1252 pos.col = prop.tp_col;
1253 if (wp->w_popup_pos == POPPOS_TOPLEFT
1254 || wp->w_popup_pos == POPPOS_BOTLEFT)
1255 pos.col += prop.tp_len - 1;
1256 textpos2screenpos(prop_win, &pos, &screen_row,
1257 &screen_scol, &screen_ccol, &screen_ecol);
1258
Bram Moolenaar82db31c2021-02-10 14:56:11 +01001259 if (screen_scol == 0)
1260 {
1261 // position is off screen, make the width zero to hide it.
1262 wp->w_width = 0;
1263 return;
1264 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001265 if (wp->w_popup_pos == POPPOS_TOPLEFT
1266 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1267 // below the text
1268 wantline = screen_row + wantline + 1;
1269 else
1270 // above the text
1271 wantline = screen_row + wantline - 1;
1272 center_vert = FALSE;
1273 if (wp->w_popup_pos == POPPOS_TOPLEFT
1274 || wp->w_popup_pos == POPPOS_BOTLEFT)
1275 // right of the text
1276 wantcol = screen_ecol + wantcol;
1277 else
1278 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001279 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001280 use_wantcol = TRUE;
1281 }
1282 else
1283 {
1284 // If no line was specified default to vertical centering.
1285 if (wantline == 0)
1286 center_vert = TRUE;
1287 else if (wantline < 0)
1288 // If "wantline" is negative it actually means zero.
1289 wantline = 0;
1290 if (wantcol < 0)
1291 // If "wantcol" is negative it actually means zero.
1292 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001293 }
1294
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001295 if (wp->w_popup_pos == POPPOS_CENTER)
1296 {
1297 // center after computing the size
1298 center_vert = TRUE;
1299 center_hor = TRUE;
1300 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001301 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001302 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001303 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1304 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001305 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001306 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001307 if (wp->w_winrow >= Rows)
1308 wp->w_winrow = Rows - 1;
1309 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01001310 if (wp->w_popup_pos == POPPOS_BOTTOM)
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001311 {
1312 // Assume that each buffer line takes one screen line, and one line
1313 // for the top border. First make sure cmdline_row is valid,
1314 // calling update_screen() will set it only later.
1315 compute_cmdrow();
Bram Moolenaar37fef162022-08-29 18:16:32 +01001316 wp->w_winrow = MAX(cmdline_row
1317 - wp->w_buffer->b_ml.ml_line_count - 1, 0);
Bram Moolenaar87e74d02022-09-11 20:12:15 +01001318 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001320 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001321 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001322 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1323 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001324 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001325 wp->w_wincol = wantcol - 1;
Bram Moolenaarba2920f2020-03-06 21:43:17 +01001326 // Need to see at least one character after the decoration.
1327 if (wp->w_wincol > Columns - left_extra - 1)
1328 wp->w_wincol = Columns - left_extra - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001329 }
1330 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001331
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001332 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001333 // When left aligned use the space available, but shift to the left when we
1334 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001335 maxspace = Columns - wp->w_wincol - left_extra;
1336 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001337 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 {
1339 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001340 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001341 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001342
1343 if (wp->w_p_nu || wp->w_p_rnu)
1344 margin_width = number_width(wp) + 1;
1345#ifdef FEAT_FOLDING
1346 margin_width += wp->w_p_fdc;
1347#endif
1348#ifdef FEAT_SIGNS
1349 if (signcolumn_on(wp))
1350 margin_width += 2;
1351#endif
1352 if (margin_width >= maxwidth)
1353 margin_width = maxwidth - 1;
1354
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001355 minwidth = wp->w_minwidth;
Bram Moolenaar19398262020-03-14 15:28:08 +01001356 minheight = wp->w_minheight;
1357#ifdef FEAT_TERMINAL
1358 // A terminal popup initially does not have content, use a default minimal
1359 // width of 20 characters and height of 5 lines.
1360 if (wp->w_buffer->b_term != NULL)
1361 {
1362 if (minwidth == 0)
1363 minwidth = 20;
1364 if (minheight == 0)
1365 minheight = 5;
1366 }
1367#endif
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001368
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001369 if (wp->w_maxheight > 0)
1370 maxheight = wp->w_maxheight;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01001371 else if (wp->w_popup_pos == POPPOS_BOTTOM)
1372 maxheight = cmdline_row - 1;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001373
Bram Moolenaar8d241042019-06-12 23:40:01 +02001374 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001375 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001376 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001377 if (wp->w_topline < 1)
1378 wp->w_topline = 1;
1379 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001380 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1381
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001382 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001383 // Use a minimum width of one, so that something shows when there is no
1384 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001385 // When "firstline" is -1 then start with the last buffer line and go
1386 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001387 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001388 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001389 if (wp->w_firstline < 0)
1390 lnum = wp->w_buffer->b_ml.ml_line_count;
1391 else
1392 lnum = wp->w_topline;
1393 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001394 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001395 int len;
1396 int w_width = wp->w_width;
1397
1398 // Count Tabs for what they are worth and compute the length based on
1399 // the maximum width (matters when 'showbreak' is set).
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001400 // "margin_width" is added to "len" where it matters.
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001401 if (wp->w_width < maxwidth)
1402 wp->w_width = maxwidth;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001403 len = win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001404 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001405 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001406
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001407 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001408 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001409 while (len + margin_width > maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001410 {
1411 ++wrapped;
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001412 len -= maxwidth - margin_width;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001413 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001414 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 }
1416 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001417 else if (len + margin_width > maxwidth
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001418 && allow_adjust_left
1419 && (wp->w_popup_pos == POPPOS_TOPLEFT
1420 || wp->w_popup_pos == POPPOS_BOTLEFT))
1421 {
1422 // adjust leftwise to fit text on screen
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001423 int shift_by = len + margin_width - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001424
Bram Moolenaar51c31312019-06-15 22:27:23 +02001425 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001426 {
1427 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001428
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001429 len -= truncate_shift;
1430 shift_by -= truncate_shift;
1431 }
1432
1433 wp->w_wincol -= shift_by;
1434 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001435 wp->w_width = maxwidth;
1436 }
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001437 if (wp->w_width < len + margin_width)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001438 {
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02001439 wp->w_width = len + margin_width;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001440 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1441 wp->w_width = wp->w_maxwidth;
1442 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001443
1444 if (wp->w_firstline < 0)
1445 --lnum;
1446 else
1447 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001448
1449 // do not use the width of lines we're not going to show
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001450 if (maxheight > 0
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001451 && (wp->w_firstline >= 0
1452 ? lnum - wp->w_topline
1453 : wp->w_buffer->b_ml.ml_line_count - lnum)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001454 + wrapped >= maxheight)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001455 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001456 }
1457
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001458 if (wp->w_firstline < 0)
LemonBoy0044e512022-04-20 19:47:37 +01001459 wp->w_topline = lnum + 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001460
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001461 wp->w_has_scrollbar = wp->w_want_scrollbar
1462 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001463#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001464 if (wp->w_buffer->b_term != NULL && !term_is_finished(wp->w_buffer))
1465 // Terminal window with running job never has a scrollbar, adjusts to
1466 // window height.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001467 wp->w_has_scrollbar = FALSE;
1468#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001469 maxwidth_no_scrollbar = maxwidth;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001470 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001471 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001472 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001473 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001474 // make space for the scrollbar if needed, when lines wrap and when
1475 // applying minwidth
1476 if (maxwidth + right_extra >= maxspace
1477 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1478 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001479 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001480
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001481 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1482 {
1483 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1484
1485 if (minwidth < title_len)
1486 minwidth = title_len;
1487 }
1488
1489 if (minwidth > 0 && wp->w_width < minwidth)
1490 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001491 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001492 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001493 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001494 // some columns cut off on the right
1495 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001496
1497 // If the window doesn't fit because 'minwidth' is set then the
1498 // scrollbar is at the far right of the screen, use the size without
1499 // the scrollbar.
1500 if (wp->w_has_scrollbar && wp->w_minwidth > 0)
1501 {
1502 int off = wp->w_width - maxwidth;
1503
1504 if (off > right_extra)
1505 extra_width -= right_extra;
1506 else
1507 extra_width -= off;
1508 wp->w_width = maxwidth_no_scrollbar;
1509 }
1510 else
1511 {
1512 wp->w_width = maxwidth;
1513
1514 // when adding a scrollbar below need to adjust the width
1515 width_with_scrollbar = maxwidth_no_scrollbar - right_extra;
1516 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02001517 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001518 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001519 {
1520 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1521 if (wp->w_wincol < 0)
1522 wp->w_wincol = 0;
1523 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001524 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1525 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1526 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001527 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001528
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001529 // Right aligned: move to the right if needed.
1530 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001531 if (leftoff >= 0)
1532 wp->w_wincol = leftoff;
1533 else if (wp->w_popup_fixed)
1534 {
1535 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001536 // columns when displaying the window, minus left border and
1537 // padding.
1538 if (-leftoff > left_extra)
1539 wp->w_leftcol = -leftoff - left_extra;
1540 wp->w_width -= wp->w_leftcol;
1541 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001542 if (wp->w_width < 0)
1543 wp->w_width = 0;
1544 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001545 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001546
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001547 if (wp->w_p_wrap || (!wp->w_popup_fixed
1548 && (wp->w_popup_pos == POPPOS_TOPLEFT
1549 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001550 {
1551 int want_col = 0;
1552
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001553 // try to show the right border and any scrollbar
1554 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001555 if (want_col > 0 && wp->w_wincol > 0
1556 && wp->w_wincol + want_col >= Columns)
1557 {
1558 wp->w_wincol = Columns - want_col;
1559 if (wp->w_wincol < 0)
1560 wp->w_wincol = 0;
1561 }
1562 }
1563
Bram Moolenaar8d241042019-06-12 23:40:01 +02001564 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1565 + 1 + wrapped;
Bram Moolenaar19398262020-03-14 15:28:08 +01001566 if (minheight > 0 && wp->w_height < minheight)
1567 wp->w_height = minheight;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001568 if (maxheight > 0 && wp->w_height > maxheight)
1569 wp->w_height = maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001570 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001571 if (wp->w_height > Rows - wp->w_winrow)
1572 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001573
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001574 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001575 {
1576 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1577 if (wp->w_winrow < 0)
1578 wp->w_winrow = 0;
1579 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001580 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001581 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001582 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001583 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001584 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001585 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001586 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1587 {
1588 // Bottom aligned but does not fit, and less space on the other
1589 // side or "posinvert" is off: reduce height.
1590 wp->w_winrow = 0;
1591 wp->w_height = wantline - extra_height;
1592 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001593 else
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001594 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001595 // Not enough space and more space on the other side: make top
1596 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001597 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001598 adjust_height_for_top_aligned = TRUE;
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001599 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001600 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001601 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1602 || wp->w_popup_pos == POPPOS_TOPLEFT)
1603 {
Bram Moolenaareabddc42022-04-02 15:32:16 +01001604 if (wp != popup_dragwin
1605 && wantline + (wp->w_height + extra_height) - 1 > Rows
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001606 && wantline * 2 > Rows
1607 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001608 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001609 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001610 // make bottom aligned and recompute the height
1611 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001612 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001613 if (wp->w_winrow < 0)
1614 {
1615 wp->w_height += wp->w_winrow;
1616 wp->w_winrow = 0;
1617 }
1618 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001619 else
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001620 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001621 wp->w_winrow = wantline - 1;
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001622 adjust_height_for_top_aligned = TRUE;
1623 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001624 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001625
1626 if (adjust_height_for_top_aligned && wp->w_want_scrollbar
1627 && wp->w_winrow + wp->w_height + extra_height > Rows)
1628 {
1629 // Bottom of the popup goes below the last line, reduce the height and
1630 // add a scrollbar.
1631 wp->w_height = Rows - wp->w_winrow - extra_height;
1632#ifdef FEAT_TERMINAL
Bram Moolenaard28950f2022-05-29 14:13:04 +01001633 if (wp->w_buffer->b_term == NULL || term_is_finished(wp->w_buffer))
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001634#endif
Bram Moolenaareabddc42022-04-02 15:32:16 +01001635 {
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001636 wp->w_has_scrollbar = TRUE;
Bram Moolenaareabddc42022-04-02 15:32:16 +01001637 if (width_with_scrollbar > 0)
1638 wp->w_width = width_with_scrollbar;
1639 }
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02001640 }
1641
1642 // make sure w_winrow is valid
Bram Moolenaar12034e22019-08-25 22:25:02 +02001643 if (wp->w_winrow >= Rows)
1644 wp->w_winrow = Rows - 1;
1645 else if (wp->w_winrow < 0)
1646 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001647
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02001648 if (wp->w_height != org_height)
1649 win_comp_scroll(wp);
1650
Bram Moolenaar17146962019-05-30 00:12:11 +02001651 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001652 if (win_valid(wp->w_popup_prop_win))
1653 {
1654 wp->w_popup_prop_changedtick =
1655 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1656 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1657 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001658
1659 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001660 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001661 if (org_winrow != wp->w_winrow
1662 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001663 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001664 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001665 || org_width != wp->w_width
1666 || org_height != wp->w_height)
1667 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001668 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001669 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1670 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001671 popup_mask_refresh = TRUE;
1672 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001673}
1674
Bram Moolenaar17627312019-06-02 19:53:44 +02001675typedef enum
1676{
1677 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001678 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001679 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001680 TYPE_NOTIFICATION,
Bram Moolenaar9198de32022-08-27 21:30:03 +01001681 TYPE_MESSAGE_WIN, // similar to TYPE_NOTIFICATION
Bram Moolenaara730e552019-06-16 19:05:31 +02001682 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001683 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001684 TYPE_PREVIEW, // preview window
1685 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001686} create_type_T;
1687
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001688/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001689 * Return TRUE if "type" is TYPE_NOTIFICATION or TYPE_MESSAGE_WIN.
1690 */
1691 static int
1692popup_is_notification(create_type_T type)
1693{
1694 return type == TYPE_NOTIFICATION || type == TYPE_MESSAGE_WIN;
1695}
1696
1697/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001698 * Make "buf" empty and set the contents to "text".
1699 * Used by popup_create() and popup_settext().
1700 */
1701 static void
1702popup_set_buffer_text(buf_T *buf, typval_T text)
1703{
1704 int lnum;
1705
1706 // Clear the buffer, then replace the lines.
1707 curbuf = buf;
1708 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
Bram Moolenaarca70c072020-05-30 20:30:46 +02001709 ml_delete(lnum);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001710 curbuf = curwin->w_buffer;
1711
1712 // Add text to the buffer.
1713 if (text.v_type == VAR_STRING)
1714 {
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001715 char_u *s = text.vval.v_string;
1716
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001717 // just a string
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001718 ml_append_buf(buf, 0, s == NULL ? (char_u *)"" : s, (colnr_T)0, TRUE);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001719 }
1720 else
1721 {
1722 list_T *l = text.vval.v_list;
1723
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02001724 if (l != NULL && l->lv_len > 0)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001725 {
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001726 if (l->lv_first == &range_list_item)
1727 emsg(_(e_using_number_as_string));
1728 else if (l->lv_first->li_tv.v_type == VAR_STRING)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001729 // list of strings
1730 add_popup_strings(buf, l);
1731 else
1732 // list of dictionaries
1733 add_popup_dicts(buf, l);
1734 }
1735 }
1736
1737 // delete the line that was in the empty buffer
1738 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001739 ml_delete(buf->b_ml.ml_line_count);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001740 curbuf = curwin->w_buffer;
1741}
1742
1743/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001744 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1745 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001746 * Return FAIL if the parsing fails.
1747 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001748 static int
1749parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001750{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001751 char_u *p =
1752#ifdef FEAT_QUICKFIX
1753 !is_preview ? p_cpp :
1754#endif
1755 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001756
Bram Moolenaar258cef52019-08-21 17:29:29 +02001757 if (wp != NULL)
1758 wp->w_popup_flags &= ~POPF_INFO_MENU;
1759
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001760 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001761 {
1762 char_u *e, *dig;
1763 char_u *s = p;
1764 int x;
1765
1766 e = vim_strchr(p, ':');
1767 if (e == NULL || e[1] == NUL)
1768 return FAIL;
1769
1770 p = vim_strchr(e, ',');
1771 if (p == NULL)
1772 p = e + STRLEN(e);
1773 dig = e + 1;
1774 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001775
1776 if (STRNCMP(s, "height:", 7) == 0)
1777 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001778 if (dig != p)
1779 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001780 if (wp != NULL)
1781 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001782 if (is_preview)
1783 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001784 wp->w_maxheight = x;
1785 }
1786 }
1787 else if (STRNCMP(s, "width:", 6) == 0)
1788 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001789 if (dig != p)
1790 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001791 if (wp != NULL)
1792 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001793 if (is_preview)
1794 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001795 wp->w_maxwidth = x;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001796 wp->w_maxwidth_opt = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001797 }
1798 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001799 else if (STRNCMP(s, "highlight:", 10) == 0)
1800 {
1801 if (wp != NULL)
1802 {
1803 int c = *p;
1804
1805 *p = NUL;
1806 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1807 s + 10, OPT_FREE|OPT_LOCAL, 0);
1808 *p = c;
1809 }
1810 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001811 else if (STRNCMP(s, "border:", 7) == 0)
1812 {
1813 char_u *arg = s + 7;
1814 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1815 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1816 int i;
1817
1818 if (!on && !off)
1819 return FAIL;
1820 if (wp != NULL)
1821 {
1822 for (i = 0; i < 4; ++i)
1823 wp->w_popup_border[i] = on ? 1 : 0;
1824 if (off)
1825 // only show the X for close when there is a border
1826 wp->w_popup_close = POPCLOSE_NONE;
1827 }
1828 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001829 else if (STRNCMP(s, "align:", 6) == 0)
1830 {
1831 char_u *arg = s + 6;
1832 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1833 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1834
1835 if (!menu && !item)
1836 return FAIL;
1837 if (wp != NULL && menu)
1838 wp->w_popup_flags |= POPF_INFO_MENU;
1839 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001840 else
1841 return FAIL;
1842 }
1843 return OK;
1844}
1845
1846/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001847 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1848 * is not NULL.
1849 * Return FAIL if the parsing fails.
1850 */
1851 int
1852parse_previewpopup(win_T *wp)
1853{
1854 return parse_popup_option(wp, TRUE);
1855}
1856
1857/*
1858 * Parse the 'completepopup' option and apply the values to window "wp" if it
1859 * is not NULL.
1860 * Return FAIL if the parsing fails.
1861 */
1862 int
1863parse_completepopup(win_T *wp)
1864{
1865 return parse_popup_option(wp, FALSE);
1866}
1867
1868/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001869 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001870 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001871 */
1872 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001873popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001874{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001875 poppos_T ppt = POPPOS_NONE;
1876
1877 if (d != NULL)
1878 ppt = get_pos_entry(d, FALSE);
1879
Bram Moolenaar79648732019-07-18 21:43:07 +02001880 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001881 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001882 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001883 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1884 }
1885 else
1886 {
1887 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1888 if (wp->w_wantline == 0) // cursor in first line
1889 {
1890 wp->w_wantline = 2;
1891 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1892 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1893 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001894 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001895
Bram Moolenaar79648732019-07-18 21:43:07 +02001896 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001897 if (wp->w_wantcol > Columns - width)
1898 {
1899 wp->w_wantcol = Columns - width;
1900 if (wp->w_wantcol < 1)
1901 wp->w_wantcol = 1;
1902 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001903
Bram Moolenaar79648732019-07-18 21:43:07 +02001904 popup_adjust_position(wp);
1905}
1906
1907/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001908 * Set w_wantline and w_wantcol for the a given screen position.
1909 * Caller must take care of running into the window border.
1910 */
1911 void
1912popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1913{
1914 wp->w_wantline = row;
1915 wp->w_wantcol = col;
1916 popup_adjust_position(wp);
1917}
1918
1919/*
1920 * Add a border and lef&right padding.
1921 */
1922 static void
1923add_border_left_right_padding(win_T *wp)
1924{
1925 int i;
1926
1927 for (i = 0; i < 4; ++i)
1928 {
1929 wp->w_popup_border[i] = 1;
1930 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1931 }
1932}
1933
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001934#ifdef FEAT_TERMINAL
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001935/*
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001936 * Return TRUE if there is any popup window with a terminal buffer.
1937 */
1938 static int
1939popup_terminal_exists(void)
1940{
1941 win_T *wp;
1942 tabpage_T *tp;
1943
1944 FOR_ALL_POPUPWINS(wp)
1945 if (wp->w_buffer->b_term != NULL)
1946 return TRUE;
1947 FOR_ALL_TABPAGES(tp)
1948 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1949 if (wp->w_buffer->b_term != NULL)
1950 return TRUE;
1951 return FALSE;
1952}
Bram Moolenaarc33b3212020-05-18 20:12:09 +02001953#endif
Bram Moolenaarb5383b12020-05-18 19:46:48 +02001954
1955/*
Bram Moolenaarb13d3402022-08-29 13:44:28 +01001956 * Mark all popup windows in the current tab and global for redrawing.
1957 */
1958 void
1959popup_redraw_all(void)
1960{
1961 win_T *wp;
1962
1963 FOR_ALL_POPUPWINS(wp)
1964 wp->w_redr_type = UPD_NOT_VALID;
1965 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1966 wp->w_redr_type = UPD_NOT_VALID;
1967}
1968
1969/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001970 * Set the color for a notification window.
1971 */
1972 static void
1973popup_update_color(win_T *wp, create_type_T type)
1974{
1975 char *hiname = type == TYPE_MESSAGE_WIN
1976 ? "MessageWindow" : "PopupNotification";
1977 int nr = syn_name2id((char_u *)hiname);
1978
1979 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1980 (char_u *)(nr == 0 ? "WarningMsg" : hiname),
1981 OPT_FREE|OPT_LOCAL, 0);
1982}
1983
1984/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001985 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001986 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001987 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001988 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001989 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001990 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001991popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001992{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001993 win_T *wp;
1994 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001995 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001996 int new_buffer;
1997 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001998 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001999 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002000
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002002 {
Yegappan Lakshmanan733b1242021-11-15 11:22:09 +00002003 if (in_vim9script()
2004 && (check_for_string_or_number_or_list_arg(argvars, 0) == FAIL
2005 || check_for_dict_arg(argvars, 1) == FAIL))
2006 return NULL;
2007
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002008 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02002009 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002010 {
Bram Moolenaar49540192019-12-11 19:34:54 +01002011 buf = buflist_findnr(argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002012 if (buf == NULL)
2013 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002014 semsg(_(e_buffer_nr_does_not_exist), argvars[0].vval.v_number);
Bram Moolenaar79648732019-07-18 21:43:07 +02002015 return NULL;
2016 }
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002017#ifdef FEAT_TERMINAL
2018 if (buf->b_term != NULL && popup_terminal_exists())
2019 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002020 emsg(_(e_cannot_open_second_popup_with_terminal));
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002021 return NULL;
2022 }
2023#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002024 }
2025 else if (!(argvars[0].v_type == VAR_STRING
2026 && argvars[0].vval.v_string != NULL)
2027 && !(argvars[0].v_type == VAR_LIST
2028 && argvars[0].vval.v_list != NULL))
2029 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002030 emsg(_(e_buffer_number_text_or_list_required));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002031 return NULL;
2032 }
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002033 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002034 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002035 d = argvars[1].vval.v_dict;
2036 }
2037
2038 if (d != NULL)
2039 {
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01002040 if (dict_has_key(d, "tabpage"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01002041 tabnr = (int)dict_get_number(d, "tabpage");
Bram Moolenaar9198de32022-08-27 21:30:03 +01002042 else if (popup_is_notification(type))
Bram Moolenaar79648732019-07-18 21:43:07 +02002043 tabnr = -1; // notifications are global by default
2044 else
2045 tabnr = 0;
2046 if (tabnr > 0)
2047 {
2048 tp = find_tabpage(tabnr);
2049 if (tp == NULL)
2050 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002051 semsg(_(e_tabpage_not_found_nr), tabnr);
Bram Moolenaar79648732019-07-18 21:43:07 +02002052 return NULL;
2053 }
2054 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002055 }
Bram Moolenaar45690202022-09-26 12:57:11 +01002056 else if (popup_is_notification(type))
2057 tabnr = -1; // show on all tabs
Bram Moolenaara3fce622019-06-20 02:31:49 +02002058
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002059 // Create the window and buffer.
2060 wp = win_alloc_popup_win();
2061 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02002062 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02002063 if (rettv != NULL)
2064 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002065 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002066 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002067
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002068 if (buf != NULL)
2069 {
2070 // use existing buffer
2071 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02002072 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02002073 set_local_options_default(wp, FALSE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002074 swap_exists_action = SEA_READONLY;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002075 buffer_ensure_loaded(buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +01002076 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002077 }
2078 else
2079 {
2080 // create a new buffer associated with the popup
2081 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002082 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002083 if (buf == NULL)
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002084 {
2085 win_free_popup(wp);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002086 return NULL;
Yegappan Lakshmanan0dac1ab2022-04-02 21:46:19 +01002087 }
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002088 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002089
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002090 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02002091
Bram Moolenaar46451042019-08-24 15:50:46 +02002092 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002093 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002094 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002095 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02002096 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002097 buf->b_p_ul = -1; // no undo
2098 buf->b_p_swf = FALSE; // no swap file
2099 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002100 buf->b_locked = TRUE; // prevent deleting the buffer
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002101
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002102 // Avoid that 'buftype' is reset when this buffer is entered.
2103 buf->b_p_initialized = TRUE;
2104 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002105 wp->w_p_wrap = TRUE; // 'wrap' is default on
2106 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002107
Bram Moolenaara3fce622019-06-20 02:31:49 +02002108 if (tp != NULL)
2109 {
2110 // popup on specified tab page
2111 wp->w_next = tp->tp_first_popupwin;
2112 tp->tp_first_popupwin = wp;
2113 }
2114 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002115 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002116 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002117 wp->w_next = curtab->tp_first_popupwin;
2118 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002119 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02002120 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002121 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002122 win_T *prev = first_popupwin;
2123
2124 // Global popup: add at the end, so that it gets displayed on top of
2125 // older ones with the same zindex. Matters for notifications.
2126 if (first_popupwin == NULL)
2127 first_popupwin = wp;
2128 else
2129 {
2130 while (prev->w_next != NULL)
2131 prev = prev->w_next;
2132 prev->w_next = wp;
2133 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002134 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002135
Bram Moolenaar79648732019-07-18 21:43:07 +02002136 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002137 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002138
Bram Moolenaar79648732019-07-18 21:43:07 +02002139 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02002140 {
2141 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02002142 }
2143 if (type == TYPE_ATCURSOR)
2144 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002145 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02002146 set_moved_values(wp);
2147 set_moved_columns(wp, FIND_STRING);
2148 }
2149
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002150 if (type == TYPE_BEVAL)
2151 {
2152 wp->w_popup_pos = POPPOS_BOTLEFT;
2153
2154 // by default use the mouse position
2155 wp->w_wantline = mouse_row;
2156 if (wp->w_wantline <= 0) // mouse on first line
2157 {
2158 wp->w_wantline = 2;
2159 wp->w_popup_pos = POPPOS_TOPLEFT;
2160 }
2161 wp->w_wantcol = mouse_col + 1;
2162 set_mousemoved_values(wp);
2163 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
2164 }
2165
Bram Moolenaar33796b32019-06-08 16:01:13 +02002166 // set default values
2167 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002168 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002169
Bram Moolenaar9198de32022-08-27 21:30:03 +01002170 if (popup_is_notification(type))
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002171 {
2172 win_T *twp, *nextwin;
2173 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002174
2175 // Try to not overlap with another global popup. Guess we need 3
2176 // more screen lines than buffer lines.
2177 wp->w_wantline = 1;
2178 for (twp = first_popupwin; twp != NULL; twp = nextwin)
2179 {
2180 nextwin = twp->w_next;
2181 if (twp != wp
2182 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
2183 && twp->w_winrow <= wp->w_wantline - 1 + height
2184 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
2185 {
2186 // move to below this popup and restart the loop to check for
2187 // overlap with other popups
2188 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
2189 nextwin = first_popupwin;
2190 }
2191 }
2192 if (wp->w_wantline + height > Rows)
2193 {
2194 // can't avoid overlap, put on top in the hope that message goes
2195 // away soon.
2196 wp->w_wantline = 1;
2197 }
2198
2199 wp->w_wantcol = 10;
2200 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002201 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002202 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002203 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002204 for (i = 0; i < 4; ++i)
2205 wp->w_popup_border[i] = 1;
2206 wp->w_popup_padding[1] = 1;
2207 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002208
Bram Moolenaar9198de32022-08-27 21:30:03 +01002209 popup_update_color(wp, type);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002210 }
2211
Bram Moolenaara730e552019-06-16 19:05:31 +02002212 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002213 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02002214 wp->w_popup_pos = POPPOS_CENTER;
2215 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002216 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002217 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002218 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002219 }
2220
Bram Moolenaara730e552019-06-16 19:05:31 +02002221 if (type == TYPE_MENU)
2222 {
2223 typval_T tv;
2224 callback_T callback;
2225
2226 tv.v_type = VAR_STRING;
2227 tv.vval.v_string = (char_u *)"popup_filter_menu";
2228 callback = get_callback(&tv);
2229 if (callback.cb_name != NULL)
2230 set_callback(&wp->w_filter_cb, &callback);
2231
2232 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002233 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002234 }
2235
Bram Moolenaar79648732019-07-18 21:43:07 +02002236 if (type == TYPE_PREVIEW)
2237 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002238 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02002239 wp->w_popup_close = POPCLOSE_BUTTON;
2240 for (i = 0; i < 4; ++i)
2241 wp->w_popup_border[i] = 1;
2242 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01002243 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002244 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002245# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002246 if (type == TYPE_INFO)
2247 {
2248 wp->w_popup_pos = POPPOS_TOPLEFT;
2249 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
2250 wp->w_popup_close = POPCLOSE_BUTTON;
2251 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002252 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02002253 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02002254# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02002255
Bram Moolenaarae943152019-06-16 22:54:14 +02002256 for (i = 0; i < 4; ++i)
2257 VIM_CLEAR(wp->w_border_highlight[i]);
2258 for (i = 0; i < 8; ++i)
2259 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002260 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002261 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002262 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002263
Bram Moolenaar79648732019-07-18 21:43:07 +02002264 if (d != NULL)
2265 // Deal with options.
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002266 apply_options(wp, d, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002267
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002268#ifdef FEAT_TIMERS
Bram Moolenaar9198de32022-08-27 21:30:03 +01002269 if (popup_is_notification(type) && wp->w_popup_timer == NULL)
2270 popup_add_timeout(wp, 3000, type == TYPE_NOTIFICATION);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002271#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002272
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002273 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002274
2275 wp->w_vsep_width = 0;
2276
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002277 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002278 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002279
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002280#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002281 // When running a terminal in the popup it becomes the current window.
2282 if (buf->b_term != NULL)
2283 win_enter(wp, FALSE);
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002284#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002285
Bram Moolenaara730e552019-06-16 19:05:31 +02002286 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002287}
2288
2289/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002290 * popup_clear()
2291 */
2292 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002293f_popup_clear(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002294{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002295 int force = FALSE;
2296
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002297 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2298 return;
2299
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002300 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002301 force = (int)tv_get_bool(&argvars[0]);
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002302 close_all_popups(force);
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002303}
2304
2305/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002306 * popup_create({text}, {options})
2307 */
2308 void
2309f_popup_create(typval_T *argvars, typval_T *rettv)
2310{
Bram Moolenaar17627312019-06-02 19:53:44 +02002311 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002312}
2313
2314/*
2315 * popup_atcursor({text}, {options})
2316 */
2317 void
2318f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2319{
Bram Moolenaar17627312019-06-02 19:53:44 +02002320 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002321}
2322
2323/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002324 * popup_beval({text}, {options})
2325 */
2326 void
2327f_popup_beval(typval_T *argvars, typval_T *rettv)
2328{
2329 popup_create(argvars, rettv, TYPE_BEVAL);
2330}
2331
2332/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002333 * Invoke the close callback for window "wp" with value "result".
2334 * Careful: The callback may make "wp" invalid!
2335 */
2336 static void
2337invoke_popup_callback(win_T *wp, typval_T *result)
2338{
2339 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002340 typval_T argv[3];
2341
2342 argv[0].v_type = VAR_NUMBER;
2343 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2344
2345 if (result != NULL && result->v_type != VAR_UNKNOWN)
2346 copy_tv(result, &argv[1]);
2347 else
2348 {
2349 argv[1].v_type = VAR_NUMBER;
2350 argv[1].vval.v_number = 0;
2351 }
2352
2353 argv[2].v_type = VAR_UNKNOWN;
2354
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002355 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002356 if (result != NULL)
2357 clear_tv(&argv[1]);
2358 clear_tv(&rettv);
2359}
2360
2361/*
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002362 * Make "prevwin" the current window, unless it's equal to "wp".
2363 * Otherwise make "firstwin" the current window.
2364 */
2365 static void
2366back_to_prevwin(win_T *wp)
2367{
2368 if (win_valid(prevwin) && wp != prevwin)
2369 win_enter(prevwin, FALSE);
2370 else
2371 win_enter(firstwin, FALSE);
2372}
2373
2374/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002375 * Close popup "wp" and invoke any close callback for it.
2376 */
2377 static void
2378popup_close_and_callback(win_T *wp, typval_T *arg)
2379{
2380 int id = wp->w_id;
2381
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002382#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002383 if (wp == curwin && curbuf->b_term != NULL)
2384 {
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002385 win_T *owp;
2386
2387 // Closing popup window with a terminal: put focus back on the first
2388 // that works:
2389 // - another popup window with a terminal
2390 // - the previous window
2391 // - the first one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002392 FOR_ALL_POPUPWINS(owp)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002393 if (owp != curwin && owp->w_buffer->b_term != NULL)
2394 break;
2395 if (owp != NULL)
2396 win_enter(owp, FALSE);
2397 else
2398 {
2399 for (owp = curtab->tp_first_popupwin; owp != NULL;
2400 owp = owp->w_next)
2401 if (owp != curwin && owp->w_buffer->b_term != NULL)
2402 break;
2403 if (owp != NULL)
2404 win_enter(owp, FALSE);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002405 else
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002406 back_to_prevwin(wp);
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002407 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002408 }
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01002409#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002410
Bram Moolenaar49540192019-12-11 19:34:54 +01002411 // Just in case a check higher up is missing.
2412 if (wp == curwin && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002413 {
2414 // To avoid getting stuck when win_execute() does something that causes
2415 // an error, stop calling the filter callback.
2416 free_callback(&wp->w_filter_cb);
2417
Bram Moolenaar49540192019-12-11 19:34:54 +01002418 return;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02002419 }
Bram Moolenaar49540192019-12-11 19:34:54 +01002420
Bram Moolenaarcee52202020-03-11 14:19:58 +01002421 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002422 if (wp->w_close_cb.cb_name != NULL)
2423 // Careful: This may make "wp" invalid.
2424 invoke_popup_callback(wp, arg);
2425
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002426 popup_close(id, FALSE);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002427 CHECK_CURBUF;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002428}
2429
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002430 void
Bram Moolenaar12034e22019-08-25 22:25:02 +02002431popup_close_with_retval(win_T *wp, int retval)
2432{
2433 typval_T res;
2434
2435 res.v_type = VAR_NUMBER;
2436 res.vval.v_number = retval;
2437 popup_close_and_callback(wp, &res);
2438}
2439
Bram Moolenaar3397f742019-06-02 18:40:06 +02002440/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002441 * Close popup "wp" because of a mouse click.
2442 */
2443 void
2444popup_close_for_mouse_click(win_T *wp)
2445{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002446 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002447}
2448
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002449 static void
2450check_mouse_moved(win_T *wp, win_T *mouse_wp)
2451{
2452 // Close the popup when all if these are true:
2453 // - the mouse is not on this popup
2454 // - "mousemoved" was used
2455 // - the mouse is no longer on the same screen row or the mouse column is
2456 // outside of the relevant text
2457 if (wp != mouse_wp
2458 && wp->w_popup_mouse_row != 0
2459 && (wp->w_popup_mouse_row != mouse_row
2460 || mouse_col < wp->w_popup_mouse_mincol
2461 || mouse_col > wp->w_popup_mouse_maxcol))
2462 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002463 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002464 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002465 }
2466}
2467
2468/*
2469 * Called when the mouse moved: may close a popup with "mousemoved".
2470 */
2471 void
2472popup_handle_mouse_moved(void)
2473{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002474 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002475 win_T *mouse_wp;
2476 int row = mouse_row;
2477 int col = mouse_col;
2478
2479 // find the window where the mouse is in
2480 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2481
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002482 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2483 {
2484 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002485 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002486 }
2487 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2488 {
2489 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002490 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002491 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002492}
2493
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002494/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002495 * In a filter: check if the typed key is a mouse event that is used for
2496 * dragging the popup.
2497 */
2498 static void
2499filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2500{
2501 int row = mouse_row;
2502 int col = mouse_col;
2503
Bram Moolenaar0b74d002021-11-29 17:38:02 +00002504 if ((wp->w_popup_flags & (POPF_DRAG | POPF_DRAGALL))
Bram Moolenaara730e552019-06-16 19:05:31 +02002505 && is_mouse_key(c)
2506 && (wp == popup_dragwin
2507 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2508 // do not consume the key, allow for dragging the popup
2509 rettv->vval.v_number = 0;
2510}
2511
Bram Moolenaara730e552019-06-16 19:05:31 +02002512/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002513 * popup_filter_menu({id}, {key})
Bram Moolenaara730e552019-06-16 19:05:31 +02002514 */
2515 void
2516f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2517{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002518 int id;
2519 win_T *wp;
2520 char_u *key;
Bram Moolenaara730e552019-06-16 19:05:31 +02002521 typval_T res;
2522 int c;
2523 linenr_T old_lnum;
2524
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002525 if (in_vim9script()
2526 && (check_for_number_arg(argvars, 0) == FAIL
2527 || check_for_string_arg(argvars, 1) == FAIL))
2528 return;
2529
2530 id = tv_get_number(&argvars[0]);
2531 wp = win_id2wp(id);
2532 key = tv_get_string(&argvars[1]);
Bram Moolenaara730e552019-06-16 19:05:31 +02002533 // If the popup has been closed do not consume the key.
2534 if (wp == NULL)
2535 return;
2536
2537 c = *key;
2538 if (c == K_SPECIAL && key[1] != NUL)
2539 c = TO_SPECIAL(key[1], key[2]);
2540
2541 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002542 rettv->v_type = VAR_BOOL;
2543 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002544 res.v_type = VAR_NUMBER;
2545
2546 old_lnum = wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002547 if ((c == 'k' || c == 'K' || c == K_UP || c == Ctrl_P)
2548 && wp->w_cursor.lnum > 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02002549 --wp->w_cursor.lnum;
Bram Moolenaar014f6982021-01-04 13:18:30 +01002550 if ((c == 'j' || c == 'J' || c == K_DOWN || c == Ctrl_N)
Bram Moolenaara730e552019-06-16 19:05:31 +02002551 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2552 ++wp->w_cursor.lnum;
2553 if (old_lnum != wp->w_cursor.lnum)
2554 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002555 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002556 return;
2557 }
2558
2559 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2560 {
2561 // Cancelled, invoke callback with -1
2562 res.vval.v_number = -1;
2563 popup_close_and_callback(wp, &res);
2564 return;
2565 }
2566 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2567 {
2568 // Invoke callback with current index.
2569 res.vval.v_number = wp->w_cursor.lnum;
2570 popup_close_and_callback(wp, &res);
2571 return;
2572 }
2573
2574 filter_handle_drag(wp, c, rettv);
2575}
2576
2577/*
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002578 * popup_filter_yesno({id}, {key})
Bram Moolenaara42d9452019-06-15 21:46:30 +02002579 */
2580 void
2581f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2582{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002583 int id;
2584 win_T *wp;
2585 char_u *key;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002586 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002587 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002588
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002589 if (in_vim9script()
2590 && (check_for_number_arg(argvars, 0) == FAIL
2591 || check_for_string_arg(argvars, 1) == FAIL))
2592 return;
2593
2594 id = tv_get_number(&argvars[0]);
2595 wp = win_id2wp(id);
2596 key = tv_get_string(&argvars[1]);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002597 // If the popup has been closed don't consume the key.
2598 if (wp == NULL)
2599 return;
2600
Bram Moolenaara730e552019-06-16 19:05:31 +02002601 c = *key;
2602 if (c == K_SPECIAL && key[1] != NUL)
2603 c = TO_SPECIAL(key[1], key[2]);
2604
Bram Moolenaara42d9452019-06-15 21:46:30 +02002605 // consume all keys until done
Bram Moolenaar403dc312020-10-17 19:29:51 +02002606 rettv->v_type = VAR_BOOL;
2607 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002608
Bram Moolenaara730e552019-06-16 19:05:31 +02002609 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002610 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002611 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002612 res.vval.v_number = 0;
2613 else
2614 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002615 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002616 return;
2617 }
2618
2619 // Invoke callback
2620 res.v_type = VAR_NUMBER;
2621 popup_close_and_callback(wp, &res);
2622}
2623
2624/*
2625 * popup_dialog({text}, {options})
2626 */
2627 void
2628f_popup_dialog(typval_T *argvars, typval_T *rettv)
2629{
2630 popup_create(argvars, rettv, TYPE_DIALOG);
2631}
2632
2633/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002634 * popup_menu({text}, {options})
2635 */
2636 void
2637f_popup_menu(typval_T *argvars, typval_T *rettv)
2638{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002639 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002640}
2641
2642/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002643 * popup_notification({text}, {options})
2644 */
2645 void
2646f_popup_notification(typval_T *argvars, typval_T *rettv)
2647{
2648 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2649}
2650
2651/*
2652 * Find the popup window with window-ID "id".
2653 * If the popup window does not exist NULL is returned.
2654 * If the window is not a popup window, and error message is given.
2655 */
2656 static win_T *
2657find_popup_win(int id)
2658{
2659 win_T *wp = win_id2wp(id);
2660
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002661 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002662 {
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002663 semsg(_(e_window_nr_is_not_popup_window), id);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002664 return NULL;
2665 }
2666 return wp;
2667}
2668
2669/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002670 * popup_close({id})
2671 */
2672 void
2673f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2674{
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002675 int id;
Bram Moolenaar49540192019-12-11 19:34:54 +01002676 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002677
Yegappan Lakshmanana764e732021-07-25 15:57:32 +02002678 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2679 return;
2680
2681 id = (int)tv_get_number(argvars);
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002682 if (
2683# ifdef FEAT_TERMINAL
2684 // if the popup contains a terminal it will become hidden
2685 curbuf->b_term == NULL &&
2686# endif
2687 ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar49540192019-12-11 19:34:54 +01002688 return;
2689
2690 wp = find_popup_win(id);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002691 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002692 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002693}
2694
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002695 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002696popup_hide(win_T *wp)
2697{
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002698#ifdef FEAT_TERMINAL
2699 if (error_if_term_popup_window())
2700 return;
2701#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002702 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2703 {
2704 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002705 // Do not decrement b_nwindows, we still reference the buffer.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002706 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002707 popup_mask_refresh = TRUE;
2708 }
2709}
2710
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002711/*
2712 * popup_hide({id})
2713 */
2714 void
2715f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2716{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002717 int id;
2718 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002719
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002720 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2721 return;
2722
2723 id = (int)tv_get_number(argvars);
2724 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002725 if (wp != NULL)
Bram Moolenaar27724252022-05-08 15:00:04 +01002726 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002727 popup_hide(wp);
Bram Moolenaar27724252022-05-08 15:00:04 +01002728 wp->w_popup_flags |= POPF_HIDDEN_FORCE;
2729 }
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002730}
2731
2732 void
2733popup_show(win_T *wp)
2734{
2735 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002736 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002737 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002738 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002739 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002740 }
2741}
2742
2743/*
2744 * popup_show({id})
2745 */
2746 void
2747f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2748{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002749 int id;
2750 win_T *wp;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002751
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002752 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
2753 return;
2754
2755 id = (int)tv_get_number(argvars);
2756 wp = find_popup_win(id);
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002757 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002758 {
Bram Moolenaar27724252022-05-08 15:00:04 +01002759 wp->w_popup_flags &= ~POPF_HIDDEN_FORCE;
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002760 popup_show(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002761#ifdef FEAT_QUICKFIX
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002762 if (wp->w_popup_flags & POPF_INFO)
2763 pum_position_info_popup(wp);
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002764#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002765 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002766}
2767
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002768/*
2769 * popup_settext({id}, {text})
2770 */
2771 void
2772f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2773{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002774 int id;
2775 win_T *wp;
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002776
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002777 if (in_vim9script()
2778 && (check_for_number_arg(argvars, 0) == FAIL
2779 || check_for_string_or_list_arg(argvars, 1) == FAIL))
2780 return;
2781
2782 id = (int)tv_get_number(&argvars[0]);
2783 wp = find_popup_win(id);
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002784 if (wp != NULL)
2785 {
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002786 if (check_for_string_or_list_arg(argvars, 1) != FAIL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002787 {
2788 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002789 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002790 popup_adjust_position(wp);
2791 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002792 }
2793}
2794
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002795 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002796popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002797{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002798 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002799 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002800 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002801 clear_cmdline = TRUE;
2802 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002803
Bram Moolenaar43568642022-08-28 13:02:45 +01002804#ifdef HAS_MESSAGE_WINDOW
2805 if (wp == message_win)
2806 message_win = NULL;
2807#endif
2808
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002809 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002810 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002811}
2812
Bram Moolenaar82106932020-03-13 14:34:38 +01002813 static void
2814error_for_popup_window(void)
2815{
Bram Moolenaar11de43d2022-01-06 21:41:11 +00002816 emsg(_(e_not_allowed_in_popup_window));
Bram Moolenaar82106932020-03-13 14:34:38 +01002817}
2818
2819 int
2820error_if_popup_window(int also_with_term UNUSED)
2821{
2822 // win_execute() may set "curwin" to a popup window temporarily, but many
2823 // commands are disallowed then. When a terminal runs in the popup most
2824 // things are allowed. When a terminal is finished it can be closed.
2825 if (WIN_IS_POPUP(curwin)
2826# ifdef FEAT_TERMINAL
2827 && (also_with_term || curbuf->b_term == NULL)
Bram Moolenaar82106932020-03-13 14:34:38 +01002828# endif
2829 )
2830 {
2831 error_for_popup_window();
2832 return TRUE;
2833 }
2834 return FALSE;
2835}
2836
Bram Moolenaarec583842019-05-26 14:11:23 +02002837/*
2838 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002839 * Does not invoke the callback.
Bram Moolenaard502aa42020-05-13 01:04:32 +02002840 * Return OK if the popup was closed, FAIL otherwise.
Bram Moolenaarec583842019-05-26 14:11:23 +02002841 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002842 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002843popup_close(int id, int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002844{
2845 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002846 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002847 win_T *prev = NULL;
2848
Bram Moolenaarec583842019-05-26 14:11:23 +02002849 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002850 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002851 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002852 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002853 if (wp == curwin)
2854 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002855 if (!force)
2856 {
2857 error_for_popup_window();
2858 return FAIL;
2859 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002860 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002861 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002862 if (prev == NULL)
2863 first_popupwin = wp->w_next;
2864 else
2865 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002866 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002867 return OK;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002868 }
2869
Bram Moolenaarec583842019-05-26 14:11:23 +02002870 // go through tab-local popups
2871 FOR_ALL_TABPAGES(tp)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002872 if (popup_close_tabpage(tp, id, force) == OK)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002873 return OK;
2874 return FAIL;
Bram Moolenaarec583842019-05-26 14:11:23 +02002875}
2876
2877/*
2878 * Close a popup window with Window-id "id" in tabpage "tp".
2879 */
Bram Moolenaard502aa42020-05-13 01:04:32 +02002880 int
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002881popup_close_tabpage(tabpage_T *tp, int id, int force)
Bram Moolenaarec583842019-05-26 14:11:23 +02002882{
2883 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002884 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002885 win_T *prev = NULL;
2886
Bram Moolenaarec583842019-05-26 14:11:23 +02002887 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2888 if (wp->w_id == id)
2889 {
Bram Moolenaarcee52202020-03-11 14:19:58 +01002890 if (wp == curwin)
2891 {
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002892 if (!force)
2893 {
2894 error_for_popup_window();
2895 return FAIL;
2896 }
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002897 back_to_prevwin(wp);
Bram Moolenaarcee52202020-03-11 14:19:58 +01002898 }
Bram Moolenaarec583842019-05-26 14:11:23 +02002899 if (prev == NULL)
2900 *root = wp->w_next;
2901 else
2902 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002903 popup_free(wp);
Bram Moolenaard502aa42020-05-13 01:04:32 +02002904 return OK;
Bram Moolenaarec583842019-05-26 14:11:23 +02002905 }
Bram Moolenaard502aa42020-05-13 01:04:32 +02002906 return FAIL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002907}
2908
2909 void
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002910close_all_popups(int force)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002911{
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002912 if (!force && ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002913 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002914 while (first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002915 if (popup_close(first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002916 return;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002917 while (curtab->tp_first_popupwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002918 if (popup_close(curtab->tp_first_popupwin->w_id, force) == FAIL)
Bram Moolenaard502aa42020-05-13 01:04:32 +02002919 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002920}
2921
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002922/*
2923 * popup_move({id}, {options})
2924 */
2925 void
2926f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2927{
Bram Moolenaarae943152019-06-16 22:54:14 +02002928 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002929 int id;
2930 win_T *wp;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002931
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002932 if (in_vim9script()
2933 && (check_for_number_arg(argvars, 0) == FAIL
2934 || check_for_dict_arg(argvars, 1) == FAIL))
2935 return;
2936
2937 id = (int)tv_get_number(argvars);
2938 wp = find_popup_win(id);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002939 if (wp == NULL)
2940 return; // invalid {id}
2941
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002942 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002943 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002944 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002945
Bram Moolenaarae943152019-06-16 22:54:14 +02002946 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002947
2948 if (wp->w_winrow + wp->w_height >= cmdline_row)
2949 clear_cmdline = TRUE;
2950 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002951}
2952
Bram Moolenaarbc133542019-05-29 20:26:46 +02002953/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002954 * popup_setoptions({id}, {options})
2955 */
2956 void
2957f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2958{
2959 dict_T *dict;
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002960 int id;
2961 win_T *wp;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002962 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002963
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02002964 if (in_vim9script()
2965 && (check_for_number_arg(argvars, 0) == FAIL
2966 || check_for_dict_arg(argvars, 1) == FAIL))
2967 return;
2968
2969 id = (int)tv_get_number(argvars);
2970 wp = find_popup_win(id);
Bram Moolenaarae943152019-06-16 22:54:14 +02002971 if (wp == NULL)
2972 return; // invalid {id}
2973
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002974 if (check_for_nonnull_dict_arg(argvars, 1) == FAIL)
Bram Moolenaarae943152019-06-16 22:54:14 +02002975 return;
Bram Moolenaarae943152019-06-16 22:54:14 +02002976 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002977 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002978
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002979 apply_options(wp, dict, FALSE);
Bram Moolenaarae943152019-06-16 22:54:14 +02002980
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002981 if (old_firstline != wp->w_firstline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002982 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaarae943152019-06-16 22:54:14 +02002983 popup_adjust_position(wp);
2984}
2985
2986/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002987 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002988 */
2989 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002990f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002991{
2992 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002993 int id;
2994 win_T *wp;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002995 int top_extra;
2996 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002997
2998 if (rettv_dict_alloc(rettv) == OK)
2999 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003000 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3001 return;
3002
3003 id = (int)tv_get_number(argvars);
3004 wp = find_popup_win(id);
Bram Moolenaarbc133542019-05-29 20:26:46 +02003005 if (wp == NULL)
3006 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003007 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003008 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
3009
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003010 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02003011 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003012 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003013
Bram Moolenaarbc133542019-05-29 20:26:46 +02003014 dict_add_number(dict, "line", wp->w_winrow + 1);
3015 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02003016 dict_add_number(dict, "width", wp->w_width + left_extra
3017 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
3018 dict_add_number(dict, "height", wp->w_height + top_extra
3019 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02003020
3021 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
3022 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
3023 dict_add_number(dict, "core_width", wp->w_width);
3024 dict_add_number(dict, "core_height", wp->w_height);
3025
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003026 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02003027 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01003028 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003029 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02003030 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02003031
3032 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003033 }
3034}
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003035
3036/*
3037 * popup_list()
3038 */
3039 void
3040f_popup_list(typval_T *argvars UNUSED, typval_T *rettv)
3041{
3042 win_T *wp;
3043 tabpage_T *tp;
3044
Bram Moolenaar8088ae92022-06-20 11:38:17 +01003045 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003046 return;
3047 FOR_ALL_POPUPWINS(wp)
3048 list_append_number(rettv->vval.v_list, wp->w_id);
3049 FOR_ALL_TABPAGES(tp)
3050 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
3051 list_append_number(rettv->vval.v_list, wp->w_id);
3052}
3053
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003054/*
3055 * popup_locate({row}, {col})
3056 */
3057 void
3058f_popup_locate(typval_T *argvars, typval_T *rettv)
3059{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003060 int row;
3061 int col;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003062 win_T *wp;
3063
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003064 if (in_vim9script()
3065 && (check_for_number_arg(argvars, 0) == FAIL
3066 || check_for_number_arg(argvars, 1) == FAIL))
3067 return;
3068
3069 row = tv_get_number(&argvars[0]) - 1;
3070 col = tv_get_number(&argvars[1]) - 1;
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003071 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaarfd318112019-11-22 19:22:08 +01003072 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarb4f06282019-07-12 21:07:54 +02003073 rettv->vval.v_number = wp->w_id;
3074}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003075
3076/*
Bram Moolenaarae943152019-06-16 22:54:14 +02003077 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
3078 */
3079 static void
3080get_padding_border(dict_T *dict, int *array, char *name)
3081{
3082 list_T *list;
3083 int i;
3084
3085 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
3086 return;
3087
3088 list = list_alloc();
3089 if (list != NULL)
3090 {
3091 dict_add_list(dict, name, list);
3092 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
3093 for (i = 0; i < 4; ++i)
3094 list_append_number(list, array[i]);
3095 }
3096}
3097
3098/*
3099 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
3100 */
3101 static void
3102get_borderhighlight(dict_T *dict, win_T *wp)
3103{
3104 list_T *list;
3105 int i;
3106
3107 for (i = 0; i < 4; ++i)
3108 if (wp->w_border_highlight[i] != NULL)
3109 break;
3110 if (i == 4)
3111 return;
3112
3113 list = list_alloc();
3114 if (list != NULL)
3115 {
3116 dict_add_list(dict, "borderhighlight", list);
3117 for (i = 0; i < 4; ++i)
3118 list_append_string(list, wp->w_border_highlight[i], -1);
3119 }
3120}
3121
3122/*
3123 * For popup_getoptions(): add a "borderchars" entry to "dict".
3124 */
3125 static void
3126get_borderchars(dict_T *dict, win_T *wp)
3127{
3128 list_T *list;
3129 int i;
3130 char_u buf[NUMBUFLEN];
3131 int len;
3132
3133 for (i = 0; i < 8; ++i)
3134 if (wp->w_border_char[i] != 0)
3135 break;
3136 if (i == 8)
3137 return;
3138
3139 list = list_alloc();
3140 if (list != NULL)
3141 {
3142 dict_add_list(dict, "borderchars", list);
3143 for (i = 0; i < 8; ++i)
3144 {
3145 len = mb_char2bytes(wp->w_border_char[i], buf);
3146 list_append_string(list, buf, len);
3147 }
3148 }
3149}
3150
3151/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003152 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02003153 */
3154 static void
3155get_moved_list(dict_T *dict, win_T *wp)
3156{
3157 list_T *list;
3158
3159 list = list_alloc();
3160 if (list != NULL)
3161 {
3162 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003163 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02003164 list_append_number(list, wp->w_popup_mincol);
3165 list_append_number(list, wp->w_popup_maxcol);
3166 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02003167 list = list_alloc();
3168 if (list != NULL)
3169 {
3170 dict_add_list(dict, "mousemoved", list);
3171 list_append_number(list, wp->w_popup_mouse_row);
3172 list_append_number(list, wp->w_popup_mouse_mincol);
3173 list_append_number(list, wp->w_popup_mouse_maxcol);
3174 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003175}
3176
3177/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02003178 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003179 */
3180 void
3181f_popup_getoptions(typval_T *argvars, typval_T *rettv)
3182{
3183 dict_T *dict;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003184 int id;
3185 win_T *wp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003186 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003187 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003188
3189 if (rettv_dict_alloc(rettv) == OK)
3190 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003191 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
3192 return;
3193
3194 id = (int)tv_get_number(argvars);
3195 wp = find_popup_win(id);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003196 if (wp == NULL)
3197 return;
3198
3199 dict = rettv->vval.v_dict;
3200 dict_add_number(dict, "line", wp->w_wantline);
3201 dict_add_number(dict, "col", wp->w_wantcol);
3202 dict_add_number(dict, "minwidth", wp->w_minwidth);
3203 dict_add_number(dict, "minheight", wp->w_minheight);
3204 dict_add_number(dict, "maxheight", wp->w_maxheight);
3205 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02003206 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003207 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003208 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02003209 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar65026432021-02-06 14:22:32 +01003210 if (wp->w_popup_prop_type && win_valid_any_tab(wp->w_popup_prop_win))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003211 {
3212 proptype_T *pt = text_prop_type_by_id(
3213 wp->w_popup_prop_win->w_buffer,
3214 wp->w_popup_prop_type);
3215
3216 if (pt != NULL)
3217 dict_add_string(dict, "textprop", pt->pt_name);
3218 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
3219 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
3220 }
Bram Moolenaarae943152019-06-16 22:54:14 +02003221 dict_add_string(dict, "title", wp->w_popup_title);
3222 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003223 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar0b74d002021-11-29 17:38:02 +00003224 dict_add_number(dict, "dragall",
3225 (wp->w_popup_flags & POPF_DRAGALL) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003226 dict_add_number(dict, "mapping",
3227 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003228 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003229 dict_add_number(dict, "posinvert",
3230 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02003231 dict_add_number(dict, "cursorline",
3232 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02003233 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003234 if (wp->w_scrollbar_highlight != NULL)
3235 dict_add_string(dict, "scrollbarhighlight",
3236 wp->w_scrollbar_highlight);
3237 if (wp->w_thumb_highlight != NULL)
3238 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02003239
Bram Moolenaara3fce622019-06-20 02:31:49 +02003240 // find the tabpage that holds this popup
3241 i = 1;
3242 FOR_ALL_TABPAGES(tp)
3243 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02003244 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003245
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003246 FOR_ALL_POPUPWINS_IN_TAB(tp, twp)
3247 if (twp->w_id == id)
3248 break;
3249 if (twp != NULL)
3250 break;
3251 ++i;
Bram Moolenaara3fce622019-06-20 02:31:49 +02003252 }
3253 if (tp == NULL)
3254 i = -1; // must be global
3255 else if (tp == curtab)
3256 i = 0;
3257 dict_add_number(dict, "tabpage", i);
3258
Bram Moolenaarae943152019-06-16 22:54:14 +02003259 get_padding_border(dict, wp->w_popup_padding, "padding");
3260 get_padding_border(dict, wp->w_popup_border, "border");
3261 get_borderhighlight(dict, wp);
3262 get_borderchars(dict, wp);
3263 get_moved_list(dict, wp);
3264
3265 if (wp->w_filter_cb.cb_name != NULL)
3266 dict_add_callback(dict, "filter", &wp->w_filter_cb);
3267 if (wp->w_close_cb.cb_name != NULL)
3268 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003269
K.Takataeeec2542021-06-02 13:28:16 +02003270 for (i = 0; i < (int)ARRAY_LENGTH(poppos_entries); ++i)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02003271 if (wp->w_popup_pos == poppos_entries[i].pp_val)
3272 {
3273 dict_add_string(dict, "pos",
3274 (char_u *)poppos_entries[i].pp_name);
3275 break;
3276 }
3277
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003278 dict_add_string(dict, "close", (char_u *)(
3279 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
3280 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
3281
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02003282# if defined(FEAT_TIMERS)
3283 dict_add_number(dict, "time", wp->w_popup_timer != NULL
3284 ? (long)wp->w_popup_timer->tr_interval : 0L);
3285# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02003286 }
3287}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02003288
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003289# if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003290/*
3291 * Return TRUE if the current window is running a terminal in a popup window.
3292 * Return FALSE when the job has ended.
3293 */
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003294 int
3295error_if_term_popup_window()
3296{
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003297 if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL
3298 && term_job_running(curbuf->b_term))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003299 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00003300 emsg(_(e_not_allowed_for_terminal_in_popup_window));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003301 return TRUE;
3302 }
3303 return FALSE;
3304}
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003305# endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003306
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003307/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003308 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02003309 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003310 * Each calling function should use a different flag, see the list at
3311 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003312 */
3313 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003314popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003315{
3316 win_T *wp;
3317
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003318 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003319 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003320 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003321 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003322}
3323
3324/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003325 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003326 * Must have called popup_reset_handled() first.
3327 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
3328 * popup with the highest zindex.
3329 */
3330 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003331find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003332{
3333 win_T *wp;
3334 win_T *found_wp;
3335 int found_zindex;
3336
3337 found_zindex = lowest ? INT_MAX : 0;
3338 found_wp = NULL;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003339 FOR_ALL_POPUPWINS(wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003340 if ((wp->w_popup_handled & handled_flag) == 0
3341 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003342 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003343 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003344 {
3345 found_zindex = wp->w_zindex;
3346 found_wp = wp;
3347 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003348 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003349 if ((wp->w_popup_handled & handled_flag) == 0
3350 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003351 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003352 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003353 {
3354 found_zindex = wp->w_zindex;
3355 found_wp = wp;
3356 }
3357
3358 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003359 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003360 return found_wp;
3361}
3362
3363/*
3364 * Invoke the filter callback for window "wp" with typed character "c".
3365 * Uses the global "mod_mask" for modifiers.
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003366 * Returns the return value of the filter or -1 for CTRL-C in the current
3367 * window.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003368 * Careful: The filter may make "wp" invalid!
3369 */
3370 static int
3371invoke_popup_filter(win_T *wp, int c)
3372{
3373 int res;
3374 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003375 typval_T argv[3];
3376 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003377 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaar371806e2020-10-22 13:44:54 +02003378 int prev_did_emsg = did_emsg;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003379
Bram Moolenaara42d9452019-06-15 21:46:30 +02003380 // Emergency exit: CTRL-C closes the popup.
3381 if (c == Ctrl_C)
3382 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003383 int save_got_int = got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003384 int was_curwin = wp == curwin;
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003385
3386 // Reset got_int to avoid the callback isn't called.
3387 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003388 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02003389 got_int |= save_got_int;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003390
3391 // If the popup is the current window it probably fails to close. Then
3392 // do not consume the key.
3393 if (was_curwin && wp == curwin)
3394 return -1;
3395 return TRUE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003396 }
3397
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003398 argv[0].v_type = VAR_NUMBER;
3399 argv[0].vval.v_number = (varnumber_T)wp->w_id;
3400
3401 // Convert the number to a string, so that the function can use:
3402 // if a:c == "\<F2>"
Bram Moolenaarec084d32020-02-28 22:44:47 +01003403 buf[special_to_buf(c, mod_mask, FALSE, buf)] = NUL;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003404 argv[1].v_type = VAR_STRING;
3405 argv[1].vval.v_string = vim_strsave(buf);
3406
3407 argv[2].v_type = VAR_UNKNOWN;
3408
Bram Moolenaar638a4a72019-11-06 19:25:22 +01003409 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003410 if (call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv) == FAIL)
3411 {
3412 // Cannot call the function, close the popup to avoid that the filter
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003413 // eats keys and the user is stuck. Might as well eat the key.
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003414 popup_close_with_retval(wp, -1);
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003415 res = TRUE;
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003416 }
3417 else
3418 {
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003419 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
3420 popup_highlight_curline(wp);
3421
Bram Moolenaar371806e2020-10-22 13:44:54 +02003422 // If an error message was given always return FALSE, so that keys are
3423 // not consumed and the user can type something.
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003424 // If we get three errors in a row then close the popup. Decrement the
3425 // error count by 1/10 if there are no errors, thus allowing up to 1 in
3426 // 10 calls to cause an error.
Bram Moolenaar371806e2020-10-22 13:44:54 +02003427 if (win_valid_popup(wp) && did_emsg > prev_did_emsg)
Bram Moolenaar8e9be202020-09-08 22:55:26 +02003428 {
3429 wp->w_filter_errors += 10;
3430 if (wp->w_filter_errors >= 30)
3431 popup_close_with_retval(wp, -1);
3432 res = FALSE;
3433 }
3434 else
3435 {
3436 if (win_valid_popup(wp) && wp->w_filter_errors > 0)
3437 --wp->w_filter_errors;
3438 res = tv_get_bool(&rettv);
3439 }
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003440 }
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01003441
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003442 vim_free(argv[1].vval.v_string);
3443 clear_tv(&rettv);
3444 return res;
3445}
3446
3447/*
3448 * Called when "c" was typed: invoke popup filter callbacks.
3449 * Returns TRUE when the character was consumed,
3450 */
3451 int
3452popup_do_filter(int c)
3453{
Bram Moolenaar934470e2019-09-01 23:27:05 +02003454 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003455 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02003456 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003457 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02003458 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003459 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02003460
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003461#ifdef FEAT_TERMINAL
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003462 // Popup window with terminal always gets focus.
3463 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
3464 return FALSE;
Bram Moolenaar91cd59a2020-02-01 22:18:37 +01003465#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003466
Bram Moolenaar934470e2019-09-01 23:27:05 +02003467 if (recursive)
3468 return FALSE;
3469 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003470
Bram Moolenaarf6396232019-08-24 19:36:00 +02003471 if (c == K_LEFTMOUSE)
3472 {
3473 int row = mouse_row;
3474 int col = mouse_col;
3475
3476 wp = mouse_find_win(&row, &col, FIND_POPUP);
3477 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02003478 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02003479 }
3480
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003481 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02003482 state = get_real_state();
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003483 while (res == FALSE
3484 && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02003485 if (wp->w_filter_cb.cb_name != NULL
3486 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003487 res = invoke_popup_filter(wp, c);
3488
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02003489 if (must_redraw > was_must_redraw)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003490 {
3491 int save_got_int = got_int;
3492
3493 // Reset got_int to avoid a function used in the statusline aborts.
3494 got_int = FALSE;
Bram Moolenaare5050712021-12-09 10:51:05 +00003495 redraw_after_callback(FALSE, FALSE);
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003496 got_int |= save_got_int;
3497 }
Bram Moolenaar934470e2019-09-01 23:27:05 +02003498 recursive = FALSE;
3499 KeyTyped = save_KeyTyped;
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02003500
3501 // When interrupted return FALSE to avoid looping.
3502 return res == -1 ? FALSE : res;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02003503}
3504
Bram Moolenaar3397f742019-06-02 18:40:06 +02003505/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003506 * Return TRUE if there is a popup visible with a filter callback and the
3507 * "mapping" property off.
3508 */
3509 int
3510popup_no_mapping(void)
3511{
3512 int round;
3513 win_T *wp;
3514
3515 for (round = 1; round <= 2; ++round)
3516 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3517 wp != NULL; wp = wp->w_next)
3518 if (wp->w_filter_cb.cb_name != NULL
3519 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3520 return TRUE;
3521 return FALSE;
3522}
3523
3524/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003525 * Called when the cursor moved: check if any popup needs to be closed if the
3526 * cursor moved far enough.
3527 */
3528 void
3529popup_check_cursor_pos()
3530{
3531 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003532
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003533 popup_reset_handled(POPUP_HANDLED_3);
3534 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003535 if (wp->w_popup_curwin != NULL
3536 && (curwin != wp->w_popup_curwin
3537 || curwin->w_cursor.lnum != wp->w_popup_lnum
3538 || curwin->w_cursor.col < wp->w_popup_mincol
3539 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003540 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003541}
3542
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003543/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003544 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003545 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003546 static void
3547popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003548{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003549 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003550 char_u *cells;
3551 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003552
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003553 if (wp->w_popup_mask == NULL || width == 0 || height == 0)
3554 {
3555 vim_free(wp->w_popup_mask_cells);
3556 wp->w_popup_mask_cells = NULL;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003557 return;
Bram Moolenaar10ccfb22021-02-13 21:31:18 +01003558 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003559 if (wp->w_popup_mask_cells != NULL
3560 && wp->w_popup_mask_height == height
3561 && wp->w_popup_mask_width == width)
3562 return; // cache is still valid
3563
3564 vim_free(wp->w_popup_mask_cells);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003565 wp->w_popup_mask_cells = alloc_clear((size_t)width * height);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003566 if (wp->w_popup_mask_cells == NULL)
3567 return;
3568 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003569
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003570 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003571 {
3572 int cols, cole;
3573 int lines, linee;
3574
3575 li = lio->li_tv.vval.v_list->lv_first;
3576 cols = tv_get_number(&li->li_tv);
3577 if (cols < 0)
3578 cols = width + cols + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003579 if (cols <= 0)
3580 cols = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003581 li = li->li_next;
3582 cole = tv_get_number(&li->li_tv);
3583 if (cole < 0)
3584 cole = width + cole + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003585 if (cole > width)
3586 cole = width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003587 li = li->li_next;
3588 lines = tv_get_number(&li->li_tv);
3589 if (lines < 0)
3590 lines = height + lines + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003591 if (lines <= 0)
3592 lines = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003593 li = li->li_next;
3594 linee = tv_get_number(&li->li_tv);
3595 if (linee < 0)
3596 linee = height + linee + 1;
Bram Moolenaar4012d262020-12-29 11:57:46 +01003597 if (linee > height)
3598 linee = height;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003599
Bram Moolenaar4012d262020-12-29 11:57:46 +01003600 for (row = lines - 1; row < linee; ++row)
3601 for (col = cols - 1; col < cole; ++col)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003602 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003603 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003604}
3605
3606/*
3607 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3608 * "col" and "line" are screen coordinates.
3609 */
3610 static int
3611popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3612{
3613 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3614 int line = screenline - wp->w_winrow;
3615
3616 return col >= 0 && col < width
3617 && line >= 0 && line < height
3618 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003619}
3620
3621/*
3622 * Set flags in popup_transparent[] for window "wp" to "val".
3623 */
3624 static void
3625update_popup_transparent(win_T *wp, int val)
3626{
3627 if (wp->w_popup_mask != NULL)
3628 {
3629 int width = popup_width(wp);
3630 int height = popup_height(wp);
3631 listitem_T *lio, *li;
3632 int cols, cole;
3633 int lines, linee;
3634 int col, line;
3635
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003636 FOR_ALL_LIST_ITEMS(wp->w_popup_mask, lio)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003637 {
3638 li = lio->li_tv.vval.v_list->lv_first;
3639 cols = tv_get_number(&li->li_tv);
3640 if (cols < 0)
3641 cols = width + cols + 1;
3642 li = li->li_next;
3643 cole = tv_get_number(&li->li_tv);
3644 if (cole < 0)
3645 cole = width + cole + 1;
3646 li = li->li_next;
3647 lines = tv_get_number(&li->li_tv);
3648 if (lines < 0)
3649 lines = height + lines + 1;
3650 li = li->li_next;
3651 linee = tv_get_number(&li->li_tv);
3652 if (linee < 0)
3653 linee = height + linee + 1;
3654
3655 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003656 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003657 if (cols < 0)
3658 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003659 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003660 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003661 if (lines < 0)
3662 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003663 for (line = lines; line < linee
3664 && line + wp->w_winrow < screen_Rows; ++line)
3665 for (col = cols; col < cole
3666 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003667 popup_transparent[(line + wp->w_winrow) * screen_Columns
3668 + col + wp->w_wincol] = val;
3669 }
3670 }
3671}
3672
3673/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003674 * Only called when popup window "wp" is hidden: If the window is positioned
3675 * next to a text property, and it is now visible, then unhide the popup.
3676 * We don't check if visible popups become hidden, that is done in
3677 * popup_adjust_position().
3678 * Return TRUE if the popup became unhidden.
3679 */
3680 static int
3681check_popup_unhidden(win_T *wp)
3682{
3683 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3684 {
3685 textprop_T prop;
3686 linenr_T lnum;
3687
Bram Moolenaar27724252022-05-08 15:00:04 +01003688 if ((wp->w_popup_flags & POPF_HIDDEN_FORCE) == 0
3689 && find_visible_prop(wp->w_popup_prop_win,
3690 wp->w_popup_prop_type, wp->w_popup_prop_id,
Bram Moolenaar12034e22019-08-25 22:25:02 +02003691 &prop, &lnum) == OK)
3692 {
3693 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003694 wp->w_popup_prop_topline = 0; // force repositioning
3695 return TRUE;
3696 }
3697 }
3698 return FALSE;
3699}
3700
3701/*
3702 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3703 * That is when the buffer in the popup was changed, or the popup is following
3704 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003705 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003706 */
3707 static int
3708popup_need_position_adjust(win_T *wp)
3709{
3710 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3711 return TRUE;
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003712 if (win_valid(wp->w_popup_prop_win)
3713 && (wp->w_popup_prop_changedtick
3714 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3715 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline))
3716 return TRUE;
3717
3718 // May need to adjust the width if the cursor moved.
3719 return wp->w_cursor.lnum != wp->w_popup_last_curline;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003720}
3721
3722/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003723 * Update "popup_mask" if needed.
3724 * Also recomputes the popup size and positions.
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003725 * Also updates "popup_visible" and "popup_uses_mouse_move".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003726 * Also marks window lines for redrawing.
3727 */
3728 void
3729may_update_popup_mask(int type)
3730{
3731 win_T *wp;
3732 short *mask;
3733 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003734 int redraw_all_popups = FALSE;
3735 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003736
3737 // Need to recompute when switching tabs.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003738 // Also recompute when the type is UPD_CLEAR or UPD_NOT_VALID, something
3739 // basic (such as the screen size) must have changed.
3740 if (popup_mask_tab != curtab || type >= UPD_NOT_VALID)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003741 {
3742 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003743 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003744 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003745
3746 // Check if any popup window buffer has changed and if any popup connected
3747 // to a text property has become visible.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003748 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003749 if (wp->w_popup_flags & POPF_HIDDEN)
3750 popup_mask_refresh |= check_popup_unhidden(wp);
3751 else if (popup_need_position_adjust(wp))
3752 popup_mask_refresh = TRUE;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003753 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003754 if (wp->w_popup_flags & POPF_HIDDEN)
3755 popup_mask_refresh |= check_popup_unhidden(wp);
3756 else if (popup_need_position_adjust(wp))
3757 popup_mask_refresh = TRUE;
3758
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003759 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003760 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003761
3762 // Need to update the mask, something has changed.
3763 popup_mask_refresh = FALSE;
3764 popup_mask_tab = curtab;
3765 popup_visible = FALSE;
3766
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003767 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003768 // If redrawing only what is needed, update "popup_mask_next" and then
3769 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003770 redrawing_all_win = TRUE;
3771 FOR_ALL_WINDOWS(wp)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003772 if (wp->w_redr_type < UPD_SOME_VALID)
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003773 redrawing_all_win = FALSE;
3774 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003775 mask = popup_mask;
3776 else
3777 mask = popup_mask_next;
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00003778 vim_memset(mask, 0, (size_t)screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003779
3780 // Find the window with the lowest zindex that hasn't been handled yet,
3781 // so that the window with a higher zindex overwrites the value in
3782 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003783 popup_reset_handled(POPUP_HANDLED_4);
3784 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003785 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003786 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003787 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003788
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003789 popup_visible = TRUE;
3790
Bram Moolenaar12034e22019-08-25 22:25:02 +02003791 // Recompute the position if the text changed. It may make the popup
3792 // hidden if it's attach to a text property that is no longer visible.
3793 if (redraw_all_popups || popup_need_position_adjust(wp))
3794 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003795 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003796 if (wp->w_popup_flags & POPF_HIDDEN)
3797 continue;
3798 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003799
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003800 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003801 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003802 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003803 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003804 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003805 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003806 col < wp->w_wincol + width - wp->w_popup_leftoff
3807 && col < screen_Columns; ++col)
Bakudankun65555002021-11-17 20:40:16 +00003808 if (wp->w_zindex < POPUPMENU_ZINDEX
3809 && pum_visible()
3810 && pum_under_menu(line, col, FALSE))
3811 mask[line * screen_Columns + col] = POPUPMENU_ZINDEX;
3812 else if (wp->w_popup_mask_cells == NULL
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003813 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003814 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003815 }
3816
3817 // Only check which lines are to be updated if not already
3818 // updating all lines.
3819 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003820 {
3821 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3822 win_T *prev_wp = NULL;
3823
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003824 for (line = 0; line < screen_Rows; ++line)
3825 {
3826 int col_done = 0;
3827
3828 for (col = 0; col < screen_Columns; ++col)
3829 {
3830 int off = line * screen_Columns + col;
3831
3832 if (popup_mask[off] != popup_mask_next[off])
3833 {
3834 popup_mask[off] = popup_mask_next[off];
3835
3836 if (line >= cmdline_row)
3837 {
3838 // the command line needs to be cleared if text below
3839 // the popup is now visible.
3840 if (!msg_scrolled && popup_mask_next[off] == 0)
3841 clear_cmdline = TRUE;
3842 }
3843 else if (col >= col_done)
3844 {
3845 linenr_T lnum;
3846 int line_cp = line;
3847 int col_cp = col;
3848
3849 // The screen position "line" / "col" needs to be
3850 // redrawn. Figure out what window that is and update
3851 // w_redraw_top and w_redr_bot. Only needs to be done
3852 // once for each window line.
3853 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3854 if (wp != NULL)
3855 {
Bram Moolenaar83e74502020-07-12 20:44:37 +02003856#if defined(FEAT_TERMINAL)
Bram Moolenaar35910f22020-07-12 19:24:10 +02003857 // A terminal window needs to be redrawn.
3858 if (bt_terminal(wp->w_buffer))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003859 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003860 else
Bram Moolenaar83e74502020-07-12 20:44:37 +02003861#endif
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003862 {
Bram Moolenaar35910f22020-07-12 19:24:10 +02003863 if (wp != prev_wp)
3864 {
3865 vim_memset(plines_cache, 0,
3866 sizeof(int) * Rows);
3867 prev_wp = wp;
3868 }
3869
3870 if (line_cp >= wp->w_height)
3871 // In (or below) status line
3872 wp->w_redr_status = TRUE;
3873 else
3874 {
3875 // compute the position in the buffer line
3876 // from the position in the window
Bram Moolenaar977525f2022-03-15 10:22:39 +00003877 (void)mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003878 &lnum, plines_cache);
Bram Moolenaar35910f22020-07-12 19:24:10 +02003879 redrawWinline(wp, lnum);
3880 }
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003881 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003882
3883 // This line is going to be redrawn, no need to
3884 // check until the right side of the window.
3885 col_done = wp->w_wincol + wp->w_width - 1;
3886 }
3887 }
3888 }
3889 }
3890 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003891
3892 vim_free(plines_cache);
3893 }
Bram Moolenaarf8e43f62022-03-24 15:15:15 +00003894
3895 update_popup_uses_mouse_move();
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003896}
3897
3898/*
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02003899 * If the current window is a popup and something relevant changed, recompute
3900 * the position and size.
3901 */
3902 void
3903may_update_popup_position(void)
3904{
3905 if (popup_is_popup(curwin) && popup_need_position_adjust(curwin))
3906 popup_adjust_position(curwin);
3907}
3908
3909/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003910 * Return a string of "len" spaces in IObuff.
3911 */
3912 static char_u *
3913get_spaces(int len)
3914{
3915 vim_memset(IObuff, ' ', (size_t)len);
3916 IObuff[len] = NUL;
3917 return IObuff;
3918}
3919
3920/*
3921 * Update popup windows. They are drawn on top of normal windows.
3922 * "win_update" is called for each popup window, lowest zindex first.
3923 */
3924 void
3925update_popups(void (*win_update)(win_T *wp))
3926{
3927 win_T *wp;
3928 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003929 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003930 int total_width;
3931 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003932 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003933 int popup_attr;
3934 int border_attr[4];
3935 int border_char[8];
3936 char_u buf[MB_MAXBYTES];
3937 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003938 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003939 int padcol = 0;
Bram Moolenaard91467f2020-11-21 12:42:09 +01003940 int padendcol = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003941 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003942 int sb_thumb_top = 0;
3943 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003944 int attr_scroll = 0;
3945 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003946
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003947 // hide the cursor until redrawing is done.
3948 cursor_off();
3949
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003950 // Find the window with the lowest zindex that hasn't been updated yet,
3951 // so that the window with a higher zindex is drawn later, thus goes on
3952 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003953 popup_reset_handled(POPUP_HANDLED_5);
3954 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003955 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01003956 int title_len = 0;
3957 int title_wincol;
3958
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003959 // This drawing uses the zindex of the popup window, so that it's on
3960 // top of the text but doesn't draw when another popup with higher
3961 // zindex is on top of the character.
3962 screen_zindex = wp->w_zindex;
3963
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003964 // Set flags in popup_transparent[] for masked cells.
3965 update_popup_transparent(wp, 1);
3966
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003967 // adjust w_winrow and w_wincol for border and padding, since
3968 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003969 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003970 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3971 - wp->w_popup_leftoff;
3972 if (wp->w_wincol + left_extra < 0)
3973 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003974 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003975 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003976
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003977 // Draw the popup text, unless it's off screen.
3978 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003979 {
Bram Moolenaar10476522020-09-24 22:57:31 +02003980 // May need to update the "cursorline" highlighting, which may also
3981 // change "topline"
3982 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
3983 popup_highlight_curline(wp);
3984
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003985 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003986
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003987 // move the cursor into the visible lines, otherwise executing
3988 // commands with win_execute() may cause the text to jump.
3989 if (wp->w_cursor.lnum < wp->w_topline)
3990 wp->w_cursor.lnum = wp->w_topline;
3991 else if (wp->w_cursor.lnum >= wp->w_botline)
3992 wp->w_cursor.lnum = wp->w_botline - 1;
3993 }
3994
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003995 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003996 wp->w_wincol -= left_extra;
Bram Moolenaar6a076442020-11-15 20:32:58 +01003997
3998 // Add offset for border and padding if not done already.
3999 if ((wp->w_flags & WFLAG_WCOL_OFF_ADDED) == 0)
4000 {
4001 wp->w_wcol += left_extra;
4002 wp->w_flags |= WFLAG_WCOL_OFF_ADDED;
4003 }
4004 if ((wp->w_flags & WFLAG_WROW_OFF_ADDED) == 0)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004005 {
4006 wp->w_wrow += top_off;
Bram Moolenaar6a076442020-11-15 20:32:58 +01004007 wp->w_flags |= WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01004008 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004009
Bram Moolenaareabddc42022-04-02 15:32:16 +01004010 total_width = popup_width(wp) - wp->w_popup_rightoff;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004011 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004012 popup_attr = get_wcr_attr(wp);
4013
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02004014 if (wp->w_winrow + total_height > cmdline_row)
4015 wp->w_popup_flags |= POPF_ON_CMDLINE;
4016 else
4017 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
4018
4019
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004020 // We can only use these line drawing characters when 'encoding' is
4021 // "utf-8" and 'ambiwidth' is "single".
4022 if (enc_utf8 && *p_ambw == 's')
4023 {
4024 border_char[0] = border_char[2] = 0x2550;
4025 border_char[1] = border_char[3] = 0x2551;
4026 border_char[4] = 0x2554;
4027 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004028 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
4029 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004030 border_char[7] = 0x255a;
4031 }
4032 else
4033 {
4034 border_char[0] = border_char[2] = '-';
4035 border_char[1] = border_char[3] = '|';
4036 for (i = 4; i < 8; ++i)
4037 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02004038 if (wp->w_popup_flags & POPF_RESIZE)
4039 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004040 }
4041 for (i = 0; i < 8; ++i)
4042 if (wp->w_border_char[i] != 0)
4043 border_char[i] = wp->w_border_char[i];
4044
4045 for (i = 0; i < 4; ++i)
4046 {
4047 border_attr[i] = popup_attr;
4048 if (wp->w_border_highlight[i] != NULL)
4049 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
4050 }
4051
Bram Moolenaard91467f2020-11-21 12:42:09 +01004052 // Title goes on top of border or padding.
4053 title_wincol = wp->w_wincol + 1;
4054 if (wp->w_popup_title != NULL)
4055 {
rbtnnc6119412021-08-07 13:08:45 +02004056 title_len = vim_strsize(wp->w_popup_title);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004057
Ralf Schandlbc869872021-05-28 14:12:14 +02004058 // truncate the title if too long
Bram Moolenaard91467f2020-11-21 12:42:09 +01004059 if (title_len > total_width - 2)
Ralf Schandlbc869872021-05-28 14:12:14 +02004060 {
4061 int title_byte_len = (int)STRLEN(wp->w_popup_title);
4062 char_u *title_text = alloc(title_byte_len + 1);
4063
4064 if (title_text != NULL)
4065 {
4066 trunc_string(wp->w_popup_title, title_text,
4067 total_width - 2, title_byte_len + 1);
4068 screen_puts(title_text, wp->w_winrow, title_wincol,
4069 wp->w_popup_border[0] > 0
4070 ? border_attr[0] : popup_attr);
4071 vim_free(title_text);
4072 }
4073
Bram Moolenaard91467f2020-11-21 12:42:09 +01004074 title_len = total_width - 2;
Ralf Schandlbc869872021-05-28 14:12:14 +02004075 }
4076 else
4077 screen_puts(wp->w_popup_title, wp->w_winrow, title_wincol,
4078 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaard91467f2020-11-21 12:42:09 +01004079 }
4080
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004081 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004082 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004083 if (wp->w_popup_border[0] > 0)
4084 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004085 // top border; do not draw over the title
4086 if (title_len > 0)
4087 {
4088 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4089 wincol < 0 ? 0 : wincol, title_wincol,
4090 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004091 ? border_char[4] : border_char[0],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004092 border_char[0], border_attr[0]);
4093 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4094 title_wincol + title_len, wincol + total_width,
4095 border_char[0], border_char[0], border_attr[0]);
4096 }
4097 else
4098 {
4099 screen_fill(wp->w_winrow, wp->w_winrow + 1,
4100 wincol < 0 ? 0 : wincol, wincol + total_width,
4101 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
4102 ? border_char[4] : border_char[0],
4103 border_char[0], border_attr[0]);
4104 }
Bram Moolenaareabddc42022-04-02 15:32:16 +01004105 if (wp->w_popup_border[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004106 {
4107 buf[mb_char2bytes(border_char[5], buf)] = NUL;
4108 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004109 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004110 }
4111 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004112 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
4113 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004114
Bram Moolenaard529ba52019-07-02 23:13:53 +02004115 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
4116 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004117 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard91467f2020-11-21 12:42:09 +01004118 padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004119 - wp->w_has_scrollbar;
4120 if (padcol < 0)
4121 {
Bram Moolenaard91467f2020-11-21 12:42:09 +01004122 padendcol += padcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004123 padcol = 0;
4124 }
4125 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004126 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004127 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004128 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004129 if (title_len > 0 && row == wp->w_winrow)
Bram Moolenaard91467f2020-11-21 12:42:09 +01004130 {
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004131 // top padding and no border; do not draw over the title
4132 screen_fill(row, row + 1, padcol, title_wincol,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004133 ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004134 screen_fill(row, row + 1, title_wincol + title_len,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004135 padendcol, ' ', ' ', popup_attr);
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004136 row += 1;
4137 top_padding -= 1;
Bram Moolenaard91467f2020-11-21 12:42:09 +01004138 }
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01004139 screen_fill(row, row + top_padding, padcol, padendcol,
Bram Moolenaard91467f2020-11-21 12:42:09 +01004140 ' ', ' ', popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02004141 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004142
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004143 // Compute scrollbar thumb position and size.
4144 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004145 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02004146 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
4147 int height = wp->w_height;
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004148 int last;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004149
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004150 sb_thumb_height = ((linenr_T)height * height + linecount / 2)
4151 / linecount;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004152 if (wp->w_topline > 1 && sb_thumb_height == height)
4153 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004154 if (sb_thumb_height == 0)
4155 sb_thumb_height = 1;
Bram Moolenaareabddc42022-04-02 15:32:16 +01004156 if (linecount <= wp->w_height || wp->w_height == 0)
Bram Moolenaar437a7462019-07-05 20:17:22 +02004157 // it just fits, avoid divide by zero
4158 sb_thumb_top = 0;
4159 else
4160 sb_thumb_top = (wp->w_topline - 1
4161 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004162 * (wp->w_height - sb_thumb_height)
4163 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02004164 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
4165 sb_thumb_top = 1; // show it's scrolled
Bram Moolenaarfc376e02022-05-29 18:18:18 +01004166 last = total_height - top_off - wp->w_popup_border[2];
4167 if (sb_thumb_top >= last)
4168 // show at least one character
Bram Moolenaar89b25582022-05-30 13:20:56 +01004169 sb_thumb_top = last - 1;
Bram Moolenaar076d9882019-09-14 22:23:29 +02004170
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004171 if (wp->w_scrollbar_highlight != NULL)
4172 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
4173 else
4174 attr_scroll = highlight_attr[HLF_PSB];
4175 if (wp->w_thumb_highlight != NULL)
4176 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
4177 else
4178 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004179 }
4180
4181 for (i = wp->w_popup_border[0];
4182 i < total_height - wp->w_popup_border[2]; ++i)
4183 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02004184 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02004185 // left and right padding only needed next to the body
4186 int do_padding =
4187 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
4188 && i < total_height - wp->w_popup_border[2]
4189 - wp->w_popup_padding[2];
4190
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004191 row = wp->w_winrow + i;
4192
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004193 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004194 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004195 {
4196 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004197 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004198 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02004199 if (do_padding && wp->w_popup_padding[3] > 0)
4200 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004201 int col = wincol + wp->w_popup_border[3];
4202
Bram Moolenaard529ba52019-07-02 23:13:53 +02004203 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004204 pad_left = wp->w_popup_padding[3];
4205 if (col < 0)
4206 {
4207 pad_left += col;
4208 col = 0;
4209 }
4210 if (pad_left > 0)
4211 screen_puts(get_spaces(pad_left), row, col, popup_attr);
4212 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02004213 // scrollbar
4214 if (wp->w_has_scrollbar)
4215 {
4216 int line = i - top_off;
4217 int scroll_col = wp->w_wincol + total_width - 1
4218 - wp->w_popup_border[1];
4219
4220 if (line >= 0 && line < wp->w_height)
4221 screen_putchar(' ', row, scroll_col,
4222 line >= sb_thumb_top
4223 && line < sb_thumb_top + sb_thumb_height
4224 ? attr_thumb : attr_scroll);
4225 else
4226 screen_putchar(' ', row, scroll_col, popup_attr);
4227 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004228 // right border
4229 if (wp->w_popup_border[1] > 0)
4230 {
4231 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004232 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004233 }
4234 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02004235 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004236 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004237 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02004238 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
4239 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004240 }
4241
4242 if (wp->w_popup_padding[2] > 0)
4243 {
4244 // bottom padding
4245 row = wp->w_winrow + wp->w_popup_border[0]
4246 + wp->w_popup_padding[0] + wp->w_height;
4247 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard91467f2020-11-21 12:42:09 +01004248 padcol, padendcol, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004249 }
4250
4251 if (wp->w_popup_border[2] > 0)
4252 {
4253 // bottom border
4254 row = wp->w_winrow + total_height - 1;
4255 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004256 wincol < 0 ? 0 : wincol,
4257 wincol + total_width,
4258 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004259 ? border_char[7] : border_char[2],
4260 border_char[2], border_attr[2]);
4261 if (wp->w_popup_border[1] > 0)
4262 {
4263 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004264 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004265 }
4266 }
4267
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004268 if (wp->w_popup_close == POPCLOSE_BUTTON)
4269 {
4270 // close button goes on top of anything at the top-right corner
4271 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02004272 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02004273 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
4274 }
4275
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004276 update_popup_transparent(wp, 0);
4277
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004278 // Back to the normal zindex.
4279 screen_zindex = 0;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004280
4281#ifdef HAS_MESSAGE_WINDOW
4282 // if this was the message window popup may start the timer now
4283 may_start_message_win_timer(wp);
4284#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004285 }
Bram Moolenaar02f9e6a2020-07-15 18:27:08 +02004286
4287#if defined(FEAT_SEARCH_EXTRA)
4288 // In case win_update() called start_search_hl().
4289 end_search_hl();
4290#endif
Bram Moolenaara540f8a2019-06-14 19:23:57 +02004291}
4292
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004293/*
4294 * Mark references in callbacks of one popup window.
4295 */
4296 static int
4297set_ref_in_one_popup(win_T *wp, int copyID)
4298{
4299 int abort = FALSE;
4300 typval_T tv;
4301
4302 if (wp->w_close_cb.cb_partial != NULL)
4303 {
4304 tv.v_type = VAR_PARTIAL;
4305 tv.vval.v_partial = wp->w_close_cb.cb_partial;
4306 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4307 }
4308 if (wp->w_filter_cb.cb_partial != NULL)
4309 {
4310 tv.v_type = VAR_PARTIAL;
4311 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
4312 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4313 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004314 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004315 return abort;
4316}
4317
4318/*
4319 * Set reference in callbacks of popup windows.
4320 */
4321 int
4322set_ref_in_popups(int copyID)
4323{
4324 int abort = FALSE;
4325 win_T *wp;
4326 tabpage_T *tp;
4327
4328 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4329 abort = abort || set_ref_in_one_popup(wp, copyID);
4330
4331 FOR_ALL_TABPAGES(tp)
4332 {
4333 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
4334 abort = abort || set_ref_in_one_popup(wp, copyID);
4335 if (abort)
4336 break;
4337 }
4338 return abort;
4339}
Bram Moolenaar79648732019-07-18 21:43:07 +02004340
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004341 int
4342popup_is_popup(win_T *wp)
4343{
4344 return wp->w_popup_flags != 0;
4345}
4346
4347#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004348/*
4349 * Find an existing popup used as the preview window, in the current tab page.
4350 * Return NULL if not found.
4351 */
4352 win_T *
4353popup_find_preview_window(void)
4354{
4355 win_T *wp;
4356
4357 // Preview window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004358 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar79648732019-07-18 21:43:07 +02004359 if (wp->w_p_pvw)
4360 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004361 return NULL;
4362}
4363
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004364/*
4365 * Find an existing popup used as the info window, in the current tab page.
4366 * Return NULL if not found.
4367 */
4368 win_T *
4369popup_find_info_window(void)
4370{
4371 win_T *wp;
4372
4373 // info window popup is always local to tab page.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004374 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004375 if (wp->w_popup_flags & POPF_INFO)
4376 return wp;
4377 return NULL;
4378}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004379#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004380
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004381 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004382f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
4383{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004384#ifdef FEAT_QUICKFIX
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004385 win_T *wp = popup_find_info_window();
4386
4387 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004388#else
4389 rettv->vval.v_number = 0;
4390#endif
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004391}
4392
4393 void
4394f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004395{
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004396#ifdef FEAT_QUICKFIX
Bram Moolenaar56c0c472019-07-28 17:57:43 +02004397 win_T *wp = popup_find_preview_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 Moolenaar79648732019-07-18 21:43:07 +02004403}
4404
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004405#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02004406/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004407 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02004408 * NOTE: this makes the popup the current window, so that the file can be
4409 * edited. However, it must not remain to be the current window, the caller
4410 * must make sure of that.
4411 */
4412 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004413popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02004414{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004415 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02004416
4417 if (wp == NULL)
4418 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02004419 if (info)
4420 wp->w_popup_flags |= POPF_INFO;
4421 else
4422 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02004423
4424 // Set the width to a reasonable value, so that w_topline can be computed.
4425 if (wp->w_minwidth > 0)
4426 wp->w_width = wp->w_minwidth;
4427 else if (wp->w_maxwidth > 0)
4428 wp->w_width = wp->w_maxwidth;
4429 else
4430 wp->w_width = curwin->w_width;
4431
4432 // Will switch to another buffer soon, dummy one can be wiped.
4433 wp->w_buffer->b_locked = FALSE;
4434
4435 win_enter(wp, FALSE);
4436 return OK;
4437}
4438
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004439/*
4440 * Close any preview popup.
4441 */
Bram Moolenaar79648732019-07-18 21:43:07 +02004442 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004443popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02004444{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004445 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02004446
4447 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004448 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02004449}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02004450
4451/*
4452 * Hide the info popup.
4453 */
4454 void
4455popup_hide_info(void)
4456{
4457 win_T *wp = popup_find_info_window();
4458
4459 if (wp != NULL)
4460 popup_hide(wp);
4461}
Bram Moolenaar447bfba2020-07-18 16:07:16 +02004462
4463/*
4464 * Close any info popup.
4465 */
4466 void
4467popup_close_info(void)
4468{
4469 win_T *wp = popup_find_info_window();
4470
4471 if (wp != NULL)
4472 popup_close_with_retval(wp, -1);
4473}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004474#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004475
Bram Moolenaar9198de32022-08-27 21:30:03 +01004476#if defined(HAS_MESSAGE_WINDOW) || defined(PROTO)
4477
Bram Moolenaar9198de32022-08-27 21:30:03 +01004478/*
4479 * Get the message window.
4480 * Returns NULL if something failed.
4481 */
4482 win_T *
4483popup_get_message_win(void)
4484{
4485 if (message_win == NULL)
4486 {
4487 int i;
4488
4489 message_win = popup_create(NULL, NULL, TYPE_MESSAGE_WIN);
4490
4491 if (message_win == NULL)
4492 return NULL;
4493
4494 // use the full screen width
4495 message_win->w_width = Columns;
4496
4497 // position at bottom of screen
4498 message_win->w_popup_pos = POPPOS_BOTTOM;
4499 message_win->w_wantcol = 1;
4500 message_win->w_minwidth = 9999;
Bram Moolenaarb5b4f612022-09-01 16:43:17 +01004501 message_win->w_firstline = -1;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004502
4503 // no padding, border at the top
4504 for (i = 0; i < 4; ++i)
4505 message_win->w_popup_padding[i] = 0;
4506 for (i = 1; i < 4; ++i)
4507 message_win->w_popup_border[i] = 0;
4508
4509 if (message_win->w_popup_timer != NULL)
4510 message_win->w_popup_timer->tr_keep = TRUE;
4511 }
4512 return message_win;
4513}
4514
4515/*
4516 * If the message window is not visible: show it
4517 * If the message window is visible: reset the timeout
4518 */
4519 void
4520popup_show_message_win(void)
4521{
4522 if (message_win != NULL)
4523 {
4524 if ((message_win->w_popup_flags & POPF_HIDDEN) != 0)
4525 {
4526 // the highlight may have changed.
4527 popup_update_color(message_win, TYPE_MESSAGE_WIN);
4528 popup_show(message_win);
4529 }
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004530 start_message_win_timer = TRUE;
4531 }
4532}
4533
4534 static void
4535may_start_message_win_timer(win_T *wp)
4536{
4537 if (wp == message_win && start_message_win_timer)
4538 {
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01004539 if (message_win->w_popup_timer != NULL)
Bram Moolenaar9198de32022-08-27 21:30:03 +01004540 timer_start(message_win->w_popup_timer);
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004541 start_message_win_timer = FALSE;
Bram Moolenaar9198de32022-08-27 21:30:03 +01004542 }
4543}
4544
4545 int
4546popup_message_win_visible(void)
4547{
4548 return message_win != NULL
4549 && (message_win->w_popup_flags & POPF_HIDDEN) == 0;
4550}
4551
4552/*
4553 * If the message window is visible: hide it.
4554 */
4555 void
4556popup_hide_message_win(void)
4557{
4558 if (message_win != NULL)
4559 popup_hide(message_win);
4560}
4561
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004562// Values saved in start_echowindow() and restored in end_echowindow()
4563static int save_msg_didout = FALSE;
4564static int save_msg_col = 0;
4565// Values saved in end_echowindow() and restored in start_echowindow()
4566static int ew_msg_didout = FALSE;
4567static int ew_msg_col = 0;
4568
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004569/*
4570 * Invoked before outputting a message for ":echowindow".
4571 */
4572 void
4573start_echowindow(void)
4574{
4575 in_echowindow = TRUE;
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004576 save_msg_didout = msg_didout;
4577 save_msg_col = msg_col;
4578 msg_didout = ew_msg_didout;
4579 msg_col = ew_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004580}
4581
4582/*
4583 * Invoked after outputting a message for ":echowindow".
4584 */
4585 void
4586end_echowindow(void)
4587{
Bram Moolenaar7cf58392022-09-09 20:19:40 +01004588 in_echowindow = FALSE;
4589
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01004590 if ((State & MODE_HITRETURN) == 0)
4591 // show the message window now
4592 redraw_cmd(FALSE);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004593
4594 // do not overwrite messages
Bram Moolenaarf87eeb42022-09-22 19:02:38 +01004595 ew_msg_didout = TRUE;
4596 ew_msg_col = msg_col == 0 ? 1 : msg_col;
4597 msg_didout = save_msg_didout;
4598 msg_col = save_msg_col;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01004599}
Bram Moolenaar9198de32022-08-27 21:30:03 +01004600#endif
4601
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004602/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02004603 * Close any popup for a text property associated with "win".
4604 * Return TRUE if a popup was closed.
4605 */
4606 int
4607popup_win_closed(win_T *win)
4608{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004609 int round;
4610 win_T *wp;
4611 win_T *next;
4612 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004613
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004614 for (round = 1; round <= 2; ++round)
4615 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
4616 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02004617 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004618 next = wp->w_next;
4619 if (wp->w_popup_prop_win == win)
4620 {
4621 popup_close_with_retval(wp, -1);
4622 ret = TRUE;
4623 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02004624 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02004625 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02004626}
4627
4628/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004629 * Set the title of the popup window to the file name.
4630 */
4631 void
4632popup_set_title(win_T *wp)
4633{
4634 if (wp->w_buffer->b_fname != NULL)
4635 {
4636 char_u dirname[MAXPATHL];
4637 size_t len;
4638
4639 mch_dirname(dirname, MAXPATHL);
4640 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
4641
4642 vim_free(wp->w_popup_title);
4643 len = STRLEN(wp->w_buffer->b_fname) + 3;
4644 wp->w_popup_title = alloc((int)len);
4645 if (wp->w_popup_title != NULL)
4646 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
4647 wp->w_buffer->b_fname);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004648 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004649 }
4650}
4651
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004652# if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004653/*
4654 * If there is a preview window, update the title.
4655 * Used after changing directory.
4656 */
4657 void
4658popup_update_preview_title(void)
4659{
4660 win_T *wp = popup_find_preview_window();
4661
4662 if (wp != NULL)
4663 popup_set_title(wp);
4664}
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004665# endif
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02004666
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004667#endif // FEAT_PROP_POPUP