blob: b1c9840d261998f7481327d6f09b3b9945b19fa8 [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
Bram Moolenaarae943152019-06-16 22:54:14 +0200109set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200110{
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/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200254 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200255 */
256 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200257apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200258{
Bram Moolenaarae943152019-06-16 22:54:14 +0200259 int nr;
260
261 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
262 wp->w_minwidth = nr;
263 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
264 wp->w_minheight = nr;
265 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
266 wp->w_maxwidth = nr;
267 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
268 wp->w_maxheight = nr;
269 get_pos_options(wp, d);
270}
271
272/*
273 * Shared between popup_create() and f_popup_setoptions().
274 */
275 static void
276apply_general_options(win_T *wp, dict_T *dict)
277{
278 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200279 int nr;
280 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200281
Bram Moolenaarae943152019-06-16 22:54:14 +0200282 // TODO: flip
283
284 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200285 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200286 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
287 if (wp->w_firstline < 1)
288 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200289
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200290 str = dict_get_string(dict, (char_u *)"title", FALSE);
291 if (str != NULL)
292 {
293 vim_free(wp->w_popup_title);
294 wp->w_popup_title = vim_strsave(str);
295 }
296
Bram Moolenaar402502d2019-05-30 22:07:36 +0200297 di = dict_find(dict, (char_u *)"wrap", -1);
298 if (di != NULL)
299 {
300 nr = dict_get_number(dict, (char_u *)"wrap");
301 wp->w_p_wrap = nr != 0;
302 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200303
Bram Moolenaara42d9452019-06-15 21:46:30 +0200304 di = dict_find(dict, (char_u *)"drag", -1);
305 if (di != NULL)
306 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200307
Bram Moolenaarae943152019-06-16 22:54:14 +0200308 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
309 if (str != NULL)
310 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
311 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200312
Bram Moolenaarae943152019-06-16 22:54:14 +0200313 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
314 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200315
Bram Moolenaar790498b2019-06-01 22:15:29 +0200316 di = dict_find(dict, (char_u *)"borderhighlight", -1);
317 if (di != NULL)
318 {
319 if (di->di_tv.v_type != VAR_LIST)
320 emsg(_(e_listreq));
321 else
322 {
323 list_T *list = di->di_tv.vval.v_list;
324 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200325 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200326
327 if (list != NULL)
328 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
329 ++i, li = li->li_next)
330 {
331 str = tv_get_string(&li->li_tv);
332 if (*str != NUL)
333 wp->w_border_highlight[i] = vim_strsave(str);
334 }
335 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
336 for (i = 1; i < 4; ++i)
337 wp->w_border_highlight[i] =
338 vim_strsave(wp->w_border_highlight[0]);
339 }
340 }
341
Bram Moolenaar790498b2019-06-01 22:15:29 +0200342 di = dict_find(dict, (char_u *)"borderchars", -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;
Bram Moolenaarae943152019-06-16 22:54:14 +0200351 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200352
353 if (list != NULL)
354 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
355 ++i, li = li->li_next)
356 {
357 str = tv_get_string(&li->li_tv);
358 if (*str != NUL)
359 wp->w_border_char[i] = mb_ptr2char(str);
360 }
361 if (list->lv_len == 1)
362 for (i = 1; i < 8; ++i)
363 wp->w_border_char[i] = wp->w_border_char[0];
364 if (list->lv_len == 2)
365 {
366 for (i = 4; i < 8; ++i)
367 wp->w_border_char[i] = wp->w_border_char[1];
368 for (i = 1; i < 4; ++i)
369 wp->w_border_char[i] = wp->w_border_char[0];
370 }
371 }
372 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200373
Bram Moolenaarae943152019-06-16 22:54:14 +0200374 di = dict_find(dict, (char_u *)"zindex", -1);
375 if (di != NULL)
376 {
377 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
378 if (wp->w_zindex < 1)
379 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
380 if (wp->w_zindex > 32000)
381 wp->w_zindex = 32000;
382 }
383
384#if defined(FEAT_TIMERS)
385 // Add timer to close the popup after some time.
386 nr = dict_get_number(dict, (char_u *)"time");
387 if (nr > 0)
388 popup_add_timeout(wp, nr);
389#endif
390
Bram Moolenaar3397f742019-06-02 18:40:06 +0200391 di = dict_find(dict, (char_u *)"moved", -1);
392 if (di != NULL)
393 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200394 set_moved_values(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200395 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
396 {
397 char_u *s = di->di_tv.vval.v_string;
398 int flags = 0;
399
400 if (STRCMP(s, "word") == 0)
401 flags = FIND_IDENT | FIND_STRING;
402 else if (STRCMP(s, "WORD") == 0)
403 flags = FIND_STRING;
404 else if (STRCMP(s, "any") != 0)
405 semsg(_(e_invarg2), s);
406 if (flags != 0)
Bram Moolenaar17627312019-06-02 19:53:44 +0200407 set_moved_columns(wp, flags);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200408 }
409 else if (di->di_tv.v_type == VAR_LIST
410 && di->di_tv.vval.v_list != NULL
411 && di->di_tv.vval.v_list->lv_len == 2)
412 {
413 list_T *l = di->di_tv.vval.v_list;
414
415 wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
416 wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
417 }
418 else
419 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
420 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200421
Bram Moolenaarae943152019-06-16 22:54:14 +0200422 di = dict_find(dict, (char_u *)"filter", -1);
423 if (di != NULL)
424 {
425 callback_T callback = get_callback(&di->di_tv);
426
427 if (callback.cb_name != NULL)
428 {
429 free_callback(&wp->w_filter_cb);
430 set_callback(&wp->w_filter_cb, &callback);
431 }
432 }
433
434 di = dict_find(dict, (char_u *)"callback", -1);
435 if (di != NULL)
436 {
437 callback_T callback = get_callback(&di->di_tv);
438
439 if (callback.cb_name != NULL)
440 {
441 free_callback(&wp->w_close_cb);
442 set_callback(&wp->w_close_cb, &callback);
443 }
444 }
445}
446
447/*
448 * Go through the options in "dict" and apply them to popup window "wp".
449 */
450 static void
451apply_options(win_T *wp, dict_T *dict)
452{
453 int nr;
454
455 apply_move_options(wp, dict);
456 apply_general_options(wp, dict);
457
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200458 nr = dict_get_number(dict, (char_u *)"hidden");
459 if (nr > 0)
460 {
461 wp->w_popup_flags |= POPF_HIDDEN;
462 --wp->w_buffer->b_nwindows;
463 }
464
Bram Moolenaar33796b32019-06-08 16:01:13 +0200465 popup_mask_refresh = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200466}
467
468/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200469 * Add lines to the popup from a list of strings.
470 */
471 static void
472add_popup_strings(buf_T *buf, list_T *l)
473{
474 listitem_T *li;
475 linenr_T lnum = 0;
476 char_u *p;
477
478 for (li = l->lv_first; li != NULL; li = li->li_next)
479 if (li->li_tv.v_type == VAR_STRING)
480 {
481 p = li->li_tv.vval.v_string;
482 ml_append_buf(buf, lnum++,
483 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
484 }
485}
486
487/*
488 * Add lines to the popup from a list of dictionaries.
489 */
490 static void
491add_popup_dicts(buf_T *buf, list_T *l)
492{
493 listitem_T *li;
494 listitem_T *pli;
495 linenr_T lnum = 0;
496 char_u *p;
497 dict_T *dict;
498
499 // first add the text lines
500 for (li = l->lv_first; li != NULL; li = li->li_next)
501 {
502 if (li->li_tv.v_type != VAR_DICT)
503 {
504 emsg(_(e_dictreq));
505 return;
506 }
507 dict = li->li_tv.vval.v_dict;
508 p = dict == NULL ? NULL
509 : dict_get_string(dict, (char_u *)"text", FALSE);
510 ml_append_buf(buf, lnum++,
511 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
512 }
513
514 // add the text properties
515 lnum = 1;
516 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
517 {
518 dictitem_T *di;
519 list_T *plist;
520
521 dict = li->li_tv.vval.v_dict;
522 di = dict_find(dict, (char_u *)"props", -1);
523 if (di != NULL)
524 {
525 if (di->di_tv.v_type != VAR_LIST)
526 {
527 emsg(_(e_listreq));
528 return;
529 }
530 plist = di->di_tv.vval.v_list;
531 if (plist != NULL)
532 {
533 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
534 {
535 if (pli->li_tv.v_type != VAR_DICT)
536 {
537 emsg(_(e_dictreq));
538 return;
539 }
540 dict = pli->li_tv.vval.v_dict;
541 if (dict != NULL)
542 {
543 int col = dict_get_number(dict, (char_u *)"col");
544
545 prop_add_common( lnum, col, dict, buf, NULL);
546 }
547 }
548 }
549 }
550 }
551}
552
553/*
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200554 * Return the height of popup window "wp", including border and padding.
555 */
556 int
557popup_height(win_T *wp)
558{
559 return wp->w_height
560 + wp->w_popup_padding[0] + wp->w_popup_border[0]
561 + wp->w_popup_padding[2] + wp->w_popup_border[2];
562}
563
564/*
565 * Return the width of popup window "wp", including border and padding.
566 */
567 int
568popup_width(win_T *wp)
569{
570 return wp->w_width
571 + wp->w_popup_padding[3] + wp->w_popup_border[3]
572 + wp->w_popup_padding[1] + wp->w_popup_border[1];
573}
574
575/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200576 * Get the padding plus border at the top, adjusted to 1 if there is a title.
577 */
578 static int
579popup_top_extra(win_T *wp)
580{
581 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
582
583 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
584 return 1;
585 return extra;
586}
587
588/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200589 * Adjust the position and size of the popup to fit on the screen.
590 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200591 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200592popup_adjust_position(win_T *wp)
593{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200594 linenr_T lnum;
595 int wrapped = 0;
596 int maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200597 int center_vert = FALSE;
598 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200599 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200600 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200601 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
602 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
603 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
604 int extra_height = top_extra + bot_extra;
605 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200606 int org_winrow = wp->w_winrow;
607 int org_wincol = wp->w_wincol;
608 int org_width = wp->w_width;
609 int org_height = wp->w_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200610 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200611
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200612 wp->w_winrow = 0;
613 wp->w_wincol = 0;
614 if (wp->w_popup_pos == POPPOS_CENTER)
615 {
616 // center after computing the size
617 center_vert = TRUE;
618 center_hor = TRUE;
619 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200620 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200621 {
622 if (wp->w_wantline == 0)
623 center_vert = TRUE;
624 else if (wp->w_popup_pos == POPPOS_TOPLEFT
625 || wp->w_popup_pos == POPPOS_TOPRIGHT)
626 {
627 wp->w_winrow = wp->w_wantline - 1;
628 if (wp->w_winrow >= Rows)
629 wp->w_winrow = Rows - 1;
630 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200631
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200632 if (wp->w_wantcol == 0)
633 center_hor = TRUE;
634 else if (wp->w_popup_pos == POPPOS_TOPLEFT
635 || wp->w_popup_pos == POPPOS_BOTLEFT)
636 {
637 wp->w_wincol = wp->w_wantcol - 1;
638 if (wp->w_wincol >= Columns - 3)
639 wp->w_wincol = Columns - 3;
640 }
641 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200642
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200643 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200644 // When left aligned use the space available, but shift to the left when we
645 // hit the right of the screen.
Bram Moolenaar51c31312019-06-15 22:27:23 +0200646 maxwidth = Columns - wp->w_wincol - left_extra;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200647 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200648 {
649 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200650 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200651 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200652
Bram Moolenaar8d241042019-06-12 23:40:01 +0200653 // start at the desired first line
654 wp->w_topline = wp->w_firstline;
655 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
656 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
657
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200658 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200659 // Use a minimum width of one, so that something shows when there is no
660 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200661 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200662 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200663 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200664 {
665 int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
666
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200667 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200668 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200669 while (len > maxwidth)
670 {
671 ++wrapped;
672 len -= maxwidth;
673 wp->w_width = maxwidth;
674 }
675 }
676 else if (len > maxwidth
677 && allow_adjust_left
678 && (wp->w_popup_pos == POPPOS_TOPLEFT
679 || wp->w_popup_pos == POPPOS_BOTLEFT))
680 {
681 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200682 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200683
Bram Moolenaar51c31312019-06-15 22:27:23 +0200684 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200685 {
686 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200687
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200688 len -= truncate_shift;
689 shift_by -= truncate_shift;
690 }
691
692 wp->w_wincol -= shift_by;
693 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200694 wp->w_width = maxwidth;
695 }
696 if (wp->w_width < len)
697 wp->w_width = len;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200698 // do not use the width of lines we're not going to show
699 if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
700 - wp->w_topline + 1 + wrapped > wp->w_maxheight)
701 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200702 }
703
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200704 minwidth = wp->w_minwidth;
705 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
706 {
707 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
708
709 if (minwidth < title_len)
710 minwidth = title_len;
711 }
712
713 if (minwidth > 0 && wp->w_width < minwidth)
714 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200715 if (wp->w_width > maxwidth)
716 wp->w_width = maxwidth;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200717 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +0200718 {
719 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
720 if (wp->w_wincol < 0)
721 wp->w_wincol = 0;
722 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200723 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
724 || wp->w_popup_pos == POPPOS_TOPRIGHT)
725 {
726 // Right aligned: move to the right if needed.
727 // No truncation, because that would change the height.
Bram Moolenaar399d8982019-06-02 15:34:29 +0200728 if (wp->w_width + extra_width < wp->w_wantcol)
729 wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200730 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200731
Bram Moolenaar8d241042019-06-12 23:40:01 +0200732 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
733 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200734 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
735 wp->w_height = wp->w_minheight;
736 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
737 wp->w_height = wp->w_maxheight;
738 if (wp->w_height > Rows - wp->w_winrow)
739 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +0200740
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200741 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +0200742 {
743 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
744 if (wp->w_winrow < 0)
745 wp->w_winrow = 0;
746 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200747 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
748 || wp->w_popup_pos == POPPOS_BOTLEFT)
749 {
Bram Moolenaar399d8982019-06-02 15:34:29 +0200750 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200751 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +0200752 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200753 else
754 // not enough space, make top aligned
755 wp->w_winrow = wp->w_wantline + 1;
756 }
757
Bram Moolenaar17146962019-05-30 00:12:11 +0200758 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200759
760 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +0200761 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +0200762 if (org_winrow != wp->w_winrow
763 || org_wincol != wp->w_wincol
764 || org_width != wp->w_width
765 || org_height != wp->w_height)
766 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200767 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200768 popup_mask_refresh = TRUE;
769 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200770}
771
Bram Moolenaar17627312019-06-02 19:53:44 +0200772typedef enum
773{
774 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200775 TYPE_ATCURSOR,
Bram Moolenaara42d9452019-06-15 21:46:30 +0200776 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +0200777 TYPE_DIALOG,
778 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +0200779} create_type_T;
780
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200781/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200782 * Make "buf" empty and set the contents to "text".
783 * Used by popup_create() and popup_settext().
784 */
785 static void
786popup_set_buffer_text(buf_T *buf, typval_T text)
787{
788 int lnum;
789
790 // Clear the buffer, then replace the lines.
791 curbuf = buf;
792 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
793 ml_delete(lnum, FALSE);
794 curbuf = curwin->w_buffer;
795
796 // Add text to the buffer.
797 if (text.v_type == VAR_STRING)
798 {
799 // just a string
800 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
801 }
802 else
803 {
804 list_T *l = text.vval.v_list;
805
806 if (l->lv_len > 0)
807 {
808 if (l->lv_first->li_tv.v_type == VAR_STRING)
809 // list of strings
810 add_popup_strings(buf, l);
811 else
812 // list of dictionaries
813 add_popup_dicts(buf, l);
814 }
815 }
816
817 // delete the line that was in the empty buffer
818 curbuf = buf;
819 ml_delete(buf->b_ml.ml_line_count, FALSE);
820 curbuf = curwin->w_buffer;
821}
822
823/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200824 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200825 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826 */
Bram Moolenaara730e552019-06-16 19:05:31 +0200827 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +0200828popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200829{
830 win_T *wp;
831 buf_T *buf;
832 dict_T *d;
833 int nr;
Bram Moolenaarae943152019-06-16 22:54:14 +0200834 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200835
836 // Check arguments look OK.
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200837 if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
838 && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200839 {
840 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200841 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200842 }
843 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
844 {
845 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +0200846 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200847 }
848 d = argvars[1].vval.v_dict;
849
850 // Create the window and buffer.
851 wp = win_alloc_popup_win();
852 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200853 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200854 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200855 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200856
857 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
858 if (buf == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +0200859 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200860 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200861
862 win_init_popup_win(wp, buf);
863
864 set_local_options_default(wp);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200865 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200866 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar20c023a2019-05-26 21:03:24 +0200867 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200868 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200869 buf->b_p_ul = -1; // no undo
870 buf->b_p_swf = FALSE; // no swap file
871 buf->b_p_bl = FALSE; // unlisted buffer
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200872 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200873 wp->w_p_wrap = TRUE; // 'wrap' is default on
874
Bram Moolenaar54fabd42019-05-30 19:03:22 +0200875 // Avoid that 'buftype' is reset when this buffer is entered.
876 buf->b_p_initialized = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200877
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200878 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
879 nr = (int)dict_get_number(d, (char_u *)"tabpage");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200880 else if (type == TYPE_NOTIFICATION)
881 nr = -1; // notifications are global by default
882 else
883 nr = 0;
884
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200885 if (nr == 0)
886 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200887 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +0200888 wp->w_next = curtab->tp_first_popupwin;
889 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200890 }
891 else if (nr < 0)
892 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200893 win_T *prev = first_popupwin;
894
895 // Global popup: add at the end, so that it gets displayed on top of
896 // older ones with the same zindex. Matters for notifications.
897 if (first_popupwin == NULL)
898 first_popupwin = wp;
899 else
900 {
901 while (prev->w_next != NULL)
902 prev = prev->w_next;
903 prev->w_next = wp;
904 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200905 }
906 else
907 // TODO: find tab page "nr"
908 emsg("Not implemented yet");
909
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200910 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200911
Bram Moolenaar17627312019-06-02 19:53:44 +0200912 if (type == TYPE_ATCURSOR)
913 {
914 wp->w_popup_pos = POPPOS_BOTLEFT;
915 setcursor_mayforce(TRUE);
916 wp->w_wantline = screen_screenrow();
917 if (wp->w_wantline == 0) // cursor in first line
918 {
919 wp->w_wantline = 2;
920 wp->w_popup_pos = POPPOS_TOPLEFT;
921 }
922 wp->w_wantcol = screen_screencol() + 1;
923 set_moved_values(wp);
924 set_moved_columns(wp, FIND_STRING);
925 }
926
Bram Moolenaar33796b32019-06-08 16:01:13 +0200927 // set default values
928 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
929
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200930 if (type == TYPE_NOTIFICATION)
931 {
932 win_T *twp, *nextwin;
933 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200934
935 // Try to not overlap with another global popup. Guess we need 3
936 // more screen lines than buffer lines.
937 wp->w_wantline = 1;
938 for (twp = first_popupwin; twp != NULL; twp = nextwin)
939 {
940 nextwin = twp->w_next;
941 if (twp != wp
942 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
943 && twp->w_winrow <= wp->w_wantline - 1 + height
944 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
945 {
946 // move to below this popup and restart the loop to check for
947 // overlap with other popups
948 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
949 nextwin = first_popupwin;
950 }
951 }
952 if (wp->w_wantline + height > Rows)
953 {
954 // can't avoid overlap, put on top in the hope that message goes
955 // away soon.
956 wp->w_wantline = 1;
957 }
958
959 wp->w_wantcol = 10;
960 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200961 wp->w_minwidth = 20;
962 wp->w_popup_drag = 1;
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200963 for (i = 0; i < 4; ++i)
964 wp->w_popup_border[i] = 1;
965 wp->w_popup_padding[1] = 1;
966 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200967
968 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200969 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200970 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
971 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200972 }
973
Bram Moolenaara730e552019-06-16 19:05:31 +0200974 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +0200975 {
Bram Moolenaara42d9452019-06-15 21:46:30 +0200976 wp->w_popup_pos = POPPOS_CENTER;
977 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
978 wp->w_popup_drag = 1;
979 for (i = 0; i < 4; ++i)
980 {
981 wp->w_popup_border[i] = 1;
982 wp->w_popup_padding[i] = 1;
983 }
984 }
985
Bram Moolenaara730e552019-06-16 19:05:31 +0200986 if (type == TYPE_MENU)
987 {
988 typval_T tv;
989 callback_T callback;
990
991 tv.v_type = VAR_STRING;
992 tv.vval.v_string = (char_u *)"popup_filter_menu";
993 callback = get_callback(&tv);
994 if (callback.cb_name != NULL)
995 set_callback(&wp->w_filter_cb, &callback);
996
997 wp->w_p_wrap = 0;
998 }
999
Bram Moolenaarae943152019-06-16 22:54:14 +02001000 for (i = 0; i < 4; ++i)
1001 VIM_CLEAR(wp->w_border_highlight[i]);
1002 for (i = 0; i < 8; ++i)
1003 wp->w_border_char[i] = 0;
1004
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001005 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001006 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001007
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001008 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1009 popup_add_timeout(wp, 3000);
1010
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001011 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001012
1013 wp->w_vsep_width = 0;
1014
1015 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001016 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001017
1018 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001019}
1020
1021/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001022 * popup_clear()
1023 */
1024 void
1025f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1026{
1027 close_all_popups();
1028}
1029
1030/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001031 * popup_create({text}, {options})
1032 */
1033 void
1034f_popup_create(typval_T *argvars, typval_T *rettv)
1035{
Bram Moolenaar17627312019-06-02 19:53:44 +02001036 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001037}
1038
1039/*
1040 * popup_atcursor({text}, {options})
1041 */
1042 void
1043f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1044{
Bram Moolenaar17627312019-06-02 19:53:44 +02001045 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001046}
1047
1048/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001049 * Invoke the close callback for window "wp" with value "result".
1050 * Careful: The callback may make "wp" invalid!
1051 */
1052 static void
1053invoke_popup_callback(win_T *wp, typval_T *result)
1054{
1055 typval_T rettv;
1056 int dummy;
1057 typval_T argv[3];
1058
1059 argv[0].v_type = VAR_NUMBER;
1060 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1061
1062 if (result != NULL && result->v_type != VAR_UNKNOWN)
1063 copy_tv(result, &argv[1]);
1064 else
1065 {
1066 argv[1].v_type = VAR_NUMBER;
1067 argv[1].vval.v_number = 0;
1068 }
1069
1070 argv[2].v_type = VAR_UNKNOWN;
1071
1072 call_callback(&wp->w_close_cb, -1,
1073 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1074 if (result != NULL)
1075 clear_tv(&argv[1]);
1076 clear_tv(&rettv);
1077}
1078
1079/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001080 * Close popup "wp" and invoke any close callback for it.
1081 */
1082 static void
1083popup_close_and_callback(win_T *wp, typval_T *arg)
1084{
1085 int id = wp->w_id;
1086
1087 if (wp->w_close_cb.cb_name != NULL)
1088 // Careful: This may make "wp" invalid.
1089 invoke_popup_callback(wp, arg);
1090
1091 popup_close(id);
1092}
1093
1094/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001095 * In a filter: check if the typed key is a mouse event that is used for
1096 * dragging the popup.
1097 */
1098 static void
1099filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1100{
1101 int row = mouse_row;
1102 int col = mouse_col;
1103
1104 if (wp->w_popup_drag
1105 && is_mouse_key(c)
1106 && (wp == popup_dragwin
1107 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1108 // do not consume the key, allow for dragging the popup
1109 rettv->vval.v_number = 0;
1110}
1111
1112 static void
1113popup_highlight_curline(win_T *wp)
1114{
1115 int id;
1116 char buf[100];
1117
1118 match_delete(wp, 1, FALSE);
1119
1120 id = syn_name2id((char_u *)"PopupSelected");
1121 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
1122 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
1123 (char_u *)buf, 10, 1, NULL, NULL);
1124}
1125
1126/*
1127 * popup_filter_menu({text}, {options})
1128 */
1129 void
1130f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1131{
1132 int id = tv_get_number(&argvars[0]);
1133 win_T *wp = win_id2wp(id);
1134 char_u *key = tv_get_string(&argvars[1]);
1135 typval_T res;
1136 int c;
1137 linenr_T old_lnum;
1138
1139 // If the popup has been closed do not consume the key.
1140 if (wp == NULL)
1141 return;
1142
1143 c = *key;
1144 if (c == K_SPECIAL && key[1] != NUL)
1145 c = TO_SPECIAL(key[1], key[2]);
1146
1147 // consume all keys until done
1148 rettv->vval.v_number = 1;
1149 res.v_type = VAR_NUMBER;
1150
1151 old_lnum = wp->w_cursor.lnum;
1152 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1153 --wp->w_cursor.lnum;
1154 if ((c == 'j' || c == 'J' || c == K_DOWN)
1155 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1156 ++wp->w_cursor.lnum;
1157 if (old_lnum != wp->w_cursor.lnum)
1158 {
1159 popup_highlight_curline(wp);
1160 return;
1161 }
1162
1163 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1164 {
1165 // Cancelled, invoke callback with -1
1166 res.vval.v_number = -1;
1167 popup_close_and_callback(wp, &res);
1168 return;
1169 }
1170 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1171 {
1172 // Invoke callback with current index.
1173 res.vval.v_number = wp->w_cursor.lnum;
1174 popup_close_and_callback(wp, &res);
1175 return;
1176 }
1177
1178 filter_handle_drag(wp, c, rettv);
1179}
1180
1181/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001182 * popup_filter_yesno({text}, {options})
1183 */
1184 void
1185f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1186{
1187 int id = tv_get_number(&argvars[0]);
1188 win_T *wp = win_id2wp(id);
1189 char_u *key = tv_get_string(&argvars[1]);
1190 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001191 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001192
1193 // If the popup has been closed don't consume the key.
1194 if (wp == NULL)
1195 return;
1196
Bram Moolenaara730e552019-06-16 19:05:31 +02001197 c = *key;
1198 if (c == K_SPECIAL && key[1] != NUL)
1199 c = TO_SPECIAL(key[1], key[2]);
1200
Bram Moolenaara42d9452019-06-15 21:46:30 +02001201 // consume all keys until done
1202 rettv->vval.v_number = 1;
1203
Bram Moolenaara730e552019-06-16 19:05:31 +02001204 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001205 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001206 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001207 res.vval.v_number = 0;
1208 else
1209 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001210 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001211 return;
1212 }
1213
1214 // Invoke callback
1215 res.v_type = VAR_NUMBER;
1216 popup_close_and_callback(wp, &res);
1217}
1218
1219/*
1220 * popup_dialog({text}, {options})
1221 */
1222 void
1223f_popup_dialog(typval_T *argvars, typval_T *rettv)
1224{
1225 popup_create(argvars, rettv, TYPE_DIALOG);
1226}
1227
1228/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001229 * popup_menu({text}, {options})
1230 */
1231 void
1232f_popup_menu(typval_T *argvars, typval_T *rettv)
1233{
1234 win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
1235
1236 if (wp != NULL)
1237 popup_highlight_curline(wp);
1238}
1239
1240/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001241 * popup_notification({text}, {options})
1242 */
1243 void
1244f_popup_notification(typval_T *argvars, typval_T *rettv)
1245{
1246 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1247}
1248
1249/*
1250 * Find the popup window with window-ID "id".
1251 * If the popup window does not exist NULL is returned.
1252 * If the window is not a popup window, and error message is given.
1253 */
1254 static win_T *
1255find_popup_win(int id)
1256{
1257 win_T *wp = win_id2wp(id);
1258
1259 if (wp != NULL && !bt_popup(wp->w_buffer))
1260 {
1261 semsg(_("E993: window %d is not a popup window"), id);
1262 return NULL;
1263 }
1264 return wp;
1265}
1266
1267/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001268 * popup_close({id})
1269 */
1270 void
1271f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1272{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001273 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001274 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001275
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001276 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001277 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001278}
1279
1280/*
1281 * popup_hide({id})
1282 */
1283 void
1284f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1285{
1286 int id = (int)tv_get_number(argvars);
1287 win_T *wp = find_popup_win(id);
1288
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001289 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001290 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001291 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001292 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001293 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001294 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001295 }
1296}
1297
1298/*
1299 * popup_show({id})
1300 */
1301 void
1302f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1303{
1304 int id = (int)tv_get_number(argvars);
1305 win_T *wp = find_popup_win(id);
1306
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001307 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001308 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001309 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001310 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001311 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001312 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001313 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001314}
1315
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001316/*
1317 * popup_settext({id}, {text})
1318 */
1319 void
1320f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1321{
1322 int id = (int)tv_get_number(&argvars[0]);
1323 win_T *wp = find_popup_win(id);
1324
1325 if (wp != NULL)
1326 {
1327 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1328 popup_adjust_position(wp);
1329 }
1330}
1331
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001332 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001333popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001334{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001335 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001336 if (wp->w_winrow + wp->w_height >= cmdline_row)
1337 clear_cmdline = TRUE;
1338 win_free_popup(wp);
1339 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001340 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001341}
1342
Bram Moolenaarec583842019-05-26 14:11:23 +02001343/*
1344 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001345 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001346 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001347 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001348popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001349{
1350 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001351 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001352 win_T *prev = NULL;
1353
Bram Moolenaarec583842019-05-26 14:11:23 +02001354 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001355 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001356 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001357 {
1358 if (prev == NULL)
1359 first_popupwin = wp->w_next;
1360 else
1361 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001362 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001363 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001364 }
1365
Bram Moolenaarec583842019-05-26 14:11:23 +02001366 // go through tab-local popups
1367 FOR_ALL_TABPAGES(tp)
1368 popup_close_tabpage(tp, id);
1369}
1370
1371/*
1372 * Close a popup window with Window-id "id" in tabpage "tp".
1373 */
1374 void
1375popup_close_tabpage(tabpage_T *tp, int id)
1376{
1377 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001378 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001379 win_T *prev = NULL;
1380
Bram Moolenaarec583842019-05-26 14:11:23 +02001381 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1382 if (wp->w_id == id)
1383 {
1384 if (prev == NULL)
1385 *root = wp->w_next;
1386 else
1387 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001388 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001389 return;
1390 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001391}
1392
1393 void
1394close_all_popups(void)
1395{
1396 while (first_popupwin != NULL)
1397 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001398 while (curtab->tp_first_popupwin != NULL)
1399 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001400}
1401
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001402/*
1403 * popup_move({id}, {options})
1404 */
1405 void
1406f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1407{
Bram Moolenaarae943152019-06-16 22:54:14 +02001408 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001409 int id = (int)tv_get_number(argvars);
1410 win_T *wp = find_popup_win(id);
1411
1412 if (wp == NULL)
1413 return; // invalid {id}
1414
1415 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1416 {
1417 emsg(_(e_dictreq));
1418 return;
1419 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001420 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001421
Bram Moolenaarae943152019-06-16 22:54:14 +02001422 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001423
1424 if (wp->w_winrow + wp->w_height >= cmdline_row)
1425 clear_cmdline = TRUE;
1426 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001427}
1428
Bram Moolenaarbc133542019-05-29 20:26:46 +02001429/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001430 * popup_setoptions({id}, {options})
1431 */
1432 void
1433f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1434{
1435 dict_T *dict;
1436 int id = (int)tv_get_number(argvars);
1437 win_T *wp = find_popup_win(id);
1438
1439 if (wp == NULL)
1440 return; // invalid {id}
1441
1442 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1443 {
1444 emsg(_(e_dictreq));
1445 return;
1446 }
1447 dict = argvars[1].vval.v_dict;
1448
1449 apply_move_options(wp, dict);
1450 apply_general_options(wp, dict);
1451
Bram Moolenaarad24a712019-06-17 20:05:45 +02001452 popup_mask_refresh = TRUE;
Bram Moolenaarae943152019-06-16 22:54:14 +02001453 popup_adjust_position(wp);
1454}
1455
1456/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001457 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001458 */
1459 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001460f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001461{
1462 dict_T *dict;
1463 int id = (int)tv_get_number(argvars);
1464 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001465 int top_extra;
1466 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001467
1468 if (rettv_dict_alloc(rettv) == OK)
1469 {
1470 if (wp == NULL)
1471 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001472 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001473 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1474
Bram Moolenaarbc133542019-05-29 20:26:46 +02001475 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001476
Bram Moolenaarbc133542019-05-29 20:26:46 +02001477 dict_add_number(dict, "line", wp->w_winrow + 1);
1478 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001479 dict_add_number(dict, "width", wp->w_width + left_extra
1480 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1481 dict_add_number(dict, "height", wp->w_height + top_extra
1482 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001483
1484 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1485 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1486 dict_add_number(dict, "core_width", wp->w_width);
1487 dict_add_number(dict, "core_height", wp->w_height);
1488
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001489 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001490 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001491 }
1492}
1493
1494/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001495 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1496 */
1497 static void
1498get_padding_border(dict_T *dict, int *array, char *name)
1499{
1500 list_T *list;
1501 int i;
1502
1503 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1504 return;
1505
1506 list = list_alloc();
1507 if (list != NULL)
1508 {
1509 dict_add_list(dict, name, list);
1510 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1511 for (i = 0; i < 4; ++i)
1512 list_append_number(list, array[i]);
1513 }
1514}
1515
1516/*
1517 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1518 */
1519 static void
1520get_borderhighlight(dict_T *dict, win_T *wp)
1521{
1522 list_T *list;
1523 int i;
1524
1525 for (i = 0; i < 4; ++i)
1526 if (wp->w_border_highlight[i] != NULL)
1527 break;
1528 if (i == 4)
1529 return;
1530
1531 list = list_alloc();
1532 if (list != NULL)
1533 {
1534 dict_add_list(dict, "borderhighlight", list);
1535 for (i = 0; i < 4; ++i)
1536 list_append_string(list, wp->w_border_highlight[i], -1);
1537 }
1538}
1539
1540/*
1541 * For popup_getoptions(): add a "borderchars" entry to "dict".
1542 */
1543 static void
1544get_borderchars(dict_T *dict, win_T *wp)
1545{
1546 list_T *list;
1547 int i;
1548 char_u buf[NUMBUFLEN];
1549 int len;
1550
1551 for (i = 0; i < 8; ++i)
1552 if (wp->w_border_char[i] != 0)
1553 break;
1554 if (i == 8)
1555 return;
1556
1557 list = list_alloc();
1558 if (list != NULL)
1559 {
1560 dict_add_list(dict, "borderchars", list);
1561 for (i = 0; i < 8; ++i)
1562 {
1563 len = mb_char2bytes(wp->w_border_char[i], buf);
1564 list_append_string(list, buf, len);
1565 }
1566 }
1567}
1568
1569/*
1570 * For popup_getoptions(): add a "moved" entry to "dict".
1571 */
1572 static void
1573get_moved_list(dict_T *dict, win_T *wp)
1574{
1575 list_T *list;
1576
1577 list = list_alloc();
1578 if (list != NULL)
1579 {
1580 dict_add_list(dict, "moved", list);
1581 list_append_number(list, wp->w_popup_mincol);
1582 list_append_number(list, wp->w_popup_maxcol);
1583 }
1584}
1585
1586/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001587 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001588 */
1589 void
1590f_popup_getoptions(typval_T *argvars, typval_T *rettv)
1591{
1592 dict_T *dict;
1593 int id = (int)tv_get_number(argvars);
1594 win_T *wp = find_popup_win(id);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001595 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001596
1597 if (rettv_dict_alloc(rettv) == OK)
1598 {
1599 if (wp == NULL)
1600 return;
1601
1602 dict = rettv->vval.v_dict;
1603 dict_add_number(dict, "line", wp->w_wantline);
1604 dict_add_number(dict, "col", wp->w_wantcol);
1605 dict_add_number(dict, "minwidth", wp->w_minwidth);
1606 dict_add_number(dict, "minheight", wp->w_minheight);
1607 dict_add_number(dict, "maxheight", wp->w_maxheight);
1608 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02001609 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001610 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001611 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02001612 dict_add_string(dict, "title", wp->w_popup_title);
1613 dict_add_number(dict, "wrap", wp->w_p_wrap);
1614 dict_add_number(dict, "drag", wp->w_popup_drag);
1615 dict_add_string(dict, "highlight", wp->w_p_wcr);
1616
1617 get_padding_border(dict, wp->w_popup_padding, "padding");
1618 get_padding_border(dict, wp->w_popup_border, "border");
1619 get_borderhighlight(dict, wp);
1620 get_borderchars(dict, wp);
1621 get_moved_list(dict, wp);
1622
1623 if (wp->w_filter_cb.cb_name != NULL)
1624 dict_add_callback(dict, "filter", &wp->w_filter_cb);
1625 if (wp->w_close_cb.cb_name != NULL)
1626 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001627
1628 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
1629 ++i)
1630 if (wp->w_popup_pos == poppos_entries[i].pp_val)
1631 {
1632 dict_add_string(dict, "pos",
1633 (char_u *)poppos_entries[i].pp_name);
1634 break;
1635 }
1636
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001637# if defined(FEAT_TIMERS)
1638 dict_add_number(dict, "time", wp->w_popup_timer != NULL
1639 ? (long)wp->w_popup_timer->tr_interval : 0L);
1640# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02001641 }
1642}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001643
1644 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001645error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001646{
1647 if (bt_popup(curwin->w_buffer))
1648 {
1649 emsg(_("E994: Not allowed in a popup window"));
1650 return TRUE;
1651 }
1652 return FALSE;
1653}
1654
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001655/*
1656 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001657 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001658 */
1659 void
1660popup_reset_handled()
1661{
1662 win_T *wp;
1663
1664 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1665 wp->w_popup_flags &= ~POPF_HANDLED;
1666 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1667 wp->w_popup_flags &= ~POPF_HANDLED;
1668}
1669
1670/*
1671 * Find the next visible popup where POPF_HANDLED is not set.
1672 * Must have called popup_reset_handled() first.
1673 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
1674 * popup with the highest zindex.
1675 */
1676 win_T *
1677find_next_popup(int lowest)
1678{
1679 win_T *wp;
1680 win_T *found_wp;
1681 int found_zindex;
1682
1683 found_zindex = lowest ? INT_MAX : 0;
1684 found_wp = NULL;
1685 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1686 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1687 && (lowest ? wp->w_zindex < found_zindex
1688 : wp->w_zindex > found_zindex))
1689 {
1690 found_zindex = wp->w_zindex;
1691 found_wp = wp;
1692 }
1693 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1694 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
1695 && (lowest ? wp->w_zindex < found_zindex
1696 : wp->w_zindex > found_zindex))
1697 {
1698 found_zindex = wp->w_zindex;
1699 found_wp = wp;
1700 }
1701
1702 if (found_wp != NULL)
1703 found_wp->w_popup_flags |= POPF_HANDLED;
1704 return found_wp;
1705}
1706
1707/*
1708 * Invoke the filter callback for window "wp" with typed character "c".
1709 * Uses the global "mod_mask" for modifiers.
1710 * Returns the return value of the filter.
1711 * Careful: The filter may make "wp" invalid!
1712 */
1713 static int
1714invoke_popup_filter(win_T *wp, int c)
1715{
1716 int res;
1717 typval_T rettv;
1718 int dummy;
1719 typval_T argv[3];
1720 char_u buf[NUMBUFLEN];
1721
Bram Moolenaara42d9452019-06-15 21:46:30 +02001722 // Emergency exit: CTRL-C closes the popup.
1723 if (c == Ctrl_C)
1724 {
1725 rettv.v_type = VAR_NUMBER;
1726 rettv.vval.v_number = -1;
1727 popup_close_and_callback(wp, &rettv);
1728 return 1;
1729 }
1730
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001731 argv[0].v_type = VAR_NUMBER;
1732 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1733
1734 // Convert the number to a string, so that the function can use:
1735 // if a:c == "\<F2>"
1736 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
1737 argv[1].v_type = VAR_STRING;
1738 argv[1].vval.v_string = vim_strsave(buf);
1739
1740 argv[2].v_type = VAR_UNKNOWN;
1741
Bram Moolenaara42d9452019-06-15 21:46:30 +02001742 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001743 call_callback(&wp->w_filter_cb, -1,
1744 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1745 res = tv_get_number(&rettv);
1746 vim_free(argv[1].vval.v_string);
1747 clear_tv(&rettv);
1748 return res;
1749}
1750
1751/*
1752 * Called when "c" was typed: invoke popup filter callbacks.
1753 * Returns TRUE when the character was consumed,
1754 */
1755 int
1756popup_do_filter(int c)
1757{
1758 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001759 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001760
1761 popup_reset_handled();
1762
1763 while (!res && (wp = find_next_popup(FALSE)) != NULL)
1764 if (wp->w_filter_cb.cb_name != NULL)
1765 res = invoke_popup_filter(wp, c);
1766
1767 return res;
1768}
1769
Bram Moolenaar3397f742019-06-02 18:40:06 +02001770/*
1771 * Called when the cursor moved: check if any popup needs to be closed if the
1772 * cursor moved far enough.
1773 */
1774 void
1775popup_check_cursor_pos()
1776{
1777 win_T *wp;
1778 typval_T tv;
1779
1780 popup_reset_handled();
1781 while ((wp = find_next_popup(TRUE)) != NULL)
1782 if (wp->w_popup_curwin != NULL
1783 && (curwin != wp->w_popup_curwin
1784 || curwin->w_cursor.lnum != wp->w_popup_lnum
1785 || curwin->w_cursor.col < wp->w_popup_mincol
1786 || curwin->w_cursor.col > wp->w_popup_maxcol))
1787 {
1788 tv.v_type = VAR_NUMBER;
1789 tv.vval.v_number = -1;
1790 popup_close_and_callback(wp, &tv);
1791 }
1792}
1793
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001794/*
1795 * Update "popup_mask" if needed.
1796 * Also recomputes the popup size and positions.
1797 * Also updates "popup_visible".
1798 * Also marks window lines for redrawing.
1799 */
1800 void
1801may_update_popup_mask(int type)
1802{
1803 win_T *wp;
1804 short *mask;
1805 int line, col;
1806 int redraw_all = FALSE;
1807
1808 // Need to recompute when switching tabs.
1809 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1810 // (such as the screen size) must have changed.
1811 if (popup_mask_tab != curtab || type >= NOT_VALID)
1812 {
1813 popup_mask_refresh = TRUE;
1814 redraw_all = TRUE;
1815 }
1816 if (!popup_mask_refresh)
1817 {
1818 // Check if any buffer has changed.
1819 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1820 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1821 popup_mask_refresh = TRUE;
1822 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1823 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1824 popup_mask_refresh = TRUE;
1825 if (!popup_mask_refresh)
1826 return;
1827 }
1828
1829 // Need to update the mask, something has changed.
1830 popup_mask_refresh = FALSE;
1831 popup_mask_tab = curtab;
1832 popup_visible = FALSE;
1833
1834 // If redrawing everything, just update "popup_mask".
1835 // If redrawing only what is needed, update "popup_mask_next" and then
1836 // compare with "popup_mask" to see what changed.
1837 if (type >= SOME_VALID)
1838 mask = popup_mask;
1839 else
1840 mask = popup_mask_next;
1841 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
1842
1843 // Find the window with the lowest zindex that hasn't been handled yet,
1844 // so that the window with a higher zindex overwrites the value in
1845 // popup_mask.
1846 popup_reset_handled();
1847 while ((wp = find_next_popup(TRUE)) != NULL)
1848 {
1849 popup_visible = TRUE;
1850
1851 // Recompute the position if the text changed.
1852 if (redraw_all
1853 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1854 popup_adjust_position(wp);
1855
1856 for (line = wp->w_winrow;
1857 line < wp->w_winrow + popup_height(wp)
1858 && line < screen_Rows; ++line)
1859 for (col = wp->w_wincol;
1860 col < wp->w_wincol + popup_width(wp)
1861 && col < screen_Columns; ++col)
1862 mask[line * screen_Columns + col] = wp->w_zindex;
1863 }
1864
1865 // Only check which lines are to be updated if not already
1866 // updating all lines.
1867 if (mask == popup_mask_next)
1868 for (line = 0; line < screen_Rows; ++line)
1869 {
1870 int col_done = 0;
1871
1872 for (col = 0; col < screen_Columns; ++col)
1873 {
1874 int off = line * screen_Columns + col;
1875
1876 if (popup_mask[off] != popup_mask_next[off])
1877 {
1878 popup_mask[off] = popup_mask_next[off];
1879
1880 if (line >= cmdline_row)
1881 {
1882 // the command line needs to be cleared if text below
1883 // the popup is now visible.
1884 if (!msg_scrolled && popup_mask_next[off] == 0)
1885 clear_cmdline = TRUE;
1886 }
1887 else if (col >= col_done)
1888 {
1889 linenr_T lnum;
1890 int line_cp = line;
1891 int col_cp = col;
1892
1893 // The screen position "line" / "col" needs to be
1894 // redrawn. Figure out what window that is and update
1895 // w_redraw_top and w_redr_bot. Only needs to be done
1896 // once for each window line.
1897 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
1898 if (wp != NULL)
1899 {
1900 if (line_cp >= wp->w_height)
1901 // In (or below) status line
1902 wp->w_redr_status = TRUE;
1903 // compute the position in the buffer line from the
1904 // position on the screen
1905 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1906 &lnum))
1907 // past bottom
1908 wp->w_redr_status = TRUE;
1909 else
1910 redrawWinline(wp, lnum);
1911
1912 // This line is going to be redrawn, no need to
1913 // check until the right side of the window.
1914 col_done = wp->w_wincol + wp->w_width - 1;
1915 }
1916 }
1917 }
1918 }
1919 }
1920}
1921
1922/*
1923 * Return a string of "len" spaces in IObuff.
1924 */
1925 static char_u *
1926get_spaces(int len)
1927{
1928 vim_memset(IObuff, ' ', (size_t)len);
1929 IObuff[len] = NUL;
1930 return IObuff;
1931}
1932
1933/*
1934 * Update popup windows. They are drawn on top of normal windows.
1935 * "win_update" is called for each popup window, lowest zindex first.
1936 */
1937 void
1938update_popups(void (*win_update)(win_T *wp))
1939{
1940 win_T *wp;
1941 int top_off;
1942 int left_off;
1943 int total_width;
1944 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001945 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001946 int popup_attr;
1947 int border_attr[4];
1948 int border_char[8];
1949 char_u buf[MB_MAXBYTES];
1950 int row;
1951 int i;
1952
1953 // Find the window with the lowest zindex that hasn't been updated yet,
1954 // so that the window with a higher zindex is drawn later, thus goes on
1955 // top.
1956 popup_reset_handled();
1957 while ((wp = find_next_popup(TRUE)) != NULL)
1958 {
1959 // This drawing uses the zindex of the popup window, so that it's on
1960 // top of the text but doesn't draw when another popup with higher
1961 // zindex is on top of the character.
1962 screen_zindex = wp->w_zindex;
1963
1964 // adjust w_winrow and w_wincol for border and padding, since
1965 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001966 top_off = popup_top_extra(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001967 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1968 wp->w_winrow += top_off;
1969 wp->w_wincol += left_off;
1970
1971 // Draw the popup text.
1972 win_update(wp);
1973
1974 wp->w_winrow -= top_off;
1975 wp->w_wincol -= left_off;
1976
1977 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1978 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001979 total_height = popup_top_extra(wp)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001980 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1981 popup_attr = get_wcr_attr(wp);
1982
1983 // We can only use these line drawing characters when 'encoding' is
1984 // "utf-8" and 'ambiwidth' is "single".
1985 if (enc_utf8 && *p_ambw == 's')
1986 {
1987 border_char[0] = border_char[2] = 0x2550;
1988 border_char[1] = border_char[3] = 0x2551;
1989 border_char[4] = 0x2554;
1990 border_char[5] = 0x2557;
1991 border_char[6] = 0x255d;
1992 border_char[7] = 0x255a;
1993 }
1994 else
1995 {
1996 border_char[0] = border_char[2] = '-';
1997 border_char[1] = border_char[3] = '|';
1998 for (i = 4; i < 8; ++i)
1999 border_char[i] = '+';
2000 }
2001 for (i = 0; i < 8; ++i)
2002 if (wp->w_border_char[i] != 0)
2003 border_char[i] = wp->w_border_char[i];
2004
2005 for (i = 0; i < 4; ++i)
2006 {
2007 border_attr[i] = popup_attr;
2008 if (wp->w_border_highlight[i] != NULL)
2009 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2010 }
2011
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002012 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002013 if (wp->w_popup_border[0] > 0)
2014 {
2015 // top border
2016 screen_fill(wp->w_winrow, wp->w_winrow + 1,
2017 wp->w_wincol,
2018 wp->w_wincol + total_width,
2019 wp->w_popup_border[3] != 0
2020 ? border_char[4] : border_char[0],
2021 border_char[0], border_attr[0]);
2022 if (wp->w_popup_border[1] > 0)
2023 {
2024 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2025 screen_puts(buf, wp->w_winrow,
2026 wp->w_wincol + total_width - 1, border_attr[1]);
2027 }
2028 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002029 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2030 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002031
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002032 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002033 {
2034 // top padding
2035 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002036 screen_fill(row, row + top_padding,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002037 wp->w_wincol + wp->w_popup_border[3],
2038 wp->w_wincol + total_width - wp->w_popup_border[1],
2039 ' ', ' ', popup_attr);
2040 }
2041
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002042 // Title goes on top of border or padding.
2043 if (wp->w_popup_title != NULL)
2044 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2045 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2046
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002047 for (row = wp->w_winrow + wp->w_popup_border[0];
2048 row < wp->w_winrow + total_height - wp->w_popup_border[2];
2049 ++row)
2050 {
2051 // left border
2052 if (wp->w_popup_border[3] > 0)
2053 {
2054 buf[mb_char2bytes(border_char[3], buf)] = NUL;
2055 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
2056 }
2057 // left padding
2058 if (wp->w_popup_padding[3] > 0)
2059 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
2060 wp->w_wincol + wp->w_popup_border[3], popup_attr);
2061 // right border
2062 if (wp->w_popup_border[1] > 0)
2063 {
2064 buf[mb_char2bytes(border_char[1], buf)] = NUL;
2065 screen_puts(buf, row,
2066 wp->w_wincol + total_width - 1, border_attr[1]);
2067 }
2068 // right padding
2069 if (wp->w_popup_padding[1] > 0)
2070 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
2071 wp->w_wincol + wp->w_popup_border[3]
2072 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
2073 }
2074
2075 if (wp->w_popup_padding[2] > 0)
2076 {
2077 // bottom padding
2078 row = wp->w_winrow + wp->w_popup_border[0]
2079 + wp->w_popup_padding[0] + wp->w_height;
2080 screen_fill(row, row + wp->w_popup_padding[2],
2081 wp->w_wincol + wp->w_popup_border[3],
2082 wp->w_wincol + total_width - wp->w_popup_border[1],
2083 ' ', ' ', popup_attr);
2084 }
2085
2086 if (wp->w_popup_border[2] > 0)
2087 {
2088 // bottom border
2089 row = wp->w_winrow + total_height - 1;
2090 screen_fill(row , row + 1,
2091 wp->w_wincol,
2092 wp->w_wincol + total_width,
2093 wp->w_popup_border[3] != 0
2094 ? border_char[7] : border_char[2],
2095 border_char[2], border_attr[2]);
2096 if (wp->w_popup_border[1] > 0)
2097 {
2098 buf[mb_char2bytes(border_char[6], buf)] = NUL;
2099 screen_puts(buf, row,
2100 wp->w_wincol + total_width - 1, border_attr[2]);
2101 }
2102 }
2103
2104 // Back to the normal zindex.
2105 screen_zindex = 0;
2106 }
2107}
2108
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002109#endif // FEAT_TEXT_PROP