blob: 95435f8fe7e0bae5eea036e39e4510ae374f3d97 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
74 if (n < 1)
75 n = 1;
76 return n;
77}
78
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020079 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020080set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081{
82 dictitem_T *di;
83
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020084 di = dict_find(dict, (char_u *)name, -1);
85 if (di != NULL)
86 {
87 if (di->di_tv.v_type != VAR_LIST)
88 emsg(_(e_listreq));
89 else
90 {
91 list_T *list = di->di_tv.vval.v_list;
92 listitem_T *li;
93 int i;
94 int nr;
95
96 for (i = 0; i < 4; ++i)
97 array[i] = 1;
98 if (list != NULL)
99 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
100 ++i, li = li->li_next)
101 {
102 nr = (int)tv_get_number(&li->li_tv);
103 if (nr >= 0)
104 array[i] = nr > max_val ? max_val : nr;
105 }
106 }
107 }
108}
109
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200110/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200111 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200112 */
113 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200114set_moved_values(win_T *wp)
115{
116 wp->w_popup_curwin = curwin;
117 wp->w_popup_lnum = curwin->w_cursor.lnum;
118 wp->w_popup_mincol = curwin->w_cursor.col;
119 wp->w_popup_maxcol = curwin->w_cursor.col;
120}
121
122/*
123 * Used when popup options contain "moved" with "word" or "WORD".
124 */
125 static void
126set_moved_columns(win_T *wp, int flags)
127{
128 char_u *ptr;
129 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
130
131 if (len > 0)
132 {
133 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
134 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
135 }
136}
137
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200138/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200139 * Used when popup options contain "mousemoved": set default moved values.
140 */
141 static void
142set_mousemoved_values(win_T *wp)
143{
144 wp->w_popup_mouse_row = mouse_row;
145 wp->w_popup_mouse_mincol = mouse_col;
146 wp->w_popup_mouse_maxcol = mouse_col;
147}
148
149/*
150 * Used when popup options contain "moved" with "word" or "WORD".
151 */
152 static void
153set_mousemoved_columns(win_T *wp, int flags)
154{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200155 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200156 char_u *text;
157 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200158 pos_T pos;
159 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160
161 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200163 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200164 // convert text column to mouse column
165 pos.col = col;
166 pos.coladd = 0;
167 getvcol(textwp, &pos, &mcol, NULL, NULL);
168 wp->w_popup_mouse_mincol = mcol;
169
Bram Moolenaar10727682019-07-12 13:59:20 +0200170 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200171 getvcol(textwp, &pos, NULL, NULL, &mcol);
172 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200173 vim_free(text);
174 }
175}
176
177/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200178 * Return TRUE if "row"/"col" is on the border of the popup.
179 * The values are relative to the top-left corner.
180 */
181 int
182popup_on_border(win_T *wp, int row, int col)
183{
184 return (row == 0 && wp->w_popup_border[0] > 0)
185 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
186 || (col == 0 && wp->w_popup_border[3] > 0)
187 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
188}
189
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200190/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200191 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
192 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200193 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200194 * Caller should check the left mouse button was clicked.
195 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200196 */
197 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200199{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200200 if (wp->w_popup_close == POPCLOSE_BUTTON
201 && row == 0 && col == popup_width(wp) - 1)
202 {
203 popup_close_for_mouse_click(wp);
204 return TRUE;
205 }
206 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200207}
208
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200209// Values set when dragging a popup window starts.
210static int drag_start_row;
211static int drag_start_col;
212static int drag_start_wantline;
213static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200214static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200215
216/*
217 * Mouse down on border of popup window: start dragging it.
218 * Uses mouse_col and mouse_row.
219 */
220 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200221popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222{
223 drag_start_row = mouse_row;
224 drag_start_col = mouse_col;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200225 if (wp->w_wantline == 0)
226 drag_start_wantline = wp->w_winrow + 1;
227 else
228 drag_start_wantline = wp->w_wantline;
229 if (wp->w_wantcol == 0)
230 drag_start_wantcol = wp->w_wincol + 1;
231 else
232 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200233
234 // Stop centering the popup
235 if (wp->w_popup_pos == POPPOS_CENTER)
236 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200237
238 drag_on_resize_handle = wp->w_popup_border[1] > 0
239 && wp->w_popup_border[2] > 0
240 && row == popup_height(wp) - 1
241 && col == popup_width(wp) - 1;
242
243 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
244 {
245 if (wp->w_popup_pos == POPPOS_TOPRIGHT
246 || wp->w_popup_pos == POPPOS_BOTRIGHT)
247 wp->w_wantcol = wp->w_wincol + 1;
248 if (wp->w_popup_pos == POPPOS_BOTLEFT)
249 wp->w_wantline = wp->w_winrow + 1;
250 wp->w_popup_pos = POPPOS_TOPLEFT;
251 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200252}
253
254/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200255 * Mouse moved while dragging a popup window: adjust the window popup position
256 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200257 */
258 void
259popup_drag(win_T *wp)
260{
261 // The popup may be closed before dragging stops.
262 if (!win_valid_popup(wp))
263 return;
264
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200265 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
266 {
267 int width_inc = mouse_col - drag_start_col;
268 int height_inc = mouse_row - drag_start_row;
269
270 if (width_inc != 0)
271 {
272 int width = wp->w_width + width_inc;
273
274 if (width < 1)
275 width = 1;
276 wp->w_minwidth = width;
277 wp->w_maxwidth = width;
278 drag_start_col = mouse_col;
279 }
280
281 if (height_inc != 0)
282 {
283 int height = wp->w_height + height_inc;
284
285 if (height < 1)
286 height = 1;
287 wp->w_minheight = height;
288 wp->w_maxheight = height;
289 drag_start_row = mouse_row;
290 }
291
292 popup_adjust_position(wp);
293 return;
294 }
295
296 if (!(wp->w_popup_flags & POPF_DRAG))
297 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200298 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
299 if (wp->w_wantline < 1)
300 wp->w_wantline = 1;
301 if (wp->w_wantline > Rows)
302 wp->w_wantline = Rows;
303 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
304 if (wp->w_wantcol < 1)
305 wp->w_wantcol = 1;
306 if (wp->w_wantcol > Columns)
307 wp->w_wantcol = Columns;
308
309 popup_adjust_position(wp);
310}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200311
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200312/*
313 * Set w_firstline to match the current "wp->w_topline".
314 */
315 void
316popup_set_firstline(win_T *wp)
317{
318 int height = wp->w_height;
319
320 wp->w_firstline = wp->w_topline;
321 popup_adjust_position(wp);
322
323 // we don't want the popup to get smaller, decrement the first line
324 // until it doesn't
325 while (wp->w_firstline > 1 && wp->w_height < height)
326 {
327 --wp->w_firstline;
328 popup_adjust_position(wp);
329 }
330}
331
332/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200333 * Return TRUE if the position is in the popup window scrollbar.
334 */
335 int
336popup_is_in_scrollbar(win_T *wp, int row, int col)
337{
338 return wp->w_has_scrollbar
339 && row >= wp->w_popup_border[0]
340 && row < popup_height(wp) - wp->w_popup_border[2]
341 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
342}
343
344
345/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200346 * Handle a click in a popup window, if it is in the scrollbar.
347 */
348 void
349popup_handle_scrollbar_click(win_T *wp, int row, int col)
350{
351 int height = popup_height(wp);
352 int old_topline = wp->w_topline;
353
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200354 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200355 {
356 if (row >= height / 2)
357 {
358 // Click in lower half, scroll down.
359 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
360 ++wp->w_topline;
361 }
362 else if (wp->w_topline > 1)
363 // click on upper half, scroll up.
364 --wp->w_topline;
365 if (wp->w_topline != old_topline)
366 {
367 popup_set_firstline(wp);
368 redraw_win_later(wp, NOT_VALID);
369 }
370 }
371}
372
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200373#if defined(FEAT_TIMERS)
374 static void
375popup_add_timeout(win_T *wp, int time)
376{
377 char_u cbbuf[50];
378 char_u *ptr = cbbuf;
379 typval_T tv;
380
381 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
382 "{_ -> popup_close(%d)}", wp->w_id);
383 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
384 {
385 wp->w_popup_timer = create_timer(time, 0);
386 wp->w_popup_timer->tr_callback = get_callback(&tv);
387 clear_tv(&tv);
388 }
389}
390#endif
391
Bram Moolenaar17627312019-06-02 19:53:44 +0200392/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200393 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200394 */
395 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200396apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200397{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200398 int nr;
399 char_u *str;
400 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200401
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200402 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200403 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200404 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200405 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200406 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200407 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200408 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200409 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200410
411 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200412 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200413 wp->w_wantline = nr;
414 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200415 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200416 wp->w_wantcol = nr;
417
418 di = dict_find(d, (char_u *)"fixed", -1);
419 if (di != NULL)
420 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
421
422 str = dict_get_string(d, (char_u *)"pos", FALSE);
423 if (str != NULL)
424 {
425 for (nr = 0;
426 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
427 ++nr)
428 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
429 {
430 wp->w_popup_pos = poppos_entries[nr].pp_val;
431 nr = -1;
432 break;
433 }
434 if (nr != -1)
435 semsg(_(e_invarg2), str);
436 }
437
438 str = dict_get_string(d, (char_u *)"textprop", FALSE);
439 if (str != NULL)
440 {
441 wp->w_popup_prop_type = 0;
442 if (*str != NUL)
443 {
444 nr = find_prop_type_id(str, wp->w_buffer);
445 if (nr <= 0)
446 nr = find_prop_type_id(str, NULL);
447 if (nr <= 0)
448 semsg(_(e_invarg2), str);
449 else
450 {
451 wp->w_popup_prop_type = nr;
452 wp->w_popup_prop_win = curwin;
453
454 di = dict_find(d, (char_u *)"textpropwin", -1);
455 if (di != NULL)
456 {
457 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
458 if (win_valid(wp->w_popup_prop_win))
459 wp->w_popup_prop_win = curwin;
460 }
461 }
462 }
463 }
464
465 di = dict_find(d, (char_u *)"textpropid", -1);
466 if (di != NULL)
467 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200468}
469
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200470 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200471handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
472{
473 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
474 {
475 char_u *s = di->di_tv.vval.v_string;
476 int flags = 0;
477
478 if (STRCMP(s, "word") == 0)
479 flags = FIND_IDENT | FIND_STRING;
480 else if (STRCMP(s, "WORD") == 0)
481 flags = FIND_STRING;
482 else if (STRCMP(s, "expr") == 0)
483 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
484 else if (STRCMP(s, "any") != 0)
485 semsg(_(e_invarg2), s);
486 if (flags != 0)
487 {
488 if (mousemoved)
489 set_mousemoved_columns(wp, flags);
490 else
491 set_moved_columns(wp, flags);
492 }
493 }
494 else if (di->di_tv.v_type == VAR_LIST
495 && di->di_tv.vval.v_list != NULL
496 && di->di_tv.vval.v_list->lv_len == 2)
497 {
498 list_T *l = di->di_tv.vval.v_list;
499 int mincol = tv_get_number(&l->lv_first->li_tv);
500 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
501
502 if (mousemoved)
503 {
504 wp->w_popup_mouse_mincol = mincol;
505 wp->w_popup_mouse_maxcol = maxcol;
506 }
507 else
508 {
509 wp->w_popup_mincol = mincol;
510 wp->w_popup_maxcol = maxcol;
511 }
512 }
513 else
514 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
515}
516
517 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200518check_highlight(dict_T *dict, char *name, char_u **pval)
519{
520 dictitem_T *di;
521 char_u *str;
522
523 di = dict_find(dict, (char_u *)name, -1);
524 if (di != NULL)
525 {
526 if (di->di_tv.v_type != VAR_STRING)
527 semsg(_(e_invargval), name);
528 else
529 {
530 str = tv_get_string(&di->di_tv);
531 if (*str != NUL)
532 *pval = vim_strsave(str);
533 }
534 }
535}
536
Bram Moolenaarae943152019-06-16 22:54:14 +0200537/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200538 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200539 */
540 static void
541popup_show_curline(win_T *wp)
542{
543 if (wp->w_cursor.lnum < wp->w_topline)
544 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200545 else if (wp->w_cursor.lnum >= wp->w_botline
546 && (curwin->w_valid & VALID_BOTLINE))
547 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200548 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200549 if (wp->w_topline < 1)
550 wp->w_topline = 1;
551 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
552 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200553 while (wp->w_topline < wp->w_cursor.lnum
554 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
555 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
556 > wp->w_height)
557 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200558 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200559
560 // Don't use "firstline" now.
561 wp->w_firstline = 0;
562}
563
564/*
565 * Get the sign group name for window "wp".
566 * Returns a pointer to a static buffer, overwritten on the next call.
567 */
568 static char_u *
569popup_get_sign_name(win_T *wp)
570{
571 static char buf[30];
572
573 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
574 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200575}
576
577/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200578 * Highlight the line with the cursor.
579 * Also scrolls the text to put the cursor line in view.
580 */
581 static void
582popup_highlight_curline(win_T *wp)
583{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200584 int sign_id = 0;
585 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200586
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200587 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200588
589 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
590 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200591 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200592
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200593 if (!sign_exists_by_name(sign_name))
594 {
595 char *linehl = "PopupSelected";
596
597 if (syn_name2id((char_u *)linehl) == 0)
598 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200599 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200600 }
601
602 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
603 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
604 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200605 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200606 else
607 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200608 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200609}
610
611/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200612 * Shared between popup_create() and f_popup_setoptions().
613 */
614 static void
615apply_general_options(win_T *wp, dict_T *dict)
616{
617 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200618 int nr;
619 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200620
Bram Moolenaarae943152019-06-16 22:54:14 +0200621 // TODO: flip
622
623 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200624 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200625 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200626 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200627 if (wp->w_firstline < 0)
628 wp->w_firstline = -1;
629 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200630
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200631 di = dict_find(dict, (char_u *)"scrollbar", -1);
632 if (di != NULL)
633 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
634
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200635 str = dict_get_string(dict, (char_u *)"title", FALSE);
636 if (str != NULL)
637 {
638 vim_free(wp->w_popup_title);
639 wp->w_popup_title = vim_strsave(str);
640 }
641
Bram Moolenaar402502d2019-05-30 22:07:36 +0200642 di = dict_find(dict, (char_u *)"wrap", -1);
643 if (di != NULL)
644 {
645 nr = dict_get_number(dict, (char_u *)"wrap");
646 wp->w_p_wrap = nr != 0;
647 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200648
Bram Moolenaara42d9452019-06-15 21:46:30 +0200649 di = dict_find(dict, (char_u *)"drag", -1);
650 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200651 {
652 nr = dict_get_number(dict, (char_u *)"drag");
653 if (nr)
654 wp->w_popup_flags |= POPF_DRAG;
655 else
656 wp->w_popup_flags &= ~POPF_DRAG;
657 }
658
659 di = dict_find(dict, (char_u *)"resize", -1);
660 if (di != NULL)
661 {
662 nr = dict_get_number(dict, (char_u *)"resize");
663 if (nr)
664 wp->w_popup_flags |= POPF_RESIZE;
665 else
666 wp->w_popup_flags &= ~POPF_RESIZE;
667 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200668
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200669 di = dict_find(dict, (char_u *)"close", -1);
670 if (di != NULL)
671 {
672 int ok = TRUE;
673
674 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
675 {
676 char_u *s = di->di_tv.vval.v_string;
677
678 if (STRCMP(s, "none") == 0)
679 wp->w_popup_close = POPCLOSE_NONE;
680 else if (STRCMP(s, "button") == 0)
681 wp->w_popup_close = POPCLOSE_BUTTON;
682 else if (STRCMP(s, "click") == 0)
683 wp->w_popup_close = POPCLOSE_CLICK;
684 else
685 ok = FALSE;
686 }
687 else
688 ok = FALSE;
689 if (!ok)
690 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
691 }
692
Bram Moolenaarae943152019-06-16 22:54:14 +0200693 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
694 if (str != NULL)
695 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
696 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200697
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200698 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
699 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200700 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
701 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200702
Bram Moolenaar790498b2019-06-01 22:15:29 +0200703 di = dict_find(dict, (char_u *)"borderhighlight", -1);
704 if (di != NULL)
705 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200706 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200707 emsg(_(e_listreq));
708 else
709 {
710 list_T *list = di->di_tv.vval.v_list;
711 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200712 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200713
Bram Moolenaar403d0902019-07-17 21:37:32 +0200714 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
715 ++i, li = li->li_next)
716 {
717 str = tv_get_string(&li->li_tv);
718 if (*str != NUL)
719 wp->w_border_highlight[i] = vim_strsave(str);
720 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200721 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
722 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200723 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200724 vim_strsave(wp->w_border_highlight[0]);
725 }
726 }
727
Bram Moolenaar790498b2019-06-01 22:15:29 +0200728 di = dict_find(dict, (char_u *)"borderchars", -1);
729 if (di != NULL)
730 {
731 if (di->di_tv.v_type != VAR_LIST)
732 emsg(_(e_listreq));
733 else
734 {
735 list_T *list = di->di_tv.vval.v_list;
736 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200737 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200738
739 if (list != NULL)
740 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
741 ++i, li = li->li_next)
742 {
743 str = tv_get_string(&li->li_tv);
744 if (*str != NUL)
745 wp->w_border_char[i] = mb_ptr2char(str);
746 }
747 if (list->lv_len == 1)
748 for (i = 1; i < 8; ++i)
749 wp->w_border_char[i] = wp->w_border_char[0];
750 if (list->lv_len == 2)
751 {
752 for (i = 4; i < 8; ++i)
753 wp->w_border_char[i] = wp->w_border_char[1];
754 for (i = 1; i < 4; ++i)
755 wp->w_border_char[i] = wp->w_border_char[0];
756 }
757 }
758 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200759
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200760 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
761 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
762
Bram Moolenaarae943152019-06-16 22:54:14 +0200763 di = dict_find(dict, (char_u *)"zindex", -1);
764 if (di != NULL)
765 {
766 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
767 if (wp->w_zindex < 1)
768 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
769 if (wp->w_zindex > 32000)
770 wp->w_zindex = 32000;
771 }
772
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200773 di = dict_find(dict, (char_u *)"mask", -1);
774 if (di != NULL)
775 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200776 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200777
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200778 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200779 {
780 listitem_T *li;
781
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200782 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200783 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
784 li = li->li_next)
785 {
786 if (li->li_tv.v_type != VAR_LIST
787 || li->li_tv.vval.v_list == NULL
788 || li->li_tv.vval.v_list->lv_len != 4)
789 {
790 ok = FALSE;
791 break;
792 }
793 }
794 }
795 if (ok)
796 {
797 wp->w_popup_mask = di->di_tv.vval.v_list;
798 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200799 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200800 }
801 else
802 semsg(_(e_invargval), "mask");
803 }
804
Bram Moolenaarae943152019-06-16 22:54:14 +0200805#if defined(FEAT_TIMERS)
806 // Add timer to close the popup after some time.
807 nr = dict_get_number(dict, (char_u *)"time");
808 if (nr > 0)
809 popup_add_timeout(wp, nr);
810#endif
811
Bram Moolenaar3397f742019-06-02 18:40:06 +0200812 di = dict_find(dict, (char_u *)"moved", -1);
813 if (di != NULL)
814 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200815 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200816 handle_moved_argument(wp, di, FALSE);
817 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200818
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200819 di = dict_find(dict, (char_u *)"mousemoved", -1);
820 if (di != NULL)
821 {
822 set_mousemoved_values(wp);
823 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200824 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200825
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200826 di = dict_find(dict, (char_u *)"cursorline", -1);
827 if (di != NULL)
828 {
829 if (di->di_tv.v_type == VAR_NUMBER)
830 {
831 if (di->di_tv.vval.v_number != 0)
832 wp->w_popup_flags |= POPF_CURSORLINE;
833 else
834 wp->w_popup_flags &= ~POPF_CURSORLINE;
835 }
836 else
837 semsg(_(e_invargval), "cursorline");
838 }
839
Bram Moolenaarae943152019-06-16 22:54:14 +0200840 di = dict_find(dict, (char_u *)"filter", -1);
841 if (di != NULL)
842 {
843 callback_T callback = get_callback(&di->di_tv);
844
845 if (callback.cb_name != NULL)
846 {
847 free_callback(&wp->w_filter_cb);
848 set_callback(&wp->w_filter_cb, &callback);
849 }
850 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200851 di = dict_find(dict, (char_u *)"mapping", -1);
852 if (di != NULL)
853 {
854 nr = dict_get_number(dict, (char_u *)"mapping");
855 if (nr)
856 wp->w_popup_flags |= POPF_MAPPING;
857 else
858 wp->w_popup_flags &= ~POPF_MAPPING;
859 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200860
Bram Moolenaar581ba392019-09-03 22:08:33 +0200861 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
862 if (str != NULL)
863 {
864 if (STRCMP(str, "a") == 0)
865 wp->w_filter_mode = MODE_ALL;
866 else
867 wp->w_filter_mode = mode_str2flags(str);
868 }
869
Bram Moolenaarae943152019-06-16 22:54:14 +0200870 di = dict_find(dict, (char_u *)"callback", -1);
871 if (di != NULL)
872 {
873 callback_T callback = get_callback(&di->di_tv);
874
875 if (callback.cb_name != NULL)
876 {
877 free_callback(&wp->w_close_cb);
878 set_callback(&wp->w_close_cb, &callback);
879 }
880 }
881}
882
883/*
884 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200885 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200886 */
887 static void
888apply_options(win_T *wp, dict_T *dict)
889{
890 int nr;
891
892 apply_move_options(wp, dict);
893 apply_general_options(wp, dict);
894
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200895 nr = dict_get_number(dict, (char_u *)"hidden");
896 if (nr > 0)
897 {
898 wp->w_popup_flags |= POPF_HIDDEN;
899 --wp->w_buffer->b_nwindows;
900 }
901
Bram Moolenaar33796b32019-06-08 16:01:13 +0200902 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200903 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200904}
905
906/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200907 * Add lines to the popup from a list of strings.
908 */
909 static void
910add_popup_strings(buf_T *buf, list_T *l)
911{
912 listitem_T *li;
913 linenr_T lnum = 0;
914 char_u *p;
915
916 for (li = l->lv_first; li != NULL; li = li->li_next)
917 if (li->li_tv.v_type == VAR_STRING)
918 {
919 p = li->li_tv.vval.v_string;
920 ml_append_buf(buf, lnum++,
921 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
922 }
923}
924
925/*
926 * Add lines to the popup from a list of dictionaries.
927 */
928 static void
929add_popup_dicts(buf_T *buf, list_T *l)
930{
931 listitem_T *li;
932 listitem_T *pli;
933 linenr_T lnum = 0;
934 char_u *p;
935 dict_T *dict;
936
937 // first add the text lines
938 for (li = l->lv_first; li != NULL; li = li->li_next)
939 {
940 if (li->li_tv.v_type != VAR_DICT)
941 {
942 emsg(_(e_dictreq));
943 return;
944 }
945 dict = li->li_tv.vval.v_dict;
946 p = dict == NULL ? NULL
947 : dict_get_string(dict, (char_u *)"text", FALSE);
948 ml_append_buf(buf, lnum++,
949 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
950 }
951
952 // add the text properties
953 lnum = 1;
954 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
955 {
956 dictitem_T *di;
957 list_T *plist;
958
959 dict = li->li_tv.vval.v_dict;
960 di = dict_find(dict, (char_u *)"props", -1);
961 if (di != NULL)
962 {
963 if (di->di_tv.v_type != VAR_LIST)
964 {
965 emsg(_(e_listreq));
966 return;
967 }
968 plist = di->di_tv.vval.v_list;
969 if (plist != NULL)
970 {
971 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
972 {
973 if (pli->li_tv.v_type != VAR_DICT)
974 {
975 emsg(_(e_dictreq));
976 return;
977 }
978 dict = pli->li_tv.vval.v_dict;
979 if (dict != NULL)
980 {
981 int col = dict_get_number(dict, (char_u *)"col");
982
983 prop_add_common( lnum, col, dict, buf, NULL);
984 }
985 }
986 }
987 }
988 }
989}
990
991/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200992 * Get the padding plus border at the top, adjusted to 1 if there is a title.
993 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +0200994 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200995popup_top_extra(win_T *wp)
996{
997 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
998
999 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1000 return 1;
1001 return extra;
1002}
1003
1004/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001005 * Return the height of popup window "wp", including border and padding.
1006 */
1007 int
1008popup_height(win_T *wp)
1009{
1010 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001011 + popup_top_extra(wp)
1012 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001013}
1014
1015/*
1016 * Return the width of popup window "wp", including border, padding and
1017 * scrollbar.
1018 */
1019 int
1020popup_width(win_T *wp)
1021{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001022 // w_leftcol is how many columns of the core are left of the screen
1023 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001024 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001025 + popup_extra_width(wp)
1026 + wp->w_popup_rightoff;
1027}
1028
1029/*
1030 * Return the extra width of popup window "wp": border, padding and scrollbar.
1031 */
1032 int
1033popup_extra_width(win_T *wp)
1034{
1035 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1036 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1037 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001038}
1039
1040/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001041 * Adjust the position and size of the popup to fit on the screen.
1042 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001043 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001044popup_adjust_position(win_T *wp)
1045{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001046 linenr_T lnum;
1047 int wrapped = 0;
1048 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001049 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001050 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001051 int center_vert = FALSE;
1052 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001053 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001054 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001055 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1056 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1057 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1058 int extra_height = top_extra + bot_extra;
1059 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001060 int org_winrow = wp->w_winrow;
1061 int org_wincol = wp->w_wincol;
1062 int org_width = wp->w_width;
1063 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001064 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001065 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001066 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001067 int wantline = wp->w_wantline; // adjusted for textprop
1068 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001069
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001070 wp->w_winrow = 0;
1071 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001072 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001073 wp->w_popup_leftoff = 0;
1074 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001075
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001076 // May need to update the "cursorline" highlighting, which may also change
1077 // "topline"
1078 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1079 popup_highlight_curline(wp);
1080
Bram Moolenaar12034e22019-08-25 22:25:02 +02001081 // If no line was specified default to vertical centering.
1082 if (wantline == 0)
1083 center_vert = TRUE;
1084
1085 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1086 {
1087 win_T *prop_win = wp->w_popup_prop_win;
1088 textprop_T prop;
1089 linenr_T prop_lnum;
1090 pos_T pos;
1091 int screen_row;
1092 int screen_scol;
1093 int screen_ccol;
1094 int screen_ecol;
1095
1096 // Popup window is positioned relative to a text property.
1097 if (find_visible_prop(prop_win,
1098 wp->w_popup_prop_type, wp->w_popup_prop_id,
1099 &prop, &prop_lnum) == FAIL)
1100 {
1101 // Text property is no longer visible, hide the popup.
1102 // Unhiding the popup is done in check_popup_unhidden().
1103 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1104 {
1105 wp->w_popup_flags |= POPF_HIDDEN;
1106 --wp->w_buffer->b_nwindows;
1107 if (win_valid(wp->w_popup_prop_win))
1108 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1109 }
1110 return;
1111 }
1112
1113 // Compute the desired position from the position of the text
1114 // property. Use "wantline" and "wantcol" as offsets.
1115 pos.lnum = prop_lnum;
1116 pos.col = prop.tp_col;
1117 if (wp->w_popup_pos == POPPOS_TOPLEFT
1118 || wp->w_popup_pos == POPPOS_BOTLEFT)
1119 pos.col += prop.tp_len - 1;
1120 textpos2screenpos(prop_win, &pos, &screen_row,
1121 &screen_scol, &screen_ccol, &screen_ecol);
1122
1123 if (wp->w_popup_pos == POPPOS_TOPLEFT
1124 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1125 // below the text
1126 wantline = screen_row + wantline + 1;
1127 else
1128 // above the text
1129 wantline = screen_row + wantline - 1;
1130 center_vert = FALSE;
1131 if (wp->w_popup_pos == POPPOS_TOPLEFT
1132 || wp->w_popup_pos == POPPOS_BOTLEFT)
1133 // right of the text
1134 wantcol = screen_ecol + wantcol;
1135 else
1136 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001137 wantcol = screen_scol + wantcol - 2;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001138 }
1139
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001140 if (wp->w_popup_pos == POPPOS_CENTER)
1141 {
1142 // center after computing the size
1143 center_vert = TRUE;
1144 center_hor = TRUE;
1145 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001146 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001147 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001148 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
Bram Moolenaar12034e22019-08-25 22:25:02 +02001149 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001150 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001151 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001152 if (wp->w_winrow >= Rows)
1153 wp->w_winrow = Rows - 1;
1154 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001155
Bram Moolenaar12034e22019-08-25 22:25:02 +02001156 if (wantcol == 0)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001157 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001158 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1159 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001160 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001161 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001162 if (wp->w_wincol >= Columns - 3)
1163 wp->w_wincol = Columns - 3;
1164 }
1165 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001166
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001167 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001168 // When left aligned use the space available, but shift to the left when we
1169 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001170 maxspace = Columns - wp->w_wincol - left_extra;
1171 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001172 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001173 {
1174 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001175 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001176 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001177
Bram Moolenaar8d241042019-06-12 23:40:01 +02001178 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001179 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001180 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001181 if (wp->w_topline < 1)
1182 wp->w_topline = 1;
1183 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001184 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1185
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001186 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001187 // Use a minimum width of one, so that something shows when there is no
1188 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001189 // When "firstline" is -1 then start with the last buffer line and go
1190 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001191 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001192 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001193 if (wp->w_firstline < 0)
1194 lnum = wp->w_buffer->b_ml.ml_line_count;
1195 else
1196 lnum = wp->w_topline;
1197 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001198 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001199 int len;
1200 int w_width = wp->w_width;
1201
1202 // Count Tabs for what they are worth and compute the length based on
1203 // the maximum width (matters when 'showbreak' is set).
1204 if (wp->w_width < maxwidth)
1205 wp->w_width = maxwidth;
1206 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001207 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001208 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001209
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001210 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001211 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001212 while (len > maxwidth)
1213 {
1214 ++wrapped;
1215 len -= maxwidth;
1216 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001217 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001218 }
1219 }
1220 else if (len > maxwidth
1221 && allow_adjust_left
1222 && (wp->w_popup_pos == POPPOS_TOPLEFT
1223 || wp->w_popup_pos == POPPOS_BOTLEFT))
1224 {
1225 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001226 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001227
Bram Moolenaar51c31312019-06-15 22:27:23 +02001228 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001229 {
1230 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001231
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001232 len -= truncate_shift;
1233 shift_by -= truncate_shift;
1234 }
1235
1236 wp->w_wincol -= shift_by;
1237 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001238 wp->w_width = maxwidth;
1239 }
1240 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001241 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001242 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001243 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1244 wp->w_width = wp->w_maxwidth;
1245 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001246 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001247 if (wp->w_maxheight > 0
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001248 && (wp->w_firstline >= 0
1249 ? lnum - wp->w_topline
1250 : wp->w_buffer->b_ml.ml_line_count - lnum)
1251 + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001252 break;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001253
1254 if (wp->w_firstline < 0)
1255 --lnum;
1256 else
1257 ++lnum;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001258 }
1259
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001260 if (wp->w_firstline < 0)
1261 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1262
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001263 wp->w_has_scrollbar = wp->w_want_scrollbar
1264 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001265 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001266 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001267 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001268 ++extra_width;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001269 if (used_maxwidth)
1270 maxwidth -= 2; // try to show the scrollbar
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001271 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001272
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001273 minwidth = wp->w_minwidth;
1274 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1275 {
1276 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1277
1278 if (minwidth < title_len)
1279 minwidth = title_len;
1280 }
1281
1282 if (minwidth > 0 && wp->w_width < minwidth)
1283 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001284 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001285 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001286 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001287 // some columns cut off on the right
1288 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001289 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001290 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001291 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001292 {
1293 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1294 if (wp->w_wincol < 0)
1295 wp->w_wincol = 0;
1296 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001297 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1298 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1299 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001300 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001301
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001302 // Right aligned: move to the right if needed.
1303 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001304 if (leftoff >= 0)
1305 wp->w_wincol = leftoff;
1306 else if (wp->w_popup_fixed)
1307 {
1308 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001309 // columns when displaying the window, minus left border and
1310 // padding.
1311 if (-leftoff > left_extra)
1312 wp->w_leftcol = -leftoff - left_extra;
1313 wp->w_width -= wp->w_leftcol;
1314 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001315 if (wp->w_width < 0)
1316 wp->w_width = 0;
1317 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001318 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001320 if (wp->w_p_wrap || (!wp->w_popup_fixed
1321 && (wp->w_popup_pos == POPPOS_TOPLEFT
1322 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001323 {
1324 int want_col = 0;
1325
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001326 // try to show the right border and any scrollbar
1327 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001328 if (want_col > 0 && wp->w_wincol > 0
1329 && wp->w_wincol + want_col >= Columns)
1330 {
1331 wp->w_wincol = Columns - want_col;
1332 if (wp->w_wincol < 0)
1333 wp->w_wincol = 0;
1334 }
1335 }
1336
Bram Moolenaar8d241042019-06-12 23:40:01 +02001337 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1338 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001339 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1340 wp->w_height = wp->w_minheight;
1341 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1342 wp->w_height = wp->w_maxheight;
1343 if (wp->w_height > Rows - wp->w_winrow)
1344 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001345
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001346 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001347 {
1348 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1349 if (wp->w_winrow < 0)
1350 wp->w_winrow = 0;
1351 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001352 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1353 || wp->w_popup_pos == POPPOS_BOTLEFT)
1354 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001355 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001356 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001357 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001358 else
1359 // not enough space, make top aligned
Bram Moolenaar12034e22019-08-25 22:25:02 +02001360 wp->w_winrow = wantline + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001361 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001362 if (wp->w_winrow >= Rows)
1363 wp->w_winrow = Rows - 1;
1364 else if (wp->w_winrow < 0)
1365 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001366
Bram Moolenaar17146962019-05-30 00:12:11 +02001367 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001368 if (win_valid(wp->w_popup_prop_win))
1369 {
1370 wp->w_popup_prop_changedtick =
1371 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1372 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1373 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001374
1375 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001376 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001377 if (org_winrow != wp->w_winrow
1378 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001379 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001380 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001381 || org_width != wp->w_width
1382 || org_height != wp->w_height)
1383 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001384 redraw_win_later(wp, NOT_VALID);
1385 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1386 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001387 popup_mask_refresh = TRUE;
1388 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001389}
1390
Bram Moolenaar17627312019-06-02 19:53:44 +02001391typedef enum
1392{
1393 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001394 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001395 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001396 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001397 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001398 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001399 TYPE_PREVIEW, // preview window
1400 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001401} create_type_T;
1402
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001403/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001404 * Make "buf" empty and set the contents to "text".
1405 * Used by popup_create() and popup_settext().
1406 */
1407 static void
1408popup_set_buffer_text(buf_T *buf, typval_T text)
1409{
1410 int lnum;
1411
1412 // Clear the buffer, then replace the lines.
1413 curbuf = buf;
1414 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1415 ml_delete(lnum, FALSE);
1416 curbuf = curwin->w_buffer;
1417
1418 // Add text to the buffer.
1419 if (text.v_type == VAR_STRING)
1420 {
1421 // just a string
1422 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1423 }
1424 else
1425 {
1426 list_T *l = text.vval.v_list;
1427
1428 if (l->lv_len > 0)
1429 {
1430 if (l->lv_first->li_tv.v_type == VAR_STRING)
1431 // list of strings
1432 add_popup_strings(buf, l);
1433 else
1434 // list of dictionaries
1435 add_popup_dicts(buf, l);
1436 }
1437 }
1438
1439 // delete the line that was in the empty buffer
1440 curbuf = buf;
1441 ml_delete(buf->b_ml.ml_line_count, FALSE);
1442 curbuf = curwin->w_buffer;
1443}
1444
1445/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001446 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1447 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001448 * Return FAIL if the parsing fails.
1449 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001450 static int
1451parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001452{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001453 char_u *p =
1454#ifdef FEAT_QUICKFIX
1455 !is_preview ? p_cpp :
1456#endif
1457 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001458
Bram Moolenaar258cef52019-08-21 17:29:29 +02001459 if (wp != NULL)
1460 wp->w_popup_flags &= ~POPF_INFO_MENU;
1461
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001462 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001463 {
1464 char_u *e, *dig;
1465 char_u *s = p;
1466 int x;
1467
1468 e = vim_strchr(p, ':');
1469 if (e == NULL || e[1] == NUL)
1470 return FAIL;
1471
1472 p = vim_strchr(e, ',');
1473 if (p == NULL)
1474 p = e + STRLEN(e);
1475 dig = e + 1;
1476 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001477
1478 if (STRNCMP(s, "height:", 7) == 0)
1479 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001480 if (dig != p)
1481 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001482 if (wp != NULL)
1483 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001484 if (is_preview)
1485 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001486 wp->w_maxheight = x;
1487 }
1488 }
1489 else if (STRNCMP(s, "width:", 6) == 0)
1490 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001491 if (dig != p)
1492 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001493 if (wp != NULL)
1494 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001495 if (is_preview)
1496 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001497 wp->w_maxwidth = x;
1498 }
1499 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001500 else if (STRNCMP(s, "highlight:", 10) == 0)
1501 {
1502 if (wp != NULL)
1503 {
1504 int c = *p;
1505
1506 *p = NUL;
1507 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1508 s + 10, OPT_FREE|OPT_LOCAL, 0);
1509 *p = c;
1510 }
1511 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001512 else if (STRNCMP(s, "border:", 7) == 0)
1513 {
1514 char_u *arg = s + 7;
1515 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1516 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1517 int i;
1518
1519 if (!on && !off)
1520 return FAIL;
1521 if (wp != NULL)
1522 {
1523 for (i = 0; i < 4; ++i)
1524 wp->w_popup_border[i] = on ? 1 : 0;
1525 if (off)
1526 // only show the X for close when there is a border
1527 wp->w_popup_close = POPCLOSE_NONE;
1528 }
1529 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001530 else if (STRNCMP(s, "align:", 6) == 0)
1531 {
1532 char_u *arg = s + 6;
1533 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1534 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1535
1536 if (!menu && !item)
1537 return FAIL;
1538 if (wp != NULL && menu)
1539 wp->w_popup_flags |= POPF_INFO_MENU;
1540 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001541 else
1542 return FAIL;
1543 }
1544 return OK;
1545}
1546
1547/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001548 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1549 * is not NULL.
1550 * Return FAIL if the parsing fails.
1551 */
1552 int
1553parse_previewpopup(win_T *wp)
1554{
1555 return parse_popup_option(wp, TRUE);
1556}
1557
1558/*
1559 * Parse the 'completepopup' option and apply the values to window "wp" if it
1560 * is not NULL.
1561 * Return FAIL if the parsing fails.
1562 */
1563 int
1564parse_completepopup(win_T *wp)
1565{
1566 return parse_popup_option(wp, FALSE);
1567}
1568
1569/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001570 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001571 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001572 */
1573 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001574popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001575{
1576 setcursor_mayforce(TRUE);
1577 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1578 if (wp->w_wantline == 0) // cursor in first line
1579 {
1580 wp->w_wantline = 2;
1581 wp->w_popup_pos = POPPOS_TOPLEFT;
1582 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001583
Bram Moolenaar79648732019-07-18 21:43:07 +02001584 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001585 if (wp->w_wantcol > Columns - width)
1586 {
1587 wp->w_wantcol = Columns - width;
1588 if (wp->w_wantcol < 1)
1589 wp->w_wantcol = 1;
1590 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001591 popup_adjust_position(wp);
1592}
1593
1594/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001595 * Set w_wantline and w_wantcol for the a given screen position.
1596 * Caller must take care of running into the window border.
1597 */
1598 void
1599popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1600{
1601 wp->w_wantline = row;
1602 wp->w_wantcol = col;
1603 popup_adjust_position(wp);
1604}
1605
1606/*
1607 * Add a border and lef&right padding.
1608 */
1609 static void
1610add_border_left_right_padding(win_T *wp)
1611{
1612 int i;
1613
1614 for (i = 0; i < 4; ++i)
1615 {
1616 wp->w_popup_border[i] = 1;
1617 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1618 }
1619}
1620
1621/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001622 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001623 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001624 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001625 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001626 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001627 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001628popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001629{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001630 win_T *wp;
1631 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001632 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001633 int new_buffer;
1634 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001635 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001636 int nr;
1637 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001638
Bram Moolenaar79648732019-07-18 21:43:07 +02001639 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001640 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001641 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001642 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001643 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001644 buf = buflist_findnr( argvars[0].vval.v_number);
1645 if (buf == NULL)
1646 {
1647 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1648 return NULL;
1649 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001650#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001651 if (buf->b_term != NULL)
1652 {
1653 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1654 return NULL;
1655 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001656#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001657 }
1658 else if (!(argvars[0].v_type == VAR_STRING
1659 && argvars[0].vval.v_string != NULL)
1660 && !(argvars[0].v_type == VAR_LIST
1661 && argvars[0].vval.v_list != NULL))
1662 {
1663 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001664 return NULL;
1665 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001667 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001668 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001669 return NULL;
1670 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001671 d = argvars[1].vval.v_dict;
1672 }
1673
1674 if (d != NULL)
1675 {
1676 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1677 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1678 else if (type == TYPE_NOTIFICATION)
1679 tabnr = -1; // notifications are global by default
1680 else
1681 tabnr = 0;
1682 if (tabnr > 0)
1683 {
1684 tp = find_tabpage(tabnr);
1685 if (tp == NULL)
1686 {
1687 semsg(_("E997: Tabpage not found: %d"), tabnr);
1688 return NULL;
1689 }
1690 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001691 }
1692
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001693 // Create the window and buffer.
1694 wp = win_alloc_popup_win();
1695 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001696 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001697 if (rettv != NULL)
1698 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001699 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001700 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001701
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001702 if (buf != NULL)
1703 {
1704 // use existing buffer
1705 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001706 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001707 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001708 buffer_ensure_loaded(buf);
1709 }
1710 else
1711 {
1712 // create a new buffer associated with the popup
1713 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001714 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001715 if (buf == NULL)
1716 return NULL;
1717 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001718
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001719 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001720
Bram Moolenaar46451042019-08-24 15:50:46 +02001721 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001722 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001723 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001724 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001725 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001726 buf->b_p_ul = -1; // no undo
1727 buf->b_p_swf = FALSE; // no swap file
1728 buf->b_p_bl = FALSE; // unlisted buffer
1729 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001730
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001731 // Avoid that 'buftype' is reset when this buffer is entered.
1732 buf->b_p_initialized = TRUE;
1733 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001734 wp->w_p_wrap = TRUE; // 'wrap' is default on
1735 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001736
Bram Moolenaara3fce622019-06-20 02:31:49 +02001737 if (tp != NULL)
1738 {
1739 // popup on specified tab page
1740 wp->w_next = tp->tp_first_popupwin;
1741 tp->tp_first_popupwin = wp;
1742 }
1743 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001744 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001745 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001746 wp->w_next = curtab->tp_first_popupwin;
1747 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001748 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001749 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001750 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001751 win_T *prev = first_popupwin;
1752
1753 // Global popup: add at the end, so that it gets displayed on top of
1754 // older ones with the same zindex. Matters for notifications.
1755 if (first_popupwin == NULL)
1756 first_popupwin = wp;
1757 else
1758 {
1759 while (prev->w_next != NULL)
1760 prev = prev->w_next;
1761 prev->w_next = wp;
1762 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001763 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001764
Bram Moolenaar79648732019-07-18 21:43:07 +02001765 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001766 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001767
Bram Moolenaar79648732019-07-18 21:43:07 +02001768 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001769 {
1770 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001771 }
1772 if (type == TYPE_ATCURSOR)
1773 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001774 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001775 set_moved_values(wp);
1776 set_moved_columns(wp, FIND_STRING);
1777 }
1778
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001779 if (type == TYPE_BEVAL)
1780 {
1781 wp->w_popup_pos = POPPOS_BOTLEFT;
1782
1783 // by default use the mouse position
1784 wp->w_wantline = mouse_row;
1785 if (wp->w_wantline <= 0) // mouse on first line
1786 {
1787 wp->w_wantline = 2;
1788 wp->w_popup_pos = POPPOS_TOPLEFT;
1789 }
1790 wp->w_wantcol = mouse_col + 1;
1791 set_mousemoved_values(wp);
1792 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1793 }
1794
Bram Moolenaar33796b32019-06-08 16:01:13 +02001795 // set default values
1796 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001797 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001798
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001799 if (type == TYPE_NOTIFICATION)
1800 {
1801 win_T *twp, *nextwin;
1802 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001803
1804 // Try to not overlap with another global popup. Guess we need 3
1805 // more screen lines than buffer lines.
1806 wp->w_wantline = 1;
1807 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1808 {
1809 nextwin = twp->w_next;
1810 if (twp != wp
1811 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1812 && twp->w_winrow <= wp->w_wantline - 1 + height
1813 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1814 {
1815 // move to below this popup and restart the loop to check for
1816 // overlap with other popups
1817 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1818 nextwin = first_popupwin;
1819 }
1820 }
1821 if (wp->w_wantline + height > Rows)
1822 {
1823 // can't avoid overlap, put on top in the hope that message goes
1824 // away soon.
1825 wp->w_wantline = 1;
1826 }
1827
1828 wp->w_wantcol = 10;
1829 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001830 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001831 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001832 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001833 for (i = 0; i < 4; ++i)
1834 wp->w_popup_border[i] = 1;
1835 wp->w_popup_padding[1] = 1;
1836 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001837
1838 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001839 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001840 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1841 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001842 }
1843
Bram Moolenaara730e552019-06-16 19:05:31 +02001844 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001845 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001846 wp->w_popup_pos = POPPOS_CENTER;
1847 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001848 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001849 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001850 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001851 }
1852
Bram Moolenaara730e552019-06-16 19:05:31 +02001853 if (type == TYPE_MENU)
1854 {
1855 typval_T tv;
1856 callback_T callback;
1857
1858 tv.v_type = VAR_STRING;
1859 tv.vval.v_string = (char_u *)"popup_filter_menu";
1860 callback = get_callback(&tv);
1861 if (callback.cb_name != NULL)
1862 set_callback(&wp->w_filter_cb, &callback);
1863
1864 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001865 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001866 }
1867
Bram Moolenaar79648732019-07-18 21:43:07 +02001868 if (type == TYPE_PREVIEW)
1869 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001870 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001871 wp->w_popup_close = POPCLOSE_BUTTON;
1872 for (i = 0; i < 4; ++i)
1873 wp->w_popup_border[i] = 1;
1874 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001875 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1876 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001877# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001878 if (type == TYPE_INFO)
1879 {
1880 wp->w_popup_pos = POPPOS_TOPLEFT;
1881 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1882 wp->w_popup_close = POPCLOSE_BUTTON;
1883 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001884 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001885 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001886# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001887
Bram Moolenaarae943152019-06-16 22:54:14 +02001888 for (i = 0; i < 4; ++i)
1889 VIM_CLEAR(wp->w_border_highlight[i]);
1890 for (i = 0; i < 8; ++i)
1891 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001892 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001893 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001894 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001895
Bram Moolenaar79648732019-07-18 21:43:07 +02001896 if (d != NULL)
1897 // Deal with options.
1898 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001899
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001900#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001901 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1902 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001903#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001904
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001905 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001906
1907 wp->w_vsep_width = 0;
1908
1909 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001910 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001911
1912 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001913}
1914
1915/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001916 * popup_clear()
1917 */
1918 void
1919f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1920{
1921 close_all_popups();
1922}
1923
1924/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001925 * popup_create({text}, {options})
1926 */
1927 void
1928f_popup_create(typval_T *argvars, typval_T *rettv)
1929{
Bram Moolenaar17627312019-06-02 19:53:44 +02001930 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001931}
1932
1933/*
1934 * popup_atcursor({text}, {options})
1935 */
1936 void
1937f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1938{
Bram Moolenaar17627312019-06-02 19:53:44 +02001939 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001940}
1941
1942/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001943 * popup_beval({text}, {options})
1944 */
1945 void
1946f_popup_beval(typval_T *argvars, typval_T *rettv)
1947{
1948 popup_create(argvars, rettv, TYPE_BEVAL);
1949}
1950
1951/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001952 * Invoke the close callback for window "wp" with value "result".
1953 * Careful: The callback may make "wp" invalid!
1954 */
1955 static void
1956invoke_popup_callback(win_T *wp, typval_T *result)
1957{
1958 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001959 typval_T argv[3];
1960
1961 argv[0].v_type = VAR_NUMBER;
1962 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1963
1964 if (result != NULL && result->v_type != VAR_UNKNOWN)
1965 copy_tv(result, &argv[1]);
1966 else
1967 {
1968 argv[1].v_type = VAR_NUMBER;
1969 argv[1].vval.v_number = 0;
1970 }
1971
1972 argv[2].v_type = VAR_UNKNOWN;
1973
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001974 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001975 if (result != NULL)
1976 clear_tv(&argv[1]);
1977 clear_tv(&rettv);
1978}
1979
1980/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001981 * Close popup "wp" and invoke any close callback for it.
1982 */
1983 static void
1984popup_close_and_callback(win_T *wp, typval_T *arg)
1985{
1986 int id = wp->w_id;
1987
1988 if (wp->w_close_cb.cb_name != NULL)
1989 // Careful: This may make "wp" invalid.
1990 invoke_popup_callback(wp, arg);
1991
1992 popup_close(id);
1993}
1994
Bram Moolenaar12034e22019-08-25 22:25:02 +02001995 static void
1996popup_close_with_retval(win_T *wp, int retval)
1997{
1998 typval_T res;
1999
2000 res.v_type = VAR_NUMBER;
2001 res.vval.v_number = retval;
2002 popup_close_and_callback(wp, &res);
2003}
2004
Bram Moolenaar3397f742019-06-02 18:40:06 +02002005/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002006 * Close popup "wp" because of a mouse click.
2007 */
2008 void
2009popup_close_for_mouse_click(win_T *wp)
2010{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002011 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002012}
2013
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002014 static void
2015check_mouse_moved(win_T *wp, win_T *mouse_wp)
2016{
2017 // Close the popup when all if these are true:
2018 // - the mouse is not on this popup
2019 // - "mousemoved" was used
2020 // - the mouse is no longer on the same screen row or the mouse column is
2021 // outside of the relevant text
2022 if (wp != mouse_wp
2023 && wp->w_popup_mouse_row != 0
2024 && (wp->w_popup_mouse_row != mouse_row
2025 || mouse_col < wp->w_popup_mouse_mincol
2026 || mouse_col > wp->w_popup_mouse_maxcol))
2027 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002028 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002029 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002030 }
2031}
2032
2033/*
2034 * Called when the mouse moved: may close a popup with "mousemoved".
2035 */
2036 void
2037popup_handle_mouse_moved(void)
2038{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002039 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002040 win_T *mouse_wp;
2041 int row = mouse_row;
2042 int col = mouse_col;
2043
2044 // find the window where the mouse is in
2045 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2046
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002047 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2048 {
2049 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002050 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002051 }
2052 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2053 {
2054 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002055 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002056 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002057}
2058
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002059/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002060 * In a filter: check if the typed key is a mouse event that is used for
2061 * dragging the popup.
2062 */
2063 static void
2064filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2065{
2066 int row = mouse_row;
2067 int col = mouse_col;
2068
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002069 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002070 && is_mouse_key(c)
2071 && (wp == popup_dragwin
2072 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2073 // do not consume the key, allow for dragging the popup
2074 rettv->vval.v_number = 0;
2075}
2076
Bram Moolenaara730e552019-06-16 19:05:31 +02002077/*
2078 * popup_filter_menu({text}, {options})
2079 */
2080 void
2081f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2082{
2083 int id = tv_get_number(&argvars[0]);
2084 win_T *wp = win_id2wp(id);
2085 char_u *key = tv_get_string(&argvars[1]);
2086 typval_T res;
2087 int c;
2088 linenr_T old_lnum;
2089
2090 // If the popup has been closed do not consume the key.
2091 if (wp == NULL)
2092 return;
2093
2094 c = *key;
2095 if (c == K_SPECIAL && key[1] != NUL)
2096 c = TO_SPECIAL(key[1], key[2]);
2097
2098 // consume all keys until done
2099 rettv->vval.v_number = 1;
2100 res.v_type = VAR_NUMBER;
2101
2102 old_lnum = wp->w_cursor.lnum;
2103 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2104 --wp->w_cursor.lnum;
2105 if ((c == 'j' || c == 'J' || c == K_DOWN)
2106 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2107 ++wp->w_cursor.lnum;
2108 if (old_lnum != wp->w_cursor.lnum)
2109 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002110 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002111 return;
2112 }
2113
2114 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2115 {
2116 // Cancelled, invoke callback with -1
2117 res.vval.v_number = -1;
2118 popup_close_and_callback(wp, &res);
2119 return;
2120 }
2121 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2122 {
2123 // Invoke callback with current index.
2124 res.vval.v_number = wp->w_cursor.lnum;
2125 popup_close_and_callback(wp, &res);
2126 return;
2127 }
2128
2129 filter_handle_drag(wp, c, rettv);
2130}
2131
2132/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002133 * popup_filter_yesno({text}, {options})
2134 */
2135 void
2136f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2137{
2138 int id = tv_get_number(&argvars[0]);
2139 win_T *wp = win_id2wp(id);
2140 char_u *key = tv_get_string(&argvars[1]);
2141 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002142 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002143
2144 // If the popup has been closed don't consume the key.
2145 if (wp == NULL)
2146 return;
2147
Bram Moolenaara730e552019-06-16 19:05:31 +02002148 c = *key;
2149 if (c == K_SPECIAL && key[1] != NUL)
2150 c = TO_SPECIAL(key[1], key[2]);
2151
Bram Moolenaara42d9452019-06-15 21:46:30 +02002152 // consume all keys until done
2153 rettv->vval.v_number = 1;
2154
Bram Moolenaara730e552019-06-16 19:05:31 +02002155 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002156 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002157 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002158 res.vval.v_number = 0;
2159 else
2160 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002161 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002162 return;
2163 }
2164
2165 // Invoke callback
2166 res.v_type = VAR_NUMBER;
2167 popup_close_and_callback(wp, &res);
2168}
2169
2170/*
2171 * popup_dialog({text}, {options})
2172 */
2173 void
2174f_popup_dialog(typval_T *argvars, typval_T *rettv)
2175{
2176 popup_create(argvars, rettv, TYPE_DIALOG);
2177}
2178
2179/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002180 * popup_menu({text}, {options})
2181 */
2182 void
2183f_popup_menu(typval_T *argvars, typval_T *rettv)
2184{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002185 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002186}
2187
2188/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002189 * popup_notification({text}, {options})
2190 */
2191 void
2192f_popup_notification(typval_T *argvars, typval_T *rettv)
2193{
2194 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2195}
2196
2197/*
2198 * Find the popup window with window-ID "id".
2199 * If the popup window does not exist NULL is returned.
2200 * If the window is not a popup window, and error message is given.
2201 */
2202 static win_T *
2203find_popup_win(int id)
2204{
2205 win_T *wp = win_id2wp(id);
2206
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002207 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002208 {
2209 semsg(_("E993: window %d is not a popup window"), id);
2210 return NULL;
2211 }
2212 return wp;
2213}
2214
2215/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002216 * popup_close({id})
2217 */
2218 void
2219f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2220{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002221 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002222 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002223
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002224 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002225 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002226}
2227
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002228 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002229popup_hide(win_T *wp)
2230{
2231 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2232 {
2233 wp->w_popup_flags |= POPF_HIDDEN;
2234 --wp->w_buffer->b_nwindows;
2235 redraw_all_later(NOT_VALID);
2236 popup_mask_refresh = TRUE;
2237 }
2238}
2239
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002240/*
2241 * popup_hide({id})
2242 */
2243 void
2244f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2245{
2246 int id = (int)tv_get_number(argvars);
2247 win_T *wp = find_popup_win(id);
2248
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002249 if (wp != NULL)
2250 popup_hide(wp);
2251}
2252
2253 void
2254popup_show(win_T *wp)
2255{
2256 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002257 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002258 wp->w_popup_flags &= ~POPF_HIDDEN;
2259 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002260 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002261 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002262 }
2263}
2264
2265/*
2266 * popup_show({id})
2267 */
2268 void
2269f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2270{
2271 int id = (int)tv_get_number(argvars);
2272 win_T *wp = find_popup_win(id);
2273
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002274 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002275 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002276 popup_show(wp);
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002277 if (wp->w_popup_flags & POPF_INFO)
2278 pum_position_info_popup(wp);
2279 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002280}
2281
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002282/*
2283 * popup_settext({id}, {text})
2284 */
2285 void
2286f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2287{
2288 int id = (int)tv_get_number(&argvars[0]);
2289 win_T *wp = find_popup_win(id);
2290
2291 if (wp != NULL)
2292 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002293 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2294 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2295 else
2296 {
2297 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002298 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002299 popup_adjust_position(wp);
2300 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002301 }
2302}
2303
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002304 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002305popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002306{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002307 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002308 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002309 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002310 clear_cmdline = TRUE;
2311 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002312
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002313 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002314 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002315}
2316
Bram Moolenaarec583842019-05-26 14:11:23 +02002317/*
2318 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002319 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002320 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002321 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002322popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002323{
2324 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002325 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002326 win_T *prev = NULL;
2327
Bram Moolenaarec583842019-05-26 14:11:23 +02002328 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002329 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002330 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002331 {
2332 if (prev == NULL)
2333 first_popupwin = wp->w_next;
2334 else
2335 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002336 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002337 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002338 }
2339
Bram Moolenaarec583842019-05-26 14:11:23 +02002340 // go through tab-local popups
2341 FOR_ALL_TABPAGES(tp)
2342 popup_close_tabpage(tp, id);
2343}
2344
2345/*
2346 * Close a popup window with Window-id "id" in tabpage "tp".
2347 */
2348 void
2349popup_close_tabpage(tabpage_T *tp, int id)
2350{
2351 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002352 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002353 win_T *prev = NULL;
2354
Bram Moolenaarec583842019-05-26 14:11:23 +02002355 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2356 if (wp->w_id == id)
2357 {
2358 if (prev == NULL)
2359 *root = wp->w_next;
2360 else
2361 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002362 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002363 return;
2364 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002365}
2366
2367 void
2368close_all_popups(void)
2369{
2370 while (first_popupwin != NULL)
2371 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002372 while (curtab->tp_first_popupwin != NULL)
2373 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002374}
2375
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002376/*
2377 * popup_move({id}, {options})
2378 */
2379 void
2380f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2381{
Bram Moolenaarae943152019-06-16 22:54:14 +02002382 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002383 int id = (int)tv_get_number(argvars);
2384 win_T *wp = find_popup_win(id);
2385
2386 if (wp == NULL)
2387 return; // invalid {id}
2388
2389 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2390 {
2391 emsg(_(e_dictreq));
2392 return;
2393 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002394 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002395
Bram Moolenaarae943152019-06-16 22:54:14 +02002396 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002397
2398 if (wp->w_winrow + wp->w_height >= cmdline_row)
2399 clear_cmdline = TRUE;
2400 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002401}
2402
Bram Moolenaarbc133542019-05-29 20:26:46 +02002403/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002404 * popup_setoptions({id}, {options})
2405 */
2406 void
2407f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2408{
2409 dict_T *dict;
2410 int id = (int)tv_get_number(argvars);
2411 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002412 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002413
2414 if (wp == NULL)
2415 return; // invalid {id}
2416
2417 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2418 {
2419 emsg(_(e_dictreq));
2420 return;
2421 }
2422 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002423 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002424
2425 apply_move_options(wp, dict);
2426 apply_general_options(wp, dict);
2427
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002428 if (old_firstline != wp->w_firstline)
2429 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002430 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002431 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002432 popup_adjust_position(wp);
2433}
2434
2435/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002436 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002437 */
2438 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002439f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002440{
2441 dict_T *dict;
2442 int id = (int)tv_get_number(argvars);
2443 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002444 int top_extra;
2445 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002446
2447 if (rettv_dict_alloc(rettv) == OK)
2448 {
2449 if (wp == NULL)
2450 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002451 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002452 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2453
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002454 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002455 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002456 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002457
Bram Moolenaarbc133542019-05-29 20:26:46 +02002458 dict_add_number(dict, "line", wp->w_winrow + 1);
2459 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002460 dict_add_number(dict, "width", wp->w_width + left_extra
2461 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2462 dict_add_number(dict, "height", wp->w_height + top_extra
2463 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002464
2465 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2466 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2467 dict_add_number(dict, "core_width", wp->w_width);
2468 dict_add_number(dict, "core_height", wp->w_height);
2469
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002470 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002471 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002472 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002473 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002474
2475 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002476 }
2477}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002478/*
2479 * popup_locate({row}, {col})
2480 */
2481 void
2482f_popup_locate(typval_T *argvars, typval_T *rettv)
2483{
2484 int row = tv_get_number(&argvars[0]) - 1;
2485 int col = tv_get_number(&argvars[1]) - 1;
2486 win_T *wp;
2487
2488 wp = mouse_find_win(&row, &col, FIND_POPUP);
2489 if (WIN_IS_POPUP(wp))
2490 rettv->vval.v_number = wp->w_id;
2491}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002492
2493/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002494 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2495 */
2496 static void
2497get_padding_border(dict_T *dict, int *array, char *name)
2498{
2499 list_T *list;
2500 int i;
2501
2502 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2503 return;
2504
2505 list = list_alloc();
2506 if (list != NULL)
2507 {
2508 dict_add_list(dict, name, list);
2509 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2510 for (i = 0; i < 4; ++i)
2511 list_append_number(list, array[i]);
2512 }
2513}
2514
2515/*
2516 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2517 */
2518 static void
2519get_borderhighlight(dict_T *dict, win_T *wp)
2520{
2521 list_T *list;
2522 int i;
2523
2524 for (i = 0; i < 4; ++i)
2525 if (wp->w_border_highlight[i] != NULL)
2526 break;
2527 if (i == 4)
2528 return;
2529
2530 list = list_alloc();
2531 if (list != NULL)
2532 {
2533 dict_add_list(dict, "borderhighlight", list);
2534 for (i = 0; i < 4; ++i)
2535 list_append_string(list, wp->w_border_highlight[i], -1);
2536 }
2537}
2538
2539/*
2540 * For popup_getoptions(): add a "borderchars" entry to "dict".
2541 */
2542 static void
2543get_borderchars(dict_T *dict, win_T *wp)
2544{
2545 list_T *list;
2546 int i;
2547 char_u buf[NUMBUFLEN];
2548 int len;
2549
2550 for (i = 0; i < 8; ++i)
2551 if (wp->w_border_char[i] != 0)
2552 break;
2553 if (i == 8)
2554 return;
2555
2556 list = list_alloc();
2557 if (list != NULL)
2558 {
2559 dict_add_list(dict, "borderchars", list);
2560 for (i = 0; i < 8; ++i)
2561 {
2562 len = mb_char2bytes(wp->w_border_char[i], buf);
2563 list_append_string(list, buf, len);
2564 }
2565 }
2566}
2567
2568/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002569 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002570 */
2571 static void
2572get_moved_list(dict_T *dict, win_T *wp)
2573{
2574 list_T *list;
2575
2576 list = list_alloc();
2577 if (list != NULL)
2578 {
2579 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002580 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002581 list_append_number(list, wp->w_popup_mincol);
2582 list_append_number(list, wp->w_popup_maxcol);
2583 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002584 list = list_alloc();
2585 if (list != NULL)
2586 {
2587 dict_add_list(dict, "mousemoved", list);
2588 list_append_number(list, wp->w_popup_mouse_row);
2589 list_append_number(list, wp->w_popup_mouse_mincol);
2590 list_append_number(list, wp->w_popup_mouse_maxcol);
2591 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002592}
2593
2594/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002595 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002596 */
2597 void
2598f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2599{
2600 dict_T *dict;
2601 int id = (int)tv_get_number(argvars);
2602 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002603 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002604 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002605
2606 if (rettv_dict_alloc(rettv) == OK)
2607 {
2608 if (wp == NULL)
2609 return;
2610
2611 dict = rettv->vval.v_dict;
2612 dict_add_number(dict, "line", wp->w_wantline);
2613 dict_add_number(dict, "col", wp->w_wantcol);
2614 dict_add_number(dict, "minwidth", wp->w_minwidth);
2615 dict_add_number(dict, "minheight", wp->w_minheight);
2616 dict_add_number(dict, "maxheight", wp->w_maxheight);
2617 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002618 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002619 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002620 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002621 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002622 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2623 {
2624 proptype_T *pt = text_prop_type_by_id(
2625 wp->w_popup_prop_win->w_buffer,
2626 wp->w_popup_prop_type);
2627
2628 if (pt != NULL)
2629 dict_add_string(dict, "textprop", pt->pt_name);
2630 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2631 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2632 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002633 dict_add_string(dict, "title", wp->w_popup_title);
2634 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002635 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002636 dict_add_number(dict, "mapping",
2637 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002638 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002639 dict_add_number(dict, "cursorline",
2640 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002641 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002642 if (wp->w_scrollbar_highlight != NULL)
2643 dict_add_string(dict, "scrollbarhighlight",
2644 wp->w_scrollbar_highlight);
2645 if (wp->w_thumb_highlight != NULL)
2646 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002647
Bram Moolenaara3fce622019-06-20 02:31:49 +02002648 // find the tabpage that holds this popup
2649 i = 1;
2650 FOR_ALL_TABPAGES(tp)
2651 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002652 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002653
Bram Moolenaar1824f452019-10-02 23:06:46 +02002654 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2655 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002656 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002657 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002658 break;
2659 ++i;
2660 }
2661 if (tp == NULL)
2662 i = -1; // must be global
2663 else if (tp == curtab)
2664 i = 0;
2665 dict_add_number(dict, "tabpage", i);
2666
Bram Moolenaarae943152019-06-16 22:54:14 +02002667 get_padding_border(dict, wp->w_popup_padding, "padding");
2668 get_padding_border(dict, wp->w_popup_border, "border");
2669 get_borderhighlight(dict, wp);
2670 get_borderchars(dict, wp);
2671 get_moved_list(dict, wp);
2672
2673 if (wp->w_filter_cb.cb_name != NULL)
2674 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2675 if (wp->w_close_cb.cb_name != NULL)
2676 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002677
2678 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2679 ++i)
2680 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2681 {
2682 dict_add_string(dict, "pos",
2683 (char_u *)poppos_entries[i].pp_name);
2684 break;
2685 }
2686
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002687 dict_add_string(dict, "close", (char_u *)(
2688 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2689 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2690
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002691# if defined(FEAT_TIMERS)
2692 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2693 ? (long)wp->w_popup_timer->tr_interval : 0L);
2694# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002695 }
2696}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002697
2698 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002699error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002700{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002701 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002702 {
2703 emsg(_("E994: Not allowed in a popup window"));
2704 return TRUE;
2705 }
2706 return FALSE;
2707}
2708
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002709/*
2710 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002711 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002712 */
2713 void
2714popup_reset_handled()
2715{
2716 win_T *wp;
2717
2718 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2719 wp->w_popup_flags &= ~POPF_HANDLED;
2720 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2721 wp->w_popup_flags &= ~POPF_HANDLED;
2722}
2723
2724/*
2725 * Find the next visible popup where POPF_HANDLED is not set.
2726 * Must have called popup_reset_handled() first.
2727 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2728 * popup with the highest zindex.
2729 */
2730 win_T *
2731find_next_popup(int lowest)
2732{
2733 win_T *wp;
2734 win_T *found_wp;
2735 int found_zindex;
2736
2737 found_zindex = lowest ? INT_MAX : 0;
2738 found_wp = NULL;
2739 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2740 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2741 && (lowest ? wp->w_zindex < found_zindex
2742 : wp->w_zindex > found_zindex))
2743 {
2744 found_zindex = wp->w_zindex;
2745 found_wp = wp;
2746 }
2747 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2748 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2749 && (lowest ? wp->w_zindex < found_zindex
2750 : wp->w_zindex > found_zindex))
2751 {
2752 found_zindex = wp->w_zindex;
2753 found_wp = wp;
2754 }
2755
2756 if (found_wp != NULL)
2757 found_wp->w_popup_flags |= POPF_HANDLED;
2758 return found_wp;
2759}
2760
2761/*
2762 * Invoke the filter callback for window "wp" with typed character "c".
2763 * Uses the global "mod_mask" for modifiers.
2764 * Returns the return value of the filter.
2765 * Careful: The filter may make "wp" invalid!
2766 */
2767 static int
2768invoke_popup_filter(win_T *wp, int c)
2769{
2770 int res;
2771 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002772 typval_T argv[3];
2773 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002774 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002775
Bram Moolenaara42d9452019-06-15 21:46:30 +02002776 // Emergency exit: CTRL-C closes the popup.
2777 if (c == Ctrl_C)
2778 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002779 int save_got_int = got_int;
2780
2781 // Reset got_int to avoid the callback isn't called.
2782 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002783 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002784 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002785 return 1;
2786 }
2787
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002788 argv[0].v_type = VAR_NUMBER;
2789 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2790
2791 // Convert the number to a string, so that the function can use:
2792 // if a:c == "\<F2>"
2793 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2794 argv[1].v_type = VAR_STRING;
2795 argv[1].vval.v_string = vim_strsave(buf);
2796
2797 argv[2].v_type = VAR_UNKNOWN;
2798
Bram Moolenaara42d9452019-06-15 21:46:30 +02002799 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002800 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002801 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002802 popup_highlight_curline(wp);
2803
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002804 res = tv_get_number(&rettv);
2805 vim_free(argv[1].vval.v_string);
2806 clear_tv(&rettv);
2807 return res;
2808}
2809
2810/*
2811 * Called when "c" was typed: invoke popup filter callbacks.
2812 * Returns TRUE when the character was consumed,
2813 */
2814 int
2815popup_do_filter(int c)
2816{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002817 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002818 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002819 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002820 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002821 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002822 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002823
2824 if (recursive)
2825 return FALSE;
2826 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002827
2828 popup_reset_handled();
2829
Bram Moolenaarf6396232019-08-24 19:36:00 +02002830 if (c == K_LEFTMOUSE)
2831 {
2832 int row = mouse_row;
2833 int col = mouse_col;
2834
2835 wp = mouse_find_win(&row, &col, FIND_POPUP);
2836 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002837 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002838 }
2839
Bram Moolenaar581ba392019-09-03 22:08:33 +02002840 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002841 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002842 if (wp->w_filter_cb.cb_name != NULL
2843 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002844 res = invoke_popup_filter(wp, c);
2845
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002846 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002847 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002848 recursive = FALSE;
2849 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002850 return res;
2851}
2852
Bram Moolenaar3397f742019-06-02 18:40:06 +02002853/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002854 * Return TRUE if there is a popup visible with a filter callback and the
2855 * "mapping" property off.
2856 */
2857 int
2858popup_no_mapping(void)
2859{
2860 int round;
2861 win_T *wp;
2862
2863 for (round = 1; round <= 2; ++round)
2864 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2865 wp != NULL; wp = wp->w_next)
2866 if (wp->w_filter_cb.cb_name != NULL
2867 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2868 return TRUE;
2869 return FALSE;
2870}
2871
2872/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002873 * Called when the cursor moved: check if any popup needs to be closed if the
2874 * cursor moved far enough.
2875 */
2876 void
2877popup_check_cursor_pos()
2878{
2879 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002880
2881 popup_reset_handled();
2882 while ((wp = find_next_popup(TRUE)) != NULL)
2883 if (wp->w_popup_curwin != NULL
2884 && (curwin != wp->w_popup_curwin
2885 || curwin->w_cursor.lnum != wp->w_popup_lnum
2886 || curwin->w_cursor.col < wp->w_popup_mincol
2887 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002888 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002889}
2890
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002891/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002892 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002893 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002894 static void
2895popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002896{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002897 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002898 char_u *cells;
2899 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002900
2901 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002902 return;
2903 if (wp->w_popup_mask_cells != NULL
2904 && wp->w_popup_mask_height == height
2905 && wp->w_popup_mask_width == width)
2906 return; // cache is still valid
2907
2908 vim_free(wp->w_popup_mask_cells);
2909 wp->w_popup_mask_cells = alloc_clear(width * height);
2910 if (wp->w_popup_mask_cells == NULL)
2911 return;
2912 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002913
2914 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2915 {
2916 int cols, cole;
2917 int lines, linee;
2918
2919 li = lio->li_tv.vval.v_list->lv_first;
2920 cols = tv_get_number(&li->li_tv);
2921 if (cols < 0)
2922 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002923 li = li->li_next;
2924 cole = tv_get_number(&li->li_tv);
2925 if (cole < 0)
2926 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002927 li = li->li_next;
2928 lines = tv_get_number(&li->li_tv);
2929 if (lines < 0)
2930 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002931 li = li->li_next;
2932 linee = tv_get_number(&li->li_tv);
2933 if (linee < 0)
2934 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002935
2936 for (row = lines - 1; row < linee && row < height; ++row)
2937 for (col = cols - 1; col < cole && col < width; ++col)
2938 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002939 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002940}
2941
2942/*
2943 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2944 * "col" and "line" are screen coordinates.
2945 */
2946 static int
2947popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2948{
2949 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2950 int line = screenline - wp->w_winrow;
2951
2952 return col >= 0 && col < width
2953 && line >= 0 && line < height
2954 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002955}
2956
2957/*
2958 * Set flags in popup_transparent[] for window "wp" to "val".
2959 */
2960 static void
2961update_popup_transparent(win_T *wp, int val)
2962{
2963 if (wp->w_popup_mask != NULL)
2964 {
2965 int width = popup_width(wp);
2966 int height = popup_height(wp);
2967 listitem_T *lio, *li;
2968 int cols, cole;
2969 int lines, linee;
2970 int col, line;
2971
2972 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2973 {
2974 li = lio->li_tv.vval.v_list->lv_first;
2975 cols = tv_get_number(&li->li_tv);
2976 if (cols < 0)
2977 cols = width + cols + 1;
2978 li = li->li_next;
2979 cole = tv_get_number(&li->li_tv);
2980 if (cole < 0)
2981 cole = width + cole + 1;
2982 li = li->li_next;
2983 lines = tv_get_number(&li->li_tv);
2984 if (lines < 0)
2985 lines = height + lines + 1;
2986 li = li->li_next;
2987 linee = tv_get_number(&li->li_tv);
2988 if (linee < 0)
2989 linee = height + linee + 1;
2990
2991 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002992 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002993 if (cols < 0)
2994 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002995 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002996 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002997 if (lines < 0)
2998 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002999 for (line = lines; line < linee
3000 && line + wp->w_winrow < screen_Rows; ++line)
3001 for (col = cols; col < cole
3002 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003003 popup_transparent[(line + wp->w_winrow) * screen_Columns
3004 + col + wp->w_wincol] = val;
3005 }
3006 }
3007}
3008
3009/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003010 * Only called when popup window "wp" is hidden: If the window is positioned
3011 * next to a text property, and it is now visible, then unhide the popup.
3012 * We don't check if visible popups become hidden, that is done in
3013 * popup_adjust_position().
3014 * Return TRUE if the popup became unhidden.
3015 */
3016 static int
3017check_popup_unhidden(win_T *wp)
3018{
3019 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3020 {
3021 textprop_T prop;
3022 linenr_T lnum;
3023
3024 if (find_visible_prop(wp->w_popup_prop_win,
3025 wp->w_popup_prop_type, wp->w_popup_prop_id,
3026 &prop, &lnum) == OK)
3027 {
3028 wp->w_popup_flags &= ~POPF_HIDDEN;
3029 ++wp->w_buffer->b_nwindows;
3030 wp->w_popup_prop_topline = 0; // force repositioning
3031 return TRUE;
3032 }
3033 }
3034 return FALSE;
3035}
3036
3037/*
3038 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3039 * That is when the buffer in the popup was changed, or the popup is following
3040 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003041 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003042 */
3043 static int
3044popup_need_position_adjust(win_T *wp)
3045{
3046 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3047 return TRUE;
3048 if (win_valid(wp->w_popup_prop_win))
3049 return wp->w_popup_prop_changedtick
3050 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003051 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3052 || ((wp->w_popup_flags & POPF_CURSORLINE)
3053 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003054 return FALSE;
3055}
3056
3057/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003058 * Update "popup_mask" if needed.
3059 * Also recomputes the popup size and positions.
3060 * Also updates "popup_visible".
3061 * Also marks window lines for redrawing.
3062 */
3063 void
3064may_update_popup_mask(int type)
3065{
3066 win_T *wp;
3067 short *mask;
3068 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003069 int redraw_all_popups = FALSE;
3070 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003071
3072 // Need to recompute when switching tabs.
3073 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3074 // (such as the screen size) must have changed.
3075 if (popup_mask_tab != curtab || type >= NOT_VALID)
3076 {
3077 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003078 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003079 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003080
3081 // Check if any popup window buffer has changed and if any popup connected
3082 // to a text property has become visible.
3083 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3084 if (wp->w_popup_flags & POPF_HIDDEN)
3085 popup_mask_refresh |= check_popup_unhidden(wp);
3086 else if (popup_need_position_adjust(wp))
3087 popup_mask_refresh = TRUE;
3088 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3089 if (wp->w_popup_flags & POPF_HIDDEN)
3090 popup_mask_refresh |= check_popup_unhidden(wp);
3091 else if (popup_need_position_adjust(wp))
3092 popup_mask_refresh = TRUE;
3093
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003094 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003095 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003096
3097 // Need to update the mask, something has changed.
3098 popup_mask_refresh = FALSE;
3099 popup_mask_tab = curtab;
3100 popup_visible = FALSE;
3101
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003102 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003103 // If redrawing only what is needed, update "popup_mask_next" and then
3104 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003105 redrawing_all_win = TRUE;
3106 FOR_ALL_WINDOWS(wp)
3107 if (wp->w_redr_type < SOME_VALID)
3108 redrawing_all_win = FALSE;
3109 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003110 mask = popup_mask;
3111 else
3112 mask = popup_mask_next;
3113 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3114
3115 // Find the window with the lowest zindex that hasn't been handled yet,
3116 // so that the window with a higher zindex overwrites the value in
3117 // popup_mask.
3118 popup_reset_handled();
3119 while ((wp = find_next_popup(TRUE)) != NULL)
3120 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003121 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003122 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003123
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003124 popup_visible = TRUE;
3125
Bram Moolenaar12034e22019-08-25 22:25:02 +02003126 // Recompute the position if the text changed. It may make the popup
3127 // hidden if it's attach to a text property that is no longer visible.
3128 if (redraw_all_popups || popup_need_position_adjust(wp))
3129 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003130 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003131 if (wp->w_popup_flags & POPF_HIDDEN)
3132 continue;
3133 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003134
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003135 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003136 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003137 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003138 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003139 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003140 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003141 col < wp->w_wincol + width - wp->w_popup_leftoff
3142 && col < screen_Columns; ++col)
3143 if (wp->w_popup_mask_cells == NULL
3144 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003145 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003146 }
3147
3148 // Only check which lines are to be updated if not already
3149 // updating all lines.
3150 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003151 {
3152 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3153 win_T *prev_wp = NULL;
3154
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003155 for (line = 0; line < screen_Rows; ++line)
3156 {
3157 int col_done = 0;
3158
3159 for (col = 0; col < screen_Columns; ++col)
3160 {
3161 int off = line * screen_Columns + col;
3162
3163 if (popup_mask[off] != popup_mask_next[off])
3164 {
3165 popup_mask[off] = popup_mask_next[off];
3166
3167 if (line >= cmdline_row)
3168 {
3169 // the command line needs to be cleared if text below
3170 // the popup is now visible.
3171 if (!msg_scrolled && popup_mask_next[off] == 0)
3172 clear_cmdline = TRUE;
3173 }
3174 else if (col >= col_done)
3175 {
3176 linenr_T lnum;
3177 int line_cp = line;
3178 int col_cp = col;
3179
3180 // The screen position "line" / "col" needs to be
3181 // redrawn. Figure out what window that is and update
3182 // w_redraw_top and w_redr_bot. Only needs to be done
3183 // once for each window line.
3184 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3185 if (wp != NULL)
3186 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003187 if (wp != prev_wp)
3188 {
3189 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3190 prev_wp = wp;
3191 }
3192
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003193 if (line_cp >= wp->w_height)
3194 // In (or below) status line
3195 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003196 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003197 {
3198 // compute the position in the buffer line from
3199 // the position in the window
3200 mouse_comp_pos(wp, &line_cp, &col_cp,
3201 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003202 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003203 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003204
3205 // This line is going to be redrawn, no need to
3206 // check until the right side of the window.
3207 col_done = wp->w_wincol + wp->w_width - 1;
3208 }
3209 }
3210 }
3211 }
3212 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003213
3214 vim_free(plines_cache);
3215 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003216}
3217
3218/*
3219 * Return a string of "len" spaces in IObuff.
3220 */
3221 static char_u *
3222get_spaces(int len)
3223{
3224 vim_memset(IObuff, ' ', (size_t)len);
3225 IObuff[len] = NUL;
3226 return IObuff;
3227}
3228
3229/*
3230 * Update popup windows. They are drawn on top of normal windows.
3231 * "win_update" is called for each popup window, lowest zindex first.
3232 */
3233 void
3234update_popups(void (*win_update)(win_T *wp))
3235{
3236 win_T *wp;
3237 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003238 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003239 int total_width;
3240 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003241 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003242 int popup_attr;
3243 int border_attr[4];
3244 int border_char[8];
3245 char_u buf[MB_MAXBYTES];
3246 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003247 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003248 int padcol = 0;
3249 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003250 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003251 int sb_thumb_top = 0;
3252 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003253 int attr_scroll = 0;
3254 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003255
3256 // Find the window with the lowest zindex that hasn't been updated yet,
3257 // so that the window with a higher zindex is drawn later, thus goes on
3258 // top.
3259 popup_reset_handled();
3260 while ((wp = find_next_popup(TRUE)) != NULL)
3261 {
3262 // This drawing uses the zindex of the popup window, so that it's on
3263 // top of the text but doesn't draw when another popup with higher
3264 // zindex is on top of the character.
3265 screen_zindex = wp->w_zindex;
3266
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003267 // Set flags in popup_transparent[] for masked cells.
3268 update_popup_transparent(wp, 1);
3269
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003270 // adjust w_winrow and w_wincol for border and padding, since
3271 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003272 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003273 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3274 - wp->w_popup_leftoff;
3275 if (wp->w_wincol + left_extra < 0)
3276 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003277 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003278 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003279
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003280 // Draw the popup text, unless it's off screen.
3281 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003282 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003283 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003284
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003285 // move the cursor into the visible lines, otherwise executing
3286 // commands with win_execute() may cause the text to jump.
3287 if (wp->w_cursor.lnum < wp->w_topline)
3288 wp->w_cursor.lnum = wp->w_topline;
3289 else if (wp->w_cursor.lnum >= wp->w_botline)
3290 wp->w_cursor.lnum = wp->w_botline - 1;
3291 }
3292
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 Moolenaard529ba52019-07-02 23:13:53 +02003296 total_width = popup_width(wp);
3297 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003298 popup_attr = get_wcr_attr(wp);
3299
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003300 if (wp->w_winrow + total_height > cmdline_row)
3301 wp->w_popup_flags |= POPF_ON_CMDLINE;
3302 else
3303 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3304
3305
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003306 // We can only use these line drawing characters when 'encoding' is
3307 // "utf-8" and 'ambiwidth' is "single".
3308 if (enc_utf8 && *p_ambw == 's')
3309 {
3310 border_char[0] = border_char[2] = 0x2550;
3311 border_char[1] = border_char[3] = 0x2551;
3312 border_char[4] = 0x2554;
3313 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003314 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3315 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003316 border_char[7] = 0x255a;
3317 }
3318 else
3319 {
3320 border_char[0] = border_char[2] = '-';
3321 border_char[1] = border_char[3] = '|';
3322 for (i = 4; i < 8; ++i)
3323 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003324 if (wp->w_popup_flags & POPF_RESIZE)
3325 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003326 }
3327 for (i = 0; i < 8; ++i)
3328 if (wp->w_border_char[i] != 0)
3329 border_char[i] = wp->w_border_char[i];
3330
3331 for (i = 0; i < 4; ++i)
3332 {
3333 border_attr[i] = popup_attr;
3334 if (wp->w_border_highlight[i] != NULL)
3335 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3336 }
3337
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003338 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003339 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003340 if (wp->w_popup_border[0] > 0)
3341 {
3342 // top border
3343 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003344 wincol < 0 ? 0 : wincol, wincol + total_width,
3345 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003346 ? border_char[4] : border_char[0],
3347 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003348 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003349 {
3350 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3351 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003352 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003353 }
3354 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003355 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3356 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003357
Bram Moolenaard529ba52019-07-02 23:13:53 +02003358 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3359 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003360 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003361 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3362 - wp->w_has_scrollbar;
3363 if (padcol < 0)
3364 {
3365 padwidth += padcol;
3366 padcol = 0;
3367 }
3368 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003369 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003370 {
3371 // top padding
3372 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003373 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003374 ' ', ' ', popup_attr);
3375 }
3376
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003377 // Title goes on top of border or padding.
3378 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003379 {
3380 int len = (int)STRLEN(wp->w_popup_title) + 1;
3381 char_u *title = alloc(len);
3382
3383 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3384 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003385 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003386 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003387 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003388
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003389 // Compute scrollbar thumb position and size.
3390 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003391 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003392 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3393 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003394
Bram Moolenaar076d9882019-09-14 22:23:29 +02003395 sb_thumb_height = (height * height + linecount / 2) / linecount;
3396 if (wp->w_topline > 1 && sb_thumb_height == height)
3397 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003398 if (sb_thumb_height == 0)
3399 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003400 if (linecount <= wp->w_height)
3401 // it just fits, avoid divide by zero
3402 sb_thumb_top = 0;
3403 else
3404 sb_thumb_top = (wp->w_topline - 1
3405 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003406 * (wp->w_height - sb_thumb_height)
3407 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003408 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3409 sb_thumb_top = 1; // show it's scrolled
3410
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003411 if (wp->w_scrollbar_highlight != NULL)
3412 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3413 else
3414 attr_scroll = highlight_attr[HLF_PSB];
3415 if (wp->w_thumb_highlight != NULL)
3416 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3417 else
3418 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003419 }
3420
3421 for (i = wp->w_popup_border[0];
3422 i < total_height - wp->w_popup_border[2]; ++i)
3423 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003424 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003425 // left and right padding only needed next to the body
3426 int do_padding =
3427 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3428 && i < total_height - wp->w_popup_border[2]
3429 - wp->w_popup_padding[2];
3430
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003431 row = wp->w_winrow + i;
3432
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003433 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003434 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003435 {
3436 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003437 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003438 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003439 if (do_padding && wp->w_popup_padding[3] > 0)
3440 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003441 int col = wincol + wp->w_popup_border[3];
3442
Bram Moolenaard529ba52019-07-02 23:13:53 +02003443 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003444 pad_left = wp->w_popup_padding[3];
3445 if (col < 0)
3446 {
3447 pad_left += col;
3448 col = 0;
3449 }
3450 if (pad_left > 0)
3451 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3452 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003453 // scrollbar
3454 if (wp->w_has_scrollbar)
3455 {
3456 int line = i - top_off;
3457 int scroll_col = wp->w_wincol + total_width - 1
3458 - wp->w_popup_border[1];
3459
3460 if (line >= 0 && line < wp->w_height)
3461 screen_putchar(' ', row, scroll_col,
3462 line >= sb_thumb_top
3463 && line < sb_thumb_top + sb_thumb_height
3464 ? attr_thumb : attr_scroll);
3465 else
3466 screen_putchar(' ', row, scroll_col, popup_attr);
3467 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003468 // right border
3469 if (wp->w_popup_border[1] > 0)
3470 {
3471 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003472 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003473 }
3474 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003475 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003476 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003477 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003478 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3479 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003480 }
3481
3482 if (wp->w_popup_padding[2] > 0)
3483 {
3484 // bottom padding
3485 row = wp->w_winrow + wp->w_popup_border[0]
3486 + wp->w_popup_padding[0] + wp->w_height;
3487 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003488 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003489 }
3490
3491 if (wp->w_popup_border[2] > 0)
3492 {
3493 // bottom border
3494 row = wp->w_winrow + total_height - 1;
3495 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003496 wincol < 0 ? 0 : wincol,
3497 wincol + total_width,
3498 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003499 ? border_char[7] : border_char[2],
3500 border_char[2], border_attr[2]);
3501 if (wp->w_popup_border[1] > 0)
3502 {
3503 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003504 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003505 }
3506 }
3507
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003508 if (wp->w_popup_close == POPCLOSE_BUTTON)
3509 {
3510 // close button goes on top of anything at the top-right corner
3511 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003512 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003513 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3514 }
3515
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003516 update_popup_transparent(wp, 0);
3517
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003518 // Back to the normal zindex.
3519 screen_zindex = 0;
3520 }
3521}
3522
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003523/*
3524 * Mark references in callbacks of one popup window.
3525 */
3526 static int
3527set_ref_in_one_popup(win_T *wp, int copyID)
3528{
3529 int abort = FALSE;
3530 typval_T tv;
3531
3532 if (wp->w_close_cb.cb_partial != NULL)
3533 {
3534 tv.v_type = VAR_PARTIAL;
3535 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3536 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3537 }
3538 if (wp->w_filter_cb.cb_partial != NULL)
3539 {
3540 tv.v_type = VAR_PARTIAL;
3541 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3542 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3543 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003544 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003545 return abort;
3546}
3547
3548/*
3549 * Set reference in callbacks of popup windows.
3550 */
3551 int
3552set_ref_in_popups(int copyID)
3553{
3554 int abort = FALSE;
3555 win_T *wp;
3556 tabpage_T *tp;
3557
3558 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3559 abort = abort || set_ref_in_one_popup(wp, copyID);
3560
3561 FOR_ALL_TABPAGES(tp)
3562 {
3563 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3564 abort = abort || set_ref_in_one_popup(wp, copyID);
3565 if (abort)
3566 break;
3567 }
3568 return abort;
3569}
Bram Moolenaar79648732019-07-18 21:43:07 +02003570
3571/*
3572 * Find an existing popup used as the preview window, in the current tab page.
3573 * Return NULL if not found.
3574 */
3575 win_T *
3576popup_find_preview_window(void)
3577{
3578 win_T *wp;
3579
3580 // Preview window popup is always local to tab page.
3581 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3582 if (wp->w_p_pvw)
3583 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003584 return NULL;
3585}
3586
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003587 int
3588popup_is_popup(win_T *wp)
3589{
3590 return wp->w_popup_flags != 0;
3591}
3592
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003593#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003594/*
3595 * Find an existing popup used as the info window, in the current tab page.
3596 * Return NULL if not found.
3597 */
3598 win_T *
3599popup_find_info_window(void)
3600{
3601 win_T *wp;
3602
3603 // info window popup is always local to tab page.
3604 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3605 if (wp->w_popup_flags & POPF_INFO)
3606 return wp;
3607 return NULL;
3608}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003609#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003610
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003611 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003612f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3613{
3614 win_T *wp = popup_find_info_window();
3615
3616 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3617}
3618
3619 void
3620f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003621{
3622 win_T *wp = popup_find_preview_window();
3623
3624 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003625}
3626
Bram Moolenaar79648732019-07-18 21:43:07 +02003627/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003628 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003629 * NOTE: this makes the popup the current window, so that the file can be
3630 * edited. However, it must not remain to be the current window, the caller
3631 * must make sure of that.
3632 */
3633 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003634popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003635{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003636 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003637
3638 if (wp == NULL)
3639 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003640 if (info)
3641 wp->w_popup_flags |= POPF_INFO;
3642 else
3643 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003644
3645 // Set the width to a reasonable value, so that w_topline can be computed.
3646 if (wp->w_minwidth > 0)
3647 wp->w_width = wp->w_minwidth;
3648 else if (wp->w_maxwidth > 0)
3649 wp->w_width = wp->w_maxwidth;
3650 else
3651 wp->w_width = curwin->w_width;
3652
3653 // Will switch to another buffer soon, dummy one can be wiped.
3654 wp->w_buffer->b_locked = FALSE;
3655
3656 win_enter(wp, FALSE);
3657 return OK;
3658}
3659
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003660#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003661/*
3662 * Close any preview popup.
3663 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003664 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003665popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003666{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003667 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003668
3669 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003670 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003671}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003672
3673/*
3674 * Hide the info popup.
3675 */
3676 void
3677popup_hide_info(void)
3678{
3679 win_T *wp = popup_find_info_window();
3680
3681 if (wp != NULL)
3682 popup_hide(wp);
3683}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003684#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003685
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003686/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003687 * Close any popup for a text property associated with "win".
3688 * Return TRUE if a popup was closed.
3689 */
3690 int
3691popup_win_closed(win_T *win)
3692{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003693 int round;
3694 win_T *wp;
3695 win_T *next;
3696 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003697
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003698 for (round = 1; round <= 2; ++round)
3699 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3700 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003701 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003702 next = wp->w_next;
3703 if (wp->w_popup_prop_win == win)
3704 {
3705 popup_close_with_retval(wp, -1);
3706 ret = TRUE;
3707 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003708 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003709 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003710}
3711
3712/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003713 * Set the title of the popup window to the file name.
3714 */
3715 void
3716popup_set_title(win_T *wp)
3717{
3718 if (wp->w_buffer->b_fname != NULL)
3719 {
3720 char_u dirname[MAXPATHL];
3721 size_t len;
3722
3723 mch_dirname(dirname, MAXPATHL);
3724 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3725
3726 vim_free(wp->w_popup_title);
3727 len = STRLEN(wp->w_buffer->b_fname) + 3;
3728 wp->w_popup_title = alloc((int)len);
3729 if (wp->w_popup_title != NULL)
3730 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3731 wp->w_buffer->b_fname);
3732 redraw_win_later(wp, VALID);
3733 }
3734}
3735
3736/*
3737 * If there is a preview window, update the title.
3738 * Used after changing directory.
3739 */
3740 void
3741popup_update_preview_title(void)
3742{
3743 win_T *wp = popup_find_preview_window();
3744
3745 if (wp != NULL)
3746 popup_set_title(wp);
3747}
3748
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003749#endif // FEAT_TEXT_PROP