blob: 212076bae30f7a46fef9d4008f85b1588cb786e6 [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
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
100 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
101 ++i, li = li->li_next)
102 {
103 nr = (int)tv_get_number(&li->li_tv);
104 if (nr >= 0)
105 array[i] = nr > max_val ? max_val : nr;
106 }
107 }
108 }
109}
110
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200111/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200112 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200113 */
114 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200115set_moved_values(win_T *wp)
116{
117 wp->w_popup_curwin = curwin;
118 wp->w_popup_lnum = curwin->w_cursor.lnum;
119 wp->w_popup_mincol = curwin->w_cursor.col;
120 wp->w_popup_maxcol = curwin->w_cursor.col;
121}
122
123/*
124 * Used when popup options contain "moved" with "word" or "WORD".
125 */
126 static void
127set_moved_columns(win_T *wp, int flags)
128{
129 char_u *ptr;
130 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
131
132 if (len > 0)
133 {
134 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
135 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
136 }
137}
138
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200139/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200140 * Used when popup options contain "mousemoved": set default moved values.
141 */
142 static void
143set_mousemoved_values(win_T *wp)
144{
145 wp->w_popup_mouse_row = mouse_row;
146 wp->w_popup_mouse_mincol = mouse_col;
147 wp->w_popup_mouse_maxcol = mouse_col;
148}
149
150/*
151 * Used when popup options contain "moved" with "word" or "WORD".
152 */
153 static void
154set_mousemoved_columns(win_T *wp, int flags)
155{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200156 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200157 char_u *text;
158 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 pos_T pos;
160 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200161
162 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200163 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200165 // convert text column to mouse column
166 pos.col = col;
167 pos.coladd = 0;
168 getvcol(textwp, &pos, &mcol, NULL, NULL);
169 wp->w_popup_mouse_mincol = mcol;
170
Bram Moolenaar10727682019-07-12 13:59:20 +0200171 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200172 getvcol(textwp, &pos, NULL, NULL, &mcol);
173 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200174 vim_free(text);
175 }
176}
177
178/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200179 * Return TRUE if "row"/"col" is on the border of the popup.
180 * The values are relative to the top-left corner.
181 */
182 int
183popup_on_border(win_T *wp, int row, int col)
184{
185 return (row == 0 && wp->w_popup_border[0] > 0)
186 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
187 || (col == 0 && wp->w_popup_border[3] > 0)
188 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
189}
190
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200191/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200192 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
193 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Caller should check the left mouse button was clicked.
196 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 */
198 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200199popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200201 if (wp->w_popup_close == POPCLOSE_BUTTON
202 && row == 0 && col == popup_width(wp) - 1)
203 {
204 popup_close_for_mouse_click(wp);
205 return TRUE;
206 }
207 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200208}
209
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210// Values set when dragging a popup window starts.
211static int drag_start_row;
212static int drag_start_col;
213static int drag_start_wantline;
214static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200215static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200216
217/*
218 * Mouse down on border of popup window: start dragging it.
219 * Uses mouse_col and mouse_row.
220 */
221 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200222popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200223{
224 drag_start_row = mouse_row;
225 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200226 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200227 drag_start_wantline = wp->w_winrow + 1;
228 else
229 drag_start_wantline = wp->w_wantline;
230 if (wp->w_wantcol == 0)
231 drag_start_wantcol = wp->w_wincol + 1;
232 else
233 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200234
235 // Stop centering the popup
236 if (wp->w_popup_pos == POPPOS_CENTER)
237 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200238
239 drag_on_resize_handle = wp->w_popup_border[1] > 0
240 && wp->w_popup_border[2] > 0
241 && row == popup_height(wp) - 1
242 && col == popup_width(wp) - 1;
243
244 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
245 {
246 if (wp->w_popup_pos == POPPOS_TOPRIGHT
247 || wp->w_popup_pos == POPPOS_BOTRIGHT)
248 wp->w_wantcol = wp->w_wincol + 1;
249 if (wp->w_popup_pos == POPPOS_BOTLEFT)
250 wp->w_wantline = wp->w_winrow + 1;
251 wp->w_popup_pos = POPPOS_TOPLEFT;
252 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253}
254
255/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200256 * Mouse moved while dragging a popup window: adjust the window popup position
257 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 */
259 void
260popup_drag(win_T *wp)
261{
262 // The popup may be closed before dragging stops.
263 if (!win_valid_popup(wp))
264 return;
265
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200266 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
267 {
268 int width_inc = mouse_col - drag_start_col;
269 int height_inc = mouse_row - drag_start_row;
270
271 if (width_inc != 0)
272 {
273 int width = wp->w_width + width_inc;
274
275 if (width < 1)
276 width = 1;
277 wp->w_minwidth = width;
278 wp->w_maxwidth = width;
279 drag_start_col = mouse_col;
280 }
281
282 if (height_inc != 0)
283 {
284 int height = wp->w_height + height_inc;
285
286 if (height < 1)
287 height = 1;
288 wp->w_minheight = height;
289 wp->w_maxheight = height;
290 drag_start_row = mouse_row;
291 }
292
293 popup_adjust_position(wp);
294 return;
295 }
296
297 if (!(wp->w_popup_flags & POPF_DRAG))
298 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200299 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
300 if (wp->w_wantline < 1)
301 wp->w_wantline = 1;
302 if (wp->w_wantline > Rows)
303 wp->w_wantline = Rows;
304 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
305 if (wp->w_wantcol < 1)
306 wp->w_wantcol = 1;
307 if (wp->w_wantcol > Columns)
308 wp->w_wantcol = Columns;
309
310 popup_adjust_position(wp);
311}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200312
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200313/*
314 * Set w_firstline to match the current "wp->w_topline".
315 */
316 void
317popup_set_firstline(win_T *wp)
318{
319 int height = wp->w_height;
320
321 wp->w_firstline = wp->w_topline;
322 popup_adjust_position(wp);
323
324 // we don't want the popup to get smaller, decrement the first line
325 // until it doesn't
326 while (wp->w_firstline > 1 && wp->w_height < height)
327 {
328 --wp->w_firstline;
329 popup_adjust_position(wp);
330 }
331}
332
333/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200334 * Return TRUE if the position is in the popup window scrollbar.
335 */
336 int
337popup_is_in_scrollbar(win_T *wp, int row, int col)
338{
339 return wp->w_has_scrollbar
340 && row >= wp->w_popup_border[0]
341 && row < popup_height(wp) - wp->w_popup_border[2]
342 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
343}
344
345
346/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200347 * Handle a click in a popup window, if it is in the scrollbar.
348 */
349 void
350popup_handle_scrollbar_click(win_T *wp, int row, int col)
351{
352 int height = popup_height(wp);
353 int old_topline = wp->w_topline;
354
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200355 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200356 {
357 if (row >= height / 2)
358 {
359 // Click in lower half, scroll down.
360 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
361 ++wp->w_topline;
362 }
363 else if (wp->w_topline > 1)
364 // click on upper half, scroll up.
365 --wp->w_topline;
366 if (wp->w_topline != old_topline)
367 {
368 popup_set_firstline(wp);
369 redraw_win_later(wp, NOT_VALID);
370 }
371 }
372}
373
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200374#if defined(FEAT_TIMERS)
375 static void
376popup_add_timeout(win_T *wp, int time)
377{
378 char_u cbbuf[50];
379 char_u *ptr = cbbuf;
380 typval_T tv;
381
382 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
383 "{_ -> popup_close(%d)}", wp->w_id);
384 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
385 {
386 wp->w_popup_timer = create_timer(time, 0);
387 wp->w_popup_timer->tr_callback = get_callback(&tv);
388 clear_tv(&tv);
389 }
390}
391#endif
392
Bram Moolenaar17627312019-06-02 19:53:44 +0200393/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200394 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200395 */
396 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200397apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200399 int nr;
400 char_u *str;
401 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200402
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200403 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200404 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200405 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200406 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200407 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200408 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200409 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200410 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200411
412 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200413 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200414 wp->w_wantline = nr;
415 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200416 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200417 wp->w_wantcol = nr;
418
419 di = dict_find(d, (char_u *)"fixed", -1);
420 if (di != NULL)
421 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
422
423 str = dict_get_string(d, (char_u *)"pos", FALSE);
424 if (str != NULL)
425 {
426 for (nr = 0;
427 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
428 ++nr)
429 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
430 {
431 wp->w_popup_pos = poppos_entries[nr].pp_val;
432 nr = -1;
433 break;
434 }
435 if (nr != -1)
436 semsg(_(e_invarg2), str);
437 }
438
439 str = dict_get_string(d, (char_u *)"textprop", FALSE);
440 if (str != NULL)
441 {
442 wp->w_popup_prop_type = 0;
443 if (*str != NUL)
444 {
445 nr = find_prop_type_id(str, wp->w_buffer);
446 if (nr <= 0)
447 nr = find_prop_type_id(str, NULL);
448 if (nr <= 0)
449 semsg(_(e_invarg2), str);
450 else
451 {
452 wp->w_popup_prop_type = nr;
453 wp->w_popup_prop_win = curwin;
454
455 di = dict_find(d, (char_u *)"textpropwin", -1);
456 if (di != NULL)
457 {
458 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
459 if (win_valid(wp->w_popup_prop_win))
460 wp->w_popup_prop_win = curwin;
461 }
462 }
463 }
464 }
465
466 di = dict_find(d, (char_u *)"textpropid", -1);
467 if (di != NULL)
468 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200469}
470
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200471 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200472handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
473{
474 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
475 {
476 char_u *s = di->di_tv.vval.v_string;
477 int flags = 0;
478
479 if (STRCMP(s, "word") == 0)
480 flags = FIND_IDENT | FIND_STRING;
481 else if (STRCMP(s, "WORD") == 0)
482 flags = FIND_STRING;
483 else if (STRCMP(s, "expr") == 0)
484 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
485 else if (STRCMP(s, "any") != 0)
486 semsg(_(e_invarg2), s);
487 if (flags != 0)
488 {
489 if (mousemoved)
490 set_mousemoved_columns(wp, flags);
491 else
492 set_moved_columns(wp, flags);
493 }
494 }
495 else if (di->di_tv.v_type == VAR_LIST
496 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200497 && (di->di_tv.vval.v_list->lv_len == 2
498 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200499 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200500 list_T *l = di->di_tv.vval.v_list;
501 listitem_T *li = l->lv_first;
502 int mincol;
503 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200504
Bram Moolenaara1396152019-10-20 18:46:05 +0200505 if (di->di_tv.vval.v_list->lv_len == 3)
506 {
507 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
508
509 // Three numbers, might be from popup_getoptions().
510 if (mousemoved)
511 wp->w_popup_mouse_row = nr;
512 else
513 wp->w_popup_lnum = nr;
514 li = li->li_next;
515 }
516
517 mincol = tv_get_number(&li->li_tv);
518 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200519 if (mousemoved)
520 {
521 wp->w_popup_mouse_mincol = mincol;
522 wp->w_popup_mouse_maxcol = maxcol;
523 }
524 else
525 {
526 wp->w_popup_mincol = mincol;
527 wp->w_popup_maxcol = maxcol;
528 }
529 }
530 else
531 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
532}
533
534 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200535check_highlight(dict_T *dict, char *name, char_u **pval)
536{
537 dictitem_T *di;
538 char_u *str;
539
540 di = dict_find(dict, (char_u *)name, -1);
541 if (di != NULL)
542 {
543 if (di->di_tv.v_type != VAR_STRING)
544 semsg(_(e_invargval), name);
545 else
546 {
547 str = tv_get_string(&di->di_tv);
548 if (*str != NUL)
549 *pval = vim_strsave(str);
550 }
551 }
552}
553
Bram Moolenaarae943152019-06-16 22:54:14 +0200554/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200555 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200556 */
557 static void
558popup_show_curline(win_T *wp)
559{
560 if (wp->w_cursor.lnum < wp->w_topline)
561 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200562 else if (wp->w_cursor.lnum >= wp->w_botline
563 && (curwin->w_valid & VALID_BOTLINE))
564 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200565 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200566 if (wp->w_topline < 1)
567 wp->w_topline = 1;
568 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
569 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200570 while (wp->w_topline < wp->w_cursor.lnum
571 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
572 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
573 > wp->w_height)
574 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200575 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200576
577 // Don't use "firstline" now.
578 wp->w_firstline = 0;
579}
580
581/*
582 * Get the sign group name for window "wp".
583 * Returns a pointer to a static buffer, overwritten on the next call.
584 */
585 static char_u *
586popup_get_sign_name(win_T *wp)
587{
588 static char buf[30];
589
590 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
591 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200592}
593
594/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200595 * Highlight the line with the cursor.
596 * Also scrolls the text to put the cursor line in view.
597 */
598 static void
599popup_highlight_curline(win_T *wp)
600{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200601 int sign_id = 0;
602 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200603
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200604 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200605
606 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
607 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200608 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200609
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200610 if (!sign_exists_by_name(sign_name))
611 {
612 char *linehl = "PopupSelected";
613
614 if (syn_name2id((char_u *)linehl) == 0)
615 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200616 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200617 }
618
619 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
620 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
621 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200623 else
624 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200625 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200626}
627
628/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200629 * Shared between popup_create() and f_popup_setoptions().
630 */
631 static void
632apply_general_options(win_T *wp, dict_T *dict)
633{
634 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200635 int nr;
636 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200637
Bram Moolenaarae943152019-06-16 22:54:14 +0200638 // TODO: flip
639
640 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200641 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200642 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200643 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200644 if (wp->w_firstline < 0)
645 wp->w_firstline = -1;
646 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200647
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200648 di = dict_find(dict, (char_u *)"scrollbar", -1);
649 if (di != NULL)
650 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
651
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200652 str = dict_get_string(dict, (char_u *)"title", FALSE);
653 if (str != NULL)
654 {
655 vim_free(wp->w_popup_title);
656 wp->w_popup_title = vim_strsave(str);
657 }
658
Bram Moolenaar402502d2019-05-30 22:07:36 +0200659 di = dict_find(dict, (char_u *)"wrap", -1);
660 if (di != NULL)
661 {
662 nr = dict_get_number(dict, (char_u *)"wrap");
663 wp->w_p_wrap = nr != 0;
664 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200665
Bram Moolenaara42d9452019-06-15 21:46:30 +0200666 di = dict_find(dict, (char_u *)"drag", -1);
667 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200668 {
669 nr = dict_get_number(dict, (char_u *)"drag");
670 if (nr)
671 wp->w_popup_flags |= POPF_DRAG;
672 else
673 wp->w_popup_flags &= ~POPF_DRAG;
674 }
675
676 di = dict_find(dict, (char_u *)"resize", -1);
677 if (di != NULL)
678 {
679 nr = dict_get_number(dict, (char_u *)"resize");
680 if (nr)
681 wp->w_popup_flags |= POPF_RESIZE;
682 else
683 wp->w_popup_flags &= ~POPF_RESIZE;
684 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200685
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200686 di = dict_find(dict, (char_u *)"close", -1);
687 if (di != NULL)
688 {
689 int ok = TRUE;
690
691 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
692 {
693 char_u *s = di->di_tv.vval.v_string;
694
695 if (STRCMP(s, "none") == 0)
696 wp->w_popup_close = POPCLOSE_NONE;
697 else if (STRCMP(s, "button") == 0)
698 wp->w_popup_close = POPCLOSE_BUTTON;
699 else if (STRCMP(s, "click") == 0)
700 wp->w_popup_close = POPCLOSE_CLICK;
701 else
702 ok = FALSE;
703 }
704 else
705 ok = FALSE;
706 if (!ok)
707 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
708 }
709
Bram Moolenaarae943152019-06-16 22:54:14 +0200710 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
711 if (str != NULL)
712 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
713 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200714
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200715 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
716 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200717 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
718 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200719
Bram Moolenaar790498b2019-06-01 22:15:29 +0200720 di = dict_find(dict, (char_u *)"borderhighlight", -1);
721 if (di != NULL)
722 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200723 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200724 emsg(_(e_listreq));
725 else
726 {
727 list_T *list = di->di_tv.vval.v_list;
728 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200729 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200730
Bram Moolenaar403d0902019-07-17 21:37:32 +0200731 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
732 ++i, li = li->li_next)
733 {
734 str = tv_get_string(&li->li_tv);
735 if (*str != NUL)
736 wp->w_border_highlight[i] = vim_strsave(str);
737 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200738 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
739 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200740 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200741 vim_strsave(wp->w_border_highlight[0]);
742 }
743 }
744
Bram Moolenaar790498b2019-06-01 22:15:29 +0200745 di = dict_find(dict, (char_u *)"borderchars", -1);
746 if (di != NULL)
747 {
748 if (di->di_tv.v_type != VAR_LIST)
749 emsg(_(e_listreq));
750 else
751 {
752 list_T *list = di->di_tv.vval.v_list;
753 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200754 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200755
756 if (list != NULL)
757 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
758 ++i, li = li->li_next)
759 {
760 str = tv_get_string(&li->li_tv);
761 if (*str != NUL)
762 wp->w_border_char[i] = mb_ptr2char(str);
763 }
764 if (list->lv_len == 1)
765 for (i = 1; i < 8; ++i)
766 wp->w_border_char[i] = wp->w_border_char[0];
767 if (list->lv_len == 2)
768 {
769 for (i = 4; i < 8; ++i)
770 wp->w_border_char[i] = wp->w_border_char[1];
771 for (i = 1; i < 4; ++i)
772 wp->w_border_char[i] = wp->w_border_char[0];
773 }
774 }
775 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200776
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200777 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
778 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
779
Bram Moolenaarae943152019-06-16 22:54:14 +0200780 di = dict_find(dict, (char_u *)"zindex", -1);
781 if (di != NULL)
782 {
783 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
784 if (wp->w_zindex < 1)
785 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
786 if (wp->w_zindex > 32000)
787 wp->w_zindex = 32000;
788 }
789
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200790 di = dict_find(dict, (char_u *)"mask", -1);
791 if (di != NULL)
792 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200793 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200794
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200795 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200796 {
797 listitem_T *li;
798
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200799 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200800 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
801 li = li->li_next)
802 {
803 if (li->li_tv.v_type != VAR_LIST
804 || li->li_tv.vval.v_list == NULL
805 || li->li_tv.vval.v_list->lv_len != 4)
806 {
807 ok = FALSE;
808 break;
809 }
810 }
811 }
812 if (ok)
813 {
814 wp->w_popup_mask = di->di_tv.vval.v_list;
815 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200816 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200817 }
818 else
819 semsg(_(e_invargval), "mask");
820 }
821
Bram Moolenaarae943152019-06-16 22:54:14 +0200822#if defined(FEAT_TIMERS)
823 // Add timer to close the popup after some time.
824 nr = dict_get_number(dict, (char_u *)"time");
825 if (nr > 0)
826 popup_add_timeout(wp, nr);
827#endif
828
Bram Moolenaar3397f742019-06-02 18:40:06 +0200829 di = dict_find(dict, (char_u *)"moved", -1);
830 if (di != NULL)
831 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200832 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200833 handle_moved_argument(wp, di, FALSE);
834 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200835
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200836 di = dict_find(dict, (char_u *)"mousemoved", -1);
837 if (di != NULL)
838 {
839 set_mousemoved_values(wp);
840 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200841 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200842
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200843 di = dict_find(dict, (char_u *)"cursorline", -1);
844 if (di != NULL)
845 {
846 if (di->di_tv.v_type == VAR_NUMBER)
847 {
848 if (di->di_tv.vval.v_number != 0)
849 wp->w_popup_flags |= POPF_CURSORLINE;
850 else
851 wp->w_popup_flags &= ~POPF_CURSORLINE;
852 }
853 else
854 semsg(_(e_invargval), "cursorline");
855 }
856
Bram Moolenaarae943152019-06-16 22:54:14 +0200857 di = dict_find(dict, (char_u *)"filter", -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_filter_cb);
865 set_callback(&wp->w_filter_cb, &callback);
866 }
867 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200868 di = dict_find(dict, (char_u *)"mapping", -1);
869 if (di != NULL)
870 {
871 nr = dict_get_number(dict, (char_u *)"mapping");
872 if (nr)
873 wp->w_popup_flags |= POPF_MAPPING;
874 else
875 wp->w_popup_flags &= ~POPF_MAPPING;
876 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200877
Bram Moolenaar581ba392019-09-03 22:08:33 +0200878 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
879 if (str != NULL)
880 {
881 if (STRCMP(str, "a") == 0)
882 wp->w_filter_mode = MODE_ALL;
883 else
884 wp->w_filter_mode = mode_str2flags(str);
885 }
886
Bram Moolenaarae943152019-06-16 22:54:14 +0200887 di = dict_find(dict, (char_u *)"callback", -1);
888 if (di != NULL)
889 {
890 callback_T callback = get_callback(&di->di_tv);
891
892 if (callback.cb_name != NULL)
893 {
894 free_callback(&wp->w_close_cb);
895 set_callback(&wp->w_close_cb, &callback);
896 }
897 }
898}
899
900/*
901 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200902 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200903 */
904 static void
905apply_options(win_T *wp, dict_T *dict)
906{
907 int nr;
908
909 apply_move_options(wp, dict);
910 apply_general_options(wp, dict);
911
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200912 nr = dict_get_number(dict, (char_u *)"hidden");
913 if (nr > 0)
914 {
915 wp->w_popup_flags |= POPF_HIDDEN;
916 --wp->w_buffer->b_nwindows;
917 }
918
Bram Moolenaar33796b32019-06-08 16:01:13 +0200919 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200920 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200921}
922
923/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200924 * Add lines to the popup from a list of strings.
925 */
926 static void
927add_popup_strings(buf_T *buf, list_T *l)
928{
929 listitem_T *li;
930 linenr_T lnum = 0;
931 char_u *p;
932
933 for (li = l->lv_first; li != NULL; li = li->li_next)
934 if (li->li_tv.v_type == VAR_STRING)
935 {
936 p = li->li_tv.vval.v_string;
937 ml_append_buf(buf, lnum++,
938 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
939 }
940}
941
942/*
943 * Add lines to the popup from a list of dictionaries.
944 */
945 static void
946add_popup_dicts(buf_T *buf, list_T *l)
947{
948 listitem_T *li;
949 listitem_T *pli;
950 linenr_T lnum = 0;
951 char_u *p;
952 dict_T *dict;
953
954 // first add the text lines
955 for (li = l->lv_first; li != NULL; li = li->li_next)
956 {
957 if (li->li_tv.v_type != VAR_DICT)
958 {
959 emsg(_(e_dictreq));
960 return;
961 }
962 dict = li->li_tv.vval.v_dict;
963 p = dict == NULL ? NULL
964 : dict_get_string(dict, (char_u *)"text", FALSE);
965 ml_append_buf(buf, lnum++,
966 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
967 }
968
969 // add the text properties
970 lnum = 1;
971 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
972 {
973 dictitem_T *di;
974 list_T *plist;
975
976 dict = li->li_tv.vval.v_dict;
977 di = dict_find(dict, (char_u *)"props", -1);
978 if (di != NULL)
979 {
980 if (di->di_tv.v_type != VAR_LIST)
981 {
982 emsg(_(e_listreq));
983 return;
984 }
985 plist = di->di_tv.vval.v_list;
986 if (plist != NULL)
987 {
988 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
989 {
990 if (pli->li_tv.v_type != VAR_DICT)
991 {
992 emsg(_(e_dictreq));
993 return;
994 }
995 dict = pli->li_tv.vval.v_dict;
996 if (dict != NULL)
997 {
998 int col = dict_get_number(dict, (char_u *)"col");
999
1000 prop_add_common( lnum, col, dict, buf, NULL);
1001 }
1002 }
1003 }
1004 }
1005 }
1006}
1007
1008/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001009 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1010 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001011 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001012popup_top_extra(win_T *wp)
1013{
1014 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1015
1016 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1017 return 1;
1018 return extra;
1019}
1020
1021/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001022 * Return the height of popup window "wp", including border and padding.
1023 */
1024 int
1025popup_height(win_T *wp)
1026{
1027 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001028 + popup_top_extra(wp)
1029 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001030}
1031
1032/*
1033 * Return the width of popup window "wp", including border, padding and
1034 * scrollbar.
1035 */
1036 int
1037popup_width(win_T *wp)
1038{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001039 // w_leftcol is how many columns of the core are left of the screen
1040 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001041 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001042 + popup_extra_width(wp)
1043 + wp->w_popup_rightoff;
1044}
1045
1046/*
1047 * Return the extra width of popup window "wp": border, padding and scrollbar.
1048 */
1049 int
1050popup_extra_width(win_T *wp)
1051{
1052 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1053 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1054 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001055}
1056
1057/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001058 * Adjust the position and size of the popup to fit on the screen.
1059 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001060 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001061popup_adjust_position(win_T *wp)
1062{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001063 linenr_T lnum;
1064 int wrapped = 0;
1065 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001066 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001067 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001068 int center_vert = FALSE;
1069 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001070 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001071 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001072 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1073 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1074 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1075 int extra_height = top_extra + bot_extra;
1076 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001077 int org_winrow = wp->w_winrow;
1078 int org_wincol = wp->w_wincol;
1079 int org_width = wp->w_width;
1080 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001081 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001082 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001083 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001084 int wantline = wp->w_wantline; // adjusted for textprop
1085 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001086 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001087
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001088 wp->w_winrow = 0;
1089 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001090 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001091 wp->w_popup_leftoff = 0;
1092 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001093
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001094 // May need to update the "cursorline" highlighting, which may also change
1095 // "topline"
1096 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1097 popup_highlight_curline(wp);
1098
Bram Moolenaar12034e22019-08-25 22:25:02 +02001099 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1100 {
1101 win_T *prop_win = wp->w_popup_prop_win;
1102 textprop_T prop;
1103 linenr_T prop_lnum;
1104 pos_T pos;
1105 int screen_row;
1106 int screen_scol;
1107 int screen_ccol;
1108 int screen_ecol;
1109
1110 // Popup window is positioned relative to a text property.
1111 if (find_visible_prop(prop_win,
1112 wp->w_popup_prop_type, wp->w_popup_prop_id,
1113 &prop, &prop_lnum) == FAIL)
1114 {
1115 // Text property is no longer visible, hide the popup.
1116 // Unhiding the popup is done in check_popup_unhidden().
1117 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1118 {
1119 wp->w_popup_flags |= POPF_HIDDEN;
1120 --wp->w_buffer->b_nwindows;
1121 if (win_valid(wp->w_popup_prop_win))
1122 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1123 }
1124 return;
1125 }
1126
1127 // Compute the desired position from the position of the text
1128 // property. Use "wantline" and "wantcol" as offsets.
1129 pos.lnum = prop_lnum;
1130 pos.col = prop.tp_col;
1131 if (wp->w_popup_pos == POPPOS_TOPLEFT
1132 || wp->w_popup_pos == POPPOS_BOTLEFT)
1133 pos.col += prop.tp_len - 1;
1134 textpos2screenpos(prop_win, &pos, &screen_row,
1135 &screen_scol, &screen_ccol, &screen_ecol);
1136
1137 if (wp->w_popup_pos == POPPOS_TOPLEFT
1138 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1139 // below the text
1140 wantline = screen_row + wantline + 1;
1141 else
1142 // above the text
1143 wantline = screen_row + wantline - 1;
1144 center_vert = FALSE;
1145 if (wp->w_popup_pos == POPPOS_TOPLEFT
1146 || wp->w_popup_pos == POPPOS_BOTLEFT)
1147 // right of the text
1148 wantcol = screen_ecol + wantcol;
1149 else
1150 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001151 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001152 use_wantcol = TRUE;
1153 }
1154 else
1155 {
1156 // If no line was specified default to vertical centering.
1157 if (wantline == 0)
1158 center_vert = TRUE;
1159 else if (wantline < 0)
1160 // If "wantline" is negative it actually means zero.
1161 wantline = 0;
1162 if (wantcol < 0)
1163 // If "wantcol" is negative it actually means zero.
1164 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001165 }
1166
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001167 if (wp->w_popup_pos == POPPOS_CENTER)
1168 {
1169 // center after computing the size
1170 center_vert = TRUE;
1171 center_hor = TRUE;
1172 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001173 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001174 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001175 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1176 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001177 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001178 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001179 if (wp->w_winrow >= Rows)
1180 wp->w_winrow = Rows - 1;
1181 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001182
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001183 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001184 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001185 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1186 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001187 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001188 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001189 if (wp->w_wincol >= Columns - 3)
1190 wp->w_wincol = Columns - 3;
1191 }
1192 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001193
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001194 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001195 // When left aligned use the space available, but shift to the left when we
1196 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001197 maxspace = Columns - wp->w_wincol - left_extra;
1198 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001199 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001200 {
1201 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001202 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001203 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001204
Bram Moolenaar8d241042019-06-12 23:40:01 +02001205 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001206 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001207 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001208 if (wp->w_topline < 1)
1209 wp->w_topline = 1;
1210 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001211 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1212
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001213 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001214 // Use a minimum width of one, so that something shows when there is no
1215 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001216 // When "firstline" is -1 then start with the last buffer line and go
1217 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001218 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001219 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001220 if (wp->w_firstline < 0)
1221 lnum = wp->w_buffer->b_ml.ml_line_count;
1222 else
1223 lnum = wp->w_topline;
1224 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001225 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001226 int len;
1227 int w_width = wp->w_width;
1228
1229 // Count Tabs for what they are worth and compute the length based on
1230 // the maximum width (matters when 'showbreak' is set).
1231 if (wp->w_width < maxwidth)
1232 wp->w_width = maxwidth;
1233 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001234 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001235 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001236
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001237 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001238 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001239 while (len > maxwidth)
1240 {
1241 ++wrapped;
1242 len -= maxwidth;
1243 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001244 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001245 }
1246 }
1247 else if (len > maxwidth
1248 && allow_adjust_left
1249 && (wp->w_popup_pos == POPPOS_TOPLEFT
1250 || wp->w_popup_pos == POPPOS_BOTLEFT))
1251 {
1252 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001253 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001254
Bram Moolenaar51c31312019-06-15 22:27:23 +02001255 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001256 {
1257 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001258
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001259 len -= truncate_shift;
1260 shift_by -= truncate_shift;
1261 }
1262
1263 wp->w_wincol -= shift_by;
1264 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001265 wp->w_width = maxwidth;
1266 }
1267 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001268 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001269 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001270 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1271 wp->w_width = wp->w_maxwidth;
1272 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001273 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001274 if (wp->w_maxheight > 0
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001275 && (wp->w_firstline >= 0
1276 ? lnum - wp->w_topline
1277 : wp->w_buffer->b_ml.ml_line_count - lnum)
1278 + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001279 break;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001280
1281 if (wp->w_firstline < 0)
1282 --lnum;
1283 else
1284 ++lnum;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001285 }
1286
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001287 if (wp->w_firstline < 0)
1288 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1289
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001290 wp->w_has_scrollbar = wp->w_want_scrollbar
1291 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001292 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001293 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001294 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001295 ++extra_width;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001296 if (used_maxwidth)
1297 maxwidth -= 2; // try to show the scrollbar
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001298 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001299
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001300 minwidth = wp->w_minwidth;
1301 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1302 {
1303 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1304
1305 if (minwidth < title_len)
1306 minwidth = title_len;
1307 }
1308
1309 if (minwidth > 0 && wp->w_width < minwidth)
1310 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001311 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001312 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001313 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001314 // some columns cut off on the right
1315 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001316 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001317 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001318 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001319 {
1320 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1321 if (wp->w_wincol < 0)
1322 wp->w_wincol = 0;
1323 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001324 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1325 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1326 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001327 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001328
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001329 // Right aligned: move to the right if needed.
1330 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001331 if (leftoff >= 0)
1332 wp->w_wincol = leftoff;
1333 else if (wp->w_popup_fixed)
1334 {
1335 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001336 // columns when displaying the window, minus left border and
1337 // padding.
1338 if (-leftoff > left_extra)
1339 wp->w_leftcol = -leftoff - left_extra;
1340 wp->w_width -= wp->w_leftcol;
1341 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001342 if (wp->w_width < 0)
1343 wp->w_width = 0;
1344 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001345 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001346
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001347 if (wp->w_p_wrap || (!wp->w_popup_fixed
1348 && (wp->w_popup_pos == POPPOS_TOPLEFT
1349 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001350 {
1351 int want_col = 0;
1352
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001353 // try to show the right border and any scrollbar
1354 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001355 if (want_col > 0 && wp->w_wincol > 0
1356 && wp->w_wincol + want_col >= Columns)
1357 {
1358 wp->w_wincol = Columns - want_col;
1359 if (wp->w_wincol < 0)
1360 wp->w_wincol = 0;
1361 }
1362 }
1363
Bram Moolenaar8d241042019-06-12 23:40:01 +02001364 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1365 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001366 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1367 wp->w_height = wp->w_minheight;
1368 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1369 wp->w_height = wp->w_maxheight;
1370 if (wp->w_height > Rows - wp->w_winrow)
1371 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001372
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001373 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001374 {
1375 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1376 if (wp->w_winrow < 0)
1377 wp->w_winrow = 0;
1378 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001379 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1380 || wp->w_popup_pos == POPPOS_BOTLEFT)
1381 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001382 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001383 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001384 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001385 else
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001386 // Not enough space, make top aligned.
1387 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001388 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001389 if (wp->w_winrow >= Rows)
1390 wp->w_winrow = Rows - 1;
1391 else if (wp->w_winrow < 0)
1392 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001393
Bram Moolenaar17146962019-05-30 00:12:11 +02001394 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001395 if (win_valid(wp->w_popup_prop_win))
1396 {
1397 wp->w_popup_prop_changedtick =
1398 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1399 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1400 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001401
1402 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001403 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001404 if (org_winrow != wp->w_winrow
1405 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001406 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001407 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001408 || org_width != wp->w_width
1409 || org_height != wp->w_height)
1410 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001411 redraw_win_later(wp, NOT_VALID);
1412 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1413 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001414 popup_mask_refresh = TRUE;
1415 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001416}
1417
Bram Moolenaar17627312019-06-02 19:53:44 +02001418typedef enum
1419{
1420 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001421 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001422 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001423 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001424 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001425 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001426 TYPE_PREVIEW, // preview window
1427 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001428} create_type_T;
1429
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001430/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001431 * Make "buf" empty and set the contents to "text".
1432 * Used by popup_create() and popup_settext().
1433 */
1434 static void
1435popup_set_buffer_text(buf_T *buf, typval_T text)
1436{
1437 int lnum;
1438
1439 // Clear the buffer, then replace the lines.
1440 curbuf = buf;
1441 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1442 ml_delete(lnum, FALSE);
1443 curbuf = curwin->w_buffer;
1444
1445 // Add text to the buffer.
1446 if (text.v_type == VAR_STRING)
1447 {
1448 // just a string
1449 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1450 }
1451 else
1452 {
1453 list_T *l = text.vval.v_list;
1454
1455 if (l->lv_len > 0)
1456 {
1457 if (l->lv_first->li_tv.v_type == VAR_STRING)
1458 // list of strings
1459 add_popup_strings(buf, l);
1460 else
1461 // list of dictionaries
1462 add_popup_dicts(buf, l);
1463 }
1464 }
1465
1466 // delete the line that was in the empty buffer
1467 curbuf = buf;
1468 ml_delete(buf->b_ml.ml_line_count, FALSE);
1469 curbuf = curwin->w_buffer;
1470}
1471
1472/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001473 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1474 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001475 * Return FAIL if the parsing fails.
1476 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001477 static int
1478parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001479{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001480 char_u *p =
1481#ifdef FEAT_QUICKFIX
1482 !is_preview ? p_cpp :
1483#endif
1484 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001485
Bram Moolenaar258cef52019-08-21 17:29:29 +02001486 if (wp != NULL)
1487 wp->w_popup_flags &= ~POPF_INFO_MENU;
1488
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001489 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001490 {
1491 char_u *e, *dig;
1492 char_u *s = p;
1493 int x;
1494
1495 e = vim_strchr(p, ':');
1496 if (e == NULL || e[1] == NUL)
1497 return FAIL;
1498
1499 p = vim_strchr(e, ',');
1500 if (p == NULL)
1501 p = e + STRLEN(e);
1502 dig = e + 1;
1503 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001504
1505 if (STRNCMP(s, "height:", 7) == 0)
1506 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001507 if (dig != p)
1508 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001509 if (wp != NULL)
1510 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001511 if (is_preview)
1512 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001513 wp->w_maxheight = x;
1514 }
1515 }
1516 else if (STRNCMP(s, "width:", 6) == 0)
1517 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001518 if (dig != p)
1519 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001520 if (wp != NULL)
1521 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001522 if (is_preview)
1523 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001524 wp->w_maxwidth = x;
1525 }
1526 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001527 else if (STRNCMP(s, "highlight:", 10) == 0)
1528 {
1529 if (wp != NULL)
1530 {
1531 int c = *p;
1532
1533 *p = NUL;
1534 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1535 s + 10, OPT_FREE|OPT_LOCAL, 0);
1536 *p = c;
1537 }
1538 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001539 else if (STRNCMP(s, "border:", 7) == 0)
1540 {
1541 char_u *arg = s + 7;
1542 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1543 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1544 int i;
1545
1546 if (!on && !off)
1547 return FAIL;
1548 if (wp != NULL)
1549 {
1550 for (i = 0; i < 4; ++i)
1551 wp->w_popup_border[i] = on ? 1 : 0;
1552 if (off)
1553 // only show the X for close when there is a border
1554 wp->w_popup_close = POPCLOSE_NONE;
1555 }
1556 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001557 else if (STRNCMP(s, "align:", 6) == 0)
1558 {
1559 char_u *arg = s + 6;
1560 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1561 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1562
1563 if (!menu && !item)
1564 return FAIL;
1565 if (wp != NULL && menu)
1566 wp->w_popup_flags |= POPF_INFO_MENU;
1567 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001568 else
1569 return FAIL;
1570 }
1571 return OK;
1572}
1573
1574/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001575 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1576 * is not NULL.
1577 * Return FAIL if the parsing fails.
1578 */
1579 int
1580parse_previewpopup(win_T *wp)
1581{
1582 return parse_popup_option(wp, TRUE);
1583}
1584
1585/*
1586 * Parse the 'completepopup' option and apply the values to window "wp" if it
1587 * is not NULL.
1588 * Return FAIL if the parsing fails.
1589 */
1590 int
1591parse_completepopup(win_T *wp)
1592{
1593 return parse_popup_option(wp, FALSE);
1594}
1595
1596/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001597 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001598 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001599 */
1600 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001601popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001602{
1603 setcursor_mayforce(TRUE);
1604 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1605 if (wp->w_wantline == 0) // cursor in first line
1606 {
1607 wp->w_wantline = 2;
1608 wp->w_popup_pos = POPPOS_TOPLEFT;
1609 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001610
Bram Moolenaar79648732019-07-18 21:43:07 +02001611 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001612 if (wp->w_wantcol > Columns - width)
1613 {
1614 wp->w_wantcol = Columns - width;
1615 if (wp->w_wantcol < 1)
1616 wp->w_wantcol = 1;
1617 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001618 popup_adjust_position(wp);
1619}
1620
1621/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001622 * Set w_wantline and w_wantcol for the a given screen position.
1623 * Caller must take care of running into the window border.
1624 */
1625 void
1626popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1627{
1628 wp->w_wantline = row;
1629 wp->w_wantcol = col;
1630 popup_adjust_position(wp);
1631}
1632
1633/*
1634 * Add a border and lef&right padding.
1635 */
1636 static void
1637add_border_left_right_padding(win_T *wp)
1638{
1639 int i;
1640
1641 for (i = 0; i < 4; ++i)
1642 {
1643 wp->w_popup_border[i] = 1;
1644 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1645 }
1646}
1647
1648/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001649 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001650 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001651 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001652 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001653 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001654 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001655popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001656{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001657 win_T *wp;
1658 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001659 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001660 int new_buffer;
1661 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001662 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001663 int nr;
1664 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001665
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001667 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001668 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001669 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001670 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001671 buf = buflist_findnr( argvars[0].vval.v_number);
1672 if (buf == NULL)
1673 {
1674 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1675 return NULL;
1676 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001677#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001678 if (buf->b_term != NULL)
1679 {
1680 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1681 return NULL;
1682 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001683#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001684 }
1685 else if (!(argvars[0].v_type == VAR_STRING
1686 && argvars[0].vval.v_string != NULL)
1687 && !(argvars[0].v_type == VAR_LIST
1688 && argvars[0].vval.v_list != NULL))
1689 {
1690 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001691 return NULL;
1692 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001693 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001694 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001695 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001696 return NULL;
1697 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001698 d = argvars[1].vval.v_dict;
1699 }
1700
1701 if (d != NULL)
1702 {
1703 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1704 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1705 else if (type == TYPE_NOTIFICATION)
1706 tabnr = -1; // notifications are global by default
1707 else
1708 tabnr = 0;
1709 if (tabnr > 0)
1710 {
1711 tp = find_tabpage(tabnr);
1712 if (tp == NULL)
1713 {
1714 semsg(_("E997: Tabpage not found: %d"), tabnr);
1715 return NULL;
1716 }
1717 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001718 }
1719
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001720 // Create the window and buffer.
1721 wp = win_alloc_popup_win();
1722 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001723 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001724 if (rettv != NULL)
1725 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001726 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001727 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001728
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001729 if (buf != NULL)
1730 {
1731 // use existing buffer
1732 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001733 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001734 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001735 buffer_ensure_loaded(buf);
1736 }
1737 else
1738 {
1739 // create a new buffer associated with the popup
1740 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001741 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001742 if (buf == NULL)
1743 return NULL;
1744 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001745
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001746 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001747
Bram Moolenaar46451042019-08-24 15:50:46 +02001748 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001749 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001750 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001751 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001752 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001753 buf->b_p_ul = -1; // no undo
1754 buf->b_p_swf = FALSE; // no swap file
1755 buf->b_p_bl = FALSE; // unlisted buffer
1756 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001757
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001758 // Avoid that 'buftype' is reset when this buffer is entered.
1759 buf->b_p_initialized = TRUE;
1760 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001761 wp->w_p_wrap = TRUE; // 'wrap' is default on
1762 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001763
Bram Moolenaara3fce622019-06-20 02:31:49 +02001764 if (tp != NULL)
1765 {
1766 // popup on specified tab page
1767 wp->w_next = tp->tp_first_popupwin;
1768 tp->tp_first_popupwin = wp;
1769 }
1770 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001771 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001772 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001773 wp->w_next = curtab->tp_first_popupwin;
1774 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001775 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001776 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001777 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001778 win_T *prev = first_popupwin;
1779
1780 // Global popup: add at the end, so that it gets displayed on top of
1781 // older ones with the same zindex. Matters for notifications.
1782 if (first_popupwin == NULL)
1783 first_popupwin = wp;
1784 else
1785 {
1786 while (prev->w_next != NULL)
1787 prev = prev->w_next;
1788 prev->w_next = wp;
1789 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001790 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001791
Bram Moolenaar79648732019-07-18 21:43:07 +02001792 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001793 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001794
Bram Moolenaar79648732019-07-18 21:43:07 +02001795 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001796 {
1797 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001798 }
1799 if (type == TYPE_ATCURSOR)
1800 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001801 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001802 set_moved_values(wp);
1803 set_moved_columns(wp, FIND_STRING);
1804 }
1805
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001806 if (type == TYPE_BEVAL)
1807 {
1808 wp->w_popup_pos = POPPOS_BOTLEFT;
1809
1810 // by default use the mouse position
1811 wp->w_wantline = mouse_row;
1812 if (wp->w_wantline <= 0) // mouse on first line
1813 {
1814 wp->w_wantline = 2;
1815 wp->w_popup_pos = POPPOS_TOPLEFT;
1816 }
1817 wp->w_wantcol = mouse_col + 1;
1818 set_mousemoved_values(wp);
1819 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1820 }
1821
Bram Moolenaar33796b32019-06-08 16:01:13 +02001822 // set default values
1823 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001824 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001825
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001826 if (type == TYPE_NOTIFICATION)
1827 {
1828 win_T *twp, *nextwin;
1829 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001830
1831 // Try to not overlap with another global popup. Guess we need 3
1832 // more screen lines than buffer lines.
1833 wp->w_wantline = 1;
1834 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1835 {
1836 nextwin = twp->w_next;
1837 if (twp != wp
1838 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1839 && twp->w_winrow <= wp->w_wantline - 1 + height
1840 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1841 {
1842 // move to below this popup and restart the loop to check for
1843 // overlap with other popups
1844 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1845 nextwin = first_popupwin;
1846 }
1847 }
1848 if (wp->w_wantline + height > Rows)
1849 {
1850 // can't avoid overlap, put on top in the hope that message goes
1851 // away soon.
1852 wp->w_wantline = 1;
1853 }
1854
1855 wp->w_wantcol = 10;
1856 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001857 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001858 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001859 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001860 for (i = 0; i < 4; ++i)
1861 wp->w_popup_border[i] = 1;
1862 wp->w_popup_padding[1] = 1;
1863 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001864
1865 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001866 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001867 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1868 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001869 }
1870
Bram Moolenaara730e552019-06-16 19:05:31 +02001871 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001872 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001873 wp->w_popup_pos = POPPOS_CENTER;
1874 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001875 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001876 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001877 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001878 }
1879
Bram Moolenaara730e552019-06-16 19:05:31 +02001880 if (type == TYPE_MENU)
1881 {
1882 typval_T tv;
1883 callback_T callback;
1884
1885 tv.v_type = VAR_STRING;
1886 tv.vval.v_string = (char_u *)"popup_filter_menu";
1887 callback = get_callback(&tv);
1888 if (callback.cb_name != NULL)
1889 set_callback(&wp->w_filter_cb, &callback);
1890
1891 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001892 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001893 }
1894
Bram Moolenaar79648732019-07-18 21:43:07 +02001895 if (type == TYPE_PREVIEW)
1896 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001897 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001898 wp->w_popup_close = POPCLOSE_BUTTON;
1899 for (i = 0; i < 4; ++i)
1900 wp->w_popup_border[i] = 1;
1901 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001902 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1903 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001904# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001905 if (type == TYPE_INFO)
1906 {
1907 wp->w_popup_pos = POPPOS_TOPLEFT;
1908 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1909 wp->w_popup_close = POPCLOSE_BUTTON;
1910 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001911 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001912 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001913# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001914
Bram Moolenaarae943152019-06-16 22:54:14 +02001915 for (i = 0; i < 4; ++i)
1916 VIM_CLEAR(wp->w_border_highlight[i]);
1917 for (i = 0; i < 8; ++i)
1918 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001919 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001920 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001921 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001922
Bram Moolenaar79648732019-07-18 21:43:07 +02001923 if (d != NULL)
1924 // Deal with options.
1925 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001926
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001927#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001928 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1929 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001930#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001931
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001932 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001933
1934 wp->w_vsep_width = 0;
1935
1936 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001937 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001938
1939 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001940}
1941
1942/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001943 * popup_clear()
1944 */
1945 void
1946f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1947{
1948 close_all_popups();
1949}
1950
1951/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001952 * popup_create({text}, {options})
1953 */
1954 void
1955f_popup_create(typval_T *argvars, typval_T *rettv)
1956{
Bram Moolenaar17627312019-06-02 19:53:44 +02001957 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001958}
1959
1960/*
1961 * popup_atcursor({text}, {options})
1962 */
1963 void
1964f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1965{
Bram Moolenaar17627312019-06-02 19:53:44 +02001966 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001967}
1968
1969/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001970 * popup_beval({text}, {options})
1971 */
1972 void
1973f_popup_beval(typval_T *argvars, typval_T *rettv)
1974{
1975 popup_create(argvars, rettv, TYPE_BEVAL);
1976}
1977
1978/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001979 * Invoke the close callback for window "wp" with value "result".
1980 * Careful: The callback may make "wp" invalid!
1981 */
1982 static void
1983invoke_popup_callback(win_T *wp, typval_T *result)
1984{
1985 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001986 typval_T argv[3];
1987
1988 argv[0].v_type = VAR_NUMBER;
1989 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1990
1991 if (result != NULL && result->v_type != VAR_UNKNOWN)
1992 copy_tv(result, &argv[1]);
1993 else
1994 {
1995 argv[1].v_type = VAR_NUMBER;
1996 argv[1].vval.v_number = 0;
1997 }
1998
1999 argv[2].v_type = VAR_UNKNOWN;
2000
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002001 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002002 if (result != NULL)
2003 clear_tv(&argv[1]);
2004 clear_tv(&rettv);
2005}
2006
2007/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002008 * Close popup "wp" and invoke any close callback for it.
2009 */
2010 static void
2011popup_close_and_callback(win_T *wp, typval_T *arg)
2012{
2013 int id = wp->w_id;
2014
2015 if (wp->w_close_cb.cb_name != NULL)
2016 // Careful: This may make "wp" invalid.
2017 invoke_popup_callback(wp, arg);
2018
2019 popup_close(id);
2020}
2021
Bram Moolenaar12034e22019-08-25 22:25:02 +02002022 static void
2023popup_close_with_retval(win_T *wp, int retval)
2024{
2025 typval_T res;
2026
2027 res.v_type = VAR_NUMBER;
2028 res.vval.v_number = retval;
2029 popup_close_and_callback(wp, &res);
2030}
2031
Bram Moolenaar3397f742019-06-02 18:40:06 +02002032/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002033 * Close popup "wp" because of a mouse click.
2034 */
2035 void
2036popup_close_for_mouse_click(win_T *wp)
2037{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002038 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002039}
2040
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002041 static void
2042check_mouse_moved(win_T *wp, win_T *mouse_wp)
2043{
2044 // Close the popup when all if these are true:
2045 // - the mouse is not on this popup
2046 // - "mousemoved" was used
2047 // - the mouse is no longer on the same screen row or the mouse column is
2048 // outside of the relevant text
2049 if (wp != mouse_wp
2050 && wp->w_popup_mouse_row != 0
2051 && (wp->w_popup_mouse_row != mouse_row
2052 || mouse_col < wp->w_popup_mouse_mincol
2053 || mouse_col > wp->w_popup_mouse_maxcol))
2054 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002055 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002056 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002057 }
2058}
2059
2060/*
2061 * Called when the mouse moved: may close a popup with "mousemoved".
2062 */
2063 void
2064popup_handle_mouse_moved(void)
2065{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002066 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002067 win_T *mouse_wp;
2068 int row = mouse_row;
2069 int col = mouse_col;
2070
2071 // find the window where the mouse is in
2072 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2073
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002074 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2075 {
2076 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002077 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002078 }
2079 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2080 {
2081 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002082 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002083 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002084}
2085
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002086/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002087 * In a filter: check if the typed key is a mouse event that is used for
2088 * dragging the popup.
2089 */
2090 static void
2091filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2092{
2093 int row = mouse_row;
2094 int col = mouse_col;
2095
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002096 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002097 && is_mouse_key(c)
2098 && (wp == popup_dragwin
2099 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2100 // do not consume the key, allow for dragging the popup
2101 rettv->vval.v_number = 0;
2102}
2103
Bram Moolenaara730e552019-06-16 19:05:31 +02002104/*
2105 * popup_filter_menu({text}, {options})
2106 */
2107 void
2108f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2109{
2110 int id = tv_get_number(&argvars[0]);
2111 win_T *wp = win_id2wp(id);
2112 char_u *key = tv_get_string(&argvars[1]);
2113 typval_T res;
2114 int c;
2115 linenr_T old_lnum;
2116
2117 // If the popup has been closed do not consume the key.
2118 if (wp == NULL)
2119 return;
2120
2121 c = *key;
2122 if (c == K_SPECIAL && key[1] != NUL)
2123 c = TO_SPECIAL(key[1], key[2]);
2124
2125 // consume all keys until done
2126 rettv->vval.v_number = 1;
2127 res.v_type = VAR_NUMBER;
2128
2129 old_lnum = wp->w_cursor.lnum;
2130 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2131 --wp->w_cursor.lnum;
2132 if ((c == 'j' || c == 'J' || c == K_DOWN)
2133 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2134 ++wp->w_cursor.lnum;
2135 if (old_lnum != wp->w_cursor.lnum)
2136 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002137 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002138 return;
2139 }
2140
2141 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2142 {
2143 // Cancelled, invoke callback with -1
2144 res.vval.v_number = -1;
2145 popup_close_and_callback(wp, &res);
2146 return;
2147 }
2148 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2149 {
2150 // Invoke callback with current index.
2151 res.vval.v_number = wp->w_cursor.lnum;
2152 popup_close_and_callback(wp, &res);
2153 return;
2154 }
2155
2156 filter_handle_drag(wp, c, rettv);
2157}
2158
2159/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002160 * popup_filter_yesno({text}, {options})
2161 */
2162 void
2163f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2164{
2165 int id = tv_get_number(&argvars[0]);
2166 win_T *wp = win_id2wp(id);
2167 char_u *key = tv_get_string(&argvars[1]);
2168 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002169 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002170
2171 // If the popup has been closed don't consume the key.
2172 if (wp == NULL)
2173 return;
2174
Bram Moolenaara730e552019-06-16 19:05:31 +02002175 c = *key;
2176 if (c == K_SPECIAL && key[1] != NUL)
2177 c = TO_SPECIAL(key[1], key[2]);
2178
Bram Moolenaara42d9452019-06-15 21:46:30 +02002179 // consume all keys until done
2180 rettv->vval.v_number = 1;
2181
Bram Moolenaara730e552019-06-16 19:05:31 +02002182 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002183 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002184 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002185 res.vval.v_number = 0;
2186 else
2187 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002188 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002189 return;
2190 }
2191
2192 // Invoke callback
2193 res.v_type = VAR_NUMBER;
2194 popup_close_and_callback(wp, &res);
2195}
2196
2197/*
2198 * popup_dialog({text}, {options})
2199 */
2200 void
2201f_popup_dialog(typval_T *argvars, typval_T *rettv)
2202{
2203 popup_create(argvars, rettv, TYPE_DIALOG);
2204}
2205
2206/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002207 * popup_menu({text}, {options})
2208 */
2209 void
2210f_popup_menu(typval_T *argvars, typval_T *rettv)
2211{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002212 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002213}
2214
2215/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002216 * popup_notification({text}, {options})
2217 */
2218 void
2219f_popup_notification(typval_T *argvars, typval_T *rettv)
2220{
2221 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2222}
2223
2224/*
2225 * Find the popup window with window-ID "id".
2226 * If the popup window does not exist NULL is returned.
2227 * If the window is not a popup window, and error message is given.
2228 */
2229 static win_T *
2230find_popup_win(int id)
2231{
2232 win_T *wp = win_id2wp(id);
2233
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002234 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002235 {
2236 semsg(_("E993: window %d is not a popup window"), id);
2237 return NULL;
2238 }
2239 return wp;
2240}
2241
2242/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002243 * popup_close({id})
2244 */
2245 void
2246f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2247{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002248 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002249 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002250
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002251 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002252 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002253}
2254
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002255 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002256popup_hide(win_T *wp)
2257{
2258 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2259 {
2260 wp->w_popup_flags |= POPF_HIDDEN;
2261 --wp->w_buffer->b_nwindows;
2262 redraw_all_later(NOT_VALID);
2263 popup_mask_refresh = TRUE;
2264 }
2265}
2266
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002267/*
2268 * popup_hide({id})
2269 */
2270 void
2271f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2272{
2273 int id = (int)tv_get_number(argvars);
2274 win_T *wp = find_popup_win(id);
2275
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002276 if (wp != NULL)
2277 popup_hide(wp);
2278}
2279
2280 void
2281popup_show(win_T *wp)
2282{
2283 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002284 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002285 wp->w_popup_flags &= ~POPF_HIDDEN;
2286 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002287 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002288 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002289 }
2290}
2291
2292/*
2293 * popup_show({id})
2294 */
2295 void
2296f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2297{
2298 int id = (int)tv_get_number(argvars);
2299 win_T *wp = find_popup_win(id);
2300
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002301 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002302 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002303 popup_show(wp);
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002304 if (wp->w_popup_flags & POPF_INFO)
2305 pum_position_info_popup(wp);
2306 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002307}
2308
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002309/*
2310 * popup_settext({id}, {text})
2311 */
2312 void
2313f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2314{
2315 int id = (int)tv_get_number(&argvars[0]);
2316 win_T *wp = find_popup_win(id);
2317
2318 if (wp != NULL)
2319 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002320 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2321 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2322 else
2323 {
2324 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002325 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002326 popup_adjust_position(wp);
2327 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002328 }
2329}
2330
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002331 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002332popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002333{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002334 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002335 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002336 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002337 clear_cmdline = TRUE;
2338 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002339
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002340 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002341 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002342}
2343
Bram Moolenaarec583842019-05-26 14:11:23 +02002344/*
2345 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002346 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002347 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002348 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002349popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002350{
2351 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002352 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002353 win_T *prev = NULL;
2354
Bram Moolenaarec583842019-05-26 14:11:23 +02002355 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002356 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002357 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002358 {
2359 if (prev == NULL)
2360 first_popupwin = wp->w_next;
2361 else
2362 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002363 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002364 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002365 }
2366
Bram Moolenaarec583842019-05-26 14:11:23 +02002367 // go through tab-local popups
2368 FOR_ALL_TABPAGES(tp)
2369 popup_close_tabpage(tp, id);
2370}
2371
2372/*
2373 * Close a popup window with Window-id "id" in tabpage "tp".
2374 */
2375 void
2376popup_close_tabpage(tabpage_T *tp, int id)
2377{
2378 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002379 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002380 win_T *prev = NULL;
2381
Bram Moolenaarec583842019-05-26 14:11:23 +02002382 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2383 if (wp->w_id == id)
2384 {
2385 if (prev == NULL)
2386 *root = wp->w_next;
2387 else
2388 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002389 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002390 return;
2391 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002392}
2393
2394 void
2395close_all_popups(void)
2396{
2397 while (first_popupwin != NULL)
2398 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002399 while (curtab->tp_first_popupwin != NULL)
2400 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002401}
2402
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002403/*
2404 * popup_move({id}, {options})
2405 */
2406 void
2407f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2408{
Bram Moolenaarae943152019-06-16 22:54:14 +02002409 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002410 int id = (int)tv_get_number(argvars);
2411 win_T *wp = find_popup_win(id);
2412
2413 if (wp == NULL)
2414 return; // invalid {id}
2415
2416 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2417 {
2418 emsg(_(e_dictreq));
2419 return;
2420 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002421 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002422
Bram Moolenaarae943152019-06-16 22:54:14 +02002423 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002424
2425 if (wp->w_winrow + wp->w_height >= cmdline_row)
2426 clear_cmdline = TRUE;
2427 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002428}
2429
Bram Moolenaarbc133542019-05-29 20:26:46 +02002430/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002431 * popup_setoptions({id}, {options})
2432 */
2433 void
2434f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2435{
2436 dict_T *dict;
2437 int id = (int)tv_get_number(argvars);
2438 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002439 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002440
2441 if (wp == NULL)
2442 return; // invalid {id}
2443
2444 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2445 {
2446 emsg(_(e_dictreq));
2447 return;
2448 }
2449 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002450 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002451
2452 apply_move_options(wp, dict);
2453 apply_general_options(wp, dict);
2454
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002455 if (old_firstline != wp->w_firstline)
2456 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002457 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002458 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002459 popup_adjust_position(wp);
2460}
2461
2462/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002463 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002464 */
2465 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002466f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002467{
2468 dict_T *dict;
2469 int id = (int)tv_get_number(argvars);
2470 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002471 int top_extra;
2472 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002473
2474 if (rettv_dict_alloc(rettv) == OK)
2475 {
2476 if (wp == NULL)
2477 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002478 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002479 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2480
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002481 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002482 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002483 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002484
Bram Moolenaarbc133542019-05-29 20:26:46 +02002485 dict_add_number(dict, "line", wp->w_winrow + 1);
2486 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002487 dict_add_number(dict, "width", wp->w_width + left_extra
2488 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2489 dict_add_number(dict, "height", wp->w_height + top_extra
2490 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002491
2492 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2493 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2494 dict_add_number(dict, "core_width", wp->w_width);
2495 dict_add_number(dict, "core_height", wp->w_height);
2496
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002497 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002498 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002499 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002500 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002501
2502 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002503 }
2504}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002505/*
2506 * popup_locate({row}, {col})
2507 */
2508 void
2509f_popup_locate(typval_T *argvars, typval_T *rettv)
2510{
2511 int row = tv_get_number(&argvars[0]) - 1;
2512 int col = tv_get_number(&argvars[1]) - 1;
2513 win_T *wp;
2514
2515 wp = mouse_find_win(&row, &col, FIND_POPUP);
2516 if (WIN_IS_POPUP(wp))
2517 rettv->vval.v_number = wp->w_id;
2518}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002519
2520/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002521 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2522 */
2523 static void
2524get_padding_border(dict_T *dict, int *array, char *name)
2525{
2526 list_T *list;
2527 int i;
2528
2529 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2530 return;
2531
2532 list = list_alloc();
2533 if (list != NULL)
2534 {
2535 dict_add_list(dict, name, list);
2536 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2537 for (i = 0; i < 4; ++i)
2538 list_append_number(list, array[i]);
2539 }
2540}
2541
2542/*
2543 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2544 */
2545 static void
2546get_borderhighlight(dict_T *dict, win_T *wp)
2547{
2548 list_T *list;
2549 int i;
2550
2551 for (i = 0; i < 4; ++i)
2552 if (wp->w_border_highlight[i] != NULL)
2553 break;
2554 if (i == 4)
2555 return;
2556
2557 list = list_alloc();
2558 if (list != NULL)
2559 {
2560 dict_add_list(dict, "borderhighlight", list);
2561 for (i = 0; i < 4; ++i)
2562 list_append_string(list, wp->w_border_highlight[i], -1);
2563 }
2564}
2565
2566/*
2567 * For popup_getoptions(): add a "borderchars" entry to "dict".
2568 */
2569 static void
2570get_borderchars(dict_T *dict, win_T *wp)
2571{
2572 list_T *list;
2573 int i;
2574 char_u buf[NUMBUFLEN];
2575 int len;
2576
2577 for (i = 0; i < 8; ++i)
2578 if (wp->w_border_char[i] != 0)
2579 break;
2580 if (i == 8)
2581 return;
2582
2583 list = list_alloc();
2584 if (list != NULL)
2585 {
2586 dict_add_list(dict, "borderchars", list);
2587 for (i = 0; i < 8; ++i)
2588 {
2589 len = mb_char2bytes(wp->w_border_char[i], buf);
2590 list_append_string(list, buf, len);
2591 }
2592 }
2593}
2594
2595/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002596 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002597 */
2598 static void
2599get_moved_list(dict_T *dict, win_T *wp)
2600{
2601 list_T *list;
2602
2603 list = list_alloc();
2604 if (list != NULL)
2605 {
2606 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002607 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002608 list_append_number(list, wp->w_popup_mincol);
2609 list_append_number(list, wp->w_popup_maxcol);
2610 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002611 list = list_alloc();
2612 if (list != NULL)
2613 {
2614 dict_add_list(dict, "mousemoved", list);
2615 list_append_number(list, wp->w_popup_mouse_row);
2616 list_append_number(list, wp->w_popup_mouse_mincol);
2617 list_append_number(list, wp->w_popup_mouse_maxcol);
2618 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002619}
2620
2621/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002622 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002623 */
2624 void
2625f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2626{
2627 dict_T *dict;
2628 int id = (int)tv_get_number(argvars);
2629 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002630 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002631 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002632
2633 if (rettv_dict_alloc(rettv) == OK)
2634 {
2635 if (wp == NULL)
2636 return;
2637
2638 dict = rettv->vval.v_dict;
2639 dict_add_number(dict, "line", wp->w_wantline);
2640 dict_add_number(dict, "col", wp->w_wantcol);
2641 dict_add_number(dict, "minwidth", wp->w_minwidth);
2642 dict_add_number(dict, "minheight", wp->w_minheight);
2643 dict_add_number(dict, "maxheight", wp->w_maxheight);
2644 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002645 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002646 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002647 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002648 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002649 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2650 {
2651 proptype_T *pt = text_prop_type_by_id(
2652 wp->w_popup_prop_win->w_buffer,
2653 wp->w_popup_prop_type);
2654
2655 if (pt != NULL)
2656 dict_add_string(dict, "textprop", pt->pt_name);
2657 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2658 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2659 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002660 dict_add_string(dict, "title", wp->w_popup_title);
2661 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002662 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002663 dict_add_number(dict, "mapping",
2664 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002665 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002666 dict_add_number(dict, "cursorline",
2667 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002668 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002669 if (wp->w_scrollbar_highlight != NULL)
2670 dict_add_string(dict, "scrollbarhighlight",
2671 wp->w_scrollbar_highlight);
2672 if (wp->w_thumb_highlight != NULL)
2673 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002674
Bram Moolenaara3fce622019-06-20 02:31:49 +02002675 // find the tabpage that holds this popup
2676 i = 1;
2677 FOR_ALL_TABPAGES(tp)
2678 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002679 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002680
Bram Moolenaar1824f452019-10-02 23:06:46 +02002681 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2682 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002683 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002684 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002685 break;
2686 ++i;
2687 }
2688 if (tp == NULL)
2689 i = -1; // must be global
2690 else if (tp == curtab)
2691 i = 0;
2692 dict_add_number(dict, "tabpage", i);
2693
Bram Moolenaarae943152019-06-16 22:54:14 +02002694 get_padding_border(dict, wp->w_popup_padding, "padding");
2695 get_padding_border(dict, wp->w_popup_border, "border");
2696 get_borderhighlight(dict, wp);
2697 get_borderchars(dict, wp);
2698 get_moved_list(dict, wp);
2699
2700 if (wp->w_filter_cb.cb_name != NULL)
2701 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2702 if (wp->w_close_cb.cb_name != NULL)
2703 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002704
2705 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2706 ++i)
2707 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2708 {
2709 dict_add_string(dict, "pos",
2710 (char_u *)poppos_entries[i].pp_name);
2711 break;
2712 }
2713
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002714 dict_add_string(dict, "close", (char_u *)(
2715 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2716 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2717
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002718# if defined(FEAT_TIMERS)
2719 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2720 ? (long)wp->w_popup_timer->tr_interval : 0L);
2721# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002722 }
2723}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002724
2725 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002726error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002727{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002728 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002729 {
2730 emsg(_("E994: Not allowed in a popup window"));
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002736/*
2737 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002738 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002739 */
2740 void
2741popup_reset_handled()
2742{
2743 win_T *wp;
2744
2745 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2746 wp->w_popup_flags &= ~POPF_HANDLED;
2747 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2748 wp->w_popup_flags &= ~POPF_HANDLED;
2749}
2750
2751/*
2752 * Find the next visible popup where POPF_HANDLED is not set.
2753 * Must have called popup_reset_handled() first.
2754 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2755 * popup with the highest zindex.
2756 */
2757 win_T *
2758find_next_popup(int lowest)
2759{
2760 win_T *wp;
2761 win_T *found_wp;
2762 int found_zindex;
2763
2764 found_zindex = lowest ? INT_MAX : 0;
2765 found_wp = NULL;
2766 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2767 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2768 && (lowest ? wp->w_zindex < found_zindex
2769 : wp->w_zindex > found_zindex))
2770 {
2771 found_zindex = wp->w_zindex;
2772 found_wp = wp;
2773 }
2774 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2775 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2776 && (lowest ? wp->w_zindex < found_zindex
2777 : wp->w_zindex > found_zindex))
2778 {
2779 found_zindex = wp->w_zindex;
2780 found_wp = wp;
2781 }
2782
2783 if (found_wp != NULL)
2784 found_wp->w_popup_flags |= POPF_HANDLED;
2785 return found_wp;
2786}
2787
2788/*
2789 * Invoke the filter callback for window "wp" with typed character "c".
2790 * Uses the global "mod_mask" for modifiers.
2791 * Returns the return value of the filter.
2792 * Careful: The filter may make "wp" invalid!
2793 */
2794 static int
2795invoke_popup_filter(win_T *wp, int c)
2796{
2797 int res;
2798 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002799 typval_T argv[3];
2800 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002801 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002802
Bram Moolenaara42d9452019-06-15 21:46:30 +02002803 // Emergency exit: CTRL-C closes the popup.
2804 if (c == Ctrl_C)
2805 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002806 int save_got_int = got_int;
2807
2808 // Reset got_int to avoid the callback isn't called.
2809 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002810 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002811 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002812 return 1;
2813 }
2814
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002815 argv[0].v_type = VAR_NUMBER;
2816 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2817
2818 // Convert the number to a string, so that the function can use:
2819 // if a:c == "\<F2>"
2820 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2821 argv[1].v_type = VAR_STRING;
2822 argv[1].vval.v_string = vim_strsave(buf);
2823
2824 argv[2].v_type = VAR_UNKNOWN;
2825
Bram Moolenaara42d9452019-06-15 21:46:30 +02002826 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002827 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002828 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002829 popup_highlight_curline(wp);
2830
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002831 res = tv_get_number(&rettv);
2832 vim_free(argv[1].vval.v_string);
2833 clear_tv(&rettv);
2834 return res;
2835}
2836
2837/*
2838 * Called when "c" was typed: invoke popup filter callbacks.
2839 * Returns TRUE when the character was consumed,
2840 */
2841 int
2842popup_do_filter(int c)
2843{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002844 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002845 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002846 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002847 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002848 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002849 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002850
2851 if (recursive)
2852 return FALSE;
2853 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002854
2855 popup_reset_handled();
2856
Bram Moolenaarf6396232019-08-24 19:36:00 +02002857 if (c == K_LEFTMOUSE)
2858 {
2859 int row = mouse_row;
2860 int col = mouse_col;
2861
2862 wp = mouse_find_win(&row, &col, FIND_POPUP);
2863 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002864 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002865 }
2866
Bram Moolenaar581ba392019-09-03 22:08:33 +02002867 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002868 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002869 if (wp->w_filter_cb.cb_name != NULL
2870 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002871 res = invoke_popup_filter(wp, c);
2872
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002873 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002874 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002875 recursive = FALSE;
2876 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002877 return res;
2878}
2879
Bram Moolenaar3397f742019-06-02 18:40:06 +02002880/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002881 * Return TRUE if there is a popup visible with a filter callback and the
2882 * "mapping" property off.
2883 */
2884 int
2885popup_no_mapping(void)
2886{
2887 int round;
2888 win_T *wp;
2889
2890 for (round = 1; round <= 2; ++round)
2891 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2892 wp != NULL; wp = wp->w_next)
2893 if (wp->w_filter_cb.cb_name != NULL
2894 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2895 return TRUE;
2896 return FALSE;
2897}
2898
2899/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002900 * Called when the cursor moved: check if any popup needs to be closed if the
2901 * cursor moved far enough.
2902 */
2903 void
2904popup_check_cursor_pos()
2905{
2906 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002907
2908 popup_reset_handled();
2909 while ((wp = find_next_popup(TRUE)) != NULL)
2910 if (wp->w_popup_curwin != NULL
2911 && (curwin != wp->w_popup_curwin
2912 || curwin->w_cursor.lnum != wp->w_popup_lnum
2913 || curwin->w_cursor.col < wp->w_popup_mincol
2914 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002915 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002916}
2917
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002918/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002919 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002920 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002921 static void
2922popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002923{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002924 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002925 char_u *cells;
2926 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002927
2928 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002929 return;
2930 if (wp->w_popup_mask_cells != NULL
2931 && wp->w_popup_mask_height == height
2932 && wp->w_popup_mask_width == width)
2933 return; // cache is still valid
2934
2935 vim_free(wp->w_popup_mask_cells);
2936 wp->w_popup_mask_cells = alloc_clear(width * height);
2937 if (wp->w_popup_mask_cells == NULL)
2938 return;
2939 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002940
2941 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2942 {
2943 int cols, cole;
2944 int lines, linee;
2945
2946 li = lio->li_tv.vval.v_list->lv_first;
2947 cols = tv_get_number(&li->li_tv);
2948 if (cols < 0)
2949 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002950 li = li->li_next;
2951 cole = tv_get_number(&li->li_tv);
2952 if (cole < 0)
2953 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002954 li = li->li_next;
2955 lines = tv_get_number(&li->li_tv);
2956 if (lines < 0)
2957 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002958 li = li->li_next;
2959 linee = tv_get_number(&li->li_tv);
2960 if (linee < 0)
2961 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002962
2963 for (row = lines - 1; row < linee && row < height; ++row)
2964 for (col = cols - 1; col < cole && col < width; ++col)
2965 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002966 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002967}
2968
2969/*
2970 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2971 * "col" and "line" are screen coordinates.
2972 */
2973 static int
2974popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2975{
2976 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2977 int line = screenline - wp->w_winrow;
2978
2979 return col >= 0 && col < width
2980 && line >= 0 && line < height
2981 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002982}
2983
2984/*
2985 * Set flags in popup_transparent[] for window "wp" to "val".
2986 */
2987 static void
2988update_popup_transparent(win_T *wp, int val)
2989{
2990 if (wp->w_popup_mask != NULL)
2991 {
2992 int width = popup_width(wp);
2993 int height = popup_height(wp);
2994 listitem_T *lio, *li;
2995 int cols, cole;
2996 int lines, linee;
2997 int col, line;
2998
2999 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3000 {
3001 li = lio->li_tv.vval.v_list->lv_first;
3002 cols = tv_get_number(&li->li_tv);
3003 if (cols < 0)
3004 cols = width + cols + 1;
3005 li = li->li_next;
3006 cole = tv_get_number(&li->li_tv);
3007 if (cole < 0)
3008 cole = width + cole + 1;
3009 li = li->li_next;
3010 lines = tv_get_number(&li->li_tv);
3011 if (lines < 0)
3012 lines = height + lines + 1;
3013 li = li->li_next;
3014 linee = tv_get_number(&li->li_tv);
3015 if (linee < 0)
3016 linee = height + linee + 1;
3017
3018 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003019 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003020 if (cols < 0)
3021 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003022 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003023 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003024 if (lines < 0)
3025 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003026 for (line = lines; line < linee
3027 && line + wp->w_winrow < screen_Rows; ++line)
3028 for (col = cols; col < cole
3029 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003030 popup_transparent[(line + wp->w_winrow) * screen_Columns
3031 + col + wp->w_wincol] = val;
3032 }
3033 }
3034}
3035
3036/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003037 * Only called when popup window "wp" is hidden: If the window is positioned
3038 * next to a text property, and it is now visible, then unhide the popup.
3039 * We don't check if visible popups become hidden, that is done in
3040 * popup_adjust_position().
3041 * Return TRUE if the popup became unhidden.
3042 */
3043 static int
3044check_popup_unhidden(win_T *wp)
3045{
3046 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3047 {
3048 textprop_T prop;
3049 linenr_T lnum;
3050
3051 if (find_visible_prop(wp->w_popup_prop_win,
3052 wp->w_popup_prop_type, wp->w_popup_prop_id,
3053 &prop, &lnum) == OK)
3054 {
3055 wp->w_popup_flags &= ~POPF_HIDDEN;
3056 ++wp->w_buffer->b_nwindows;
3057 wp->w_popup_prop_topline = 0; // force repositioning
3058 return TRUE;
3059 }
3060 }
3061 return FALSE;
3062}
3063
3064/*
3065 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3066 * That is when the buffer in the popup was changed, or the popup is following
3067 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003068 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003069 */
3070 static int
3071popup_need_position_adjust(win_T *wp)
3072{
3073 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3074 return TRUE;
3075 if (win_valid(wp->w_popup_prop_win))
3076 return wp->w_popup_prop_changedtick
3077 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003078 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3079 || ((wp->w_popup_flags & POPF_CURSORLINE)
3080 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003081 return FALSE;
3082}
3083
3084/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003085 * Update "popup_mask" if needed.
3086 * Also recomputes the popup size and positions.
3087 * Also updates "popup_visible".
3088 * Also marks window lines for redrawing.
3089 */
3090 void
3091may_update_popup_mask(int type)
3092{
3093 win_T *wp;
3094 short *mask;
3095 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003096 int redraw_all_popups = FALSE;
3097 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003098
3099 // Need to recompute when switching tabs.
3100 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3101 // (such as the screen size) must have changed.
3102 if (popup_mask_tab != curtab || type >= NOT_VALID)
3103 {
3104 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003105 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003106 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003107
3108 // Check if any popup window buffer has changed and if any popup connected
3109 // to a text property has become visible.
3110 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3111 if (wp->w_popup_flags & POPF_HIDDEN)
3112 popup_mask_refresh |= check_popup_unhidden(wp);
3113 else if (popup_need_position_adjust(wp))
3114 popup_mask_refresh = TRUE;
3115 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3116 if (wp->w_popup_flags & POPF_HIDDEN)
3117 popup_mask_refresh |= check_popup_unhidden(wp);
3118 else if (popup_need_position_adjust(wp))
3119 popup_mask_refresh = TRUE;
3120
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003121 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003122 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003123
3124 // Need to update the mask, something has changed.
3125 popup_mask_refresh = FALSE;
3126 popup_mask_tab = curtab;
3127 popup_visible = FALSE;
3128
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003129 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003130 // If redrawing only what is needed, update "popup_mask_next" and then
3131 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003132 redrawing_all_win = TRUE;
3133 FOR_ALL_WINDOWS(wp)
3134 if (wp->w_redr_type < SOME_VALID)
3135 redrawing_all_win = FALSE;
3136 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003137 mask = popup_mask;
3138 else
3139 mask = popup_mask_next;
3140 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3141
3142 // Find the window with the lowest zindex that hasn't been handled yet,
3143 // so that the window with a higher zindex overwrites the value in
3144 // popup_mask.
3145 popup_reset_handled();
3146 while ((wp = find_next_popup(TRUE)) != NULL)
3147 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003148 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003149 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003150
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003151 popup_visible = TRUE;
3152
Bram Moolenaar12034e22019-08-25 22:25:02 +02003153 // Recompute the position if the text changed. It may make the popup
3154 // hidden if it's attach to a text property that is no longer visible.
3155 if (redraw_all_popups || popup_need_position_adjust(wp))
3156 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003157 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003158 if (wp->w_popup_flags & POPF_HIDDEN)
3159 continue;
3160 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003161
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003162 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003163 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003164 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003165 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003166 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003167 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003168 col < wp->w_wincol + width - wp->w_popup_leftoff
3169 && col < screen_Columns; ++col)
3170 if (wp->w_popup_mask_cells == NULL
3171 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003172 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003173 }
3174
3175 // Only check which lines are to be updated if not already
3176 // updating all lines.
3177 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003178 {
3179 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3180 win_T *prev_wp = NULL;
3181
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003182 for (line = 0; line < screen_Rows; ++line)
3183 {
3184 int col_done = 0;
3185
3186 for (col = 0; col < screen_Columns; ++col)
3187 {
3188 int off = line * screen_Columns + col;
3189
3190 if (popup_mask[off] != popup_mask_next[off])
3191 {
3192 popup_mask[off] = popup_mask_next[off];
3193
3194 if (line >= cmdline_row)
3195 {
3196 // the command line needs to be cleared if text below
3197 // the popup is now visible.
3198 if (!msg_scrolled && popup_mask_next[off] == 0)
3199 clear_cmdline = TRUE;
3200 }
3201 else if (col >= col_done)
3202 {
3203 linenr_T lnum;
3204 int line_cp = line;
3205 int col_cp = col;
3206
3207 // The screen position "line" / "col" needs to be
3208 // redrawn. Figure out what window that is and update
3209 // w_redraw_top and w_redr_bot. Only needs to be done
3210 // once for each window line.
3211 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3212 if (wp != NULL)
3213 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003214 if (wp != prev_wp)
3215 {
3216 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3217 prev_wp = wp;
3218 }
3219
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003220 if (line_cp >= wp->w_height)
3221 // In (or below) status line
3222 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003223 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003224 {
3225 // compute the position in the buffer line from
3226 // the position in the window
3227 mouse_comp_pos(wp, &line_cp, &col_cp,
3228 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003229 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003230 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003231
3232 // This line is going to be redrawn, no need to
3233 // check until the right side of the window.
3234 col_done = wp->w_wincol + wp->w_width - 1;
3235 }
3236 }
3237 }
3238 }
3239 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003240
3241 vim_free(plines_cache);
3242 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003243}
3244
3245/*
3246 * Return a string of "len" spaces in IObuff.
3247 */
3248 static char_u *
3249get_spaces(int len)
3250{
3251 vim_memset(IObuff, ' ', (size_t)len);
3252 IObuff[len] = NUL;
3253 return IObuff;
3254}
3255
3256/*
3257 * Update popup windows. They are drawn on top of normal windows.
3258 * "win_update" is called for each popup window, lowest zindex first.
3259 */
3260 void
3261update_popups(void (*win_update)(win_T *wp))
3262{
3263 win_T *wp;
3264 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003265 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003266 int total_width;
3267 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003268 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003269 int popup_attr;
3270 int border_attr[4];
3271 int border_char[8];
3272 char_u buf[MB_MAXBYTES];
3273 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003274 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003275 int padcol = 0;
3276 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003277 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003278 int sb_thumb_top = 0;
3279 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003280 int attr_scroll = 0;
3281 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003282
3283 // Find the window with the lowest zindex that hasn't been updated yet,
3284 // so that the window with a higher zindex is drawn later, thus goes on
3285 // top.
3286 popup_reset_handled();
3287 while ((wp = find_next_popup(TRUE)) != NULL)
3288 {
3289 // This drawing uses the zindex of the popup window, so that it's on
3290 // top of the text but doesn't draw when another popup with higher
3291 // zindex is on top of the character.
3292 screen_zindex = wp->w_zindex;
3293
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003294 // Set flags in popup_transparent[] for masked cells.
3295 update_popup_transparent(wp, 1);
3296
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003297 // adjust w_winrow and w_wincol for border and padding, since
3298 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003299 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003300 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3301 - wp->w_popup_leftoff;
3302 if (wp->w_wincol + left_extra < 0)
3303 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003304 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003305 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003306
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003307 // Draw the popup text, unless it's off screen.
3308 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003309 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003310 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003311
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003312 // move the cursor into the visible lines, otherwise executing
3313 // commands with win_execute() may cause the text to jump.
3314 if (wp->w_cursor.lnum < wp->w_topline)
3315 wp->w_cursor.lnum = wp->w_topline;
3316 else if (wp->w_cursor.lnum >= wp->w_botline)
3317 wp->w_cursor.lnum = wp->w_botline - 1;
3318 }
3319
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003320 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003321 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003322
Bram Moolenaard529ba52019-07-02 23:13:53 +02003323 total_width = popup_width(wp);
3324 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003325 popup_attr = get_wcr_attr(wp);
3326
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003327 if (wp->w_winrow + total_height > cmdline_row)
3328 wp->w_popup_flags |= POPF_ON_CMDLINE;
3329 else
3330 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3331
3332
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003333 // We can only use these line drawing characters when 'encoding' is
3334 // "utf-8" and 'ambiwidth' is "single".
3335 if (enc_utf8 && *p_ambw == 's')
3336 {
3337 border_char[0] = border_char[2] = 0x2550;
3338 border_char[1] = border_char[3] = 0x2551;
3339 border_char[4] = 0x2554;
3340 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003341 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3342 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003343 border_char[7] = 0x255a;
3344 }
3345 else
3346 {
3347 border_char[0] = border_char[2] = '-';
3348 border_char[1] = border_char[3] = '|';
3349 for (i = 4; i < 8; ++i)
3350 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003351 if (wp->w_popup_flags & POPF_RESIZE)
3352 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003353 }
3354 for (i = 0; i < 8; ++i)
3355 if (wp->w_border_char[i] != 0)
3356 border_char[i] = wp->w_border_char[i];
3357
3358 for (i = 0; i < 4; ++i)
3359 {
3360 border_attr[i] = popup_attr;
3361 if (wp->w_border_highlight[i] != NULL)
3362 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3363 }
3364
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003365 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003366 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003367 if (wp->w_popup_border[0] > 0)
3368 {
3369 // top border
3370 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003371 wincol < 0 ? 0 : wincol, wincol + total_width,
3372 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003373 ? border_char[4] : border_char[0],
3374 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003375 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003376 {
3377 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3378 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003379 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003380 }
3381 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003382 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3383 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003384
Bram Moolenaard529ba52019-07-02 23:13:53 +02003385 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3386 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003387 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003388 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3389 - wp->w_has_scrollbar;
3390 if (padcol < 0)
3391 {
3392 padwidth += padcol;
3393 padcol = 0;
3394 }
3395 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003396 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003397 {
3398 // top padding
3399 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003400 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003401 ' ', ' ', popup_attr);
3402 }
3403
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003404 // Title goes on top of border or padding.
3405 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003406 {
3407 int len = (int)STRLEN(wp->w_popup_title) + 1;
3408 char_u *title = alloc(len);
3409
3410 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3411 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003412 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003413 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003414 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003415
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003416 // Compute scrollbar thumb position and size.
3417 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003418 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003419 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3420 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003421
Bram Moolenaar076d9882019-09-14 22:23:29 +02003422 sb_thumb_height = (height * height + linecount / 2) / linecount;
3423 if (wp->w_topline > 1 && sb_thumb_height == height)
3424 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003425 if (sb_thumb_height == 0)
3426 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003427 if (linecount <= wp->w_height)
3428 // it just fits, avoid divide by zero
3429 sb_thumb_top = 0;
3430 else
3431 sb_thumb_top = (wp->w_topline - 1
3432 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003433 * (wp->w_height - sb_thumb_height)
3434 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003435 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3436 sb_thumb_top = 1; // show it's scrolled
3437
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003438 if (wp->w_scrollbar_highlight != NULL)
3439 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3440 else
3441 attr_scroll = highlight_attr[HLF_PSB];
3442 if (wp->w_thumb_highlight != NULL)
3443 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3444 else
3445 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003446 }
3447
3448 for (i = wp->w_popup_border[0];
3449 i < total_height - wp->w_popup_border[2]; ++i)
3450 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003451 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003452 // left and right padding only needed next to the body
3453 int do_padding =
3454 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3455 && i < total_height - wp->w_popup_border[2]
3456 - wp->w_popup_padding[2];
3457
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003458 row = wp->w_winrow + i;
3459
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003460 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003461 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003462 {
3463 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003464 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003465 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003466 if (do_padding && wp->w_popup_padding[3] > 0)
3467 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003468 int col = wincol + wp->w_popup_border[3];
3469
Bram Moolenaard529ba52019-07-02 23:13:53 +02003470 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003471 pad_left = wp->w_popup_padding[3];
3472 if (col < 0)
3473 {
3474 pad_left += col;
3475 col = 0;
3476 }
3477 if (pad_left > 0)
3478 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3479 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003480 // scrollbar
3481 if (wp->w_has_scrollbar)
3482 {
3483 int line = i - top_off;
3484 int scroll_col = wp->w_wincol + total_width - 1
3485 - wp->w_popup_border[1];
3486
3487 if (line >= 0 && line < wp->w_height)
3488 screen_putchar(' ', row, scroll_col,
3489 line >= sb_thumb_top
3490 && line < sb_thumb_top + sb_thumb_height
3491 ? attr_thumb : attr_scroll);
3492 else
3493 screen_putchar(' ', row, scroll_col, popup_attr);
3494 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003495 // right border
3496 if (wp->w_popup_border[1] > 0)
3497 {
3498 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003499 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003500 }
3501 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003502 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003503 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003504 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003505 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3506 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003507 }
3508
3509 if (wp->w_popup_padding[2] > 0)
3510 {
3511 // bottom padding
3512 row = wp->w_winrow + wp->w_popup_border[0]
3513 + wp->w_popup_padding[0] + wp->w_height;
3514 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003515 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003516 }
3517
3518 if (wp->w_popup_border[2] > 0)
3519 {
3520 // bottom border
3521 row = wp->w_winrow + total_height - 1;
3522 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003523 wincol < 0 ? 0 : wincol,
3524 wincol + total_width,
3525 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003526 ? border_char[7] : border_char[2],
3527 border_char[2], border_attr[2]);
3528 if (wp->w_popup_border[1] > 0)
3529 {
3530 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003531 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003532 }
3533 }
3534
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003535 if (wp->w_popup_close == POPCLOSE_BUTTON)
3536 {
3537 // close button goes on top of anything at the top-right corner
3538 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003539 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003540 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3541 }
3542
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003543 update_popup_transparent(wp, 0);
3544
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003545 // Back to the normal zindex.
3546 screen_zindex = 0;
3547 }
3548}
3549
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003550/*
3551 * Mark references in callbacks of one popup window.
3552 */
3553 static int
3554set_ref_in_one_popup(win_T *wp, int copyID)
3555{
3556 int abort = FALSE;
3557 typval_T tv;
3558
3559 if (wp->w_close_cb.cb_partial != NULL)
3560 {
3561 tv.v_type = VAR_PARTIAL;
3562 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3563 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3564 }
3565 if (wp->w_filter_cb.cb_partial != NULL)
3566 {
3567 tv.v_type = VAR_PARTIAL;
3568 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3569 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3570 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003571 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003572 return abort;
3573}
3574
3575/*
3576 * Set reference in callbacks of popup windows.
3577 */
3578 int
3579set_ref_in_popups(int copyID)
3580{
3581 int abort = FALSE;
3582 win_T *wp;
3583 tabpage_T *tp;
3584
3585 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3586 abort = abort || set_ref_in_one_popup(wp, copyID);
3587
3588 FOR_ALL_TABPAGES(tp)
3589 {
3590 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3591 abort = abort || set_ref_in_one_popup(wp, copyID);
3592 if (abort)
3593 break;
3594 }
3595 return abort;
3596}
Bram Moolenaar79648732019-07-18 21:43:07 +02003597
3598/*
3599 * Find an existing popup used as the preview window, in the current tab page.
3600 * Return NULL if not found.
3601 */
3602 win_T *
3603popup_find_preview_window(void)
3604{
3605 win_T *wp;
3606
3607 // Preview window popup is always local to tab page.
3608 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3609 if (wp->w_p_pvw)
3610 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003611 return NULL;
3612}
3613
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003614 int
3615popup_is_popup(win_T *wp)
3616{
3617 return wp->w_popup_flags != 0;
3618}
3619
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003620#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003621/*
3622 * Find an existing popup used as the info window, in the current tab page.
3623 * Return NULL if not found.
3624 */
3625 win_T *
3626popup_find_info_window(void)
3627{
3628 win_T *wp;
3629
3630 // info window popup is always local to tab page.
3631 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3632 if (wp->w_popup_flags & POPF_INFO)
3633 return wp;
3634 return NULL;
3635}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003636#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003637
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003638 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003639f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3640{
3641 win_T *wp = popup_find_info_window();
3642
3643 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3644}
3645
3646 void
3647f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003648{
3649 win_T *wp = popup_find_preview_window();
3650
3651 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003652}
3653
Bram Moolenaar79648732019-07-18 21:43:07 +02003654/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003655 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003656 * NOTE: this makes the popup the current window, so that the file can be
3657 * edited. However, it must not remain to be the current window, the caller
3658 * must make sure of that.
3659 */
3660 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003661popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003662{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003663 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003664
3665 if (wp == NULL)
3666 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003667 if (info)
3668 wp->w_popup_flags |= POPF_INFO;
3669 else
3670 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003671
3672 // Set the width to a reasonable value, so that w_topline can be computed.
3673 if (wp->w_minwidth > 0)
3674 wp->w_width = wp->w_minwidth;
3675 else if (wp->w_maxwidth > 0)
3676 wp->w_width = wp->w_maxwidth;
3677 else
3678 wp->w_width = curwin->w_width;
3679
3680 // Will switch to another buffer soon, dummy one can be wiped.
3681 wp->w_buffer->b_locked = FALSE;
3682
3683 win_enter(wp, FALSE);
3684 return OK;
3685}
3686
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003687#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003688/*
3689 * Close any preview popup.
3690 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003691 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003692popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003693{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003694 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003695
3696 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003697 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003698}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003699
3700/*
3701 * Hide the info popup.
3702 */
3703 void
3704popup_hide_info(void)
3705{
3706 win_T *wp = popup_find_info_window();
3707
3708 if (wp != NULL)
3709 popup_hide(wp);
3710}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003711#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003712
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003713/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003714 * Close any popup for a text property associated with "win".
3715 * Return TRUE if a popup was closed.
3716 */
3717 int
3718popup_win_closed(win_T *win)
3719{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003720 int round;
3721 win_T *wp;
3722 win_T *next;
3723 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003724
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003725 for (round = 1; round <= 2; ++round)
3726 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3727 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003728 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003729 next = wp->w_next;
3730 if (wp->w_popup_prop_win == win)
3731 {
3732 popup_close_with_retval(wp, -1);
3733 ret = TRUE;
3734 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003735 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003736 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003737}
3738
3739/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003740 * Set the title of the popup window to the file name.
3741 */
3742 void
3743popup_set_title(win_T *wp)
3744{
3745 if (wp->w_buffer->b_fname != NULL)
3746 {
3747 char_u dirname[MAXPATHL];
3748 size_t len;
3749
3750 mch_dirname(dirname, MAXPATHL);
3751 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3752
3753 vim_free(wp->w_popup_title);
3754 len = STRLEN(wp->w_buffer->b_fname) + 3;
3755 wp->w_popup_title = alloc((int)len);
3756 if (wp->w_popup_title != NULL)
3757 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3758 wp->w_buffer->b_fname);
3759 redraw_win_later(wp, VALID);
3760 }
3761}
3762
3763/*
3764 * If there is a preview window, update the title.
3765 * Used after changing directory.
3766 */
3767 void
3768popup_update_preview_title(void)
3769{
3770 win_T *wp = popup_find_preview_window();
3771
3772 if (wp != NULL)
3773 popup_set_title(wp);
3774}
3775
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003776#endif // FEAT_TEXT_PROP