blob: c0b5e1300cea66557758892e8ecbb7d244f8a704 [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.
604 // TODO: more accurate wrapping
605 wp->w_width = 0;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200606 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200607 {
608 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
609
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200610 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200611 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200612 while (len > maxwidth)
613 {
614 ++wrapped;
615 len -= maxwidth;
616 wp->w_width = maxwidth;
617 }
618 }
619 else if (len > maxwidth
620 && allow_adjust_left
621 && (wp->w_popup_pos == POPPOS_TOPLEFT
622 || wp->w_popup_pos == POPPOS_BOTLEFT))
623 {
624 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200625 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200626
Bram Moolenaar51c31312019-06-15 22:27:23 +0200627 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200628 {
629 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200630
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200631 len -= truncate_shift;
632 shift_by -= truncate_shift;
633 }
634
635 wp->w_wincol -= shift_by;
636 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200637 wp->w_width = maxwidth;
638 }
639 if (wp->w_width < len)
640 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200641 // do not use the width of lines we're not going to show
642 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
643 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
644 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200645 }
646
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200647 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
648 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200649 if (wp->w_width > maxwidth)
650 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200651 if (center_hor)
652 wp->w_wincol = (Columns - wp->w_width) / 2;
653 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
654 || wp->w_popup_pos == POPPOS_TOPRIGHT)
655 {
656 // Right aligned: move to the right if needed.
657 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200658 if (wp->w_width + extra_width < wp->w_wantcol)
659 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200660 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200661
Bram Moolenaar8d241042019-06-12 23:40:01 +0200662 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
663 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200664 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
665 wp->w_height = wp->w_minheight;
666 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
667 wp->w_height = wp->w_maxheight;
668 if (wp->w_height > Rows - wp->w_winrow)
669 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200670
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200671 if (center_vert)
672 wp->w_winrow = (Rows - wp->w_height) / 2;
673 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
674 || wp->w_popup_pos == POPPOS_BOTLEFT)
675 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200676 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200677 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200678 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200679 else
680 // not enough space, make top aligned
681 wp->w_winrow = wp->w_wantline + 1;
682 }
683
Bram Moolenaar17146962019-05-30 00:12:11 +0200684 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200685
686 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200687 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200688 if (org_winrow != wp->w_winrow
689 || org_wincol != wp->w_wincol
690 || org_width != wp->w_width
691 || org_height != wp->w_height)
692 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200693 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200694 popup_mask_refresh = TRUE;
695 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200696}
697
Bram Moolenaar17627312019-06-02 19:53:44 +0200698typedef enum
699{
700 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200701 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200702 TYPE_NOTIFICATION,
703 TYPE_DIALOG
Bram Moolenaar17627312019-06-02 19:53:44 +0200704} create_type_T;
705
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200706/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200707 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200708 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200709 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200710 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200711popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200712{
713 win_T *wp;
714 buf_T *buf;
715 dict_T *d;
716 int nr;
717
718 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200719 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
720 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200721 {
722 emsg(_(e_listreq));
723 return;
724 }
725 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
726 {
727 emsg(_(e_dictreq));
728 return;
729 }
730 d = argvars[1].vval.v_dict;
731
732 // Create the window and buffer.
733 wp = win_alloc_popup_win();
734 if (wp == NULL)
735 return;
736 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200737 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200738
739 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
740 if (buf == NULL)
741 return;
742 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200743
744 win_init_popup_win(wp, buf);
745
746 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200747 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200748 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200749 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200750 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200751 buf->b_p_ul = -1; // no undo
752 buf->b_p_swf = FALSE; // no swap file
753 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200754 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200755 wp->w_p_wrap = TRUE; // 'wrap' is default on
756
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200757 // Avoid that 'buftype' is reset when this buffer is entered.
758 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200759
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200760 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
761 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200762 else if (type == TYPE_NOTIFICATION)
763 nr = -1; // notifications are global by default
764 else
765 nr = 0;
766
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200767 if (nr == 0)
768 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200769 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200770 wp->w_next = curtab->tp_first_popupwin;
771 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772 }
773 else if (nr < 0)
774 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200775 win_T *prev = first_popupwin;
776
777 // Global popup: add at the end, so that it gets displayed on top of
778 // older ones with the same zindex. Matters for notifications.
779 if (first_popupwin == NULL)
780 first_popupwin = wp;
781 else
782 {
783 while (prev->w_next != NULL)
784 prev = prev->w_next;
785 prev->w_next = wp;
786 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200787 }
788 else
789 // TODO: find tab page "nr"
790 emsg("Not implemented yet");
791
792 // Add text to the buffer.
793 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200794 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200795 // just a string
796 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200797 }
798 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200799 {
800 list_T *l = argvars[0].vval.v_list;
801
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200802 if (l->lv_len > 0)
803 {
804 if (l->lv_first->li_tv.v_type == VAR_STRING)
805 // list of strings
806 add_popup_strings(buf, l);
807 else
808 // list of dictionaries
809 add_popup_dicts(buf, l);
810 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200811 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200812
813 // Delete the line of the empty buffer.
814 curbuf = buf;
815 ml_delete(buf->b_ml.ml_line_count, FALSE);
816 curbuf = curwin->w_buffer;
817
Bram Moolenaar17627312019-06-02 19:53:44 +0200818 if (type == TYPE_ATCURSOR)
819 {
820 wp->w_popup_pos = POPPOS_BOTLEFT;
821 setcursor_mayforce(TRUE);
822 wp->w_wantline = screen_screenrow();
823 if (wp->w_wantline == 0) // cursor in first line
824 {
825 wp->w_wantline = 2;
826 wp->w_popup_pos = POPPOS_TOPLEFT;
827 }
828 wp->w_wantcol = screen_screencol() + 1;
829 set_moved_values(wp);
830 set_moved_columns(wp, FIND_STRING);
831 }
832
Bram Moolenaar33796b32019-06-08 16:01:13 +0200833 // set default values
834 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
835
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200836 if (type == TYPE_NOTIFICATION)
837 {
838 win_T *twp, *nextwin;
839 int height = buf->b_ml.ml_line_count + 3;
840 int i;
841
842 // Try to not overlap with another global popup. Guess we need 3
843 // more screen lines than buffer lines.
844 wp->w_wantline = 1;
845 for (twp = first_popupwin; twp != NULL; twp = nextwin)
846 {
847 nextwin = twp->w_next;
848 if (twp != wp
849 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
850 && twp->w_winrow <= wp->w_wantline - 1 + height
851 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
852 {
853 // move to below this popup and restart the loop to check for
854 // overlap with other popups
855 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
856 nextwin = first_popupwin;
857 }
858 }
859 if (wp->w_wantline + height > Rows)
860 {
861 // can't avoid overlap, put on top in the hope that message goes
862 // away soon.
863 wp->w_wantline = 1;
864 }
865
866 wp->w_wantcol = 10;
867 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200868 wp->w_minwidth = 20;
869 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200870 for (i = 0; i < 4; ++i)
871 wp->w_popup_border[i] = 1;
872 wp->w_popup_padding[1] = 1;
873 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200874
875 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200876 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200877 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
878 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200879 }
880
Bram Moolenaara42d9452019-06-15 21:46:30 +0200881 if (type == TYPE_DIALOG)
882 {
883 int i;
884
885 wp->w_popup_pos = POPPOS_CENTER;
886 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
887 wp->w_popup_drag = 1;
888 for (i = 0; i < 4; ++i)
889 {
890 wp->w_popup_border[i] = 1;
891 wp->w_popup_padding[i] = 1;
892 }
893 }
894
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200895 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200896 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200897
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200898 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
899 popup_add_timeout(wp, 3000);
900
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200901 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200902
903 wp->w_vsep_width = 0;
904
905 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200906 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200907}
908
909/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200910 * popup_clear()
911 */
912 void
913f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
914{
915 close_all_popups();
916}
917
918/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200919 * popup_create({text}, {options})
920 */
921 void
922f_popup_create(typval_T *argvars, typval_T *rettv)
923{
Bram Moolenaar17627312019-06-02 19:53:44 +0200924 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200925}
926
927/*
928 * popup_atcursor({text}, {options})
929 */
930 void
931f_popup_atcursor(typval_T *argvars, typval_T *rettv)
932{
Bram Moolenaar17627312019-06-02 19:53:44 +0200933 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200934}
935
936/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200937 * Invoke the close callback for window "wp" with value "result".
938 * Careful: The callback may make "wp" invalid!
939 */
940 static void
941invoke_popup_callback(win_T *wp, typval_T *result)
942{
943 typval_T rettv;
944 int dummy;
945 typval_T argv[3];
946
947 argv[0].v_type = VAR_NUMBER;
948 argv[0].vval.v_number = (varnumber_T)wp->w_id;
949
950 if (result != NULL && result->v_type != VAR_UNKNOWN)
951 copy_tv(result, &argv[1]);
952 else
953 {
954 argv[1].v_type = VAR_NUMBER;
955 argv[1].vval.v_number = 0;
956 }
957
958 argv[2].v_type = VAR_UNKNOWN;
959
960 call_callback(&wp->w_close_cb, -1,
961 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
962 if (result != NULL)
963 clear_tv(&argv[1]);
964 clear_tv(&rettv);
965}
966
967/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200968 * Close popup "wp" and invoke any close callback for it.
969 */
970 static void
971popup_close_and_callback(win_T *wp, typval_T *arg)
972{
973 int id = wp->w_id;
974
975 if (wp->w_close_cb.cb_name != NULL)
976 // Careful: This may make "wp" invalid.
977 invoke_popup_callback(wp, arg);
978
979 popup_close(id);
980}
981
982/*
Bram Moolenaara42d9452019-06-15 21:46:30 +0200983 * popup_filter_yesno({text}, {options})
984 */
985 void
986f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
987{
988 int id = tv_get_number(&argvars[0]);
989 win_T *wp = win_id2wp(id);
990 char_u *key = tv_get_string(&argvars[1]);
991 typval_T res;
992
993 // If the popup has been closed don't consume the key.
994 if (wp == NULL)
995 return;
996
997 // consume all keys until done
998 rettv->vval.v_number = 1;
999
1000 if (STRCMP(key, "y") == 0 || STRCMP(key, "Y") == 0)
1001 res.vval.v_number = 1;
1002 else if (STRCMP(key, "n") == 0 || STRCMP(key, "N") == 0
1003 || STRCMP(key, "x") == 0 || STRCMP(key, "X") == 0
1004 || STRCMP(key, "\x1b") == 0)
1005 res.vval.v_number = 0;
1006 else
1007 {
1008 int c = *key;
1009 int row = mouse_row;
1010 int col = mouse_col;
1011
1012 if (c == K_SPECIAL && key[1] != NUL)
1013 c = TO_SPECIAL(key[1], key[2]);
1014 if (wp->w_popup_drag
1015 && is_mouse_key(c)
1016 && (wp == popup_dragwin
1017 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1018 // allow for dragging the popup
1019 rettv->vval.v_number = 0;
1020
1021 // ignore this key
1022 return;
1023 }
1024
1025 // Invoke callback
1026 res.v_type = VAR_NUMBER;
1027 popup_close_and_callback(wp, &res);
1028}
1029
1030/*
1031 * popup_dialog({text}, {options})
1032 */
1033 void
1034f_popup_dialog(typval_T *argvars, typval_T *rettv)
1035{
1036 popup_create(argvars, rettv, TYPE_DIALOG);
1037}
1038
1039/*
1040 * popup_notification({text}, {options})
1041 */
1042 void
1043f_popup_notification(typval_T *argvars, typval_T *rettv)
1044{
1045 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1046}
1047
1048/*
1049 * Find the popup window with window-ID "id".
1050 * If the popup window does not exist NULL is returned.
1051 * If the window is not a popup window, and error message is given.
1052 */
1053 static win_T *
1054find_popup_win(int id)
1055{
1056 win_T *wp = win_id2wp(id);
1057
1058 if (wp != NULL && !bt_popup(wp->w_buffer))
1059 {
1060 semsg(_("E993: window %d is not a popup window"), id);
1061 return NULL;
1062 }
1063 return wp;
1064}
1065
1066/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001067 * popup_close({id})
1068 */
1069 void
1070f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1071{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001072 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001073 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001074
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001075 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001076 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001077}
1078
1079/*
1080 * popup_hide({id})
1081 */
1082 void
1083f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1084{
1085 int id = (int)tv_get_number(argvars);
1086 win_T *wp = find_popup_win(id);
1087
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001088 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001089 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001090 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001091 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001092 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001093 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001094 }
1095}
1096
1097/*
1098 * popup_show({id})
1099 */
1100 void
1101f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1102{
1103 int id = (int)tv_get_number(argvars);
1104 win_T *wp = find_popup_win(id);
1105
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001106 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001107 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001108 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001109 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001110 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001111 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001112 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001113}
1114
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001115 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001116popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001117{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001118 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001119 if (wp->w_winrow + wp->w_height >= cmdline_row)
1120 clear_cmdline = TRUE;
1121 win_free_popup(wp);
1122 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001123 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001124}
1125
Bram Moolenaarec583842019-05-26 14:11:23 +02001126/*
1127 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001128 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001129 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001130 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001131popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001132{
1133 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001134 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001135 win_T *prev = NULL;
1136
Bram Moolenaarec583842019-05-26 14:11:23 +02001137 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001138 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001139 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001140 {
1141 if (prev == NULL)
1142 first_popupwin = wp->w_next;
1143 else
1144 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001145 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001146 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001147 }
1148
Bram Moolenaarec583842019-05-26 14:11:23 +02001149 // go through tab-local popups
1150 FOR_ALL_TABPAGES(tp)
1151 popup_close_tabpage(tp, id);
1152}
1153
1154/*
1155 * Close a popup window with Window-id "id" in tabpage "tp".
1156 */
1157 void
1158popup_close_tabpage(tabpage_T *tp, int id)
1159{
1160 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001161 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001162 win_T *prev = NULL;
1163
Bram Moolenaarec583842019-05-26 14:11:23 +02001164 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1165 if (wp->w_id == id)
1166 {
1167 if (prev == NULL)
1168 *root = wp->w_next;
1169 else
1170 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001171 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001172 return;
1173 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001174}
1175
1176 void
1177close_all_popups(void)
1178{
1179 while (first_popupwin != NULL)
1180 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001181 while (curtab->tp_first_popupwin != NULL)
1182 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001183}
1184
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001185/*
1186 * popup_move({id}, {options})
1187 */
1188 void
1189f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1190{
1191 dict_T *d;
1192 int nr;
1193 int id = (int)tv_get_number(argvars);
1194 win_T *wp = find_popup_win(id);
1195
1196 if (wp == NULL)
1197 return; // invalid {id}
1198
1199 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1200 {
1201 emsg(_(e_dictreq));
1202 return;
1203 }
1204 d = argvars[1].vval.v_dict;
1205
1206 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1207 wp->w_minwidth = nr;
1208 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1209 wp->w_minheight = nr;
1210 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1211 wp->w_maxwidth = nr;
1212 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1213 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001214 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001215
1216 if (wp->w_winrow + wp->w_height >= cmdline_row)
1217 clear_cmdline = TRUE;
1218 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001219}
1220
Bram Moolenaarbc133542019-05-29 20:26:46 +02001221/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001222 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001223 */
1224 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001225f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001226{
1227 dict_T *dict;
1228 int id = (int)tv_get_number(argvars);
1229 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001230 int top_extra;
1231 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001232
1233 if (rettv_dict_alloc(rettv) == OK)
1234 {
1235 if (wp == NULL)
1236 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001237 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1238 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1239
Bram Moolenaarbc133542019-05-29 20:26:46 +02001240 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001241
Bram Moolenaarbc133542019-05-29 20:26:46 +02001242 dict_add_number(dict, "line", wp->w_winrow + 1);
1243 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001244 dict_add_number(dict, "width", wp->w_width + left_extra
1245 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1246 dict_add_number(dict, "height", wp->w_height + top_extra
1247 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001248
1249 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1250 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1251 dict_add_number(dict, "core_width", wp->w_width);
1252 dict_add_number(dict, "core_height", wp->w_height);
1253
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001254 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001255 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001256 }
1257}
1258
1259/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001260 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001261 */
1262 void
1263f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1264{
1265 dict_T *dict;
1266 int id = (int)tv_get_number(argvars);
1267 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001268 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001269
1270 if (rettv_dict_alloc(rettv) == OK)
1271 {
1272 if (wp == NULL)
1273 return;
1274
1275 dict = rettv->vval.v_dict;
1276 dict_add_number(dict, "line", wp->w_wantline);
1277 dict_add_number(dict, "col", wp->w_wantcol);
1278 dict_add_number(dict, "minwidth", wp->w_minwidth);
1279 dict_add_number(dict, "minheight", wp->w_minheight);
1280 dict_add_number(dict, "maxheight", wp->w_maxheight);
1281 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001282 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001283 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001284 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001285
1286 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1287 ++i)
1288 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1289 {
1290 dict_add_string(dict, "pos",
1291 (char_u *)poppos_entries[i].pp_name);
1292 break;
1293 }
1294
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001295# if defined(FEAT_TIMERS)
1296 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1297 ? (long)wp->w_popup_timer->tr_interval : 0L);
1298# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001299 }
1300}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001301
1302 int
1303not_in_popup_window()
1304{
1305 if (bt_popup(curwin->w_buffer))
1306 {
1307 emsg(_("E994: Not allowed in a popup window"));
1308 return TRUE;
1309 }
1310 return FALSE;
1311}
1312
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001313/*
1314 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001315 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001316 */
1317 void
1318popup_reset_handled()
1319{
1320 win_T *wp;
1321
1322 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1323 wp->w_popup_flags &= ~POPF_HANDLED;
1324 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1325 wp->w_popup_flags &= ~POPF_HANDLED;
1326}
1327
1328/*
1329 * Find the next visible popup where POPF_HANDLED is not set.
1330 * Must have called popup_reset_handled() first.
1331 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1332 * popup with the highest zindex.
1333 */
1334 win_T *
1335find_next_popup(int lowest)
1336{
1337 win_T *wp;
1338 win_T *found_wp;
1339 int found_zindex;
1340
1341 found_zindex = lowest ? INT_MAX : 0;
1342 found_wp = NULL;
1343 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1344 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1345 && (lowest ? wp->w_zindex < found_zindex
1346 : wp->w_zindex > found_zindex))
1347 {
1348 found_zindex = wp->w_zindex;
1349 found_wp = wp;
1350 }
1351 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1352 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1353 && (lowest ? wp->w_zindex < found_zindex
1354 : wp->w_zindex > found_zindex))
1355 {
1356 found_zindex = wp->w_zindex;
1357 found_wp = wp;
1358 }
1359
1360 if (found_wp != NULL)
1361 found_wp->w_popup_flags |= POPF_HANDLED;
1362 return found_wp;
1363}
1364
1365/*
1366 * Invoke the filter callback for window "wp" with typed character "c".
1367 * Uses the global "mod_mask" for modifiers.
1368 * Returns the return value of the filter.
1369 * Careful: The filter may make "wp" invalid!
1370 */
1371 static int
1372invoke_popup_filter(win_T *wp, int c)
1373{
1374 int res;
1375 typval_T rettv;
1376 int dummy;
1377 typval_T argv[3];
1378 char_u buf[NUMBUFLEN];
1379
Bram Moolenaara42d9452019-06-15 21:46:30 +02001380 // Emergency exit: CTRL-C closes the popup.
1381 if (c == Ctrl_C)
1382 {
1383 rettv.v_type = VAR_NUMBER;
1384 rettv.vval.v_number = -1;
1385 popup_close_and_callback(wp, &rettv);
1386 return 1;
1387 }
1388
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001389 argv[0].v_type = VAR_NUMBER;
1390 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1391
1392 // Convert the number to a string, so that the function can use:
1393 // if a:c == "\<F2>"
1394 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1395 argv[1].v_type = VAR_STRING;
1396 argv[1].vval.v_string = vim_strsave(buf);
1397
1398 argv[2].v_type = VAR_UNKNOWN;
1399
Bram Moolenaara42d9452019-06-15 21:46:30 +02001400 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001401 call_callback(&wp->w_filter_cb, -1,
1402 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1403 res = tv_get_number(&rettv);
1404 vim_free(argv[1].vval.v_string);
1405 clear_tv(&rettv);
1406 return res;
1407}
1408
1409/*
1410 * Called when "c" was typed: invoke popup filter callbacks.
1411 * Returns TRUE when the character was consumed,
1412 */
1413 int
1414popup_do_filter(int c)
1415{
1416 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001417 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001418
1419 popup_reset_handled();
1420
1421 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1422 if (wp->w_filter_cb.cb_name != NULL)
1423 res = invoke_popup_filter(wp, c);
1424
1425 return res;
1426}
1427
Bram Moolenaar3397f742019-06-02 18:40:06 +02001428/*
1429 * Called when the cursor moved: check if any popup needs to be closed if the
1430 * cursor moved far enough.
1431 */
1432 void
1433popup_check_cursor_pos()
1434{
1435 win_T *wp;
1436 typval_T tv;
1437
1438 popup_reset_handled();
1439 while ((wp = find_next_popup(TRUE)) != NULL)
1440 if (wp->w_popup_curwin != NULL
1441 && (curwin != wp->w_popup_curwin
1442 || curwin->w_cursor.lnum != wp->w_popup_lnum
1443 || curwin->w_cursor.col < wp->w_popup_mincol
1444 || curwin->w_cursor.col > wp->w_popup_maxcol))
1445 {
1446 tv.v_type = VAR_NUMBER;
1447 tv.vval.v_number = -1;
1448 popup_close_and_callback(wp, &tv);
1449 }
1450}
1451
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001452/*
1453 * Update "popup_mask" if needed.
1454 * Also recomputes the popup size and positions.
1455 * Also updates "popup_visible".
1456 * Also marks window lines for redrawing.
1457 */
1458 void
1459may_update_popup_mask(int type)
1460{
1461 win_T *wp;
1462 short *mask;
1463 int line, col;
1464 int redraw_all = FALSE;
1465
1466 // Need to recompute when switching tabs.
1467 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1468 // (such as the screen size) must have changed.
1469 if (popup_mask_tab != curtab || type >= NOT_VALID)
1470 {
1471 popup_mask_refresh = TRUE;
1472 redraw_all = TRUE;
1473 }
1474 if (!popup_mask_refresh)
1475 {
1476 // Check if any buffer has changed.
1477 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1478 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1479 popup_mask_refresh = TRUE;
1480 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1481 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1482 popup_mask_refresh = TRUE;
1483 if (!popup_mask_refresh)
1484 return;
1485 }
1486
1487 // Need to update the mask, something has changed.
1488 popup_mask_refresh = FALSE;
1489 popup_mask_tab = curtab;
1490 popup_visible = FALSE;
1491
1492 // If redrawing everything, just update "popup_mask".
1493 // If redrawing only what is needed, update "popup_mask_next" and then
1494 // compare with "popup_mask" to see what changed.
1495 if (type >= SOME_VALID)
1496 mask = popup_mask;
1497 else
1498 mask = popup_mask_next;
1499 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1500
1501 // Find the window with the lowest zindex that hasn't been handled yet,
1502 // so that the window with a higher zindex overwrites the value in
1503 // popup_mask.
1504 popup_reset_handled();
1505 while ((wp = find_next_popup(TRUE)) != NULL)
1506 {
1507 popup_visible = TRUE;
1508
1509 // Recompute the position if the text changed.
1510 if (redraw_all
1511 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1512 popup_adjust_position(wp);
1513
1514 for (line = wp->w_winrow;
1515 line < wp->w_winrow + popup_height(wp)
1516 && line < screen_Rows; ++line)
1517 for (col = wp->w_wincol;
1518 col < wp->w_wincol + popup_width(wp)
1519 && col < screen_Columns; ++col)
1520 mask[line * screen_Columns + col] = wp->w_zindex;
1521 }
1522
1523 // Only check which lines are to be updated if not already
1524 // updating all lines.
1525 if (mask == popup_mask_next)
1526 for (line = 0; line < screen_Rows; ++line)
1527 {
1528 int col_done = 0;
1529
1530 for (col = 0; col < screen_Columns; ++col)
1531 {
1532 int off = line * screen_Columns + col;
1533
1534 if (popup_mask[off] != popup_mask_next[off])
1535 {
1536 popup_mask[off] = popup_mask_next[off];
1537
1538 if (line >= cmdline_row)
1539 {
1540 // the command line needs to be cleared if text below
1541 // the popup is now visible.
1542 if (!msg_scrolled && popup_mask_next[off] == 0)
1543 clear_cmdline = TRUE;
1544 }
1545 else if (col >= col_done)
1546 {
1547 linenr_T lnum;
1548 int line_cp = line;
1549 int col_cp = col;
1550
1551 // The screen position "line" / "col" needs to be
1552 // redrawn. Figure out what window that is and update
1553 // w_redraw_top and w_redr_bot. Only needs to be done
1554 // once for each window line.
1555 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1556 if (wp != NULL)
1557 {
1558 if (line_cp >= wp->w_height)
1559 // In (or below) status line
1560 wp->w_redr_status = TRUE;
1561 // compute the position in the buffer line from the
1562 // position on the screen
1563 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1564 &lnum))
1565 // past bottom
1566 wp->w_redr_status = TRUE;
1567 else
1568 redrawWinline(wp, lnum);
1569
1570 // This line is going to be redrawn, no need to
1571 // check until the right side of the window.
1572 col_done = wp->w_wincol + wp->w_width - 1;
1573 }
1574 }
1575 }
1576 }
1577 }
1578}
1579
1580/*
1581 * Return a string of "len" spaces in IObuff.
1582 */
1583 static char_u *
1584get_spaces(int len)
1585{
1586 vim_memset(IObuff, ' ', (size_t)len);
1587 IObuff[len] = NUL;
1588 return IObuff;
1589}
1590
1591/*
1592 * Update popup windows. They are drawn on top of normal windows.
1593 * "win_update" is called for each popup window, lowest zindex first.
1594 */
1595 void
1596update_popups(void (*win_update)(win_T *wp))
1597{
1598 win_T *wp;
1599 int top_off;
1600 int left_off;
1601 int total_width;
1602 int total_height;
1603 int popup_attr;
1604 int border_attr[4];
1605 int border_char[8];
1606 char_u buf[MB_MAXBYTES];
1607 int row;
1608 int i;
1609
1610 // Find the window with the lowest zindex that hasn't been updated yet,
1611 // so that the window with a higher zindex is drawn later, thus goes on
1612 // top.
1613 popup_reset_handled();
1614 while ((wp = find_next_popup(TRUE)) != NULL)
1615 {
1616 // This drawing uses the zindex of the popup window, so that it's on
1617 // top of the text but doesn't draw when another popup with higher
1618 // zindex is on top of the character.
1619 screen_zindex = wp->w_zindex;
1620
1621 // adjust w_winrow and w_wincol for border and padding, since
1622 // win_update() doesn't handle them.
1623 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1624 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1625 wp->w_winrow += top_off;
1626 wp->w_wincol += left_off;
1627
1628 // Draw the popup text.
1629 win_update(wp);
1630
1631 wp->w_winrow -= top_off;
1632 wp->w_wincol -= left_off;
1633
1634 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1635 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1636 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1637 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1638 popup_attr = get_wcr_attr(wp);
1639
1640 // We can only use these line drawing characters when 'encoding' is
1641 // "utf-8" and 'ambiwidth' is "single".
1642 if (enc_utf8 && *p_ambw == 's')
1643 {
1644 border_char[0] = border_char[2] = 0x2550;
1645 border_char[1] = border_char[3] = 0x2551;
1646 border_char[4] = 0x2554;
1647 border_char[5] = 0x2557;
1648 border_char[6] = 0x255d;
1649 border_char[7] = 0x255a;
1650 }
1651 else
1652 {
1653 border_char[0] = border_char[2] = '-';
1654 border_char[1] = border_char[3] = '|';
1655 for (i = 4; i < 8; ++i)
1656 border_char[i] = '+';
1657 }
1658 for (i = 0; i < 8; ++i)
1659 if (wp->w_border_char[i] != 0)
1660 border_char[i] = wp->w_border_char[i];
1661
1662 for (i = 0; i < 4; ++i)
1663 {
1664 border_attr[i] = popup_attr;
1665 if (wp->w_border_highlight[i] != NULL)
1666 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1667 }
1668
1669 if (wp->w_popup_border[0] > 0)
1670 {
1671 // top border
1672 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1673 wp->w_wincol,
1674 wp->w_wincol + total_width,
1675 wp->w_popup_border[3] != 0
1676 ? border_char[4] : border_char[0],
1677 border_char[0], border_attr[0]);
1678 if (wp->w_popup_border[1] > 0)
1679 {
1680 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1681 screen_puts(buf, wp->w_winrow,
1682 wp->w_wincol + total_width - 1, border_attr[1]);
1683 }
1684 }
1685
1686 if (wp->w_popup_padding[0] > 0)
1687 {
1688 // top padding
1689 row = wp->w_winrow + wp->w_popup_border[0];
1690 screen_fill(row, row + wp->w_popup_padding[0],
1691 wp->w_wincol + wp->w_popup_border[3],
1692 wp->w_wincol + total_width - wp->w_popup_border[1],
1693 ' ', ' ', popup_attr);
1694 }
1695
1696 for (row = wp->w_winrow + wp->w_popup_border[0];
1697 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1698 ++row)
1699 {
1700 // left border
1701 if (wp->w_popup_border[3] > 0)
1702 {
1703 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1704 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1705 }
1706 // left padding
1707 if (wp->w_popup_padding[3] > 0)
1708 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1709 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1710 // right border
1711 if (wp->w_popup_border[1] > 0)
1712 {
1713 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1714 screen_puts(buf, row,
1715 wp->w_wincol + total_width - 1, border_attr[1]);
1716 }
1717 // right padding
1718 if (wp->w_popup_padding[1] > 0)
1719 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1720 wp->w_wincol + wp->w_popup_border[3]
1721 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1722 }
1723
1724 if (wp->w_popup_padding[2] > 0)
1725 {
1726 // bottom padding
1727 row = wp->w_winrow + wp->w_popup_border[0]
1728 + wp->w_popup_padding[0] + wp->w_height;
1729 screen_fill(row, row + wp->w_popup_padding[2],
1730 wp->w_wincol + wp->w_popup_border[3],
1731 wp->w_wincol + total_width - wp->w_popup_border[1],
1732 ' ', ' ', popup_attr);
1733 }
1734
1735 if (wp->w_popup_border[2] > 0)
1736 {
1737 // bottom border
1738 row = wp->w_winrow + total_height - 1;
1739 screen_fill(row , row + 1,
1740 wp->w_wincol,
1741 wp->w_wincol + total_width,
1742 wp->w_popup_border[3] != 0
1743 ? border_char[7] : border_char[2],
1744 border_char[2], border_attr[2]);
1745 if (wp->w_popup_border[1] > 0)
1746 {
1747 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1748 screen_puts(buf, row,
1749 wp->w_wincol + total_width - 1, border_attr[2]);
1750 }
1751 }
1752
1753 // Back to the normal zindex.
1754 screen_zindex = 0;
1755 }
1756}
1757
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001758#endif // FEAT_TEXT_PROP