blob: ea1a1e1e711a28fc56f04c7b0afda67abda4e527 [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
16#ifdef FEAT_TEXT_PROP
17
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 Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
81
82 nr = popup_options_one(dict, (char_u *)"line");
83 if (nr > 0)
84 wp->w_wantline = nr;
85 nr = popup_options_one(dict, (char_u *)"col");
86 if (nr > 0)
87 wp->w_wantcol = nr;
88
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020089 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
90
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020091 str = dict_get_string(dict, (char_u *)"pos", FALSE);
92 if (str != NULL)
93 {
94 for (nr = 0;
95 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
96 ++nr)
97 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
98 {
99 wp->w_popup_pos = poppos_entries[nr].pp_val;
100 nr = -1;
101 break;
102 }
103 if (nr != -1)
104 semsg(_(e_invarg2), str);
105 }
106}
107
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200108 static void
109get_padding_border(dict_T *dict, int *array, char *name, int max_val)
110{
111 dictitem_T *di;
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 di = dict_find(dict, (char_u *)name, -1);
114 if (di != NULL)
115 {
116 if (di->di_tv.v_type != VAR_LIST)
117 emsg(_(e_listreq));
118 else
119 {
120 list_T *list = di->di_tv.vval.v_list;
121 listitem_T *li;
122 int i;
123 int nr;
124
125 for (i = 0; i < 4; ++i)
126 array[i] = 1;
127 if (list != NULL)
128 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
129 ++i, li = li->li_next)
130 {
131 nr = (int)tv_get_number(&li->li_tv);
132 if (nr >= 0)
133 array[i] = nr > max_val ? max_val : nr;
134 }
135 }
136 }
137}
138
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200139/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200140 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200141 */
142 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200143set_moved_values(win_T *wp)
144{
145 wp->w_popup_curwin = curwin;
146 wp->w_popup_lnum = curwin->w_cursor.lnum;
147 wp->w_popup_mincol = curwin->w_cursor.col;
148 wp->w_popup_maxcol = curwin->w_cursor.col;
149}
150
151/*
152 * Used when popup options contain "moved" with "word" or "WORD".
153 */
154 static void
155set_moved_columns(win_T *wp, int flags)
156{
157 char_u *ptr;
158 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
159
160 if (len > 0)
161 {
162 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
163 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
164 }
165}
166
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200167/*
168 * Return TRUE if "row"/"col" is on the border of the popup.
169 * The values are relative to the top-left corner.
170 */
171 int
172popup_on_border(win_T *wp, int row, int col)
173{
174 return (row == 0 && wp->w_popup_border[0] > 0)
175 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
176 || (col == 0 && wp->w_popup_border[3] > 0)
177 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
178}
179
180// Values set when dragging a popup window starts.
181static int drag_start_row;
182static int drag_start_col;
183static int drag_start_wantline;
184static int drag_start_wantcol;
185
186/*
187 * Mouse down on border of popup window: start dragging it.
188 * Uses mouse_col and mouse_row.
189 */
190 void
191popup_start_drag(win_T *wp)
192{
193 drag_start_row = mouse_row;
194 drag_start_col = mouse_col;
195 // TODO: handle using different corner
196 if (wp->w_wantline == 0)
197 drag_start_wantline = wp->w_winrow + 1;
198 else
199 drag_start_wantline = wp->w_wantline;
200 if (wp->w_wantcol == 0)
201 drag_start_wantcol = wp->w_wincol + 1;
202 else
203 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200204
205 // Stop centering the popup
206 if (wp->w_popup_pos == POPPOS_CENTER)
207 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200208}
209
210/*
211 * Mouse moved while dragging a popup window: adjust the window popup position.
212 */
213 void
214popup_drag(win_T *wp)
215{
216 // The popup may be closed before dragging stops.
217 if (!win_valid_popup(wp))
218 return;
219
220 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
221 if (wp->w_wantline < 1)
222 wp->w_wantline = 1;
223 if (wp->w_wantline > Rows)
224 wp->w_wantline = Rows;
225 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
226 if (wp->w_wantcol < 1)
227 wp->w_wantcol = 1;
228 if (wp->w_wantcol > Columns)
229 wp->w_wantcol = Columns;
230
231 popup_adjust_position(wp);
232}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200233
234#if defined(FEAT_TIMERS)
235 static void
236popup_add_timeout(win_T *wp, int time)
237{
238 char_u cbbuf[50];
239 char_u *ptr = cbbuf;
240 typval_T tv;
241
242 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
243 "{_ -> popup_close(%d)}", wp->w_id);
244 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
245 {
246 wp->w_popup_timer = create_timer(time, 0);
247 wp->w_popup_timer->tr_callback = get_callback(&tv);
248 clear_tv(&tv);
249 }
250}
251#endif
252
Bram Moolenaar17627312019-06-02 19:53:44 +0200253/*
254 * Go through the options in "dict" and apply them to buffer "buf" displayed in
255 * popup window "wp".
256 */
257 static void
258apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200259{
Bram Moolenaar402502d2019-05-30 22:07:36 +0200260 int nr;
261 char_u *str;
262 dictitem_T *di;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200263 int i;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200264
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200265 di = dict_find(dict, (char_u *)"minwidth", -1);
266 if (di != NULL)
267 wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200268 wp->w_minheight = dict_get_number(dict, (char_u *)"minheight");
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200269 wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth");
270 wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight");
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200271
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200272 get_pos_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200273
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200274 di = dict_find(dict, (char_u *)"zindex", -1);
275 if (di != NULL)
276 {
277 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
278 if (wp->w_zindex < 1)
279 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
280 if (wp->w_zindex > 32000)
281 wp->w_zindex = 32000;
282 }
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200283
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200284#if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200285 // Add timer to close the popup after some time.
286 nr = dict_get_number(dict, (char_u *)"time");
287 if (nr > 0)
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200288 popup_add_timeout(wp, nr);
289#endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200290
Bram Moolenaar402502d2019-05-30 22:07:36 +0200291 // Option values resulting in setting an option.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200292 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200293 if (str != NULL)
294 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
295 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200296
Bram Moolenaar8d241042019-06-12 23:40:01 +0200297 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
298 if (wp->w_firstline < 1)
299 wp->w_firstline = 1;
300
Bram Moolenaar402502d2019-05-30 22:07:36 +0200301 di = dict_find(dict, (char_u *)"wrap", -1);
302 if (di != NULL)
303 {
304 nr = dict_get_number(dict, (char_u *)"wrap");
305 wp->w_p_wrap = nr != 0;
306 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200307
Bram Moolenaara42d9452019-06-15 21:46:30 +0200308 di = dict_find(dict, (char_u *)"drag", -1);
309 if (di != NULL)
310 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200311
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200312 di = dict_find(dict, (char_u *)"callback", -1);
313 if (di != NULL)
314 {
315 callback_T callback = get_callback(&di->di_tv);
316
317 if (callback.cb_name != NULL)
318 set_callback(&wp->w_close_cb, &callback);
319 }
320
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200321 di = dict_find(dict, (char_u *)"filter", -1);
322 if (di != NULL)
323 {
324 callback_T callback = get_callback(&di->di_tv);
325
326 if (callback.cb_name != NULL)
327 set_callback(&wp->w_filter_cb, &callback);
328 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200329
330 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
331 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200332
333 for (i = 0; i < 4; ++i)
334 VIM_CLEAR(wp->w_border_highlight[i]);
335 di = dict_find(dict, (char_u *)"borderhighlight", -1);
336 if (di != NULL)
337 {
338 if (di->di_tv.v_type != VAR_LIST)
339 emsg(_(e_listreq));
340 else
341 {
342 list_T *list = di->di_tv.vval.v_list;
343 listitem_T *li;
344
345 if (list != NULL)
346 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
347 ++i, li = li->li_next)
348 {
349 str = tv_get_string(&li->li_tv);
350 if (*str != NUL)
351 wp->w_border_highlight[i] = vim_strsave(str);
352 }
353 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
354 for (i = 1; i < 4; ++i)
355 wp->w_border_highlight[i] =
356 vim_strsave(wp->w_border_highlight[0]);
357 }
358 }
359
360 for (i = 0; i < 8; ++i)
361 wp->w_border_char[i] = 0;
362 di = dict_find(dict, (char_u *)"borderchars", -1);
363 if (di != NULL)
364 {
365 if (di->di_tv.v_type != VAR_LIST)
366 emsg(_(e_listreq));
367 else
368 {
369 list_T *list = di->di_tv.vval.v_list;
370 listitem_T *li;
371
372 if (list != NULL)
373 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
374 ++i, li = li->li_next)
375 {
376 str = tv_get_string(&li->li_tv);
377 if (*str != NUL)
378 wp->w_border_char[i] = mb_ptr2char(str);
379 }
380 if (list->lv_len == 1)
381 for (i = 1; i < 8; ++i)
382 wp->w_border_char[i] = wp->w_border_char[0];
383 if (list->lv_len == 2)
384 {
385 for (i = 4; i < 8; ++i)
386 wp->w_border_char[i] = wp->w_border_char[1];
387 for (i = 1; i < 4; ++i)
388 wp->w_border_char[i] = wp->w_border_char[0];
389 }
390 }
391 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200392
393 di = dict_find(dict, (char_u *)"moved", -1);
394 if (di != NULL)
395 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200396 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200397 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
398 {
399 char_u *s = di->di_tv.vval.v_string;
400 int flags = 0;
401
402 if (STRCMP(s, "word") == 0)
403 flags = FIND_IDENT | FIND_STRING;
404 else if (STRCMP(s, "WORD") == 0)
405 flags = FIND_STRING;
406 else if (STRCMP(s, "any") != 0)
407 semsg(_(e_invarg2), s);
408 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200409 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200410 }
411 else if (di->di_tv.v_type == VAR_LIST
412 && di->di_tv.vval.v_list != NULL
413 && di->di_tv.vval.v_list->lv_len == 2)
414 {
415 list_T *l = di->di_tv.vval.v_list;
416
417 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
418 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200423
424 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200425}
426
427/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 * Add lines to the popup from a list of strings.
429 */
430 static void
431add_popup_strings(buf_T *buf, list_T *l)
432{
433 listitem_T *li;
434 linenr_T lnum = 0;
435 char_u *p;
436
437 for (li = l->lv_first; li != NULL; li = li->li_next)
438 if (li->li_tv.v_type == VAR_STRING)
439 {
440 p = li->li_tv.vval.v_string;
441 ml_append_buf(buf, lnum++,
442 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
443 }
444}
445
446/*
447 * Add lines to the popup from a list of dictionaries.
448 */
449 static void
450add_popup_dicts(buf_T *buf, list_T *l)
451{
452 listitem_T *li;
453 listitem_T *pli;
454 linenr_T lnum = 0;
455 char_u *p;
456 dict_T *dict;
457
458 // first add the text lines
459 for (li = l->lv_first; li != NULL; li = li->li_next)
460 {
461 if (li->li_tv.v_type != VAR_DICT)
462 {
463 emsg(_(e_dictreq));
464 return;
465 }
466 dict = li->li_tv.vval.v_dict;
467 p = dict == NULL ? NULL
468 : dict_get_string(dict, (char_u *)"text", FALSE);
469 ml_append_buf(buf, lnum++,
470 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
471 }
472
473 // add the text properties
474 lnum = 1;
475 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
476 {
477 dictitem_T *di;
478 list_T *plist;
479
480 dict = li->li_tv.vval.v_dict;
481 di = dict_find(dict, (char_u *)"props", -1);
482 if (di != NULL)
483 {
484 if (di->di_tv.v_type != VAR_LIST)
485 {
486 emsg(_(e_listreq));
487 return;
488 }
489 plist = di->di_tv.vval.v_list;
490 if (plist != NULL)
491 {
492 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
493 {
494 if (pli->li_tv.v_type != VAR_DICT)
495 {
496 emsg(_(e_dictreq));
497 return;
498 }
499 dict = pli->li_tv.vval.v_dict;
500 if (dict != NULL)
501 {
502 int col = dict_get_number(dict, (char_u *)"col");
503
504 prop_add_common( lnum, col, dict, buf, NULL);
505 }
506 }
507 }
508 }
509 }
510}
511
512/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200513 * Return the height of popup window "wp", including border and padding.
514 */
515 int
516popup_height(win_T *wp)
517{
518 return wp->w_height
519 + wp->w_popup_padding[0] + wp->w_popup_border[0]
520 + wp->w_popup_padding[2] + wp->w_popup_border[2];
521}
522
523/*
524 * Return the width of popup window "wp", including border and padding.
525 */
526 int
527popup_width(win_T *wp)
528{
529 return wp->w_width
530 + wp->w_popup_padding[3] + wp->w_popup_border[3]
531 + wp->w_popup_padding[1] + wp->w_popup_border[1];
532}
533
534/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200535 * Adjust the position and size of the popup to fit on the screen.
536 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200537 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200538popup_adjust_position(win_T *wp)
539{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200540 linenr_T lnum;
541 int wrapped = 0;
542 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200543 int center_vert = FALSE;
544 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200545 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaar399d8982019-06-02 15:34:29 +0200546 int top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
547 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
548 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
549 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
550 int extra_height = top_extra + bot_extra;
551 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200552 int org_winrow = wp->w_winrow;
553 int org_wincol = wp->w_wincol;
554 int org_width = wp->w_width;
555 int org_height = wp->w_height;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200556
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200557 wp->w_winrow = 0;
558 wp->w_wincol = 0;
559 if (wp->w_popup_pos == POPPOS_CENTER)
560 {
561 // center after computing the size
562 center_vert = TRUE;
563 center_hor = TRUE;
564 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200565 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200566 {
567 if (wp->w_wantline == 0)
568 center_vert = TRUE;
569 else if (wp->w_popup_pos == POPPOS_TOPLEFT
570 || wp->w_popup_pos == POPPOS_TOPRIGHT)
571 {
572 wp->w_winrow = wp->w_wantline - 1;
573 if (wp->w_winrow >= Rows)
574 wp->w_winrow = Rows - 1;
575 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200576
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200577 if (wp->w_wantcol == 0)
578 center_hor = TRUE;
579 else if (wp->w_popup_pos == POPPOS_TOPLEFT
580 || wp->w_popup_pos == POPPOS_BOTLEFT)
581 {
582 wp->w_wincol = wp->w_wantcol - 1;
583 if (wp->w_wincol >= Columns - 3)
584 wp->w_wincol = Columns - 3;
585 }
586 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200587
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200588 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200589 // When left aligned use the space available, but shift to the left when we
590 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200591 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200592 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200593 {
594 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200595 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200596 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200597
Bram Moolenaar8d241042019-06-12 23:40:01 +0200598 // start at the desired first line
599 wp->w_topline = wp->w_firstline;
600 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
601 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
602
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200603 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200604 // Use a minimum width of one, so that something shows when there is no
605 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200606 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200607 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200608 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200609 {
610 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
611
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200612 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200613 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200614 while (len > maxwidth)
615 {
616 ++wrapped;
617 len -= maxwidth;
618 wp->w_width = maxwidth;
619 }
620 }
621 else if (len > maxwidth
622 && allow_adjust_left
623 && (wp->w_popup_pos == POPPOS_TOPLEFT
624 || wp->w_popup_pos == POPPOS_BOTLEFT))
625 {
626 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200627 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200628
Bram Moolenaar51c31312019-06-15 22:27:23 +0200629 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200630 {
631 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200632
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200633 len -= truncate_shift;
634 shift_by -= truncate_shift;
635 }
636
637 wp->w_wincol -= shift_by;
638 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200639 wp->w_width = maxwidth;
640 }
641 if (wp->w_width < len)
642 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200643 // do not use the width of lines we're not going to show
644 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
645 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
646 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200647 }
648
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200649 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
650 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200651 if (wp->w_width > maxwidth)
652 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200653 if (center_hor)
654 wp->w_wincol = (Columns - wp->w_width) / 2;
655 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
656 || wp->w_popup_pos == POPPOS_TOPRIGHT)
657 {
658 // Right aligned: move to the right if needed.
659 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200660 if (wp->w_width + extra_width < wp->w_wantcol)
661 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200662 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200663
Bram Moolenaar8d241042019-06-12 23:40:01 +0200664 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
665 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200666 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
667 wp->w_height = wp->w_minheight;
668 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
669 wp->w_height = wp->w_maxheight;
670 if (wp->w_height > Rows - wp->w_winrow)
671 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200672
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200673 if (center_vert)
674 wp->w_winrow = (Rows - wp->w_height) / 2;
675 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
676 || wp->w_popup_pos == POPPOS_BOTLEFT)
677 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200678 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200679 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200680 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200681 else
682 // not enough space, make top aligned
683 wp->w_winrow = wp->w_wantline + 1;
684 }
685
Bram Moolenaar17146962019-05-30 00:12:11 +0200686 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200687
688 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200689 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200690 if (org_winrow != wp->w_winrow
691 || org_wincol != wp->w_wincol
692 || org_width != wp->w_width
693 || org_height != wp->w_height)
694 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200695 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200696 popup_mask_refresh = TRUE;
697 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200698}
699
Bram Moolenaar17627312019-06-02 19:53:44 +0200700typedef enum
701{
702 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200703 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200704 TYPE_NOTIFICATION,
705 TYPE_DIALOG
Bram Moolenaar17627312019-06-02 19:53:44 +0200706} create_type_T;
707
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200708/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200709 * Make "buf" empty and set the contents to "text".
710 * Used by popup_create() and popup_settext().
711 */
712 static void
713popup_set_buffer_text(buf_T *buf, typval_T text)
714{
715 int lnum;
716
717 // Clear the buffer, then replace the lines.
718 curbuf = buf;
719 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
720 ml_delete(lnum, FALSE);
721 curbuf = curwin->w_buffer;
722
723 // Add text to the buffer.
724 if (text.v_type == VAR_STRING)
725 {
726 // just a string
727 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
728 }
729 else
730 {
731 list_T *l = text.vval.v_list;
732
733 if (l->lv_len > 0)
734 {
735 if (l->lv_first->li_tv.v_type == VAR_STRING)
736 // list of strings
737 add_popup_strings(buf, l);
738 else
739 // list of dictionaries
740 add_popup_dicts(buf, l);
741 }
742 }
743
744 // delete the line that was in the empty buffer
745 curbuf = buf;
746 ml_delete(buf->b_ml.ml_line_count, FALSE);
747 curbuf = curwin->w_buffer;
748}
749
750/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200751 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200752 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200753 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200754 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200755popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200756{
757 win_T *wp;
758 buf_T *buf;
759 dict_T *d;
760 int nr;
761
762 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200763 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
764 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200765 {
766 emsg(_(e_listreq));
767 return;
768 }
769 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
770 {
771 emsg(_(e_dictreq));
772 return;
773 }
774 d = argvars[1].vval.v_dict;
775
776 // Create the window and buffer.
777 wp = win_alloc_popup_win();
778 if (wp == NULL)
779 return;
780 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200781 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200782
783 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
784 if (buf == NULL)
785 return;
786 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200787
788 win_init_popup_win(wp, buf);
789
790 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200791 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200792 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200793 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200794 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200795 buf->b_p_ul = -1; // no undo
796 buf->b_p_swf = FALSE; // no swap file
797 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200798 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200799 wp->w_p_wrap = TRUE; // 'wrap' is default on
800
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200801 // Avoid that 'buftype' is reset when this buffer is entered.
802 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200803
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200804 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
805 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200806 else if (type == TYPE_NOTIFICATION)
807 nr = -1; // notifications are global by default
808 else
809 nr = 0;
810
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200811 if (nr == 0)
812 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200813 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200814 wp->w_next = curtab->tp_first_popupwin;
815 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816 }
817 else if (nr < 0)
818 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200819 win_T *prev = first_popupwin;
820
821 // Global popup: add at the end, so that it gets displayed on top of
822 // older ones with the same zindex. Matters for notifications.
823 if (first_popupwin == NULL)
824 first_popupwin = wp;
825 else
826 {
827 while (prev->w_next != NULL)
828 prev = prev->w_next;
829 prev->w_next = wp;
830 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200831 }
832 else
833 // TODO: find tab page "nr"
834 emsg("Not implemented yet");
835
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200836 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200837
Bram Moolenaar17627312019-06-02 19:53:44 +0200838 if (type == TYPE_ATCURSOR)
839 {
840 wp->w_popup_pos = POPPOS_BOTLEFT;
841 setcursor_mayforce(TRUE);
842 wp->w_wantline = screen_screenrow();
843 if (wp->w_wantline == 0) // cursor in first line
844 {
845 wp->w_wantline = 2;
846 wp->w_popup_pos = POPPOS_TOPLEFT;
847 }
848 wp->w_wantcol = screen_screencol() + 1;
849 set_moved_values(wp);
850 set_moved_columns(wp, FIND_STRING);
851 }
852
Bram Moolenaar33796b32019-06-08 16:01:13 +0200853 // set default values
854 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
855
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200856 if (type == TYPE_NOTIFICATION)
857 {
858 win_T *twp, *nextwin;
859 int height = buf->b_ml.ml_line_count + 3;
860 int i;
861
862 // Try to not overlap with another global popup. Guess we need 3
863 // more screen lines than buffer lines.
864 wp->w_wantline = 1;
865 for (twp = first_popupwin; twp != NULL; twp = nextwin)
866 {
867 nextwin = twp->w_next;
868 if (twp != wp
869 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
870 && twp->w_winrow <= wp->w_wantline - 1 + height
871 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
872 {
873 // move to below this popup and restart the loop to check for
874 // overlap with other popups
875 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
876 nextwin = first_popupwin;
877 }
878 }
879 if (wp->w_wantline + height > Rows)
880 {
881 // can't avoid overlap, put on top in the hope that message goes
882 // away soon.
883 wp->w_wantline = 1;
884 }
885
886 wp->w_wantcol = 10;
887 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200888 wp->w_minwidth = 20;
889 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200890 for (i = 0; i < 4; ++i)
891 wp->w_popup_border[i] = 1;
892 wp->w_popup_padding[1] = 1;
893 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200894
895 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200896 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200897 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
898 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200899 }
900
Bram Moolenaara42d9452019-06-15 21:46:30 +0200901 if (type == TYPE_DIALOG)
902 {
903 int i;
904
905 wp->w_popup_pos = POPPOS_CENTER;
906 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
907 wp->w_popup_drag = 1;
908 for (i = 0; i < 4; ++i)
909 {
910 wp->w_popup_border[i] = 1;
911 wp->w_popup_padding[i] = 1;
912 }
913 }
914
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200915 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200916 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200917
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200918 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
919 popup_add_timeout(wp, 3000);
920
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200921 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200922
923 wp->w_vsep_width = 0;
924
925 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200926 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200927}
928
929/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200930 * popup_clear()
931 */
932 void
933f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
934{
935 close_all_popups();
936}
937
938/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200939 * popup_create({text}, {options})
940 */
941 void
942f_popup_create(typval_T *argvars, typval_T *rettv)
943{
Bram Moolenaar17627312019-06-02 19:53:44 +0200944 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200945}
946
947/*
948 * popup_atcursor({text}, {options})
949 */
950 void
951f_popup_atcursor(typval_T *argvars, typval_T *rettv)
952{
Bram Moolenaar17627312019-06-02 19:53:44 +0200953 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200954}
955
956/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200957 * Invoke the close callback for window "wp" with value "result".
958 * Careful: The callback may make "wp" invalid!
959 */
960 static void
961invoke_popup_callback(win_T *wp, typval_T *result)
962{
963 typval_T rettv;
964 int dummy;
965 typval_T argv[3];
966
967 argv[0].v_type = VAR_NUMBER;
968 argv[0].vval.v_number = (varnumber_T)wp->w_id;
969
970 if (result != NULL && result->v_type != VAR_UNKNOWN)
971 copy_tv(result, &argv[1]);
972 else
973 {
974 argv[1].v_type = VAR_NUMBER;
975 argv[1].vval.v_number = 0;
976 }
977
978 argv[2].v_type = VAR_UNKNOWN;
979
980 call_callback(&wp->w_close_cb, -1,
981 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
982 if (result != NULL)
983 clear_tv(&argv[1]);
984 clear_tv(&rettv);
985}
986
987/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200988 * Close popup "wp" and invoke any close callback for it.
989 */
990 static void
991popup_close_and_callback(win_T *wp, typval_T *arg)
992{
993 int id = wp->w_id;
994
995 if (wp->w_close_cb.cb_name != NULL)
996 // Careful: This may make "wp" invalid.
997 invoke_popup_callback(wp, arg);
998
999 popup_close(id);
1000}
1001
1002/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001003 * popup_filter_yesno({text}, {options})
1004 */
1005 void
1006f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1007{
1008 int id = tv_get_number(&argvars[0]);
1009 win_T *wp = win_id2wp(id);
1010 char_u *key = tv_get_string(&argvars[1]);
1011 typval_T res;
1012
1013 // If the popup has been closed don't consume the key.
1014 if (wp == NULL)
1015 return;
1016
1017 // consume all keys until done
1018 rettv->vval.v_number = 1;
1019
1020 if (STRCMP(key, "y") == 0 || STRCMP(key, "Y") == 0)
1021 res.vval.v_number = 1;
1022 else if (STRCMP(key, "n") == 0 || STRCMP(key, "N") == 0
1023 || STRCMP(key, "x") == 0 || STRCMP(key, "X") == 0
1024 || STRCMP(key, "\x1b") == 0)
1025 res.vval.v_number = 0;
1026 else
1027 {
1028 int c = *key;
1029 int row = mouse_row;
1030 int col = mouse_col;
1031
1032 if (c == K_SPECIAL && key[1] != NUL)
1033 c = TO_SPECIAL(key[1], key[2]);
1034 if (wp->w_popup_drag
1035 && is_mouse_key(c)
1036 && (wp == popup_dragwin
1037 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1038 // allow for dragging the popup
1039 rettv->vval.v_number = 0;
1040
1041 // ignore this key
1042 return;
1043 }
1044
1045 // Invoke callback
1046 res.v_type = VAR_NUMBER;
1047 popup_close_and_callback(wp, &res);
1048}
1049
1050/*
1051 * popup_dialog({text}, {options})
1052 */
1053 void
1054f_popup_dialog(typval_T *argvars, typval_T *rettv)
1055{
1056 popup_create(argvars, rettv, TYPE_DIALOG);
1057}
1058
1059/*
1060 * popup_notification({text}, {options})
1061 */
1062 void
1063f_popup_notification(typval_T *argvars, typval_T *rettv)
1064{
1065 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1066}
1067
1068/*
1069 * Find the popup window with window-ID "id".
1070 * If the popup window does not exist NULL is returned.
1071 * If the window is not a popup window, and error message is given.
1072 */
1073 static win_T *
1074find_popup_win(int id)
1075{
1076 win_T *wp = win_id2wp(id);
1077
1078 if (wp != NULL && !bt_popup(wp->w_buffer))
1079 {
1080 semsg(_("E993: window %d is not a popup window"), id);
1081 return NULL;
1082 }
1083 return wp;
1084}
1085
1086/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001087 * popup_close({id})
1088 */
1089 void
1090f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1091{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001092 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001093 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001094
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001095 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001096 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001097}
1098
1099/*
1100 * popup_hide({id})
1101 */
1102 void
1103f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1104{
1105 int id = (int)tv_get_number(argvars);
1106 win_T *wp = find_popup_win(id);
1107
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001108 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001109 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001110 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001111 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001112 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001113 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001114 }
1115}
1116
1117/*
1118 * popup_show({id})
1119 */
1120 void
1121f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1122{
1123 int id = (int)tv_get_number(argvars);
1124 win_T *wp = find_popup_win(id);
1125
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001126 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001127 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001128 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001129 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001130 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001131 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001132 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001133}
1134
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001135/*
1136 * popup_settext({id}, {text})
1137 */
1138 void
1139f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1140{
1141 int id = (int)tv_get_number(&argvars[0]);
1142 win_T *wp = find_popup_win(id);
1143
1144 if (wp != NULL)
1145 {
1146 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1147 popup_adjust_position(wp);
1148 }
1149}
1150
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001151 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001152popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001153{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001154 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001155 if (wp->w_winrow + wp->w_height >= cmdline_row)
1156 clear_cmdline = TRUE;
1157 win_free_popup(wp);
1158 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001159 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001160}
1161
Bram Moolenaarec583842019-05-26 14:11:23 +02001162/*
1163 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001164 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001165 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001166 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001167popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001168{
1169 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001170 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001171 win_T *prev = NULL;
1172
Bram Moolenaarec583842019-05-26 14:11:23 +02001173 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001174 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001175 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001176 {
1177 if (prev == NULL)
1178 first_popupwin = wp->w_next;
1179 else
1180 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001181 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001182 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001183 }
1184
Bram Moolenaarec583842019-05-26 14:11:23 +02001185 // go through tab-local popups
1186 FOR_ALL_TABPAGES(tp)
1187 popup_close_tabpage(tp, id);
1188}
1189
1190/*
1191 * Close a popup window with Window-id "id" in tabpage "tp".
1192 */
1193 void
1194popup_close_tabpage(tabpage_T *tp, int id)
1195{
1196 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001197 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001198 win_T *prev = NULL;
1199
Bram Moolenaarec583842019-05-26 14:11:23 +02001200 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1201 if (wp->w_id == id)
1202 {
1203 if (prev == NULL)
1204 *root = wp->w_next;
1205 else
1206 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001207 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001208 return;
1209 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001210}
1211
1212 void
1213close_all_popups(void)
1214{
1215 while (first_popupwin != NULL)
1216 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001217 while (curtab->tp_first_popupwin != NULL)
1218 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001219}
1220
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001221/*
1222 * popup_move({id}, {options})
1223 */
1224 void
1225f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1226{
1227 dict_T *d;
1228 int nr;
1229 int id = (int)tv_get_number(argvars);
1230 win_T *wp = find_popup_win(id);
1231
1232 if (wp == NULL)
1233 return; // invalid {id}
1234
1235 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1236 {
1237 emsg(_(e_dictreq));
1238 return;
1239 }
1240 d = argvars[1].vval.v_dict;
1241
1242 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1243 wp->w_minwidth = nr;
1244 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1245 wp->w_minheight = nr;
1246 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1247 wp->w_maxwidth = nr;
1248 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1249 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001250 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001251
1252 if (wp->w_winrow + wp->w_height >= cmdline_row)
1253 clear_cmdline = TRUE;
1254 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001255}
1256
Bram Moolenaarbc133542019-05-29 20:26:46 +02001257/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001258 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001259 */
1260 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001261f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001262{
1263 dict_T *dict;
1264 int id = (int)tv_get_number(argvars);
1265 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001266 int top_extra;
1267 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001268
1269 if (rettv_dict_alloc(rettv) == OK)
1270 {
1271 if (wp == NULL)
1272 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001273 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1274 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1275
Bram Moolenaarbc133542019-05-29 20:26:46 +02001276 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001277
Bram Moolenaarbc133542019-05-29 20:26:46 +02001278 dict_add_number(dict, "line", wp->w_winrow + 1);
1279 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001280 dict_add_number(dict, "width", wp->w_width + left_extra
1281 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1282 dict_add_number(dict, "height", wp->w_height + top_extra
1283 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001284
1285 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1286 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1287 dict_add_number(dict, "core_width", wp->w_width);
1288 dict_add_number(dict, "core_height", wp->w_height);
1289
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001290 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001291 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001292 }
1293}
1294
1295/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001296 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001297 */
1298 void
1299f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1300{
1301 dict_T *dict;
1302 int id = (int)tv_get_number(argvars);
1303 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001304 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001305
1306 if (rettv_dict_alloc(rettv) == OK)
1307 {
1308 if (wp == NULL)
1309 return;
1310
1311 dict = rettv->vval.v_dict;
1312 dict_add_number(dict, "line", wp->w_wantline);
1313 dict_add_number(dict, "col", wp->w_wantcol);
1314 dict_add_number(dict, "minwidth", wp->w_minwidth);
1315 dict_add_number(dict, "minheight", wp->w_minheight);
1316 dict_add_number(dict, "maxheight", wp->w_maxheight);
1317 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001318 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001319 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001321
1322 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1323 ++i)
1324 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1325 {
1326 dict_add_string(dict, "pos",
1327 (char_u *)poppos_entries[i].pp_name);
1328 break;
1329 }
1330
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001331# if defined(FEAT_TIMERS)
1332 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1333 ? (long)wp->w_popup_timer->tr_interval : 0L);
1334# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001335 }
1336}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001337
1338 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001339error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001340{
1341 if (bt_popup(curwin->w_buffer))
1342 {
1343 emsg(_("E994: Not allowed in a popup window"));
1344 return TRUE;
1345 }
1346 return FALSE;
1347}
1348
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001349/*
1350 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001351 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001352 */
1353 void
1354popup_reset_handled()
1355{
1356 win_T *wp;
1357
1358 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1359 wp->w_popup_flags &= ~POPF_HANDLED;
1360 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1361 wp->w_popup_flags &= ~POPF_HANDLED;
1362}
1363
1364/*
1365 * Find the next visible popup where POPF_HANDLED is not set.
1366 * Must have called popup_reset_handled() first.
1367 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1368 * popup with the highest zindex.
1369 */
1370 win_T *
1371find_next_popup(int lowest)
1372{
1373 win_T *wp;
1374 win_T *found_wp;
1375 int found_zindex;
1376
1377 found_zindex = lowest ? INT_MAX : 0;
1378 found_wp = NULL;
1379 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1380 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1381 && (lowest ? wp->w_zindex < found_zindex
1382 : wp->w_zindex > found_zindex))
1383 {
1384 found_zindex = wp->w_zindex;
1385 found_wp = wp;
1386 }
1387 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1388 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1389 && (lowest ? wp->w_zindex < found_zindex
1390 : wp->w_zindex > found_zindex))
1391 {
1392 found_zindex = wp->w_zindex;
1393 found_wp = wp;
1394 }
1395
1396 if (found_wp != NULL)
1397 found_wp->w_popup_flags |= POPF_HANDLED;
1398 return found_wp;
1399}
1400
1401/*
1402 * Invoke the filter callback for window "wp" with typed character "c".
1403 * Uses the global "mod_mask" for modifiers.
1404 * Returns the return value of the filter.
1405 * Careful: The filter may make "wp" invalid!
1406 */
1407 static int
1408invoke_popup_filter(win_T *wp, int c)
1409{
1410 int res;
1411 typval_T rettv;
1412 int dummy;
1413 typval_T argv[3];
1414 char_u buf[NUMBUFLEN];
1415
Bram Moolenaara42d9452019-06-15 21:46:30 +02001416 // Emergency exit: CTRL-C closes the popup.
1417 if (c == Ctrl_C)
1418 {
1419 rettv.v_type = VAR_NUMBER;
1420 rettv.vval.v_number = -1;
1421 popup_close_and_callback(wp, &rettv);
1422 return 1;
1423 }
1424
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001425 argv[0].v_type = VAR_NUMBER;
1426 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1427
1428 // Convert the number to a string, so that the function can use:
1429 // if a:c == "\<F2>"
1430 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1431 argv[1].v_type = VAR_STRING;
1432 argv[1].vval.v_string = vim_strsave(buf);
1433
1434 argv[2].v_type = VAR_UNKNOWN;
1435
Bram Moolenaara42d9452019-06-15 21:46:30 +02001436 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001437 call_callback(&wp->w_filter_cb, -1,
1438 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1439 res = tv_get_number(&rettv);
1440 vim_free(argv[1].vval.v_string);
1441 clear_tv(&rettv);
1442 return res;
1443}
1444
1445/*
1446 * Called when "c" was typed: invoke popup filter callbacks.
1447 * Returns TRUE when the character was consumed,
1448 */
1449 int
1450popup_do_filter(int c)
1451{
1452 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001453 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001454
1455 popup_reset_handled();
1456
1457 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1458 if (wp->w_filter_cb.cb_name != NULL)
1459 res = invoke_popup_filter(wp, c);
1460
1461 return res;
1462}
1463
Bram Moolenaar3397f742019-06-02 18:40:06 +02001464/*
1465 * Called when the cursor moved: check if any popup needs to be closed if the
1466 * cursor moved far enough.
1467 */
1468 void
1469popup_check_cursor_pos()
1470{
1471 win_T *wp;
1472 typval_T tv;
1473
1474 popup_reset_handled();
1475 while ((wp = find_next_popup(TRUE)) != NULL)
1476 if (wp->w_popup_curwin != NULL
1477 && (curwin != wp->w_popup_curwin
1478 || curwin->w_cursor.lnum != wp->w_popup_lnum
1479 || curwin->w_cursor.col < wp->w_popup_mincol
1480 || curwin->w_cursor.col > wp->w_popup_maxcol))
1481 {
1482 tv.v_type = VAR_NUMBER;
1483 tv.vval.v_number = -1;
1484 popup_close_and_callback(wp, &tv);
1485 }
1486}
1487
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001488/*
1489 * Update "popup_mask" if needed.
1490 * Also recomputes the popup size and positions.
1491 * Also updates "popup_visible".
1492 * Also marks window lines for redrawing.
1493 */
1494 void
1495may_update_popup_mask(int type)
1496{
1497 win_T *wp;
1498 short *mask;
1499 int line, col;
1500 int redraw_all = FALSE;
1501
1502 // Need to recompute when switching tabs.
1503 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1504 // (such as the screen size) must have changed.
1505 if (popup_mask_tab != curtab || type >= NOT_VALID)
1506 {
1507 popup_mask_refresh = TRUE;
1508 redraw_all = TRUE;
1509 }
1510 if (!popup_mask_refresh)
1511 {
1512 // Check if any buffer has changed.
1513 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1514 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1515 popup_mask_refresh = TRUE;
1516 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1517 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1518 popup_mask_refresh = TRUE;
1519 if (!popup_mask_refresh)
1520 return;
1521 }
1522
1523 // Need to update the mask, something has changed.
1524 popup_mask_refresh = FALSE;
1525 popup_mask_tab = curtab;
1526 popup_visible = FALSE;
1527
1528 // If redrawing everything, just update "popup_mask".
1529 // If redrawing only what is needed, update "popup_mask_next" and then
1530 // compare with "popup_mask" to see what changed.
1531 if (type >= SOME_VALID)
1532 mask = popup_mask;
1533 else
1534 mask = popup_mask_next;
1535 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1536
1537 // Find the window with the lowest zindex that hasn't been handled yet,
1538 // so that the window with a higher zindex overwrites the value in
1539 // popup_mask.
1540 popup_reset_handled();
1541 while ((wp = find_next_popup(TRUE)) != NULL)
1542 {
1543 popup_visible = TRUE;
1544
1545 // Recompute the position if the text changed.
1546 if (redraw_all
1547 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1548 popup_adjust_position(wp);
1549
1550 for (line = wp->w_winrow;
1551 line < wp->w_winrow + popup_height(wp)
1552 && line < screen_Rows; ++line)
1553 for (col = wp->w_wincol;
1554 col < wp->w_wincol + popup_width(wp)
1555 && col < screen_Columns; ++col)
1556 mask[line * screen_Columns + col] = wp->w_zindex;
1557 }
1558
1559 // Only check which lines are to be updated if not already
1560 // updating all lines.
1561 if (mask == popup_mask_next)
1562 for (line = 0; line < screen_Rows; ++line)
1563 {
1564 int col_done = 0;
1565
1566 for (col = 0; col < screen_Columns; ++col)
1567 {
1568 int off = line * screen_Columns + col;
1569
1570 if (popup_mask[off] != popup_mask_next[off])
1571 {
1572 popup_mask[off] = popup_mask_next[off];
1573
1574 if (line >= cmdline_row)
1575 {
1576 // the command line needs to be cleared if text below
1577 // the popup is now visible.
1578 if (!msg_scrolled && popup_mask_next[off] == 0)
1579 clear_cmdline = TRUE;
1580 }
1581 else if (col >= col_done)
1582 {
1583 linenr_T lnum;
1584 int line_cp = line;
1585 int col_cp = col;
1586
1587 // The screen position "line" / "col" needs to be
1588 // redrawn. Figure out what window that is and update
1589 // w_redraw_top and w_redr_bot. Only needs to be done
1590 // once for each window line.
1591 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1592 if (wp != NULL)
1593 {
1594 if (line_cp >= wp->w_height)
1595 // In (or below) status line
1596 wp->w_redr_status = TRUE;
1597 // compute the position in the buffer line from the
1598 // position on the screen
1599 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1600 &lnum))
1601 // past bottom
1602 wp->w_redr_status = TRUE;
1603 else
1604 redrawWinline(wp, lnum);
1605
1606 // This line is going to be redrawn, no need to
1607 // check until the right side of the window.
1608 col_done = wp->w_wincol + wp->w_width - 1;
1609 }
1610 }
1611 }
1612 }
1613 }
1614}
1615
1616/*
1617 * Return a string of "len" spaces in IObuff.
1618 */
1619 static char_u *
1620get_spaces(int len)
1621{
1622 vim_memset(IObuff, ' ', (size_t)len);
1623 IObuff[len] = NUL;
1624 return IObuff;
1625}
1626
1627/*
1628 * Update popup windows. They are drawn on top of normal windows.
1629 * "win_update" is called for each popup window, lowest zindex first.
1630 */
1631 void
1632update_popups(void (*win_update)(win_T *wp))
1633{
1634 win_T *wp;
1635 int top_off;
1636 int left_off;
1637 int total_width;
1638 int total_height;
1639 int popup_attr;
1640 int border_attr[4];
1641 int border_char[8];
1642 char_u buf[MB_MAXBYTES];
1643 int row;
1644 int i;
1645
1646 // Find the window with the lowest zindex that hasn't been updated yet,
1647 // so that the window with a higher zindex is drawn later, thus goes on
1648 // top.
1649 popup_reset_handled();
1650 while ((wp = find_next_popup(TRUE)) != NULL)
1651 {
1652 // This drawing uses the zindex of the popup window, so that it's on
1653 // top of the text but doesn't draw when another popup with higher
1654 // zindex is on top of the character.
1655 screen_zindex = wp->w_zindex;
1656
1657 // adjust w_winrow and w_wincol for border and padding, since
1658 // win_update() doesn't handle them.
1659 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1660 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1661 wp->w_winrow += top_off;
1662 wp->w_wincol += left_off;
1663
1664 // Draw the popup text.
1665 win_update(wp);
1666
1667 wp->w_winrow -= top_off;
1668 wp->w_wincol -= left_off;
1669
1670 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1671 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1672 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1673 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1674 popup_attr = get_wcr_attr(wp);
1675
1676 // We can only use these line drawing characters when 'encoding' is
1677 // "utf-8" and 'ambiwidth' is "single".
1678 if (enc_utf8 && *p_ambw == 's')
1679 {
1680 border_char[0] = border_char[2] = 0x2550;
1681 border_char[1] = border_char[3] = 0x2551;
1682 border_char[4] = 0x2554;
1683 border_char[5] = 0x2557;
1684 border_char[6] = 0x255d;
1685 border_char[7] = 0x255a;
1686 }
1687 else
1688 {
1689 border_char[0] = border_char[2] = '-';
1690 border_char[1] = border_char[3] = '|';
1691 for (i = 4; i < 8; ++i)
1692 border_char[i] = '+';
1693 }
1694 for (i = 0; i < 8; ++i)
1695 if (wp->w_border_char[i] != 0)
1696 border_char[i] = wp->w_border_char[i];
1697
1698 for (i = 0; i < 4; ++i)
1699 {
1700 border_attr[i] = popup_attr;
1701 if (wp->w_border_highlight[i] != NULL)
1702 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1703 }
1704
1705 if (wp->w_popup_border[0] > 0)
1706 {
1707 // top border
1708 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1709 wp->w_wincol,
1710 wp->w_wincol + total_width,
1711 wp->w_popup_border[3] != 0
1712 ? border_char[4] : border_char[0],
1713 border_char[0], border_attr[0]);
1714 if (wp->w_popup_border[1] > 0)
1715 {
1716 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1717 screen_puts(buf, wp->w_winrow,
1718 wp->w_wincol + total_width - 1, border_attr[1]);
1719 }
1720 }
1721
1722 if (wp->w_popup_padding[0] > 0)
1723 {
1724 // top padding
1725 row = wp->w_winrow + wp->w_popup_border[0];
1726 screen_fill(row, row + wp->w_popup_padding[0],
1727 wp->w_wincol + wp->w_popup_border[3],
1728 wp->w_wincol + total_width - wp->w_popup_border[1],
1729 ' ', ' ', popup_attr);
1730 }
1731
1732 for (row = wp->w_winrow + wp->w_popup_border[0];
1733 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1734 ++row)
1735 {
1736 // left border
1737 if (wp->w_popup_border[3] > 0)
1738 {
1739 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1740 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1741 }
1742 // left padding
1743 if (wp->w_popup_padding[3] > 0)
1744 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1745 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1746 // right border
1747 if (wp->w_popup_border[1] > 0)
1748 {
1749 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1750 screen_puts(buf, row,
1751 wp->w_wincol + total_width - 1, border_attr[1]);
1752 }
1753 // right padding
1754 if (wp->w_popup_padding[1] > 0)
1755 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1756 wp->w_wincol + wp->w_popup_border[3]
1757 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1758 }
1759
1760 if (wp->w_popup_padding[2] > 0)
1761 {
1762 // bottom padding
1763 row = wp->w_winrow + wp->w_popup_border[0]
1764 + wp->w_popup_padding[0] + wp->w_height;
1765 screen_fill(row, row + wp->w_popup_padding[2],
1766 wp->w_wincol + wp->w_popup_border[3],
1767 wp->w_wincol + total_width - wp->w_popup_border[1],
1768 ' ', ' ', popup_attr);
1769 }
1770
1771 if (wp->w_popup_border[2] > 0)
1772 {
1773 // bottom border
1774 row = wp->w_winrow + total_height - 1;
1775 screen_fill(row , row + 1,
1776 wp->w_wincol,
1777 wp->w_wincol + total_width,
1778 wp->w_popup_border[3] != 0
1779 ? border_char[7] : border_char[2],
1780 border_char[2], border_attr[2]);
1781 if (wp->w_popup_border[1] > 0)
1782 {
1783 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1784 screen_puts(buf, row,
1785 wp->w_wincol + total_width - 1, border_attr[2]);
1786 }
1787 }
1788
1789 // Back to the normal zindex.
1790 screen_zindex = 0;
1791 }
1792}
1793
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001794#endif // FEAT_TEXT_PROP