blob: 1fb72bbdb571b30b1929bbd505c22df109d3146e [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 Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || 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 Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
74 if (n < 1)
75 n = 1;
76 return n;
77}
78
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020079 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020080set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081{
82 dictitem_T *di;
83
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020084 di = dict_find(dict, (char_u *)name, -1);
85 if (di != NULL)
86 {
87 if (di->di_tv.v_type != VAR_LIST)
88 emsg(_(e_listreq));
89 else
90 {
91 list_T *list = di->di_tv.vval.v_list;
92 listitem_T *li;
93 int i;
94 int nr;
95
96 for (i = 0; i < 4; ++i)
97 array[i] = 1;
98 if (list != NULL)
99 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
100 ++i, li = li->li_next)
101 {
102 nr = (int)tv_get_number(&li->li_tv);
103 if (nr >= 0)
104 array[i] = nr > max_val ? max_val : nr;
105 }
106 }
107 }
108}
109
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200110/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200111 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200112 */
113 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200114set_moved_values(win_T *wp)
115{
116 wp->w_popup_curwin = curwin;
117 wp->w_popup_lnum = curwin->w_cursor.lnum;
118 wp->w_popup_mincol = curwin->w_cursor.col;
119 wp->w_popup_maxcol = curwin->w_cursor.col;
120}
121
122/*
123 * Used when popup options contain "moved" with "word" or "WORD".
124 */
125 static void
126set_moved_columns(win_T *wp, int flags)
127{
128 char_u *ptr;
129 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
130
131 if (len > 0)
132 {
133 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
134 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
135 }
136}
137
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200138/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200139 * Used when popup options contain "mousemoved": set default moved values.
140 */
141 static void
142set_mousemoved_values(win_T *wp)
143{
144 wp->w_popup_mouse_row = mouse_row;
145 wp->w_popup_mouse_mincol = mouse_col;
146 wp->w_popup_mouse_maxcol = mouse_col;
147}
148
149/*
150 * Used when popup options contain "moved" with "word" or "WORD".
151 */
152 static void
153set_mousemoved_columns(win_T *wp, int flags)
154{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200155 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200156 char_u *text;
157 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200158 pos_T pos;
159 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160
161 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200163 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200164 // convert text column to mouse column
165 pos.col = col;
166 pos.coladd = 0;
167 getvcol(textwp, &pos, &mcol, NULL, NULL);
168 wp->w_popup_mouse_mincol = mcol;
169
Bram Moolenaar10727682019-07-12 13:59:20 +0200170 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200171 getvcol(textwp, &pos, NULL, NULL, &mcol);
172 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200173 vim_free(text);
174 }
175}
176
177/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200178 * Return TRUE if "row"/"col" is on the border of the popup.
179 * The values are relative to the top-left corner.
180 */
181 int
182popup_on_border(win_T *wp, int row, int col)
183{
184 return (row == 0 && wp->w_popup_border[0] > 0)
185 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
186 || (col == 0 && wp->w_popup_border[3] > 0)
187 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
188}
189
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200190/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200191 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
192 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200193 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200194 * Caller should check the left mouse button was clicked.
195 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200196 */
197 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200199{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200200 if (wp->w_popup_close == POPCLOSE_BUTTON
201 && row == 0 && col == popup_width(wp) - 1)
202 {
203 popup_close_for_mouse_click(wp);
204 return TRUE;
205 }
206 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200207}
208
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200209// Values set when dragging a popup window starts.
210static int drag_start_row;
211static int drag_start_col;
212static int drag_start_wantline;
213static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200214static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200215
216/*
217 * Mouse down on border of popup window: start dragging it.
218 * Uses mouse_col and mouse_row.
219 */
220 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200221popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222{
223 drag_start_row = mouse_row;
224 drag_start_col = mouse_col;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200225 if (wp->w_wantline == 0)
226 drag_start_wantline = wp->w_winrow + 1;
227 else
228 drag_start_wantline = wp->w_wantline;
229 if (wp->w_wantcol == 0)
230 drag_start_wantcol = wp->w_wincol + 1;
231 else
232 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200233
234 // Stop centering the popup
235 if (wp->w_popup_pos == POPPOS_CENTER)
236 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200237
238 drag_on_resize_handle = wp->w_popup_border[1] > 0
239 && wp->w_popup_border[2] > 0
240 && row == popup_height(wp) - 1
241 && col == popup_width(wp) - 1;
242
243 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
244 {
245 if (wp->w_popup_pos == POPPOS_TOPRIGHT
246 || wp->w_popup_pos == POPPOS_BOTRIGHT)
247 wp->w_wantcol = wp->w_wincol + 1;
248 if (wp->w_popup_pos == POPPOS_BOTLEFT)
249 wp->w_wantline = wp->w_winrow + 1;
250 wp->w_popup_pos = POPPOS_TOPLEFT;
251 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200252}
253
254/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200255 * Mouse moved while dragging a popup window: adjust the window popup position
256 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200257 */
258 void
259popup_drag(win_T *wp)
260{
261 // The popup may be closed before dragging stops.
262 if (!win_valid_popup(wp))
263 return;
264
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200265 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
266 {
267 int width_inc = mouse_col - drag_start_col;
268 int height_inc = mouse_row - drag_start_row;
269
270 if (width_inc != 0)
271 {
272 int width = wp->w_width + width_inc;
273
274 if (width < 1)
275 width = 1;
276 wp->w_minwidth = width;
277 wp->w_maxwidth = width;
278 drag_start_col = mouse_col;
279 }
280
281 if (height_inc != 0)
282 {
283 int height = wp->w_height + height_inc;
284
285 if (height < 1)
286 height = 1;
287 wp->w_minheight = height;
288 wp->w_maxheight = height;
289 drag_start_row = mouse_row;
290 }
291
292 popup_adjust_position(wp);
293 return;
294 }
295
296 if (!(wp->w_popup_flags & POPF_DRAG))
297 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200298 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
299 if (wp->w_wantline < 1)
300 wp->w_wantline = 1;
301 if (wp->w_wantline > Rows)
302 wp->w_wantline = Rows;
303 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
304 if (wp->w_wantcol < 1)
305 wp->w_wantcol = 1;
306 if (wp->w_wantcol > Columns)
307 wp->w_wantcol = Columns;
308
309 popup_adjust_position(wp);
310}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200311
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200312/*
313 * Set w_firstline to match the current "wp->w_topline".
314 */
315 void
316popup_set_firstline(win_T *wp)
317{
318 int height = wp->w_height;
319
320 wp->w_firstline = wp->w_topline;
321 popup_adjust_position(wp);
322
323 // we don't want the popup to get smaller, decrement the first line
324 // until it doesn't
325 while (wp->w_firstline > 1 && wp->w_height < height)
326 {
327 --wp->w_firstline;
328 popup_adjust_position(wp);
329 }
330}
331
332/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200333 * Return TRUE if the position is in the popup window scrollbar.
334 */
335 int
336popup_is_in_scrollbar(win_T *wp, int row, int col)
337{
338 return wp->w_has_scrollbar
339 && row >= wp->w_popup_border[0]
340 && row < popup_height(wp) - wp->w_popup_border[2]
341 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
342}
343
344
345/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200346 * Handle a click in a popup window, if it is in the scrollbar.
347 */
348 void
349popup_handle_scrollbar_click(win_T *wp, int row, int col)
350{
351 int height = popup_height(wp);
352 int old_topline = wp->w_topline;
353
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200354 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200355 {
356 if (row >= height / 2)
357 {
358 // Click in lower half, scroll down.
359 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
360 ++wp->w_topline;
361 }
362 else if (wp->w_topline > 1)
363 // click on upper half, scroll up.
364 --wp->w_topline;
365 if (wp->w_topline != old_topline)
366 {
367 popup_set_firstline(wp);
368 redraw_win_later(wp, NOT_VALID);
369 }
370 }
371}
372
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200373#if defined(FEAT_TIMERS)
374 static void
375popup_add_timeout(win_T *wp, int time)
376{
377 char_u cbbuf[50];
378 char_u *ptr = cbbuf;
379 typval_T tv;
380
381 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
382 "{_ -> popup_close(%d)}", wp->w_id);
383 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
384 {
385 wp->w_popup_timer = create_timer(time, 0);
386 wp->w_popup_timer->tr_callback = get_callback(&tv);
387 clear_tv(&tv);
388 }
389}
390#endif
391
Bram Moolenaar17627312019-06-02 19:53:44 +0200392/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200393 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200394 */
395 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200396apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200397{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200398 int nr;
399 char_u *str;
400 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200401
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200402 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200403 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200404 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200405 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200406 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200407 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200408 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200409 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200410
411 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200412 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200413 wp->w_wantline = nr;
414 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200415 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200416 wp->w_wantcol = nr;
417
418 di = dict_find(d, (char_u *)"fixed", -1);
419 if (di != NULL)
420 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
421
422 str = dict_get_string(d, (char_u *)"pos", FALSE);
423 if (str != NULL)
424 {
425 for (nr = 0;
426 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
427 ++nr)
428 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
429 {
430 wp->w_popup_pos = poppos_entries[nr].pp_val;
431 nr = -1;
432 break;
433 }
434 if (nr != -1)
435 semsg(_(e_invarg2), str);
436 }
437
438 str = dict_get_string(d, (char_u *)"textprop", FALSE);
439 if (str != NULL)
440 {
441 wp->w_popup_prop_type = 0;
442 if (*str != NUL)
443 {
444 nr = find_prop_type_id(str, wp->w_buffer);
445 if (nr <= 0)
446 nr = find_prop_type_id(str, NULL);
447 if (nr <= 0)
448 semsg(_(e_invarg2), str);
449 else
450 {
451 wp->w_popup_prop_type = nr;
452 wp->w_popup_prop_win = curwin;
453
454 di = dict_find(d, (char_u *)"textpropwin", -1);
455 if (di != NULL)
456 {
457 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
458 if (win_valid(wp->w_popup_prop_win))
459 wp->w_popup_prop_win = curwin;
460 }
461 }
462 }
463 }
464
465 di = dict_find(d, (char_u *)"textpropid", -1);
466 if (di != NULL)
467 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200468}
469
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200470 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200471handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
472{
473 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
474 {
475 char_u *s = di->di_tv.vval.v_string;
476 int flags = 0;
477
478 if (STRCMP(s, "word") == 0)
479 flags = FIND_IDENT | FIND_STRING;
480 else if (STRCMP(s, "WORD") == 0)
481 flags = FIND_STRING;
482 else if (STRCMP(s, "expr") == 0)
483 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
484 else if (STRCMP(s, "any") != 0)
485 semsg(_(e_invarg2), s);
486 if (flags != 0)
487 {
488 if (mousemoved)
489 set_mousemoved_columns(wp, flags);
490 else
491 set_moved_columns(wp, flags);
492 }
493 }
494 else if (di->di_tv.v_type == VAR_LIST
495 && di->di_tv.vval.v_list != NULL
496 && di->di_tv.vval.v_list->lv_len == 2)
497 {
498 list_T *l = di->di_tv.vval.v_list;
499 int mincol = tv_get_number(&l->lv_first->li_tv);
500 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
501
502 if (mousemoved)
503 {
504 wp->w_popup_mouse_mincol = mincol;
505 wp->w_popup_mouse_maxcol = maxcol;
506 }
507 else
508 {
509 wp->w_popup_mincol = mincol;
510 wp->w_popup_maxcol = maxcol;
511 }
512 }
513 else
514 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
515}
516
517 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200518check_highlight(dict_T *dict, char *name, char_u **pval)
519{
520 dictitem_T *di;
521 char_u *str;
522
523 di = dict_find(dict, (char_u *)name, -1);
524 if (di != NULL)
525 {
526 if (di->di_tv.v_type != VAR_STRING)
527 semsg(_(e_invargval), name);
528 else
529 {
530 str = tv_get_string(&di->di_tv);
531 if (*str != NUL)
532 *pval = vim_strsave(str);
533 }
534 }
535}
536
Bram Moolenaarae943152019-06-16 22:54:14 +0200537/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200538 * Scroll to show the line with the cursor. This assumes lines don't wrap.
539 */
540 static void
541popup_show_curline(win_T *wp)
542{
543 if (wp->w_cursor.lnum < wp->w_topline)
544 wp->w_topline = wp->w_cursor.lnum;
545 else if (wp->w_cursor.lnum >= wp->w_botline)
546 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200547
548 // Don't use "firstline" now.
549 wp->w_firstline = 0;
550}
551
552/*
553 * Get the sign group name for window "wp".
554 * Returns a pointer to a static buffer, overwritten on the next call.
555 */
556 static char_u *
557popup_get_sign_name(win_T *wp)
558{
559 static char buf[30];
560
561 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
562 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200563}
564
565/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200566 * Highlight the line with the cursor.
567 * Also scrolls the text to put the cursor line in view.
568 */
569 static void
570popup_highlight_curline(win_T *wp)
571{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200572 int sign_id = 0;
573 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200574
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200575 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200576
577 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
578 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200579 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200580
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200581 if (!sign_exists_by_name(sign_name))
582 {
583 char *linehl = "PopupSelected";
584
585 if (syn_name2id((char_u *)linehl) == 0)
586 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200587 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200588 }
589
590 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
591 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
592 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200593 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200594 else
595 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200596}
597
598/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200599 * Shared between popup_create() and f_popup_setoptions().
600 */
601 static void
602apply_general_options(win_T *wp, dict_T *dict)
603{
604 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200605 int nr;
606 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200607
Bram Moolenaarae943152019-06-16 22:54:14 +0200608 // TODO: flip
609
610 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200611 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200612 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200613 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200614 if (wp->w_firstline < 0)
615 wp->w_firstline = -1;
616 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200617
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200618 di = dict_find(dict, (char_u *)"scrollbar", -1);
619 if (di != NULL)
620 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
621
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200622 str = dict_get_string(dict, (char_u *)"title", FALSE);
623 if (str != NULL)
624 {
625 vim_free(wp->w_popup_title);
626 wp->w_popup_title = vim_strsave(str);
627 }
628
Bram Moolenaar402502d2019-05-30 22:07:36 +0200629 di = dict_find(dict, (char_u *)"wrap", -1);
630 if (di != NULL)
631 {
632 nr = dict_get_number(dict, (char_u *)"wrap");
633 wp->w_p_wrap = nr != 0;
634 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200635
Bram Moolenaara42d9452019-06-15 21:46:30 +0200636 di = dict_find(dict, (char_u *)"drag", -1);
637 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200638 {
639 nr = dict_get_number(dict, (char_u *)"drag");
640 if (nr)
641 wp->w_popup_flags |= POPF_DRAG;
642 else
643 wp->w_popup_flags &= ~POPF_DRAG;
644 }
645
646 di = dict_find(dict, (char_u *)"resize", -1);
647 if (di != NULL)
648 {
649 nr = dict_get_number(dict, (char_u *)"resize");
650 if (nr)
651 wp->w_popup_flags |= POPF_RESIZE;
652 else
653 wp->w_popup_flags &= ~POPF_RESIZE;
654 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200655
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200656 di = dict_find(dict, (char_u *)"close", -1);
657 if (di != NULL)
658 {
659 int ok = TRUE;
660
661 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
662 {
663 char_u *s = di->di_tv.vval.v_string;
664
665 if (STRCMP(s, "none") == 0)
666 wp->w_popup_close = POPCLOSE_NONE;
667 else if (STRCMP(s, "button") == 0)
668 wp->w_popup_close = POPCLOSE_BUTTON;
669 else if (STRCMP(s, "click") == 0)
670 wp->w_popup_close = POPCLOSE_CLICK;
671 else
672 ok = FALSE;
673 }
674 else
675 ok = FALSE;
676 if (!ok)
677 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
678 }
679
Bram Moolenaarae943152019-06-16 22:54:14 +0200680 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
681 if (str != NULL)
682 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
683 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200684
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200685 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
686 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200687 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
688 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200689
Bram Moolenaar790498b2019-06-01 22:15:29 +0200690 di = dict_find(dict, (char_u *)"borderhighlight", -1);
691 if (di != NULL)
692 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200693 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200694 emsg(_(e_listreq));
695 else
696 {
697 list_T *list = di->di_tv.vval.v_list;
698 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200699 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200700
Bram Moolenaar403d0902019-07-17 21:37:32 +0200701 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
702 ++i, li = li->li_next)
703 {
704 str = tv_get_string(&li->li_tv);
705 if (*str != NUL)
706 wp->w_border_highlight[i] = vim_strsave(str);
707 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200708 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
709 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200710 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200711 vim_strsave(wp->w_border_highlight[0]);
712 }
713 }
714
Bram Moolenaar790498b2019-06-01 22:15:29 +0200715 di = dict_find(dict, (char_u *)"borderchars", -1);
716 if (di != NULL)
717 {
718 if (di->di_tv.v_type != VAR_LIST)
719 emsg(_(e_listreq));
720 else
721 {
722 list_T *list = di->di_tv.vval.v_list;
723 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200724 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200725
726 if (list != NULL)
727 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
728 ++i, li = li->li_next)
729 {
730 str = tv_get_string(&li->li_tv);
731 if (*str != NUL)
732 wp->w_border_char[i] = mb_ptr2char(str);
733 }
734 if (list->lv_len == 1)
735 for (i = 1; i < 8; ++i)
736 wp->w_border_char[i] = wp->w_border_char[0];
737 if (list->lv_len == 2)
738 {
739 for (i = 4; i < 8; ++i)
740 wp->w_border_char[i] = wp->w_border_char[1];
741 for (i = 1; i < 4; ++i)
742 wp->w_border_char[i] = wp->w_border_char[0];
743 }
744 }
745 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200746
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200747 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
748 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
749
Bram Moolenaarae943152019-06-16 22:54:14 +0200750 di = dict_find(dict, (char_u *)"zindex", -1);
751 if (di != NULL)
752 {
753 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
754 if (wp->w_zindex < 1)
755 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
756 if (wp->w_zindex > 32000)
757 wp->w_zindex = 32000;
758 }
759
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200760 di = dict_find(dict, (char_u *)"mask", -1);
761 if (di != NULL)
762 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200763 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200764
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200765 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200766 {
767 listitem_T *li;
768
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200769 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200770 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
771 li = li->li_next)
772 {
773 if (li->li_tv.v_type != VAR_LIST
774 || li->li_tv.vval.v_list == NULL
775 || li->li_tv.vval.v_list->lv_len != 4)
776 {
777 ok = FALSE;
778 break;
779 }
780 }
781 }
782 if (ok)
783 {
784 wp->w_popup_mask = di->di_tv.vval.v_list;
785 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200786 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200787 }
788 else
789 semsg(_(e_invargval), "mask");
790 }
791
Bram Moolenaarae943152019-06-16 22:54:14 +0200792#if defined(FEAT_TIMERS)
793 // Add timer to close the popup after some time.
794 nr = dict_get_number(dict, (char_u *)"time");
795 if (nr > 0)
796 popup_add_timeout(wp, nr);
797#endif
798
Bram Moolenaar3397f742019-06-02 18:40:06 +0200799 di = dict_find(dict, (char_u *)"moved", -1);
800 if (di != NULL)
801 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200802 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200803 handle_moved_argument(wp, di, FALSE);
804 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200805
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200806 di = dict_find(dict, (char_u *)"mousemoved", -1);
807 if (di != NULL)
808 {
809 set_mousemoved_values(wp);
810 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200811 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200812
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200813 di = dict_find(dict, (char_u *)"cursorline", -1);
814 if (di != NULL)
815 {
816 if (di->di_tv.v_type == VAR_NUMBER)
817 {
818 if (di->di_tv.vval.v_number != 0)
819 wp->w_popup_flags |= POPF_CURSORLINE;
820 else
821 wp->w_popup_flags &= ~POPF_CURSORLINE;
822 }
823 else
824 semsg(_(e_invargval), "cursorline");
825 }
826
Bram Moolenaarae943152019-06-16 22:54:14 +0200827 di = dict_find(dict, (char_u *)"filter", -1);
828 if (di != NULL)
829 {
830 callback_T callback = get_callback(&di->di_tv);
831
832 if (callback.cb_name != NULL)
833 {
834 free_callback(&wp->w_filter_cb);
835 set_callback(&wp->w_filter_cb, &callback);
836 }
837 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200838 di = dict_find(dict, (char_u *)"mapping", -1);
839 if (di != NULL)
840 {
841 nr = dict_get_number(dict, (char_u *)"mapping");
842 if (nr)
843 wp->w_popup_flags |= POPF_MAPPING;
844 else
845 wp->w_popup_flags &= ~POPF_MAPPING;
846 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200847
Bram Moolenaar581ba392019-09-03 22:08:33 +0200848 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
849 if (str != NULL)
850 {
851 if (STRCMP(str, "a") == 0)
852 wp->w_filter_mode = MODE_ALL;
853 else
854 wp->w_filter_mode = mode_str2flags(str);
855 }
856
Bram Moolenaarae943152019-06-16 22:54:14 +0200857 di = dict_find(dict, (char_u *)"callback", -1);
858 if (di != NULL)
859 {
860 callback_T callback = get_callback(&di->di_tv);
861
862 if (callback.cb_name != NULL)
863 {
864 free_callback(&wp->w_close_cb);
865 set_callback(&wp->w_close_cb, &callback);
866 }
867 }
868}
869
870/*
871 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200872 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200873 */
874 static void
875apply_options(win_T *wp, dict_T *dict)
876{
877 int nr;
878
879 apply_move_options(wp, dict);
880 apply_general_options(wp, dict);
881
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200882 nr = dict_get_number(dict, (char_u *)"hidden");
883 if (nr > 0)
884 {
885 wp->w_popup_flags |= POPF_HIDDEN;
886 --wp->w_buffer->b_nwindows;
887 }
888
Bram Moolenaar33796b32019-06-08 16:01:13 +0200889 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200890 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200891}
892
893/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200894 * Add lines to the popup from a list of strings.
895 */
896 static void
897add_popup_strings(buf_T *buf, list_T *l)
898{
899 listitem_T *li;
900 linenr_T lnum = 0;
901 char_u *p;
902
903 for (li = l->lv_first; li != NULL; li = li->li_next)
904 if (li->li_tv.v_type == VAR_STRING)
905 {
906 p = li->li_tv.vval.v_string;
907 ml_append_buf(buf, lnum++,
908 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
909 }
910}
911
912/*
913 * Add lines to the popup from a list of dictionaries.
914 */
915 static void
916add_popup_dicts(buf_T *buf, list_T *l)
917{
918 listitem_T *li;
919 listitem_T *pli;
920 linenr_T lnum = 0;
921 char_u *p;
922 dict_T *dict;
923
924 // first add the text lines
925 for (li = l->lv_first; li != NULL; li = li->li_next)
926 {
927 if (li->li_tv.v_type != VAR_DICT)
928 {
929 emsg(_(e_dictreq));
930 return;
931 }
932 dict = li->li_tv.vval.v_dict;
933 p = dict == NULL ? NULL
934 : dict_get_string(dict, (char_u *)"text", FALSE);
935 ml_append_buf(buf, lnum++,
936 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
937 }
938
939 // add the text properties
940 lnum = 1;
941 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
942 {
943 dictitem_T *di;
944 list_T *plist;
945
946 dict = li->li_tv.vval.v_dict;
947 di = dict_find(dict, (char_u *)"props", -1);
948 if (di != NULL)
949 {
950 if (di->di_tv.v_type != VAR_LIST)
951 {
952 emsg(_(e_listreq));
953 return;
954 }
955 plist = di->di_tv.vval.v_list;
956 if (plist != NULL)
957 {
958 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
959 {
960 if (pli->li_tv.v_type != VAR_DICT)
961 {
962 emsg(_(e_dictreq));
963 return;
964 }
965 dict = pli->li_tv.vval.v_dict;
966 if (dict != NULL)
967 {
968 int col = dict_get_number(dict, (char_u *)"col");
969
970 prop_add_common( lnum, col, dict, buf, NULL);
971 }
972 }
973 }
974 }
975 }
976}
977
978/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200979 * Get the padding plus border at the top, adjusted to 1 if there is a title.
980 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +0200981 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200982popup_top_extra(win_T *wp)
983{
984 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
985
986 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
987 return 1;
988 return extra;
989}
990
991/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200992 * Return the height of popup window "wp", including border and padding.
993 */
994 int
995popup_height(win_T *wp)
996{
997 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200998 + popup_top_extra(wp)
999 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001000}
1001
1002/*
1003 * Return the width of popup window "wp", including border, padding and
1004 * scrollbar.
1005 */
1006 int
1007popup_width(win_T *wp)
1008{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001009 // w_leftcol is how many columns of the core are left of the screen
1010 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001011 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001012 + popup_extra_width(wp)
1013 + wp->w_popup_rightoff;
1014}
1015
1016/*
1017 * Return the extra width of popup window "wp": border, padding and scrollbar.
1018 */
1019 int
1020popup_extra_width(win_T *wp)
1021{
1022 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1023 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1024 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001025}
1026
1027/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001028 * Adjust the position and size of the popup to fit on the screen.
1029 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001030 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001031popup_adjust_position(win_T *wp)
1032{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001033 linenr_T lnum;
1034 int wrapped = 0;
1035 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001036 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001037 int center_vert = FALSE;
1038 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001039 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001040 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001041 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1042 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1043 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1044 int extra_height = top_extra + bot_extra;
1045 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001046 int org_winrow = wp->w_winrow;
1047 int org_wincol = wp->w_wincol;
1048 int org_width = wp->w_width;
1049 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001050 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001051 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001052 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001053 int wantline = wp->w_wantline; // adjusted for textprop
1054 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001055
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001056 wp->w_winrow = 0;
1057 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001058 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001059 wp->w_popup_leftoff = 0;
1060 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001061
1062 // If no line was specified default to vertical centering.
1063 if (wantline == 0)
1064 center_vert = TRUE;
1065
1066 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1067 {
1068 win_T *prop_win = wp->w_popup_prop_win;
1069 textprop_T prop;
1070 linenr_T prop_lnum;
1071 pos_T pos;
1072 int screen_row;
1073 int screen_scol;
1074 int screen_ccol;
1075 int screen_ecol;
1076
1077 // Popup window is positioned relative to a text property.
1078 if (find_visible_prop(prop_win,
1079 wp->w_popup_prop_type, wp->w_popup_prop_id,
1080 &prop, &prop_lnum) == FAIL)
1081 {
1082 // Text property is no longer visible, hide the popup.
1083 // Unhiding the popup is done in check_popup_unhidden().
1084 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1085 {
1086 wp->w_popup_flags |= POPF_HIDDEN;
1087 --wp->w_buffer->b_nwindows;
1088 if (win_valid(wp->w_popup_prop_win))
1089 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1090 }
1091 return;
1092 }
1093
1094 // Compute the desired position from the position of the text
1095 // property. Use "wantline" and "wantcol" as offsets.
1096 pos.lnum = prop_lnum;
1097 pos.col = prop.tp_col;
1098 if (wp->w_popup_pos == POPPOS_TOPLEFT
1099 || wp->w_popup_pos == POPPOS_BOTLEFT)
1100 pos.col += prop.tp_len - 1;
1101 textpos2screenpos(prop_win, &pos, &screen_row,
1102 &screen_scol, &screen_ccol, &screen_ecol);
1103
1104 if (wp->w_popup_pos == POPPOS_TOPLEFT
1105 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1106 // below the text
1107 wantline = screen_row + wantline + 1;
1108 else
1109 // above the text
1110 wantline = screen_row + wantline - 1;
1111 center_vert = FALSE;
1112 if (wp->w_popup_pos == POPPOS_TOPLEFT
1113 || wp->w_popup_pos == POPPOS_BOTLEFT)
1114 // right of the text
1115 wantcol = screen_ecol + wantcol;
1116 else
1117 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001118 wantcol = screen_scol + wantcol - 2;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001119 }
1120
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001121 if (wp->w_popup_pos == POPPOS_CENTER)
1122 {
1123 // center after computing the size
1124 center_vert = TRUE;
1125 center_hor = TRUE;
1126 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001127 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001128 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001129 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
Bram Moolenaar12034e22019-08-25 22:25:02 +02001130 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001131 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001132 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001133 if (wp->w_winrow >= Rows)
1134 wp->w_winrow = Rows - 1;
1135 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001136
Bram Moolenaar12034e22019-08-25 22:25:02 +02001137 if (wantcol == 0)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001138 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001139 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1140 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001141 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001142 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001143 if (wp->w_wincol >= Columns - 3)
1144 wp->w_wincol = Columns - 3;
1145 }
1146 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001147
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001148 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001149 // When left aligned use the space available, but shift to the left when we
1150 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001151 maxspace = Columns - wp->w_wincol - left_extra;
1152 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001153 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001154 {
1155 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001156 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001157 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001158
Bram Moolenaar8d241042019-06-12 23:40:01 +02001159 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001160 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001161 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001162 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1163 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1164
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001165 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001166 // Use a minimum width of one, so that something shows when there is no
1167 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001168 // When "firstline" is -1 then start with the last buffer line and go
1169 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001170 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001171 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001172 if (wp->w_firstline < 0)
1173 lnum = wp->w_buffer->b_ml.ml_line_count;
1174 else
1175 lnum = wp->w_topline;
1176 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001177 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001178 int len;
1179 int w_width = wp->w_width;
1180
1181 // Count Tabs for what they are worth and compute the length based on
1182 // the maximum width (matters when 'showbreak' is set).
1183 if (wp->w_width < maxwidth)
1184 wp->w_width = maxwidth;
1185 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001186 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001187 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001188
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001189 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001190 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001191 while (len > maxwidth)
1192 {
1193 ++wrapped;
1194 len -= maxwidth;
1195 wp->w_width = maxwidth;
1196 }
1197 }
1198 else if (len > maxwidth
1199 && allow_adjust_left
1200 && (wp->w_popup_pos == POPPOS_TOPLEFT
1201 || wp->w_popup_pos == POPPOS_BOTLEFT))
1202 {
1203 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001204 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001205
Bram Moolenaar51c31312019-06-15 22:27:23 +02001206 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001207 {
1208 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001209
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001210 len -= truncate_shift;
1211 shift_by -= truncate_shift;
1212 }
1213
1214 wp->w_wincol -= shift_by;
1215 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001216 wp->w_width = maxwidth;
1217 }
1218 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001219 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001220 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001221 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1222 wp->w_width = wp->w_maxwidth;
1223 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001224 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001225 if (wp->w_maxheight > 0
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001226 && (wp->w_firstline >= 0
1227 ? lnum - wp->w_topline
1228 : wp->w_buffer->b_ml.ml_line_count - lnum)
1229 + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001230 break;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001231
1232 if (wp->w_firstline < 0)
1233 --lnum;
1234 else
1235 ++lnum;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001236 }
1237
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001238 if (wp->w_firstline < 0)
1239 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1240
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001241 wp->w_has_scrollbar = wp->w_want_scrollbar
1242 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001243 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001244 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001245 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001246 ++extra_width;
1247 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001248
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001249 minwidth = wp->w_minwidth;
1250 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1251 {
1252 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1253
1254 if (minwidth < title_len)
1255 minwidth = title_len;
1256 }
1257
1258 if (minwidth > 0 && wp->w_width < minwidth)
1259 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001260 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001261 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001262 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001263 // some columns cut off on the right
1264 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001265 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001266 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001267 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001268 {
1269 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1270 if (wp->w_wincol < 0)
1271 wp->w_wincol = 0;
1272 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001273 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1274 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1275 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001276 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001277
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001278 // Right aligned: move to the right if needed.
1279 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001280 if (leftoff >= 0)
1281 wp->w_wincol = leftoff;
1282 else if (wp->w_popup_fixed)
1283 {
1284 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001285 // columns when displaying the window, minus left border and
1286 // padding.
1287 if (-leftoff > left_extra)
1288 wp->w_leftcol = -leftoff - left_extra;
1289 wp->w_width -= wp->w_leftcol;
1290 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001291 if (wp->w_width < 0)
1292 wp->w_width = 0;
1293 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001294 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001295
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001296 if (wp->w_p_wrap || (!wp->w_popup_fixed
1297 && (wp->w_popup_pos == POPPOS_TOPLEFT
1298 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001299 {
1300 int want_col = 0;
1301
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001302 // try to show the right border and any scrollbar
1303 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001304 if (want_col > 0 && wp->w_wincol > 0
1305 && wp->w_wincol + want_col >= Columns)
1306 {
1307 wp->w_wincol = Columns - want_col;
1308 if (wp->w_wincol < 0)
1309 wp->w_wincol = 0;
1310 }
1311 }
1312
Bram Moolenaar8d241042019-06-12 23:40:01 +02001313 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1314 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001315 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1316 wp->w_height = wp->w_minheight;
1317 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1318 wp->w_height = wp->w_maxheight;
1319 if (wp->w_height > Rows - wp->w_winrow)
1320 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001321
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001322 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001323 {
1324 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1325 if (wp->w_winrow < 0)
1326 wp->w_winrow = 0;
1327 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001328 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1329 || wp->w_popup_pos == POPPOS_BOTLEFT)
1330 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001331 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001332 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001333 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001334 else
1335 // not enough space, make top aligned
Bram Moolenaar12034e22019-08-25 22:25:02 +02001336 wp->w_winrow = wantline + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001337 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001338 if (wp->w_winrow >= Rows)
1339 wp->w_winrow = Rows - 1;
1340 else if (wp->w_winrow < 0)
1341 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001342
Bram Moolenaar17146962019-05-30 00:12:11 +02001343 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001344 if (win_valid(wp->w_popup_prop_win))
1345 {
1346 wp->w_popup_prop_changedtick =
1347 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1348 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1349 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001350
1351 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001352 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001353 if (org_winrow != wp->w_winrow
1354 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001355 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001356 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001357 || org_width != wp->w_width
1358 || org_height != wp->w_height)
1359 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001360 redraw_win_later(wp, NOT_VALID);
1361 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1362 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001363 popup_mask_refresh = TRUE;
1364 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001365}
1366
Bram Moolenaar17627312019-06-02 19:53:44 +02001367typedef enum
1368{
1369 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001370 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001371 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001372 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001373 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001374 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001375 TYPE_PREVIEW, // preview window
1376 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001377} create_type_T;
1378
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001379/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001380 * Make "buf" empty and set the contents to "text".
1381 * Used by popup_create() and popup_settext().
1382 */
1383 static void
1384popup_set_buffer_text(buf_T *buf, typval_T text)
1385{
1386 int lnum;
1387
1388 // Clear the buffer, then replace the lines.
1389 curbuf = buf;
1390 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1391 ml_delete(lnum, FALSE);
1392 curbuf = curwin->w_buffer;
1393
1394 // Add text to the buffer.
1395 if (text.v_type == VAR_STRING)
1396 {
1397 // just a string
1398 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1399 }
1400 else
1401 {
1402 list_T *l = text.vval.v_list;
1403
1404 if (l->lv_len > 0)
1405 {
1406 if (l->lv_first->li_tv.v_type == VAR_STRING)
1407 // list of strings
1408 add_popup_strings(buf, l);
1409 else
1410 // list of dictionaries
1411 add_popup_dicts(buf, l);
1412 }
1413 }
1414
1415 // delete the line that was in the empty buffer
1416 curbuf = buf;
1417 ml_delete(buf->b_ml.ml_line_count, FALSE);
1418 curbuf = curwin->w_buffer;
1419}
1420
1421/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001422 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1423 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001424 * Return FAIL if the parsing fails.
1425 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001426 static int
1427parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001428{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001429 char_u *p =
1430#ifdef FEAT_QUICKFIX
1431 !is_preview ? p_cpp :
1432#endif
1433 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001434
Bram Moolenaar258cef52019-08-21 17:29:29 +02001435 if (wp != NULL)
1436 wp->w_popup_flags &= ~POPF_INFO_MENU;
1437
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001438 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001439 {
1440 char_u *e, *dig;
1441 char_u *s = p;
1442 int x;
1443
1444 e = vim_strchr(p, ':');
1445 if (e == NULL || e[1] == NUL)
1446 return FAIL;
1447
1448 p = vim_strchr(e, ',');
1449 if (p == NULL)
1450 p = e + STRLEN(e);
1451 dig = e + 1;
1452 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001453
1454 if (STRNCMP(s, "height:", 7) == 0)
1455 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001456 if (dig != p)
1457 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001458 if (wp != NULL)
1459 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001460 if (is_preview)
1461 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001462 wp->w_maxheight = x;
1463 }
1464 }
1465 else if (STRNCMP(s, "width:", 6) == 0)
1466 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001467 if (dig != p)
1468 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001469 if (wp != NULL)
1470 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001471 if (is_preview)
1472 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001473 wp->w_maxwidth = x;
1474 }
1475 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001476 else if (STRNCMP(s, "highlight:", 10) == 0)
1477 {
1478 if (wp != NULL)
1479 {
1480 int c = *p;
1481
1482 *p = NUL;
1483 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1484 s + 10, OPT_FREE|OPT_LOCAL, 0);
1485 *p = c;
1486 }
1487 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001488 else if (STRNCMP(s, "border:", 7) == 0)
1489 {
1490 char_u *arg = s + 7;
1491 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1492 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1493 int i;
1494
1495 if (!on && !off)
1496 return FAIL;
1497 if (wp != NULL)
1498 {
1499 for (i = 0; i < 4; ++i)
1500 wp->w_popup_border[i] = on ? 1 : 0;
1501 if (off)
1502 // only show the X for close when there is a border
1503 wp->w_popup_close = POPCLOSE_NONE;
1504 }
1505 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001506 else if (STRNCMP(s, "align:", 6) == 0)
1507 {
1508 char_u *arg = s + 6;
1509 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1510 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1511
1512 if (!menu && !item)
1513 return FAIL;
1514 if (wp != NULL && menu)
1515 wp->w_popup_flags |= POPF_INFO_MENU;
1516 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001517 else
1518 return FAIL;
1519 }
1520 return OK;
1521}
1522
1523/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001524 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1525 * is not NULL.
1526 * Return FAIL if the parsing fails.
1527 */
1528 int
1529parse_previewpopup(win_T *wp)
1530{
1531 return parse_popup_option(wp, TRUE);
1532}
1533
1534/*
1535 * Parse the 'completepopup' option and apply the values to window "wp" if it
1536 * is not NULL.
1537 * Return FAIL if the parsing fails.
1538 */
1539 int
1540parse_completepopup(win_T *wp)
1541{
1542 return parse_popup_option(wp, FALSE);
1543}
1544
1545/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001546 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001547 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001548 */
1549 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001550popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001551{
1552 setcursor_mayforce(TRUE);
1553 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1554 if (wp->w_wantline == 0) // cursor in first line
1555 {
1556 wp->w_wantline = 2;
1557 wp->w_popup_pos = POPPOS_TOPLEFT;
1558 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001559
Bram Moolenaar79648732019-07-18 21:43:07 +02001560 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001561 if (wp->w_wantcol > Columns - width)
1562 {
1563 wp->w_wantcol = Columns - width;
1564 if (wp->w_wantcol < 1)
1565 wp->w_wantcol = 1;
1566 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001567 popup_adjust_position(wp);
1568}
1569
1570/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001571 * Set w_wantline and w_wantcol for the a given screen position.
1572 * Caller must take care of running into the window border.
1573 */
1574 void
1575popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1576{
1577 wp->w_wantline = row;
1578 wp->w_wantcol = col;
1579 popup_adjust_position(wp);
1580}
1581
1582/*
1583 * Add a border and lef&right padding.
1584 */
1585 static void
1586add_border_left_right_padding(win_T *wp)
1587{
1588 int i;
1589
1590 for (i = 0; i < 4; ++i)
1591 {
1592 wp->w_popup_border[i] = 1;
1593 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1594 }
1595}
1596
1597/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001598 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001599 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001600 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001601 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001602 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001603 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001604popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001605{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001606 win_T *wp;
1607 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001608 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001609 int new_buffer;
1610 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001611 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001612 int nr;
1613 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001614
Bram Moolenaar79648732019-07-18 21:43:07 +02001615 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001616 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001617 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001618 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001619 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001620 buf = buflist_findnr( argvars[0].vval.v_number);
1621 if (buf == NULL)
1622 {
1623 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1624 return NULL;
1625 }
1626 }
1627 else if (!(argvars[0].v_type == VAR_STRING
1628 && argvars[0].vval.v_string != NULL)
1629 && !(argvars[0].v_type == VAR_LIST
1630 && argvars[0].vval.v_list != NULL))
1631 {
1632 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001633 return NULL;
1634 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001635 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001636 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001637 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001638 return NULL;
1639 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001640 d = argvars[1].vval.v_dict;
1641 }
1642
1643 if (d != NULL)
1644 {
1645 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1646 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1647 else if (type == TYPE_NOTIFICATION)
1648 tabnr = -1; // notifications are global by default
1649 else
1650 tabnr = 0;
1651 if (tabnr > 0)
1652 {
1653 tp = find_tabpage(tabnr);
1654 if (tp == NULL)
1655 {
1656 semsg(_("E997: Tabpage not found: %d"), tabnr);
1657 return NULL;
1658 }
1659 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001660 }
1661
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001662 // Create the window and buffer.
1663 wp = win_alloc_popup_win();
1664 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001665 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 if (rettv != NULL)
1667 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001668 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001669 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001670
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001671 if (buf != NULL)
1672 {
1673 // use existing buffer
1674 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001675 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001676 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001677 buffer_ensure_loaded(buf);
1678 }
1679 else
1680 {
1681 // create a new buffer associated with the popup
1682 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001683 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001684 if (buf == NULL)
1685 return NULL;
1686 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001687
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001688 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001689
Bram Moolenaar46451042019-08-24 15:50:46 +02001690 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001691 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001692 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001693 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001694 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001695 buf->b_p_ul = -1; // no undo
1696 buf->b_p_swf = FALSE; // no swap file
1697 buf->b_p_bl = FALSE; // unlisted buffer
1698 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001699
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001700 // Avoid that 'buftype' is reset when this buffer is entered.
1701 buf->b_p_initialized = TRUE;
1702 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001703 wp->w_p_wrap = TRUE; // 'wrap' is default on
1704 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001705
Bram Moolenaara3fce622019-06-20 02:31:49 +02001706 if (tp != NULL)
1707 {
1708 // popup on specified tab page
1709 wp->w_next = tp->tp_first_popupwin;
1710 tp->tp_first_popupwin = wp;
1711 }
1712 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001713 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001714 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001715 wp->w_next = curtab->tp_first_popupwin;
1716 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001717 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001718 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001719 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001720 win_T *prev = first_popupwin;
1721
1722 // Global popup: add at the end, so that it gets displayed on top of
1723 // older ones with the same zindex. Matters for notifications.
1724 if (first_popupwin == NULL)
1725 first_popupwin = wp;
1726 else
1727 {
1728 while (prev->w_next != NULL)
1729 prev = prev->w_next;
1730 prev->w_next = wp;
1731 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001732 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001733
Bram Moolenaar79648732019-07-18 21:43:07 +02001734 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001735 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001736
Bram Moolenaar79648732019-07-18 21:43:07 +02001737 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001738 {
1739 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 }
1741 if (type == TYPE_ATCURSOR)
1742 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001743 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001744 set_moved_values(wp);
1745 set_moved_columns(wp, FIND_STRING);
1746 }
1747
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001748 if (type == TYPE_BEVAL)
1749 {
1750 wp->w_popup_pos = POPPOS_BOTLEFT;
1751
1752 // by default use the mouse position
1753 wp->w_wantline = mouse_row;
1754 if (wp->w_wantline <= 0) // mouse on first line
1755 {
1756 wp->w_wantline = 2;
1757 wp->w_popup_pos = POPPOS_TOPLEFT;
1758 }
1759 wp->w_wantcol = mouse_col + 1;
1760 set_mousemoved_values(wp);
1761 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1762 }
1763
Bram Moolenaar33796b32019-06-08 16:01:13 +02001764 // set default values
1765 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001766 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001767
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001768 if (type == TYPE_NOTIFICATION)
1769 {
1770 win_T *twp, *nextwin;
1771 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001772
1773 // Try to not overlap with another global popup. Guess we need 3
1774 // more screen lines than buffer lines.
1775 wp->w_wantline = 1;
1776 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1777 {
1778 nextwin = twp->w_next;
1779 if (twp != wp
1780 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1781 && twp->w_winrow <= wp->w_wantline - 1 + height
1782 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1783 {
1784 // move to below this popup and restart the loop to check for
1785 // overlap with other popups
1786 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1787 nextwin = first_popupwin;
1788 }
1789 }
1790 if (wp->w_wantline + height > Rows)
1791 {
1792 // can't avoid overlap, put on top in the hope that message goes
1793 // away soon.
1794 wp->w_wantline = 1;
1795 }
1796
1797 wp->w_wantcol = 10;
1798 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001799 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001800 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001801 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001802 for (i = 0; i < 4; ++i)
1803 wp->w_popup_border[i] = 1;
1804 wp->w_popup_padding[1] = 1;
1805 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001806
1807 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001808 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001809 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1810 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001811 }
1812
Bram Moolenaara730e552019-06-16 19:05:31 +02001813 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001814 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001815 wp->w_popup_pos = POPPOS_CENTER;
1816 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001817 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001818 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001819 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001820 }
1821
Bram Moolenaara730e552019-06-16 19:05:31 +02001822 if (type == TYPE_MENU)
1823 {
1824 typval_T tv;
1825 callback_T callback;
1826
1827 tv.v_type = VAR_STRING;
1828 tv.vval.v_string = (char_u *)"popup_filter_menu";
1829 callback = get_callback(&tv);
1830 if (callback.cb_name != NULL)
1831 set_callback(&wp->w_filter_cb, &callback);
1832
1833 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001834 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001835 }
1836
Bram Moolenaar79648732019-07-18 21:43:07 +02001837 if (type == TYPE_PREVIEW)
1838 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001839 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001840 wp->w_popup_close = POPCLOSE_BUTTON;
1841 for (i = 0; i < 4; ++i)
1842 wp->w_popup_border[i] = 1;
1843 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001844 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1845 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001846# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001847 if (type == TYPE_INFO)
1848 {
1849 wp->w_popup_pos = POPPOS_TOPLEFT;
1850 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1851 wp->w_popup_close = POPCLOSE_BUTTON;
1852 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001853 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001854 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001855# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001856
Bram Moolenaarae943152019-06-16 22:54:14 +02001857 for (i = 0; i < 4; ++i)
1858 VIM_CLEAR(wp->w_border_highlight[i]);
1859 for (i = 0; i < 8; ++i)
1860 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001861 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001862 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001863 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001864
Bram Moolenaar79648732019-07-18 21:43:07 +02001865 if (d != NULL)
1866 // Deal with options.
1867 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001868
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001869#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001870 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1871 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001872#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001873
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001874 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001875
1876 wp->w_vsep_width = 0;
1877
1878 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001879 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001880
1881 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001882}
1883
1884/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001885 * popup_clear()
1886 */
1887 void
1888f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1889{
1890 close_all_popups();
1891}
1892
1893/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001894 * popup_create({text}, {options})
1895 */
1896 void
1897f_popup_create(typval_T *argvars, typval_T *rettv)
1898{
Bram Moolenaar17627312019-06-02 19:53:44 +02001899 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001900}
1901
1902/*
1903 * popup_atcursor({text}, {options})
1904 */
1905 void
1906f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1907{
Bram Moolenaar17627312019-06-02 19:53:44 +02001908 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001909}
1910
1911/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001912 * popup_beval({text}, {options})
1913 */
1914 void
1915f_popup_beval(typval_T *argvars, typval_T *rettv)
1916{
1917 popup_create(argvars, rettv, TYPE_BEVAL);
1918}
1919
1920/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001921 * Invoke the close callback for window "wp" with value "result".
1922 * Careful: The callback may make "wp" invalid!
1923 */
1924 static void
1925invoke_popup_callback(win_T *wp, typval_T *result)
1926{
1927 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001928 typval_T argv[3];
1929
1930 argv[0].v_type = VAR_NUMBER;
1931 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1932
1933 if (result != NULL && result->v_type != VAR_UNKNOWN)
1934 copy_tv(result, &argv[1]);
1935 else
1936 {
1937 argv[1].v_type = VAR_NUMBER;
1938 argv[1].vval.v_number = 0;
1939 }
1940
1941 argv[2].v_type = VAR_UNKNOWN;
1942
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001943 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001944 if (result != NULL)
1945 clear_tv(&argv[1]);
1946 clear_tv(&rettv);
1947}
1948
1949/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001950 * Close popup "wp" and invoke any close callback for it.
1951 */
1952 static void
1953popup_close_and_callback(win_T *wp, typval_T *arg)
1954{
1955 int id = wp->w_id;
1956
1957 if (wp->w_close_cb.cb_name != NULL)
1958 // Careful: This may make "wp" invalid.
1959 invoke_popup_callback(wp, arg);
1960
1961 popup_close(id);
1962}
1963
Bram Moolenaar12034e22019-08-25 22:25:02 +02001964 static void
1965popup_close_with_retval(win_T *wp, int retval)
1966{
1967 typval_T res;
1968
1969 res.v_type = VAR_NUMBER;
1970 res.vval.v_number = retval;
1971 popup_close_and_callback(wp, &res);
1972}
1973
Bram Moolenaar3397f742019-06-02 18:40:06 +02001974/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001975 * Close popup "wp" because of a mouse click.
1976 */
1977 void
1978popup_close_for_mouse_click(win_T *wp)
1979{
Bram Moolenaar12034e22019-08-25 22:25:02 +02001980 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001981}
1982
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001983 static void
1984check_mouse_moved(win_T *wp, win_T *mouse_wp)
1985{
1986 // Close the popup when all if these are true:
1987 // - the mouse is not on this popup
1988 // - "mousemoved" was used
1989 // - the mouse is no longer on the same screen row or the mouse column is
1990 // outside of the relevant text
1991 if (wp != mouse_wp
1992 && wp->w_popup_mouse_row != 0
1993 && (wp->w_popup_mouse_row != mouse_row
1994 || mouse_col < wp->w_popup_mouse_mincol
1995 || mouse_col > wp->w_popup_mouse_maxcol))
1996 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001997 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02001998 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001999 }
2000}
2001
2002/*
2003 * Called when the mouse moved: may close a popup with "mousemoved".
2004 */
2005 void
2006popup_handle_mouse_moved(void)
2007{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002008 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002009 win_T *mouse_wp;
2010 int row = mouse_row;
2011 int col = mouse_col;
2012
2013 // find the window where the mouse is in
2014 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2015
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002016 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2017 {
2018 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002019 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002020 }
2021 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2022 {
2023 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002024 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002025 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002026}
2027
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002028/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002029 * In a filter: check if the typed key is a mouse event that is used for
2030 * dragging the popup.
2031 */
2032 static void
2033filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2034{
2035 int row = mouse_row;
2036 int col = mouse_col;
2037
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002038 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002039 && is_mouse_key(c)
2040 && (wp == popup_dragwin
2041 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2042 // do not consume the key, allow for dragging the popup
2043 rettv->vval.v_number = 0;
2044}
2045
Bram Moolenaara730e552019-06-16 19:05:31 +02002046/*
2047 * popup_filter_menu({text}, {options})
2048 */
2049 void
2050f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2051{
2052 int id = tv_get_number(&argvars[0]);
2053 win_T *wp = win_id2wp(id);
2054 char_u *key = tv_get_string(&argvars[1]);
2055 typval_T res;
2056 int c;
2057 linenr_T old_lnum;
2058
2059 // If the popup has been closed do not consume the key.
2060 if (wp == NULL)
2061 return;
2062
2063 c = *key;
2064 if (c == K_SPECIAL && key[1] != NUL)
2065 c = TO_SPECIAL(key[1], key[2]);
2066
2067 // consume all keys until done
2068 rettv->vval.v_number = 1;
2069 res.v_type = VAR_NUMBER;
2070
2071 old_lnum = wp->w_cursor.lnum;
2072 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2073 --wp->w_cursor.lnum;
2074 if ((c == 'j' || c == 'J' || c == K_DOWN)
2075 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2076 ++wp->w_cursor.lnum;
2077 if (old_lnum != wp->w_cursor.lnum)
2078 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002079 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002080 return;
2081 }
2082
2083 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2084 {
2085 // Cancelled, invoke callback with -1
2086 res.vval.v_number = -1;
2087 popup_close_and_callback(wp, &res);
2088 return;
2089 }
2090 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2091 {
2092 // Invoke callback with current index.
2093 res.vval.v_number = wp->w_cursor.lnum;
2094 popup_close_and_callback(wp, &res);
2095 return;
2096 }
2097
2098 filter_handle_drag(wp, c, rettv);
2099}
2100
2101/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002102 * popup_filter_yesno({text}, {options})
2103 */
2104 void
2105f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2106{
2107 int id = tv_get_number(&argvars[0]);
2108 win_T *wp = win_id2wp(id);
2109 char_u *key = tv_get_string(&argvars[1]);
2110 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002111 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002112
2113 // If the popup has been closed don't consume the key.
2114 if (wp == NULL)
2115 return;
2116
Bram Moolenaara730e552019-06-16 19:05:31 +02002117 c = *key;
2118 if (c == K_SPECIAL && key[1] != NUL)
2119 c = TO_SPECIAL(key[1], key[2]);
2120
Bram Moolenaara42d9452019-06-15 21:46:30 +02002121 // consume all keys until done
2122 rettv->vval.v_number = 1;
2123
Bram Moolenaara730e552019-06-16 19:05:31 +02002124 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002125 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002126 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002127 res.vval.v_number = 0;
2128 else
2129 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002130 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002131 return;
2132 }
2133
2134 // Invoke callback
2135 res.v_type = VAR_NUMBER;
2136 popup_close_and_callback(wp, &res);
2137}
2138
2139/*
2140 * popup_dialog({text}, {options})
2141 */
2142 void
2143f_popup_dialog(typval_T *argvars, typval_T *rettv)
2144{
2145 popup_create(argvars, rettv, TYPE_DIALOG);
2146}
2147
2148/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002149 * popup_menu({text}, {options})
2150 */
2151 void
2152f_popup_menu(typval_T *argvars, typval_T *rettv)
2153{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002154 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002155}
2156
2157/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002158 * popup_notification({text}, {options})
2159 */
2160 void
2161f_popup_notification(typval_T *argvars, typval_T *rettv)
2162{
2163 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2164}
2165
2166/*
2167 * Find the popup window with window-ID "id".
2168 * If the popup window does not exist NULL is returned.
2169 * If the window is not a popup window, and error message is given.
2170 */
2171 static win_T *
2172find_popup_win(int id)
2173{
2174 win_T *wp = win_id2wp(id);
2175
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002176 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002177 {
2178 semsg(_("E993: window %d is not a popup window"), id);
2179 return NULL;
2180 }
2181 return wp;
2182}
2183
2184/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002185 * popup_close({id})
2186 */
2187 void
2188f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2189{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002190 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002191 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002192
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002193 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002194 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002195}
2196
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002197 static void
2198popup_hide(win_T *wp)
2199{
2200 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2201 {
2202 wp->w_popup_flags |= POPF_HIDDEN;
2203 --wp->w_buffer->b_nwindows;
2204 redraw_all_later(NOT_VALID);
2205 popup_mask_refresh = TRUE;
2206 }
2207}
2208
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002209/*
2210 * popup_hide({id})
2211 */
2212 void
2213f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2214{
2215 int id = (int)tv_get_number(argvars);
2216 win_T *wp = find_popup_win(id);
2217
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002218 if (wp != NULL)
2219 popup_hide(wp);
2220}
2221
2222 void
2223popup_show(win_T *wp)
2224{
2225 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002226 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002227 wp->w_popup_flags &= ~POPF_HIDDEN;
2228 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002229 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002230 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002231 }
2232}
2233
2234/*
2235 * popup_show({id})
2236 */
2237 void
2238f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2239{
2240 int id = (int)tv_get_number(argvars);
2241 win_T *wp = find_popup_win(id);
2242
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002243 if (wp != NULL)
2244 popup_show(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002245}
2246
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002247/*
2248 * popup_settext({id}, {text})
2249 */
2250 void
2251f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2252{
2253 int id = (int)tv_get_number(&argvars[0]);
2254 win_T *wp = find_popup_win(id);
2255
2256 if (wp != NULL)
2257 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002258 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2259 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2260 else
2261 {
2262 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002263 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002264 popup_adjust_position(wp);
2265 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002266 }
2267}
2268
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002269 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002270popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002271{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002272 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002273 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002274 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002275 clear_cmdline = TRUE;
2276 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002277
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002278 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002279 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002280}
2281
Bram Moolenaarec583842019-05-26 14:11:23 +02002282/*
2283 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002284 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002285 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002286 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002287popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002288{
2289 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002290 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002291 win_T *prev = NULL;
2292
Bram Moolenaarec583842019-05-26 14:11:23 +02002293 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002294 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002295 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002296 {
2297 if (prev == NULL)
2298 first_popupwin = wp->w_next;
2299 else
2300 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002301 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002302 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002303 }
2304
Bram Moolenaarec583842019-05-26 14:11:23 +02002305 // go through tab-local popups
2306 FOR_ALL_TABPAGES(tp)
2307 popup_close_tabpage(tp, id);
2308}
2309
2310/*
2311 * Close a popup window with Window-id "id" in tabpage "tp".
2312 */
2313 void
2314popup_close_tabpage(tabpage_T *tp, int id)
2315{
2316 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002317 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002318 win_T *prev = NULL;
2319
Bram Moolenaarec583842019-05-26 14:11:23 +02002320 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2321 if (wp->w_id == id)
2322 {
2323 if (prev == NULL)
2324 *root = wp->w_next;
2325 else
2326 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002327 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002328 return;
2329 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002330}
2331
2332 void
2333close_all_popups(void)
2334{
2335 while (first_popupwin != NULL)
2336 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002337 while (curtab->tp_first_popupwin != NULL)
2338 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002339}
2340
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002341/*
2342 * popup_move({id}, {options})
2343 */
2344 void
2345f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2346{
Bram Moolenaarae943152019-06-16 22:54:14 +02002347 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002348 int id = (int)tv_get_number(argvars);
2349 win_T *wp = find_popup_win(id);
2350
2351 if (wp == NULL)
2352 return; // invalid {id}
2353
2354 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2355 {
2356 emsg(_(e_dictreq));
2357 return;
2358 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002359 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002360
Bram Moolenaarae943152019-06-16 22:54:14 +02002361 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002362
2363 if (wp->w_winrow + wp->w_height >= cmdline_row)
2364 clear_cmdline = TRUE;
2365 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002366}
2367
Bram Moolenaarbc133542019-05-29 20:26:46 +02002368/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002369 * popup_setoptions({id}, {options})
2370 */
2371 void
2372f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2373{
2374 dict_T *dict;
2375 int id = (int)tv_get_number(argvars);
2376 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002377 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002378
2379 if (wp == NULL)
2380 return; // invalid {id}
2381
2382 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2383 {
2384 emsg(_(e_dictreq));
2385 return;
2386 }
2387 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002388 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002389
2390 apply_move_options(wp, dict);
2391 apply_general_options(wp, dict);
2392
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002393 if (old_firstline != wp->w_firstline)
2394 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002395 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002396 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002397 popup_adjust_position(wp);
2398}
2399
2400/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002401 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002402 */
2403 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002404f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002405{
2406 dict_T *dict;
2407 int id = (int)tv_get_number(argvars);
2408 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002409 int top_extra;
2410 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002411
2412 if (rettv_dict_alloc(rettv) == OK)
2413 {
2414 if (wp == NULL)
2415 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002416 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002417 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2418
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002419 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002420 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002421 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002422
Bram Moolenaarbc133542019-05-29 20:26:46 +02002423 dict_add_number(dict, "line", wp->w_winrow + 1);
2424 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002425 dict_add_number(dict, "width", wp->w_width + left_extra
2426 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2427 dict_add_number(dict, "height", wp->w_height + top_extra
2428 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002429
2430 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2431 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2432 dict_add_number(dict, "core_width", wp->w_width);
2433 dict_add_number(dict, "core_height", wp->w_height);
2434
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002435 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002436 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002437 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002438 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002439
2440 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002441 }
2442}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002443/*
2444 * popup_locate({row}, {col})
2445 */
2446 void
2447f_popup_locate(typval_T *argvars, typval_T *rettv)
2448{
2449 int row = tv_get_number(&argvars[0]) - 1;
2450 int col = tv_get_number(&argvars[1]) - 1;
2451 win_T *wp;
2452
2453 wp = mouse_find_win(&row, &col, FIND_POPUP);
2454 if (WIN_IS_POPUP(wp))
2455 rettv->vval.v_number = wp->w_id;
2456}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002457
2458/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002459 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2460 */
2461 static void
2462get_padding_border(dict_T *dict, int *array, char *name)
2463{
2464 list_T *list;
2465 int i;
2466
2467 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2468 return;
2469
2470 list = list_alloc();
2471 if (list != NULL)
2472 {
2473 dict_add_list(dict, name, list);
2474 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2475 for (i = 0; i < 4; ++i)
2476 list_append_number(list, array[i]);
2477 }
2478}
2479
2480/*
2481 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2482 */
2483 static void
2484get_borderhighlight(dict_T *dict, win_T *wp)
2485{
2486 list_T *list;
2487 int i;
2488
2489 for (i = 0; i < 4; ++i)
2490 if (wp->w_border_highlight[i] != NULL)
2491 break;
2492 if (i == 4)
2493 return;
2494
2495 list = list_alloc();
2496 if (list != NULL)
2497 {
2498 dict_add_list(dict, "borderhighlight", list);
2499 for (i = 0; i < 4; ++i)
2500 list_append_string(list, wp->w_border_highlight[i], -1);
2501 }
2502}
2503
2504/*
2505 * For popup_getoptions(): add a "borderchars" entry to "dict".
2506 */
2507 static void
2508get_borderchars(dict_T *dict, win_T *wp)
2509{
2510 list_T *list;
2511 int i;
2512 char_u buf[NUMBUFLEN];
2513 int len;
2514
2515 for (i = 0; i < 8; ++i)
2516 if (wp->w_border_char[i] != 0)
2517 break;
2518 if (i == 8)
2519 return;
2520
2521 list = list_alloc();
2522 if (list != NULL)
2523 {
2524 dict_add_list(dict, "borderchars", list);
2525 for (i = 0; i < 8; ++i)
2526 {
2527 len = mb_char2bytes(wp->w_border_char[i], buf);
2528 list_append_string(list, buf, len);
2529 }
2530 }
2531}
2532
2533/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002534 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002535 */
2536 static void
2537get_moved_list(dict_T *dict, win_T *wp)
2538{
2539 list_T *list;
2540
2541 list = list_alloc();
2542 if (list != NULL)
2543 {
2544 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002545 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002546 list_append_number(list, wp->w_popup_mincol);
2547 list_append_number(list, wp->w_popup_maxcol);
2548 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002549 list = list_alloc();
2550 if (list != NULL)
2551 {
2552 dict_add_list(dict, "mousemoved", list);
2553 list_append_number(list, wp->w_popup_mouse_row);
2554 list_append_number(list, wp->w_popup_mouse_mincol);
2555 list_append_number(list, wp->w_popup_mouse_maxcol);
2556 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002557}
2558
2559/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002560 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002561 */
2562 void
2563f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2564{
2565 dict_T *dict;
2566 int id = (int)tv_get_number(argvars);
2567 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002568 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002569 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002570
2571 if (rettv_dict_alloc(rettv) == OK)
2572 {
2573 if (wp == NULL)
2574 return;
2575
2576 dict = rettv->vval.v_dict;
2577 dict_add_number(dict, "line", wp->w_wantline);
2578 dict_add_number(dict, "col", wp->w_wantcol);
2579 dict_add_number(dict, "minwidth", wp->w_minwidth);
2580 dict_add_number(dict, "minheight", wp->w_minheight);
2581 dict_add_number(dict, "maxheight", wp->w_maxheight);
2582 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002583 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002584 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002585 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002586 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002587 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2588 {
2589 proptype_T *pt = text_prop_type_by_id(
2590 wp->w_popup_prop_win->w_buffer,
2591 wp->w_popup_prop_type);
2592
2593 if (pt != NULL)
2594 dict_add_string(dict, "textprop", pt->pt_name);
2595 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2596 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2597 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002598 dict_add_string(dict, "title", wp->w_popup_title);
2599 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002600 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002601 dict_add_number(dict, "mapping",
2602 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002603 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002604 dict_add_number(dict, "cursorline",
2605 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002606 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002607 if (wp->w_scrollbar_highlight != NULL)
2608 dict_add_string(dict, "scrollbarhighlight",
2609 wp->w_scrollbar_highlight);
2610 if (wp->w_thumb_highlight != NULL)
2611 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002612
Bram Moolenaara3fce622019-06-20 02:31:49 +02002613 // find the tabpage that holds this popup
2614 i = 1;
2615 FOR_ALL_TABPAGES(tp)
2616 {
2617 win_T *p;
2618
2619 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2620 if (p->w_id == id)
2621 break;
2622 if (p != NULL)
2623 break;
2624 ++i;
2625 }
2626 if (tp == NULL)
2627 i = -1; // must be global
2628 else if (tp == curtab)
2629 i = 0;
2630 dict_add_number(dict, "tabpage", i);
2631
Bram Moolenaarae943152019-06-16 22:54:14 +02002632 get_padding_border(dict, wp->w_popup_padding, "padding");
2633 get_padding_border(dict, wp->w_popup_border, "border");
2634 get_borderhighlight(dict, wp);
2635 get_borderchars(dict, wp);
2636 get_moved_list(dict, wp);
2637
2638 if (wp->w_filter_cb.cb_name != NULL)
2639 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2640 if (wp->w_close_cb.cb_name != NULL)
2641 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002642
2643 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2644 ++i)
2645 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2646 {
2647 dict_add_string(dict, "pos",
2648 (char_u *)poppos_entries[i].pp_name);
2649 break;
2650 }
2651
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002652 dict_add_string(dict, "close", (char_u *)(
2653 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2654 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2655
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002656# if defined(FEAT_TIMERS)
2657 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2658 ? (long)wp->w_popup_timer->tr_interval : 0L);
2659# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002660 }
2661}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002662
2663 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002664error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002665{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002666 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002667 {
2668 emsg(_("E994: Not allowed in a popup window"));
2669 return TRUE;
2670 }
2671 return FALSE;
2672}
2673
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002674/*
2675 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002676 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002677 */
2678 void
2679popup_reset_handled()
2680{
2681 win_T *wp;
2682
2683 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2684 wp->w_popup_flags &= ~POPF_HANDLED;
2685 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2686 wp->w_popup_flags &= ~POPF_HANDLED;
2687}
2688
2689/*
2690 * Find the next visible popup where POPF_HANDLED is not set.
2691 * Must have called popup_reset_handled() first.
2692 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2693 * popup with the highest zindex.
2694 */
2695 win_T *
2696find_next_popup(int lowest)
2697{
2698 win_T *wp;
2699 win_T *found_wp;
2700 int found_zindex;
2701
2702 found_zindex = lowest ? INT_MAX : 0;
2703 found_wp = NULL;
2704 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2705 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2706 && (lowest ? wp->w_zindex < found_zindex
2707 : wp->w_zindex > found_zindex))
2708 {
2709 found_zindex = wp->w_zindex;
2710 found_wp = wp;
2711 }
2712 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2713 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2714 && (lowest ? wp->w_zindex < found_zindex
2715 : wp->w_zindex > found_zindex))
2716 {
2717 found_zindex = wp->w_zindex;
2718 found_wp = wp;
2719 }
2720
2721 if (found_wp != NULL)
2722 found_wp->w_popup_flags |= POPF_HANDLED;
2723 return found_wp;
2724}
2725
2726/*
2727 * Invoke the filter callback for window "wp" with typed character "c".
2728 * Uses the global "mod_mask" for modifiers.
2729 * Returns the return value of the filter.
2730 * Careful: The filter may make "wp" invalid!
2731 */
2732 static int
2733invoke_popup_filter(win_T *wp, int c)
2734{
2735 int res;
2736 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002737 typval_T argv[3];
2738 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002739 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002740
Bram Moolenaara42d9452019-06-15 21:46:30 +02002741 // Emergency exit: CTRL-C closes the popup.
2742 if (c == Ctrl_C)
2743 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02002744 popup_close_with_retval(wp, -1);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002745 return 1;
2746 }
2747
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002748 argv[0].v_type = VAR_NUMBER;
2749 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2750
2751 // Convert the number to a string, so that the function can use:
2752 // if a:c == "\<F2>"
2753 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2754 argv[1].v_type = VAR_STRING;
2755 argv[1].vval.v_string = vim_strsave(buf);
2756
2757 argv[2].v_type = VAR_UNKNOWN;
2758
Bram Moolenaara42d9452019-06-15 21:46:30 +02002759 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002760 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002761 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002762 popup_highlight_curline(wp);
2763
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002764 res = tv_get_number(&rettv);
2765 vim_free(argv[1].vval.v_string);
2766 clear_tv(&rettv);
2767 return res;
2768}
2769
2770/*
2771 * Called when "c" was typed: invoke popup filter callbacks.
2772 * Returns TRUE when the character was consumed,
2773 */
2774 int
2775popup_do_filter(int c)
2776{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002777 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002778 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002779 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002780 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002781 int state;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002782
2783 if (recursive)
2784 return FALSE;
2785 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002786
2787 popup_reset_handled();
2788
Bram Moolenaarf6396232019-08-24 19:36:00 +02002789 if (c == K_LEFTMOUSE)
2790 {
2791 int row = mouse_row;
2792 int col = mouse_col;
2793
2794 wp = mouse_find_win(&row, &col, FIND_POPUP);
2795 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002796 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002797 }
2798
Bram Moolenaar581ba392019-09-03 22:08:33 +02002799 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002800 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002801 if (wp->w_filter_cb.cb_name != NULL
2802 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002803 res = invoke_popup_filter(wp, c);
2804
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002805 if (must_redraw)
2806 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002807 recursive = FALSE;
2808 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002809 return res;
2810}
2811
Bram Moolenaar3397f742019-06-02 18:40:06 +02002812/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002813 * Return TRUE if there is a popup visible with a filter callback and the
2814 * "mapping" property off.
2815 */
2816 int
2817popup_no_mapping(void)
2818{
2819 int round;
2820 win_T *wp;
2821
2822 for (round = 1; round <= 2; ++round)
2823 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2824 wp != NULL; wp = wp->w_next)
2825 if (wp->w_filter_cb.cb_name != NULL
2826 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2827 return TRUE;
2828 return FALSE;
2829}
2830
2831/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002832 * Called when the cursor moved: check if any popup needs to be closed if the
2833 * cursor moved far enough.
2834 */
2835 void
2836popup_check_cursor_pos()
2837{
2838 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002839
2840 popup_reset_handled();
2841 while ((wp = find_next_popup(TRUE)) != NULL)
2842 if (wp->w_popup_curwin != NULL
2843 && (curwin != wp->w_popup_curwin
2844 || curwin->w_cursor.lnum != wp->w_popup_lnum
2845 || curwin->w_cursor.col < wp->w_popup_mincol
2846 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002847 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002848}
2849
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002850/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002851 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002852 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002853 static void
2854popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002855{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002856 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002857 char_u *cells;
2858 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002859
2860 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002861 return;
2862 if (wp->w_popup_mask_cells != NULL
2863 && wp->w_popup_mask_height == height
2864 && wp->w_popup_mask_width == width)
2865 return; // cache is still valid
2866
2867 vim_free(wp->w_popup_mask_cells);
2868 wp->w_popup_mask_cells = alloc_clear(width * height);
2869 if (wp->w_popup_mask_cells == NULL)
2870 return;
2871 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002872
2873 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2874 {
2875 int cols, cole;
2876 int lines, linee;
2877
2878 li = lio->li_tv.vval.v_list->lv_first;
2879 cols = tv_get_number(&li->li_tv);
2880 if (cols < 0)
2881 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002882 li = li->li_next;
2883 cole = tv_get_number(&li->li_tv);
2884 if (cole < 0)
2885 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002886 li = li->li_next;
2887 lines = tv_get_number(&li->li_tv);
2888 if (lines < 0)
2889 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002890 li = li->li_next;
2891 linee = tv_get_number(&li->li_tv);
2892 if (linee < 0)
2893 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002894
2895 for (row = lines - 1; row < linee && row < height; ++row)
2896 for (col = cols - 1; col < cole && col < width; ++col)
2897 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002898 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002899}
2900
2901/*
2902 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2903 * "col" and "line" are screen coordinates.
2904 */
2905 static int
2906popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2907{
2908 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2909 int line = screenline - wp->w_winrow;
2910
2911 return col >= 0 && col < width
2912 && line >= 0 && line < height
2913 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002914}
2915
2916/*
2917 * Set flags in popup_transparent[] for window "wp" to "val".
2918 */
2919 static void
2920update_popup_transparent(win_T *wp, int val)
2921{
2922 if (wp->w_popup_mask != NULL)
2923 {
2924 int width = popup_width(wp);
2925 int height = popup_height(wp);
2926 listitem_T *lio, *li;
2927 int cols, cole;
2928 int lines, linee;
2929 int col, line;
2930
2931 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2932 {
2933 li = lio->li_tv.vval.v_list->lv_first;
2934 cols = tv_get_number(&li->li_tv);
2935 if (cols < 0)
2936 cols = width + cols + 1;
2937 li = li->li_next;
2938 cole = tv_get_number(&li->li_tv);
2939 if (cole < 0)
2940 cole = width + cole + 1;
2941 li = li->li_next;
2942 lines = tv_get_number(&li->li_tv);
2943 if (lines < 0)
2944 lines = height + lines + 1;
2945 li = li->li_next;
2946 linee = tv_get_number(&li->li_tv);
2947 if (linee < 0)
2948 linee = height + linee + 1;
2949
2950 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002951 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002952 if (cols < 0)
2953 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002954 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002955 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002956 if (lines < 0)
2957 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002958 for (line = lines; line < linee
2959 && line + wp->w_winrow < screen_Rows; ++line)
2960 for (col = cols; col < cole
2961 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002962 popup_transparent[(line + wp->w_winrow) * screen_Columns
2963 + col + wp->w_wincol] = val;
2964 }
2965 }
2966}
2967
2968/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02002969 * Only called when popup window "wp" is hidden: If the window is positioned
2970 * next to a text property, and it is now visible, then unhide the popup.
2971 * We don't check if visible popups become hidden, that is done in
2972 * popup_adjust_position().
2973 * Return TRUE if the popup became unhidden.
2974 */
2975 static int
2976check_popup_unhidden(win_T *wp)
2977{
2978 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
2979 {
2980 textprop_T prop;
2981 linenr_T lnum;
2982
2983 if (find_visible_prop(wp->w_popup_prop_win,
2984 wp->w_popup_prop_type, wp->w_popup_prop_id,
2985 &prop, &lnum) == OK)
2986 {
2987 wp->w_popup_flags &= ~POPF_HIDDEN;
2988 ++wp->w_buffer->b_nwindows;
2989 wp->w_popup_prop_topline = 0; // force repositioning
2990 return TRUE;
2991 }
2992 }
2993 return FALSE;
2994}
2995
2996/*
2997 * Return TRUE if popup_adjust_position() needs to be called for "wp".
2998 * That is when the buffer in the popup was changed, or the popup is following
2999 * a textprop and the referenced buffer was changed.
3000 */
3001 static int
3002popup_need_position_adjust(win_T *wp)
3003{
3004 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3005 return TRUE;
3006 if (win_valid(wp->w_popup_prop_win))
3007 return wp->w_popup_prop_changedtick
3008 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
3009 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline;
3010 return FALSE;
3011}
3012
3013/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003014 * Update "popup_mask" if needed.
3015 * Also recomputes the popup size and positions.
3016 * Also updates "popup_visible".
3017 * Also marks window lines for redrawing.
3018 */
3019 void
3020may_update_popup_mask(int type)
3021{
3022 win_T *wp;
3023 short *mask;
3024 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003025 int redraw_all_popups = FALSE;
3026 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003027
3028 // Need to recompute when switching tabs.
3029 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3030 // (such as the screen size) must have changed.
3031 if (popup_mask_tab != curtab || type >= NOT_VALID)
3032 {
3033 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003034 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003035 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003036
3037 // Check if any popup window buffer has changed and if any popup connected
3038 // to a text property has become visible.
3039 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3040 if (wp->w_popup_flags & POPF_HIDDEN)
3041 popup_mask_refresh |= check_popup_unhidden(wp);
3042 else if (popup_need_position_adjust(wp))
3043 popup_mask_refresh = TRUE;
3044 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3045 if (wp->w_popup_flags & POPF_HIDDEN)
3046 popup_mask_refresh |= check_popup_unhidden(wp);
3047 else if (popup_need_position_adjust(wp))
3048 popup_mask_refresh = TRUE;
3049
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003050 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003051 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003052
3053 // Need to update the mask, something has changed.
3054 popup_mask_refresh = FALSE;
3055 popup_mask_tab = curtab;
3056 popup_visible = FALSE;
3057
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003058 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003059 // If redrawing only what is needed, update "popup_mask_next" and then
3060 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003061 redrawing_all_win = TRUE;
3062 FOR_ALL_WINDOWS(wp)
3063 if (wp->w_redr_type < SOME_VALID)
3064 redrawing_all_win = FALSE;
3065 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003066 mask = popup_mask;
3067 else
3068 mask = popup_mask_next;
3069 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3070
3071 // Find the window with the lowest zindex that hasn't been handled yet,
3072 // so that the window with a higher zindex overwrites the value in
3073 // popup_mask.
3074 popup_reset_handled();
3075 while ((wp = find_next_popup(TRUE)) != NULL)
3076 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003077 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003078 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003079
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003080 popup_visible = TRUE;
3081
Bram Moolenaar12034e22019-08-25 22:25:02 +02003082 // Recompute the position if the text changed. It may make the popup
3083 // hidden if it's attach to a text property that is no longer visible.
3084 if (redraw_all_popups || popup_need_position_adjust(wp))
3085 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003086 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003087 if (wp->w_popup_flags & POPF_HIDDEN)
3088 continue;
3089 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003090
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003091 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003092 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003093 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003094 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003095 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003096 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003097 col < wp->w_wincol + width - wp->w_popup_leftoff
3098 && col < screen_Columns; ++col)
3099 if (wp->w_popup_mask_cells == NULL
3100 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003101 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003102 }
3103
3104 // Only check which lines are to be updated if not already
3105 // updating all lines.
3106 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003107 {
3108 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3109 win_T *prev_wp = NULL;
3110
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003111 for (line = 0; line < screen_Rows; ++line)
3112 {
3113 int col_done = 0;
3114
3115 for (col = 0; col < screen_Columns; ++col)
3116 {
3117 int off = line * screen_Columns + col;
3118
3119 if (popup_mask[off] != popup_mask_next[off])
3120 {
3121 popup_mask[off] = popup_mask_next[off];
3122
3123 if (line >= cmdline_row)
3124 {
3125 // the command line needs to be cleared if text below
3126 // the popup is now visible.
3127 if (!msg_scrolled && popup_mask_next[off] == 0)
3128 clear_cmdline = TRUE;
3129 }
3130 else if (col >= col_done)
3131 {
3132 linenr_T lnum;
3133 int line_cp = line;
3134 int col_cp = col;
3135
3136 // The screen position "line" / "col" needs to be
3137 // redrawn. Figure out what window that is and update
3138 // w_redraw_top and w_redr_bot. Only needs to be done
3139 // once for each window line.
3140 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3141 if (wp != NULL)
3142 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003143 if (wp != prev_wp)
3144 {
3145 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3146 prev_wp = wp;
3147 }
3148
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003149 if (line_cp >= wp->w_height)
3150 // In (or below) status line
3151 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003152 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003153 {
3154 // compute the position in the buffer line from
3155 // the position in the window
3156 mouse_comp_pos(wp, &line_cp, &col_cp,
3157 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003158 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003159 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003160
3161 // This line is going to be redrawn, no need to
3162 // check until the right side of the window.
3163 col_done = wp->w_wincol + wp->w_width - 1;
3164 }
3165 }
3166 }
3167 }
3168 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003169
3170 vim_free(plines_cache);
3171 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003172}
3173
3174/*
3175 * Return a string of "len" spaces in IObuff.
3176 */
3177 static char_u *
3178get_spaces(int len)
3179{
3180 vim_memset(IObuff, ' ', (size_t)len);
3181 IObuff[len] = NUL;
3182 return IObuff;
3183}
3184
3185/*
3186 * Update popup windows. They are drawn on top of normal windows.
3187 * "win_update" is called for each popup window, lowest zindex first.
3188 */
3189 void
3190update_popups(void (*win_update)(win_T *wp))
3191{
3192 win_T *wp;
3193 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003194 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003195 int total_width;
3196 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003197 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003198 int popup_attr;
3199 int border_attr[4];
3200 int border_char[8];
3201 char_u buf[MB_MAXBYTES];
3202 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003203 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003204 int padcol = 0;
3205 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003206 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003207 int sb_thumb_top = 0;
3208 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003209 int attr_scroll = 0;
3210 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003211
3212 // Find the window with the lowest zindex that hasn't been updated yet,
3213 // so that the window with a higher zindex is drawn later, thus goes on
3214 // top.
3215 popup_reset_handled();
3216 while ((wp = find_next_popup(TRUE)) != NULL)
3217 {
3218 // This drawing uses the zindex of the popup window, so that it's on
3219 // top of the text but doesn't draw when another popup with higher
3220 // zindex is on top of the character.
3221 screen_zindex = wp->w_zindex;
3222
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003223 // Set flags in popup_transparent[] for masked cells.
3224 update_popup_transparent(wp, 1);
3225
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003226 // adjust w_winrow and w_wincol for border and padding, since
3227 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003228 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003229 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3230 - wp->w_popup_leftoff;
3231 if (wp->w_wincol + left_extra < 0)
3232 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003233 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003234 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003235
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003236 // Draw the popup text, unless it's off screen.
3237 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003238 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003239 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003240
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003241 // move the cursor into the visible lines, otherwise executing
3242 // commands with win_execute() may cause the text to jump.
3243 if (wp->w_cursor.lnum < wp->w_topline)
3244 wp->w_cursor.lnum = wp->w_topline;
3245 else if (wp->w_cursor.lnum >= wp->w_botline)
3246 wp->w_cursor.lnum = wp->w_botline - 1;
3247 }
3248
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003249 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003250 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003251
Bram Moolenaard529ba52019-07-02 23:13:53 +02003252 total_width = popup_width(wp);
3253 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003254 popup_attr = get_wcr_attr(wp);
3255
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003256 if (wp->w_winrow + total_height > cmdline_row)
3257 wp->w_popup_flags |= POPF_ON_CMDLINE;
3258 else
3259 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3260
3261
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003262 // We can only use these line drawing characters when 'encoding' is
3263 // "utf-8" and 'ambiwidth' is "single".
3264 if (enc_utf8 && *p_ambw == 's')
3265 {
3266 border_char[0] = border_char[2] = 0x2550;
3267 border_char[1] = border_char[3] = 0x2551;
3268 border_char[4] = 0x2554;
3269 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003270 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3271 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003272 border_char[7] = 0x255a;
3273 }
3274 else
3275 {
3276 border_char[0] = border_char[2] = '-';
3277 border_char[1] = border_char[3] = '|';
3278 for (i = 4; i < 8; ++i)
3279 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003280 if (wp->w_popup_flags & POPF_RESIZE)
3281 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003282 }
3283 for (i = 0; i < 8; ++i)
3284 if (wp->w_border_char[i] != 0)
3285 border_char[i] = wp->w_border_char[i];
3286
3287 for (i = 0; i < 4; ++i)
3288 {
3289 border_attr[i] = popup_attr;
3290 if (wp->w_border_highlight[i] != NULL)
3291 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3292 }
3293
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003294 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003295 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003296 if (wp->w_popup_border[0] > 0)
3297 {
3298 // top border
3299 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003300 wincol < 0 ? 0 : wincol, wincol + total_width,
3301 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003302 ? border_char[4] : border_char[0],
3303 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003304 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003305 {
3306 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3307 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003308 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003309 }
3310 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003311 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3312 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003313
Bram Moolenaard529ba52019-07-02 23:13:53 +02003314 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3315 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003316 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003317 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3318 - wp->w_has_scrollbar;
3319 if (padcol < 0)
3320 {
3321 padwidth += padcol;
3322 padcol = 0;
3323 }
3324 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003325 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003326 {
3327 // top padding
3328 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003329 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003330 ' ', ' ', popup_attr);
3331 }
3332
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003333 // Title goes on top of border or padding.
3334 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003335 {
3336 int len = (int)STRLEN(wp->w_popup_title) + 1;
3337 char_u *title = alloc(len);
3338
3339 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3340 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003341 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003342 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003343
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003344 // Compute scrollbar thumb position and size.
3345 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003346 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003347 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3348
Bram Moolenaar68acb412019-06-26 03:40:36 +02003349 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3350 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003351 if (sb_thumb_height == 0)
3352 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003353 if (linecount <= wp->w_height)
3354 // it just fits, avoid divide by zero
3355 sb_thumb_top = 0;
3356 else
3357 sb_thumb_top = (wp->w_topline - 1
3358 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003359 * (wp->w_height - sb_thumb_height)
3360 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003361 if (wp->w_scrollbar_highlight != NULL)
3362 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3363 else
3364 attr_scroll = highlight_attr[HLF_PSB];
3365 if (wp->w_thumb_highlight != NULL)
3366 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3367 else
3368 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003369 }
3370
3371 for (i = wp->w_popup_border[0];
3372 i < total_height - wp->w_popup_border[2]; ++i)
3373 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003374 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003375 // left and right padding only needed next to the body
3376 int do_padding =
3377 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3378 && i < total_height - wp->w_popup_border[2]
3379 - wp->w_popup_padding[2];
3380
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003381 row = wp->w_winrow + i;
3382
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003383 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003384 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003385 {
3386 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003387 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003388 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003389 if (do_padding && wp->w_popup_padding[3] > 0)
3390 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003391 int col = wincol + wp->w_popup_border[3];
3392
Bram Moolenaard529ba52019-07-02 23:13:53 +02003393 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003394 pad_left = wp->w_popup_padding[3];
3395 if (col < 0)
3396 {
3397 pad_left += col;
3398 col = 0;
3399 }
3400 if (pad_left > 0)
3401 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3402 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003403 // scrollbar
3404 if (wp->w_has_scrollbar)
3405 {
3406 int line = i - top_off;
3407 int scroll_col = wp->w_wincol + total_width - 1
3408 - wp->w_popup_border[1];
3409
3410 if (line >= 0 && line < wp->w_height)
3411 screen_putchar(' ', row, scroll_col,
3412 line >= sb_thumb_top
3413 && line < sb_thumb_top + sb_thumb_height
3414 ? attr_thumb : attr_scroll);
3415 else
3416 screen_putchar(' ', row, scroll_col, popup_attr);
3417 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003418 // right border
3419 if (wp->w_popup_border[1] > 0)
3420 {
3421 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003422 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003423 }
3424 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003425 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003426 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003427 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003428 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3429 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003430 }
3431
3432 if (wp->w_popup_padding[2] > 0)
3433 {
3434 // bottom padding
3435 row = wp->w_winrow + wp->w_popup_border[0]
3436 + wp->w_popup_padding[0] + wp->w_height;
3437 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003438 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003439 }
3440
3441 if (wp->w_popup_border[2] > 0)
3442 {
3443 // bottom border
3444 row = wp->w_winrow + total_height - 1;
3445 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003446 wincol < 0 ? 0 : wincol,
3447 wincol + total_width,
3448 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003449 ? border_char[7] : border_char[2],
3450 border_char[2], border_attr[2]);
3451 if (wp->w_popup_border[1] > 0)
3452 {
3453 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003454 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003455 }
3456 }
3457
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003458 if (wp->w_popup_close == POPCLOSE_BUTTON)
3459 {
3460 // close button goes on top of anything at the top-right corner
3461 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003462 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003463 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3464 }
3465
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003466 update_popup_transparent(wp, 0);
3467
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003468 // Back to the normal zindex.
3469 screen_zindex = 0;
3470 }
3471}
3472
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003473/*
3474 * Mark references in callbacks of one popup window.
3475 */
3476 static int
3477set_ref_in_one_popup(win_T *wp, int copyID)
3478{
3479 int abort = FALSE;
3480 typval_T tv;
3481
3482 if (wp->w_close_cb.cb_partial != NULL)
3483 {
3484 tv.v_type = VAR_PARTIAL;
3485 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3486 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3487 }
3488 if (wp->w_filter_cb.cb_partial != NULL)
3489 {
3490 tv.v_type = VAR_PARTIAL;
3491 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3492 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3493 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003494 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003495 return abort;
3496}
3497
3498/*
3499 * Set reference in callbacks of popup windows.
3500 */
3501 int
3502set_ref_in_popups(int copyID)
3503{
3504 int abort = FALSE;
3505 win_T *wp;
3506 tabpage_T *tp;
3507
3508 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3509 abort = abort || set_ref_in_one_popup(wp, copyID);
3510
3511 FOR_ALL_TABPAGES(tp)
3512 {
3513 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3514 abort = abort || set_ref_in_one_popup(wp, copyID);
3515 if (abort)
3516 break;
3517 }
3518 return abort;
3519}
Bram Moolenaar79648732019-07-18 21:43:07 +02003520
3521/*
3522 * Find an existing popup used as the preview window, in the current tab page.
3523 * Return NULL if not found.
3524 */
3525 win_T *
3526popup_find_preview_window(void)
3527{
3528 win_T *wp;
3529
3530 // Preview window popup is always local to tab page.
3531 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3532 if (wp->w_p_pvw)
3533 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003534 return NULL;
3535}
3536
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003537 int
3538popup_is_popup(win_T *wp)
3539{
3540 return wp->w_popup_flags != 0;
3541}
3542
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003543#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003544/*
3545 * Find an existing popup used as the info window, in the current tab page.
3546 * Return NULL if not found.
3547 */
3548 win_T *
3549popup_find_info_window(void)
3550{
3551 win_T *wp;
3552
3553 // info window popup is always local to tab page.
3554 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3555 if (wp->w_popup_flags & POPF_INFO)
3556 return wp;
3557 return NULL;
3558}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003559#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003560
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003561 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003562f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3563{
3564 win_T *wp = popup_find_info_window();
3565
3566 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3567}
3568
3569 void
3570f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003571{
3572 win_T *wp = popup_find_preview_window();
3573
3574 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003575}
3576
Bram Moolenaar79648732019-07-18 21:43:07 +02003577/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003578 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003579 * NOTE: this makes the popup the current window, so that the file can be
3580 * edited. However, it must not remain to be the current window, the caller
3581 * must make sure of that.
3582 */
3583 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003584popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003585{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003586 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003587
3588 if (wp == NULL)
3589 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003590 if (info)
3591 wp->w_popup_flags |= POPF_INFO;
3592 else
3593 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003594
3595 // Set the width to a reasonable value, so that w_topline can be computed.
3596 if (wp->w_minwidth > 0)
3597 wp->w_width = wp->w_minwidth;
3598 else if (wp->w_maxwidth > 0)
3599 wp->w_width = wp->w_maxwidth;
3600 else
3601 wp->w_width = curwin->w_width;
3602
3603 // Will switch to another buffer soon, dummy one can be wiped.
3604 wp->w_buffer->b_locked = FALSE;
3605
3606 win_enter(wp, FALSE);
3607 return OK;
3608}
3609
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003610#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003611/*
3612 * Close any preview popup.
3613 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003614 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003615popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003616{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003617 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003618
3619 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003620 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003621}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003622
3623/*
3624 * Hide the info popup.
3625 */
3626 void
3627popup_hide_info(void)
3628{
3629 win_T *wp = popup_find_info_window();
3630
3631 if (wp != NULL)
3632 popup_hide(wp);
3633}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003634#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003635
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003636/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003637 * Close any popup for a text property associated with "win".
3638 * Return TRUE if a popup was closed.
3639 */
3640 int
3641popup_win_closed(win_T *win)
3642{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003643 int round;
3644 win_T *wp;
3645 win_T *next;
3646 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003647
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003648 for (round = 1; round <= 2; ++round)
3649 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3650 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003651 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003652 next = wp->w_next;
3653 if (wp->w_popup_prop_win == win)
3654 {
3655 popup_close_with_retval(wp, -1);
3656 ret = TRUE;
3657 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003658 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003659 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003660}
3661
3662/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003663 * Set the title of the popup window to the file name.
3664 */
3665 void
3666popup_set_title(win_T *wp)
3667{
3668 if (wp->w_buffer->b_fname != NULL)
3669 {
3670 char_u dirname[MAXPATHL];
3671 size_t len;
3672
3673 mch_dirname(dirname, MAXPATHL);
3674 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3675
3676 vim_free(wp->w_popup_title);
3677 len = STRLEN(wp->w_buffer->b_fname) + 3;
3678 wp->w_popup_title = alloc((int)len);
3679 if (wp->w_popup_title != NULL)
3680 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3681 wp->w_buffer->b_fname);
3682 redraw_win_later(wp, VALID);
3683 }
3684}
3685
3686/*
3687 * If there is a preview window, update the title.
3688 * Used after changing directory.
3689 */
3690 void
3691popup_update_preview_title(void)
3692{
3693 win_T *wp = popup_find_preview_window();
3694
3695 if (wp != NULL)
3696 popup_set_title(wp);
3697}
3698
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003699#endif // FEAT_TEXT_PROP