blob: 3603a1c7172fe08933e09e8027fa5c63f408900d [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 Moolenaareb2310d2019-06-16 20:09:10 +0200297 str = dict_get_string(dict, (char_u *)"title", FALSE);
298 if (str != NULL)
299 {
300 vim_free(wp->w_popup_title);
301 wp->w_popup_title = vim_strsave(str);
302 }
303
Bram Moolenaar8d241042019-06-12 23:40:01 +0200304 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
305 if (wp->w_firstline < 1)
306 wp->w_firstline = 1;
307
Bram Moolenaar402502d2019-05-30 22:07:36 +0200308 di = dict_find(dict, (char_u *)"wrap", -1);
309 if (di != NULL)
310 {
311 nr = dict_get_number(dict, (char_u *)"wrap");
312 wp->w_p_wrap = nr != 0;
313 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200314
Bram Moolenaara42d9452019-06-15 21:46:30 +0200315 di = dict_find(dict, (char_u *)"drag", -1);
316 if (di != NULL)
317 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200318
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200319 di = dict_find(dict, (char_u *)"callback", -1);
320 if (di != NULL)
321 {
322 callback_T callback = get_callback(&di->di_tv);
323
324 if (callback.cb_name != NULL)
325 set_callback(&wp->w_close_cb, &callback);
326 }
327
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200328 di = dict_find(dict, (char_u *)"filter", -1);
329 if (di != NULL)
330 {
331 callback_T callback = get_callback(&di->di_tv);
332
333 if (callback.cb_name != NULL)
334 set_callback(&wp->w_filter_cb, &callback);
335 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200336
337 get_padding_border(dict, wp->w_popup_padding, "padding", 999);
338 get_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar790498b2019-06-01 22:15:29 +0200339
340 for (i = 0; i < 4; ++i)
341 VIM_CLEAR(wp->w_border_highlight[i]);
342 di = dict_find(dict, (char_u *)"borderhighlight", -1);
343 if (di != NULL)
344 {
345 if (di->di_tv.v_type != VAR_LIST)
346 emsg(_(e_listreq));
347 else
348 {
349 list_T *list = di->di_tv.vval.v_list;
350 listitem_T *li;
351
352 if (list != NULL)
353 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
354 ++i, li = li->li_next)
355 {
356 str = tv_get_string(&li->li_tv);
357 if (*str != NUL)
358 wp->w_border_highlight[i] = vim_strsave(str);
359 }
360 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
361 for (i = 1; i < 4; ++i)
362 wp->w_border_highlight[i] =
363 vim_strsave(wp->w_border_highlight[0]);
364 }
365 }
366
367 for (i = 0; i < 8; ++i)
368 wp->w_border_char[i] = 0;
369 di = dict_find(dict, (char_u *)"borderchars", -1);
370 if (di != NULL)
371 {
372 if (di->di_tv.v_type != VAR_LIST)
373 emsg(_(e_listreq));
374 else
375 {
376 list_T *list = di->di_tv.vval.v_list;
377 listitem_T *li;
378
379 if (list != NULL)
380 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
381 ++i, li = li->li_next)
382 {
383 str = tv_get_string(&li->li_tv);
384 if (*str != NUL)
385 wp->w_border_char[i] = mb_ptr2char(str);
386 }
387 if (list->lv_len == 1)
388 for (i = 1; i < 8; ++i)
389 wp->w_border_char[i] = wp->w_border_char[0];
390 if (list->lv_len == 2)
391 {
392 for (i = 4; i < 8; ++i)
393 wp->w_border_char[i] = wp->w_border_char[1];
394 for (i = 1; i < 4; ++i)
395 wp->w_border_char[i] = wp->w_border_char[0];
396 }
397 }
398 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200399
400 di = dict_find(dict, (char_u *)"moved", -1);
401 if (di != NULL)
402 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200403 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200404 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
405 {
406 char_u *s = di->di_tv.vval.v_string;
407 int flags = 0;
408
409 if (STRCMP(s, "word") == 0)
410 flags = FIND_IDENT | FIND_STRING;
411 else if (STRCMP(s, "WORD") == 0)
412 flags = FIND_STRING;
413 else if (STRCMP(s, "any") != 0)
414 semsg(_(e_invarg2), s);
415 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200416 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200417 }
418 else if (di->di_tv.v_type == VAR_LIST
419 && di->di_tv.vval.v_list != NULL
420 && di->di_tv.vval.v_list->lv_len == 2)
421 {
422 list_T *l = di->di_tv.vval.v_list;
423
424 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
425 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
426 }
427 else
428 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
429 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200430
431 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200432}
433
434/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200435 * Add lines to the popup from a list of strings.
436 */
437 static void
438add_popup_strings(buf_T *buf, list_T *l)
439{
440 listitem_T *li;
441 linenr_T lnum = 0;
442 char_u *p;
443
444 for (li = l->lv_first; li != NULL; li = li->li_next)
445 if (li->li_tv.v_type == VAR_STRING)
446 {
447 p = li->li_tv.vval.v_string;
448 ml_append_buf(buf, lnum++,
449 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
450 }
451}
452
453/*
454 * Add lines to the popup from a list of dictionaries.
455 */
456 static void
457add_popup_dicts(buf_T *buf, list_T *l)
458{
459 listitem_T *li;
460 listitem_T *pli;
461 linenr_T lnum = 0;
462 char_u *p;
463 dict_T *dict;
464
465 // first add the text lines
466 for (li = l->lv_first; li != NULL; li = li->li_next)
467 {
468 if (li->li_tv.v_type != VAR_DICT)
469 {
470 emsg(_(e_dictreq));
471 return;
472 }
473 dict = li->li_tv.vval.v_dict;
474 p = dict == NULL ? NULL
475 : dict_get_string(dict, (char_u *)"text", FALSE);
476 ml_append_buf(buf, lnum++,
477 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
478 }
479
480 // add the text properties
481 lnum = 1;
482 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
483 {
484 dictitem_T *di;
485 list_T *plist;
486
487 dict = li->li_tv.vval.v_dict;
488 di = dict_find(dict, (char_u *)"props", -1);
489 if (di != NULL)
490 {
491 if (di->di_tv.v_type != VAR_LIST)
492 {
493 emsg(_(e_listreq));
494 return;
495 }
496 plist = di->di_tv.vval.v_list;
497 if (plist != NULL)
498 {
499 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
500 {
501 if (pli->li_tv.v_type != VAR_DICT)
502 {
503 emsg(_(e_dictreq));
504 return;
505 }
506 dict = pli->li_tv.vval.v_dict;
507 if (dict != NULL)
508 {
509 int col = dict_get_number(dict, (char_u *)"col");
510
511 prop_add_common( lnum, col, dict, buf, NULL);
512 }
513 }
514 }
515 }
516 }
517}
518
519/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200520 * Return the height of popup window "wp", including border and padding.
521 */
522 int
523popup_height(win_T *wp)
524{
525 return wp->w_height
526 + wp->w_popup_padding[0] + wp->w_popup_border[0]
527 + wp->w_popup_padding[2] + wp->w_popup_border[2];
528}
529
530/*
531 * Return the width of popup window "wp", including border and padding.
532 */
533 int
534popup_width(win_T *wp)
535{
536 return wp->w_width
537 + wp->w_popup_padding[3] + wp->w_popup_border[3]
538 + wp->w_popup_padding[1] + wp->w_popup_border[1];
539}
540
541/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200542 * Get the padding plus border at the top, adjusted to 1 if there is a title.
543 */
544 static int
545popup_top_extra(win_T *wp)
546{
547 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
548
549 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
550 return 1;
551 return extra;
552}
553
554/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200555 * Adjust the position and size of the popup to fit on the screen.
556 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200557 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200558popup_adjust_position(win_T *wp)
559{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200560 linenr_T lnum;
561 int wrapped = 0;
562 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200563 int center_vert = FALSE;
564 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200565 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200566 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200567 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
568 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
569 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
570 int extra_height = top_extra + bot_extra;
571 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200572 int org_winrow = wp->w_winrow;
573 int org_wincol = wp->w_wincol;
574 int org_width = wp->w_width;
575 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200576 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200577
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200578 wp->w_winrow = 0;
579 wp->w_wincol = 0;
580 if (wp->w_popup_pos == POPPOS_CENTER)
581 {
582 // center after computing the size
583 center_vert = TRUE;
584 center_hor = TRUE;
585 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200586 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200587 {
588 if (wp->w_wantline == 0)
589 center_vert = TRUE;
590 else if (wp->w_popup_pos == POPPOS_TOPLEFT
591 || wp->w_popup_pos == POPPOS_TOPRIGHT)
592 {
593 wp->w_winrow = wp->w_wantline - 1;
594 if (wp->w_winrow >= Rows)
595 wp->w_winrow = Rows - 1;
596 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200597
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200598 if (wp->w_wantcol == 0)
599 center_hor = TRUE;
600 else if (wp->w_popup_pos == POPPOS_TOPLEFT
601 || wp->w_popup_pos == POPPOS_BOTLEFT)
602 {
603 wp->w_wincol = wp->w_wantcol - 1;
604 if (wp->w_wincol >= Columns - 3)
605 wp->w_wincol = Columns - 3;
606 }
607 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200608
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200609 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200610 // When left aligned use the space available, but shift to the left when we
611 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200612 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200613 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200614 {
615 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200616 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200617 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200618
Bram Moolenaar8d241042019-06-12 23:40:01 +0200619 // start at the desired first line
620 wp->w_topline = wp->w_firstline;
621 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
622 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
623
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200624 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200625 // Use a minimum width of one, so that something shows when there is no
626 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200627 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200628 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200629 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200630 {
631 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
632
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200633 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200634 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200635 while (len > maxwidth)
636 {
637 ++wrapped;
638 len -= maxwidth;
639 wp->w_width = maxwidth;
640 }
641 }
642 else if (len > maxwidth
643 && allow_adjust_left
644 && (wp->w_popup_pos == POPPOS_TOPLEFT
645 || wp->w_popup_pos == POPPOS_BOTLEFT))
646 {
647 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200648 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200649
Bram Moolenaar51c31312019-06-15 22:27:23 +0200650 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200651 {
652 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200653
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200654 len -= truncate_shift;
655 shift_by -= truncate_shift;
656 }
657
658 wp->w_wincol -= shift_by;
659 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200660 wp->w_width = maxwidth;
661 }
662 if (wp->w_width < len)
663 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200664 // do not use the width of lines we're not going to show
665 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
666 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
667 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200668 }
669
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200670 minwidth = wp->w_minwidth;
671 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
672 {
673 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
674
675 if (minwidth < title_len)
676 minwidth = title_len;
677 }
678
679 if (minwidth > 0 && wp->w_width < minwidth)
680 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200681 if (wp->w_width > maxwidth)
682 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200683 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200684 {
685 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
686 if (wp->w_wincol < 0)
687 wp->w_wincol = 0;
688 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200689 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
690 || wp->w_popup_pos == POPPOS_TOPRIGHT)
691 {
692 // Right aligned: move to the right if needed.
693 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200694 if (wp->w_width + extra_width < wp->w_wantcol)
695 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200696 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200697
Bram Moolenaar8d241042019-06-12 23:40:01 +0200698 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
699 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200700 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
701 wp->w_height = wp->w_minheight;
702 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
703 wp->w_height = wp->w_maxheight;
704 if (wp->w_height > Rows - wp->w_winrow)
705 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200706
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200707 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200708 {
709 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
710 if (wp->w_winrow < 0)
711 wp->w_winrow = 0;
712 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200713 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
714 || wp->w_popup_pos == POPPOS_BOTLEFT)
715 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200716 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200717 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200718 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200719 else
720 // not enough space, make top aligned
721 wp->w_winrow = wp->w_wantline + 1;
722 }
723
Bram Moolenaar17146962019-05-30 00:12:11 +0200724 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200725
726 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200727 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200728 if (org_winrow != wp->w_winrow
729 || org_wincol != wp->w_wincol
730 || org_width != wp->w_width
731 || org_height != wp->w_height)
732 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200733 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200734 popup_mask_refresh = TRUE;
735 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200736}
737
Bram Moolenaar17627312019-06-02 19:53:44 +0200738typedef enum
739{
740 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200741 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200742 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200743 TYPE_DIALOG,
744 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200745} create_type_T;
746
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200747/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200748 * Make "buf" empty and set the contents to "text".
749 * Used by popup_create() and popup_settext().
750 */
751 static void
752popup_set_buffer_text(buf_T *buf, typval_T text)
753{
754 int lnum;
755
756 // Clear the buffer, then replace the lines.
757 curbuf = buf;
758 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
759 ml_delete(lnum, FALSE);
760 curbuf = curwin->w_buffer;
761
762 // Add text to the buffer.
763 if (text.v_type == VAR_STRING)
764 {
765 // just a string
766 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
767 }
768 else
769 {
770 list_T *l = text.vval.v_list;
771
772 if (l->lv_len > 0)
773 {
774 if (l->lv_first->li_tv.v_type == VAR_STRING)
775 // list of strings
776 add_popup_strings(buf, l);
777 else
778 // list of dictionaries
779 add_popup_dicts(buf, l);
780 }
781 }
782
783 // delete the line that was in the empty buffer
784 curbuf = buf;
785 ml_delete(buf->b_ml.ml_line_count, FALSE);
786 curbuf = curwin->w_buffer;
787}
788
789/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200790 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200791 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200792 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200793 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200794popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200795{
796 win_T *wp;
797 buf_T *buf;
798 dict_T *d;
799 int nr;
800
801 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200802 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
803 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200804 {
805 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200806 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200807 }
808 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
809 {
810 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200811 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200812 }
813 d = argvars[1].vval.v_dict;
814
815 // Create the window and buffer.
816 wp = win_alloc_popup_win();
817 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200818 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200819 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200820 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200821
822 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
823 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200824 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200826
827 win_init_popup_win(wp, buf);
828
829 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200830 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200831 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200832 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200833 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200834 buf->b_p_ul = -1; // no undo
835 buf->b_p_swf = FALSE; // no swap file
836 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200837 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200838 wp->w_p_wrap = TRUE; // 'wrap' is default on
839
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200840 // Avoid that 'buftype' is reset when this buffer is entered.
841 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200842
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200843 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
844 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200845 else if (type == TYPE_NOTIFICATION)
846 nr = -1; // notifications are global by default
847 else
848 nr = 0;
849
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200850 if (nr == 0)
851 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200852 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200853 wp->w_next = curtab->tp_first_popupwin;
854 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200855 }
856 else if (nr < 0)
857 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200858 win_T *prev = first_popupwin;
859
860 // Global popup: add at the end, so that it gets displayed on top of
861 // older ones with the same zindex. Matters for notifications.
862 if (first_popupwin == NULL)
863 first_popupwin = wp;
864 else
865 {
866 while (prev->w_next != NULL)
867 prev = prev->w_next;
868 prev->w_next = wp;
869 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200870 }
871 else
872 // TODO: find tab page "nr"
873 emsg("Not implemented yet");
874
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200875 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200876
Bram Moolenaar17627312019-06-02 19:53:44 +0200877 if (type == TYPE_ATCURSOR)
878 {
879 wp->w_popup_pos = POPPOS_BOTLEFT;
880 setcursor_mayforce(TRUE);
881 wp->w_wantline = screen_screenrow();
882 if (wp->w_wantline == 0) // cursor in first line
883 {
884 wp->w_wantline = 2;
885 wp->w_popup_pos = POPPOS_TOPLEFT;
886 }
887 wp->w_wantcol = screen_screencol() + 1;
888 set_moved_values(wp);
889 set_moved_columns(wp, FIND_STRING);
890 }
891
Bram Moolenaar33796b32019-06-08 16:01:13 +0200892 // set default values
893 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
894
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200895 if (type == TYPE_NOTIFICATION)
896 {
897 win_T *twp, *nextwin;
898 int height = buf->b_ml.ml_line_count + 3;
899 int i;
900
901 // Try to not overlap with another global popup. Guess we need 3
902 // more screen lines than buffer lines.
903 wp->w_wantline = 1;
904 for (twp = first_popupwin; twp != NULL; twp = nextwin)
905 {
906 nextwin = twp->w_next;
907 if (twp != wp
908 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
909 && twp->w_winrow <= wp->w_wantline - 1 + height
910 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
911 {
912 // move to below this popup and restart the loop to check for
913 // overlap with other popups
914 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
915 nextwin = first_popupwin;
916 }
917 }
918 if (wp->w_wantline + height > Rows)
919 {
920 // can't avoid overlap, put on top in the hope that message goes
921 // away soon.
922 wp->w_wantline = 1;
923 }
924
925 wp->w_wantcol = 10;
926 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200927 wp->w_minwidth = 20;
928 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200929 for (i = 0; i < 4; ++i)
930 wp->w_popup_border[i] = 1;
931 wp->w_popup_padding[1] = 1;
932 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200933
934 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200935 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200936 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
937 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200938 }
939
Bram Moolenaara730e552019-06-16 19:05:31 +0200940 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +0200941 {
942 int i;
943
944 wp->w_popup_pos = POPPOS_CENTER;
945 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
946 wp->w_popup_drag = 1;
947 for (i = 0; i < 4; ++i)
948 {
949 wp->w_popup_border[i] = 1;
950 wp->w_popup_padding[i] = 1;
951 }
952 }
953
Bram Moolenaara730e552019-06-16 19:05:31 +0200954 if (type == TYPE_MENU)
955 {
956 typval_T tv;
957 callback_T callback;
958
959 tv.v_type = VAR_STRING;
960 tv.vval.v_string = (char_u *)"popup_filter_menu";
961 callback = get_callback(&tv);
962 if (callback.cb_name != NULL)
963 set_callback(&wp->w_filter_cb, &callback);
964
965 wp->w_p_wrap = 0;
966 }
967
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200968 // Deal with options.
Bram Moolenaar17627312019-06-02 19:53:44 +0200969 apply_options(wp, buf, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200970
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200971 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
972 popup_add_timeout(wp, 3000);
973
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200974 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200975
976 wp->w_vsep_width = 0;
977
978 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200979 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +0200980
981 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200982}
983
984/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200985 * popup_clear()
986 */
987 void
988f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
989{
990 close_all_popups();
991}
992
993/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200994 * popup_create({text}, {options})
995 */
996 void
997f_popup_create(typval_T *argvars, typval_T *rettv)
998{
Bram Moolenaar17627312019-06-02 19:53:44 +0200999 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001000}
1001
1002/*
1003 * popup_atcursor({text}, {options})
1004 */
1005 void
1006f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1007{
Bram Moolenaar17627312019-06-02 19:53:44 +02001008 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001009}
1010
1011/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001012 * Invoke the close callback for window "wp" with value "result".
1013 * Careful: The callback may make "wp" invalid!
1014 */
1015 static void
1016invoke_popup_callback(win_T *wp, typval_T *result)
1017{
1018 typval_T rettv;
1019 int dummy;
1020 typval_T argv[3];
1021
1022 argv[0].v_type = VAR_NUMBER;
1023 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1024
1025 if (result != NULL && result->v_type != VAR_UNKNOWN)
1026 copy_tv(result, &argv[1]);
1027 else
1028 {
1029 argv[1].v_type = VAR_NUMBER;
1030 argv[1].vval.v_number = 0;
1031 }
1032
1033 argv[2].v_type = VAR_UNKNOWN;
1034
1035 call_callback(&wp->w_close_cb, -1,
1036 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1037 if (result != NULL)
1038 clear_tv(&argv[1]);
1039 clear_tv(&rettv);
1040}
1041
1042/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001043 * Close popup "wp" and invoke any close callback for it.
1044 */
1045 static void
1046popup_close_and_callback(win_T *wp, typval_T *arg)
1047{
1048 int id = wp->w_id;
1049
1050 if (wp->w_close_cb.cb_name != NULL)
1051 // Careful: This may make "wp" invalid.
1052 invoke_popup_callback(wp, arg);
1053
1054 popup_close(id);
1055}
1056
1057/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001058 * In a filter: check if the typed key is a mouse event that is used for
1059 * dragging the popup.
1060 */
1061 static void
1062filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1063{
1064 int row = mouse_row;
1065 int col = mouse_col;
1066
1067 if (wp->w_popup_drag
1068 && is_mouse_key(c)
1069 && (wp == popup_dragwin
1070 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1071 // do not consume the key, allow for dragging the popup
1072 rettv->vval.v_number = 0;
1073}
1074
1075 static void
1076popup_highlight_curline(win_T *wp)
1077{
1078 int id;
1079 char buf[100];
1080
1081 match_delete(wp, 1, FALSE);
1082
1083 id = syn_name2id((char_u *)"PopupSelected");
1084 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1085 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1086 (char_u *)buf, 10, 1, NULL, NULL);
1087}
1088
1089/*
1090 * popup_filter_menu({text}, {options})
1091 */
1092 void
1093f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1094{
1095 int id = tv_get_number(&argvars[0]);
1096 win_T *wp = win_id2wp(id);
1097 char_u *key = tv_get_string(&argvars[1]);
1098 typval_T res;
1099 int c;
1100 linenr_T old_lnum;
1101
1102 // If the popup has been closed do not consume the key.
1103 if (wp == NULL)
1104 return;
1105
1106 c = *key;
1107 if (c == K_SPECIAL && key[1] != NUL)
1108 c = TO_SPECIAL(key[1], key[2]);
1109
1110 // consume all keys until done
1111 rettv->vval.v_number = 1;
1112 res.v_type = VAR_NUMBER;
1113
1114 old_lnum = wp->w_cursor.lnum;
1115 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1116 --wp->w_cursor.lnum;
1117 if ((c == 'j' || c == 'J' || c == K_DOWN)
1118 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1119 ++wp->w_cursor.lnum;
1120 if (old_lnum != wp->w_cursor.lnum)
1121 {
1122 popup_highlight_curline(wp);
1123 return;
1124 }
1125
1126 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1127 {
1128 // Cancelled, invoke callback with -1
1129 res.vval.v_number = -1;
1130 popup_close_and_callback(wp, &res);
1131 return;
1132 }
1133 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1134 {
1135 // Invoke callback with current index.
1136 res.vval.v_number = wp->w_cursor.lnum;
1137 popup_close_and_callback(wp, &res);
1138 return;
1139 }
1140
1141 filter_handle_drag(wp, c, rettv);
1142}
1143
1144/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001145 * popup_filter_yesno({text}, {options})
1146 */
1147 void
1148f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1149{
1150 int id = tv_get_number(&argvars[0]);
1151 win_T *wp = win_id2wp(id);
1152 char_u *key = tv_get_string(&argvars[1]);
1153 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001154 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001155
1156 // If the popup has been closed don't consume the key.
1157 if (wp == NULL)
1158 return;
1159
Bram Moolenaara730e552019-06-16 19:05:31 +02001160 c = *key;
1161 if (c == K_SPECIAL && key[1] != NUL)
1162 c = TO_SPECIAL(key[1], key[2]);
1163
Bram Moolenaara42d9452019-06-15 21:46:30 +02001164 // consume all keys until done
1165 rettv->vval.v_number = 1;
1166
Bram Moolenaara730e552019-06-16 19:05:31 +02001167 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001168 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001169 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001170 res.vval.v_number = 0;
1171 else
1172 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001173 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001174 return;
1175 }
1176
1177 // Invoke callback
1178 res.v_type = VAR_NUMBER;
1179 popup_close_and_callback(wp, &res);
1180}
1181
1182/*
1183 * popup_dialog({text}, {options})
1184 */
1185 void
1186f_popup_dialog(typval_T *argvars, typval_T *rettv)
1187{
1188 popup_create(argvars, rettv, TYPE_DIALOG);
1189}
1190
1191/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001192 * popup_menu({text}, {options})
1193 */
1194 void
1195f_popup_menu(typval_T *argvars, typval_T *rettv)
1196{
1197 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1198
1199 if (wp != NULL)
1200 popup_highlight_curline(wp);
1201}
1202
1203/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001204 * popup_notification({text}, {options})
1205 */
1206 void
1207f_popup_notification(typval_T *argvars, typval_T *rettv)
1208{
1209 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1210}
1211
1212/*
1213 * Find the popup window with window-ID "id".
1214 * If the popup window does not exist NULL is returned.
1215 * If the window is not a popup window, and error message is given.
1216 */
1217 static win_T *
1218find_popup_win(int id)
1219{
1220 win_T *wp = win_id2wp(id);
1221
1222 if (wp != NULL && !bt_popup(wp->w_buffer))
1223 {
1224 semsg(_("E993: window %d is not a popup window"), id);
1225 return NULL;
1226 }
1227 return wp;
1228}
1229
1230/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001231 * popup_close({id})
1232 */
1233 void
1234f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1235{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001236 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001237 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001238
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001239 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001240 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001241}
1242
1243/*
1244 * popup_hide({id})
1245 */
1246 void
1247f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1248{
1249 int id = (int)tv_get_number(argvars);
1250 win_T *wp = find_popup_win(id);
1251
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001252 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001253 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001254 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001255 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001256 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001257 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001258 }
1259}
1260
1261/*
1262 * popup_show({id})
1263 */
1264 void
1265f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1266{
1267 int id = (int)tv_get_number(argvars);
1268 win_T *wp = find_popup_win(id);
1269
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001270 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001271 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001272 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001273 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001274 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001275 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001276 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001277}
1278
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001279/*
1280 * popup_settext({id}, {text})
1281 */
1282 void
1283f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1284{
1285 int id = (int)tv_get_number(&argvars[0]);
1286 win_T *wp = find_popup_win(id);
1287
1288 if (wp != NULL)
1289 {
1290 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1291 popup_adjust_position(wp);
1292 }
1293}
1294
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001295 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001296popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001297{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001298 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001299 if (wp->w_winrow + wp->w_height >= cmdline_row)
1300 clear_cmdline = TRUE;
1301 win_free_popup(wp);
1302 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001303 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001304}
1305
Bram Moolenaarec583842019-05-26 14:11:23 +02001306/*
1307 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001308 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001309 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001310 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001311popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001312{
1313 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001314 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001315 win_T *prev = NULL;
1316
Bram Moolenaarec583842019-05-26 14:11:23 +02001317 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001318 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001319 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001320 {
1321 if (prev == NULL)
1322 first_popupwin = wp->w_next;
1323 else
1324 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001325 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001326 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001327 }
1328
Bram Moolenaarec583842019-05-26 14:11:23 +02001329 // go through tab-local popups
1330 FOR_ALL_TABPAGES(tp)
1331 popup_close_tabpage(tp, id);
1332}
1333
1334/*
1335 * Close a popup window with Window-id "id" in tabpage "tp".
1336 */
1337 void
1338popup_close_tabpage(tabpage_T *tp, int id)
1339{
1340 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001341 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001342 win_T *prev = NULL;
1343
Bram Moolenaarec583842019-05-26 14:11:23 +02001344 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1345 if (wp->w_id == id)
1346 {
1347 if (prev == NULL)
1348 *root = wp->w_next;
1349 else
1350 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001351 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001352 return;
1353 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001354}
1355
1356 void
1357close_all_popups(void)
1358{
1359 while (first_popupwin != NULL)
1360 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001361 while (curtab->tp_first_popupwin != NULL)
1362 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001363}
1364
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001365/*
1366 * popup_move({id}, {options})
1367 */
1368 void
1369f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1370{
1371 dict_T *d;
1372 int nr;
1373 int id = (int)tv_get_number(argvars);
1374 win_T *wp = find_popup_win(id);
1375
1376 if (wp == NULL)
1377 return; // invalid {id}
1378
1379 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1380 {
1381 emsg(_(e_dictreq));
1382 return;
1383 }
1384 d = argvars[1].vval.v_dict;
1385
1386 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
1387 wp->w_minwidth = nr;
1388 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
1389 wp->w_minheight = nr;
1390 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
1391 wp->w_maxwidth = nr;
1392 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
1393 wp->w_maxheight = nr;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001394 get_pos_options(wp, d);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001395
1396 if (wp->w_winrow + wp->w_height >= cmdline_row)
1397 clear_cmdline = TRUE;
1398 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001399}
1400
Bram Moolenaarbc133542019-05-29 20:26:46 +02001401/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001402 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001403 */
1404 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001405f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001406{
1407 dict_T *dict;
1408 int id = (int)tv_get_number(argvars);
1409 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001410 int top_extra;
1411 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001412
1413 if (rettv_dict_alloc(rettv) == OK)
1414 {
1415 if (wp == NULL)
1416 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001417 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001418 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1419
Bram Moolenaarbc133542019-05-29 20:26:46 +02001420 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001421
Bram Moolenaarbc133542019-05-29 20:26:46 +02001422 dict_add_number(dict, "line", wp->w_winrow + 1);
1423 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001424 dict_add_number(dict, "width", wp->w_width + left_extra
1425 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1426 dict_add_number(dict, "height", wp->w_height + top_extra
1427 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001428
1429 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1430 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1431 dict_add_number(dict, "core_width", wp->w_width);
1432 dict_add_number(dict, "core_height", wp->w_height);
1433
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001434 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001435 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001436 }
1437}
1438
1439/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001440 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001441 */
1442 void
1443f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1444{
1445 dict_T *dict;
1446 int id = (int)tv_get_number(argvars);
1447 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001448 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001449
1450 if (rettv_dict_alloc(rettv) == OK)
1451 {
1452 if (wp == NULL)
1453 return;
1454
1455 dict = rettv->vval.v_dict;
1456 dict_add_number(dict, "line", wp->w_wantline);
1457 dict_add_number(dict, "col", wp->w_wantcol);
1458 dict_add_number(dict, "minwidth", wp->w_minwidth);
1459 dict_add_number(dict, "minheight", wp->w_minheight);
1460 dict_add_number(dict, "maxheight", wp->w_maxheight);
1461 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001462 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001463 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001464 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001465
1466 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1467 ++i)
1468 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1469 {
1470 dict_add_string(dict, "pos",
1471 (char_u *)poppos_entries[i].pp_name);
1472 break;
1473 }
1474
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001475# if defined(FEAT_TIMERS)
1476 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1477 ? (long)wp->w_popup_timer->tr_interval : 0L);
1478# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001479 }
1480}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001481
1482 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001483error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001484{
1485 if (bt_popup(curwin->w_buffer))
1486 {
1487 emsg(_("E994: Not allowed in a popup window"));
1488 return TRUE;
1489 }
1490 return FALSE;
1491}
1492
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001493/*
1494 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001495 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001496 */
1497 void
1498popup_reset_handled()
1499{
1500 win_T *wp;
1501
1502 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1503 wp->w_popup_flags &= ~POPF_HANDLED;
1504 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1505 wp->w_popup_flags &= ~POPF_HANDLED;
1506}
1507
1508/*
1509 * Find the next visible popup where POPF_HANDLED is not set.
1510 * Must have called popup_reset_handled() first.
1511 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1512 * popup with the highest zindex.
1513 */
1514 win_T *
1515find_next_popup(int lowest)
1516{
1517 win_T *wp;
1518 win_T *found_wp;
1519 int found_zindex;
1520
1521 found_zindex = lowest ? INT_MAX : 0;
1522 found_wp = NULL;
1523 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1524 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1525 && (lowest ? wp->w_zindex < found_zindex
1526 : wp->w_zindex > found_zindex))
1527 {
1528 found_zindex = wp->w_zindex;
1529 found_wp = wp;
1530 }
1531 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1532 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1533 && (lowest ? wp->w_zindex < found_zindex
1534 : wp->w_zindex > found_zindex))
1535 {
1536 found_zindex = wp->w_zindex;
1537 found_wp = wp;
1538 }
1539
1540 if (found_wp != NULL)
1541 found_wp->w_popup_flags |= POPF_HANDLED;
1542 return found_wp;
1543}
1544
1545/*
1546 * Invoke the filter callback for window "wp" with typed character "c".
1547 * Uses the global "mod_mask" for modifiers.
1548 * Returns the return value of the filter.
1549 * Careful: The filter may make "wp" invalid!
1550 */
1551 static int
1552invoke_popup_filter(win_T *wp, int c)
1553{
1554 int res;
1555 typval_T rettv;
1556 int dummy;
1557 typval_T argv[3];
1558 char_u buf[NUMBUFLEN];
1559
Bram Moolenaara42d9452019-06-15 21:46:30 +02001560 // Emergency exit: CTRL-C closes the popup.
1561 if (c == Ctrl_C)
1562 {
1563 rettv.v_type = VAR_NUMBER;
1564 rettv.vval.v_number = -1;
1565 popup_close_and_callback(wp, &rettv);
1566 return 1;
1567 }
1568
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001569 argv[0].v_type = VAR_NUMBER;
1570 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1571
1572 // Convert the number to a string, so that the function can use:
1573 // if a:c == "\<F2>"
1574 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1575 argv[1].v_type = VAR_STRING;
1576 argv[1].vval.v_string = vim_strsave(buf);
1577
1578 argv[2].v_type = VAR_UNKNOWN;
1579
Bram Moolenaara42d9452019-06-15 21:46:30 +02001580 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001581 call_callback(&wp->w_filter_cb, -1,
1582 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1583 res = tv_get_number(&rettv);
1584 vim_free(argv[1].vval.v_string);
1585 clear_tv(&rettv);
1586 return res;
1587}
1588
1589/*
1590 * Called when "c" was typed: invoke popup filter callbacks.
1591 * Returns TRUE when the character was consumed,
1592 */
1593 int
1594popup_do_filter(int c)
1595{
1596 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001597 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001598
1599 popup_reset_handled();
1600
1601 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1602 if (wp->w_filter_cb.cb_name != NULL)
1603 res = invoke_popup_filter(wp, c);
1604
1605 return res;
1606}
1607
Bram Moolenaar3397f742019-06-02 18:40:06 +02001608/*
1609 * Called when the cursor moved: check if any popup needs to be closed if the
1610 * cursor moved far enough.
1611 */
1612 void
1613popup_check_cursor_pos()
1614{
1615 win_T *wp;
1616 typval_T tv;
1617
1618 popup_reset_handled();
1619 while ((wp = find_next_popup(TRUE)) != NULL)
1620 if (wp->w_popup_curwin != NULL
1621 && (curwin != wp->w_popup_curwin
1622 || curwin->w_cursor.lnum != wp->w_popup_lnum
1623 || curwin->w_cursor.col < wp->w_popup_mincol
1624 || curwin->w_cursor.col > wp->w_popup_maxcol))
1625 {
1626 tv.v_type = VAR_NUMBER;
1627 tv.vval.v_number = -1;
1628 popup_close_and_callback(wp, &tv);
1629 }
1630}
1631
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001632/*
1633 * Update "popup_mask" if needed.
1634 * Also recomputes the popup size and positions.
1635 * Also updates "popup_visible".
1636 * Also marks window lines for redrawing.
1637 */
1638 void
1639may_update_popup_mask(int type)
1640{
1641 win_T *wp;
1642 short *mask;
1643 int line, col;
1644 int redraw_all = FALSE;
1645
1646 // Need to recompute when switching tabs.
1647 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1648 // (such as the screen size) must have changed.
1649 if (popup_mask_tab != curtab || type >= NOT_VALID)
1650 {
1651 popup_mask_refresh = TRUE;
1652 redraw_all = TRUE;
1653 }
1654 if (!popup_mask_refresh)
1655 {
1656 // Check if any buffer has changed.
1657 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1658 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1659 popup_mask_refresh = TRUE;
1660 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1661 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1662 popup_mask_refresh = TRUE;
1663 if (!popup_mask_refresh)
1664 return;
1665 }
1666
1667 // Need to update the mask, something has changed.
1668 popup_mask_refresh = FALSE;
1669 popup_mask_tab = curtab;
1670 popup_visible = FALSE;
1671
1672 // If redrawing everything, just update "popup_mask".
1673 // If redrawing only what is needed, update "popup_mask_next" and then
1674 // compare with "popup_mask" to see what changed.
1675 if (type >= SOME_VALID)
1676 mask = popup_mask;
1677 else
1678 mask = popup_mask_next;
1679 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1680
1681 // Find the window with the lowest zindex that hasn't been handled yet,
1682 // so that the window with a higher zindex overwrites the value in
1683 // popup_mask.
1684 popup_reset_handled();
1685 while ((wp = find_next_popup(TRUE)) != NULL)
1686 {
1687 popup_visible = TRUE;
1688
1689 // Recompute the position if the text changed.
1690 if (redraw_all
1691 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1692 popup_adjust_position(wp);
1693
1694 for (line = wp->w_winrow;
1695 line < wp->w_winrow + popup_height(wp)
1696 && line < screen_Rows; ++line)
1697 for (col = wp->w_wincol;
1698 col < wp->w_wincol + popup_width(wp)
1699 && col < screen_Columns; ++col)
1700 mask[line * screen_Columns + col] = wp->w_zindex;
1701 }
1702
1703 // Only check which lines are to be updated if not already
1704 // updating all lines.
1705 if (mask == popup_mask_next)
1706 for (line = 0; line < screen_Rows; ++line)
1707 {
1708 int col_done = 0;
1709
1710 for (col = 0; col < screen_Columns; ++col)
1711 {
1712 int off = line * screen_Columns + col;
1713
1714 if (popup_mask[off] != popup_mask_next[off])
1715 {
1716 popup_mask[off] = popup_mask_next[off];
1717
1718 if (line >= cmdline_row)
1719 {
1720 // the command line needs to be cleared if text below
1721 // the popup is now visible.
1722 if (!msg_scrolled && popup_mask_next[off] == 0)
1723 clear_cmdline = TRUE;
1724 }
1725 else if (col >= col_done)
1726 {
1727 linenr_T lnum;
1728 int line_cp = line;
1729 int col_cp = col;
1730
1731 // The screen position "line" / "col" needs to be
1732 // redrawn. Figure out what window that is and update
1733 // w_redraw_top and w_redr_bot. Only needs to be done
1734 // once for each window line.
1735 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1736 if (wp != NULL)
1737 {
1738 if (line_cp >= wp->w_height)
1739 // In (or below) status line
1740 wp->w_redr_status = TRUE;
1741 // compute the position in the buffer line from the
1742 // position on the screen
1743 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1744 &lnum))
1745 // past bottom
1746 wp->w_redr_status = TRUE;
1747 else
1748 redrawWinline(wp, lnum);
1749
1750 // This line is going to be redrawn, no need to
1751 // check until the right side of the window.
1752 col_done = wp->w_wincol + wp->w_width - 1;
1753 }
1754 }
1755 }
1756 }
1757 }
1758}
1759
1760/*
1761 * Return a string of "len" spaces in IObuff.
1762 */
1763 static char_u *
1764get_spaces(int len)
1765{
1766 vim_memset(IObuff, ' ', (size_t)len);
1767 IObuff[len] = NUL;
1768 return IObuff;
1769}
1770
1771/*
1772 * Update popup windows. They are drawn on top of normal windows.
1773 * "win_update" is called for each popup window, lowest zindex first.
1774 */
1775 void
1776update_popups(void (*win_update)(win_T *wp))
1777{
1778 win_T *wp;
1779 int top_off;
1780 int left_off;
1781 int total_width;
1782 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001783 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001784 int popup_attr;
1785 int border_attr[4];
1786 int border_char[8];
1787 char_u buf[MB_MAXBYTES];
1788 int row;
1789 int i;
1790
1791 // Find the window with the lowest zindex that hasn't been updated yet,
1792 // so that the window with a higher zindex is drawn later, thus goes on
1793 // top.
1794 popup_reset_handled();
1795 while ((wp = find_next_popup(TRUE)) != NULL)
1796 {
1797 // This drawing uses the zindex of the popup window, so that it's on
1798 // top of the text but doesn't draw when another popup with higher
1799 // zindex is on top of the character.
1800 screen_zindex = wp->w_zindex;
1801
1802 // adjust w_winrow and w_wincol for border and padding, since
1803 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001804 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001805 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1806 wp->w_winrow += top_off;
1807 wp->w_wincol += left_off;
1808
1809 // Draw the popup text.
1810 win_update(wp);
1811
1812 wp->w_winrow -= top_off;
1813 wp->w_wincol -= left_off;
1814
1815 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1816 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001817 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001818 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1819 popup_attr = get_wcr_attr(wp);
1820
1821 // We can only use these line drawing characters when 'encoding' is
1822 // "utf-8" and 'ambiwidth' is "single".
1823 if (enc_utf8 && *p_ambw == 's')
1824 {
1825 border_char[0] = border_char[2] = 0x2550;
1826 border_char[1] = border_char[3] = 0x2551;
1827 border_char[4] = 0x2554;
1828 border_char[5] = 0x2557;
1829 border_char[6] = 0x255d;
1830 border_char[7] = 0x255a;
1831 }
1832 else
1833 {
1834 border_char[0] = border_char[2] = '-';
1835 border_char[1] = border_char[3] = '|';
1836 for (i = 4; i < 8; ++i)
1837 border_char[i] = '+';
1838 }
1839 for (i = 0; i < 8; ++i)
1840 if (wp->w_border_char[i] != 0)
1841 border_char[i] = wp->w_border_char[i];
1842
1843 for (i = 0; i < 4; ++i)
1844 {
1845 border_attr[i] = popup_attr;
1846 if (wp->w_border_highlight[i] != NULL)
1847 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
1848 }
1849
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001850 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001851 if (wp->w_popup_border[0] > 0)
1852 {
1853 // top border
1854 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1855 wp->w_wincol,
1856 wp->w_wincol + total_width,
1857 wp->w_popup_border[3] != 0
1858 ? border_char[4] : border_char[0],
1859 border_char[0], border_attr[0]);
1860 if (wp->w_popup_border[1] > 0)
1861 {
1862 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1863 screen_puts(buf, wp->w_winrow,
1864 wp->w_wincol + total_width - 1, border_attr[1]);
1865 }
1866 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001867 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
1868 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001869
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001870 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001871 {
1872 // top padding
1873 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001874 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001875 wp->w_wincol + wp->w_popup_border[3],
1876 wp->w_wincol + total_width - wp->w_popup_border[1],
1877 ' ', ' ', popup_attr);
1878 }
1879
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001880 // Title goes on top of border or padding.
1881 if (wp->w_popup_title != NULL)
1882 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
1883 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
1884
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001885 for (row = wp->w_winrow + wp->w_popup_border[0];
1886 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1887 ++row)
1888 {
1889 // left border
1890 if (wp->w_popup_border[3] > 0)
1891 {
1892 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1893 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1894 }
1895 // left padding
1896 if (wp->w_popup_padding[3] > 0)
1897 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1898 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1899 // right border
1900 if (wp->w_popup_border[1] > 0)
1901 {
1902 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1903 screen_puts(buf, row,
1904 wp->w_wincol + total_width - 1, border_attr[1]);
1905 }
1906 // right padding
1907 if (wp->w_popup_padding[1] > 0)
1908 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1909 wp->w_wincol + wp->w_popup_border[3]
1910 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1911 }
1912
1913 if (wp->w_popup_padding[2] > 0)
1914 {
1915 // bottom padding
1916 row = wp->w_winrow + wp->w_popup_border[0]
1917 + wp->w_popup_padding[0] + wp->w_height;
1918 screen_fill(row, row + wp->w_popup_padding[2],
1919 wp->w_wincol + wp->w_popup_border[3],
1920 wp->w_wincol + total_width - wp->w_popup_border[1],
1921 ' ', ' ', popup_attr);
1922 }
1923
1924 if (wp->w_popup_border[2] > 0)
1925 {
1926 // bottom border
1927 row = wp->w_winrow + total_height - 1;
1928 screen_fill(row , row + 1,
1929 wp->w_wincol,
1930 wp->w_wincol + total_width,
1931 wp->w_popup_border[3] != 0
1932 ? border_char[7] : border_char[2],
1933 border_char[2], border_attr[2]);
1934 if (wp->w_popup_border[1] > 0)
1935 {
1936 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1937 screen_puts(buf, row,
1938 wp->w_wincol + total_width - 1, border_attr[2]);
1939 }
1940 }
1941
1942 // Back to the normal zindex.
1943 screen_zindex = 0;
1944 }
1945}
1946
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001947#endif // FEAT_TEXT_PROP