blob: b8fc8fdf219edf5017972745d61d22df6e6d9b89 [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
Bram Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
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 Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
36 */
37 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020038popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020039{
40 dictitem_T *di;
41 char_u *val;
42 char_u *s;
43 char_u *endp;
44 int n = 0;
45
46 di = dict_find(dict, key, -1);
47 if (di == NULL)
48 return 0;
49
50 val = tv_get_string(&di->di_tv);
51 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020052 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020053
54 setcursor_mayforce(TRUE);
55 s = val + 6;
56 if (*s != NUL)
57 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020058 endp = s;
59 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
60 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020061 if (endp != NULL && *skipwhite(endp) != NUL)
62 {
63 semsg(_(e_invexpr2), val);
64 return 0;
65 }
66 }
67
68 if (STRCMP(key, "line") == 0)
69 n = screen_screenrow() + 1 + n;
70 else // "col"
71 n = screen_screencol() + 1 + n;
72
73 if (n < 1)
74 n = 1;
75 return n;
76}
77
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020078 static void
79get_pos_options(win_T *wp, dict_T *dict)
80{
81 char_u *str;
82 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020083 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020084
85 nr = popup_options_one(dict, (char_u *)"line");
86 if (nr > 0)
87 wp->w_wantline = nr;
88 nr = popup_options_one(dict, (char_u *)"col");
89 if (nr > 0)
90 wp->w_wantcol = nr;
91
Bram Moolenaar711d02c2019-06-28 04:06:50 +020092 di = dict_find(dict, (char_u *)"fixed", -1);
93 if (di != NULL)
94 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020095
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020096 str = dict_get_string(dict, (char_u *)"pos", FALSE);
97 if (str != NULL)
98 {
99 for (nr = 0;
100 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
101 ++nr)
102 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
103 {
104 wp->w_popup_pos = poppos_entries[nr].pp_val;
105 nr = -1;
106 break;
107 }
108 if (nr != -1)
109 semsg(_(e_invarg2), str);
110 }
111}
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200114set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200115{
116 dictitem_T *di;
117
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200118 di = dict_find(dict, (char_u *)name, -1);
119 if (di != NULL)
120 {
121 if (di->di_tv.v_type != VAR_LIST)
122 emsg(_(e_listreq));
123 else
124 {
125 list_T *list = di->di_tv.vval.v_list;
126 listitem_T *li;
127 int i;
128 int nr;
129
130 for (i = 0; i < 4; ++i)
131 array[i] = 1;
132 if (list != NULL)
133 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
134 ++i, li = li->li_next)
135 {
136 nr = (int)tv_get_number(&li->li_tv);
137 if (nr >= 0)
138 array[i] = nr > max_val ? max_val : nr;
139 }
140 }
141 }
142}
143
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200144/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200145 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200146 */
147 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200148set_moved_values(win_T *wp)
149{
150 wp->w_popup_curwin = curwin;
151 wp->w_popup_lnum = curwin->w_cursor.lnum;
152 wp->w_popup_mincol = curwin->w_cursor.col;
153 wp->w_popup_maxcol = curwin->w_cursor.col;
154}
155
156/*
157 * Used when popup options contain "moved" with "word" or "WORD".
158 */
159 static void
160set_moved_columns(win_T *wp, int flags)
161{
162 char_u *ptr;
163 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
164
165 if (len > 0)
166 {
167 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
168 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
169 }
170}
171
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200172/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200173 * Used when popup options contain "mousemoved": set default moved values.
174 */
175 static void
176set_mousemoved_values(win_T *wp)
177{
178 wp->w_popup_mouse_row = mouse_row;
179 wp->w_popup_mouse_mincol = mouse_col;
180 wp->w_popup_mouse_maxcol = mouse_col;
181}
182
183/*
184 * Used when popup options contain "moved" with "word" or "WORD".
185 */
186 static void
187set_mousemoved_columns(win_T *wp, int flags)
188{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200189 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200190 char_u *text;
191 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200192 pos_T pos;
193 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200194
195 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200197 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200198 // convert text column to mouse column
199 pos.col = col;
200 pos.coladd = 0;
201 getvcol(textwp, &pos, &mcol, NULL, NULL);
202 wp->w_popup_mouse_mincol = mcol;
203
Bram Moolenaar10727682019-07-12 13:59:20 +0200204 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200205 getvcol(textwp, &pos, NULL, NULL, &mcol);
206 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200207 vim_free(text);
208 }
209}
210
211/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200212 * Return TRUE if "row"/"col" is on the border of the popup.
213 * The values are relative to the top-left corner.
214 */
215 int
216popup_on_border(win_T *wp, int row, int col)
217{
218 return (row == 0 && wp->w_popup_border[0] > 0)
219 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
220 || (col == 0 && wp->w_popup_border[3] > 0)
221 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
222}
223
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200224/*
225 * Return TRUE if "row"/"col" is on the "X" button of the popup.
226 * The values are relative to the top-left corner.
227 * Caller should check w_popup_close is POPCLOSE_BUTTON.
228 */
229 int
230popup_on_X_button(win_T *wp, int row, int col)
231{
232 return row == 0 && col == popup_width(wp) - 1;
233}
234
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200235// Values set when dragging a popup window starts.
236static int drag_start_row;
237static int drag_start_col;
238static int drag_start_wantline;
239static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200240static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200241
242/*
243 * Mouse down on border of popup window: start dragging it.
244 * Uses mouse_col and mouse_row.
245 */
246 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200247popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200248{
249 drag_start_row = mouse_row;
250 drag_start_col = mouse_col;
251 // TODO: handle using different corner
252 if (wp->w_wantline == 0)
253 drag_start_wantline = wp->w_winrow + 1;
254 else
255 drag_start_wantline = wp->w_wantline;
256 if (wp->w_wantcol == 0)
257 drag_start_wantcol = wp->w_wincol + 1;
258 else
259 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200260
261 // Stop centering the popup
262 if (wp->w_popup_pos == POPPOS_CENTER)
263 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200264
265 drag_on_resize_handle = wp->w_popup_border[1] > 0
266 && wp->w_popup_border[2] > 0
267 && row == popup_height(wp) - 1
268 && col == popup_width(wp) - 1;
269
270 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
271 {
272 if (wp->w_popup_pos == POPPOS_TOPRIGHT
273 || wp->w_popup_pos == POPPOS_BOTRIGHT)
274 wp->w_wantcol = wp->w_wincol + 1;
275 if (wp->w_popup_pos == POPPOS_BOTLEFT)
276 wp->w_wantline = wp->w_winrow + 1;
277 wp->w_popup_pos = POPPOS_TOPLEFT;
278 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200279}
280
281/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200282 * Mouse moved while dragging a popup window: adjust the window popup position
283 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200284 */
285 void
286popup_drag(win_T *wp)
287{
288 // The popup may be closed before dragging stops.
289 if (!win_valid_popup(wp))
290 return;
291
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200292 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
293 {
294 int width_inc = mouse_col - drag_start_col;
295 int height_inc = mouse_row - drag_start_row;
296
297 if (width_inc != 0)
298 {
299 int width = wp->w_width + width_inc;
300
301 if (width < 1)
302 width = 1;
303 wp->w_minwidth = width;
304 wp->w_maxwidth = width;
305 drag_start_col = mouse_col;
306 }
307
308 if (height_inc != 0)
309 {
310 int height = wp->w_height + height_inc;
311
312 if (height < 1)
313 height = 1;
314 wp->w_minheight = height;
315 wp->w_maxheight = height;
316 drag_start_row = mouse_row;
317 }
318
319 popup_adjust_position(wp);
320 return;
321 }
322
323 if (!(wp->w_popup_flags & POPF_DRAG))
324 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200325 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
326 if (wp->w_wantline < 1)
327 wp->w_wantline = 1;
328 if (wp->w_wantline > Rows)
329 wp->w_wantline = Rows;
330 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
331 if (wp->w_wantcol < 1)
332 wp->w_wantcol = 1;
333 if (wp->w_wantcol > Columns)
334 wp->w_wantcol = Columns;
335
336 popup_adjust_position(wp);
337}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200338
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200339/*
340 * Set w_firstline to match the current "wp->w_topline".
341 */
342 void
343popup_set_firstline(win_T *wp)
344{
345 int height = wp->w_height;
346
347 wp->w_firstline = wp->w_topline;
348 popup_adjust_position(wp);
349
350 // we don't want the popup to get smaller, decrement the first line
351 // until it doesn't
352 while (wp->w_firstline > 1 && wp->w_height < height)
353 {
354 --wp->w_firstline;
355 popup_adjust_position(wp);
356 }
357}
358
359/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200360 * Return TRUE if the position is in the popup window scrollbar.
361 */
362 int
363popup_is_in_scrollbar(win_T *wp, int row, int col)
364{
365 return wp->w_has_scrollbar
366 && row >= wp->w_popup_border[0]
367 && row < popup_height(wp) - wp->w_popup_border[2]
368 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
369}
370
371
372/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200373 * Handle a click in a popup window, if it is in the scrollbar.
374 */
375 void
376popup_handle_scrollbar_click(win_T *wp, int row, int col)
377{
378 int height = popup_height(wp);
379 int old_topline = wp->w_topline;
380
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200381 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200382 {
383 if (row >= height / 2)
384 {
385 // Click in lower half, scroll down.
386 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
387 ++wp->w_topline;
388 }
389 else if (wp->w_topline > 1)
390 // click on upper half, scroll up.
391 --wp->w_topline;
392 if (wp->w_topline != old_topline)
393 {
394 popup_set_firstline(wp);
395 redraw_win_later(wp, NOT_VALID);
396 }
397 }
398}
399
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200400#if defined(FEAT_TIMERS)
401 static void
402popup_add_timeout(win_T *wp, int time)
403{
404 char_u cbbuf[50];
405 char_u *ptr = cbbuf;
406 typval_T tv;
407
408 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
409 "{_ -> popup_close(%d)}", wp->w_id);
410 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
411 {
412 wp->w_popup_timer = create_timer(time, 0);
413 wp->w_popup_timer->tr_callback = get_callback(&tv);
414 clear_tv(&tv);
415 }
416}
417#endif
418
Bram Moolenaar17627312019-06-02 19:53:44 +0200419/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200420 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200421 */
422 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200423apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200424{
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 int nr;
426
427 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
428 wp->w_minwidth = nr;
429 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
430 wp->w_minheight = nr;
431 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
432 wp->w_maxwidth = nr;
433 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
434 wp->w_maxheight = nr;
435 get_pos_options(wp, d);
436}
437
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200438 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200439handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
440{
441 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
442 {
443 char_u *s = di->di_tv.vval.v_string;
444 int flags = 0;
445
446 if (STRCMP(s, "word") == 0)
447 flags = FIND_IDENT | FIND_STRING;
448 else if (STRCMP(s, "WORD") == 0)
449 flags = FIND_STRING;
450 else if (STRCMP(s, "expr") == 0)
451 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
452 else if (STRCMP(s, "any") != 0)
453 semsg(_(e_invarg2), s);
454 if (flags != 0)
455 {
456 if (mousemoved)
457 set_mousemoved_columns(wp, flags);
458 else
459 set_moved_columns(wp, flags);
460 }
461 }
462 else if (di->di_tv.v_type == VAR_LIST
463 && di->di_tv.vval.v_list != NULL
464 && di->di_tv.vval.v_list->lv_len == 2)
465 {
466 list_T *l = di->di_tv.vval.v_list;
467 int mincol = tv_get_number(&l->lv_first->li_tv);
468 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
469
470 if (mousemoved)
471 {
472 wp->w_popup_mouse_mincol = mincol;
473 wp->w_popup_mouse_maxcol = maxcol;
474 }
475 else
476 {
477 wp->w_popup_mincol = mincol;
478 wp->w_popup_maxcol = maxcol;
479 }
480 }
481 else
482 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
483}
484
485 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200486check_highlight(dict_T *dict, char *name, char_u **pval)
487{
488 dictitem_T *di;
489 char_u *str;
490
491 di = dict_find(dict, (char_u *)name, -1);
492 if (di != NULL)
493 {
494 if (di->di_tv.v_type != VAR_STRING)
495 semsg(_(e_invargval), name);
496 else
497 {
498 str = tv_get_string(&di->di_tv);
499 if (*str != NUL)
500 *pval = vim_strsave(str);
501 }
502 }
503}
504
Bram Moolenaarae943152019-06-16 22:54:14 +0200505/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200506 * Scroll to show the line with the cursor. This assumes lines don't wrap.
507 */
508 static void
509popup_show_curline(win_T *wp)
510{
511 if (wp->w_cursor.lnum < wp->w_topline)
512 wp->w_topline = wp->w_cursor.lnum;
513 else if (wp->w_cursor.lnum >= wp->w_botline)
514 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200515
516 // Don't use "firstline" now.
517 wp->w_firstline = 0;
518}
519
520/*
521 * Get the sign group name for window "wp".
522 * Returns a pointer to a static buffer, overwritten on the next call.
523 */
524 static char_u *
525popup_get_sign_name(win_T *wp)
526{
527 static char buf[30];
528
529 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
530 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200531}
532
533/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200534 * Highlight the line with the cursor.
535 * Also scrolls the text to put the cursor line in view.
536 */
537 static void
538popup_highlight_curline(win_T *wp)
539{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200540 int sign_id = 0;
541 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200542
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200543 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200544
545 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
546 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200547 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200548
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200549 if (!sign_exists_by_name(sign_name))
550 {
551 char *linehl = "PopupSelected";
552
553 if (syn_name2id((char_u *)linehl) == 0)
554 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200555 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200556 }
557
558 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
559 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
560 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200561 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200562 else
563 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200564}
565
566/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200567 * Shared between popup_create() and f_popup_setoptions().
568 */
569 static void
570apply_general_options(win_T *wp, dict_T *dict)
571{
572 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200573 int nr;
574 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200575
Bram Moolenaarae943152019-06-16 22:54:14 +0200576 // TODO: flip
577
578 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200579 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200580 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
581 if (wp->w_firstline < 1)
582 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200583
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200584 di = dict_find(dict, (char_u *)"scrollbar", -1);
585 if (di != NULL)
586 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
587
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200588 str = dict_get_string(dict, (char_u *)"title", FALSE);
589 if (str != NULL)
590 {
591 vim_free(wp->w_popup_title);
592 wp->w_popup_title = vim_strsave(str);
593 }
594
Bram Moolenaar402502d2019-05-30 22:07:36 +0200595 di = dict_find(dict, (char_u *)"wrap", -1);
596 if (di != NULL)
597 {
598 nr = dict_get_number(dict, (char_u *)"wrap");
599 wp->w_p_wrap = nr != 0;
600 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200601
Bram Moolenaara42d9452019-06-15 21:46:30 +0200602 di = dict_find(dict, (char_u *)"drag", -1);
603 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200604 {
605 nr = dict_get_number(dict, (char_u *)"drag");
606 if (nr)
607 wp->w_popup_flags |= POPF_DRAG;
608 else
609 wp->w_popup_flags &= ~POPF_DRAG;
610 }
611
612 di = dict_find(dict, (char_u *)"resize", -1);
613 if (di != NULL)
614 {
615 nr = dict_get_number(dict, (char_u *)"resize");
616 if (nr)
617 wp->w_popup_flags |= POPF_RESIZE;
618 else
619 wp->w_popup_flags &= ~POPF_RESIZE;
620 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200621
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200622 di = dict_find(dict, (char_u *)"close", -1);
623 if (di != NULL)
624 {
625 int ok = TRUE;
626
627 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
628 {
629 char_u *s = di->di_tv.vval.v_string;
630
631 if (STRCMP(s, "none") == 0)
632 wp->w_popup_close = POPCLOSE_NONE;
633 else if (STRCMP(s, "button") == 0)
634 wp->w_popup_close = POPCLOSE_BUTTON;
635 else if (STRCMP(s, "click") == 0)
636 wp->w_popup_close = POPCLOSE_CLICK;
637 else
638 ok = FALSE;
639 }
640 else
641 ok = FALSE;
642 if (!ok)
643 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
644 }
645
Bram Moolenaarae943152019-06-16 22:54:14 +0200646 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
647 if (str != NULL)
648 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
649 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200650
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200651 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
652 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200653 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
654 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200655
Bram Moolenaar790498b2019-06-01 22:15:29 +0200656 di = dict_find(dict, (char_u *)"borderhighlight", -1);
657 if (di != NULL)
658 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200659 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200660 emsg(_(e_listreq));
661 else
662 {
663 list_T *list = di->di_tv.vval.v_list;
664 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200665 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200666
Bram Moolenaar403d0902019-07-17 21:37:32 +0200667 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
668 ++i, li = li->li_next)
669 {
670 str = tv_get_string(&li->li_tv);
671 if (*str != NUL)
672 wp->w_border_highlight[i] = vim_strsave(str);
673 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200674 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
675 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200676 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200677 vim_strsave(wp->w_border_highlight[0]);
678 }
679 }
680
Bram Moolenaar790498b2019-06-01 22:15:29 +0200681 di = dict_find(dict, (char_u *)"borderchars", -1);
682 if (di != NULL)
683 {
684 if (di->di_tv.v_type != VAR_LIST)
685 emsg(_(e_listreq));
686 else
687 {
688 list_T *list = di->di_tv.vval.v_list;
689 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200690 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200691
692 if (list != NULL)
693 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
694 ++i, li = li->li_next)
695 {
696 str = tv_get_string(&li->li_tv);
697 if (*str != NUL)
698 wp->w_border_char[i] = mb_ptr2char(str);
699 }
700 if (list->lv_len == 1)
701 for (i = 1; i < 8; ++i)
702 wp->w_border_char[i] = wp->w_border_char[0];
703 if (list->lv_len == 2)
704 {
705 for (i = 4; i < 8; ++i)
706 wp->w_border_char[i] = wp->w_border_char[1];
707 for (i = 1; i < 4; ++i)
708 wp->w_border_char[i] = wp->w_border_char[0];
709 }
710 }
711 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200712
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200713 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
714 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
715
Bram Moolenaarae943152019-06-16 22:54:14 +0200716 di = dict_find(dict, (char_u *)"zindex", -1);
717 if (di != NULL)
718 {
719 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
720 if (wp->w_zindex < 1)
721 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
722 if (wp->w_zindex > 32000)
723 wp->w_zindex = 32000;
724 }
725
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200726 di = dict_find(dict, (char_u *)"mask", -1);
727 if (di != NULL)
728 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200729 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200730
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200731 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200732 {
733 listitem_T *li;
734
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200735 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200736 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
737 li = li->li_next)
738 {
739 if (li->li_tv.v_type != VAR_LIST
740 || li->li_tv.vval.v_list == NULL
741 || li->li_tv.vval.v_list->lv_len != 4)
742 {
743 ok = FALSE;
744 break;
745 }
746 }
747 }
748 if (ok)
749 {
750 wp->w_popup_mask = di->di_tv.vval.v_list;
751 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200752 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200753 }
754 else
755 semsg(_(e_invargval), "mask");
756 }
757
Bram Moolenaarae943152019-06-16 22:54:14 +0200758#if defined(FEAT_TIMERS)
759 // Add timer to close the popup after some time.
760 nr = dict_get_number(dict, (char_u *)"time");
761 if (nr > 0)
762 popup_add_timeout(wp, nr);
763#endif
764
Bram Moolenaar3397f742019-06-02 18:40:06 +0200765 di = dict_find(dict, (char_u *)"moved", -1);
766 if (di != NULL)
767 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200768 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200769 handle_moved_argument(wp, di, FALSE);
770 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200771
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200772 di = dict_find(dict, (char_u *)"mousemoved", -1);
773 if (di != NULL)
774 {
775 set_mousemoved_values(wp);
776 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200777 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200778
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200779 di = dict_find(dict, (char_u *)"cursorline", -1);
780 if (di != NULL)
781 {
782 if (di->di_tv.v_type == VAR_NUMBER)
783 {
784 if (di->di_tv.vval.v_number != 0)
785 wp->w_popup_flags |= POPF_CURSORLINE;
786 else
787 wp->w_popup_flags &= ~POPF_CURSORLINE;
788 }
789 else
790 semsg(_(e_invargval), "cursorline");
791 }
792
Bram Moolenaarae943152019-06-16 22:54:14 +0200793 di = dict_find(dict, (char_u *)"filter", -1);
794 if (di != NULL)
795 {
796 callback_T callback = get_callback(&di->di_tv);
797
798 if (callback.cb_name != NULL)
799 {
800 free_callback(&wp->w_filter_cb);
801 set_callback(&wp->w_filter_cb, &callback);
802 }
803 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200804 di = dict_find(dict, (char_u *)"mapping", -1);
805 if (di != NULL)
806 {
807 nr = dict_get_number(dict, (char_u *)"mapping");
808 if (nr)
809 wp->w_popup_flags |= POPF_MAPPING;
810 else
811 wp->w_popup_flags &= ~POPF_MAPPING;
812 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200813
814 di = dict_find(dict, (char_u *)"callback", -1);
815 if (di != NULL)
816 {
817 callback_T callback = get_callback(&di->di_tv);
818
819 if (callback.cb_name != NULL)
820 {
821 free_callback(&wp->w_close_cb);
822 set_callback(&wp->w_close_cb, &callback);
823 }
824 }
825}
826
827/*
828 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200829 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200830 */
831 static void
832apply_options(win_T *wp, dict_T *dict)
833{
834 int nr;
835
836 apply_move_options(wp, dict);
837 apply_general_options(wp, dict);
838
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200839 nr = dict_get_number(dict, (char_u *)"hidden");
840 if (nr > 0)
841 {
842 wp->w_popup_flags |= POPF_HIDDEN;
843 --wp->w_buffer->b_nwindows;
844 }
845
Bram Moolenaar33796b32019-06-08 16:01:13 +0200846 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200847 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200848}
849
850/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200851 * Add lines to the popup from a list of strings.
852 */
853 static void
854add_popup_strings(buf_T *buf, list_T *l)
855{
856 listitem_T *li;
857 linenr_T lnum = 0;
858 char_u *p;
859
860 for (li = l->lv_first; li != NULL; li = li->li_next)
861 if (li->li_tv.v_type == VAR_STRING)
862 {
863 p = li->li_tv.vval.v_string;
864 ml_append_buf(buf, lnum++,
865 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
866 }
867}
868
869/*
870 * Add lines to the popup from a list of dictionaries.
871 */
872 static void
873add_popup_dicts(buf_T *buf, list_T *l)
874{
875 listitem_T *li;
876 listitem_T *pli;
877 linenr_T lnum = 0;
878 char_u *p;
879 dict_T *dict;
880
881 // first add the text lines
882 for (li = l->lv_first; li != NULL; li = li->li_next)
883 {
884 if (li->li_tv.v_type != VAR_DICT)
885 {
886 emsg(_(e_dictreq));
887 return;
888 }
889 dict = li->li_tv.vval.v_dict;
890 p = dict == NULL ? NULL
891 : dict_get_string(dict, (char_u *)"text", FALSE);
892 ml_append_buf(buf, lnum++,
893 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
894 }
895
896 // add the text properties
897 lnum = 1;
898 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
899 {
900 dictitem_T *di;
901 list_T *plist;
902
903 dict = li->li_tv.vval.v_dict;
904 di = dict_find(dict, (char_u *)"props", -1);
905 if (di != NULL)
906 {
907 if (di->di_tv.v_type != VAR_LIST)
908 {
909 emsg(_(e_listreq));
910 return;
911 }
912 plist = di->di_tv.vval.v_list;
913 if (plist != NULL)
914 {
915 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
916 {
917 if (pli->li_tv.v_type != VAR_DICT)
918 {
919 emsg(_(e_dictreq));
920 return;
921 }
922 dict = pli->li_tv.vval.v_dict;
923 if (dict != NULL)
924 {
925 int col = dict_get_number(dict, (char_u *)"col");
926
927 prop_add_common( lnum, col, dict, buf, NULL);
928 }
929 }
930 }
931 }
932 }
933}
934
935/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200936 * Get the padding plus border at the top, adjusted to 1 if there is a title.
937 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +0200938 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200939popup_top_extra(win_T *wp)
940{
941 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
942
943 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
944 return 1;
945 return extra;
946}
947
948/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200949 * Return the height of popup window "wp", including border and padding.
950 */
951 int
952popup_height(win_T *wp)
953{
954 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200955 + popup_top_extra(wp)
956 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +0200957}
958
959/*
960 * Return the width of popup window "wp", including border, padding and
961 * scrollbar.
962 */
963 int
964popup_width(win_T *wp)
965{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200966 // w_leftcol is how many columns of the core are left of the screen
967 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200968 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200969 + popup_extra_width(wp)
970 + wp->w_popup_rightoff;
971}
972
973/*
974 * Return the extra width of popup window "wp": border, padding and scrollbar.
975 */
976 int
977popup_extra_width(win_T *wp)
978{
979 return wp->w_popup_padding[3] + wp->w_popup_border[3]
980 + wp->w_popup_padding[1] + wp->w_popup_border[1]
981 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200982}
983
984/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200985 * Adjust the position and size of the popup to fit on the screen.
986 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200987 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200988popup_adjust_position(win_T *wp)
989{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200990 linenr_T lnum;
991 int wrapped = 0;
992 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200993 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200994 int center_vert = FALSE;
995 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200996 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200997 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200998 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
999 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1000 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1001 int extra_height = top_extra + bot_extra;
1002 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001003 int org_winrow = wp->w_winrow;
1004 int org_wincol = wp->w_wincol;
1005 int org_width = wp->w_width;
1006 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001007 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001008 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001009 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001010
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001011 wp->w_winrow = 0;
1012 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001013 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001014 wp->w_popup_leftoff = 0;
1015 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001016 if (wp->w_popup_pos == POPPOS_CENTER)
1017 {
1018 // center after computing the size
1019 center_vert = TRUE;
1020 center_hor = TRUE;
1021 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001022 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001023 {
1024 if (wp->w_wantline == 0)
1025 center_vert = TRUE;
1026 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1027 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1028 {
1029 wp->w_winrow = wp->w_wantline - 1;
1030 if (wp->w_winrow >= Rows)
1031 wp->w_winrow = Rows - 1;
1032 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001033
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001034 if (wp->w_wantcol == 0)
1035 center_hor = TRUE;
1036 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1037 || wp->w_popup_pos == POPPOS_BOTLEFT)
1038 {
1039 wp->w_wincol = wp->w_wantcol - 1;
1040 if (wp->w_wincol >= Columns - 3)
1041 wp->w_wincol = Columns - 3;
1042 }
1043 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001044
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001045 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001046 // When left aligned use the space available, but shift to the left when we
1047 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001048 maxspace = Columns - wp->w_wincol - left_extra;
1049 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001050 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001051 {
1052 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001053 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001054 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001055
Bram Moolenaar8d241042019-06-12 23:40:01 +02001056 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001057 if (wp->w_firstline != 0)
1058 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001059 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1060 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1061
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001062 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001063 // Use a minimum width of one, so that something shows when there is no
1064 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001065 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001066 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001067 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001068 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001069 int len;
1070 int w_width = wp->w_width;
1071
1072 // Count Tabs for what they are worth and compute the length based on
1073 // the maximum width (matters when 'showbreak' is set).
1074 if (wp->w_width < maxwidth)
1075 wp->w_width = maxwidth;
1076 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001077 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001078 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001079
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001080 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001081 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001082 while (len > maxwidth)
1083 {
1084 ++wrapped;
1085 len -= maxwidth;
1086 wp->w_width = maxwidth;
1087 }
1088 }
1089 else if (len > maxwidth
1090 && allow_adjust_left
1091 && (wp->w_popup_pos == POPPOS_TOPLEFT
1092 || wp->w_popup_pos == POPPOS_BOTLEFT))
1093 {
1094 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001095 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001096
Bram Moolenaar51c31312019-06-15 22:27:23 +02001097 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001098 {
1099 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001100
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001101 len -= truncate_shift;
1102 shift_by -= truncate_shift;
1103 }
1104
1105 wp->w_wincol -= shift_by;
1106 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001107 wp->w_width = maxwidth;
1108 }
1109 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001110 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001111 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001112 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1113 wp->w_width = wp->w_maxwidth;
1114 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001115 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001116 if (wp->w_maxheight > 0
1117 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001118 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001119 }
1120
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001121 wp->w_has_scrollbar = wp->w_want_scrollbar
1122 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001123 if (wp->w_has_scrollbar)
1124 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001125
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001126 minwidth = wp->w_minwidth;
1127 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1128 {
1129 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1130
1131 if (minwidth < title_len)
1132 minwidth = title_len;
1133 }
1134
1135 if (minwidth > 0 && wp->w_width < minwidth)
1136 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001137 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001138 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001139 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001140 // some columns cut off on the right
1141 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001142 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001143 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001144 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001145 {
1146 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1147 if (wp->w_wincol < 0)
1148 wp->w_wincol = 0;
1149 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001150 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1151 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1152 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001153 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1154
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001155 // Right aligned: move to the right if needed.
1156 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001157 if (leftoff >= 0)
1158 wp->w_wincol = leftoff;
1159 else if (wp->w_popup_fixed)
1160 {
1161 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001162 // columns when displaying the window, minus left border and
1163 // padding.
1164 if (-leftoff > left_extra)
1165 wp->w_leftcol = -leftoff - left_extra;
1166 wp->w_width -= wp->w_leftcol;
1167 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001168 if (wp->w_width < 0)
1169 wp->w_width = 0;
1170 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001171 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001172
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001173 if (wp->w_p_wrap || (!wp->w_popup_fixed
1174 && (wp->w_popup_pos == POPPOS_TOPLEFT
1175 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001176 {
1177 int want_col = 0;
1178
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001179 // try to show the right border and any scrollbar
1180 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001181 if (want_col > 0 && wp->w_wincol > 0
1182 && wp->w_wincol + want_col >= Columns)
1183 {
1184 wp->w_wincol = Columns - want_col;
1185 if (wp->w_wincol < 0)
1186 wp->w_wincol = 0;
1187 }
1188 }
1189
Bram Moolenaar8d241042019-06-12 23:40:01 +02001190 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1191 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001192 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1193 wp->w_height = wp->w_minheight;
1194 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1195 wp->w_height = wp->w_maxheight;
1196 if (wp->w_height > Rows - wp->w_winrow)
1197 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001198
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001199 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001200 {
1201 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1202 if (wp->w_winrow < 0)
1203 wp->w_winrow = 0;
1204 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001205 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1206 || wp->w_popup_pos == POPPOS_BOTLEFT)
1207 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001208 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001209 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001210 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001211 else
1212 // not enough space, make top aligned
1213 wp->w_winrow = wp->w_wantline + 1;
1214 }
1215
Bram Moolenaar17146962019-05-30 00:12:11 +02001216 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001217
1218 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001219 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001220 if (org_winrow != wp->w_winrow
1221 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001222 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001223 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001224 || org_width != wp->w_width
1225 || org_height != wp->w_height)
1226 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001227 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001228 redraw_win_later(wp, NOT_VALID);
1229 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1230 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001231 popup_mask_refresh = TRUE;
1232 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001233}
1234
Bram Moolenaar17627312019-06-02 19:53:44 +02001235typedef enum
1236{
1237 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001238 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001239 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001240 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001241 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001242 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001243 TYPE_PREVIEW, // preview window
1244 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001245} create_type_T;
1246
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001247/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001248 * Make "buf" empty and set the contents to "text".
1249 * Used by popup_create() and popup_settext().
1250 */
1251 static void
1252popup_set_buffer_text(buf_T *buf, typval_T text)
1253{
1254 int lnum;
1255
1256 // Clear the buffer, then replace the lines.
1257 curbuf = buf;
1258 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1259 ml_delete(lnum, FALSE);
1260 curbuf = curwin->w_buffer;
1261
1262 // Add text to the buffer.
1263 if (text.v_type == VAR_STRING)
1264 {
1265 // just a string
1266 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1267 }
1268 else
1269 {
1270 list_T *l = text.vval.v_list;
1271
1272 if (l->lv_len > 0)
1273 {
1274 if (l->lv_first->li_tv.v_type == VAR_STRING)
1275 // list of strings
1276 add_popup_strings(buf, l);
1277 else
1278 // list of dictionaries
1279 add_popup_dicts(buf, l);
1280 }
1281 }
1282
1283 // delete the line that was in the empty buffer
1284 curbuf = buf;
1285 ml_delete(buf->b_ml.ml_line_count, FALSE);
1286 curbuf = curwin->w_buffer;
1287}
1288
1289/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001290 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1291 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001292 * Return FAIL if the parsing fails.
1293 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001294 static int
1295parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001296{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001297 char_u *p =
1298#ifdef FEAT_QUICKFIX
1299 !is_preview ? p_cpp :
1300#endif
1301 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001302
Bram Moolenaar258cef52019-08-21 17:29:29 +02001303 if (wp != NULL)
1304 wp->w_popup_flags &= ~POPF_INFO_MENU;
1305
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001306 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001307 {
1308 char_u *e, *dig;
1309 char_u *s = p;
1310 int x;
1311
1312 e = vim_strchr(p, ':');
1313 if (e == NULL || e[1] == NUL)
1314 return FAIL;
1315
1316 p = vim_strchr(e, ',');
1317 if (p == NULL)
1318 p = e + STRLEN(e);
1319 dig = e + 1;
1320 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001321
1322 if (STRNCMP(s, "height:", 7) == 0)
1323 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001324 if (dig != p)
1325 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001326 if (wp != NULL)
1327 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001328 if (is_preview)
1329 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001330 wp->w_maxheight = x;
1331 }
1332 }
1333 else if (STRNCMP(s, "width:", 6) == 0)
1334 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001335 if (dig != p)
1336 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001337 if (wp != NULL)
1338 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001339 if (is_preview)
1340 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001341 wp->w_maxwidth = x;
1342 }
1343 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001344 else if (STRNCMP(s, "highlight:", 10) == 0)
1345 {
1346 if (wp != NULL)
1347 {
1348 int c = *p;
1349
1350 *p = NUL;
1351 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1352 s + 10, OPT_FREE|OPT_LOCAL, 0);
1353 *p = c;
1354 }
1355 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001356 else if (STRNCMP(s, "border:", 7) == 0)
1357 {
1358 char_u *arg = s + 7;
1359 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1360 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1361 int i;
1362
1363 if (!on && !off)
1364 return FAIL;
1365 if (wp != NULL)
1366 {
1367 for (i = 0; i < 4; ++i)
1368 wp->w_popup_border[i] = on ? 1 : 0;
1369 if (off)
1370 // only show the X for close when there is a border
1371 wp->w_popup_close = POPCLOSE_NONE;
1372 }
1373 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001374 else if (STRNCMP(s, "align:", 6) == 0)
1375 {
1376 char_u *arg = s + 6;
1377 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1378 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1379
1380 if (!menu && !item)
1381 return FAIL;
1382 if (wp != NULL && menu)
1383 wp->w_popup_flags |= POPF_INFO_MENU;
1384 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001385 else
1386 return FAIL;
1387 }
1388 return OK;
1389}
1390
1391/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001392 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1393 * is not NULL.
1394 * Return FAIL if the parsing fails.
1395 */
1396 int
1397parse_previewpopup(win_T *wp)
1398{
1399 return parse_popup_option(wp, TRUE);
1400}
1401
1402/*
1403 * Parse the 'completepopup' option and apply the values to window "wp" if it
1404 * is not NULL.
1405 * Return FAIL if the parsing fails.
1406 */
1407 int
1408parse_completepopup(win_T *wp)
1409{
1410 return parse_popup_option(wp, FALSE);
1411}
1412
1413/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001414 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001415 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001416 */
1417 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001418popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001419{
1420 setcursor_mayforce(TRUE);
1421 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1422 if (wp->w_wantline == 0) // cursor in first line
1423 {
1424 wp->w_wantline = 2;
1425 wp->w_popup_pos = POPPOS_TOPLEFT;
1426 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001427
Bram Moolenaar79648732019-07-18 21:43:07 +02001428 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001429 if (wp->w_wantcol > Columns - width)
1430 {
1431 wp->w_wantcol = Columns - width;
1432 if (wp->w_wantcol < 1)
1433 wp->w_wantcol = 1;
1434 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001435 popup_adjust_position(wp);
1436}
1437
1438/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001439 * Set w_wantline and w_wantcol for the a given screen position.
1440 * Caller must take care of running into the window border.
1441 */
1442 void
1443popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1444{
1445 wp->w_wantline = row;
1446 wp->w_wantcol = col;
1447 popup_adjust_position(wp);
1448}
1449
1450/*
1451 * Add a border and lef&right padding.
1452 */
1453 static void
1454add_border_left_right_padding(win_T *wp)
1455{
1456 int i;
1457
1458 for (i = 0; i < 4; ++i)
1459 {
1460 wp->w_popup_border[i] = 1;
1461 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1462 }
1463}
1464
1465/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001466 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001467 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001468 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001469 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001470 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001471 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001472popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001473{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001474 win_T *wp;
1475 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001476 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001477 int new_buffer;
1478 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001479 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001480 int nr;
1481 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001482
Bram Moolenaar79648732019-07-18 21:43:07 +02001483 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001484 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001485 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001486 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001487 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001488 buf = buflist_findnr( argvars[0].vval.v_number);
1489 if (buf == NULL)
1490 {
1491 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1492 return NULL;
1493 }
1494 }
1495 else if (!(argvars[0].v_type == VAR_STRING
1496 && argvars[0].vval.v_string != NULL)
1497 && !(argvars[0].v_type == VAR_LIST
1498 && argvars[0].vval.v_list != NULL))
1499 {
1500 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001501 return NULL;
1502 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001503 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001504 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001505 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001506 return NULL;
1507 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001508 d = argvars[1].vval.v_dict;
1509 }
1510
1511 if (d != NULL)
1512 {
1513 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1514 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1515 else if (type == TYPE_NOTIFICATION)
1516 tabnr = -1; // notifications are global by default
1517 else
1518 tabnr = 0;
1519 if (tabnr > 0)
1520 {
1521 tp = find_tabpage(tabnr);
1522 if (tp == NULL)
1523 {
1524 semsg(_("E997: Tabpage not found: %d"), tabnr);
1525 return NULL;
1526 }
1527 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001528 }
1529
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001530 // Create the window and buffer.
1531 wp = win_alloc_popup_win();
1532 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001533 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001534 if (rettv != NULL)
1535 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001536 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001537 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001538
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001539 if (buf != NULL)
1540 {
1541 // use existing buffer
1542 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001543 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001544 buffer_ensure_loaded(buf);
1545 }
1546 else
1547 {
1548 // create a new buffer associated with the popup
1549 new_buffer = TRUE;
1550 buf = buflist_new(NULL, NULL, (linenr_T)0,
1551 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1552 if (buf == NULL)
1553 return NULL;
1554 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001555
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001556 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001557
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001558 set_local_options_default(wp);
1559 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001560 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001561 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001562 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001563 buf->b_p_ul = -1; // no undo
1564 buf->b_p_swf = FALSE; // no swap file
1565 buf->b_p_bl = FALSE; // unlisted buffer
1566 buf->b_locked = TRUE;
1567 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001568
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001569 // Avoid that 'buftype' is reset when this buffer is entered.
1570 buf->b_p_initialized = TRUE;
1571 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001572
Bram Moolenaara3fce622019-06-20 02:31:49 +02001573 if (tp != NULL)
1574 {
1575 // popup on specified tab page
1576 wp->w_next = tp->tp_first_popupwin;
1577 tp->tp_first_popupwin = wp;
1578 }
1579 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001580 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001581 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001582 wp->w_next = curtab->tp_first_popupwin;
1583 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001584 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001585 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001586 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001587 win_T *prev = first_popupwin;
1588
1589 // Global popup: add at the end, so that it gets displayed on top of
1590 // older ones with the same zindex. Matters for notifications.
1591 if (first_popupwin == NULL)
1592 first_popupwin = wp;
1593 else
1594 {
1595 while (prev->w_next != NULL)
1596 prev = prev->w_next;
1597 prev->w_next = wp;
1598 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001599 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001600
Bram Moolenaar79648732019-07-18 21:43:07 +02001601 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001602 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001603
Bram Moolenaar79648732019-07-18 21:43:07 +02001604 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001605 {
1606 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001607 }
1608 if (type == TYPE_ATCURSOR)
1609 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001610 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001611 set_moved_values(wp);
1612 set_moved_columns(wp, FIND_STRING);
1613 }
1614
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001615 if (type == TYPE_BEVAL)
1616 {
1617 wp->w_popup_pos = POPPOS_BOTLEFT;
1618
1619 // by default use the mouse position
1620 wp->w_wantline = mouse_row;
1621 if (wp->w_wantline <= 0) // mouse on first line
1622 {
1623 wp->w_wantline = 2;
1624 wp->w_popup_pos = POPPOS_TOPLEFT;
1625 }
1626 wp->w_wantcol = mouse_col + 1;
1627 set_mousemoved_values(wp);
1628 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1629 }
1630
Bram Moolenaar33796b32019-06-08 16:01:13 +02001631 // set default values
1632 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001633 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001634
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001635 if (type == TYPE_NOTIFICATION)
1636 {
1637 win_T *twp, *nextwin;
1638 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001639
1640 // Try to not overlap with another global popup. Guess we need 3
1641 // more screen lines than buffer lines.
1642 wp->w_wantline = 1;
1643 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1644 {
1645 nextwin = twp->w_next;
1646 if (twp != wp
1647 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1648 && twp->w_winrow <= wp->w_wantline - 1 + height
1649 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1650 {
1651 // move to below this popup and restart the loop to check for
1652 // overlap with other popups
1653 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1654 nextwin = first_popupwin;
1655 }
1656 }
1657 if (wp->w_wantline + height > Rows)
1658 {
1659 // can't avoid overlap, put on top in the hope that message goes
1660 // away soon.
1661 wp->w_wantline = 1;
1662 }
1663
1664 wp->w_wantcol = 10;
1665 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001666 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001667 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001668 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001669 for (i = 0; i < 4; ++i)
1670 wp->w_popup_border[i] = 1;
1671 wp->w_popup_padding[1] = 1;
1672 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001673
1674 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001675 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001676 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1677 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001678 }
1679
Bram Moolenaara730e552019-06-16 19:05:31 +02001680 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001681 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001682 wp->w_popup_pos = POPPOS_CENTER;
1683 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001684 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001685 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001686 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001687 }
1688
Bram Moolenaara730e552019-06-16 19:05:31 +02001689 if (type == TYPE_MENU)
1690 {
1691 typval_T tv;
1692 callback_T callback;
1693
1694 tv.v_type = VAR_STRING;
1695 tv.vval.v_string = (char_u *)"popup_filter_menu";
1696 callback = get_callback(&tv);
1697 if (callback.cb_name != NULL)
1698 set_callback(&wp->w_filter_cb, &callback);
1699
1700 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001701 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001702 }
1703
Bram Moolenaar79648732019-07-18 21:43:07 +02001704 if (type == TYPE_PREVIEW)
1705 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001706 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 wp->w_popup_close = POPCLOSE_BUTTON;
1708 for (i = 0; i < 4; ++i)
1709 wp->w_popup_border[i] = 1;
1710 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001711 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1712 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001713# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001714 if (type == TYPE_INFO)
1715 {
1716 wp->w_popup_pos = POPPOS_TOPLEFT;
1717 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1718 wp->w_popup_close = POPCLOSE_BUTTON;
1719 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001720 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001721 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001722# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001723
Bram Moolenaarae943152019-06-16 22:54:14 +02001724 for (i = 0; i < 4; ++i)
1725 VIM_CLEAR(wp->w_border_highlight[i]);
1726 for (i = 0; i < 8; ++i)
1727 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001728 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001729 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001730
Bram Moolenaar79648732019-07-18 21:43:07 +02001731 if (d != NULL)
1732 // Deal with options.
1733 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001735#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001736 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1737 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001738#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001739
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001740 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001741
1742 wp->w_vsep_width = 0;
1743
1744 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001745 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001746
1747 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001748}
1749
1750/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001751 * popup_clear()
1752 */
1753 void
1754f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1755{
1756 close_all_popups();
1757}
1758
1759/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001760 * popup_create({text}, {options})
1761 */
1762 void
1763f_popup_create(typval_T *argvars, typval_T *rettv)
1764{
Bram Moolenaar17627312019-06-02 19:53:44 +02001765 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001766}
1767
1768/*
1769 * popup_atcursor({text}, {options})
1770 */
1771 void
1772f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1773{
Bram Moolenaar17627312019-06-02 19:53:44 +02001774 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001775}
1776
1777/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001778 * popup_beval({text}, {options})
1779 */
1780 void
1781f_popup_beval(typval_T *argvars, typval_T *rettv)
1782{
1783 popup_create(argvars, rettv, TYPE_BEVAL);
1784}
1785
1786/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001787 * Invoke the close callback for window "wp" with value "result".
1788 * Careful: The callback may make "wp" invalid!
1789 */
1790 static void
1791invoke_popup_callback(win_T *wp, typval_T *result)
1792{
1793 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001794 typval_T argv[3];
1795
1796 argv[0].v_type = VAR_NUMBER;
1797 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1798
1799 if (result != NULL && result->v_type != VAR_UNKNOWN)
1800 copy_tv(result, &argv[1]);
1801 else
1802 {
1803 argv[1].v_type = VAR_NUMBER;
1804 argv[1].vval.v_number = 0;
1805 }
1806
1807 argv[2].v_type = VAR_UNKNOWN;
1808
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001809 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001810 if (result != NULL)
1811 clear_tv(&argv[1]);
1812 clear_tv(&rettv);
1813}
1814
1815/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001816 * Close popup "wp" and invoke any close callback for it.
1817 */
1818 static void
1819popup_close_and_callback(win_T *wp, typval_T *arg)
1820{
1821 int id = wp->w_id;
1822
1823 if (wp->w_close_cb.cb_name != NULL)
1824 // Careful: This may make "wp" invalid.
1825 invoke_popup_callback(wp, arg);
1826
1827 popup_close(id);
1828}
1829
1830/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001831 * Close popup "wp" because of a mouse click.
1832 */
1833 void
1834popup_close_for_mouse_click(win_T *wp)
1835{
1836 typval_T res;
1837
1838 res.v_type = VAR_NUMBER;
1839 res.vval.v_number = -2;
1840 popup_close_and_callback(wp, &res);
1841}
1842
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001843 static void
1844check_mouse_moved(win_T *wp, win_T *mouse_wp)
1845{
1846 // Close the popup when all if these are true:
1847 // - the mouse is not on this popup
1848 // - "mousemoved" was used
1849 // - the mouse is no longer on the same screen row or the mouse column is
1850 // outside of the relevant text
1851 if (wp != mouse_wp
1852 && wp->w_popup_mouse_row != 0
1853 && (wp->w_popup_mouse_row != mouse_row
1854 || mouse_col < wp->w_popup_mouse_mincol
1855 || mouse_col > wp->w_popup_mouse_maxcol))
1856 {
1857 typval_T res;
1858
1859 res.v_type = VAR_NUMBER;
1860 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001861 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001862 popup_close_and_callback(wp, &res);
1863 }
1864}
1865
1866/*
1867 * Called when the mouse moved: may close a popup with "mousemoved".
1868 */
1869 void
1870popup_handle_mouse_moved(void)
1871{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001872 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001873 win_T *mouse_wp;
1874 int row = mouse_row;
1875 int col = mouse_col;
1876
1877 // find the window where the mouse is in
1878 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1879
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001880 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1881 {
1882 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001883 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001884 }
1885 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1886 {
1887 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001888 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001889 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001890}
1891
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001892/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001893 * In a filter: check if the typed key is a mouse event that is used for
1894 * dragging the popup.
1895 */
1896 static void
1897filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1898{
1899 int row = mouse_row;
1900 int col = mouse_col;
1901
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001902 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001903 && is_mouse_key(c)
1904 && (wp == popup_dragwin
1905 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1906 // do not consume the key, allow for dragging the popup
1907 rettv->vval.v_number = 0;
1908}
1909
Bram Moolenaara730e552019-06-16 19:05:31 +02001910/*
1911 * popup_filter_menu({text}, {options})
1912 */
1913 void
1914f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1915{
1916 int id = tv_get_number(&argvars[0]);
1917 win_T *wp = win_id2wp(id);
1918 char_u *key = tv_get_string(&argvars[1]);
1919 typval_T res;
1920 int c;
1921 linenr_T old_lnum;
1922
1923 // If the popup has been closed do not consume the key.
1924 if (wp == NULL)
1925 return;
1926
1927 c = *key;
1928 if (c == K_SPECIAL && key[1] != NUL)
1929 c = TO_SPECIAL(key[1], key[2]);
1930
1931 // consume all keys until done
1932 rettv->vval.v_number = 1;
1933 res.v_type = VAR_NUMBER;
1934
1935 old_lnum = wp->w_cursor.lnum;
1936 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1937 --wp->w_cursor.lnum;
1938 if ((c == 'j' || c == 'J' || c == K_DOWN)
1939 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1940 ++wp->w_cursor.lnum;
1941 if (old_lnum != wp->w_cursor.lnum)
1942 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001943 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001944 return;
1945 }
1946
1947 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1948 {
1949 // Cancelled, invoke callback with -1
1950 res.vval.v_number = -1;
1951 popup_close_and_callback(wp, &res);
1952 return;
1953 }
1954 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1955 {
1956 // Invoke callback with current index.
1957 res.vval.v_number = wp->w_cursor.lnum;
1958 popup_close_and_callback(wp, &res);
1959 return;
1960 }
1961
1962 filter_handle_drag(wp, c, rettv);
1963}
1964
1965/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001966 * popup_filter_yesno({text}, {options})
1967 */
1968 void
1969f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1970{
1971 int id = tv_get_number(&argvars[0]);
1972 win_T *wp = win_id2wp(id);
1973 char_u *key = tv_get_string(&argvars[1]);
1974 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001975 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001976
1977 // If the popup has been closed don't consume the key.
1978 if (wp == NULL)
1979 return;
1980
Bram Moolenaara730e552019-06-16 19:05:31 +02001981 c = *key;
1982 if (c == K_SPECIAL && key[1] != NUL)
1983 c = TO_SPECIAL(key[1], key[2]);
1984
Bram Moolenaara42d9452019-06-15 21:46:30 +02001985 // consume all keys until done
1986 rettv->vval.v_number = 1;
1987
Bram Moolenaara730e552019-06-16 19:05:31 +02001988 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001989 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001990 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001991 res.vval.v_number = 0;
1992 else
1993 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001994 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001995 return;
1996 }
1997
1998 // Invoke callback
1999 res.v_type = VAR_NUMBER;
2000 popup_close_and_callback(wp, &res);
2001}
2002
2003/*
2004 * popup_dialog({text}, {options})
2005 */
2006 void
2007f_popup_dialog(typval_T *argvars, typval_T *rettv)
2008{
2009 popup_create(argvars, rettv, TYPE_DIALOG);
2010}
2011
2012/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002013 * popup_menu({text}, {options})
2014 */
2015 void
2016f_popup_menu(typval_T *argvars, typval_T *rettv)
2017{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002018 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002019}
2020
2021/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002022 * popup_notification({text}, {options})
2023 */
2024 void
2025f_popup_notification(typval_T *argvars, typval_T *rettv)
2026{
2027 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2028}
2029
2030/*
2031 * Find the popup window with window-ID "id".
2032 * If the popup window does not exist NULL is returned.
2033 * If the window is not a popup window, and error message is given.
2034 */
2035 static win_T *
2036find_popup_win(int id)
2037{
2038 win_T *wp = win_id2wp(id);
2039
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002040 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002041 {
2042 semsg(_("E993: window %d is not a popup window"), id);
2043 return NULL;
2044 }
2045 return wp;
2046}
2047
2048/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002049 * popup_close({id})
2050 */
2051 void
2052f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2053{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002054 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002055 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002056
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002057 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002058 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002059}
2060
2061/*
2062 * popup_hide({id})
2063 */
2064 void
2065f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2066{
2067 int id = (int)tv_get_number(argvars);
2068 win_T *wp = find_popup_win(id);
2069
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002070 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002071 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002072 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02002073 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002074 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002075 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002076 }
2077}
2078
2079/*
2080 * popup_show({id})
2081 */
2082 void
2083f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2084{
2085 int id = (int)tv_get_number(argvars);
2086 win_T *wp = find_popup_win(id);
2087
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002088 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002089 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002090 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02002091 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002092 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002093 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002094 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002095}
2096
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002097/*
2098 * popup_settext({id}, {text})
2099 */
2100 void
2101f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2102{
2103 int id = (int)tv_get_number(&argvars[0]);
2104 win_T *wp = find_popup_win(id);
2105
2106 if (wp != NULL)
2107 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002108 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2109 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2110 else
2111 {
2112 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002113 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002114 popup_adjust_position(wp);
2115 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002116 }
2117}
2118
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002119 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002120popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002121{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002122 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002123 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002124 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002125 clear_cmdline = TRUE;
2126 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002127
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002128 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002129 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002130}
2131
Bram Moolenaarec583842019-05-26 14:11:23 +02002132/*
2133 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002134 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002135 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002136 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002137popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002138{
2139 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002140 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002141 win_T *prev = NULL;
2142
Bram Moolenaarec583842019-05-26 14:11:23 +02002143 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002144 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002145 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002146 {
2147 if (prev == NULL)
2148 first_popupwin = wp->w_next;
2149 else
2150 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002151 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002152 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002153 }
2154
Bram Moolenaarec583842019-05-26 14:11:23 +02002155 // go through tab-local popups
2156 FOR_ALL_TABPAGES(tp)
2157 popup_close_tabpage(tp, id);
2158}
2159
2160/*
2161 * Close a popup window with Window-id "id" in tabpage "tp".
2162 */
2163 void
2164popup_close_tabpage(tabpage_T *tp, int id)
2165{
2166 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002167 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002168 win_T *prev = NULL;
2169
Bram Moolenaarec583842019-05-26 14:11:23 +02002170 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2171 if (wp->w_id == id)
2172 {
2173 if (prev == NULL)
2174 *root = wp->w_next;
2175 else
2176 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002177 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002178 return;
2179 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002180}
2181
2182 void
2183close_all_popups(void)
2184{
2185 while (first_popupwin != NULL)
2186 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002187 while (curtab->tp_first_popupwin != NULL)
2188 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002189}
2190
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002191/*
2192 * popup_move({id}, {options})
2193 */
2194 void
2195f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2196{
Bram Moolenaarae943152019-06-16 22:54:14 +02002197 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002198 int id = (int)tv_get_number(argvars);
2199 win_T *wp = find_popup_win(id);
2200
2201 if (wp == NULL)
2202 return; // invalid {id}
2203
2204 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2205 {
2206 emsg(_(e_dictreq));
2207 return;
2208 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002209 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002210
Bram Moolenaarae943152019-06-16 22:54:14 +02002211 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002212
2213 if (wp->w_winrow + wp->w_height >= cmdline_row)
2214 clear_cmdline = TRUE;
2215 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002216}
2217
Bram Moolenaarbc133542019-05-29 20:26:46 +02002218/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002219 * popup_setoptions({id}, {options})
2220 */
2221 void
2222f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2223{
2224 dict_T *dict;
2225 int id = (int)tv_get_number(argvars);
2226 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002227 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002228
2229 if (wp == NULL)
2230 return; // invalid {id}
2231
2232 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2233 {
2234 emsg(_(e_dictreq));
2235 return;
2236 }
2237 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002238 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002239
2240 apply_move_options(wp, dict);
2241 apply_general_options(wp, dict);
2242
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002243 if (old_firstline != wp->w_firstline)
2244 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002245 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002246 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002247 popup_adjust_position(wp);
2248}
2249
2250/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002251 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002252 */
2253 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002254f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002255{
2256 dict_T *dict;
2257 int id = (int)tv_get_number(argvars);
2258 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002259 int top_extra;
2260 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002261
2262 if (rettv_dict_alloc(rettv) == OK)
2263 {
2264 if (wp == NULL)
2265 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002266 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002267 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2268
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002269 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002270 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002271 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002272
Bram Moolenaarbc133542019-05-29 20:26:46 +02002273 dict_add_number(dict, "line", wp->w_winrow + 1);
2274 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002275 dict_add_number(dict, "width", wp->w_width + left_extra
2276 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2277 dict_add_number(dict, "height", wp->w_height + top_extra
2278 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002279
2280 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2281 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2282 dict_add_number(dict, "core_width", wp->w_width);
2283 dict_add_number(dict, "core_height", wp->w_height);
2284
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002285 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002286 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002287 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002288 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002289
2290 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002291 }
2292}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002293/*
2294 * popup_locate({row}, {col})
2295 */
2296 void
2297f_popup_locate(typval_T *argvars, typval_T *rettv)
2298{
2299 int row = tv_get_number(&argvars[0]) - 1;
2300 int col = tv_get_number(&argvars[1]) - 1;
2301 win_T *wp;
2302
2303 wp = mouse_find_win(&row, &col, FIND_POPUP);
2304 if (WIN_IS_POPUP(wp))
2305 rettv->vval.v_number = wp->w_id;
2306}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002307
2308/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002309 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2310 */
2311 static void
2312get_padding_border(dict_T *dict, int *array, char *name)
2313{
2314 list_T *list;
2315 int i;
2316
2317 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2318 return;
2319
2320 list = list_alloc();
2321 if (list != NULL)
2322 {
2323 dict_add_list(dict, name, list);
2324 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2325 for (i = 0; i < 4; ++i)
2326 list_append_number(list, array[i]);
2327 }
2328}
2329
2330/*
2331 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2332 */
2333 static void
2334get_borderhighlight(dict_T *dict, win_T *wp)
2335{
2336 list_T *list;
2337 int i;
2338
2339 for (i = 0; i < 4; ++i)
2340 if (wp->w_border_highlight[i] != NULL)
2341 break;
2342 if (i == 4)
2343 return;
2344
2345 list = list_alloc();
2346 if (list != NULL)
2347 {
2348 dict_add_list(dict, "borderhighlight", list);
2349 for (i = 0; i < 4; ++i)
2350 list_append_string(list, wp->w_border_highlight[i], -1);
2351 }
2352}
2353
2354/*
2355 * For popup_getoptions(): add a "borderchars" entry to "dict".
2356 */
2357 static void
2358get_borderchars(dict_T *dict, win_T *wp)
2359{
2360 list_T *list;
2361 int i;
2362 char_u buf[NUMBUFLEN];
2363 int len;
2364
2365 for (i = 0; i < 8; ++i)
2366 if (wp->w_border_char[i] != 0)
2367 break;
2368 if (i == 8)
2369 return;
2370
2371 list = list_alloc();
2372 if (list != NULL)
2373 {
2374 dict_add_list(dict, "borderchars", list);
2375 for (i = 0; i < 8; ++i)
2376 {
2377 len = mb_char2bytes(wp->w_border_char[i], buf);
2378 list_append_string(list, buf, len);
2379 }
2380 }
2381}
2382
2383/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002384 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002385 */
2386 static void
2387get_moved_list(dict_T *dict, win_T *wp)
2388{
2389 list_T *list;
2390
2391 list = list_alloc();
2392 if (list != NULL)
2393 {
2394 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002395 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002396 list_append_number(list, wp->w_popup_mincol);
2397 list_append_number(list, wp->w_popup_maxcol);
2398 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002399 list = list_alloc();
2400 if (list != NULL)
2401 {
2402 dict_add_list(dict, "mousemoved", list);
2403 list_append_number(list, wp->w_popup_mouse_row);
2404 list_append_number(list, wp->w_popup_mouse_mincol);
2405 list_append_number(list, wp->w_popup_mouse_maxcol);
2406 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002407}
2408
2409/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002410 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002411 */
2412 void
2413f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2414{
2415 dict_T *dict;
2416 int id = (int)tv_get_number(argvars);
2417 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002418 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002419 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002420
2421 if (rettv_dict_alloc(rettv) == OK)
2422 {
2423 if (wp == NULL)
2424 return;
2425
2426 dict = rettv->vval.v_dict;
2427 dict_add_number(dict, "line", wp->w_wantline);
2428 dict_add_number(dict, "col", wp->w_wantcol);
2429 dict_add_number(dict, "minwidth", wp->w_minwidth);
2430 dict_add_number(dict, "minheight", wp->w_minheight);
2431 dict_add_number(dict, "maxheight", wp->w_maxheight);
2432 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002433 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002434 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002435 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002436 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002437 dict_add_string(dict, "title", wp->w_popup_title);
2438 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002439 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02002440 dict_add_number(dict, "mapping", (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002441 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002442 dict_add_number(dict, "cursorline",
2443 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002444 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002445 if (wp->w_scrollbar_highlight != NULL)
2446 dict_add_string(dict, "scrollbarhighlight",
2447 wp->w_scrollbar_highlight);
2448 if (wp->w_thumb_highlight != NULL)
2449 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002450
Bram Moolenaara3fce622019-06-20 02:31:49 +02002451 // find the tabpage that holds this popup
2452 i = 1;
2453 FOR_ALL_TABPAGES(tp)
2454 {
2455 win_T *p;
2456
2457 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2458 if (p->w_id == id)
2459 break;
2460 if (p != NULL)
2461 break;
2462 ++i;
2463 }
2464 if (tp == NULL)
2465 i = -1; // must be global
2466 else if (tp == curtab)
2467 i = 0;
2468 dict_add_number(dict, "tabpage", i);
2469
Bram Moolenaarae943152019-06-16 22:54:14 +02002470 get_padding_border(dict, wp->w_popup_padding, "padding");
2471 get_padding_border(dict, wp->w_popup_border, "border");
2472 get_borderhighlight(dict, wp);
2473 get_borderchars(dict, wp);
2474 get_moved_list(dict, wp);
2475
2476 if (wp->w_filter_cb.cb_name != NULL)
2477 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2478 if (wp->w_close_cb.cb_name != NULL)
2479 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002480
2481 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2482 ++i)
2483 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2484 {
2485 dict_add_string(dict, "pos",
2486 (char_u *)poppos_entries[i].pp_name);
2487 break;
2488 }
2489
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002490 dict_add_string(dict, "close", (char_u *)(
2491 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2492 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2493
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002494# if defined(FEAT_TIMERS)
2495 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2496 ? (long)wp->w_popup_timer->tr_interval : 0L);
2497# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002498 }
2499}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002500
2501 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002502error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002503{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002504 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002505 {
2506 emsg(_("E994: Not allowed in a popup window"));
2507 return TRUE;
2508 }
2509 return FALSE;
2510}
2511
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002512/*
2513 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002514 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002515 */
2516 void
2517popup_reset_handled()
2518{
2519 win_T *wp;
2520
2521 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2522 wp->w_popup_flags &= ~POPF_HANDLED;
2523 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2524 wp->w_popup_flags &= ~POPF_HANDLED;
2525}
2526
2527/*
2528 * Find the next visible popup where POPF_HANDLED is not set.
2529 * Must have called popup_reset_handled() first.
2530 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2531 * popup with the highest zindex.
2532 */
2533 win_T *
2534find_next_popup(int lowest)
2535{
2536 win_T *wp;
2537 win_T *found_wp;
2538 int found_zindex;
2539
2540 found_zindex = lowest ? INT_MAX : 0;
2541 found_wp = NULL;
2542 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2543 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2544 && (lowest ? wp->w_zindex < found_zindex
2545 : wp->w_zindex > found_zindex))
2546 {
2547 found_zindex = wp->w_zindex;
2548 found_wp = wp;
2549 }
2550 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2551 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2552 && (lowest ? wp->w_zindex < found_zindex
2553 : wp->w_zindex > found_zindex))
2554 {
2555 found_zindex = wp->w_zindex;
2556 found_wp = wp;
2557 }
2558
2559 if (found_wp != NULL)
2560 found_wp->w_popup_flags |= POPF_HANDLED;
2561 return found_wp;
2562}
2563
2564/*
2565 * Invoke the filter callback for window "wp" with typed character "c".
2566 * Uses the global "mod_mask" for modifiers.
2567 * Returns the return value of the filter.
2568 * Careful: The filter may make "wp" invalid!
2569 */
2570 static int
2571invoke_popup_filter(win_T *wp, int c)
2572{
2573 int res;
2574 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002575 typval_T argv[3];
2576 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002577 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002578
Bram Moolenaara42d9452019-06-15 21:46:30 +02002579 // Emergency exit: CTRL-C closes the popup.
2580 if (c == Ctrl_C)
2581 {
2582 rettv.v_type = VAR_NUMBER;
2583 rettv.vval.v_number = -1;
2584 popup_close_and_callback(wp, &rettv);
2585 return 1;
2586 }
2587
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002588 argv[0].v_type = VAR_NUMBER;
2589 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2590
2591 // Convert the number to a string, so that the function can use:
2592 // if a:c == "\<F2>"
2593 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2594 argv[1].v_type = VAR_STRING;
2595 argv[1].vval.v_string = vim_strsave(buf);
2596
2597 argv[2].v_type = VAR_UNKNOWN;
2598
Bram Moolenaara42d9452019-06-15 21:46:30 +02002599 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002600 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002601 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002602 popup_highlight_curline(wp);
2603
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002604 res = tv_get_number(&rettv);
2605 vim_free(argv[1].vval.v_string);
2606 clear_tv(&rettv);
2607 return res;
2608}
2609
2610/*
2611 * Called when "c" was typed: invoke popup filter callbacks.
2612 * Returns TRUE when the character was consumed,
2613 */
2614 int
2615popup_do_filter(int c)
2616{
2617 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002618 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002619
2620 popup_reset_handled();
2621
2622 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2623 if (wp->w_filter_cb.cb_name != NULL)
2624 res = invoke_popup_filter(wp, c);
2625
2626 return res;
2627}
2628
Bram Moolenaar3397f742019-06-02 18:40:06 +02002629/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002630 * Return TRUE if there is a popup visible with a filter callback and the
2631 * "mapping" property off.
2632 */
2633 int
2634popup_no_mapping(void)
2635{
2636 int round;
2637 win_T *wp;
2638
2639 for (round = 1; round <= 2; ++round)
2640 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2641 wp != NULL; wp = wp->w_next)
2642 if (wp->w_filter_cb.cb_name != NULL
2643 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2644 return TRUE;
2645 return FALSE;
2646}
2647
2648/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002649 * Called when the cursor moved: check if any popup needs to be closed if the
2650 * cursor moved far enough.
2651 */
2652 void
2653popup_check_cursor_pos()
2654{
2655 win_T *wp;
2656 typval_T tv;
2657
2658 popup_reset_handled();
2659 while ((wp = find_next_popup(TRUE)) != NULL)
2660 if (wp->w_popup_curwin != NULL
2661 && (curwin != wp->w_popup_curwin
2662 || curwin->w_cursor.lnum != wp->w_popup_lnum
2663 || curwin->w_cursor.col < wp->w_popup_mincol
2664 || curwin->w_cursor.col > wp->w_popup_maxcol))
2665 {
2666 tv.v_type = VAR_NUMBER;
2667 tv.vval.v_number = -1;
2668 popup_close_and_callback(wp, &tv);
2669 }
2670}
2671
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002672/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002673 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002674 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002675 static void
2676popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002677{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002678 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002679 char_u *cells;
2680 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002681
2682 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002683 return;
2684 if (wp->w_popup_mask_cells != NULL
2685 && wp->w_popup_mask_height == height
2686 && wp->w_popup_mask_width == width)
2687 return; // cache is still valid
2688
2689 vim_free(wp->w_popup_mask_cells);
2690 wp->w_popup_mask_cells = alloc_clear(width * height);
2691 if (wp->w_popup_mask_cells == NULL)
2692 return;
2693 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002694
2695 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2696 {
2697 int cols, cole;
2698 int lines, linee;
2699
2700 li = lio->li_tv.vval.v_list->lv_first;
2701 cols = tv_get_number(&li->li_tv);
2702 if (cols < 0)
2703 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002704 li = li->li_next;
2705 cole = tv_get_number(&li->li_tv);
2706 if (cole < 0)
2707 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002708 li = li->li_next;
2709 lines = tv_get_number(&li->li_tv);
2710 if (lines < 0)
2711 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002712 li = li->li_next;
2713 linee = tv_get_number(&li->li_tv);
2714 if (linee < 0)
2715 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002716
2717 for (row = lines - 1; row < linee && row < height; ++row)
2718 for (col = cols - 1; col < cole && col < width; ++col)
2719 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002720 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002721}
2722
2723/*
2724 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2725 * "col" and "line" are screen coordinates.
2726 */
2727 static int
2728popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2729{
2730 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2731 int line = screenline - wp->w_winrow;
2732
2733 return col >= 0 && col < width
2734 && line >= 0 && line < height
2735 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002736}
2737
2738/*
2739 * Set flags in popup_transparent[] for window "wp" to "val".
2740 */
2741 static void
2742update_popup_transparent(win_T *wp, int val)
2743{
2744 if (wp->w_popup_mask != NULL)
2745 {
2746 int width = popup_width(wp);
2747 int height = popup_height(wp);
2748 listitem_T *lio, *li;
2749 int cols, cole;
2750 int lines, linee;
2751 int col, line;
2752
2753 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2754 {
2755 li = lio->li_tv.vval.v_list->lv_first;
2756 cols = tv_get_number(&li->li_tv);
2757 if (cols < 0)
2758 cols = width + cols + 1;
2759 li = li->li_next;
2760 cole = tv_get_number(&li->li_tv);
2761 if (cole < 0)
2762 cole = width + cole + 1;
2763 li = li->li_next;
2764 lines = tv_get_number(&li->li_tv);
2765 if (lines < 0)
2766 lines = height + lines + 1;
2767 li = li->li_next;
2768 linee = tv_get_number(&li->li_tv);
2769 if (linee < 0)
2770 linee = height + linee + 1;
2771
2772 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002773 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002774 if (cols < 0)
2775 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002776 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002777 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002778 if (lines < 0)
2779 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002780 for (line = lines; line < linee
2781 && line + wp->w_winrow < screen_Rows; ++line)
2782 for (col = cols; col < cole
2783 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002784 popup_transparent[(line + wp->w_winrow) * screen_Columns
2785 + col + wp->w_wincol] = val;
2786 }
2787 }
2788}
2789
2790/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002791 * Update "popup_mask" if needed.
2792 * Also recomputes the popup size and positions.
2793 * Also updates "popup_visible".
2794 * Also marks window lines for redrawing.
2795 */
2796 void
2797may_update_popup_mask(int type)
2798{
2799 win_T *wp;
2800 short *mask;
2801 int line, col;
2802 int redraw_all = FALSE;
2803
2804 // Need to recompute when switching tabs.
2805 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2806 // (such as the screen size) must have changed.
2807 if (popup_mask_tab != curtab || type >= NOT_VALID)
2808 {
2809 popup_mask_refresh = TRUE;
2810 redraw_all = TRUE;
2811 }
2812 if (!popup_mask_refresh)
2813 {
2814 // Check if any buffer has changed.
2815 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2816 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2817 popup_mask_refresh = TRUE;
2818 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2819 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2820 popup_mask_refresh = TRUE;
2821 if (!popup_mask_refresh)
2822 return;
2823 }
2824
2825 // Need to update the mask, something has changed.
2826 popup_mask_refresh = FALSE;
2827 popup_mask_tab = curtab;
2828 popup_visible = FALSE;
2829
2830 // If redrawing everything, just update "popup_mask".
2831 // If redrawing only what is needed, update "popup_mask_next" and then
2832 // compare with "popup_mask" to see what changed.
2833 if (type >= SOME_VALID)
2834 mask = popup_mask;
2835 else
2836 mask = popup_mask_next;
2837 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2838
2839 // Find the window with the lowest zindex that hasn't been handled yet,
2840 // so that the window with a higher zindex overwrites the value in
2841 // popup_mask.
2842 popup_reset_handled();
2843 while ((wp = find_next_popup(TRUE)) != NULL)
2844 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002845 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002846 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002847
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002848 popup_visible = TRUE;
2849
2850 // Recompute the position if the text changed.
2851 if (redraw_all
2852 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2853 popup_adjust_position(wp);
2854
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002855 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002856 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002857 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002858 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002859 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002860 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002861 col < wp->w_wincol + width - wp->w_popup_leftoff
2862 && col < screen_Columns; ++col)
2863 if (wp->w_popup_mask_cells == NULL
2864 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002865 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002866 }
2867
2868 // Only check which lines are to be updated if not already
2869 // updating all lines.
2870 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002871 {
2872 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2873 win_T *prev_wp = NULL;
2874
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002875 for (line = 0; line < screen_Rows; ++line)
2876 {
2877 int col_done = 0;
2878
2879 for (col = 0; col < screen_Columns; ++col)
2880 {
2881 int off = line * screen_Columns + col;
2882
2883 if (popup_mask[off] != popup_mask_next[off])
2884 {
2885 popup_mask[off] = popup_mask_next[off];
2886
2887 if (line >= cmdline_row)
2888 {
2889 // the command line needs to be cleared if text below
2890 // the popup is now visible.
2891 if (!msg_scrolled && popup_mask_next[off] == 0)
2892 clear_cmdline = TRUE;
2893 }
2894 else if (col >= col_done)
2895 {
2896 linenr_T lnum;
2897 int line_cp = line;
2898 int col_cp = col;
2899
2900 // The screen position "line" / "col" needs to be
2901 // redrawn. Figure out what window that is and update
2902 // w_redraw_top and w_redr_bot. Only needs to be done
2903 // once for each window line.
2904 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2905 if (wp != NULL)
2906 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002907 if (wp != prev_wp)
2908 {
2909 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2910 prev_wp = wp;
2911 }
2912
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002913 if (line_cp >= wp->w_height)
2914 // In (or below) status line
2915 wp->w_redr_status = TRUE;
2916 // compute the position in the buffer line from the
2917 // position on the screen
2918 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002919 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002920 // past bottom
2921 wp->w_redr_status = TRUE;
2922 else
2923 redrawWinline(wp, lnum);
2924
2925 // This line is going to be redrawn, no need to
2926 // check until the right side of the window.
2927 col_done = wp->w_wincol + wp->w_width - 1;
2928 }
2929 }
2930 }
2931 }
2932 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002933
2934 vim_free(plines_cache);
2935 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002936}
2937
2938/*
2939 * Return a string of "len" spaces in IObuff.
2940 */
2941 static char_u *
2942get_spaces(int len)
2943{
2944 vim_memset(IObuff, ' ', (size_t)len);
2945 IObuff[len] = NUL;
2946 return IObuff;
2947}
2948
2949/*
2950 * Update popup windows. They are drawn on top of normal windows.
2951 * "win_update" is called for each popup window, lowest zindex first.
2952 */
2953 void
2954update_popups(void (*win_update)(win_T *wp))
2955{
2956 win_T *wp;
2957 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002958 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002959 int total_width;
2960 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002961 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002962 int popup_attr;
2963 int border_attr[4];
2964 int border_char[8];
2965 char_u buf[MB_MAXBYTES];
2966 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002967 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002968 int padcol = 0;
2969 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002970 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002971 int sb_thumb_top = 0;
2972 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002973 int attr_scroll = 0;
2974 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002975
2976 // Find the window with the lowest zindex that hasn't been updated yet,
2977 // so that the window with a higher zindex is drawn later, thus goes on
2978 // top.
2979 popup_reset_handled();
2980 while ((wp = find_next_popup(TRUE)) != NULL)
2981 {
2982 // This drawing uses the zindex of the popup window, so that it's on
2983 // top of the text but doesn't draw when another popup with higher
2984 // zindex is on top of the character.
2985 screen_zindex = wp->w_zindex;
2986
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002987 // Set flags in popup_transparent[] for masked cells.
2988 update_popup_transparent(wp, 1);
2989
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002990 // adjust w_winrow and w_wincol for border and padding, since
2991 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002992 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002993 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2994 - wp->w_popup_leftoff;
2995 if (wp->w_wincol + left_extra < 0)
2996 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002997 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002998 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002999
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003000 // Draw the popup text, unless it's off screen.
3001 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
3002 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003003
3004 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003005 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003006
Bram Moolenaard529ba52019-07-02 23:13:53 +02003007 total_width = popup_width(wp);
3008 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003009 popup_attr = get_wcr_attr(wp);
3010
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003011 if (wp->w_winrow + total_height > cmdline_row)
3012 wp->w_popup_flags |= POPF_ON_CMDLINE;
3013 else
3014 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3015
3016
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003017 // We can only use these line drawing characters when 'encoding' is
3018 // "utf-8" and 'ambiwidth' is "single".
3019 if (enc_utf8 && *p_ambw == 's')
3020 {
3021 border_char[0] = border_char[2] = 0x2550;
3022 border_char[1] = border_char[3] = 0x2551;
3023 border_char[4] = 0x2554;
3024 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003025 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3026 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003027 border_char[7] = 0x255a;
3028 }
3029 else
3030 {
3031 border_char[0] = border_char[2] = '-';
3032 border_char[1] = border_char[3] = '|';
3033 for (i = 4; i < 8; ++i)
3034 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003035 if (wp->w_popup_flags & POPF_RESIZE)
3036 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003037 }
3038 for (i = 0; i < 8; ++i)
3039 if (wp->w_border_char[i] != 0)
3040 border_char[i] = wp->w_border_char[i];
3041
3042 for (i = 0; i < 4; ++i)
3043 {
3044 border_attr[i] = popup_attr;
3045 if (wp->w_border_highlight[i] != NULL)
3046 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3047 }
3048
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003049 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003050 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003051 if (wp->w_popup_border[0] > 0)
3052 {
3053 // top border
3054 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003055 wincol < 0 ? 0 : wincol, wincol + total_width,
3056 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003057 ? border_char[4] : border_char[0],
3058 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003059 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003060 {
3061 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3062 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003063 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003064 }
3065 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003066 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3067 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003068
Bram Moolenaard529ba52019-07-02 23:13:53 +02003069 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3070 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003071 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003072 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3073 - wp->w_has_scrollbar;
3074 if (padcol < 0)
3075 {
3076 padwidth += padcol;
3077 padcol = 0;
3078 }
3079 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003080 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003081 {
3082 // top padding
3083 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003084 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003085 ' ', ' ', popup_attr);
3086 }
3087
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003088 // Title goes on top of border or padding.
3089 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003090 {
3091 int len = (int)STRLEN(wp->w_popup_title) + 1;
3092 char_u *title = alloc(len);
3093
3094 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3095 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003096 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003097 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003098
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003099 // Compute scrollbar thumb position and size.
3100 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003101 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003102 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3103
Bram Moolenaar68acb412019-06-26 03:40:36 +02003104 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3105 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003106 if (sb_thumb_height == 0)
3107 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003108 if (linecount <= wp->w_height)
3109 // it just fits, avoid divide by zero
3110 sb_thumb_top = 0;
3111 else
3112 sb_thumb_top = (wp->w_topline - 1
3113 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003114 * (wp->w_height - sb_thumb_height)
3115 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003116 if (wp->w_scrollbar_highlight != NULL)
3117 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3118 else
3119 attr_scroll = highlight_attr[HLF_PSB];
3120 if (wp->w_thumb_highlight != NULL)
3121 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3122 else
3123 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003124 }
3125
3126 for (i = wp->w_popup_border[0];
3127 i < total_height - wp->w_popup_border[2]; ++i)
3128 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003129 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003130 // left and right padding only needed next to the body
3131 int do_padding =
3132 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3133 && i < total_height - wp->w_popup_border[2]
3134 - wp->w_popup_padding[2];
3135
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003136 row = wp->w_winrow + i;
3137
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003138 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003139 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003140 {
3141 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003142 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003143 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003144 if (do_padding && wp->w_popup_padding[3] > 0)
3145 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003146 int col = wincol + wp->w_popup_border[3];
3147
Bram Moolenaard529ba52019-07-02 23:13:53 +02003148 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003149 pad_left = wp->w_popup_padding[3];
3150 if (col < 0)
3151 {
3152 pad_left += col;
3153 col = 0;
3154 }
3155 if (pad_left > 0)
3156 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3157 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003158 // scrollbar
3159 if (wp->w_has_scrollbar)
3160 {
3161 int line = i - top_off;
3162 int scroll_col = wp->w_wincol + total_width - 1
3163 - wp->w_popup_border[1];
3164
3165 if (line >= 0 && line < wp->w_height)
3166 screen_putchar(' ', row, scroll_col,
3167 line >= sb_thumb_top
3168 && line < sb_thumb_top + sb_thumb_height
3169 ? attr_thumb : attr_scroll);
3170 else
3171 screen_putchar(' ', row, scroll_col, popup_attr);
3172 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003173 // right border
3174 if (wp->w_popup_border[1] > 0)
3175 {
3176 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003177 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003178 }
3179 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003180 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003181 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003182 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003183 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3184 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003185 }
3186
3187 if (wp->w_popup_padding[2] > 0)
3188 {
3189 // bottom padding
3190 row = wp->w_winrow + wp->w_popup_border[0]
3191 + wp->w_popup_padding[0] + wp->w_height;
3192 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003193 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003194 }
3195
3196 if (wp->w_popup_border[2] > 0)
3197 {
3198 // bottom border
3199 row = wp->w_winrow + total_height - 1;
3200 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003201 wincol < 0 ? 0 : wincol,
3202 wincol + total_width,
3203 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003204 ? border_char[7] : border_char[2],
3205 border_char[2], border_attr[2]);
3206 if (wp->w_popup_border[1] > 0)
3207 {
3208 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003209 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003210 }
3211 }
3212
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003213 if (wp->w_popup_close == POPCLOSE_BUTTON)
3214 {
3215 // close button goes on top of anything at the top-right corner
3216 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003217 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003218 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3219 }
3220
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003221 update_popup_transparent(wp, 0);
3222
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003223 // Back to the normal zindex.
3224 screen_zindex = 0;
3225 }
3226}
3227
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003228/*
3229 * Mark references in callbacks of one popup window.
3230 */
3231 static int
3232set_ref_in_one_popup(win_T *wp, int copyID)
3233{
3234 int abort = FALSE;
3235 typval_T tv;
3236
3237 if (wp->w_close_cb.cb_partial != NULL)
3238 {
3239 tv.v_type = VAR_PARTIAL;
3240 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3241 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3242 }
3243 if (wp->w_filter_cb.cb_partial != NULL)
3244 {
3245 tv.v_type = VAR_PARTIAL;
3246 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3247 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3248 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003249 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003250 return abort;
3251}
3252
3253/*
3254 * Set reference in callbacks of popup windows.
3255 */
3256 int
3257set_ref_in_popups(int copyID)
3258{
3259 int abort = FALSE;
3260 win_T *wp;
3261 tabpage_T *tp;
3262
3263 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3264 abort = abort || set_ref_in_one_popup(wp, copyID);
3265
3266 FOR_ALL_TABPAGES(tp)
3267 {
3268 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3269 abort = abort || set_ref_in_one_popup(wp, copyID);
3270 if (abort)
3271 break;
3272 }
3273 return abort;
3274}
Bram Moolenaar79648732019-07-18 21:43:07 +02003275
3276/*
3277 * Find an existing popup used as the preview window, in the current tab page.
3278 * Return NULL if not found.
3279 */
3280 win_T *
3281popup_find_preview_window(void)
3282{
3283 win_T *wp;
3284
3285 // Preview window popup is always local to tab page.
3286 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3287 if (wp->w_p_pvw)
3288 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003289 return NULL;
3290}
3291
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003292 int
3293popup_is_popup(win_T *wp)
3294{
3295 return wp->w_popup_flags != 0;
3296}
3297
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003298#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003299/*
3300 * Find an existing popup used as the info window, in the current tab page.
3301 * Return NULL if not found.
3302 */
3303 win_T *
3304popup_find_info_window(void)
3305{
3306 win_T *wp;
3307
3308 // info window popup is always local to tab page.
3309 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3310 if (wp->w_popup_flags & POPF_INFO)
3311 return wp;
3312 return NULL;
3313}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003314#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003315
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003316 void
3317f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3318{
3319 win_T *wp = popup_find_preview_window();
3320
3321 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003322}
3323
Bram Moolenaar79648732019-07-18 21:43:07 +02003324/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003325 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003326 * NOTE: this makes the popup the current window, so that the file can be
3327 * edited. However, it must not remain to be the current window, the caller
3328 * must make sure of that.
3329 */
3330 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003331popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003332{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003333 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003334
3335 if (wp == NULL)
3336 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003337 if (info)
3338 wp->w_popup_flags |= POPF_INFO;
3339 else
3340 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003341
3342 // Set the width to a reasonable value, so that w_topline can be computed.
3343 if (wp->w_minwidth > 0)
3344 wp->w_width = wp->w_minwidth;
3345 else if (wp->w_maxwidth > 0)
3346 wp->w_width = wp->w_maxwidth;
3347 else
3348 wp->w_width = curwin->w_width;
3349
3350 // Will switch to another buffer soon, dummy one can be wiped.
3351 wp->w_buffer->b_locked = FALSE;
3352
3353 win_enter(wp, FALSE);
3354 return OK;
3355}
3356
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003357#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar79648732019-07-18 21:43:07 +02003358 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003359popup_close_preview(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003360{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003361 win_T *wp = info ? popup_find_info_window() : popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003362
3363 if (wp != NULL)
3364 {
3365 typval_T res;
3366
3367 res.v_type = VAR_NUMBER;
3368 res.vval.v_number = -1;
3369 popup_close_and_callback(wp, &res);
3370 }
3371}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003372#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003373
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003374/*
3375 * Set the title of the popup window to the file name.
3376 */
3377 void
3378popup_set_title(win_T *wp)
3379{
3380 if (wp->w_buffer->b_fname != NULL)
3381 {
3382 char_u dirname[MAXPATHL];
3383 size_t len;
3384
3385 mch_dirname(dirname, MAXPATHL);
3386 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3387
3388 vim_free(wp->w_popup_title);
3389 len = STRLEN(wp->w_buffer->b_fname) + 3;
3390 wp->w_popup_title = alloc((int)len);
3391 if (wp->w_popup_title != NULL)
3392 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3393 wp->w_buffer->b_fname);
3394 redraw_win_later(wp, VALID);
3395 }
3396}
3397
3398/*
3399 * If there is a preview window, update the title.
3400 * Used after changing directory.
3401 */
3402 void
3403popup_update_preview_title(void)
3404{
3405 win_T *wp = popup_find_preview_window();
3406
3407 if (wp != NULL)
3408 popup_set_title(wp);
3409}
3410
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003411#endif // FEAT_TEXT_PROP