blob: adbc72914938b877cd01510a6663fe725893d9d9 [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 Moolenaar88c4e1f2019-05-29 23:14:28 +0200591 maxwidth = Columns - wp->w_wincol;
592 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
625 int shift_by = ( len - maxwidth );
626
627 if ( shift_by > wp->w_wincol )
628 {
629 int truncate_shift = shift_by - wp->w_wincol;
630 len -= truncate_shift;
631 shift_by -= truncate_shift;
632 }
633
634 wp->w_wincol -= shift_by;
635 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200636 wp->w_width = maxwidth;
637 }
638 if (wp->w_width < len)
639 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200640 // do not use the width of lines we're not going to show
641 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
642 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
643 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200644 }
645
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200646 if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth)
647 wp->w_width = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200648 if (wp->w_width > maxwidth)
649 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200650 if (center_hor)
651 wp->w_wincol = (Columns - wp->w_width) / 2;
652 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
653 || wp->w_popup_pos == POPPOS_TOPRIGHT)
654 {
655 // Right aligned: move to the right if needed.
656 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200657 if (wp->w_width + extra_width < wp->w_wantcol)
658 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200659 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200660
Bram Moolenaar8d241042019-06-12 23:40:01 +0200661 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
662 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200663 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
664 wp->w_height = wp->w_minheight;
665 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
666 wp->w_height = wp->w_maxheight;
667 if (wp->w_height > Rows - wp->w_winrow)
668 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200669
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200670 if (center_vert)
671 wp->w_winrow = (Rows - wp->w_height) / 2;
672 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
673 || wp->w_popup_pos == POPPOS_BOTLEFT)
674 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200675 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200676 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200677 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200678 else
679 // not enough space, make top aligned
680 wp->w_winrow = wp->w_wantline + 1;
681 }
682
Bram Moolenaar17146962019-05-30 00:12:11 +0200683 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200684
685 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200686 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200687 if (org_winrow != wp->w_winrow
688 || org_wincol != wp->w_wincol
689 || org_width != wp->w_width
690 || org_height != wp->w_height)
691 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200692 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200693 popup_mask_refresh = TRUE;
694 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200695}
696
Bram Moolenaar17627312019-06-02 19:53:44 +0200697typedef enum
698{
699 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200700 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200701 TYPE_NOTIFICATION,
702 TYPE_DIALOG
Bram Moolenaar17627312019-06-02 19:53:44 +0200703} create_type_T;
704
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200705/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200706 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200707 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200708 */
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200709 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200710popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200711{
712 win_T *wp;
713 buf_T *buf;
714 dict_T *d;
715 int nr;
716
717 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200718 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
719 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200720 {
721 emsg(_(e_listreq));
722 return;
723 }
724 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
725 {
726 emsg(_(e_dictreq));
727 return;
728 }
729 d = argvars[1].vval.v_dict;
730
731 // Create the window and buffer.
732 wp = win_alloc_popup_win();
733 if (wp == NULL)
734 return;
735 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200736 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200737
738 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
739 if (buf == NULL)
740 return;
741 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200742
743 win_init_popup_win(wp, buf);
744
745 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200746 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200747 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200748 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200749 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200750 buf->b_p_ul = -1; // no undo
751 buf->b_p_swf = FALSE; // no swap file
752 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200753 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200754 wp->w_p_wrap = TRUE; // 'wrap' is default on
755
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200756 // Avoid that 'buftype' is reset when this buffer is entered.
757 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200758
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200759 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
760 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200761 else if (type == TYPE_NOTIFICATION)
762 nr = -1; // notifications are global by default
763 else
764 nr = 0;
765
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200766 if (nr == 0)
767 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200768 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200769 wp->w_next = curtab->tp_first_popupwin;
770 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200771 }
772 else if (nr < 0)
773 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200774 win_T *prev = first_popupwin;
775
776 // Global popup: add at the end, so that it gets displayed on top of
777 // older ones with the same zindex. Matters for notifications.
778 if (first_popupwin == NULL)
779 first_popupwin = wp;
780 else
781 {
782 while (prev->w_next != NULL)
783 prev = prev->w_next;
784 prev->w_next = wp;
785 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200786 }
787 else
788 // TODO: find tab page "nr"
789 emsg("Not implemented yet");
790
791 // Add text to the buffer.
792 if (argvars[0].v_type == VAR_STRING)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200793 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200794 // just a string
795 ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200796 }
797 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200798 {
799 list_T *l = argvars[0].vval.v_list;
800
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200801 if (l->lv_len > 0)
802 {
803 if (l->lv_first->li_tv.v_type == VAR_STRING)
804 // list of strings
805 add_popup_strings(buf, l);
806 else
807 // list of dictionaries
808 add_popup_dicts(buf, l);
809 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200810 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200811
812 // Delete the line of the empty buffer.
813 curbuf = buf;
814 ml_delete(buf->b_ml.ml_line_count, FALSE);
815 curbuf = curwin->w_buffer;
816
Bram Moolenaar17627312019-06-02 19:53:44 +0200817 if (type == TYPE_ATCURSOR)
818 {
819 wp->w_popup_pos = POPPOS_BOTLEFT;
820 setcursor_mayforce(TRUE);
821 wp->w_wantline = screen_screenrow();
822 if (wp->w_wantline == 0) // cursor in first line
823 {
824 wp->w_wantline = 2;
825 wp->w_popup_pos = POPPOS_TOPLEFT;
826 }
827 wp->w_wantcol = screen_screencol() + 1;
828 set_moved_values(wp);
829 set_moved_columns(wp, FIND_STRING);
830 }
831
Bram Moolenaar33796b32019-06-08 16:01:13 +0200832 // set default values
833 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
834
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200835 if (type == TYPE_NOTIFICATION)
836 {
837 win_T *twp, *nextwin;
838 int height = buf->b_ml.ml_line_count + 3;
839 int i;
840
841 // Try to not overlap with another global popup. Guess we need 3
842 // more screen lines than buffer lines.
843 wp->w_wantline = 1;
844 for (twp = first_popupwin; twp != NULL; twp = nextwin)
845 {
846 nextwin = twp->w_next;
847 if (twp != wp
848 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
849 && twp->w_winrow <= wp->w_wantline - 1 + height
850 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
851 {
852 // move to below this popup and restart the loop to check for
853 // overlap with other popups
854 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
855 nextwin = first_popupwin;
856 }
857 }
858 if (wp->w_wantline + height > Rows)
859 {
860 // can't avoid overlap, put on top in the hope that message goes
861 // away soon.
862 wp->w_wantline = 1;
863 }
864
865 wp->w_wantcol = 10;
866 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200867 wp->w_minwidth = 20;
868 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200869 for (i = 0; i < 4; ++i)
870 wp->w_popup_border[i] = 1;
871 wp->w_popup_padding[1] = 1;
872 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200873
874 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200875 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200876 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
877 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200878 }
879
Bram Moolenaara42d9452019-06-15 21:46:30 +0200880 if (type == TYPE_DIALOG)
881 {
882 int i;
883
884 wp->w_popup_pos = POPPOS_CENTER;
885 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
886 wp->w_popup_drag = 1;
887 for (i = 0; i < 4; ++i)
888 {
889 wp->w_popup_border[i] = 1;
890 wp->w_popup_padding[i] = 1;
891 }
892 }
893
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200894 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200895 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200896
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200897 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
898 popup_add_timeout(wp, 3000);
899
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200900 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200901
902 wp->w_vsep_width = 0;
903
904 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200905 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200906}
907
908/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200909 * popup_clear()
910 */
911 void
912f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
913{
914 close_all_popups();
915}
916
917/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200918 * popup_create({text}, {options})
919 */
920 void
921f_popup_create(typval_T *argvars, typval_T *rettv)
922{
Bram Moolenaar17627312019-06-02 19:53:44 +0200923 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200924}
925
926/*
927 * popup_atcursor({text}, {options})
928 */
929 void
930f_popup_atcursor(typval_T *argvars, typval_T *rettv)
931{
Bram Moolenaar17627312019-06-02 19:53:44 +0200932 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200933}
934
935/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200936 * Invoke the close callback for window "wp" with value "result".
937 * Careful: The callback may make "wp" invalid!
938 */
939 static void
940invoke_popup_callback(win_T *wp, typval_T *result)
941{
942 typval_T rettv;
943 int dummy;
944 typval_T argv[3];
945
946 argv[0].v_type = VAR_NUMBER;
947 argv[0].vval.v_number = (varnumber_T)wp->w_id;
948
949 if (result != NULL && result->v_type != VAR_UNKNOWN)
950 copy_tv(result, &argv[1]);
951 else
952 {
953 argv[1].v_type = VAR_NUMBER;
954 argv[1].vval.v_number = 0;
955 }
956
957 argv[2].v_type = VAR_UNKNOWN;
958
959 call_callback(&wp->w_close_cb, -1,
960 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
961 if (result != NULL)
962 clear_tv(&argv[1]);
963 clear_tv(&rettv);
964}
965
966/*
Bram Moolenaar3397f742019-06-02 18:40:06 +0200967 * Close popup "wp" and invoke any close callback for it.
968 */
969 static void
970popup_close_and_callback(win_T *wp, typval_T *arg)
971{
972 int id = wp->w_id;
973
974 if (wp->w_close_cb.cb_name != NULL)
975 // Careful: This may make "wp" invalid.
976 invoke_popup_callback(wp, arg);
977
978 popup_close(id);
979}
980
981/*
Bram Moolenaara42d9452019-06-15 21:46:30 +0200982 * popup_filter_yesno({text}, {options})
983 */
984 void
985f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
986{
987 int id = tv_get_number(&argvars[0]);
988 win_T *wp = win_id2wp(id);
989 char_u *key = tv_get_string(&argvars[1]);
990 typval_T res;
991
992 // If the popup has been closed don't consume the key.
993 if (wp == NULL)
994 return;
995
996 // consume all keys until done
997 rettv->vval.v_number = 1;
998
999 if (STRCMP(key, "y") == 0 || STRCMP(key, "Y") == 0)
1000 res.vval.v_number = 1;
1001 else if (STRCMP(key, "n") == 0 || STRCMP(key, "N") == 0
1002 || STRCMP(key, "x") == 0 || STRCMP(key, "X") == 0
1003 || STRCMP(key, "\x1b") == 0)
1004 res.vval.v_number = 0;
1005 else
1006 {
1007 int c = *key;
1008 int row = mouse_row;
1009 int col = mouse_col;
1010
1011 if (c == K_SPECIAL && key[1] != NUL)
1012 c = TO_SPECIAL(key[1], key[2]);
1013 if (wp->w_popup_drag
1014 && is_mouse_key(c)
1015 && (wp == popup_dragwin
1016 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1017 // allow for dragging the popup
1018 rettv->vval.v_number = 0;
1019
1020 // ignore this key
1021 return;
1022 }
1023
1024 // Invoke callback
1025 res.v_type = VAR_NUMBER;
1026 popup_close_and_callback(wp, &res);
1027}
1028
1029/*
1030 * popup_dialog({text}, {options})
1031 */
1032 void
1033f_popup_dialog(typval_T *argvars, typval_T *rettv)
1034{
1035 popup_create(argvars, rettv, TYPE_DIALOG);
1036}
1037
1038/*
1039 * popup_notification({text}, {options})
1040 */
1041 void
1042f_popup_notification(typval_T *argvars, typval_T *rettv)
1043{
1044 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1045}
1046
1047/*
1048 * Find the popup window with window-ID "id".
1049 * If the popup window does not exist NULL is returned.
1050 * If the window is not a popup window, and error message is given.
1051 */
1052 static win_T *
1053find_popup_win(int id)
1054{
1055 win_T *wp = win_id2wp(id);
1056
1057 if (wp != NULL && !bt_popup(wp->w_buffer))
1058 {
1059 semsg(_("E993: window %d is not a popup window"), id);
1060 return NULL;
1061 }
1062 return wp;
1063}
1064
1065/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001066 * popup_close({id})
1067 */
1068 void
1069f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1070{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001071 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001072 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001073
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001074 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001075 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001076}
1077
1078/*
1079 * popup_hide({id})
1080 */
1081 void
1082f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1083{
1084 int id = (int)tv_get_number(argvars);
1085 win_T *wp = find_popup_win(id);
1086
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001087 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001088 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001089 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001090 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001091 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001092 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001093 }
1094}
1095
1096/*
1097 * popup_show({id})
1098 */
1099 void
1100f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1101{
1102 int id = (int)tv_get_number(argvars);
1103 win_T *wp = find_popup_win(id);
1104
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001105 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001106 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001107 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001108 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001109 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001110 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001111 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001112}
1113
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001114 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001115popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001116{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001117 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001118 if (wp->w_winrow + wp->w_height >= cmdline_row)
1119 clear_cmdline = TRUE;
1120 win_free_popup(wp);
1121 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001122 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001123}
1124
Bram Moolenaarec583842019-05-26 14:11:23 +02001125/*
1126 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001127 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001128 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001129 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001130popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001131{
1132 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001133 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001134 win_T *prev = NULL;
1135
Bram Moolenaarec583842019-05-26 14:11:23 +02001136 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001137 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001138 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001139 {
1140 if (prev == NULL)
1141 first_popupwin = wp->w_next;
1142 else
1143 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001144 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001145 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001146 }
1147
Bram Moolenaarec583842019-05-26 14:11:23 +02001148 // go through tab-local popups
1149 FOR_ALL_TABPAGES(tp)
1150 popup_close_tabpage(tp, id);
1151}
1152
1153/*
1154 * Close a popup window with Window-id "id" in tabpage "tp".
1155 */
1156 void
1157popup_close_tabpage(tabpage_T *tp, int id)
1158{
1159 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001160 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001161 win_T *prev = NULL;
1162
Bram Moolenaarec583842019-05-26 14:11:23 +02001163 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1164 if (wp->w_id == id)
1165 {
1166 if (prev == NULL)
1167 *root = wp->w_next;
1168 else
1169 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001170 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001171 return;
1172 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001173}
1174
1175 void
1176close_all_popups(void)
1177{
1178 while (first_popupwin != NULL)
1179 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001180 while (curtab->tp_first_popupwin != NULL)
1181 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001182}
1183
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001184/*
1185 * popup_move({id}, {options})
1186 */
1187 void
1188f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1189{
1190 dict_T *d;
1191 int nr;
1192 int id = (int)tv_get_number(argvars);
1193 win_T *wp = find_popup_win(id);
1194
1195 if (wp == NULL)
1196 return; // invalid {id}
1197
1198 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1199 {
1200 emsg(_(e_dictreq));
1201 return;
1202 }
1203 d = argvars[1].vval.v_dict;
1204
1205 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1206 wp->w_minwidth = nr;
1207 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1208 wp->w_minheight = nr;
1209 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1210 wp->w_maxwidth = nr;
1211 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1212 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001213 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001214
1215 if (wp->w_winrow + wp->w_height >= cmdline_row)
1216 clear_cmdline = TRUE;
1217 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001218}
1219
Bram Moolenaarbc133542019-05-29 20:26:46 +02001220/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001221 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001222 */
1223 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001224f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001225{
1226 dict_T *dict;
1227 int id = (int)tv_get_number(argvars);
1228 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001229 int top_extra;
1230 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001231
1232 if (rettv_dict_alloc(rettv) == OK)
1233 {
1234 if (wp == NULL)
1235 return; // invalid {id}
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001236 top_extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1237 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1238
Bram Moolenaarbc133542019-05-29 20:26:46 +02001239 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001240
Bram Moolenaarbc133542019-05-29 20:26:46 +02001241 dict_add_number(dict, "line", wp->w_winrow + 1);
1242 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001243 dict_add_number(dict, "width", wp->w_width + left_extra
1244 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1245 dict_add_number(dict, "height", wp->w_height + top_extra
1246 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001247
1248 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1249 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1250 dict_add_number(dict, "core_width", wp->w_width);
1251 dict_add_number(dict, "core_height", wp->w_height);
1252
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001253 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001254 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001255 }
1256}
1257
1258/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001259 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001260 */
1261 void
1262f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1263{
1264 dict_T *dict;
1265 int id = (int)tv_get_number(argvars);
1266 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001267 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001268
1269 if (rettv_dict_alloc(rettv) == OK)
1270 {
1271 if (wp == NULL)
1272 return;
1273
1274 dict = rettv->vval.v_dict;
1275 dict_add_number(dict, "line", wp->w_wantline);
1276 dict_add_number(dict, "col", wp->w_wantcol);
1277 dict_add_number(dict, "minwidth", wp->w_minwidth);
1278 dict_add_number(dict, "minheight", wp->w_minheight);
1279 dict_add_number(dict, "maxheight", wp->w_maxheight);
1280 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001281 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001282 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001283 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001284
1285 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1286 ++i)
1287 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1288 {
1289 dict_add_string(dict, "pos",
1290 (char_u *)poppos_entries[i].pp_name);
1291 break;
1292 }
1293
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001294# if defined(FEAT_TIMERS)
1295 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1296 ? (long)wp->w_popup_timer->tr_interval : 0L);
1297# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001298 }
1299}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001300
1301 int
1302not_in_popup_window()
1303{
1304 if (bt_popup(curwin->w_buffer))
1305 {
1306 emsg(_("E994: Not allowed in a popup window"));
1307 return TRUE;
1308 }
1309 return FALSE;
1310}
1311
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001312/*
1313 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001314 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001315 */
1316 void
1317popup_reset_handled()
1318{
1319 win_T *wp;
1320
1321 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1322 wp->w_popup_flags &= ~POPF_HANDLED;
1323 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1324 wp->w_popup_flags &= ~POPF_HANDLED;
1325}
1326
1327/*
1328 * Find the next visible popup where POPF_HANDLED is not set.
1329 * Must have called popup_reset_handled() first.
1330 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1331 * popup with the highest zindex.
1332 */
1333 win_T *
1334find_next_popup(int lowest)
1335{
1336 win_T *wp;
1337 win_T *found_wp;
1338 int found_zindex;
1339
1340 found_zindex = lowest ? INT_MAX : 0;
1341 found_wp = NULL;
1342 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1343 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1344 && (lowest ? wp->w_zindex < found_zindex
1345 : wp->w_zindex > found_zindex))
1346 {
1347 found_zindex = wp->w_zindex;
1348 found_wp = wp;
1349 }
1350 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1351 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1352 && (lowest ? wp->w_zindex < found_zindex
1353 : wp->w_zindex > found_zindex))
1354 {
1355 found_zindex = wp->w_zindex;
1356 found_wp = wp;
1357 }
1358
1359 if (found_wp != NULL)
1360 found_wp->w_popup_flags |= POPF_HANDLED;
1361 return found_wp;
1362}
1363
1364/*
1365 * Invoke the filter callback for window "wp" with typed character "c".
1366 * Uses the global "mod_mask" for modifiers.
1367 * Returns the return value of the filter.
1368 * Careful: The filter may make "wp" invalid!
1369 */
1370 static int
1371invoke_popup_filter(win_T *wp, int c)
1372{
1373 int res;
1374 typval_T rettv;
1375 int dummy;
1376 typval_T argv[3];
1377 char_u buf[NUMBUFLEN];
1378
Bram Moolenaara42d9452019-06-15 21:46:30 +02001379 // Emergency exit: CTRL-C closes the popup.
1380 if (c == Ctrl_C)
1381 {
1382 rettv.v_type = VAR_NUMBER;
1383 rettv.vval.v_number = -1;
1384 popup_close_and_callback(wp, &rettv);
1385 return 1;
1386 }
1387
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001388 argv[0].v_type = VAR_NUMBER;
1389 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1390
1391 // Convert the number to a string, so that the function can use:
1392 // if a:c == "\<F2>"
1393 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1394 argv[1].v_type = VAR_STRING;
1395 argv[1].vval.v_string = vim_strsave(buf);
1396
1397 argv[2].v_type = VAR_UNKNOWN;
1398
Bram Moolenaara42d9452019-06-15 21:46:30 +02001399 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001400 call_callback(&wp->w_filter_cb, -1,
1401 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1402 res = tv_get_number(&rettv);
1403 vim_free(argv[1].vval.v_string);
1404 clear_tv(&rettv);
1405 return res;
1406}
1407
1408/*
1409 * Called when "c" was typed: invoke popup filter callbacks.
1410 * Returns TRUE when the character was consumed,
1411 */
1412 int
1413popup_do_filter(int c)
1414{
1415 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001416 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001417
1418 popup_reset_handled();
1419
1420 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1421 if (wp->w_filter_cb.cb_name != NULL)
1422 res = invoke_popup_filter(wp, c);
1423
1424 return res;
1425}
1426
Bram Moolenaar3397f742019-06-02 18:40:06 +02001427/*
1428 * Called when the cursor moved: check if any popup needs to be closed if the
1429 * cursor moved far enough.
1430 */
1431 void
1432popup_check_cursor_pos()
1433{
1434 win_T *wp;
1435 typval_T tv;
1436
1437 popup_reset_handled();
1438 while ((wp = find_next_popup(TRUE)) != NULL)
1439 if (wp->w_popup_curwin != NULL
1440 && (curwin != wp->w_popup_curwin
1441 || curwin->w_cursor.lnum != wp->w_popup_lnum
1442 || curwin->w_cursor.col < wp->w_popup_mincol
1443 || curwin->w_cursor.col > wp->w_popup_maxcol))
1444 {
1445 tv.v_type = VAR_NUMBER;
1446 tv.vval.v_number = -1;
1447 popup_close_and_callback(wp, &tv);
1448 }
1449}
1450
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001451/*
1452 * Update "popup_mask" if needed.
1453 * Also recomputes the popup size and positions.
1454 * Also updates "popup_visible".
1455 * Also marks window lines for redrawing.
1456 */
1457 void
1458may_update_popup_mask(int type)
1459{
1460 win_T *wp;
1461 short *mask;
1462 int line, col;
1463 int redraw_all = FALSE;
1464
1465 // Need to recompute when switching tabs.
1466 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1467 // (such as the screen size) must have changed.
1468 if (popup_mask_tab != curtab || type >= NOT_VALID)
1469 {
1470 popup_mask_refresh = TRUE;
1471 redraw_all = TRUE;
1472 }
1473 if (!popup_mask_refresh)
1474 {
1475 // Check if any buffer has changed.
1476 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1477 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1478 popup_mask_refresh = TRUE;
1479 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1480 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1481 popup_mask_refresh = TRUE;
1482 if (!popup_mask_refresh)
1483 return;
1484 }
1485
1486 // Need to update the mask, something has changed.
1487 popup_mask_refresh = FALSE;
1488 popup_mask_tab = curtab;
1489 popup_visible = FALSE;
1490
1491 // If redrawing everything, just update "popup_mask".
1492 // If redrawing only what is needed, update "popup_mask_next" and then
1493 // compare with "popup_mask" to see what changed.
1494 if (type >= SOME_VALID)
1495 mask = popup_mask;
1496 else
1497 mask = popup_mask_next;
1498 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1499
1500 // Find the window with the lowest zindex that hasn't been handled yet,
1501 // so that the window with a higher zindex overwrites the value in
1502 // popup_mask.
1503 popup_reset_handled();
1504 while ((wp = find_next_popup(TRUE)) != NULL)
1505 {
1506 popup_visible = TRUE;
1507
1508 // Recompute the position if the text changed.
1509 if (redraw_all
1510 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1511 popup_adjust_position(wp);
1512
1513 for (line = wp->w_winrow;
1514 line < wp->w_winrow + popup_height(wp)
1515 && line < screen_Rows; ++line)
1516 for (col = wp->w_wincol;
1517 col < wp->w_wincol + popup_width(wp)
1518 && col < screen_Columns; ++col)
1519 mask[line * screen_Columns + col] = wp->w_zindex;
1520 }
1521
1522 // Only check which lines are to be updated if not already
1523 // updating all lines.
1524 if (mask == popup_mask_next)
1525 for (line = 0; line < screen_Rows; ++line)
1526 {
1527 int col_done = 0;
1528
1529 for (col = 0; col < screen_Columns; ++col)
1530 {
1531 int off = line * screen_Columns + col;
1532
1533 if (popup_mask[off] != popup_mask_next[off])
1534 {
1535 popup_mask[off] = popup_mask_next[off];
1536
1537 if (line >= cmdline_row)
1538 {
1539 // the command line needs to be cleared if text below
1540 // the popup is now visible.
1541 if (!msg_scrolled && popup_mask_next[off] == 0)
1542 clear_cmdline = TRUE;
1543 }
1544 else if (col >= col_done)
1545 {
1546 linenr_T lnum;
1547 int line_cp = line;
1548 int col_cp = col;
1549
1550 // The screen position "line" / "col" needs to be
1551 // redrawn. Figure out what window that is and update
1552 // w_redraw_top and w_redr_bot. Only needs to be done
1553 // once for each window line.
1554 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1555 if (wp != NULL)
1556 {
1557 if (line_cp >= wp->w_height)
1558 // In (or below) status line
1559 wp->w_redr_status = TRUE;
1560 // compute the position in the buffer line from the
1561 // position on the screen
1562 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1563 &lnum))
1564 // past bottom
1565 wp->w_redr_status = TRUE;
1566 else
1567 redrawWinline(wp, lnum);
1568
1569 // This line is going to be redrawn, no need to
1570 // check until the right side of the window.
1571 col_done = wp->w_wincol + wp->w_width - 1;
1572 }
1573 }
1574 }
1575 }
1576 }
1577}
1578
1579/*
1580 * Return a string of "len" spaces in IObuff.
1581 */
1582 static char_u *
1583get_spaces(int len)
1584{
1585 vim_memset(IObuff, ' ', (size_t)len);
1586 IObuff[len] = NUL;
1587 return IObuff;
1588}
1589
1590/*
1591 * Update popup windows. They are drawn on top of normal windows.
1592 * "win_update" is called for each popup window, lowest zindex first.
1593 */
1594 void
1595update_popups(void (*win_update)(win_T *wp))
1596{
1597 win_T *wp;
1598 int top_off;
1599 int left_off;
1600 int total_width;
1601 int total_height;
1602 int popup_attr;
1603 int border_attr[4];
1604 int border_char[8];
1605 char_u buf[MB_MAXBYTES];
1606 int row;
1607 int i;
1608
1609 // Find the window with the lowest zindex that hasn't been updated yet,
1610 // so that the window with a higher zindex is drawn later, thus goes on
1611 // top.
1612 popup_reset_handled();
1613 while ((wp = find_next_popup(TRUE)) != NULL)
1614 {
1615 // This drawing uses the zindex of the popup window, so that it's on
1616 // top of the text but doesn't draw when another popup with higher
1617 // zindex is on top of the character.
1618 screen_zindex = wp->w_zindex;
1619
1620 // adjust w_winrow and w_wincol for border and padding, since
1621 // win_update() doesn't handle them.
1622 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1623 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1624 wp->w_winrow += top_off;
1625 wp->w_wincol += left_off;
1626
1627 // Draw the popup text.
1628 win_update(wp);
1629
1630 wp->w_winrow -= top_off;
1631 wp->w_wincol -= left_off;
1632
1633 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1634 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1635 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1636 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1637 popup_attr = get_wcr_attr(wp);
1638
1639 // We can only use these line drawing characters when 'encoding' is
1640 // "utf-8" and 'ambiwidth' is "single".
1641 if (enc_utf8 && *p_ambw == 's')
1642 {
1643 border_char[0] = border_char[2] = 0x2550;
1644 border_char[1] = border_char[3] = 0x2551;
1645 border_char[4] = 0x2554;
1646 border_char[5] = 0x2557;
1647 border_char[6] = 0x255d;
1648 border_char[7] = 0x255a;
1649 }
1650 else
1651 {
1652 border_char[0] = border_char[2] = '-';
1653 border_char[1] = border_char[3] = '|';
1654 for (i = 4; i < 8; ++i)
1655 border_char[i] = '+';
1656 }
1657 for (i = 0; i < 8; ++i)
1658 if (wp->w_border_char[i] != 0)
1659 border_char[i] = wp->w_border_char[i];
1660
1661 for (i = 0; i < 4; ++i)
1662 {
1663 border_attr[i] = popup_attr;
1664 if (wp->w_border_highlight[i] != NULL)
1665 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1666 }
1667
1668 if (wp->w_popup_border[0] > 0)
1669 {
1670 // top border
1671 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1672 wp->w_wincol,
1673 wp->w_wincol + total_width,
1674 wp->w_popup_border[3] != 0
1675 ? border_char[4] : border_char[0],
1676 border_char[0], border_attr[0]);
1677 if (wp->w_popup_border[1] > 0)
1678 {
1679 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1680 screen_puts(buf, wp->w_winrow,
1681 wp->w_wincol + total_width - 1, border_attr[1]);
1682 }
1683 }
1684
1685 if (wp->w_popup_padding[0] > 0)
1686 {
1687 // top padding
1688 row = wp->w_winrow + wp->w_popup_border[0];
1689 screen_fill(row, row + wp->w_popup_padding[0],
1690 wp->w_wincol + wp->w_popup_border[3],
1691 wp->w_wincol + total_width - wp->w_popup_border[1],
1692 ' ', ' ', popup_attr);
1693 }
1694
1695 for (row = wp->w_winrow + wp->w_popup_border[0];
1696 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1697 ++row)
1698 {
1699 // left border
1700 if (wp->w_popup_border[3] > 0)
1701 {
1702 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1703 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1704 }
1705 // left padding
1706 if (wp->w_popup_padding[3] > 0)
1707 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1708 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1709 // right border
1710 if (wp->w_popup_border[1] > 0)
1711 {
1712 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1713 screen_puts(buf, row,
1714 wp->w_wincol + total_width - 1, border_attr[1]);
1715 }
1716 // right padding
1717 if (wp->w_popup_padding[1] > 0)
1718 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1719 wp->w_wincol + wp->w_popup_border[3]
1720 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1721 }
1722
1723 if (wp->w_popup_padding[2] > 0)
1724 {
1725 // bottom padding
1726 row = wp->w_winrow + wp->w_popup_border[0]
1727 + wp->w_popup_padding[0] + wp->w_height;
1728 screen_fill(row, row + wp->w_popup_padding[2],
1729 wp->w_wincol + wp->w_popup_border[3],
1730 wp->w_wincol + total_width - wp->w_popup_border[1],
1731 ' ', ' ', popup_attr);
1732 }
1733
1734 if (wp->w_popup_border[2] > 0)
1735 {
1736 // bottom border
1737 row = wp->w_winrow + total_height - 1;
1738 screen_fill(row , row + 1,
1739 wp->w_wincol,
1740 wp->w_wincol + total_width,
1741 wp->w_popup_border[3] != 0
1742 ? border_char[7] : border_char[2],
1743 border_char[2], border_attr[2]);
1744 if (wp->w_popup_border[1] > 0)
1745 {
1746 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1747 screen_puts(buf, row,
1748 wp->w_wincol + total_width - 1, border_attr[2]);
1749 }
1750 }
1751
1752 // Back to the normal zindex.
1753 screen_zindex = 0;
1754 }
1755}
1756
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001757#endif // FEAT_TEXT_PROP