blob: a9579320e8f7226f85a2a09230f58595584d0839 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
74 if (n < 1)
75 n = 1;
76 return n;
77}
78
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020079 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020080set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081{
82 dictitem_T *di;
83
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020084 di = dict_find(dict, (char_u *)name, -1);
85 if (di != NULL)
86 {
87 if (di->di_tv.v_type != VAR_LIST)
88 emsg(_(e_listreq));
89 else
90 {
91 list_T *list = di->di_tv.vval.v_list;
92 listitem_T *li;
93 int i;
94 int nr;
95
96 for (i = 0; i < 4; ++i)
97 array[i] = 1;
98 if (list != NULL)
99 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
100 ++i, li = li->li_next)
101 {
102 nr = (int)tv_get_number(&li->li_tv);
103 if (nr >= 0)
104 array[i] = nr > max_val ? max_val : nr;
105 }
106 }
107 }
108}
109
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200110/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200111 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200112 */
113 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200114set_moved_values(win_T *wp)
115{
116 wp->w_popup_curwin = curwin;
117 wp->w_popup_lnum = curwin->w_cursor.lnum;
118 wp->w_popup_mincol = curwin->w_cursor.col;
119 wp->w_popup_maxcol = curwin->w_cursor.col;
120}
121
122/*
123 * Used when popup options contain "moved" with "word" or "WORD".
124 */
125 static void
126set_moved_columns(win_T *wp, int flags)
127{
128 char_u *ptr;
129 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
130
131 if (len > 0)
132 {
133 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
134 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
135 }
136}
137
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200138/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200139 * Used when popup options contain "mousemoved": set default moved values.
140 */
141 static void
142set_mousemoved_values(win_T *wp)
143{
144 wp->w_popup_mouse_row = mouse_row;
145 wp->w_popup_mouse_mincol = mouse_col;
146 wp->w_popup_mouse_maxcol = mouse_col;
147}
148
149/*
150 * Used when popup options contain "moved" with "word" or "WORD".
151 */
152 static void
153set_mousemoved_columns(win_T *wp, int flags)
154{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200155 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200156 char_u *text;
157 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200158 pos_T pos;
159 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160
161 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200163 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200164 // convert text column to mouse column
165 pos.col = col;
166 pos.coladd = 0;
167 getvcol(textwp, &pos, &mcol, NULL, NULL);
168 wp->w_popup_mouse_mincol = mcol;
169
Bram Moolenaar10727682019-07-12 13:59:20 +0200170 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200171 getvcol(textwp, &pos, NULL, NULL, &mcol);
172 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200173 vim_free(text);
174 }
175}
176
177/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200178 * Return TRUE if "row"/"col" is on the border of the popup.
179 * The values are relative to the top-left corner.
180 */
181 int
182popup_on_border(win_T *wp, int row, int col)
183{
184 return (row == 0 && wp->w_popup_border[0] > 0)
185 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
186 || (col == 0 && wp->w_popup_border[3] > 0)
187 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
188}
189
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200190/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200191 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
192 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200193 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200194 * Caller should check the left mouse button was clicked.
195 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200196 */
197 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200199{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200200 if (wp->w_popup_close == POPCLOSE_BUTTON
201 && row == 0 && col == popup_width(wp) - 1)
202 {
203 popup_close_for_mouse_click(wp);
204 return TRUE;
205 }
206 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200207}
208
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200209// Values set when dragging a popup window starts.
210static int drag_start_row;
211static int drag_start_col;
212static int drag_start_wantline;
213static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200214static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200215
216/*
217 * Mouse down on border of popup window: start dragging it.
218 * Uses mouse_col and mouse_row.
219 */
220 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200221popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222{
223 drag_start_row = mouse_row;
224 drag_start_col = mouse_col;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200225 if (wp->w_wantline == 0)
226 drag_start_wantline = wp->w_winrow + 1;
227 else
228 drag_start_wantline = wp->w_wantline;
229 if (wp->w_wantcol == 0)
230 drag_start_wantcol = wp->w_wincol + 1;
231 else
232 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200233
234 // Stop centering the popup
235 if (wp->w_popup_pos == POPPOS_CENTER)
236 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200237
238 drag_on_resize_handle = wp->w_popup_border[1] > 0
239 && wp->w_popup_border[2] > 0
240 && row == popup_height(wp) - 1
241 && col == popup_width(wp) - 1;
242
243 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
244 {
245 if (wp->w_popup_pos == POPPOS_TOPRIGHT
246 || wp->w_popup_pos == POPPOS_BOTRIGHT)
247 wp->w_wantcol = wp->w_wincol + 1;
248 if (wp->w_popup_pos == POPPOS_BOTLEFT)
249 wp->w_wantline = wp->w_winrow + 1;
250 wp->w_popup_pos = POPPOS_TOPLEFT;
251 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200252}
253
254/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200255 * Mouse moved while dragging a popup window: adjust the window popup position
256 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200257 */
258 void
259popup_drag(win_T *wp)
260{
261 // The popup may be closed before dragging stops.
262 if (!win_valid_popup(wp))
263 return;
264
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200265 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
266 {
267 int width_inc = mouse_col - drag_start_col;
268 int height_inc = mouse_row - drag_start_row;
269
270 if (width_inc != 0)
271 {
272 int width = wp->w_width + width_inc;
273
274 if (width < 1)
275 width = 1;
276 wp->w_minwidth = width;
277 wp->w_maxwidth = width;
278 drag_start_col = mouse_col;
279 }
280
281 if (height_inc != 0)
282 {
283 int height = wp->w_height + height_inc;
284
285 if (height < 1)
286 height = 1;
287 wp->w_minheight = height;
288 wp->w_maxheight = height;
289 drag_start_row = mouse_row;
290 }
291
292 popup_adjust_position(wp);
293 return;
294 }
295
296 if (!(wp->w_popup_flags & POPF_DRAG))
297 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200298 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
299 if (wp->w_wantline < 1)
300 wp->w_wantline = 1;
301 if (wp->w_wantline > Rows)
302 wp->w_wantline = Rows;
303 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
304 if (wp->w_wantcol < 1)
305 wp->w_wantcol = 1;
306 if (wp->w_wantcol > Columns)
307 wp->w_wantcol = Columns;
308
309 popup_adjust_position(wp);
310}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200311
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200312/*
313 * Set w_firstline to match the current "wp->w_topline".
314 */
315 void
316popup_set_firstline(win_T *wp)
317{
318 int height = wp->w_height;
319
320 wp->w_firstline = wp->w_topline;
321 popup_adjust_position(wp);
322
323 // we don't want the popup to get smaller, decrement the first line
324 // until it doesn't
325 while (wp->w_firstline > 1 && wp->w_height < height)
326 {
327 --wp->w_firstline;
328 popup_adjust_position(wp);
329 }
330}
331
332/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200333 * Return TRUE if the position is in the popup window scrollbar.
334 */
335 int
336popup_is_in_scrollbar(win_T *wp, int row, int col)
337{
338 return wp->w_has_scrollbar
339 && row >= wp->w_popup_border[0]
340 && row < popup_height(wp) - wp->w_popup_border[2]
341 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
342}
343
344
345/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200346 * Handle a click in a popup window, if it is in the scrollbar.
347 */
348 void
349popup_handle_scrollbar_click(win_T *wp, int row, int col)
350{
351 int height = popup_height(wp);
352 int old_topline = wp->w_topline;
353
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200354 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200355 {
356 if (row >= height / 2)
357 {
358 // Click in lower half, scroll down.
359 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
360 ++wp->w_topline;
361 }
362 else if (wp->w_topline > 1)
363 // click on upper half, scroll up.
364 --wp->w_topline;
365 if (wp->w_topline != old_topline)
366 {
367 popup_set_firstline(wp);
368 redraw_win_later(wp, NOT_VALID);
369 }
370 }
371}
372
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200373#if defined(FEAT_TIMERS)
374 static void
375popup_add_timeout(win_T *wp, int time)
376{
377 char_u cbbuf[50];
378 char_u *ptr = cbbuf;
379 typval_T tv;
380
381 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
382 "{_ -> popup_close(%d)}", wp->w_id);
383 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
384 {
385 wp->w_popup_timer = create_timer(time, 0);
386 wp->w_popup_timer->tr_callback = get_callback(&tv);
387 clear_tv(&tv);
388 }
389}
390#endif
391
Bram Moolenaar17627312019-06-02 19:53:44 +0200392/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200393 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200394 */
395 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200396apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200397{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200398 int nr;
399 char_u *str;
400 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200401
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200402 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200403 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200404 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200405 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200406 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200407 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200408 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200409 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200410
411 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200412 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200413 wp->w_wantline = nr;
414 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200415 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200416 wp->w_wantcol = nr;
417
418 di = dict_find(d, (char_u *)"fixed", -1);
419 if (di != NULL)
420 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
421
422 str = dict_get_string(d, (char_u *)"pos", FALSE);
423 if (str != NULL)
424 {
425 for (nr = 0;
426 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
427 ++nr)
428 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
429 {
430 wp->w_popup_pos = poppos_entries[nr].pp_val;
431 nr = -1;
432 break;
433 }
434 if (nr != -1)
435 semsg(_(e_invarg2), str);
436 }
437
438 str = dict_get_string(d, (char_u *)"textprop", FALSE);
439 if (str != NULL)
440 {
441 wp->w_popup_prop_type = 0;
442 if (*str != NUL)
443 {
444 nr = find_prop_type_id(str, wp->w_buffer);
445 if (nr <= 0)
446 nr = find_prop_type_id(str, NULL);
447 if (nr <= 0)
448 semsg(_(e_invarg2), str);
449 else
450 {
451 wp->w_popup_prop_type = nr;
452 wp->w_popup_prop_win = curwin;
453
454 di = dict_find(d, (char_u *)"textpropwin", -1);
455 if (di != NULL)
456 {
457 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
458 if (win_valid(wp->w_popup_prop_win))
459 wp->w_popup_prop_win = curwin;
460 }
461 }
462 }
463 }
464
465 di = dict_find(d, (char_u *)"textpropid", -1);
466 if (di != NULL)
467 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200468}
469
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200470 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200471handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
472{
473 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
474 {
475 char_u *s = di->di_tv.vval.v_string;
476 int flags = 0;
477
478 if (STRCMP(s, "word") == 0)
479 flags = FIND_IDENT | FIND_STRING;
480 else if (STRCMP(s, "WORD") == 0)
481 flags = FIND_STRING;
482 else if (STRCMP(s, "expr") == 0)
483 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
484 else if (STRCMP(s, "any") != 0)
485 semsg(_(e_invarg2), s);
486 if (flags != 0)
487 {
488 if (mousemoved)
489 set_mousemoved_columns(wp, flags);
490 else
491 set_moved_columns(wp, flags);
492 }
493 }
494 else if (di->di_tv.v_type == VAR_LIST
495 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200496 && (di->di_tv.vval.v_list->lv_len == 2
497 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200498 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200499 list_T *l = di->di_tv.vval.v_list;
500 listitem_T *li = l->lv_first;
501 int mincol;
502 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200503
Bram Moolenaara1396152019-10-20 18:46:05 +0200504 if (di->di_tv.vval.v_list->lv_len == 3)
505 {
506 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
507
508 // Three numbers, might be from popup_getoptions().
509 if (mousemoved)
510 wp->w_popup_mouse_row = nr;
511 else
512 wp->w_popup_lnum = nr;
513 li = li->li_next;
514 }
515
516 mincol = tv_get_number(&li->li_tv);
517 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200518 if (mousemoved)
519 {
520 wp->w_popup_mouse_mincol = mincol;
521 wp->w_popup_mouse_maxcol = maxcol;
522 }
523 else
524 {
525 wp->w_popup_mincol = mincol;
526 wp->w_popup_maxcol = maxcol;
527 }
528 }
529 else
530 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
531}
532
533 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200534check_highlight(dict_T *dict, char *name, char_u **pval)
535{
536 dictitem_T *di;
537 char_u *str;
538
539 di = dict_find(dict, (char_u *)name, -1);
540 if (di != NULL)
541 {
542 if (di->di_tv.v_type != VAR_STRING)
543 semsg(_(e_invargval), name);
544 else
545 {
546 str = tv_get_string(&di->di_tv);
547 if (*str != NUL)
548 *pval = vim_strsave(str);
549 }
550 }
551}
552
Bram Moolenaarae943152019-06-16 22:54:14 +0200553/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200554 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200555 */
556 static void
557popup_show_curline(win_T *wp)
558{
559 if (wp->w_cursor.lnum < wp->w_topline)
560 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200561 else if (wp->w_cursor.lnum >= wp->w_botline
562 && (curwin->w_valid & VALID_BOTLINE))
563 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200564 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200565 if (wp->w_topline < 1)
566 wp->w_topline = 1;
567 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
568 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200569 while (wp->w_topline < wp->w_cursor.lnum
570 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
571 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
572 > wp->w_height)
573 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200574 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200575
576 // Don't use "firstline" now.
577 wp->w_firstline = 0;
578}
579
580/*
581 * Get the sign group name for window "wp".
582 * Returns a pointer to a static buffer, overwritten on the next call.
583 */
584 static char_u *
585popup_get_sign_name(win_T *wp)
586{
587 static char buf[30];
588
589 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
590 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200591}
592
593/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200594 * Highlight the line with the cursor.
595 * Also scrolls the text to put the cursor line in view.
596 */
597 static void
598popup_highlight_curline(win_T *wp)
599{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200600 int sign_id = 0;
601 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200602
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200603 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200604
605 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
606 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200607 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200608
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200609 if (!sign_exists_by_name(sign_name))
610 {
611 char *linehl = "PopupSelected";
612
613 if (syn_name2id((char_u *)linehl) == 0)
614 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200615 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200616 }
617
618 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
619 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
620 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200621 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200622 else
623 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200624 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200625}
626
627/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200628 * Shared between popup_create() and f_popup_setoptions().
629 */
630 static void
631apply_general_options(win_T *wp, dict_T *dict)
632{
633 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200634 int nr;
635 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200636
Bram Moolenaarae943152019-06-16 22:54:14 +0200637 // TODO: flip
638
639 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200640 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200641 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200642 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200643 if (wp->w_firstline < 0)
644 wp->w_firstline = -1;
645 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200646
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200647 di = dict_find(dict, (char_u *)"scrollbar", -1);
648 if (di != NULL)
649 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
650
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200651 str = dict_get_string(dict, (char_u *)"title", FALSE);
652 if (str != NULL)
653 {
654 vim_free(wp->w_popup_title);
655 wp->w_popup_title = vim_strsave(str);
656 }
657
Bram Moolenaar402502d2019-05-30 22:07:36 +0200658 di = dict_find(dict, (char_u *)"wrap", -1);
659 if (di != NULL)
660 {
661 nr = dict_get_number(dict, (char_u *)"wrap");
662 wp->w_p_wrap = nr != 0;
663 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200664
Bram Moolenaara42d9452019-06-15 21:46:30 +0200665 di = dict_find(dict, (char_u *)"drag", -1);
666 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200667 {
668 nr = dict_get_number(dict, (char_u *)"drag");
669 if (nr)
670 wp->w_popup_flags |= POPF_DRAG;
671 else
672 wp->w_popup_flags &= ~POPF_DRAG;
673 }
674
675 di = dict_find(dict, (char_u *)"resize", -1);
676 if (di != NULL)
677 {
678 nr = dict_get_number(dict, (char_u *)"resize");
679 if (nr)
680 wp->w_popup_flags |= POPF_RESIZE;
681 else
682 wp->w_popup_flags &= ~POPF_RESIZE;
683 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200684
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200685 di = dict_find(dict, (char_u *)"close", -1);
686 if (di != NULL)
687 {
688 int ok = TRUE;
689
690 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
691 {
692 char_u *s = di->di_tv.vval.v_string;
693
694 if (STRCMP(s, "none") == 0)
695 wp->w_popup_close = POPCLOSE_NONE;
696 else if (STRCMP(s, "button") == 0)
697 wp->w_popup_close = POPCLOSE_BUTTON;
698 else if (STRCMP(s, "click") == 0)
699 wp->w_popup_close = POPCLOSE_CLICK;
700 else
701 ok = FALSE;
702 }
703 else
704 ok = FALSE;
705 if (!ok)
706 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
707 }
708
Bram Moolenaarae943152019-06-16 22:54:14 +0200709 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
710 if (str != NULL)
711 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
712 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200713
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200714 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
715 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200716 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
717 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200718
Bram Moolenaar790498b2019-06-01 22:15:29 +0200719 di = dict_find(dict, (char_u *)"borderhighlight", -1);
720 if (di != NULL)
721 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200722 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200723 emsg(_(e_listreq));
724 else
725 {
726 list_T *list = di->di_tv.vval.v_list;
727 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200728 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200729
Bram Moolenaar403d0902019-07-17 21:37:32 +0200730 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
731 ++i, li = li->li_next)
732 {
733 str = tv_get_string(&li->li_tv);
734 if (*str != NUL)
735 wp->w_border_highlight[i] = vim_strsave(str);
736 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200737 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
738 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200739 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200740 vim_strsave(wp->w_border_highlight[0]);
741 }
742 }
743
Bram Moolenaar790498b2019-06-01 22:15:29 +0200744 di = dict_find(dict, (char_u *)"borderchars", -1);
745 if (di != NULL)
746 {
747 if (di->di_tv.v_type != VAR_LIST)
748 emsg(_(e_listreq));
749 else
750 {
751 list_T *list = di->di_tv.vval.v_list;
752 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200753 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200754
755 if (list != NULL)
756 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
757 ++i, li = li->li_next)
758 {
759 str = tv_get_string(&li->li_tv);
760 if (*str != NUL)
761 wp->w_border_char[i] = mb_ptr2char(str);
762 }
763 if (list->lv_len == 1)
764 for (i = 1; i < 8; ++i)
765 wp->w_border_char[i] = wp->w_border_char[0];
766 if (list->lv_len == 2)
767 {
768 for (i = 4; i < 8; ++i)
769 wp->w_border_char[i] = wp->w_border_char[1];
770 for (i = 1; i < 4; ++i)
771 wp->w_border_char[i] = wp->w_border_char[0];
772 }
773 }
774 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200775
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200776 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
777 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
778
Bram Moolenaarae943152019-06-16 22:54:14 +0200779 di = dict_find(dict, (char_u *)"zindex", -1);
780 if (di != NULL)
781 {
782 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
783 if (wp->w_zindex < 1)
784 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
785 if (wp->w_zindex > 32000)
786 wp->w_zindex = 32000;
787 }
788
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200789 di = dict_find(dict, (char_u *)"mask", -1);
790 if (di != NULL)
791 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200792 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200793
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200794 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200795 {
796 listitem_T *li;
797
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200798 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200799 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
800 li = li->li_next)
801 {
802 if (li->li_tv.v_type != VAR_LIST
803 || li->li_tv.vval.v_list == NULL
804 || li->li_tv.vval.v_list->lv_len != 4)
805 {
806 ok = FALSE;
807 break;
808 }
809 }
810 }
811 if (ok)
812 {
813 wp->w_popup_mask = di->di_tv.vval.v_list;
814 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200815 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200816 }
817 else
818 semsg(_(e_invargval), "mask");
819 }
820
Bram Moolenaarae943152019-06-16 22:54:14 +0200821#if defined(FEAT_TIMERS)
822 // Add timer to close the popup after some time.
823 nr = dict_get_number(dict, (char_u *)"time");
824 if (nr > 0)
825 popup_add_timeout(wp, nr);
826#endif
827
Bram Moolenaar3397f742019-06-02 18:40:06 +0200828 di = dict_find(dict, (char_u *)"moved", -1);
829 if (di != NULL)
830 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200831 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200832 handle_moved_argument(wp, di, FALSE);
833 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200834
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200835 di = dict_find(dict, (char_u *)"mousemoved", -1);
836 if (di != NULL)
837 {
838 set_mousemoved_values(wp);
839 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200840 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200841
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200842 di = dict_find(dict, (char_u *)"cursorline", -1);
843 if (di != NULL)
844 {
845 if (di->di_tv.v_type == VAR_NUMBER)
846 {
847 if (di->di_tv.vval.v_number != 0)
848 wp->w_popup_flags |= POPF_CURSORLINE;
849 else
850 wp->w_popup_flags &= ~POPF_CURSORLINE;
851 }
852 else
853 semsg(_(e_invargval), "cursorline");
854 }
855
Bram Moolenaarae943152019-06-16 22:54:14 +0200856 di = dict_find(dict, (char_u *)"filter", -1);
857 if (di != NULL)
858 {
859 callback_T callback = get_callback(&di->di_tv);
860
861 if (callback.cb_name != NULL)
862 {
863 free_callback(&wp->w_filter_cb);
864 set_callback(&wp->w_filter_cb, &callback);
865 }
866 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200867 di = dict_find(dict, (char_u *)"mapping", -1);
868 if (di != NULL)
869 {
870 nr = dict_get_number(dict, (char_u *)"mapping");
871 if (nr)
872 wp->w_popup_flags |= POPF_MAPPING;
873 else
874 wp->w_popup_flags &= ~POPF_MAPPING;
875 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200876
Bram Moolenaar581ba392019-09-03 22:08:33 +0200877 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
878 if (str != NULL)
879 {
880 if (STRCMP(str, "a") == 0)
881 wp->w_filter_mode = MODE_ALL;
882 else
883 wp->w_filter_mode = mode_str2flags(str);
884 }
885
Bram Moolenaarae943152019-06-16 22:54:14 +0200886 di = dict_find(dict, (char_u *)"callback", -1);
887 if (di != NULL)
888 {
889 callback_T callback = get_callback(&di->di_tv);
890
891 if (callback.cb_name != NULL)
892 {
893 free_callback(&wp->w_close_cb);
894 set_callback(&wp->w_close_cb, &callback);
895 }
896 }
897}
898
899/*
900 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200901 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200902 */
903 static void
904apply_options(win_T *wp, dict_T *dict)
905{
906 int nr;
907
908 apply_move_options(wp, dict);
909 apply_general_options(wp, dict);
910
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200911 nr = dict_get_number(dict, (char_u *)"hidden");
912 if (nr > 0)
913 {
914 wp->w_popup_flags |= POPF_HIDDEN;
915 --wp->w_buffer->b_nwindows;
916 }
917
Bram Moolenaar33796b32019-06-08 16:01:13 +0200918 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200919 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200920}
921
922/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200923 * Add lines to the popup from a list of strings.
924 */
925 static void
926add_popup_strings(buf_T *buf, list_T *l)
927{
928 listitem_T *li;
929 linenr_T lnum = 0;
930 char_u *p;
931
932 for (li = l->lv_first; li != NULL; li = li->li_next)
933 if (li->li_tv.v_type == VAR_STRING)
934 {
935 p = li->li_tv.vval.v_string;
936 ml_append_buf(buf, lnum++,
937 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
938 }
939}
940
941/*
942 * Add lines to the popup from a list of dictionaries.
943 */
944 static void
945add_popup_dicts(buf_T *buf, list_T *l)
946{
947 listitem_T *li;
948 listitem_T *pli;
949 linenr_T lnum = 0;
950 char_u *p;
951 dict_T *dict;
952
953 // first add the text lines
954 for (li = l->lv_first; li != NULL; li = li->li_next)
955 {
956 if (li->li_tv.v_type != VAR_DICT)
957 {
958 emsg(_(e_dictreq));
959 return;
960 }
961 dict = li->li_tv.vval.v_dict;
962 p = dict == NULL ? NULL
963 : dict_get_string(dict, (char_u *)"text", FALSE);
964 ml_append_buf(buf, lnum++,
965 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
966 }
967
968 // add the text properties
969 lnum = 1;
970 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
971 {
972 dictitem_T *di;
973 list_T *plist;
974
975 dict = li->li_tv.vval.v_dict;
976 di = dict_find(dict, (char_u *)"props", -1);
977 if (di != NULL)
978 {
979 if (di->di_tv.v_type != VAR_LIST)
980 {
981 emsg(_(e_listreq));
982 return;
983 }
984 plist = di->di_tv.vval.v_list;
985 if (plist != NULL)
986 {
987 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
988 {
989 if (pli->li_tv.v_type != VAR_DICT)
990 {
991 emsg(_(e_dictreq));
992 return;
993 }
994 dict = pli->li_tv.vval.v_dict;
995 if (dict != NULL)
996 {
997 int col = dict_get_number(dict, (char_u *)"col");
998
999 prop_add_common( lnum, col, dict, buf, NULL);
1000 }
1001 }
1002 }
1003 }
1004 }
1005}
1006
1007/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001008 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1009 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001010 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001011popup_top_extra(win_T *wp)
1012{
1013 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1014
1015 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1016 return 1;
1017 return extra;
1018}
1019
1020/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001021 * Return the height of popup window "wp", including border and padding.
1022 */
1023 int
1024popup_height(win_T *wp)
1025{
1026 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001027 + popup_top_extra(wp)
1028 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001029}
1030
1031/*
1032 * Return the width of popup window "wp", including border, padding and
1033 * scrollbar.
1034 */
1035 int
1036popup_width(win_T *wp)
1037{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001038 // w_leftcol is how many columns of the core are left of the screen
1039 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001040 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001041 + popup_extra_width(wp)
1042 + wp->w_popup_rightoff;
1043}
1044
1045/*
1046 * Return the extra width of popup window "wp": border, padding and scrollbar.
1047 */
1048 int
1049popup_extra_width(win_T *wp)
1050{
1051 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1052 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1053 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001054}
1055
1056/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001057 * Adjust the position and size of the popup to fit on the screen.
1058 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001059 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001060popup_adjust_position(win_T *wp)
1061{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001062 linenr_T lnum;
1063 int wrapped = 0;
1064 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001065 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001066 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001067 int center_vert = FALSE;
1068 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001069 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001070 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001071 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1072 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1073 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1074 int extra_height = top_extra + bot_extra;
1075 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001076 int org_winrow = wp->w_winrow;
1077 int org_wincol = wp->w_wincol;
1078 int org_width = wp->w_width;
1079 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001080 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001081 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001082 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001083 int wantline = wp->w_wantline; // adjusted for textprop
1084 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001085
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001086 wp->w_winrow = 0;
1087 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001088 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001089 wp->w_popup_leftoff = 0;
1090 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001091
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001092 // May need to update the "cursorline" highlighting, which may also change
1093 // "topline"
1094 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1095 popup_highlight_curline(wp);
1096
Bram Moolenaar12034e22019-08-25 22:25:02 +02001097 // If no line was specified default to vertical centering.
1098 if (wantline == 0)
1099 center_vert = TRUE;
1100
1101 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1102 {
1103 win_T *prop_win = wp->w_popup_prop_win;
1104 textprop_T prop;
1105 linenr_T prop_lnum;
1106 pos_T pos;
1107 int screen_row;
1108 int screen_scol;
1109 int screen_ccol;
1110 int screen_ecol;
1111
1112 // Popup window is positioned relative to a text property.
1113 if (find_visible_prop(prop_win,
1114 wp->w_popup_prop_type, wp->w_popup_prop_id,
1115 &prop, &prop_lnum) == FAIL)
1116 {
1117 // Text property is no longer visible, hide the popup.
1118 // Unhiding the popup is done in check_popup_unhidden().
1119 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1120 {
1121 wp->w_popup_flags |= POPF_HIDDEN;
1122 --wp->w_buffer->b_nwindows;
1123 if (win_valid(wp->w_popup_prop_win))
1124 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1125 }
1126 return;
1127 }
1128
1129 // Compute the desired position from the position of the text
1130 // property. Use "wantline" and "wantcol" as offsets.
1131 pos.lnum = prop_lnum;
1132 pos.col = prop.tp_col;
1133 if (wp->w_popup_pos == POPPOS_TOPLEFT
1134 || wp->w_popup_pos == POPPOS_BOTLEFT)
1135 pos.col += prop.tp_len - 1;
1136 textpos2screenpos(prop_win, &pos, &screen_row,
1137 &screen_scol, &screen_ccol, &screen_ecol);
1138
1139 if (wp->w_popup_pos == POPPOS_TOPLEFT
1140 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1141 // below the text
1142 wantline = screen_row + wantline + 1;
1143 else
1144 // above the text
1145 wantline = screen_row + wantline - 1;
1146 center_vert = FALSE;
1147 if (wp->w_popup_pos == POPPOS_TOPLEFT
1148 || wp->w_popup_pos == POPPOS_BOTLEFT)
1149 // right of the text
1150 wantcol = screen_ecol + wantcol;
1151 else
1152 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001153 wantcol = screen_scol + wantcol - 2;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001154 }
1155
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001156 if (wp->w_popup_pos == POPPOS_CENTER)
1157 {
1158 // center after computing the size
1159 center_vert = TRUE;
1160 center_hor = TRUE;
1161 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001162 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001163 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001164 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
Bram Moolenaar12034e22019-08-25 22:25:02 +02001165 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001166 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001167 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001168 if (wp->w_winrow >= Rows)
1169 wp->w_winrow = Rows - 1;
1170 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171
Bram Moolenaar12034e22019-08-25 22:25:02 +02001172 if (wantcol == 0)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001173 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001174 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1175 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001176 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001177 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001178 if (wp->w_wincol >= Columns - 3)
1179 wp->w_wincol = Columns - 3;
1180 }
1181 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001182
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001183 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001184 // When left aligned use the space available, but shift to the left when we
1185 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001186 maxspace = Columns - wp->w_wincol - left_extra;
1187 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001188 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001189 {
1190 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001191 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001192 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001193
Bram Moolenaar8d241042019-06-12 23:40:01 +02001194 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001195 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001196 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001197 if (wp->w_topline < 1)
1198 wp->w_topline = 1;
1199 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001200 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1201
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001202 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001203 // Use a minimum width of one, so that something shows when there is no
1204 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001205 // When "firstline" is -1 then start with the last buffer line and go
1206 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001207 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001208 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001209 if (wp->w_firstline < 0)
1210 lnum = wp->w_buffer->b_ml.ml_line_count;
1211 else
1212 lnum = wp->w_topline;
1213 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001214 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001215 int len;
1216 int w_width = wp->w_width;
1217
1218 // Count Tabs for what they are worth and compute the length based on
1219 // the maximum width (matters when 'showbreak' is set).
1220 if (wp->w_width < maxwidth)
1221 wp->w_width = maxwidth;
1222 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001223 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001224 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001225
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001226 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001227 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001228 while (len > maxwidth)
1229 {
1230 ++wrapped;
1231 len -= maxwidth;
1232 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001233 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001234 }
1235 }
1236 else if (len > maxwidth
1237 && allow_adjust_left
1238 && (wp->w_popup_pos == POPPOS_TOPLEFT
1239 || wp->w_popup_pos == POPPOS_BOTLEFT))
1240 {
1241 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001242 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001243
Bram Moolenaar51c31312019-06-15 22:27:23 +02001244 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001245 {
1246 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001247
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001248 len -= truncate_shift;
1249 shift_by -= truncate_shift;
1250 }
1251
1252 wp->w_wincol -= shift_by;
1253 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001254 wp->w_width = maxwidth;
1255 }
1256 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001257 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001258 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001259 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1260 wp->w_width = wp->w_maxwidth;
1261 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001262 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001263 if (wp->w_maxheight > 0
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001264 && (wp->w_firstline >= 0
1265 ? lnum - wp->w_topline
1266 : wp->w_buffer->b_ml.ml_line_count - lnum)
1267 + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001268 break;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001269
1270 if (wp->w_firstline < 0)
1271 --lnum;
1272 else
1273 ++lnum;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001274 }
1275
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001276 if (wp->w_firstline < 0)
1277 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1278
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001279 wp->w_has_scrollbar = wp->w_want_scrollbar
1280 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001281 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001282 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001283 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001284 ++extra_width;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001285 if (used_maxwidth)
1286 maxwidth -= 2; // try to show the scrollbar
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001287 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001288
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001289 minwidth = wp->w_minwidth;
1290 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1291 {
1292 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1293
1294 if (minwidth < title_len)
1295 minwidth = title_len;
1296 }
1297
1298 if (minwidth > 0 && wp->w_width < minwidth)
1299 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001300 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001301 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001302 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001303 // some columns cut off on the right
1304 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001305 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001306 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001307 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001308 {
1309 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1310 if (wp->w_wincol < 0)
1311 wp->w_wincol = 0;
1312 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001313 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1314 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1315 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001316 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001317
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001318 // Right aligned: move to the right if needed.
1319 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001320 if (leftoff >= 0)
1321 wp->w_wincol = leftoff;
1322 else if (wp->w_popup_fixed)
1323 {
1324 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001325 // columns when displaying the window, minus left border and
1326 // padding.
1327 if (-leftoff > left_extra)
1328 wp->w_leftcol = -leftoff - left_extra;
1329 wp->w_width -= wp->w_leftcol;
1330 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001331 if (wp->w_width < 0)
1332 wp->w_width = 0;
1333 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001334 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001335
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001336 if (wp->w_p_wrap || (!wp->w_popup_fixed
1337 && (wp->w_popup_pos == POPPOS_TOPLEFT
1338 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001339 {
1340 int want_col = 0;
1341
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001342 // try to show the right border and any scrollbar
1343 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001344 if (want_col > 0 && wp->w_wincol > 0
1345 && wp->w_wincol + want_col >= Columns)
1346 {
1347 wp->w_wincol = Columns - want_col;
1348 if (wp->w_wincol < 0)
1349 wp->w_wincol = 0;
1350 }
1351 }
1352
Bram Moolenaar8d241042019-06-12 23:40:01 +02001353 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1354 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001355 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1356 wp->w_height = wp->w_minheight;
1357 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1358 wp->w_height = wp->w_maxheight;
1359 if (wp->w_height > Rows - wp->w_winrow)
1360 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001361
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001362 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001363 {
1364 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1365 if (wp->w_winrow < 0)
1366 wp->w_winrow = 0;
1367 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001368 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1369 || wp->w_popup_pos == POPPOS_BOTLEFT)
1370 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001371 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001372 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001373 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001374 else
1375 // not enough space, make top aligned
Bram Moolenaar12034e22019-08-25 22:25:02 +02001376 wp->w_winrow = wantline + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001377 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001378 if (wp->w_winrow >= Rows)
1379 wp->w_winrow = Rows - 1;
1380 else if (wp->w_winrow < 0)
1381 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001382
Bram Moolenaar17146962019-05-30 00:12:11 +02001383 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001384 if (win_valid(wp->w_popup_prop_win))
1385 {
1386 wp->w_popup_prop_changedtick =
1387 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1388 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1389 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001390
1391 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001392 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001393 if (org_winrow != wp->w_winrow
1394 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001395 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001396 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001397 || org_width != wp->w_width
1398 || org_height != wp->w_height)
1399 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001400 redraw_win_later(wp, NOT_VALID);
1401 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1402 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001403 popup_mask_refresh = TRUE;
1404 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001405}
1406
Bram Moolenaar17627312019-06-02 19:53:44 +02001407typedef enum
1408{
1409 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001410 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001411 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001412 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001413 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001414 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001415 TYPE_PREVIEW, // preview window
1416 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001417} create_type_T;
1418
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001419/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001420 * Make "buf" empty and set the contents to "text".
1421 * Used by popup_create() and popup_settext().
1422 */
1423 static void
1424popup_set_buffer_text(buf_T *buf, typval_T text)
1425{
1426 int lnum;
1427
1428 // Clear the buffer, then replace the lines.
1429 curbuf = buf;
1430 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1431 ml_delete(lnum, FALSE);
1432 curbuf = curwin->w_buffer;
1433
1434 // Add text to the buffer.
1435 if (text.v_type == VAR_STRING)
1436 {
1437 // just a string
1438 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1439 }
1440 else
1441 {
1442 list_T *l = text.vval.v_list;
1443
1444 if (l->lv_len > 0)
1445 {
1446 if (l->lv_first->li_tv.v_type == VAR_STRING)
1447 // list of strings
1448 add_popup_strings(buf, l);
1449 else
1450 // list of dictionaries
1451 add_popup_dicts(buf, l);
1452 }
1453 }
1454
1455 // delete the line that was in the empty buffer
1456 curbuf = buf;
1457 ml_delete(buf->b_ml.ml_line_count, FALSE);
1458 curbuf = curwin->w_buffer;
1459}
1460
1461/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001462 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1463 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001464 * Return FAIL if the parsing fails.
1465 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001466 static int
1467parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001468{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001469 char_u *p =
1470#ifdef FEAT_QUICKFIX
1471 !is_preview ? p_cpp :
1472#endif
1473 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001474
Bram Moolenaar258cef52019-08-21 17:29:29 +02001475 if (wp != NULL)
1476 wp->w_popup_flags &= ~POPF_INFO_MENU;
1477
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001478 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001479 {
1480 char_u *e, *dig;
1481 char_u *s = p;
1482 int x;
1483
1484 e = vim_strchr(p, ':');
1485 if (e == NULL || e[1] == NUL)
1486 return FAIL;
1487
1488 p = vim_strchr(e, ',');
1489 if (p == NULL)
1490 p = e + STRLEN(e);
1491 dig = e + 1;
1492 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001493
1494 if (STRNCMP(s, "height:", 7) == 0)
1495 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001496 if (dig != p)
1497 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001498 if (wp != NULL)
1499 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001500 if (is_preview)
1501 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001502 wp->w_maxheight = x;
1503 }
1504 }
1505 else if (STRNCMP(s, "width:", 6) == 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_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001513 wp->w_maxwidth = x;
1514 }
1515 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001516 else if (STRNCMP(s, "highlight:", 10) == 0)
1517 {
1518 if (wp != NULL)
1519 {
1520 int c = *p;
1521
1522 *p = NUL;
1523 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1524 s + 10, OPT_FREE|OPT_LOCAL, 0);
1525 *p = c;
1526 }
1527 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001528 else if (STRNCMP(s, "border:", 7) == 0)
1529 {
1530 char_u *arg = s + 7;
1531 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1532 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1533 int i;
1534
1535 if (!on && !off)
1536 return FAIL;
1537 if (wp != NULL)
1538 {
1539 for (i = 0; i < 4; ++i)
1540 wp->w_popup_border[i] = on ? 1 : 0;
1541 if (off)
1542 // only show the X for close when there is a border
1543 wp->w_popup_close = POPCLOSE_NONE;
1544 }
1545 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001546 else if (STRNCMP(s, "align:", 6) == 0)
1547 {
1548 char_u *arg = s + 6;
1549 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1550 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1551
1552 if (!menu && !item)
1553 return FAIL;
1554 if (wp != NULL && menu)
1555 wp->w_popup_flags |= POPF_INFO_MENU;
1556 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001557 else
1558 return FAIL;
1559 }
1560 return OK;
1561}
1562
1563/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001564 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1565 * is not NULL.
1566 * Return FAIL if the parsing fails.
1567 */
1568 int
1569parse_previewpopup(win_T *wp)
1570{
1571 return parse_popup_option(wp, TRUE);
1572}
1573
1574/*
1575 * Parse the 'completepopup' 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_completepopup(win_T *wp)
1581{
1582 return parse_popup_option(wp, FALSE);
1583}
1584
1585/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001586 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001587 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001588 */
1589 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001590popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001591{
1592 setcursor_mayforce(TRUE);
1593 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1594 if (wp->w_wantline == 0) // cursor in first line
1595 {
1596 wp->w_wantline = 2;
1597 wp->w_popup_pos = POPPOS_TOPLEFT;
1598 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001599
Bram Moolenaar79648732019-07-18 21:43:07 +02001600 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001601 if (wp->w_wantcol > Columns - width)
1602 {
1603 wp->w_wantcol = Columns - width;
1604 if (wp->w_wantcol < 1)
1605 wp->w_wantcol = 1;
1606 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001607 popup_adjust_position(wp);
1608}
1609
1610/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001611 * Set w_wantline and w_wantcol for the a given screen position.
1612 * Caller must take care of running into the window border.
1613 */
1614 void
1615popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1616{
1617 wp->w_wantline = row;
1618 wp->w_wantcol = col;
1619 popup_adjust_position(wp);
1620}
1621
1622/*
1623 * Add a border and lef&right padding.
1624 */
1625 static void
1626add_border_left_right_padding(win_T *wp)
1627{
1628 int i;
1629
1630 for (i = 0; i < 4; ++i)
1631 {
1632 wp->w_popup_border[i] = 1;
1633 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1634 }
1635}
1636
1637/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001638 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001639 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001640 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001641 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001642 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001643 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001644popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001645{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001646 win_T *wp;
1647 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001648 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001649 int new_buffer;
1650 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001651 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001652 int nr;
1653 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001654
Bram Moolenaar79648732019-07-18 21:43:07 +02001655 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001656 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001657 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001658 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001659 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001660 buf = buflist_findnr( argvars[0].vval.v_number);
1661 if (buf == NULL)
1662 {
1663 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1664 return NULL;
1665 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001666#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001667 if (buf->b_term != NULL)
1668 {
1669 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1670 return NULL;
1671 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001672#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001673 }
1674 else if (!(argvars[0].v_type == VAR_STRING
1675 && argvars[0].vval.v_string != NULL)
1676 && !(argvars[0].v_type == VAR_LIST
1677 && argvars[0].vval.v_list != NULL))
1678 {
1679 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001680 return NULL;
1681 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001682 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001683 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001684 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001685 return NULL;
1686 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001687 d = argvars[1].vval.v_dict;
1688 }
1689
1690 if (d != NULL)
1691 {
1692 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1693 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1694 else if (type == TYPE_NOTIFICATION)
1695 tabnr = -1; // notifications are global by default
1696 else
1697 tabnr = 0;
1698 if (tabnr > 0)
1699 {
1700 tp = find_tabpage(tabnr);
1701 if (tp == NULL)
1702 {
1703 semsg(_("E997: Tabpage not found: %d"), tabnr);
1704 return NULL;
1705 }
1706 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001707 }
1708
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001709 // Create the window and buffer.
1710 wp = win_alloc_popup_win();
1711 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001712 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001713 if (rettv != NULL)
1714 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001715 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001716 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001717
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001718 if (buf != NULL)
1719 {
1720 // use existing buffer
1721 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001722 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001723 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001724 buffer_ensure_loaded(buf);
1725 }
1726 else
1727 {
1728 // create a new buffer associated with the popup
1729 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001730 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001731 if (buf == NULL)
1732 return NULL;
1733 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001734
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001735 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001736
Bram Moolenaar46451042019-08-24 15:50:46 +02001737 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001738 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001739 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001740 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001741 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001742 buf->b_p_ul = -1; // no undo
1743 buf->b_p_swf = FALSE; // no swap file
1744 buf->b_p_bl = FALSE; // unlisted buffer
1745 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001746
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001747 // Avoid that 'buftype' is reset when this buffer is entered.
1748 buf->b_p_initialized = TRUE;
1749 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001750 wp->w_p_wrap = TRUE; // 'wrap' is default on
1751 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001752
Bram Moolenaara3fce622019-06-20 02:31:49 +02001753 if (tp != NULL)
1754 {
1755 // popup on specified tab page
1756 wp->w_next = tp->tp_first_popupwin;
1757 tp->tp_first_popupwin = wp;
1758 }
1759 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001760 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001761 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001762 wp->w_next = curtab->tp_first_popupwin;
1763 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001764 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001765 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001766 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001767 win_T *prev = first_popupwin;
1768
1769 // Global popup: add at the end, so that it gets displayed on top of
1770 // older ones with the same zindex. Matters for notifications.
1771 if (first_popupwin == NULL)
1772 first_popupwin = wp;
1773 else
1774 {
1775 while (prev->w_next != NULL)
1776 prev = prev->w_next;
1777 prev->w_next = wp;
1778 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001779 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001780
Bram Moolenaar79648732019-07-18 21:43:07 +02001781 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001782 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001783
Bram Moolenaar79648732019-07-18 21:43:07 +02001784 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001785 {
1786 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001787 }
1788 if (type == TYPE_ATCURSOR)
1789 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001790 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001791 set_moved_values(wp);
1792 set_moved_columns(wp, FIND_STRING);
1793 }
1794
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001795 if (type == TYPE_BEVAL)
1796 {
1797 wp->w_popup_pos = POPPOS_BOTLEFT;
1798
1799 // by default use the mouse position
1800 wp->w_wantline = mouse_row;
1801 if (wp->w_wantline <= 0) // mouse on first line
1802 {
1803 wp->w_wantline = 2;
1804 wp->w_popup_pos = POPPOS_TOPLEFT;
1805 }
1806 wp->w_wantcol = mouse_col + 1;
1807 set_mousemoved_values(wp);
1808 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1809 }
1810
Bram Moolenaar33796b32019-06-08 16:01:13 +02001811 // set default values
1812 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001813 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001814
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001815 if (type == TYPE_NOTIFICATION)
1816 {
1817 win_T *twp, *nextwin;
1818 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001819
1820 // Try to not overlap with another global popup. Guess we need 3
1821 // more screen lines than buffer lines.
1822 wp->w_wantline = 1;
1823 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1824 {
1825 nextwin = twp->w_next;
1826 if (twp != wp
1827 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1828 && twp->w_winrow <= wp->w_wantline - 1 + height
1829 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1830 {
1831 // move to below this popup and restart the loop to check for
1832 // overlap with other popups
1833 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1834 nextwin = first_popupwin;
1835 }
1836 }
1837 if (wp->w_wantline + height > Rows)
1838 {
1839 // can't avoid overlap, put on top in the hope that message goes
1840 // away soon.
1841 wp->w_wantline = 1;
1842 }
1843
1844 wp->w_wantcol = 10;
1845 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001846 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001847 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001848 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001849 for (i = 0; i < 4; ++i)
1850 wp->w_popup_border[i] = 1;
1851 wp->w_popup_padding[1] = 1;
1852 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001853
1854 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001855 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001856 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1857 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001858 }
1859
Bram Moolenaara730e552019-06-16 19:05:31 +02001860 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001861 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001862 wp->w_popup_pos = POPPOS_CENTER;
1863 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001864 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001865 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001866 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001867 }
1868
Bram Moolenaara730e552019-06-16 19:05:31 +02001869 if (type == TYPE_MENU)
1870 {
1871 typval_T tv;
1872 callback_T callback;
1873
1874 tv.v_type = VAR_STRING;
1875 tv.vval.v_string = (char_u *)"popup_filter_menu";
1876 callback = get_callback(&tv);
1877 if (callback.cb_name != NULL)
1878 set_callback(&wp->w_filter_cb, &callback);
1879
1880 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001881 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001882 }
1883
Bram Moolenaar79648732019-07-18 21:43:07 +02001884 if (type == TYPE_PREVIEW)
1885 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001886 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001887 wp->w_popup_close = POPCLOSE_BUTTON;
1888 for (i = 0; i < 4; ++i)
1889 wp->w_popup_border[i] = 1;
1890 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001891 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1892 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001893# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001894 if (type == TYPE_INFO)
1895 {
1896 wp->w_popup_pos = POPPOS_TOPLEFT;
1897 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1898 wp->w_popup_close = POPCLOSE_BUTTON;
1899 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001900 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001901 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001902# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001903
Bram Moolenaarae943152019-06-16 22:54:14 +02001904 for (i = 0; i < 4; ++i)
1905 VIM_CLEAR(wp->w_border_highlight[i]);
1906 for (i = 0; i < 8; ++i)
1907 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001908 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001909 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001910 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001911
Bram Moolenaar79648732019-07-18 21:43:07 +02001912 if (d != NULL)
1913 // Deal with options.
1914 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001915
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001916#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001917 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1918 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001919#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001920
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001921 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001922
1923 wp->w_vsep_width = 0;
1924
1925 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001926 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001927
1928 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001929}
1930
1931/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001932 * popup_clear()
1933 */
1934 void
1935f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1936{
1937 close_all_popups();
1938}
1939
1940/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001941 * popup_create({text}, {options})
1942 */
1943 void
1944f_popup_create(typval_T *argvars, typval_T *rettv)
1945{
Bram Moolenaar17627312019-06-02 19:53:44 +02001946 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001947}
1948
1949/*
1950 * popup_atcursor({text}, {options})
1951 */
1952 void
1953f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1954{
Bram Moolenaar17627312019-06-02 19:53:44 +02001955 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001956}
1957
1958/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001959 * popup_beval({text}, {options})
1960 */
1961 void
1962f_popup_beval(typval_T *argvars, typval_T *rettv)
1963{
1964 popup_create(argvars, rettv, TYPE_BEVAL);
1965}
1966
1967/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001968 * Invoke the close callback for window "wp" with value "result".
1969 * Careful: The callback may make "wp" invalid!
1970 */
1971 static void
1972invoke_popup_callback(win_T *wp, typval_T *result)
1973{
1974 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001975 typval_T argv[3];
1976
1977 argv[0].v_type = VAR_NUMBER;
1978 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1979
1980 if (result != NULL && result->v_type != VAR_UNKNOWN)
1981 copy_tv(result, &argv[1]);
1982 else
1983 {
1984 argv[1].v_type = VAR_NUMBER;
1985 argv[1].vval.v_number = 0;
1986 }
1987
1988 argv[2].v_type = VAR_UNKNOWN;
1989
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001990 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001991 if (result != NULL)
1992 clear_tv(&argv[1]);
1993 clear_tv(&rettv);
1994}
1995
1996/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001997 * Close popup "wp" and invoke any close callback for it.
1998 */
1999 static void
2000popup_close_and_callback(win_T *wp, typval_T *arg)
2001{
2002 int id = wp->w_id;
2003
2004 if (wp->w_close_cb.cb_name != NULL)
2005 // Careful: This may make "wp" invalid.
2006 invoke_popup_callback(wp, arg);
2007
2008 popup_close(id);
2009}
2010
Bram Moolenaar12034e22019-08-25 22:25:02 +02002011 static void
2012popup_close_with_retval(win_T *wp, int retval)
2013{
2014 typval_T res;
2015
2016 res.v_type = VAR_NUMBER;
2017 res.vval.v_number = retval;
2018 popup_close_and_callback(wp, &res);
2019}
2020
Bram Moolenaar3397f742019-06-02 18:40:06 +02002021/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002022 * Close popup "wp" because of a mouse click.
2023 */
2024 void
2025popup_close_for_mouse_click(win_T *wp)
2026{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002027 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002028}
2029
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002030 static void
2031check_mouse_moved(win_T *wp, win_T *mouse_wp)
2032{
2033 // Close the popup when all if these are true:
2034 // - the mouse is not on this popup
2035 // - "mousemoved" was used
2036 // - the mouse is no longer on the same screen row or the mouse column is
2037 // outside of the relevant text
2038 if (wp != mouse_wp
2039 && wp->w_popup_mouse_row != 0
2040 && (wp->w_popup_mouse_row != mouse_row
2041 || mouse_col < wp->w_popup_mouse_mincol
2042 || mouse_col > wp->w_popup_mouse_maxcol))
2043 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002044 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002045 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002046 }
2047}
2048
2049/*
2050 * Called when the mouse moved: may close a popup with "mousemoved".
2051 */
2052 void
2053popup_handle_mouse_moved(void)
2054{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002055 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002056 win_T *mouse_wp;
2057 int row = mouse_row;
2058 int col = mouse_col;
2059
2060 // find the window where the mouse is in
2061 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2062
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002063 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2064 {
2065 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002066 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002067 }
2068 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2069 {
2070 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002071 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002072 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002073}
2074
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002075/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002076 * In a filter: check if the typed key is a mouse event that is used for
2077 * dragging the popup.
2078 */
2079 static void
2080filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2081{
2082 int row = mouse_row;
2083 int col = mouse_col;
2084
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002085 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002086 && is_mouse_key(c)
2087 && (wp == popup_dragwin
2088 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2089 // do not consume the key, allow for dragging the popup
2090 rettv->vval.v_number = 0;
2091}
2092
Bram Moolenaara730e552019-06-16 19:05:31 +02002093/*
2094 * popup_filter_menu({text}, {options})
2095 */
2096 void
2097f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2098{
2099 int id = tv_get_number(&argvars[0]);
2100 win_T *wp = win_id2wp(id);
2101 char_u *key = tv_get_string(&argvars[1]);
2102 typval_T res;
2103 int c;
2104 linenr_T old_lnum;
2105
2106 // If the popup has been closed do not consume the key.
2107 if (wp == NULL)
2108 return;
2109
2110 c = *key;
2111 if (c == K_SPECIAL && key[1] != NUL)
2112 c = TO_SPECIAL(key[1], key[2]);
2113
2114 // consume all keys until done
2115 rettv->vval.v_number = 1;
2116 res.v_type = VAR_NUMBER;
2117
2118 old_lnum = wp->w_cursor.lnum;
2119 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2120 --wp->w_cursor.lnum;
2121 if ((c == 'j' || c == 'J' || c == K_DOWN)
2122 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2123 ++wp->w_cursor.lnum;
2124 if (old_lnum != wp->w_cursor.lnum)
2125 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002126 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002127 return;
2128 }
2129
2130 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2131 {
2132 // Cancelled, invoke callback with -1
2133 res.vval.v_number = -1;
2134 popup_close_and_callback(wp, &res);
2135 return;
2136 }
2137 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2138 {
2139 // Invoke callback with current index.
2140 res.vval.v_number = wp->w_cursor.lnum;
2141 popup_close_and_callback(wp, &res);
2142 return;
2143 }
2144
2145 filter_handle_drag(wp, c, rettv);
2146}
2147
2148/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002149 * popup_filter_yesno({text}, {options})
2150 */
2151 void
2152f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2153{
2154 int id = tv_get_number(&argvars[0]);
2155 win_T *wp = win_id2wp(id);
2156 char_u *key = tv_get_string(&argvars[1]);
2157 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002158 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002159
2160 // If the popup has been closed don't consume the key.
2161 if (wp == NULL)
2162 return;
2163
Bram Moolenaara730e552019-06-16 19:05:31 +02002164 c = *key;
2165 if (c == K_SPECIAL && key[1] != NUL)
2166 c = TO_SPECIAL(key[1], key[2]);
2167
Bram Moolenaara42d9452019-06-15 21:46:30 +02002168 // consume all keys until done
2169 rettv->vval.v_number = 1;
2170
Bram Moolenaara730e552019-06-16 19:05:31 +02002171 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002172 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002173 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002174 res.vval.v_number = 0;
2175 else
2176 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002177 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002178 return;
2179 }
2180
2181 // Invoke callback
2182 res.v_type = VAR_NUMBER;
2183 popup_close_and_callback(wp, &res);
2184}
2185
2186/*
2187 * popup_dialog({text}, {options})
2188 */
2189 void
2190f_popup_dialog(typval_T *argvars, typval_T *rettv)
2191{
2192 popup_create(argvars, rettv, TYPE_DIALOG);
2193}
2194
2195/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002196 * popup_menu({text}, {options})
2197 */
2198 void
2199f_popup_menu(typval_T *argvars, typval_T *rettv)
2200{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002201 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002202}
2203
2204/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002205 * popup_notification({text}, {options})
2206 */
2207 void
2208f_popup_notification(typval_T *argvars, typval_T *rettv)
2209{
2210 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2211}
2212
2213/*
2214 * Find the popup window with window-ID "id".
2215 * If the popup window does not exist NULL is returned.
2216 * If the window is not a popup window, and error message is given.
2217 */
2218 static win_T *
2219find_popup_win(int id)
2220{
2221 win_T *wp = win_id2wp(id);
2222
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002223 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002224 {
2225 semsg(_("E993: window %d is not a popup window"), id);
2226 return NULL;
2227 }
2228 return wp;
2229}
2230
2231/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002232 * popup_close({id})
2233 */
2234 void
2235f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2236{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002237 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002238 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002239
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002240 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002241 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002242}
2243
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002244 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002245popup_hide(win_T *wp)
2246{
2247 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2248 {
2249 wp->w_popup_flags |= POPF_HIDDEN;
2250 --wp->w_buffer->b_nwindows;
2251 redraw_all_later(NOT_VALID);
2252 popup_mask_refresh = TRUE;
2253 }
2254}
2255
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002256/*
2257 * popup_hide({id})
2258 */
2259 void
2260f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2261{
2262 int id = (int)tv_get_number(argvars);
2263 win_T *wp = find_popup_win(id);
2264
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002265 if (wp != NULL)
2266 popup_hide(wp);
2267}
2268
2269 void
2270popup_show(win_T *wp)
2271{
2272 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002273 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002274 wp->w_popup_flags &= ~POPF_HIDDEN;
2275 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002276 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002277 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002278 }
2279}
2280
2281/*
2282 * popup_show({id})
2283 */
2284 void
2285f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2286{
2287 int id = (int)tv_get_number(argvars);
2288 win_T *wp = find_popup_win(id);
2289
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002290 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002291 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002292 popup_show(wp);
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002293 if (wp->w_popup_flags & POPF_INFO)
2294 pum_position_info_popup(wp);
2295 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002296}
2297
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002298/*
2299 * popup_settext({id}, {text})
2300 */
2301 void
2302f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2303{
2304 int id = (int)tv_get_number(&argvars[0]);
2305 win_T *wp = find_popup_win(id);
2306
2307 if (wp != NULL)
2308 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002309 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2310 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2311 else
2312 {
2313 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002314 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002315 popup_adjust_position(wp);
2316 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002317 }
2318}
2319
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002320 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002321popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002322{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002323 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002324 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002325 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002326 clear_cmdline = TRUE;
2327 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002328
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002329 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002330 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002331}
2332
Bram Moolenaarec583842019-05-26 14:11:23 +02002333/*
2334 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002335 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002336 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002337 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002338popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002339{
2340 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002341 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002342 win_T *prev = NULL;
2343
Bram Moolenaarec583842019-05-26 14:11:23 +02002344 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002345 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002346 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002347 {
2348 if (prev == NULL)
2349 first_popupwin = wp->w_next;
2350 else
2351 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002352 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002353 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002354 }
2355
Bram Moolenaarec583842019-05-26 14:11:23 +02002356 // go through tab-local popups
2357 FOR_ALL_TABPAGES(tp)
2358 popup_close_tabpage(tp, id);
2359}
2360
2361/*
2362 * Close a popup window with Window-id "id" in tabpage "tp".
2363 */
2364 void
2365popup_close_tabpage(tabpage_T *tp, int id)
2366{
2367 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002368 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002369 win_T *prev = NULL;
2370
Bram Moolenaarec583842019-05-26 14:11:23 +02002371 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2372 if (wp->w_id == id)
2373 {
2374 if (prev == NULL)
2375 *root = wp->w_next;
2376 else
2377 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002378 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002379 return;
2380 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002381}
2382
2383 void
2384close_all_popups(void)
2385{
2386 while (first_popupwin != NULL)
2387 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002388 while (curtab->tp_first_popupwin != NULL)
2389 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002390}
2391
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002392/*
2393 * popup_move({id}, {options})
2394 */
2395 void
2396f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2397{
Bram Moolenaarae943152019-06-16 22:54:14 +02002398 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002399 int id = (int)tv_get_number(argvars);
2400 win_T *wp = find_popup_win(id);
2401
2402 if (wp == NULL)
2403 return; // invalid {id}
2404
2405 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2406 {
2407 emsg(_(e_dictreq));
2408 return;
2409 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002410 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002411
Bram Moolenaarae943152019-06-16 22:54:14 +02002412 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002413
2414 if (wp->w_winrow + wp->w_height >= cmdline_row)
2415 clear_cmdline = TRUE;
2416 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002417}
2418
Bram Moolenaarbc133542019-05-29 20:26:46 +02002419/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002420 * popup_setoptions({id}, {options})
2421 */
2422 void
2423f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2424{
2425 dict_T *dict;
2426 int id = (int)tv_get_number(argvars);
2427 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002428 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002429
2430 if (wp == NULL)
2431 return; // invalid {id}
2432
2433 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2434 {
2435 emsg(_(e_dictreq));
2436 return;
2437 }
2438 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002439 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002440
2441 apply_move_options(wp, dict);
2442 apply_general_options(wp, dict);
2443
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002444 if (old_firstline != wp->w_firstline)
2445 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002446 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002447 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002448 popup_adjust_position(wp);
2449}
2450
2451/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002452 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002453 */
2454 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002455f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002456{
2457 dict_T *dict;
2458 int id = (int)tv_get_number(argvars);
2459 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002460 int top_extra;
2461 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002462
2463 if (rettv_dict_alloc(rettv) == OK)
2464 {
2465 if (wp == NULL)
2466 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002467 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002468 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2469
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002470 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002471 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002472 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002473
Bram Moolenaarbc133542019-05-29 20:26:46 +02002474 dict_add_number(dict, "line", wp->w_winrow + 1);
2475 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002476 dict_add_number(dict, "width", wp->w_width + left_extra
2477 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2478 dict_add_number(dict, "height", wp->w_height + top_extra
2479 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002480
2481 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2482 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2483 dict_add_number(dict, "core_width", wp->w_width);
2484 dict_add_number(dict, "core_height", wp->w_height);
2485
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002486 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002487 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002488 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002489 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002490
2491 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002492 }
2493}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002494/*
2495 * popup_locate({row}, {col})
2496 */
2497 void
2498f_popup_locate(typval_T *argvars, typval_T *rettv)
2499{
2500 int row = tv_get_number(&argvars[0]) - 1;
2501 int col = tv_get_number(&argvars[1]) - 1;
2502 win_T *wp;
2503
2504 wp = mouse_find_win(&row, &col, FIND_POPUP);
2505 if (WIN_IS_POPUP(wp))
2506 rettv->vval.v_number = wp->w_id;
2507}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002508
2509/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002510 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2511 */
2512 static void
2513get_padding_border(dict_T *dict, int *array, char *name)
2514{
2515 list_T *list;
2516 int i;
2517
2518 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2519 return;
2520
2521 list = list_alloc();
2522 if (list != NULL)
2523 {
2524 dict_add_list(dict, name, list);
2525 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2526 for (i = 0; i < 4; ++i)
2527 list_append_number(list, array[i]);
2528 }
2529}
2530
2531/*
2532 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2533 */
2534 static void
2535get_borderhighlight(dict_T *dict, win_T *wp)
2536{
2537 list_T *list;
2538 int i;
2539
2540 for (i = 0; i < 4; ++i)
2541 if (wp->w_border_highlight[i] != NULL)
2542 break;
2543 if (i == 4)
2544 return;
2545
2546 list = list_alloc();
2547 if (list != NULL)
2548 {
2549 dict_add_list(dict, "borderhighlight", list);
2550 for (i = 0; i < 4; ++i)
2551 list_append_string(list, wp->w_border_highlight[i], -1);
2552 }
2553}
2554
2555/*
2556 * For popup_getoptions(): add a "borderchars" entry to "dict".
2557 */
2558 static void
2559get_borderchars(dict_T *dict, win_T *wp)
2560{
2561 list_T *list;
2562 int i;
2563 char_u buf[NUMBUFLEN];
2564 int len;
2565
2566 for (i = 0; i < 8; ++i)
2567 if (wp->w_border_char[i] != 0)
2568 break;
2569 if (i == 8)
2570 return;
2571
2572 list = list_alloc();
2573 if (list != NULL)
2574 {
2575 dict_add_list(dict, "borderchars", list);
2576 for (i = 0; i < 8; ++i)
2577 {
2578 len = mb_char2bytes(wp->w_border_char[i], buf);
2579 list_append_string(list, buf, len);
2580 }
2581 }
2582}
2583
2584/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002585 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002586 */
2587 static void
2588get_moved_list(dict_T *dict, win_T *wp)
2589{
2590 list_T *list;
2591
2592 list = list_alloc();
2593 if (list != NULL)
2594 {
2595 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002596 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002597 list_append_number(list, wp->w_popup_mincol);
2598 list_append_number(list, wp->w_popup_maxcol);
2599 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002600 list = list_alloc();
2601 if (list != NULL)
2602 {
2603 dict_add_list(dict, "mousemoved", list);
2604 list_append_number(list, wp->w_popup_mouse_row);
2605 list_append_number(list, wp->w_popup_mouse_mincol);
2606 list_append_number(list, wp->w_popup_mouse_maxcol);
2607 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002608}
2609
2610/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002611 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002612 */
2613 void
2614f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2615{
2616 dict_T *dict;
2617 int id = (int)tv_get_number(argvars);
2618 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002619 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002620 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002621
2622 if (rettv_dict_alloc(rettv) == OK)
2623 {
2624 if (wp == NULL)
2625 return;
2626
2627 dict = rettv->vval.v_dict;
2628 dict_add_number(dict, "line", wp->w_wantline);
2629 dict_add_number(dict, "col", wp->w_wantcol);
2630 dict_add_number(dict, "minwidth", wp->w_minwidth);
2631 dict_add_number(dict, "minheight", wp->w_minheight);
2632 dict_add_number(dict, "maxheight", wp->w_maxheight);
2633 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002634 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002635 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002636 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002637 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002638 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2639 {
2640 proptype_T *pt = text_prop_type_by_id(
2641 wp->w_popup_prop_win->w_buffer,
2642 wp->w_popup_prop_type);
2643
2644 if (pt != NULL)
2645 dict_add_string(dict, "textprop", pt->pt_name);
2646 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2647 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2648 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002649 dict_add_string(dict, "title", wp->w_popup_title);
2650 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002651 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002652 dict_add_number(dict, "mapping",
2653 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002654 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002655 dict_add_number(dict, "cursorline",
2656 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002657 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002658 if (wp->w_scrollbar_highlight != NULL)
2659 dict_add_string(dict, "scrollbarhighlight",
2660 wp->w_scrollbar_highlight);
2661 if (wp->w_thumb_highlight != NULL)
2662 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002663
Bram Moolenaara3fce622019-06-20 02:31:49 +02002664 // find the tabpage that holds this popup
2665 i = 1;
2666 FOR_ALL_TABPAGES(tp)
2667 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002668 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002669
Bram Moolenaar1824f452019-10-02 23:06:46 +02002670 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2671 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002672 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002673 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002674 break;
2675 ++i;
2676 }
2677 if (tp == NULL)
2678 i = -1; // must be global
2679 else if (tp == curtab)
2680 i = 0;
2681 dict_add_number(dict, "tabpage", i);
2682
Bram Moolenaarae943152019-06-16 22:54:14 +02002683 get_padding_border(dict, wp->w_popup_padding, "padding");
2684 get_padding_border(dict, wp->w_popup_border, "border");
2685 get_borderhighlight(dict, wp);
2686 get_borderchars(dict, wp);
2687 get_moved_list(dict, wp);
2688
2689 if (wp->w_filter_cb.cb_name != NULL)
2690 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2691 if (wp->w_close_cb.cb_name != NULL)
2692 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002693
2694 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2695 ++i)
2696 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2697 {
2698 dict_add_string(dict, "pos",
2699 (char_u *)poppos_entries[i].pp_name);
2700 break;
2701 }
2702
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002703 dict_add_string(dict, "close", (char_u *)(
2704 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2705 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2706
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002707# if defined(FEAT_TIMERS)
2708 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2709 ? (long)wp->w_popup_timer->tr_interval : 0L);
2710# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002711 }
2712}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002713
2714 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002715error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002716{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002717 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002718 {
2719 emsg(_("E994: Not allowed in a popup window"));
2720 return TRUE;
2721 }
2722 return FALSE;
2723}
2724
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002725/*
2726 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002727 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002728 */
2729 void
2730popup_reset_handled()
2731{
2732 win_T *wp;
2733
2734 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2735 wp->w_popup_flags &= ~POPF_HANDLED;
2736 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2737 wp->w_popup_flags &= ~POPF_HANDLED;
2738}
2739
2740/*
2741 * Find the next visible popup where POPF_HANDLED is not set.
2742 * Must have called popup_reset_handled() first.
2743 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2744 * popup with the highest zindex.
2745 */
2746 win_T *
2747find_next_popup(int lowest)
2748{
2749 win_T *wp;
2750 win_T *found_wp;
2751 int found_zindex;
2752
2753 found_zindex = lowest ? INT_MAX : 0;
2754 found_wp = NULL;
2755 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2756 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2757 && (lowest ? wp->w_zindex < found_zindex
2758 : wp->w_zindex > found_zindex))
2759 {
2760 found_zindex = wp->w_zindex;
2761 found_wp = wp;
2762 }
2763 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2764 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2765 && (lowest ? wp->w_zindex < found_zindex
2766 : wp->w_zindex > found_zindex))
2767 {
2768 found_zindex = wp->w_zindex;
2769 found_wp = wp;
2770 }
2771
2772 if (found_wp != NULL)
2773 found_wp->w_popup_flags |= POPF_HANDLED;
2774 return found_wp;
2775}
2776
2777/*
2778 * Invoke the filter callback for window "wp" with typed character "c".
2779 * Uses the global "mod_mask" for modifiers.
2780 * Returns the return value of the filter.
2781 * Careful: The filter may make "wp" invalid!
2782 */
2783 static int
2784invoke_popup_filter(win_T *wp, int c)
2785{
2786 int res;
2787 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002788 typval_T argv[3];
2789 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002790 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002791
Bram Moolenaara42d9452019-06-15 21:46:30 +02002792 // Emergency exit: CTRL-C closes the popup.
2793 if (c == Ctrl_C)
2794 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002795 int save_got_int = got_int;
2796
2797 // Reset got_int to avoid the callback isn't called.
2798 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002799 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002800 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002801 return 1;
2802 }
2803
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002804 argv[0].v_type = VAR_NUMBER;
2805 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2806
2807 // Convert the number to a string, so that the function can use:
2808 // if a:c == "\<F2>"
2809 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2810 argv[1].v_type = VAR_STRING;
2811 argv[1].vval.v_string = vim_strsave(buf);
2812
2813 argv[2].v_type = VAR_UNKNOWN;
2814
Bram Moolenaara42d9452019-06-15 21:46:30 +02002815 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002816 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002817 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002818 popup_highlight_curline(wp);
2819
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002820 res = tv_get_number(&rettv);
2821 vim_free(argv[1].vval.v_string);
2822 clear_tv(&rettv);
2823 return res;
2824}
2825
2826/*
2827 * Called when "c" was typed: invoke popup filter callbacks.
2828 * Returns TRUE when the character was consumed,
2829 */
2830 int
2831popup_do_filter(int c)
2832{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002833 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002834 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002835 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002836 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002837 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002838 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002839
2840 if (recursive)
2841 return FALSE;
2842 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002843
2844 popup_reset_handled();
2845
Bram Moolenaarf6396232019-08-24 19:36:00 +02002846 if (c == K_LEFTMOUSE)
2847 {
2848 int row = mouse_row;
2849 int col = mouse_col;
2850
2851 wp = mouse_find_win(&row, &col, FIND_POPUP);
2852 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002853 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002854 }
2855
Bram Moolenaar581ba392019-09-03 22:08:33 +02002856 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002857 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002858 if (wp->w_filter_cb.cb_name != NULL
2859 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002860 res = invoke_popup_filter(wp, c);
2861
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002862 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002863 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002864 recursive = FALSE;
2865 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002866 return res;
2867}
2868
Bram Moolenaar3397f742019-06-02 18:40:06 +02002869/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002870 * Return TRUE if there is a popup visible with a filter callback and the
2871 * "mapping" property off.
2872 */
2873 int
2874popup_no_mapping(void)
2875{
2876 int round;
2877 win_T *wp;
2878
2879 for (round = 1; round <= 2; ++round)
2880 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2881 wp != NULL; wp = wp->w_next)
2882 if (wp->w_filter_cb.cb_name != NULL
2883 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2884 return TRUE;
2885 return FALSE;
2886}
2887
2888/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002889 * Called when the cursor moved: check if any popup needs to be closed if the
2890 * cursor moved far enough.
2891 */
2892 void
2893popup_check_cursor_pos()
2894{
2895 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002896
2897 popup_reset_handled();
2898 while ((wp = find_next_popup(TRUE)) != NULL)
2899 if (wp->w_popup_curwin != NULL
2900 && (curwin != wp->w_popup_curwin
2901 || curwin->w_cursor.lnum != wp->w_popup_lnum
2902 || curwin->w_cursor.col < wp->w_popup_mincol
2903 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002904 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002905}
2906
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002907/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002908 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002909 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002910 static void
2911popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002912{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002913 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002914 char_u *cells;
2915 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002916
2917 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002918 return;
2919 if (wp->w_popup_mask_cells != NULL
2920 && wp->w_popup_mask_height == height
2921 && wp->w_popup_mask_width == width)
2922 return; // cache is still valid
2923
2924 vim_free(wp->w_popup_mask_cells);
2925 wp->w_popup_mask_cells = alloc_clear(width * height);
2926 if (wp->w_popup_mask_cells == NULL)
2927 return;
2928 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002929
2930 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2931 {
2932 int cols, cole;
2933 int lines, linee;
2934
2935 li = lio->li_tv.vval.v_list->lv_first;
2936 cols = tv_get_number(&li->li_tv);
2937 if (cols < 0)
2938 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002939 li = li->li_next;
2940 cole = tv_get_number(&li->li_tv);
2941 if (cole < 0)
2942 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002943 li = li->li_next;
2944 lines = tv_get_number(&li->li_tv);
2945 if (lines < 0)
2946 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002947 li = li->li_next;
2948 linee = tv_get_number(&li->li_tv);
2949 if (linee < 0)
2950 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002951
2952 for (row = lines - 1; row < linee && row < height; ++row)
2953 for (col = cols - 1; col < cole && col < width; ++col)
2954 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002955 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002956}
2957
2958/*
2959 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2960 * "col" and "line" are screen coordinates.
2961 */
2962 static int
2963popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2964{
2965 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2966 int line = screenline - wp->w_winrow;
2967
2968 return col >= 0 && col < width
2969 && line >= 0 && line < height
2970 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002971}
2972
2973/*
2974 * Set flags in popup_transparent[] for window "wp" to "val".
2975 */
2976 static void
2977update_popup_transparent(win_T *wp, int val)
2978{
2979 if (wp->w_popup_mask != NULL)
2980 {
2981 int width = popup_width(wp);
2982 int height = popup_height(wp);
2983 listitem_T *lio, *li;
2984 int cols, cole;
2985 int lines, linee;
2986 int col, line;
2987
2988 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2989 {
2990 li = lio->li_tv.vval.v_list->lv_first;
2991 cols = tv_get_number(&li->li_tv);
2992 if (cols < 0)
2993 cols = width + cols + 1;
2994 li = li->li_next;
2995 cole = tv_get_number(&li->li_tv);
2996 if (cole < 0)
2997 cole = width + cole + 1;
2998 li = li->li_next;
2999 lines = tv_get_number(&li->li_tv);
3000 if (lines < 0)
3001 lines = height + lines + 1;
3002 li = li->li_next;
3003 linee = tv_get_number(&li->li_tv);
3004 if (linee < 0)
3005 linee = height + linee + 1;
3006
3007 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003008 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003009 if (cols < 0)
3010 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003011 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003012 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003013 if (lines < 0)
3014 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003015 for (line = lines; line < linee
3016 && line + wp->w_winrow < screen_Rows; ++line)
3017 for (col = cols; col < cole
3018 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003019 popup_transparent[(line + wp->w_winrow) * screen_Columns
3020 + col + wp->w_wincol] = val;
3021 }
3022 }
3023}
3024
3025/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003026 * Only called when popup window "wp" is hidden: If the window is positioned
3027 * next to a text property, and it is now visible, then unhide the popup.
3028 * We don't check if visible popups become hidden, that is done in
3029 * popup_adjust_position().
3030 * Return TRUE if the popup became unhidden.
3031 */
3032 static int
3033check_popup_unhidden(win_T *wp)
3034{
3035 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3036 {
3037 textprop_T prop;
3038 linenr_T lnum;
3039
3040 if (find_visible_prop(wp->w_popup_prop_win,
3041 wp->w_popup_prop_type, wp->w_popup_prop_id,
3042 &prop, &lnum) == OK)
3043 {
3044 wp->w_popup_flags &= ~POPF_HIDDEN;
3045 ++wp->w_buffer->b_nwindows;
3046 wp->w_popup_prop_topline = 0; // force repositioning
3047 return TRUE;
3048 }
3049 }
3050 return FALSE;
3051}
3052
3053/*
3054 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3055 * That is when the buffer in the popup was changed, or the popup is following
3056 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003057 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003058 */
3059 static int
3060popup_need_position_adjust(win_T *wp)
3061{
3062 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3063 return TRUE;
3064 if (win_valid(wp->w_popup_prop_win))
3065 return wp->w_popup_prop_changedtick
3066 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003067 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3068 || ((wp->w_popup_flags & POPF_CURSORLINE)
3069 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003070 return FALSE;
3071}
3072
3073/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003074 * Update "popup_mask" if needed.
3075 * Also recomputes the popup size and positions.
3076 * Also updates "popup_visible".
3077 * Also marks window lines for redrawing.
3078 */
3079 void
3080may_update_popup_mask(int type)
3081{
3082 win_T *wp;
3083 short *mask;
3084 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003085 int redraw_all_popups = FALSE;
3086 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003087
3088 // Need to recompute when switching tabs.
3089 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3090 // (such as the screen size) must have changed.
3091 if (popup_mask_tab != curtab || type >= NOT_VALID)
3092 {
3093 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003094 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003095 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003096
3097 // Check if any popup window buffer has changed and if any popup connected
3098 // to a text property has become visible.
3099 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3100 if (wp->w_popup_flags & POPF_HIDDEN)
3101 popup_mask_refresh |= check_popup_unhidden(wp);
3102 else if (popup_need_position_adjust(wp))
3103 popup_mask_refresh = TRUE;
3104 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3105 if (wp->w_popup_flags & POPF_HIDDEN)
3106 popup_mask_refresh |= check_popup_unhidden(wp);
3107 else if (popup_need_position_adjust(wp))
3108 popup_mask_refresh = TRUE;
3109
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003110 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003111 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003112
3113 // Need to update the mask, something has changed.
3114 popup_mask_refresh = FALSE;
3115 popup_mask_tab = curtab;
3116 popup_visible = FALSE;
3117
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003118 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003119 // If redrawing only what is needed, update "popup_mask_next" and then
3120 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003121 redrawing_all_win = TRUE;
3122 FOR_ALL_WINDOWS(wp)
3123 if (wp->w_redr_type < SOME_VALID)
3124 redrawing_all_win = FALSE;
3125 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003126 mask = popup_mask;
3127 else
3128 mask = popup_mask_next;
3129 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3130
3131 // Find the window with the lowest zindex that hasn't been handled yet,
3132 // so that the window with a higher zindex overwrites the value in
3133 // popup_mask.
3134 popup_reset_handled();
3135 while ((wp = find_next_popup(TRUE)) != NULL)
3136 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003137 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003138 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003139
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003140 popup_visible = TRUE;
3141
Bram Moolenaar12034e22019-08-25 22:25:02 +02003142 // Recompute the position if the text changed. It may make the popup
3143 // hidden if it's attach to a text property that is no longer visible.
3144 if (redraw_all_popups || popup_need_position_adjust(wp))
3145 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003146 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003147 if (wp->w_popup_flags & POPF_HIDDEN)
3148 continue;
3149 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003150
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003151 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003152 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003153 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003154 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003155 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003156 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003157 col < wp->w_wincol + width - wp->w_popup_leftoff
3158 && col < screen_Columns; ++col)
3159 if (wp->w_popup_mask_cells == NULL
3160 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003161 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003162 }
3163
3164 // Only check which lines are to be updated if not already
3165 // updating all lines.
3166 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003167 {
3168 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3169 win_T *prev_wp = NULL;
3170
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003171 for (line = 0; line < screen_Rows; ++line)
3172 {
3173 int col_done = 0;
3174
3175 for (col = 0; col < screen_Columns; ++col)
3176 {
3177 int off = line * screen_Columns + col;
3178
3179 if (popup_mask[off] != popup_mask_next[off])
3180 {
3181 popup_mask[off] = popup_mask_next[off];
3182
3183 if (line >= cmdline_row)
3184 {
3185 // the command line needs to be cleared if text below
3186 // the popup is now visible.
3187 if (!msg_scrolled && popup_mask_next[off] == 0)
3188 clear_cmdline = TRUE;
3189 }
3190 else if (col >= col_done)
3191 {
3192 linenr_T lnum;
3193 int line_cp = line;
3194 int col_cp = col;
3195
3196 // The screen position "line" / "col" needs to be
3197 // redrawn. Figure out what window that is and update
3198 // w_redraw_top and w_redr_bot. Only needs to be done
3199 // once for each window line.
3200 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3201 if (wp != NULL)
3202 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003203 if (wp != prev_wp)
3204 {
3205 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3206 prev_wp = wp;
3207 }
3208
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003209 if (line_cp >= wp->w_height)
3210 // In (or below) status line
3211 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003212 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003213 {
3214 // compute the position in the buffer line from
3215 // the position in the window
3216 mouse_comp_pos(wp, &line_cp, &col_cp,
3217 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003218 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003219 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003220
3221 // This line is going to be redrawn, no need to
3222 // check until the right side of the window.
3223 col_done = wp->w_wincol + wp->w_width - 1;
3224 }
3225 }
3226 }
3227 }
3228 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003229
3230 vim_free(plines_cache);
3231 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003232}
3233
3234/*
3235 * Return a string of "len" spaces in IObuff.
3236 */
3237 static char_u *
3238get_spaces(int len)
3239{
3240 vim_memset(IObuff, ' ', (size_t)len);
3241 IObuff[len] = NUL;
3242 return IObuff;
3243}
3244
3245/*
3246 * Update popup windows. They are drawn on top of normal windows.
3247 * "win_update" is called for each popup window, lowest zindex first.
3248 */
3249 void
3250update_popups(void (*win_update)(win_T *wp))
3251{
3252 win_T *wp;
3253 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003254 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003255 int total_width;
3256 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003257 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003258 int popup_attr;
3259 int border_attr[4];
3260 int border_char[8];
3261 char_u buf[MB_MAXBYTES];
3262 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003263 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003264 int padcol = 0;
3265 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003266 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003267 int sb_thumb_top = 0;
3268 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003269 int attr_scroll = 0;
3270 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003271
3272 // Find the window with the lowest zindex that hasn't been updated yet,
3273 // so that the window with a higher zindex is drawn later, thus goes on
3274 // top.
3275 popup_reset_handled();
3276 while ((wp = find_next_popup(TRUE)) != NULL)
3277 {
3278 // This drawing uses the zindex of the popup window, so that it's on
3279 // top of the text but doesn't draw when another popup with higher
3280 // zindex is on top of the character.
3281 screen_zindex = wp->w_zindex;
3282
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003283 // Set flags in popup_transparent[] for masked cells.
3284 update_popup_transparent(wp, 1);
3285
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003286 // adjust w_winrow and w_wincol for border and padding, since
3287 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003288 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003289 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3290 - wp->w_popup_leftoff;
3291 if (wp->w_wincol + left_extra < 0)
3292 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003293 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003294 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003295
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003296 // Draw the popup text, unless it's off screen.
3297 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003298 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003299 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003300
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003301 // move the cursor into the visible lines, otherwise executing
3302 // commands with win_execute() may cause the text to jump.
3303 if (wp->w_cursor.lnum < wp->w_topline)
3304 wp->w_cursor.lnum = wp->w_topline;
3305 else if (wp->w_cursor.lnum >= wp->w_botline)
3306 wp->w_cursor.lnum = wp->w_botline - 1;
3307 }
3308
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003309 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003310 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003311
Bram Moolenaard529ba52019-07-02 23:13:53 +02003312 total_width = popup_width(wp);
3313 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003314 popup_attr = get_wcr_attr(wp);
3315
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003316 if (wp->w_winrow + total_height > cmdline_row)
3317 wp->w_popup_flags |= POPF_ON_CMDLINE;
3318 else
3319 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3320
3321
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003322 // We can only use these line drawing characters when 'encoding' is
3323 // "utf-8" and 'ambiwidth' is "single".
3324 if (enc_utf8 && *p_ambw == 's')
3325 {
3326 border_char[0] = border_char[2] = 0x2550;
3327 border_char[1] = border_char[3] = 0x2551;
3328 border_char[4] = 0x2554;
3329 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003330 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3331 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003332 border_char[7] = 0x255a;
3333 }
3334 else
3335 {
3336 border_char[0] = border_char[2] = '-';
3337 border_char[1] = border_char[3] = '|';
3338 for (i = 4; i < 8; ++i)
3339 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003340 if (wp->w_popup_flags & POPF_RESIZE)
3341 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003342 }
3343 for (i = 0; i < 8; ++i)
3344 if (wp->w_border_char[i] != 0)
3345 border_char[i] = wp->w_border_char[i];
3346
3347 for (i = 0; i < 4; ++i)
3348 {
3349 border_attr[i] = popup_attr;
3350 if (wp->w_border_highlight[i] != NULL)
3351 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3352 }
3353
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003354 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003355 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003356 if (wp->w_popup_border[0] > 0)
3357 {
3358 // top border
3359 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003360 wincol < 0 ? 0 : wincol, wincol + total_width,
3361 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003362 ? border_char[4] : border_char[0],
3363 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003364 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003365 {
3366 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3367 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003368 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003369 }
3370 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003371 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3372 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003373
Bram Moolenaard529ba52019-07-02 23:13:53 +02003374 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3375 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003376 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003377 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3378 - wp->w_has_scrollbar;
3379 if (padcol < 0)
3380 {
3381 padwidth += padcol;
3382 padcol = 0;
3383 }
3384 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003385 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003386 {
3387 // top padding
3388 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003389 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003390 ' ', ' ', popup_attr);
3391 }
3392
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003393 // Title goes on top of border or padding.
3394 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003395 {
3396 int len = (int)STRLEN(wp->w_popup_title) + 1;
3397 char_u *title = alloc(len);
3398
3399 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3400 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003401 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003402 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003403 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003404
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003405 // Compute scrollbar thumb position and size.
3406 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003407 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003408 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3409 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003410
Bram Moolenaar076d9882019-09-14 22:23:29 +02003411 sb_thumb_height = (height * height + linecount / 2) / linecount;
3412 if (wp->w_topline > 1 && sb_thumb_height == height)
3413 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003414 if (sb_thumb_height == 0)
3415 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003416 if (linecount <= wp->w_height)
3417 // it just fits, avoid divide by zero
3418 sb_thumb_top = 0;
3419 else
3420 sb_thumb_top = (wp->w_topline - 1
3421 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003422 * (wp->w_height - sb_thumb_height)
3423 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003424 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3425 sb_thumb_top = 1; // show it's scrolled
3426
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003427 if (wp->w_scrollbar_highlight != NULL)
3428 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3429 else
3430 attr_scroll = highlight_attr[HLF_PSB];
3431 if (wp->w_thumb_highlight != NULL)
3432 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3433 else
3434 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003435 }
3436
3437 for (i = wp->w_popup_border[0];
3438 i < total_height - wp->w_popup_border[2]; ++i)
3439 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003440 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003441 // left and right padding only needed next to the body
3442 int do_padding =
3443 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3444 && i < total_height - wp->w_popup_border[2]
3445 - wp->w_popup_padding[2];
3446
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003447 row = wp->w_winrow + i;
3448
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003449 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003450 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003451 {
3452 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003453 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003454 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003455 if (do_padding && wp->w_popup_padding[3] > 0)
3456 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003457 int col = wincol + wp->w_popup_border[3];
3458
Bram Moolenaard529ba52019-07-02 23:13:53 +02003459 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003460 pad_left = wp->w_popup_padding[3];
3461 if (col < 0)
3462 {
3463 pad_left += col;
3464 col = 0;
3465 }
3466 if (pad_left > 0)
3467 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3468 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003469 // scrollbar
3470 if (wp->w_has_scrollbar)
3471 {
3472 int line = i - top_off;
3473 int scroll_col = wp->w_wincol + total_width - 1
3474 - wp->w_popup_border[1];
3475
3476 if (line >= 0 && line < wp->w_height)
3477 screen_putchar(' ', row, scroll_col,
3478 line >= sb_thumb_top
3479 && line < sb_thumb_top + sb_thumb_height
3480 ? attr_thumb : attr_scroll);
3481 else
3482 screen_putchar(' ', row, scroll_col, popup_attr);
3483 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003484 // right border
3485 if (wp->w_popup_border[1] > 0)
3486 {
3487 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003488 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003489 }
3490 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003491 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003492 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003493 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003494 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3495 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003496 }
3497
3498 if (wp->w_popup_padding[2] > 0)
3499 {
3500 // bottom padding
3501 row = wp->w_winrow + wp->w_popup_border[0]
3502 + wp->w_popup_padding[0] + wp->w_height;
3503 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003504 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003505 }
3506
3507 if (wp->w_popup_border[2] > 0)
3508 {
3509 // bottom border
3510 row = wp->w_winrow + total_height - 1;
3511 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003512 wincol < 0 ? 0 : wincol,
3513 wincol + total_width,
3514 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003515 ? border_char[7] : border_char[2],
3516 border_char[2], border_attr[2]);
3517 if (wp->w_popup_border[1] > 0)
3518 {
3519 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003520 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003521 }
3522 }
3523
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003524 if (wp->w_popup_close == POPCLOSE_BUTTON)
3525 {
3526 // close button goes on top of anything at the top-right corner
3527 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003528 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003529 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3530 }
3531
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003532 update_popup_transparent(wp, 0);
3533
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003534 // Back to the normal zindex.
3535 screen_zindex = 0;
3536 }
3537}
3538
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003539/*
3540 * Mark references in callbacks of one popup window.
3541 */
3542 static int
3543set_ref_in_one_popup(win_T *wp, int copyID)
3544{
3545 int abort = FALSE;
3546 typval_T tv;
3547
3548 if (wp->w_close_cb.cb_partial != NULL)
3549 {
3550 tv.v_type = VAR_PARTIAL;
3551 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3552 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3553 }
3554 if (wp->w_filter_cb.cb_partial != NULL)
3555 {
3556 tv.v_type = VAR_PARTIAL;
3557 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3558 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3559 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003560 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003561 return abort;
3562}
3563
3564/*
3565 * Set reference in callbacks of popup windows.
3566 */
3567 int
3568set_ref_in_popups(int copyID)
3569{
3570 int abort = FALSE;
3571 win_T *wp;
3572 tabpage_T *tp;
3573
3574 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3575 abort = abort || set_ref_in_one_popup(wp, copyID);
3576
3577 FOR_ALL_TABPAGES(tp)
3578 {
3579 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3580 abort = abort || set_ref_in_one_popup(wp, copyID);
3581 if (abort)
3582 break;
3583 }
3584 return abort;
3585}
Bram Moolenaar79648732019-07-18 21:43:07 +02003586
3587/*
3588 * Find an existing popup used as the preview window, in the current tab page.
3589 * Return NULL if not found.
3590 */
3591 win_T *
3592popup_find_preview_window(void)
3593{
3594 win_T *wp;
3595
3596 // Preview window popup is always local to tab page.
3597 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3598 if (wp->w_p_pvw)
3599 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003600 return NULL;
3601}
3602
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003603 int
3604popup_is_popup(win_T *wp)
3605{
3606 return wp->w_popup_flags != 0;
3607}
3608
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003609#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003610/*
3611 * Find an existing popup used as the info window, in the current tab page.
3612 * Return NULL if not found.
3613 */
3614 win_T *
3615popup_find_info_window(void)
3616{
3617 win_T *wp;
3618
3619 // info window popup is always local to tab page.
3620 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3621 if (wp->w_popup_flags & POPF_INFO)
3622 return wp;
3623 return NULL;
3624}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003625#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003626
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003627 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003628f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3629{
3630 win_T *wp = popup_find_info_window();
3631
3632 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3633}
3634
3635 void
3636f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003637{
3638 win_T *wp = popup_find_preview_window();
3639
3640 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003641}
3642
Bram Moolenaar79648732019-07-18 21:43:07 +02003643/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003644 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003645 * NOTE: this makes the popup the current window, so that the file can be
3646 * edited. However, it must not remain to be the current window, the caller
3647 * must make sure of that.
3648 */
3649 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003650popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003651{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003652 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003653
3654 if (wp == NULL)
3655 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003656 if (info)
3657 wp->w_popup_flags |= POPF_INFO;
3658 else
3659 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003660
3661 // Set the width to a reasonable value, so that w_topline can be computed.
3662 if (wp->w_minwidth > 0)
3663 wp->w_width = wp->w_minwidth;
3664 else if (wp->w_maxwidth > 0)
3665 wp->w_width = wp->w_maxwidth;
3666 else
3667 wp->w_width = curwin->w_width;
3668
3669 // Will switch to another buffer soon, dummy one can be wiped.
3670 wp->w_buffer->b_locked = FALSE;
3671
3672 win_enter(wp, FALSE);
3673 return OK;
3674}
3675
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003676#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003677/*
3678 * Close any preview popup.
3679 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003680 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003681popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003682{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003683 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003684
3685 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003686 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003687}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003688
3689/*
3690 * Hide the info popup.
3691 */
3692 void
3693popup_hide_info(void)
3694{
3695 win_T *wp = popup_find_info_window();
3696
3697 if (wp != NULL)
3698 popup_hide(wp);
3699}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003700#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003701
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003702/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003703 * Close any popup for a text property associated with "win".
3704 * Return TRUE if a popup was closed.
3705 */
3706 int
3707popup_win_closed(win_T *win)
3708{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003709 int round;
3710 win_T *wp;
3711 win_T *next;
3712 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003713
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003714 for (round = 1; round <= 2; ++round)
3715 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3716 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003717 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003718 next = wp->w_next;
3719 if (wp->w_popup_prop_win == win)
3720 {
3721 popup_close_with_retval(wp, -1);
3722 ret = TRUE;
3723 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003724 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003725 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003726}
3727
3728/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003729 * Set the title of the popup window to the file name.
3730 */
3731 void
3732popup_set_title(win_T *wp)
3733{
3734 if (wp->w_buffer->b_fname != NULL)
3735 {
3736 char_u dirname[MAXPATHL];
3737 size_t len;
3738
3739 mch_dirname(dirname, MAXPATHL);
3740 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3741
3742 vim_free(wp->w_popup_title);
3743 len = STRLEN(wp->w_buffer->b_fname) + 3;
3744 wp->w_popup_title = alloc((int)len);
3745 if (wp->w_popup_title != NULL)
3746 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3747 wp->w_buffer->b_fname);
3748 redraw_win_later(wp, VALID);
3749 }
3750}
3751
3752/*
3753 * If there is a preview window, update the title.
3754 * Used after changing directory.
3755 */
3756 void
3757popup_update_preview_title(void)
3758{
3759 win_T *wp = popup_find_preview_window();
3760
3761 if (wp != NULL)
3762 popup_set_title(wp);
3763}
3764
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003765#endif // FEAT_TEXT_PROP