blob: bf1205517fee2cad87100709549b3c4ef311b058 [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".
36 */
37 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020038popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020039{
40 dictitem_T *di;
41 char_u *val;
42 char_u *s;
43 char_u *endp;
44 int n = 0;
45
46 di = dict_find(dict, key, -1);
47 if (di == NULL)
48 return 0;
49
50 val = tv_get_string(&di->di_tv);
51 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020052 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020053
54 setcursor_mayforce(TRUE);
55 s = val + 6;
56 if (*s != NUL)
57 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 endp = s;
59 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
60 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020061 if (endp != NULL && *skipwhite(endp) != NUL)
62 {
63 semsg(_(e_invexpr2), val);
64 return 0;
65 }
66 }
67
68 if (STRCMP(key, "line") == 0)
69 n = screen_screenrow() + 1 + n;
70 else // "col"
71 n = screen_screencol() + 1 + n;
72
73 if (n < 1)
74 n = 1;
75 return n;
76}
77
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020078 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020079set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080{
81 dictitem_T *di;
82
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020083 di = dict_find(dict, (char_u *)name, -1);
84 if (di != NULL)
85 {
86 if (di->di_tv.v_type != VAR_LIST)
87 emsg(_(e_listreq));
88 else
89 {
90 list_T *list = di->di_tv.vval.v_list;
91 listitem_T *li;
92 int i;
93 int nr;
94
95 for (i = 0; i < 4; ++i)
96 array[i] = 1;
97 if (list != NULL)
98 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
99 ++i, li = li->li_next)
100 {
101 nr = (int)tv_get_number(&li->li_tv);
102 if (nr >= 0)
103 array[i] = nr > max_val ? max_val : nr;
104 }
105 }
106 }
107}
108
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200109/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200110 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200111 */
112 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200113set_moved_values(win_T *wp)
114{
115 wp->w_popup_curwin = curwin;
116 wp->w_popup_lnum = curwin->w_cursor.lnum;
117 wp->w_popup_mincol = curwin->w_cursor.col;
118 wp->w_popup_maxcol = curwin->w_cursor.col;
119}
120
121/*
122 * Used when popup options contain "moved" with "word" or "WORD".
123 */
124 static void
125set_moved_columns(win_T *wp, int flags)
126{
127 char_u *ptr;
128 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
129
130 if (len > 0)
131 {
132 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
133 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
134 }
135}
136
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200137/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200138 * Used when popup options contain "mousemoved": set default moved values.
139 */
140 static void
141set_mousemoved_values(win_T *wp)
142{
143 wp->w_popup_mouse_row = mouse_row;
144 wp->w_popup_mouse_mincol = mouse_col;
145 wp->w_popup_mouse_maxcol = mouse_col;
146}
147
148/*
149 * Used when popup options contain "moved" with "word" or "WORD".
150 */
151 static void
152set_mousemoved_columns(win_T *wp, int flags)
153{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200154 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200155 char_u *text;
156 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200157 pos_T pos;
158 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200159
160 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200161 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200162 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200163 // convert text column to mouse column
164 pos.col = col;
165 pos.coladd = 0;
166 getvcol(textwp, &pos, &mcol, NULL, NULL);
167 wp->w_popup_mouse_mincol = mcol;
168
Bram Moolenaar10727682019-07-12 13:59:20 +0200169 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200170 getvcol(textwp, &pos, NULL, NULL, &mcol);
171 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200172 vim_free(text);
173 }
174}
175
176/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200177 * Return TRUE if "row"/"col" is on the border of the popup.
178 * The values are relative to the top-left corner.
179 */
180 int
181popup_on_border(win_T *wp, int row, int col)
182{
183 return (row == 0 && wp->w_popup_border[0] > 0)
184 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
185 || (col == 0 && wp->w_popup_border[3] > 0)
186 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
187}
188
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200189/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200190 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
191 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200192 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200193 * Caller should check the left mouse button was clicked.
194 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200195 */
196 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200197popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200198{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200199 if (wp->w_popup_close == POPCLOSE_BUTTON
200 && row == 0 && col == popup_width(wp) - 1)
201 {
202 popup_close_for_mouse_click(wp);
203 return TRUE;
204 }
205 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200206}
207
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200208// Values set when dragging a popup window starts.
209static int drag_start_row;
210static int drag_start_col;
211static int drag_start_wantline;
212static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200213static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200214
215/*
216 * Mouse down on border of popup window: start dragging it.
217 * Uses mouse_col and mouse_row.
218 */
219 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200220popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200221{
222 drag_start_row = mouse_row;
223 drag_start_col = mouse_col;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200224 if (wp->w_wantline == 0)
225 drag_start_wantline = wp->w_winrow + 1;
226 else
227 drag_start_wantline = wp->w_wantline;
228 if (wp->w_wantcol == 0)
229 drag_start_wantcol = wp->w_wincol + 1;
230 else
231 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200232
233 // Stop centering the popup
234 if (wp->w_popup_pos == POPPOS_CENTER)
235 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200236
237 drag_on_resize_handle = wp->w_popup_border[1] > 0
238 && wp->w_popup_border[2] > 0
239 && row == popup_height(wp) - 1
240 && col == popup_width(wp) - 1;
241
242 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
243 {
244 if (wp->w_popup_pos == POPPOS_TOPRIGHT
245 || wp->w_popup_pos == POPPOS_BOTRIGHT)
246 wp->w_wantcol = wp->w_wincol + 1;
247 if (wp->w_popup_pos == POPPOS_BOTLEFT)
248 wp->w_wantline = wp->w_winrow + 1;
249 wp->w_popup_pos = POPPOS_TOPLEFT;
250 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200251}
252
253/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200254 * Mouse moved while dragging a popup window: adjust the window popup position
255 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200256 */
257 void
258popup_drag(win_T *wp)
259{
260 // The popup may be closed before dragging stops.
261 if (!win_valid_popup(wp))
262 return;
263
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
265 {
266 int width_inc = mouse_col - drag_start_col;
267 int height_inc = mouse_row - drag_start_row;
268
269 if (width_inc != 0)
270 {
271 int width = wp->w_width + width_inc;
272
273 if (width < 1)
274 width = 1;
275 wp->w_minwidth = width;
276 wp->w_maxwidth = width;
277 drag_start_col = mouse_col;
278 }
279
280 if (height_inc != 0)
281 {
282 int height = wp->w_height + height_inc;
283
284 if (height < 1)
285 height = 1;
286 wp->w_minheight = height;
287 wp->w_maxheight = height;
288 drag_start_row = mouse_row;
289 }
290
291 popup_adjust_position(wp);
292 return;
293 }
294
295 if (!(wp->w_popup_flags & POPF_DRAG))
296 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200297 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
298 if (wp->w_wantline < 1)
299 wp->w_wantline = 1;
300 if (wp->w_wantline > Rows)
301 wp->w_wantline = Rows;
302 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
303 if (wp->w_wantcol < 1)
304 wp->w_wantcol = 1;
305 if (wp->w_wantcol > Columns)
306 wp->w_wantcol = Columns;
307
308 popup_adjust_position(wp);
309}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200310
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200311/*
312 * Set w_firstline to match the current "wp->w_topline".
313 */
314 void
315popup_set_firstline(win_T *wp)
316{
317 int height = wp->w_height;
318
319 wp->w_firstline = wp->w_topline;
320 popup_adjust_position(wp);
321
322 // we don't want the popup to get smaller, decrement the first line
323 // until it doesn't
324 while (wp->w_firstline > 1 && wp->w_height < height)
325 {
326 --wp->w_firstline;
327 popup_adjust_position(wp);
328 }
329}
330
331/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200332 * Return TRUE if the position is in the popup window scrollbar.
333 */
334 int
335popup_is_in_scrollbar(win_T *wp, int row, int col)
336{
337 return wp->w_has_scrollbar
338 && row >= wp->w_popup_border[0]
339 && row < popup_height(wp) - wp->w_popup_border[2]
340 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
341}
342
343
344/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200345 * Handle a click in a popup window, if it is in the scrollbar.
346 */
347 void
348popup_handle_scrollbar_click(win_T *wp, int row, int col)
349{
350 int height = popup_height(wp);
351 int old_topline = wp->w_topline;
352
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200353 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200354 {
355 if (row >= height / 2)
356 {
357 // Click in lower half, scroll down.
358 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
359 ++wp->w_topline;
360 }
361 else if (wp->w_topline > 1)
362 // click on upper half, scroll up.
363 --wp->w_topline;
364 if (wp->w_topline != old_topline)
365 {
366 popup_set_firstline(wp);
367 redraw_win_later(wp, NOT_VALID);
368 }
369 }
370}
371
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200372#if defined(FEAT_TIMERS)
373 static void
374popup_add_timeout(win_T *wp, int time)
375{
376 char_u cbbuf[50];
377 char_u *ptr = cbbuf;
378 typval_T tv;
379
380 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
381 "{_ -> popup_close(%d)}", wp->w_id);
382 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
383 {
384 wp->w_popup_timer = create_timer(time, 0);
385 wp->w_popup_timer->tr_callback = get_callback(&tv);
386 clear_tv(&tv);
387 }
388}
389#endif
390
Bram Moolenaar17627312019-06-02 19:53:44 +0200391/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200392 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200393 */
394 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200395apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200396{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200397 int nr;
398 char_u *str;
399 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200400
401 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
402 wp->w_minwidth = nr;
403 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
404 wp->w_minheight = nr;
405 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
406 wp->w_maxwidth = nr;
407 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
408 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200409
410 nr = popup_options_one(d, (char_u *)"line");
411 if (nr > 0)
412 wp->w_wantline = nr;
413 nr = popup_options_one(d, (char_u *)"col");
414 if (nr > 0)
415 wp->w_wantcol = nr;
416
417 di = dict_find(d, (char_u *)"fixed", -1);
418 if (di != NULL)
419 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
420
421 str = dict_get_string(d, (char_u *)"pos", FALSE);
422 if (str != NULL)
423 {
424 for (nr = 0;
425 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
426 ++nr)
427 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
428 {
429 wp->w_popup_pos = poppos_entries[nr].pp_val;
430 nr = -1;
431 break;
432 }
433 if (nr != -1)
434 semsg(_(e_invarg2), str);
435 }
436
437 str = dict_get_string(d, (char_u *)"textprop", FALSE);
438 if (str != NULL)
439 {
440 wp->w_popup_prop_type = 0;
441 if (*str != NUL)
442 {
443 nr = find_prop_type_id(str, wp->w_buffer);
444 if (nr <= 0)
445 nr = find_prop_type_id(str, NULL);
446 if (nr <= 0)
447 semsg(_(e_invarg2), str);
448 else
449 {
450 wp->w_popup_prop_type = nr;
451 wp->w_popup_prop_win = curwin;
452
453 di = dict_find(d, (char_u *)"textpropwin", -1);
454 if (di != NULL)
455 {
456 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
457 if (win_valid(wp->w_popup_prop_win))
458 wp->w_popup_prop_win = curwin;
459 }
460 }
461 }
462 }
463
464 di = dict_find(d, (char_u *)"textpropid", -1);
465 if (di != NULL)
466 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200467}
468
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200469 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200470handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
471{
472 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
473 {
474 char_u *s = di->di_tv.vval.v_string;
475 int flags = 0;
476
477 if (STRCMP(s, "word") == 0)
478 flags = FIND_IDENT | FIND_STRING;
479 else if (STRCMP(s, "WORD") == 0)
480 flags = FIND_STRING;
481 else if (STRCMP(s, "expr") == 0)
482 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
483 else if (STRCMP(s, "any") != 0)
484 semsg(_(e_invarg2), s);
485 if (flags != 0)
486 {
487 if (mousemoved)
488 set_mousemoved_columns(wp, flags);
489 else
490 set_moved_columns(wp, flags);
491 }
492 }
493 else if (di->di_tv.v_type == VAR_LIST
494 && di->di_tv.vval.v_list != NULL
495 && di->di_tv.vval.v_list->lv_len == 2)
496 {
497 list_T *l = di->di_tv.vval.v_list;
498 int mincol = tv_get_number(&l->lv_first->li_tv);
499 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
500
501 if (mousemoved)
502 {
503 wp->w_popup_mouse_mincol = mincol;
504 wp->w_popup_mouse_maxcol = maxcol;
505 }
506 else
507 {
508 wp->w_popup_mincol = mincol;
509 wp->w_popup_maxcol = maxcol;
510 }
511 }
512 else
513 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
514}
515
516 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200517check_highlight(dict_T *dict, char *name, char_u **pval)
518{
519 dictitem_T *di;
520 char_u *str;
521
522 di = dict_find(dict, (char_u *)name, -1);
523 if (di != NULL)
524 {
525 if (di->di_tv.v_type != VAR_STRING)
526 semsg(_(e_invargval), name);
527 else
528 {
529 str = tv_get_string(&di->di_tv);
530 if (*str != NUL)
531 *pval = vim_strsave(str);
532 }
533 }
534}
535
Bram Moolenaarae943152019-06-16 22:54:14 +0200536/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200537 * Scroll to show the line with the cursor. This assumes lines don't wrap.
538 */
539 static void
540popup_show_curline(win_T *wp)
541{
542 if (wp->w_cursor.lnum < wp->w_topline)
543 wp->w_topline = wp->w_cursor.lnum;
544 else if (wp->w_cursor.lnum >= wp->w_botline)
545 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200546
547 // Don't use "firstline" now.
548 wp->w_firstline = 0;
549}
550
551/*
552 * Get the sign group name for window "wp".
553 * Returns a pointer to a static buffer, overwritten on the next call.
554 */
555 static char_u *
556popup_get_sign_name(win_T *wp)
557{
558 static char buf[30];
559
560 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
561 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200562}
563
564/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200565 * Highlight the line with the cursor.
566 * Also scrolls the text to put the cursor line in view.
567 */
568 static void
569popup_highlight_curline(win_T *wp)
570{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200571 int sign_id = 0;
572 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200573
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200574 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200575
576 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
577 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200578 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200579
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200580 if (!sign_exists_by_name(sign_name))
581 {
582 char *linehl = "PopupSelected";
583
584 if (syn_name2id((char_u *)linehl) == 0)
585 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200586 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200587 }
588
589 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
590 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
591 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200592 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200593 else
594 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200595}
596
597/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200598 * Shared between popup_create() and f_popup_setoptions().
599 */
600 static void
601apply_general_options(win_T *wp, dict_T *dict)
602{
603 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200604 int nr;
605 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200606
Bram Moolenaarae943152019-06-16 22:54:14 +0200607 // TODO: flip
608
609 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200610 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200611 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
612 if (wp->w_firstline < 1)
613 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200614
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200615 di = dict_find(dict, (char_u *)"scrollbar", -1);
616 if (di != NULL)
617 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
618
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200619 str = dict_get_string(dict, (char_u *)"title", FALSE);
620 if (str != NULL)
621 {
622 vim_free(wp->w_popup_title);
623 wp->w_popup_title = vim_strsave(str);
624 }
625
Bram Moolenaar402502d2019-05-30 22:07:36 +0200626 di = dict_find(dict, (char_u *)"wrap", -1);
627 if (di != NULL)
628 {
629 nr = dict_get_number(dict, (char_u *)"wrap");
630 wp->w_p_wrap = nr != 0;
631 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200632
Bram Moolenaara42d9452019-06-15 21:46:30 +0200633 di = dict_find(dict, (char_u *)"drag", -1);
634 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200635 {
636 nr = dict_get_number(dict, (char_u *)"drag");
637 if (nr)
638 wp->w_popup_flags |= POPF_DRAG;
639 else
640 wp->w_popup_flags &= ~POPF_DRAG;
641 }
642
643 di = dict_find(dict, (char_u *)"resize", -1);
644 if (di != NULL)
645 {
646 nr = dict_get_number(dict, (char_u *)"resize");
647 if (nr)
648 wp->w_popup_flags |= POPF_RESIZE;
649 else
650 wp->w_popup_flags &= ~POPF_RESIZE;
651 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200652
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200653 di = dict_find(dict, (char_u *)"close", -1);
654 if (di != NULL)
655 {
656 int ok = TRUE;
657
658 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
659 {
660 char_u *s = di->di_tv.vval.v_string;
661
662 if (STRCMP(s, "none") == 0)
663 wp->w_popup_close = POPCLOSE_NONE;
664 else if (STRCMP(s, "button") == 0)
665 wp->w_popup_close = POPCLOSE_BUTTON;
666 else if (STRCMP(s, "click") == 0)
667 wp->w_popup_close = POPCLOSE_CLICK;
668 else
669 ok = FALSE;
670 }
671 else
672 ok = FALSE;
673 if (!ok)
674 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
675 }
676
Bram Moolenaarae943152019-06-16 22:54:14 +0200677 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
678 if (str != NULL)
679 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
680 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200681
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200682 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
683 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200684 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
685 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200686
Bram Moolenaar790498b2019-06-01 22:15:29 +0200687 di = dict_find(dict, (char_u *)"borderhighlight", -1);
688 if (di != NULL)
689 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200690 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200691 emsg(_(e_listreq));
692 else
693 {
694 list_T *list = di->di_tv.vval.v_list;
695 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200696 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200697
Bram Moolenaar403d0902019-07-17 21:37:32 +0200698 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
699 ++i, li = li->li_next)
700 {
701 str = tv_get_string(&li->li_tv);
702 if (*str != NUL)
703 wp->w_border_highlight[i] = vim_strsave(str);
704 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200705 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
706 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200707 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200708 vim_strsave(wp->w_border_highlight[0]);
709 }
710 }
711
Bram Moolenaar790498b2019-06-01 22:15:29 +0200712 di = dict_find(dict, (char_u *)"borderchars", -1);
713 if (di != NULL)
714 {
715 if (di->di_tv.v_type != VAR_LIST)
716 emsg(_(e_listreq));
717 else
718 {
719 list_T *list = di->di_tv.vval.v_list;
720 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200721 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200722
723 if (list != NULL)
724 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
725 ++i, li = li->li_next)
726 {
727 str = tv_get_string(&li->li_tv);
728 if (*str != NUL)
729 wp->w_border_char[i] = mb_ptr2char(str);
730 }
731 if (list->lv_len == 1)
732 for (i = 1; i < 8; ++i)
733 wp->w_border_char[i] = wp->w_border_char[0];
734 if (list->lv_len == 2)
735 {
736 for (i = 4; i < 8; ++i)
737 wp->w_border_char[i] = wp->w_border_char[1];
738 for (i = 1; i < 4; ++i)
739 wp->w_border_char[i] = wp->w_border_char[0];
740 }
741 }
742 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200743
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200744 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
745 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
746
Bram Moolenaarae943152019-06-16 22:54:14 +0200747 di = dict_find(dict, (char_u *)"zindex", -1);
748 if (di != NULL)
749 {
750 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
751 if (wp->w_zindex < 1)
752 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
753 if (wp->w_zindex > 32000)
754 wp->w_zindex = 32000;
755 }
756
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200757 di = dict_find(dict, (char_u *)"mask", -1);
758 if (di != NULL)
759 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200760 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200761
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200762 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200763 {
764 listitem_T *li;
765
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200766 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200767 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
768 li = li->li_next)
769 {
770 if (li->li_tv.v_type != VAR_LIST
771 || li->li_tv.vval.v_list == NULL
772 || li->li_tv.vval.v_list->lv_len != 4)
773 {
774 ok = FALSE;
775 break;
776 }
777 }
778 }
779 if (ok)
780 {
781 wp->w_popup_mask = di->di_tv.vval.v_list;
782 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200783 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200784 }
785 else
786 semsg(_(e_invargval), "mask");
787 }
788
Bram Moolenaarae943152019-06-16 22:54:14 +0200789#if defined(FEAT_TIMERS)
790 // Add timer to close the popup after some time.
791 nr = dict_get_number(dict, (char_u *)"time");
792 if (nr > 0)
793 popup_add_timeout(wp, nr);
794#endif
795
Bram Moolenaar3397f742019-06-02 18:40:06 +0200796 di = dict_find(dict, (char_u *)"moved", -1);
797 if (di != NULL)
798 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200799 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200800 handle_moved_argument(wp, di, FALSE);
801 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200802
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200803 di = dict_find(dict, (char_u *)"mousemoved", -1);
804 if (di != NULL)
805 {
806 set_mousemoved_values(wp);
807 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200808 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200809
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200810 di = dict_find(dict, (char_u *)"cursorline", -1);
811 if (di != NULL)
812 {
813 if (di->di_tv.v_type == VAR_NUMBER)
814 {
815 if (di->di_tv.vval.v_number != 0)
816 wp->w_popup_flags |= POPF_CURSORLINE;
817 else
818 wp->w_popup_flags &= ~POPF_CURSORLINE;
819 }
820 else
821 semsg(_(e_invargval), "cursorline");
822 }
823
Bram Moolenaarae943152019-06-16 22:54:14 +0200824 di = dict_find(dict, (char_u *)"filter", -1);
825 if (di != NULL)
826 {
827 callback_T callback = get_callback(&di->di_tv);
828
829 if (callback.cb_name != NULL)
830 {
831 free_callback(&wp->w_filter_cb);
832 set_callback(&wp->w_filter_cb, &callback);
833 }
834 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200835 di = dict_find(dict, (char_u *)"mapping", -1);
836 if (di != NULL)
837 {
838 nr = dict_get_number(dict, (char_u *)"mapping");
839 if (nr)
840 wp->w_popup_flags |= POPF_MAPPING;
841 else
842 wp->w_popup_flags &= ~POPF_MAPPING;
843 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200844
845 di = dict_find(dict, (char_u *)"callback", -1);
846 if (di != NULL)
847 {
848 callback_T callback = get_callback(&di->di_tv);
849
850 if (callback.cb_name != NULL)
851 {
852 free_callback(&wp->w_close_cb);
853 set_callback(&wp->w_close_cb, &callback);
854 }
855 }
856}
857
858/*
859 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200860 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200861 */
862 static void
863apply_options(win_T *wp, dict_T *dict)
864{
865 int nr;
866
867 apply_move_options(wp, dict);
868 apply_general_options(wp, dict);
869
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200870 nr = dict_get_number(dict, (char_u *)"hidden");
871 if (nr > 0)
872 {
873 wp->w_popup_flags |= POPF_HIDDEN;
874 --wp->w_buffer->b_nwindows;
875 }
876
Bram Moolenaar33796b32019-06-08 16:01:13 +0200877 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200878 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200879}
880
881/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200882 * Add lines to the popup from a list of strings.
883 */
884 static void
885add_popup_strings(buf_T *buf, list_T *l)
886{
887 listitem_T *li;
888 linenr_T lnum = 0;
889 char_u *p;
890
891 for (li = l->lv_first; li != NULL; li = li->li_next)
892 if (li->li_tv.v_type == VAR_STRING)
893 {
894 p = li->li_tv.vval.v_string;
895 ml_append_buf(buf, lnum++,
896 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
897 }
898}
899
900/*
901 * Add lines to the popup from a list of dictionaries.
902 */
903 static void
904add_popup_dicts(buf_T *buf, list_T *l)
905{
906 listitem_T *li;
907 listitem_T *pli;
908 linenr_T lnum = 0;
909 char_u *p;
910 dict_T *dict;
911
912 // first add the text lines
913 for (li = l->lv_first; li != NULL; li = li->li_next)
914 {
915 if (li->li_tv.v_type != VAR_DICT)
916 {
917 emsg(_(e_dictreq));
918 return;
919 }
920 dict = li->li_tv.vval.v_dict;
921 p = dict == NULL ? NULL
922 : dict_get_string(dict, (char_u *)"text", FALSE);
923 ml_append_buf(buf, lnum++,
924 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
925 }
926
927 // add the text properties
928 lnum = 1;
929 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
930 {
931 dictitem_T *di;
932 list_T *plist;
933
934 dict = li->li_tv.vval.v_dict;
935 di = dict_find(dict, (char_u *)"props", -1);
936 if (di != NULL)
937 {
938 if (di->di_tv.v_type != VAR_LIST)
939 {
940 emsg(_(e_listreq));
941 return;
942 }
943 plist = di->di_tv.vval.v_list;
944 if (plist != NULL)
945 {
946 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
947 {
948 if (pli->li_tv.v_type != VAR_DICT)
949 {
950 emsg(_(e_dictreq));
951 return;
952 }
953 dict = pli->li_tv.vval.v_dict;
954 if (dict != NULL)
955 {
956 int col = dict_get_number(dict, (char_u *)"col");
957
958 prop_add_common( lnum, col, dict, buf, NULL);
959 }
960 }
961 }
962 }
963 }
964}
965
966/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200967 * Get the padding plus border at the top, adjusted to 1 if there is a title.
968 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +0200969 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200970popup_top_extra(win_T *wp)
971{
972 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
973
974 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
975 return 1;
976 return extra;
977}
978
979/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200980 * Return the height of popup window "wp", including border and padding.
981 */
982 int
983popup_height(win_T *wp)
984{
985 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200986 + popup_top_extra(wp)
987 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +0200988}
989
990/*
991 * Return the width of popup window "wp", including border, padding and
992 * scrollbar.
993 */
994 int
995popup_width(win_T *wp)
996{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200997 // w_leftcol is how many columns of the core are left of the screen
998 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200999 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001000 + popup_extra_width(wp)
1001 + wp->w_popup_rightoff;
1002}
1003
1004/*
1005 * Return the extra width of popup window "wp": border, padding and scrollbar.
1006 */
1007 int
1008popup_extra_width(win_T *wp)
1009{
1010 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1011 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1012 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001013}
1014
1015/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001016 * Adjust the position and size of the popup to fit on the screen.
1017 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001018 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001019popup_adjust_position(win_T *wp)
1020{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001021 linenr_T lnum;
1022 int wrapped = 0;
1023 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001024 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001025 int center_vert = FALSE;
1026 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001027 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001028 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001029 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1030 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1031 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1032 int extra_height = top_extra + bot_extra;
1033 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001034 int org_winrow = wp->w_winrow;
1035 int org_wincol = wp->w_wincol;
1036 int org_width = wp->w_width;
1037 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001038 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001039 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001040 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001041 int wantline = wp->w_wantline; // adjusted for textprop
1042 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001043
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001044 wp->w_winrow = 0;
1045 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001046 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001047 wp->w_popup_leftoff = 0;
1048 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001049
1050 // If no line was specified default to vertical centering.
1051 if (wantline == 0)
1052 center_vert = TRUE;
1053
1054 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1055 {
1056 win_T *prop_win = wp->w_popup_prop_win;
1057 textprop_T prop;
1058 linenr_T prop_lnum;
1059 pos_T pos;
1060 int screen_row;
1061 int screen_scol;
1062 int screen_ccol;
1063 int screen_ecol;
1064
1065 // Popup window is positioned relative to a text property.
1066 if (find_visible_prop(prop_win,
1067 wp->w_popup_prop_type, wp->w_popup_prop_id,
1068 &prop, &prop_lnum) == FAIL)
1069 {
1070 // Text property is no longer visible, hide the popup.
1071 // Unhiding the popup is done in check_popup_unhidden().
1072 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1073 {
1074 wp->w_popup_flags |= POPF_HIDDEN;
1075 --wp->w_buffer->b_nwindows;
1076 if (win_valid(wp->w_popup_prop_win))
1077 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1078 }
1079 return;
1080 }
1081
1082 // Compute the desired position from the position of the text
1083 // property. Use "wantline" and "wantcol" as offsets.
1084 pos.lnum = prop_lnum;
1085 pos.col = prop.tp_col;
1086 if (wp->w_popup_pos == POPPOS_TOPLEFT
1087 || wp->w_popup_pos == POPPOS_BOTLEFT)
1088 pos.col += prop.tp_len - 1;
1089 textpos2screenpos(prop_win, &pos, &screen_row,
1090 &screen_scol, &screen_ccol, &screen_ecol);
1091
1092 if (wp->w_popup_pos == POPPOS_TOPLEFT
1093 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1094 // below the text
1095 wantline = screen_row + wantline + 1;
1096 else
1097 // above the text
1098 wantline = screen_row + wantline - 1;
1099 center_vert = FALSE;
1100 if (wp->w_popup_pos == POPPOS_TOPLEFT
1101 || wp->w_popup_pos == POPPOS_BOTLEFT)
1102 // right of the text
1103 wantcol = screen_ecol + wantcol;
1104 else
1105 // left of the text
1106 wantcol = screen_scol + wantcol - 1;
1107 }
1108
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001109 if (wp->w_popup_pos == POPPOS_CENTER)
1110 {
1111 // center after computing the size
1112 center_vert = TRUE;
1113 center_hor = TRUE;
1114 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001115 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001116 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001117 if (wantline != 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1118 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001119 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001120 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001121 if (wp->w_winrow >= Rows)
1122 wp->w_winrow = Rows - 1;
1123 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001124
Bram Moolenaar12034e22019-08-25 22:25:02 +02001125 if (wantcol == 0)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001126 center_hor = TRUE;
1127 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1128 || wp->w_popup_pos == POPPOS_BOTLEFT)
1129 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001130 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001131 if (wp->w_wincol >= Columns - 3)
1132 wp->w_wincol = Columns - 3;
1133 }
1134 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001135
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001136 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001137 // When left aligned use the space available, but shift to the left when we
1138 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001139 maxspace = Columns - wp->w_wincol - left_extra;
1140 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001141 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001142 {
1143 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001144 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001145 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001146
Bram Moolenaar8d241042019-06-12 23:40:01 +02001147 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001148 if (wp->w_firstline != 0)
1149 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001150 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1151 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1152
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001153 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001154 // Use a minimum width of one, so that something shows when there is no
1155 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001156 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001157 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001158 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001159 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001160 int len;
1161 int w_width = wp->w_width;
1162
1163 // Count Tabs for what they are worth and compute the length based on
1164 // the maximum width (matters when 'showbreak' is set).
1165 if (wp->w_width < maxwidth)
1166 wp->w_width = maxwidth;
1167 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001168 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001169 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001170
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001171 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001172 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001173 while (len > maxwidth)
1174 {
1175 ++wrapped;
1176 len -= maxwidth;
1177 wp->w_width = maxwidth;
1178 }
1179 }
1180 else if (len > maxwidth
1181 && allow_adjust_left
1182 && (wp->w_popup_pos == POPPOS_TOPLEFT
1183 || wp->w_popup_pos == POPPOS_BOTLEFT))
1184 {
1185 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001186 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001187
Bram Moolenaar51c31312019-06-15 22:27:23 +02001188 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001189 {
1190 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001191
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001192 len -= truncate_shift;
1193 shift_by -= truncate_shift;
1194 }
1195
1196 wp->w_wincol -= shift_by;
1197 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001198 wp->w_width = maxwidth;
1199 }
1200 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001201 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001202 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001203 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1204 wp->w_width = wp->w_maxwidth;
1205 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001206 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001207 if (wp->w_maxheight > 0
1208 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001209 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001210 }
1211
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001212 wp->w_has_scrollbar = wp->w_want_scrollbar
1213 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001214 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001215 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001216 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001217 ++extra_width;
1218 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001219
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001220 minwidth = wp->w_minwidth;
1221 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1222 {
1223 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1224
1225 if (minwidth < title_len)
1226 minwidth = title_len;
1227 }
1228
1229 if (minwidth > 0 && wp->w_width < minwidth)
1230 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001231 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001232 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001233 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001234 // some columns cut off on the right
1235 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001236 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001237 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001238 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001239 {
1240 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1241 if (wp->w_wincol < 0)
1242 wp->w_wincol = 0;
1243 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001244 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1245 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1246 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001247 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001248
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001249 // Right aligned: move to the right if needed.
1250 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001251 if (leftoff >= 0)
1252 wp->w_wincol = leftoff;
1253 else if (wp->w_popup_fixed)
1254 {
1255 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001256 // columns when displaying the window, minus left border and
1257 // padding.
1258 if (-leftoff > left_extra)
1259 wp->w_leftcol = -leftoff - left_extra;
1260 wp->w_width -= wp->w_leftcol;
1261 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001262 if (wp->w_width < 0)
1263 wp->w_width = 0;
1264 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001265 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001266
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001267 if (wp->w_p_wrap || (!wp->w_popup_fixed
1268 && (wp->w_popup_pos == POPPOS_TOPLEFT
1269 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001270 {
1271 int want_col = 0;
1272
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001273 // try to show the right border and any scrollbar
1274 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001275 if (want_col > 0 && wp->w_wincol > 0
1276 && wp->w_wincol + want_col >= Columns)
1277 {
1278 wp->w_wincol = Columns - want_col;
1279 if (wp->w_wincol < 0)
1280 wp->w_wincol = 0;
1281 }
1282 }
1283
Bram Moolenaar8d241042019-06-12 23:40:01 +02001284 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1285 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001286 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1287 wp->w_height = wp->w_minheight;
1288 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1289 wp->w_height = wp->w_maxheight;
1290 if (wp->w_height > Rows - wp->w_winrow)
1291 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001292
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001293 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001294 {
1295 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1296 if (wp->w_winrow < 0)
1297 wp->w_winrow = 0;
1298 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001299 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1300 || wp->w_popup_pos == POPPOS_BOTLEFT)
1301 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001302 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001303 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001304 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001305 else
1306 // not enough space, make top aligned
Bram Moolenaar12034e22019-08-25 22:25:02 +02001307 wp->w_winrow = wantline + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001308 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001309 if (wp->w_winrow >= Rows)
1310 wp->w_winrow = Rows - 1;
1311 else if (wp->w_winrow < 0)
1312 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001313
Bram Moolenaar17146962019-05-30 00:12:11 +02001314 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001315 if (win_valid(wp->w_popup_prop_win))
1316 {
1317 wp->w_popup_prop_changedtick =
1318 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1319 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1320 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001321
1322 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001323 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001324 if (org_winrow != wp->w_winrow
1325 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001326 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001327 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001328 || org_width != wp->w_width
1329 || org_height != wp->w_height)
1330 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001331 redraw_win_later(wp, NOT_VALID);
1332 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1333 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001334 popup_mask_refresh = TRUE;
1335 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001336}
1337
Bram Moolenaar17627312019-06-02 19:53:44 +02001338typedef enum
1339{
1340 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001341 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001342 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001343 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001344 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001345 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001346 TYPE_PREVIEW, // preview window
1347 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001348} create_type_T;
1349
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001350/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001351 * Make "buf" empty and set the contents to "text".
1352 * Used by popup_create() and popup_settext().
1353 */
1354 static void
1355popup_set_buffer_text(buf_T *buf, typval_T text)
1356{
1357 int lnum;
1358
1359 // Clear the buffer, then replace the lines.
1360 curbuf = buf;
1361 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1362 ml_delete(lnum, FALSE);
1363 curbuf = curwin->w_buffer;
1364
1365 // Add text to the buffer.
1366 if (text.v_type == VAR_STRING)
1367 {
1368 // just a string
1369 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1370 }
1371 else
1372 {
1373 list_T *l = text.vval.v_list;
1374
1375 if (l->lv_len > 0)
1376 {
1377 if (l->lv_first->li_tv.v_type == VAR_STRING)
1378 // list of strings
1379 add_popup_strings(buf, l);
1380 else
1381 // list of dictionaries
1382 add_popup_dicts(buf, l);
1383 }
1384 }
1385
1386 // delete the line that was in the empty buffer
1387 curbuf = buf;
1388 ml_delete(buf->b_ml.ml_line_count, FALSE);
1389 curbuf = curwin->w_buffer;
1390}
1391
1392/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001393 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1394 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001395 * Return FAIL if the parsing fails.
1396 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001397 static int
1398parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001399{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001400 char_u *p =
1401#ifdef FEAT_QUICKFIX
1402 !is_preview ? p_cpp :
1403#endif
1404 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001405
Bram Moolenaar258cef52019-08-21 17:29:29 +02001406 if (wp != NULL)
1407 wp->w_popup_flags &= ~POPF_INFO_MENU;
1408
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001409 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001410 {
1411 char_u *e, *dig;
1412 char_u *s = p;
1413 int x;
1414
1415 e = vim_strchr(p, ':');
1416 if (e == NULL || e[1] == NUL)
1417 return FAIL;
1418
1419 p = vim_strchr(e, ',');
1420 if (p == NULL)
1421 p = e + STRLEN(e);
1422 dig = e + 1;
1423 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001424
1425 if (STRNCMP(s, "height:", 7) == 0)
1426 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001427 if (dig != p)
1428 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001429 if (wp != NULL)
1430 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001431 if (is_preview)
1432 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001433 wp->w_maxheight = x;
1434 }
1435 }
1436 else if (STRNCMP(s, "width:", 6) == 0)
1437 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001438 if (dig != p)
1439 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001440 if (wp != NULL)
1441 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001442 if (is_preview)
1443 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001444 wp->w_maxwidth = x;
1445 }
1446 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001447 else if (STRNCMP(s, "highlight:", 10) == 0)
1448 {
1449 if (wp != NULL)
1450 {
1451 int c = *p;
1452
1453 *p = NUL;
1454 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1455 s + 10, OPT_FREE|OPT_LOCAL, 0);
1456 *p = c;
1457 }
1458 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001459 else if (STRNCMP(s, "border:", 7) == 0)
1460 {
1461 char_u *arg = s + 7;
1462 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1463 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1464 int i;
1465
1466 if (!on && !off)
1467 return FAIL;
1468 if (wp != NULL)
1469 {
1470 for (i = 0; i < 4; ++i)
1471 wp->w_popup_border[i] = on ? 1 : 0;
1472 if (off)
1473 // only show the X for close when there is a border
1474 wp->w_popup_close = POPCLOSE_NONE;
1475 }
1476 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001477 else if (STRNCMP(s, "align:", 6) == 0)
1478 {
1479 char_u *arg = s + 6;
1480 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1481 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1482
1483 if (!menu && !item)
1484 return FAIL;
1485 if (wp != NULL && menu)
1486 wp->w_popup_flags |= POPF_INFO_MENU;
1487 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001488 else
1489 return FAIL;
1490 }
1491 return OK;
1492}
1493
1494/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001495 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1496 * is not NULL.
1497 * Return FAIL if the parsing fails.
1498 */
1499 int
1500parse_previewpopup(win_T *wp)
1501{
1502 return parse_popup_option(wp, TRUE);
1503}
1504
1505/*
1506 * Parse the 'completepopup' option and apply the values to window "wp" if it
1507 * is not NULL.
1508 * Return FAIL if the parsing fails.
1509 */
1510 int
1511parse_completepopup(win_T *wp)
1512{
1513 return parse_popup_option(wp, FALSE);
1514}
1515
1516/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001517 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001518 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001519 */
1520 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001521popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001522{
1523 setcursor_mayforce(TRUE);
1524 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1525 if (wp->w_wantline == 0) // cursor in first line
1526 {
1527 wp->w_wantline = 2;
1528 wp->w_popup_pos = POPPOS_TOPLEFT;
1529 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001530
Bram Moolenaar79648732019-07-18 21:43:07 +02001531 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001532 if (wp->w_wantcol > Columns - width)
1533 {
1534 wp->w_wantcol = Columns - width;
1535 if (wp->w_wantcol < 1)
1536 wp->w_wantcol = 1;
1537 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001538 popup_adjust_position(wp);
1539}
1540
1541/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001542 * Set w_wantline and w_wantcol for the a given screen position.
1543 * Caller must take care of running into the window border.
1544 */
1545 void
1546popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1547{
1548 wp->w_wantline = row;
1549 wp->w_wantcol = col;
1550 popup_adjust_position(wp);
1551}
1552
1553/*
1554 * Add a border and lef&right padding.
1555 */
1556 static void
1557add_border_left_right_padding(win_T *wp)
1558{
1559 int i;
1560
1561 for (i = 0; i < 4; ++i)
1562 {
1563 wp->w_popup_border[i] = 1;
1564 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1565 }
1566}
1567
1568/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001569 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001570 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001571 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001572 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001573 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001574 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001575popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001576{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001577 win_T *wp;
1578 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001579 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001580 int new_buffer;
1581 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001582 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001583 int nr;
1584 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001585
Bram Moolenaar79648732019-07-18 21:43:07 +02001586 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001587 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001588 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001589 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001590 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001591 buf = buflist_findnr( argvars[0].vval.v_number);
1592 if (buf == NULL)
1593 {
1594 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1595 return NULL;
1596 }
1597 }
1598 else if (!(argvars[0].v_type == VAR_STRING
1599 && argvars[0].vval.v_string != NULL)
1600 && !(argvars[0].v_type == VAR_LIST
1601 && argvars[0].vval.v_list != NULL))
1602 {
1603 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001604 return NULL;
1605 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001606 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001607 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001608 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001609 return NULL;
1610 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001611 d = argvars[1].vval.v_dict;
1612 }
1613
1614 if (d != NULL)
1615 {
1616 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1617 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1618 else if (type == TYPE_NOTIFICATION)
1619 tabnr = -1; // notifications are global by default
1620 else
1621 tabnr = 0;
1622 if (tabnr > 0)
1623 {
1624 tp = find_tabpage(tabnr);
1625 if (tp == NULL)
1626 {
1627 semsg(_("E997: Tabpage not found: %d"), tabnr);
1628 return NULL;
1629 }
1630 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001631 }
1632
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001633 // Create the window and buffer.
1634 wp = win_alloc_popup_win();
1635 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001636 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001637 if (rettv != NULL)
1638 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001639 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001640 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001641
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001642 if (buf != NULL)
1643 {
1644 // use existing buffer
1645 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001646 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001647 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001648 buffer_ensure_loaded(buf);
1649 }
1650 else
1651 {
1652 // create a new buffer associated with the popup
1653 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001654 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001655 if (buf == NULL)
1656 return NULL;
1657 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001658
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001659 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001660
Bram Moolenaar46451042019-08-24 15:50:46 +02001661 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001662 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001663 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001664 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001665 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001666 buf->b_p_ul = -1; // no undo
1667 buf->b_p_swf = FALSE; // no swap file
1668 buf->b_p_bl = FALSE; // unlisted buffer
1669 buf->b_locked = TRUE;
1670 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001671
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001672 // Avoid that 'buftype' is reset when this buffer is entered.
1673 buf->b_p_initialized = TRUE;
1674 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001675
Bram Moolenaara3fce622019-06-20 02:31:49 +02001676 if (tp != NULL)
1677 {
1678 // popup on specified tab page
1679 wp->w_next = tp->tp_first_popupwin;
1680 tp->tp_first_popupwin = wp;
1681 }
1682 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001683 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001684 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001685 wp->w_next = curtab->tp_first_popupwin;
1686 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001687 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001688 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001689 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001690 win_T *prev = first_popupwin;
1691
1692 // Global popup: add at the end, so that it gets displayed on top of
1693 // older ones with the same zindex. Matters for notifications.
1694 if (first_popupwin == NULL)
1695 first_popupwin = wp;
1696 else
1697 {
1698 while (prev->w_next != NULL)
1699 prev = prev->w_next;
1700 prev->w_next = wp;
1701 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001702 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001703
Bram Moolenaar79648732019-07-18 21:43:07 +02001704 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001705 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001706
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001708 {
1709 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001710 }
1711 if (type == TYPE_ATCURSOR)
1712 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001713 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001714 set_moved_values(wp);
1715 set_moved_columns(wp, FIND_STRING);
1716 }
1717
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001718 if (type == TYPE_BEVAL)
1719 {
1720 wp->w_popup_pos = POPPOS_BOTLEFT;
1721
1722 // by default use the mouse position
1723 wp->w_wantline = mouse_row;
1724 if (wp->w_wantline <= 0) // mouse on first line
1725 {
1726 wp->w_wantline = 2;
1727 wp->w_popup_pos = POPPOS_TOPLEFT;
1728 }
1729 wp->w_wantcol = mouse_col + 1;
1730 set_mousemoved_values(wp);
1731 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1732 }
1733
Bram Moolenaar33796b32019-06-08 16:01:13 +02001734 // set default values
1735 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001736 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001737
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001738 if (type == TYPE_NOTIFICATION)
1739 {
1740 win_T *twp, *nextwin;
1741 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001742
1743 // Try to not overlap with another global popup. Guess we need 3
1744 // more screen lines than buffer lines.
1745 wp->w_wantline = 1;
1746 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1747 {
1748 nextwin = twp->w_next;
1749 if (twp != wp
1750 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1751 && twp->w_winrow <= wp->w_wantline - 1 + height
1752 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1753 {
1754 // move to below this popup and restart the loop to check for
1755 // overlap with other popups
1756 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1757 nextwin = first_popupwin;
1758 }
1759 }
1760 if (wp->w_wantline + height > Rows)
1761 {
1762 // can't avoid overlap, put on top in the hope that message goes
1763 // away soon.
1764 wp->w_wantline = 1;
1765 }
1766
1767 wp->w_wantcol = 10;
1768 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001769 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001770 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001771 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001772 for (i = 0; i < 4; ++i)
1773 wp->w_popup_border[i] = 1;
1774 wp->w_popup_padding[1] = 1;
1775 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001776
1777 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001778 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001779 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1780 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001781 }
1782
Bram Moolenaara730e552019-06-16 19:05:31 +02001783 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001784 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001785 wp->w_popup_pos = POPPOS_CENTER;
1786 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001787 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001788 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001789 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001790 }
1791
Bram Moolenaara730e552019-06-16 19:05:31 +02001792 if (type == TYPE_MENU)
1793 {
1794 typval_T tv;
1795 callback_T callback;
1796
1797 tv.v_type = VAR_STRING;
1798 tv.vval.v_string = (char_u *)"popup_filter_menu";
1799 callback = get_callback(&tv);
1800 if (callback.cb_name != NULL)
1801 set_callback(&wp->w_filter_cb, &callback);
1802
1803 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001804 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001805 }
1806
Bram Moolenaar79648732019-07-18 21:43:07 +02001807 if (type == TYPE_PREVIEW)
1808 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001809 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001810 wp->w_popup_close = POPCLOSE_BUTTON;
1811 for (i = 0; i < 4; ++i)
1812 wp->w_popup_border[i] = 1;
1813 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001814 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1815 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001816# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001817 if (type == TYPE_INFO)
1818 {
1819 wp->w_popup_pos = POPPOS_TOPLEFT;
1820 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1821 wp->w_popup_close = POPCLOSE_BUTTON;
1822 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001823 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001824 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001825# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001826
Bram Moolenaarae943152019-06-16 22:54:14 +02001827 for (i = 0; i < 4; ++i)
1828 VIM_CLEAR(wp->w_border_highlight[i]);
1829 for (i = 0; i < 8; ++i)
1830 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001831 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001832 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001833
Bram Moolenaar79648732019-07-18 21:43:07 +02001834 if (d != NULL)
1835 // Deal with options.
1836 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001837
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001838#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001839 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1840 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001841#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001842
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001843 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001844
1845 wp->w_vsep_width = 0;
1846
1847 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001848 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001849
1850 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001851}
1852
1853/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001854 * popup_clear()
1855 */
1856 void
1857f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1858{
1859 close_all_popups();
1860}
1861
1862/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001863 * popup_create({text}, {options})
1864 */
1865 void
1866f_popup_create(typval_T *argvars, typval_T *rettv)
1867{
Bram Moolenaar17627312019-06-02 19:53:44 +02001868 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001869}
1870
1871/*
1872 * popup_atcursor({text}, {options})
1873 */
1874 void
1875f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1876{
Bram Moolenaar17627312019-06-02 19:53:44 +02001877 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001878}
1879
1880/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001881 * popup_beval({text}, {options})
1882 */
1883 void
1884f_popup_beval(typval_T *argvars, typval_T *rettv)
1885{
1886 popup_create(argvars, rettv, TYPE_BEVAL);
1887}
1888
1889/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001890 * Invoke the close callback for window "wp" with value "result".
1891 * Careful: The callback may make "wp" invalid!
1892 */
1893 static void
1894invoke_popup_callback(win_T *wp, typval_T *result)
1895{
1896 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001897 typval_T argv[3];
1898
1899 argv[0].v_type = VAR_NUMBER;
1900 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1901
1902 if (result != NULL && result->v_type != VAR_UNKNOWN)
1903 copy_tv(result, &argv[1]);
1904 else
1905 {
1906 argv[1].v_type = VAR_NUMBER;
1907 argv[1].vval.v_number = 0;
1908 }
1909
1910 argv[2].v_type = VAR_UNKNOWN;
1911
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001912 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001913 if (result != NULL)
1914 clear_tv(&argv[1]);
1915 clear_tv(&rettv);
1916}
1917
1918/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001919 * Close popup "wp" and invoke any close callback for it.
1920 */
1921 static void
1922popup_close_and_callback(win_T *wp, typval_T *arg)
1923{
1924 int id = wp->w_id;
1925
1926 if (wp->w_close_cb.cb_name != NULL)
1927 // Careful: This may make "wp" invalid.
1928 invoke_popup_callback(wp, arg);
1929
1930 popup_close(id);
1931}
1932
Bram Moolenaar12034e22019-08-25 22:25:02 +02001933 static void
1934popup_close_with_retval(win_T *wp, int retval)
1935{
1936 typval_T res;
1937
1938 res.v_type = VAR_NUMBER;
1939 res.vval.v_number = retval;
1940 popup_close_and_callback(wp, &res);
1941}
1942
Bram Moolenaar3397f742019-06-02 18:40:06 +02001943/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001944 * Close popup "wp" because of a mouse click.
1945 */
1946 void
1947popup_close_for_mouse_click(win_T *wp)
1948{
Bram Moolenaar12034e22019-08-25 22:25:02 +02001949 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001950}
1951
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001952 static void
1953check_mouse_moved(win_T *wp, win_T *mouse_wp)
1954{
1955 // Close the popup when all if these are true:
1956 // - the mouse is not on this popup
1957 // - "mousemoved" was used
1958 // - the mouse is no longer on the same screen row or the mouse column is
1959 // outside of the relevant text
1960 if (wp != mouse_wp
1961 && wp->w_popup_mouse_row != 0
1962 && (wp->w_popup_mouse_row != mouse_row
1963 || mouse_col < wp->w_popup_mouse_mincol
1964 || mouse_col > wp->w_popup_mouse_maxcol))
1965 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001966 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02001967 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001968 }
1969}
1970
1971/*
1972 * Called when the mouse moved: may close a popup with "mousemoved".
1973 */
1974 void
1975popup_handle_mouse_moved(void)
1976{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001977 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001978 win_T *mouse_wp;
1979 int row = mouse_row;
1980 int col = mouse_col;
1981
1982 // find the window where the mouse is in
1983 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1984
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001985 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1986 {
1987 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001988 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001989 }
1990 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1991 {
1992 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001993 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001994 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001995}
1996
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001997/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001998 * In a filter: check if the typed key is a mouse event that is used for
1999 * dragging the popup.
2000 */
2001 static void
2002filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2003{
2004 int row = mouse_row;
2005 int col = mouse_col;
2006
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002007 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002008 && is_mouse_key(c)
2009 && (wp == popup_dragwin
2010 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2011 // do not consume the key, allow for dragging the popup
2012 rettv->vval.v_number = 0;
2013}
2014
Bram Moolenaara730e552019-06-16 19:05:31 +02002015/*
2016 * popup_filter_menu({text}, {options})
2017 */
2018 void
2019f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2020{
2021 int id = tv_get_number(&argvars[0]);
2022 win_T *wp = win_id2wp(id);
2023 char_u *key = tv_get_string(&argvars[1]);
2024 typval_T res;
2025 int c;
2026 linenr_T old_lnum;
2027
2028 // If the popup has been closed do not consume the key.
2029 if (wp == NULL)
2030 return;
2031
2032 c = *key;
2033 if (c == K_SPECIAL && key[1] != NUL)
2034 c = TO_SPECIAL(key[1], key[2]);
2035
2036 // consume all keys until done
2037 rettv->vval.v_number = 1;
2038 res.v_type = VAR_NUMBER;
2039
2040 old_lnum = wp->w_cursor.lnum;
2041 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2042 --wp->w_cursor.lnum;
2043 if ((c == 'j' || c == 'J' || c == K_DOWN)
2044 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2045 ++wp->w_cursor.lnum;
2046 if (old_lnum != wp->w_cursor.lnum)
2047 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002048 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002049 return;
2050 }
2051
2052 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2053 {
2054 // Cancelled, invoke callback with -1
2055 res.vval.v_number = -1;
2056 popup_close_and_callback(wp, &res);
2057 return;
2058 }
2059 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2060 {
2061 // Invoke callback with current index.
2062 res.vval.v_number = wp->w_cursor.lnum;
2063 popup_close_and_callback(wp, &res);
2064 return;
2065 }
2066
2067 filter_handle_drag(wp, c, rettv);
2068}
2069
2070/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002071 * popup_filter_yesno({text}, {options})
2072 */
2073 void
2074f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2075{
2076 int id = tv_get_number(&argvars[0]);
2077 win_T *wp = win_id2wp(id);
2078 char_u *key = tv_get_string(&argvars[1]);
2079 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002080 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002081
2082 // If the popup has been closed don't consume the key.
2083 if (wp == NULL)
2084 return;
2085
Bram Moolenaara730e552019-06-16 19:05:31 +02002086 c = *key;
2087 if (c == K_SPECIAL && key[1] != NUL)
2088 c = TO_SPECIAL(key[1], key[2]);
2089
Bram Moolenaara42d9452019-06-15 21:46:30 +02002090 // consume all keys until done
2091 rettv->vval.v_number = 1;
2092
Bram Moolenaara730e552019-06-16 19:05:31 +02002093 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002094 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002095 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002096 res.vval.v_number = 0;
2097 else
2098 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002099 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002100 return;
2101 }
2102
2103 // Invoke callback
2104 res.v_type = VAR_NUMBER;
2105 popup_close_and_callback(wp, &res);
2106}
2107
2108/*
2109 * popup_dialog({text}, {options})
2110 */
2111 void
2112f_popup_dialog(typval_T *argvars, typval_T *rettv)
2113{
2114 popup_create(argvars, rettv, TYPE_DIALOG);
2115}
2116
2117/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002118 * popup_menu({text}, {options})
2119 */
2120 void
2121f_popup_menu(typval_T *argvars, typval_T *rettv)
2122{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002123 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002124}
2125
2126/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002127 * popup_notification({text}, {options})
2128 */
2129 void
2130f_popup_notification(typval_T *argvars, typval_T *rettv)
2131{
2132 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2133}
2134
2135/*
2136 * Find the popup window with window-ID "id".
2137 * If the popup window does not exist NULL is returned.
2138 * If the window is not a popup window, and error message is given.
2139 */
2140 static win_T *
2141find_popup_win(int id)
2142{
2143 win_T *wp = win_id2wp(id);
2144
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002145 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002146 {
2147 semsg(_("E993: window %d is not a popup window"), id);
2148 return NULL;
2149 }
2150 return wp;
2151}
2152
2153/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002154 * popup_close({id})
2155 */
2156 void
2157f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2158{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002159 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002160 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002161
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002162 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002163 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002164}
2165
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002166 static void
2167popup_hide(win_T *wp)
2168{
2169 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2170 {
2171 wp->w_popup_flags |= POPF_HIDDEN;
2172 --wp->w_buffer->b_nwindows;
2173 redraw_all_later(NOT_VALID);
2174 popup_mask_refresh = TRUE;
2175 }
2176}
2177
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002178/*
2179 * popup_hide({id})
2180 */
2181 void
2182f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2183{
2184 int id = (int)tv_get_number(argvars);
2185 win_T *wp = find_popup_win(id);
2186
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002187 if (wp != NULL)
2188 popup_hide(wp);
2189}
2190
2191 void
2192popup_show(win_T *wp)
2193{
2194 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002195 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002196 wp->w_popup_flags &= ~POPF_HIDDEN;
2197 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002198 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002199 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002200 }
2201}
2202
2203/*
2204 * popup_show({id})
2205 */
2206 void
2207f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2208{
2209 int id = (int)tv_get_number(argvars);
2210 win_T *wp = find_popup_win(id);
2211
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002212 if (wp != NULL)
2213 popup_show(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002214}
2215
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002216/*
2217 * popup_settext({id}, {text})
2218 */
2219 void
2220f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2221{
2222 int id = (int)tv_get_number(&argvars[0]);
2223 win_T *wp = find_popup_win(id);
2224
2225 if (wp != NULL)
2226 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002227 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2228 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2229 else
2230 {
2231 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002232 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002233 popup_adjust_position(wp);
2234 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002235 }
2236}
2237
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002238 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002239popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002240{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002241 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002242 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002243 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002244 clear_cmdline = TRUE;
2245 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002246
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002247 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002248 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002249}
2250
Bram Moolenaarec583842019-05-26 14:11:23 +02002251/*
2252 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002253 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002254 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002255 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002256popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002257{
2258 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002259 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002260 win_T *prev = NULL;
2261
Bram Moolenaarec583842019-05-26 14:11:23 +02002262 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002263 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002264 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002265 {
2266 if (prev == NULL)
2267 first_popupwin = wp->w_next;
2268 else
2269 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002270 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002271 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002272 }
2273
Bram Moolenaarec583842019-05-26 14:11:23 +02002274 // go through tab-local popups
2275 FOR_ALL_TABPAGES(tp)
2276 popup_close_tabpage(tp, id);
2277}
2278
2279/*
2280 * Close a popup window with Window-id "id" in tabpage "tp".
2281 */
2282 void
2283popup_close_tabpage(tabpage_T *tp, int id)
2284{
2285 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002286 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002287 win_T *prev = NULL;
2288
Bram Moolenaarec583842019-05-26 14:11:23 +02002289 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2290 if (wp->w_id == id)
2291 {
2292 if (prev == NULL)
2293 *root = wp->w_next;
2294 else
2295 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002296 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002297 return;
2298 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002299}
2300
2301 void
2302close_all_popups(void)
2303{
2304 while (first_popupwin != NULL)
2305 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002306 while (curtab->tp_first_popupwin != NULL)
2307 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002308}
2309
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002310/*
2311 * popup_move({id}, {options})
2312 */
2313 void
2314f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2315{
Bram Moolenaarae943152019-06-16 22:54:14 +02002316 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002317 int id = (int)tv_get_number(argvars);
2318 win_T *wp = find_popup_win(id);
2319
2320 if (wp == NULL)
2321 return; // invalid {id}
2322
2323 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2324 {
2325 emsg(_(e_dictreq));
2326 return;
2327 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002328 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002329
Bram Moolenaarae943152019-06-16 22:54:14 +02002330 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002331
2332 if (wp->w_winrow + wp->w_height >= cmdline_row)
2333 clear_cmdline = TRUE;
2334 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002335}
2336
Bram Moolenaarbc133542019-05-29 20:26:46 +02002337/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002338 * popup_setoptions({id}, {options})
2339 */
2340 void
2341f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2342{
2343 dict_T *dict;
2344 int id = (int)tv_get_number(argvars);
2345 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002346 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002347
2348 if (wp == NULL)
2349 return; // invalid {id}
2350
2351 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2352 {
2353 emsg(_(e_dictreq));
2354 return;
2355 }
2356 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002357 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002358
2359 apply_move_options(wp, dict);
2360 apply_general_options(wp, dict);
2361
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002362 if (old_firstline != wp->w_firstline)
2363 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002364 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002365 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002366 popup_adjust_position(wp);
2367}
2368
2369/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002370 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002371 */
2372 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002373f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002374{
2375 dict_T *dict;
2376 int id = (int)tv_get_number(argvars);
2377 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002378 int top_extra;
2379 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002380
2381 if (rettv_dict_alloc(rettv) == OK)
2382 {
2383 if (wp == NULL)
2384 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002385 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002386 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2387
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002388 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002389 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002390 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002391
Bram Moolenaarbc133542019-05-29 20:26:46 +02002392 dict_add_number(dict, "line", wp->w_winrow + 1);
2393 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002394 dict_add_number(dict, "width", wp->w_width + left_extra
2395 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2396 dict_add_number(dict, "height", wp->w_height + top_extra
2397 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002398
2399 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2400 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2401 dict_add_number(dict, "core_width", wp->w_width);
2402 dict_add_number(dict, "core_height", wp->w_height);
2403
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002404 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002405 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002406 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002407 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002408
2409 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002410 }
2411}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002412/*
2413 * popup_locate({row}, {col})
2414 */
2415 void
2416f_popup_locate(typval_T *argvars, typval_T *rettv)
2417{
2418 int row = tv_get_number(&argvars[0]) - 1;
2419 int col = tv_get_number(&argvars[1]) - 1;
2420 win_T *wp;
2421
2422 wp = mouse_find_win(&row, &col, FIND_POPUP);
2423 if (WIN_IS_POPUP(wp))
2424 rettv->vval.v_number = wp->w_id;
2425}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002426
2427/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002428 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2429 */
2430 static void
2431get_padding_border(dict_T *dict, int *array, char *name)
2432{
2433 list_T *list;
2434 int i;
2435
2436 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2437 return;
2438
2439 list = list_alloc();
2440 if (list != NULL)
2441 {
2442 dict_add_list(dict, name, list);
2443 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2444 for (i = 0; i < 4; ++i)
2445 list_append_number(list, array[i]);
2446 }
2447}
2448
2449/*
2450 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2451 */
2452 static void
2453get_borderhighlight(dict_T *dict, win_T *wp)
2454{
2455 list_T *list;
2456 int i;
2457
2458 for (i = 0; i < 4; ++i)
2459 if (wp->w_border_highlight[i] != NULL)
2460 break;
2461 if (i == 4)
2462 return;
2463
2464 list = list_alloc();
2465 if (list != NULL)
2466 {
2467 dict_add_list(dict, "borderhighlight", list);
2468 for (i = 0; i < 4; ++i)
2469 list_append_string(list, wp->w_border_highlight[i], -1);
2470 }
2471}
2472
2473/*
2474 * For popup_getoptions(): add a "borderchars" entry to "dict".
2475 */
2476 static void
2477get_borderchars(dict_T *dict, win_T *wp)
2478{
2479 list_T *list;
2480 int i;
2481 char_u buf[NUMBUFLEN];
2482 int len;
2483
2484 for (i = 0; i < 8; ++i)
2485 if (wp->w_border_char[i] != 0)
2486 break;
2487 if (i == 8)
2488 return;
2489
2490 list = list_alloc();
2491 if (list != NULL)
2492 {
2493 dict_add_list(dict, "borderchars", list);
2494 for (i = 0; i < 8; ++i)
2495 {
2496 len = mb_char2bytes(wp->w_border_char[i], buf);
2497 list_append_string(list, buf, len);
2498 }
2499 }
2500}
2501
2502/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002503 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002504 */
2505 static void
2506get_moved_list(dict_T *dict, win_T *wp)
2507{
2508 list_T *list;
2509
2510 list = list_alloc();
2511 if (list != NULL)
2512 {
2513 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002514 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002515 list_append_number(list, wp->w_popup_mincol);
2516 list_append_number(list, wp->w_popup_maxcol);
2517 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002518 list = list_alloc();
2519 if (list != NULL)
2520 {
2521 dict_add_list(dict, "mousemoved", list);
2522 list_append_number(list, wp->w_popup_mouse_row);
2523 list_append_number(list, wp->w_popup_mouse_mincol);
2524 list_append_number(list, wp->w_popup_mouse_maxcol);
2525 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002526}
2527
2528/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002529 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002530 */
2531 void
2532f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2533{
2534 dict_T *dict;
2535 int id = (int)tv_get_number(argvars);
2536 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002537 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002538 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002539
2540 if (rettv_dict_alloc(rettv) == OK)
2541 {
2542 if (wp == NULL)
2543 return;
2544
2545 dict = rettv->vval.v_dict;
2546 dict_add_number(dict, "line", wp->w_wantline);
2547 dict_add_number(dict, "col", wp->w_wantcol);
2548 dict_add_number(dict, "minwidth", wp->w_minwidth);
2549 dict_add_number(dict, "minheight", wp->w_minheight);
2550 dict_add_number(dict, "maxheight", wp->w_maxheight);
2551 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002552 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002553 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002554 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002555 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002556 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2557 {
2558 proptype_T *pt = text_prop_type_by_id(
2559 wp->w_popup_prop_win->w_buffer,
2560 wp->w_popup_prop_type);
2561
2562 if (pt != NULL)
2563 dict_add_string(dict, "textprop", pt->pt_name);
2564 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2565 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2566 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002567 dict_add_string(dict, "title", wp->w_popup_title);
2568 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002569 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002570 dict_add_number(dict, "mapping",
2571 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002572 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002573 dict_add_number(dict, "cursorline",
2574 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002575 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002576 if (wp->w_scrollbar_highlight != NULL)
2577 dict_add_string(dict, "scrollbarhighlight",
2578 wp->w_scrollbar_highlight);
2579 if (wp->w_thumb_highlight != NULL)
2580 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002581
Bram Moolenaara3fce622019-06-20 02:31:49 +02002582 // find the tabpage that holds this popup
2583 i = 1;
2584 FOR_ALL_TABPAGES(tp)
2585 {
2586 win_T *p;
2587
2588 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2589 if (p->w_id == id)
2590 break;
2591 if (p != NULL)
2592 break;
2593 ++i;
2594 }
2595 if (tp == NULL)
2596 i = -1; // must be global
2597 else if (tp == curtab)
2598 i = 0;
2599 dict_add_number(dict, "tabpage", i);
2600
Bram Moolenaarae943152019-06-16 22:54:14 +02002601 get_padding_border(dict, wp->w_popup_padding, "padding");
2602 get_padding_border(dict, wp->w_popup_border, "border");
2603 get_borderhighlight(dict, wp);
2604 get_borderchars(dict, wp);
2605 get_moved_list(dict, wp);
2606
2607 if (wp->w_filter_cb.cb_name != NULL)
2608 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2609 if (wp->w_close_cb.cb_name != NULL)
2610 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002611
2612 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2613 ++i)
2614 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2615 {
2616 dict_add_string(dict, "pos",
2617 (char_u *)poppos_entries[i].pp_name);
2618 break;
2619 }
2620
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002621 dict_add_string(dict, "close", (char_u *)(
2622 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2623 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2624
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002625# if defined(FEAT_TIMERS)
2626 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2627 ? (long)wp->w_popup_timer->tr_interval : 0L);
2628# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002629 }
2630}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002631
2632 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002633error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002634{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002635 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002636 {
2637 emsg(_("E994: Not allowed in a popup window"));
2638 return TRUE;
2639 }
2640 return FALSE;
2641}
2642
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002643/*
2644 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002645 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002646 */
2647 void
2648popup_reset_handled()
2649{
2650 win_T *wp;
2651
2652 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2653 wp->w_popup_flags &= ~POPF_HANDLED;
2654 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2655 wp->w_popup_flags &= ~POPF_HANDLED;
2656}
2657
2658/*
2659 * Find the next visible popup where POPF_HANDLED is not set.
2660 * Must have called popup_reset_handled() first.
2661 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2662 * popup with the highest zindex.
2663 */
2664 win_T *
2665find_next_popup(int lowest)
2666{
2667 win_T *wp;
2668 win_T *found_wp;
2669 int found_zindex;
2670
2671 found_zindex = lowest ? INT_MAX : 0;
2672 found_wp = NULL;
2673 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2674 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2675 && (lowest ? wp->w_zindex < found_zindex
2676 : wp->w_zindex > found_zindex))
2677 {
2678 found_zindex = wp->w_zindex;
2679 found_wp = wp;
2680 }
2681 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2682 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2683 && (lowest ? wp->w_zindex < found_zindex
2684 : wp->w_zindex > found_zindex))
2685 {
2686 found_zindex = wp->w_zindex;
2687 found_wp = wp;
2688 }
2689
2690 if (found_wp != NULL)
2691 found_wp->w_popup_flags |= POPF_HANDLED;
2692 return found_wp;
2693}
2694
2695/*
2696 * Invoke the filter callback for window "wp" with typed character "c".
2697 * Uses the global "mod_mask" for modifiers.
2698 * Returns the return value of the filter.
2699 * Careful: The filter may make "wp" invalid!
2700 */
2701 static int
2702invoke_popup_filter(win_T *wp, int c)
2703{
2704 int res;
2705 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002706 typval_T argv[3];
2707 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002708 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002709
Bram Moolenaara42d9452019-06-15 21:46:30 +02002710 // Emergency exit: CTRL-C closes the popup.
2711 if (c == Ctrl_C)
2712 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02002713 popup_close_with_retval(wp, -1);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002714 return 1;
2715 }
2716
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002717 argv[0].v_type = VAR_NUMBER;
2718 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2719
2720 // Convert the number to a string, so that the function can use:
2721 // if a:c == "\<F2>"
2722 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2723 argv[1].v_type = VAR_STRING;
2724 argv[1].vval.v_string = vim_strsave(buf);
2725
2726 argv[2].v_type = VAR_UNKNOWN;
2727
Bram Moolenaara42d9452019-06-15 21:46:30 +02002728 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002729 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002730 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002731 popup_highlight_curline(wp);
2732
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002733 res = tv_get_number(&rettv);
2734 vim_free(argv[1].vval.v_string);
2735 clear_tv(&rettv);
2736 return res;
2737}
2738
2739/*
2740 * Called when "c" was typed: invoke popup filter callbacks.
2741 * Returns TRUE when the character was consumed,
2742 */
2743 int
2744popup_do_filter(int c)
2745{
2746 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002747 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002748
2749 popup_reset_handled();
2750
Bram Moolenaarf6396232019-08-24 19:36:00 +02002751 if (c == K_LEFTMOUSE)
2752 {
2753 int row = mouse_row;
2754 int col = mouse_col;
2755
2756 wp = mouse_find_win(&row, &col, FIND_POPUP);
2757 if (wp != NULL && popup_close_if_on_X(wp, row, col))
2758 return TRUE;
2759 }
2760
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002761 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2762 if (wp->w_filter_cb.cb_name != NULL)
2763 res = invoke_popup_filter(wp, c);
2764
2765 return res;
2766}
2767
Bram Moolenaar3397f742019-06-02 18:40:06 +02002768/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002769 * Return TRUE if there is a popup visible with a filter callback and the
2770 * "mapping" property off.
2771 */
2772 int
2773popup_no_mapping(void)
2774{
2775 int round;
2776 win_T *wp;
2777
2778 for (round = 1; round <= 2; ++round)
2779 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2780 wp != NULL; wp = wp->w_next)
2781 if (wp->w_filter_cb.cb_name != NULL
2782 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2783 return TRUE;
2784 return FALSE;
2785}
2786
2787/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002788 * Called when the cursor moved: check if any popup needs to be closed if the
2789 * cursor moved far enough.
2790 */
2791 void
2792popup_check_cursor_pos()
2793{
2794 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002795
2796 popup_reset_handled();
2797 while ((wp = find_next_popup(TRUE)) != NULL)
2798 if (wp->w_popup_curwin != NULL
2799 && (curwin != wp->w_popup_curwin
2800 || curwin->w_cursor.lnum != wp->w_popup_lnum
2801 || curwin->w_cursor.col < wp->w_popup_mincol
2802 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002803 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002804}
2805
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002806/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002807 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002808 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002809 static void
2810popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002811{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002812 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002813 char_u *cells;
2814 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002815
2816 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002817 return;
2818 if (wp->w_popup_mask_cells != NULL
2819 && wp->w_popup_mask_height == height
2820 && wp->w_popup_mask_width == width)
2821 return; // cache is still valid
2822
2823 vim_free(wp->w_popup_mask_cells);
2824 wp->w_popup_mask_cells = alloc_clear(width * height);
2825 if (wp->w_popup_mask_cells == NULL)
2826 return;
2827 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002828
2829 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2830 {
2831 int cols, cole;
2832 int lines, linee;
2833
2834 li = lio->li_tv.vval.v_list->lv_first;
2835 cols = tv_get_number(&li->li_tv);
2836 if (cols < 0)
2837 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002838 li = li->li_next;
2839 cole = tv_get_number(&li->li_tv);
2840 if (cole < 0)
2841 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002842 li = li->li_next;
2843 lines = tv_get_number(&li->li_tv);
2844 if (lines < 0)
2845 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002846 li = li->li_next;
2847 linee = tv_get_number(&li->li_tv);
2848 if (linee < 0)
2849 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002850
2851 for (row = lines - 1; row < linee && row < height; ++row)
2852 for (col = cols - 1; col < cole && col < width; ++col)
2853 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002854 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002855}
2856
2857/*
2858 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2859 * "col" and "line" are screen coordinates.
2860 */
2861 static int
2862popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2863{
2864 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2865 int line = screenline - wp->w_winrow;
2866
2867 return col >= 0 && col < width
2868 && line >= 0 && line < height
2869 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002870}
2871
2872/*
2873 * Set flags in popup_transparent[] for window "wp" to "val".
2874 */
2875 static void
2876update_popup_transparent(win_T *wp, int val)
2877{
2878 if (wp->w_popup_mask != NULL)
2879 {
2880 int width = popup_width(wp);
2881 int height = popup_height(wp);
2882 listitem_T *lio, *li;
2883 int cols, cole;
2884 int lines, linee;
2885 int col, line;
2886
2887 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2888 {
2889 li = lio->li_tv.vval.v_list->lv_first;
2890 cols = tv_get_number(&li->li_tv);
2891 if (cols < 0)
2892 cols = width + cols + 1;
2893 li = li->li_next;
2894 cole = tv_get_number(&li->li_tv);
2895 if (cole < 0)
2896 cole = width + cole + 1;
2897 li = li->li_next;
2898 lines = tv_get_number(&li->li_tv);
2899 if (lines < 0)
2900 lines = height + lines + 1;
2901 li = li->li_next;
2902 linee = tv_get_number(&li->li_tv);
2903 if (linee < 0)
2904 linee = height + linee + 1;
2905
2906 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002907 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002908 if (cols < 0)
2909 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002910 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002911 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002912 if (lines < 0)
2913 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002914 for (line = lines; line < linee
2915 && line + wp->w_winrow < screen_Rows; ++line)
2916 for (col = cols; col < cole
2917 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002918 popup_transparent[(line + wp->w_winrow) * screen_Columns
2919 + col + wp->w_wincol] = val;
2920 }
2921 }
2922}
2923
2924/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02002925 * Only called when popup window "wp" is hidden: If the window is positioned
2926 * next to a text property, and it is now visible, then unhide the popup.
2927 * We don't check if visible popups become hidden, that is done in
2928 * popup_adjust_position().
2929 * Return TRUE if the popup became unhidden.
2930 */
2931 static int
2932check_popup_unhidden(win_T *wp)
2933{
2934 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
2935 {
2936 textprop_T prop;
2937 linenr_T lnum;
2938
2939 if (find_visible_prop(wp->w_popup_prop_win,
2940 wp->w_popup_prop_type, wp->w_popup_prop_id,
2941 &prop, &lnum) == OK)
2942 {
2943 wp->w_popup_flags &= ~POPF_HIDDEN;
2944 ++wp->w_buffer->b_nwindows;
2945 wp->w_popup_prop_topline = 0; // force repositioning
2946 return TRUE;
2947 }
2948 }
2949 return FALSE;
2950}
2951
2952/*
2953 * Return TRUE if popup_adjust_position() needs to be called for "wp".
2954 * That is when the buffer in the popup was changed, or the popup is following
2955 * a textprop and the referenced buffer was changed.
2956 */
2957 static int
2958popup_need_position_adjust(win_T *wp)
2959{
2960 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2961 return TRUE;
2962 if (win_valid(wp->w_popup_prop_win))
2963 return wp->w_popup_prop_changedtick
2964 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
2965 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline;
2966 return FALSE;
2967}
2968
2969/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002970 * Update "popup_mask" if needed.
2971 * Also recomputes the popup size and positions.
2972 * Also updates "popup_visible".
2973 * Also marks window lines for redrawing.
2974 */
2975 void
2976may_update_popup_mask(int type)
2977{
2978 win_T *wp;
2979 short *mask;
2980 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02002981 int redraw_all_popups = FALSE;
2982 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002983
2984 // Need to recompute when switching tabs.
2985 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2986 // (such as the screen size) must have changed.
2987 if (popup_mask_tab != curtab || type >= NOT_VALID)
2988 {
2989 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02002990 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002991 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02002992
2993 // Check if any popup window buffer has changed and if any popup connected
2994 // to a text property has become visible.
2995 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2996 if (wp->w_popup_flags & POPF_HIDDEN)
2997 popup_mask_refresh |= check_popup_unhidden(wp);
2998 else if (popup_need_position_adjust(wp))
2999 popup_mask_refresh = TRUE;
3000 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3001 if (wp->w_popup_flags & POPF_HIDDEN)
3002 popup_mask_refresh |= check_popup_unhidden(wp);
3003 else if (popup_need_position_adjust(wp))
3004 popup_mask_refresh = TRUE;
3005
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003006 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003007 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003008
3009 // Need to update the mask, something has changed.
3010 popup_mask_refresh = FALSE;
3011 popup_mask_tab = curtab;
3012 popup_visible = FALSE;
3013
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003014 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003015 // If redrawing only what is needed, update "popup_mask_next" and then
3016 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003017 redrawing_all_win = TRUE;
3018 FOR_ALL_WINDOWS(wp)
3019 if (wp->w_redr_type < SOME_VALID)
3020 redrawing_all_win = FALSE;
3021 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003022 mask = popup_mask;
3023 else
3024 mask = popup_mask_next;
3025 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3026
3027 // Find the window with the lowest zindex that hasn't been handled yet,
3028 // so that the window with a higher zindex overwrites the value in
3029 // popup_mask.
3030 popup_reset_handled();
3031 while ((wp = find_next_popup(TRUE)) != NULL)
3032 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003033 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003034 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003035
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003036 popup_visible = TRUE;
3037
Bram Moolenaar12034e22019-08-25 22:25:02 +02003038 // Recompute the position if the text changed. It may make the popup
3039 // hidden if it's attach to a text property that is no longer visible.
3040 if (redraw_all_popups || popup_need_position_adjust(wp))
3041 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003042 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003043 if (wp->w_popup_flags & POPF_HIDDEN)
3044 continue;
3045 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003046
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003047 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003048 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003049 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003050 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003051 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003052 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003053 col < wp->w_wincol + width - wp->w_popup_leftoff
3054 && col < screen_Columns; ++col)
3055 if (wp->w_popup_mask_cells == NULL
3056 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003057 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003058 }
3059
3060 // Only check which lines are to be updated if not already
3061 // updating all lines.
3062 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003063 {
3064 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3065 win_T *prev_wp = NULL;
3066
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003067 for (line = 0; line < screen_Rows; ++line)
3068 {
3069 int col_done = 0;
3070
3071 for (col = 0; col < screen_Columns; ++col)
3072 {
3073 int off = line * screen_Columns + col;
3074
3075 if (popup_mask[off] != popup_mask_next[off])
3076 {
3077 popup_mask[off] = popup_mask_next[off];
3078
3079 if (line >= cmdline_row)
3080 {
3081 // the command line needs to be cleared if text below
3082 // the popup is now visible.
3083 if (!msg_scrolled && popup_mask_next[off] == 0)
3084 clear_cmdline = TRUE;
3085 }
3086 else if (col >= col_done)
3087 {
3088 linenr_T lnum;
3089 int line_cp = line;
3090 int col_cp = col;
3091
3092 // The screen position "line" / "col" needs to be
3093 // redrawn. Figure out what window that is and update
3094 // w_redraw_top and w_redr_bot. Only needs to be done
3095 // once for each window line.
3096 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3097 if (wp != NULL)
3098 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003099 if (wp != prev_wp)
3100 {
3101 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3102 prev_wp = wp;
3103 }
3104
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003105 if (line_cp >= wp->w_height)
3106 // In (or below) status line
3107 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003108 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003109 {
3110 // compute the position in the buffer line from
3111 // the position in the window
3112 mouse_comp_pos(wp, &line_cp, &col_cp,
3113 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003114 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003115 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003116
3117 // This line is going to be redrawn, no need to
3118 // check until the right side of the window.
3119 col_done = wp->w_wincol + wp->w_width - 1;
3120 }
3121 }
3122 }
3123 }
3124 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003125
3126 vim_free(plines_cache);
3127 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003128}
3129
3130/*
3131 * Return a string of "len" spaces in IObuff.
3132 */
3133 static char_u *
3134get_spaces(int len)
3135{
3136 vim_memset(IObuff, ' ', (size_t)len);
3137 IObuff[len] = NUL;
3138 return IObuff;
3139}
3140
3141/*
3142 * Update popup windows. They are drawn on top of normal windows.
3143 * "win_update" is called for each popup window, lowest zindex first.
3144 */
3145 void
3146update_popups(void (*win_update)(win_T *wp))
3147{
3148 win_T *wp;
3149 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003150 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003151 int total_width;
3152 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003153 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003154 int popup_attr;
3155 int border_attr[4];
3156 int border_char[8];
3157 char_u buf[MB_MAXBYTES];
3158 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003159 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003160 int padcol = 0;
3161 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003162 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003163 int sb_thumb_top = 0;
3164 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003165 int attr_scroll = 0;
3166 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003167
3168 // Find the window with the lowest zindex that hasn't been updated yet,
3169 // so that the window with a higher zindex is drawn later, thus goes on
3170 // top.
3171 popup_reset_handled();
3172 while ((wp = find_next_popup(TRUE)) != NULL)
3173 {
3174 // This drawing uses the zindex of the popup window, so that it's on
3175 // top of the text but doesn't draw when another popup with higher
3176 // zindex is on top of the character.
3177 screen_zindex = wp->w_zindex;
3178
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003179 // Set flags in popup_transparent[] for masked cells.
3180 update_popup_transparent(wp, 1);
3181
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003182 // adjust w_winrow and w_wincol for border and padding, since
3183 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003184 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003185 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3186 - wp->w_popup_leftoff;
3187 if (wp->w_wincol + left_extra < 0)
3188 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003189 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003190 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003191
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003192 // Draw the popup text, unless it's off screen.
3193 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
3194 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003195
3196 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003197 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003198
Bram Moolenaard529ba52019-07-02 23:13:53 +02003199 total_width = popup_width(wp);
3200 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003201 popup_attr = get_wcr_attr(wp);
3202
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003203 if (wp->w_winrow + total_height > cmdline_row)
3204 wp->w_popup_flags |= POPF_ON_CMDLINE;
3205 else
3206 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3207
3208
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003209 // We can only use these line drawing characters when 'encoding' is
3210 // "utf-8" and 'ambiwidth' is "single".
3211 if (enc_utf8 && *p_ambw == 's')
3212 {
3213 border_char[0] = border_char[2] = 0x2550;
3214 border_char[1] = border_char[3] = 0x2551;
3215 border_char[4] = 0x2554;
3216 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003217 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3218 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003219 border_char[7] = 0x255a;
3220 }
3221 else
3222 {
3223 border_char[0] = border_char[2] = '-';
3224 border_char[1] = border_char[3] = '|';
3225 for (i = 4; i < 8; ++i)
3226 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003227 if (wp->w_popup_flags & POPF_RESIZE)
3228 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003229 }
3230 for (i = 0; i < 8; ++i)
3231 if (wp->w_border_char[i] != 0)
3232 border_char[i] = wp->w_border_char[i];
3233
3234 for (i = 0; i < 4; ++i)
3235 {
3236 border_attr[i] = popup_attr;
3237 if (wp->w_border_highlight[i] != NULL)
3238 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3239 }
3240
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003241 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003242 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003243 if (wp->w_popup_border[0] > 0)
3244 {
3245 // top border
3246 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003247 wincol < 0 ? 0 : wincol, wincol + total_width,
3248 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003249 ? border_char[4] : border_char[0],
3250 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003251 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003252 {
3253 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3254 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003255 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003256 }
3257 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003258 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3259 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003260
Bram Moolenaard529ba52019-07-02 23:13:53 +02003261 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3262 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003263 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003264 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3265 - wp->w_has_scrollbar;
3266 if (padcol < 0)
3267 {
3268 padwidth += padcol;
3269 padcol = 0;
3270 }
3271 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003272 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003273 {
3274 // top padding
3275 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003276 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003277 ' ', ' ', popup_attr);
3278 }
3279
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003280 // Title goes on top of border or padding.
3281 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003282 {
3283 int len = (int)STRLEN(wp->w_popup_title) + 1;
3284 char_u *title = alloc(len);
3285
3286 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3287 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003288 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003289 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003290
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003291 // Compute scrollbar thumb position and size.
3292 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003293 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003294 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3295
Bram Moolenaar68acb412019-06-26 03:40:36 +02003296 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3297 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003298 if (sb_thumb_height == 0)
3299 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003300 if (linecount <= wp->w_height)
3301 // it just fits, avoid divide by zero
3302 sb_thumb_top = 0;
3303 else
3304 sb_thumb_top = (wp->w_topline - 1
3305 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003306 * (wp->w_height - sb_thumb_height)
3307 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003308 if (wp->w_scrollbar_highlight != NULL)
3309 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3310 else
3311 attr_scroll = highlight_attr[HLF_PSB];
3312 if (wp->w_thumb_highlight != NULL)
3313 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3314 else
3315 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003316 }
3317
3318 for (i = wp->w_popup_border[0];
3319 i < total_height - wp->w_popup_border[2]; ++i)
3320 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003321 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003322 // left and right padding only needed next to the body
3323 int do_padding =
3324 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3325 && i < total_height - wp->w_popup_border[2]
3326 - wp->w_popup_padding[2];
3327
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003328 row = wp->w_winrow + i;
3329
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003330 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003331 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003332 {
3333 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003334 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003335 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003336 if (do_padding && wp->w_popup_padding[3] > 0)
3337 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003338 int col = wincol + wp->w_popup_border[3];
3339
Bram Moolenaard529ba52019-07-02 23:13:53 +02003340 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003341 pad_left = wp->w_popup_padding[3];
3342 if (col < 0)
3343 {
3344 pad_left += col;
3345 col = 0;
3346 }
3347 if (pad_left > 0)
3348 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3349 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003350 // scrollbar
3351 if (wp->w_has_scrollbar)
3352 {
3353 int line = i - top_off;
3354 int scroll_col = wp->w_wincol + total_width - 1
3355 - wp->w_popup_border[1];
3356
3357 if (line >= 0 && line < wp->w_height)
3358 screen_putchar(' ', row, scroll_col,
3359 line >= sb_thumb_top
3360 && line < sb_thumb_top + sb_thumb_height
3361 ? attr_thumb : attr_scroll);
3362 else
3363 screen_putchar(' ', row, scroll_col, popup_attr);
3364 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003365 // right border
3366 if (wp->w_popup_border[1] > 0)
3367 {
3368 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003369 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003370 }
3371 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003372 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003373 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003374 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003375 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3376 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003377 }
3378
3379 if (wp->w_popup_padding[2] > 0)
3380 {
3381 // bottom padding
3382 row = wp->w_winrow + wp->w_popup_border[0]
3383 + wp->w_popup_padding[0] + wp->w_height;
3384 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003385 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003386 }
3387
3388 if (wp->w_popup_border[2] > 0)
3389 {
3390 // bottom border
3391 row = wp->w_winrow + total_height - 1;
3392 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003393 wincol < 0 ? 0 : wincol,
3394 wincol + total_width,
3395 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003396 ? border_char[7] : border_char[2],
3397 border_char[2], border_attr[2]);
3398 if (wp->w_popup_border[1] > 0)
3399 {
3400 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003401 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003402 }
3403 }
3404
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003405 if (wp->w_popup_close == POPCLOSE_BUTTON)
3406 {
3407 // close button goes on top of anything at the top-right corner
3408 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003409 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003410 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3411 }
3412
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003413 update_popup_transparent(wp, 0);
3414
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003415 // Back to the normal zindex.
3416 screen_zindex = 0;
3417 }
3418}
3419
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003420/*
3421 * Mark references in callbacks of one popup window.
3422 */
3423 static int
3424set_ref_in_one_popup(win_T *wp, int copyID)
3425{
3426 int abort = FALSE;
3427 typval_T tv;
3428
3429 if (wp->w_close_cb.cb_partial != NULL)
3430 {
3431 tv.v_type = VAR_PARTIAL;
3432 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3433 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3434 }
3435 if (wp->w_filter_cb.cb_partial != NULL)
3436 {
3437 tv.v_type = VAR_PARTIAL;
3438 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3439 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3440 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003441 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003442 return abort;
3443}
3444
3445/*
3446 * Set reference in callbacks of popup windows.
3447 */
3448 int
3449set_ref_in_popups(int copyID)
3450{
3451 int abort = FALSE;
3452 win_T *wp;
3453 tabpage_T *tp;
3454
3455 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3456 abort = abort || set_ref_in_one_popup(wp, copyID);
3457
3458 FOR_ALL_TABPAGES(tp)
3459 {
3460 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3461 abort = abort || set_ref_in_one_popup(wp, copyID);
3462 if (abort)
3463 break;
3464 }
3465 return abort;
3466}
Bram Moolenaar79648732019-07-18 21:43:07 +02003467
3468/*
3469 * Find an existing popup used as the preview window, in the current tab page.
3470 * Return NULL if not found.
3471 */
3472 win_T *
3473popup_find_preview_window(void)
3474{
3475 win_T *wp;
3476
3477 // Preview window popup is always local to tab page.
3478 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3479 if (wp->w_p_pvw)
3480 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003481 return NULL;
3482}
3483
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003484 int
3485popup_is_popup(win_T *wp)
3486{
3487 return wp->w_popup_flags != 0;
3488}
3489
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003490#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003491/*
3492 * Find an existing popup used as the info window, in the current tab page.
3493 * Return NULL if not found.
3494 */
3495 win_T *
3496popup_find_info_window(void)
3497{
3498 win_T *wp;
3499
3500 // info window popup is always local to tab page.
3501 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3502 if (wp->w_popup_flags & POPF_INFO)
3503 return wp;
3504 return NULL;
3505}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003506#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003507
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003508 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003509f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3510{
3511 win_T *wp = popup_find_info_window();
3512
3513 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3514}
3515
3516 void
3517f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003518{
3519 win_T *wp = popup_find_preview_window();
3520
3521 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003522}
3523
Bram Moolenaar79648732019-07-18 21:43:07 +02003524/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003525 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003526 * NOTE: this makes the popup the current window, so that the file can be
3527 * edited. However, it must not remain to be the current window, the caller
3528 * must make sure of that.
3529 */
3530 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003531popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003532{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003533 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003534
3535 if (wp == NULL)
3536 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003537 if (info)
3538 wp->w_popup_flags |= POPF_INFO;
3539 else
3540 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003541
3542 // Set the width to a reasonable value, so that w_topline can be computed.
3543 if (wp->w_minwidth > 0)
3544 wp->w_width = wp->w_minwidth;
3545 else if (wp->w_maxwidth > 0)
3546 wp->w_width = wp->w_maxwidth;
3547 else
3548 wp->w_width = curwin->w_width;
3549
3550 // Will switch to another buffer soon, dummy one can be wiped.
3551 wp->w_buffer->b_locked = FALSE;
3552
3553 win_enter(wp, FALSE);
3554 return OK;
3555}
3556
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003557#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003558/*
3559 * Close any preview popup.
3560 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003561 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003562popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003563{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003564 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003565
3566 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003567 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003568}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003569
3570/*
3571 * Hide the info popup.
3572 */
3573 void
3574popup_hide_info(void)
3575{
3576 win_T *wp = popup_find_info_window();
3577
3578 if (wp != NULL)
3579 popup_hide(wp);
3580}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003581#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003582
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003583/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003584 * Close any popup for a text property associated with "win".
3585 * Return TRUE if a popup was closed.
3586 */
3587 int
3588popup_win_closed(win_T *win)
3589{
3590 win_T *wp;
3591
3592 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3593 if (wp->w_popup_prop_win == win)
3594 {
3595 popup_close_with_retval(wp, -1);
3596 return TRUE;
3597 }
3598 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3599 if (wp->w_popup_prop_win == win)
3600 {
3601 popup_close_with_retval(wp, -1);
3602 return TRUE;
3603 }
3604 return FALSE;
3605}
3606
3607/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003608 * Set the title of the popup window to the file name.
3609 */
3610 void
3611popup_set_title(win_T *wp)
3612{
3613 if (wp->w_buffer->b_fname != NULL)
3614 {
3615 char_u dirname[MAXPATHL];
3616 size_t len;
3617
3618 mch_dirname(dirname, MAXPATHL);
3619 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3620
3621 vim_free(wp->w_popup_title);
3622 len = STRLEN(wp->w_buffer->b_fname) + 3;
3623 wp->w_popup_title = alloc((int)len);
3624 if (wp->w_popup_title != NULL)
3625 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3626 wp->w_buffer->b_fname);
3627 redraw_win_later(wp, VALID);
3628 }
3629}
3630
3631/*
3632 * If there is a preview window, update the title.
3633 * Used after changing directory.
3634 */
3635 void
3636popup_update_preview_title(void)
3637{
3638 win_T *wp = popup_find_preview_window();
3639
3640 if (wp != NULL)
3641 popup_set_title(wp);
3642}
3643
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003644#endif // FEAT_TEXT_PROP