blob: e35aa62542c7908995d415d6571500736c187bd2 [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)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001124 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001125 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001126 ++extra_width;
1127 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001128
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001129 minwidth = wp->w_minwidth;
1130 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1131 {
1132 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1133
1134 if (minwidth < title_len)
1135 minwidth = title_len;
1136 }
1137
1138 if (minwidth > 0 && wp->w_width < minwidth)
1139 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001140 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001142 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001143 // some columns cut off on the right
1144 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001145 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001146 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001147 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001148 {
1149 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1150 if (wp->w_wincol < 0)
1151 wp->w_wincol = 0;
1152 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001153 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1154 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1155 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001156 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1157
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001158 // Right aligned: move to the right if needed.
1159 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001160 if (leftoff >= 0)
1161 wp->w_wincol = leftoff;
1162 else if (wp->w_popup_fixed)
1163 {
1164 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001165 // columns when displaying the window, minus left border and
1166 // padding.
1167 if (-leftoff > left_extra)
1168 wp->w_leftcol = -leftoff - left_extra;
1169 wp->w_width -= wp->w_leftcol;
1170 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001171 if (wp->w_width < 0)
1172 wp->w_width = 0;
1173 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001174 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001175
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001176 if (wp->w_p_wrap || (!wp->w_popup_fixed
1177 && (wp->w_popup_pos == POPPOS_TOPLEFT
1178 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001179 {
1180 int want_col = 0;
1181
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001182 // try to show the right border and any scrollbar
1183 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001184 if (want_col > 0 && wp->w_wincol > 0
1185 && wp->w_wincol + want_col >= Columns)
1186 {
1187 wp->w_wincol = Columns - want_col;
1188 if (wp->w_wincol < 0)
1189 wp->w_wincol = 0;
1190 }
1191 }
1192
Bram Moolenaar8d241042019-06-12 23:40:01 +02001193 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1194 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001195 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1196 wp->w_height = wp->w_minheight;
1197 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1198 wp->w_height = wp->w_maxheight;
1199 if (wp->w_height > Rows - wp->w_winrow)
1200 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001201
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001202 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001203 {
1204 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1205 if (wp->w_winrow < 0)
1206 wp->w_winrow = 0;
1207 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001208 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1209 || wp->w_popup_pos == POPPOS_BOTLEFT)
1210 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001211 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001212 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001213 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001214 else
1215 // not enough space, make top aligned
1216 wp->w_winrow = wp->w_wantline + 1;
1217 }
1218
Bram Moolenaar17146962019-05-30 00:12:11 +02001219 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001220
1221 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001222 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001223 if (org_winrow != wp->w_winrow
1224 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001225 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001226 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001227 || org_width != wp->w_width
1228 || org_height != wp->w_height)
1229 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001230 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar356375f2019-08-24 14:46:29 +02001231 redraw_all_later(SOME_VALID);
1232 status_redraw_all();
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001233 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1234 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001235 popup_mask_refresh = TRUE;
1236 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001237}
1238
Bram Moolenaar17627312019-06-02 19:53:44 +02001239typedef enum
1240{
1241 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001242 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001243 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001244 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001245 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001246 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001247 TYPE_PREVIEW, // preview window
1248 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001249} create_type_T;
1250
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001251/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001252 * Make "buf" empty and set the contents to "text".
1253 * Used by popup_create() and popup_settext().
1254 */
1255 static void
1256popup_set_buffer_text(buf_T *buf, typval_T text)
1257{
1258 int lnum;
1259
1260 // Clear the buffer, then replace the lines.
1261 curbuf = buf;
1262 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1263 ml_delete(lnum, FALSE);
1264 curbuf = curwin->w_buffer;
1265
1266 // Add text to the buffer.
1267 if (text.v_type == VAR_STRING)
1268 {
1269 // just a string
1270 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1271 }
1272 else
1273 {
1274 list_T *l = text.vval.v_list;
1275
1276 if (l->lv_len > 0)
1277 {
1278 if (l->lv_first->li_tv.v_type == VAR_STRING)
1279 // list of strings
1280 add_popup_strings(buf, l);
1281 else
1282 // list of dictionaries
1283 add_popup_dicts(buf, l);
1284 }
1285 }
1286
1287 // delete the line that was in the empty buffer
1288 curbuf = buf;
1289 ml_delete(buf->b_ml.ml_line_count, FALSE);
1290 curbuf = curwin->w_buffer;
1291}
1292
1293/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001294 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1295 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001296 * Return FAIL if the parsing fails.
1297 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001298 static int
1299parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001300{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001301 char_u *p =
1302#ifdef FEAT_QUICKFIX
1303 !is_preview ? p_cpp :
1304#endif
1305 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001306
Bram Moolenaar258cef52019-08-21 17:29:29 +02001307 if (wp != NULL)
1308 wp->w_popup_flags &= ~POPF_INFO_MENU;
1309
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001310 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001311 {
1312 char_u *e, *dig;
1313 char_u *s = p;
1314 int x;
1315
1316 e = vim_strchr(p, ':');
1317 if (e == NULL || e[1] == NUL)
1318 return FAIL;
1319
1320 p = vim_strchr(e, ',');
1321 if (p == NULL)
1322 p = e + STRLEN(e);
1323 dig = e + 1;
1324 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001325
1326 if (STRNCMP(s, "height:", 7) == 0)
1327 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001328 if (dig != p)
1329 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001330 if (wp != NULL)
1331 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001332 if (is_preview)
1333 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001334 wp->w_maxheight = x;
1335 }
1336 }
1337 else if (STRNCMP(s, "width:", 6) == 0)
1338 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001339 if (dig != p)
1340 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001341 if (wp != NULL)
1342 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001343 if (is_preview)
1344 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001345 wp->w_maxwidth = x;
1346 }
1347 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001348 else if (STRNCMP(s, "highlight:", 10) == 0)
1349 {
1350 if (wp != NULL)
1351 {
1352 int c = *p;
1353
1354 *p = NUL;
1355 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1356 s + 10, OPT_FREE|OPT_LOCAL, 0);
1357 *p = c;
1358 }
1359 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001360 else if (STRNCMP(s, "border:", 7) == 0)
1361 {
1362 char_u *arg = s + 7;
1363 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1364 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1365 int i;
1366
1367 if (!on && !off)
1368 return FAIL;
1369 if (wp != NULL)
1370 {
1371 for (i = 0; i < 4; ++i)
1372 wp->w_popup_border[i] = on ? 1 : 0;
1373 if (off)
1374 // only show the X for close when there is a border
1375 wp->w_popup_close = POPCLOSE_NONE;
1376 }
1377 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001378 else if (STRNCMP(s, "align:", 6) == 0)
1379 {
1380 char_u *arg = s + 6;
1381 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1382 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1383
1384 if (!menu && !item)
1385 return FAIL;
1386 if (wp != NULL && menu)
1387 wp->w_popup_flags |= POPF_INFO_MENU;
1388 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001389 else
1390 return FAIL;
1391 }
1392 return OK;
1393}
1394
1395/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001396 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1397 * is not NULL.
1398 * Return FAIL if the parsing fails.
1399 */
1400 int
1401parse_previewpopup(win_T *wp)
1402{
1403 return parse_popup_option(wp, TRUE);
1404}
1405
1406/*
1407 * Parse the 'completepopup' option and apply the values to window "wp" if it
1408 * is not NULL.
1409 * Return FAIL if the parsing fails.
1410 */
1411 int
1412parse_completepopup(win_T *wp)
1413{
1414 return parse_popup_option(wp, FALSE);
1415}
1416
1417/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001418 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001419 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001420 */
1421 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001422popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001423{
1424 setcursor_mayforce(TRUE);
1425 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1426 if (wp->w_wantline == 0) // cursor in first line
1427 {
1428 wp->w_wantline = 2;
1429 wp->w_popup_pos = POPPOS_TOPLEFT;
1430 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001431
Bram Moolenaar79648732019-07-18 21:43:07 +02001432 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001433 if (wp->w_wantcol > Columns - width)
1434 {
1435 wp->w_wantcol = Columns - width;
1436 if (wp->w_wantcol < 1)
1437 wp->w_wantcol = 1;
1438 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001439 popup_adjust_position(wp);
1440}
1441
1442/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001443 * Set w_wantline and w_wantcol for the a given screen position.
1444 * Caller must take care of running into the window border.
1445 */
1446 void
1447popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1448{
1449 wp->w_wantline = row;
1450 wp->w_wantcol = col;
1451 popup_adjust_position(wp);
1452}
1453
1454/*
1455 * Add a border and lef&right padding.
1456 */
1457 static void
1458add_border_left_right_padding(win_T *wp)
1459{
1460 int i;
1461
1462 for (i = 0; i < 4; ++i)
1463 {
1464 wp->w_popup_border[i] = 1;
1465 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1466 }
1467}
1468
1469/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001470 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001471 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001472 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001473 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001474 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001475 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001476popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001477{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001478 win_T *wp;
1479 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001480 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001481 int new_buffer;
1482 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001483 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001484 int nr;
1485 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001486
Bram Moolenaar79648732019-07-18 21:43:07 +02001487 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001488 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001489 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001490 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001491 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001492 buf = buflist_findnr( argvars[0].vval.v_number);
1493 if (buf == NULL)
1494 {
1495 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1496 return NULL;
1497 }
1498 }
1499 else if (!(argvars[0].v_type == VAR_STRING
1500 && argvars[0].vval.v_string != NULL)
1501 && !(argvars[0].v_type == VAR_LIST
1502 && argvars[0].vval.v_list != NULL))
1503 {
1504 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001505 return NULL;
1506 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001507 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001508 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001509 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001510 return NULL;
1511 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001512 d = argvars[1].vval.v_dict;
1513 }
1514
1515 if (d != NULL)
1516 {
1517 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1518 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1519 else if (type == TYPE_NOTIFICATION)
1520 tabnr = -1; // notifications are global by default
1521 else
1522 tabnr = 0;
1523 if (tabnr > 0)
1524 {
1525 tp = find_tabpage(tabnr);
1526 if (tp == NULL)
1527 {
1528 semsg(_("E997: Tabpage not found: %d"), tabnr);
1529 return NULL;
1530 }
1531 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001532 }
1533
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001534 // Create the window and buffer.
1535 wp = win_alloc_popup_win();
1536 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001537 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001538 if (rettv != NULL)
1539 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001540 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001541 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001542
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001543 if (buf != NULL)
1544 {
1545 // use existing buffer
1546 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001547 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001548 buffer_ensure_loaded(buf);
1549 }
1550 else
1551 {
1552 // create a new buffer associated with the popup
1553 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001554 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001555 if (buf == NULL)
1556 return NULL;
1557 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001558
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001559 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001560
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001561 set_local_options_default(wp);
1562 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001563 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001564 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001565 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001566 buf->b_p_ul = -1; // no undo
1567 buf->b_p_swf = FALSE; // no swap file
1568 buf->b_p_bl = FALSE; // unlisted buffer
1569 buf->b_locked = TRUE;
1570 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001571
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001572 // Avoid that 'buftype' is reset when this buffer is entered.
1573 buf->b_p_initialized = TRUE;
1574 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001575
Bram Moolenaara3fce622019-06-20 02:31:49 +02001576 if (tp != NULL)
1577 {
1578 // popup on specified tab page
1579 wp->w_next = tp->tp_first_popupwin;
1580 tp->tp_first_popupwin = wp;
1581 }
1582 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001583 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001584 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001585 wp->w_next = curtab->tp_first_popupwin;
1586 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001587 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001588 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001589 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001590 win_T *prev = first_popupwin;
1591
1592 // Global popup: add at the end, so that it gets displayed on top of
1593 // older ones with the same zindex. Matters for notifications.
1594 if (first_popupwin == NULL)
1595 first_popupwin = wp;
1596 else
1597 {
1598 while (prev->w_next != NULL)
1599 prev = prev->w_next;
1600 prev->w_next = wp;
1601 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001602 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001603
Bram Moolenaar79648732019-07-18 21:43:07 +02001604 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001605 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001606
Bram Moolenaar79648732019-07-18 21:43:07 +02001607 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001608 {
1609 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001610 }
1611 if (type == TYPE_ATCURSOR)
1612 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001613 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001614 set_moved_values(wp);
1615 set_moved_columns(wp, FIND_STRING);
1616 }
1617
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001618 if (type == TYPE_BEVAL)
1619 {
1620 wp->w_popup_pos = POPPOS_BOTLEFT;
1621
1622 // by default use the mouse position
1623 wp->w_wantline = mouse_row;
1624 if (wp->w_wantline <= 0) // mouse on first line
1625 {
1626 wp->w_wantline = 2;
1627 wp->w_popup_pos = POPPOS_TOPLEFT;
1628 }
1629 wp->w_wantcol = mouse_col + 1;
1630 set_mousemoved_values(wp);
1631 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1632 }
1633
Bram Moolenaar33796b32019-06-08 16:01:13 +02001634 // set default values
1635 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001636 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001637
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001638 if (type == TYPE_NOTIFICATION)
1639 {
1640 win_T *twp, *nextwin;
1641 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001642
1643 // Try to not overlap with another global popup. Guess we need 3
1644 // more screen lines than buffer lines.
1645 wp->w_wantline = 1;
1646 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1647 {
1648 nextwin = twp->w_next;
1649 if (twp != wp
1650 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1651 && twp->w_winrow <= wp->w_wantline - 1 + height
1652 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1653 {
1654 // move to below this popup and restart the loop to check for
1655 // overlap with other popups
1656 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1657 nextwin = first_popupwin;
1658 }
1659 }
1660 if (wp->w_wantline + height > Rows)
1661 {
1662 // can't avoid overlap, put on top in the hope that message goes
1663 // away soon.
1664 wp->w_wantline = 1;
1665 }
1666
1667 wp->w_wantcol = 10;
1668 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001669 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001670 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001671 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001672 for (i = 0; i < 4; ++i)
1673 wp->w_popup_border[i] = 1;
1674 wp->w_popup_padding[1] = 1;
1675 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001676
1677 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001678 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001679 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1680 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001681 }
1682
Bram Moolenaara730e552019-06-16 19:05:31 +02001683 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001684 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001685 wp->w_popup_pos = POPPOS_CENTER;
1686 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001687 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001688 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001689 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001690 }
1691
Bram Moolenaara730e552019-06-16 19:05:31 +02001692 if (type == TYPE_MENU)
1693 {
1694 typval_T tv;
1695 callback_T callback;
1696
1697 tv.v_type = VAR_STRING;
1698 tv.vval.v_string = (char_u *)"popup_filter_menu";
1699 callback = get_callback(&tv);
1700 if (callback.cb_name != NULL)
1701 set_callback(&wp->w_filter_cb, &callback);
1702
1703 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001704 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001705 }
1706
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 if (type == TYPE_PREVIEW)
1708 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001709 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001710 wp->w_popup_close = POPCLOSE_BUTTON;
1711 for (i = 0; i < 4; ++i)
1712 wp->w_popup_border[i] = 1;
1713 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001714 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1715 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001716# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001717 if (type == TYPE_INFO)
1718 {
1719 wp->w_popup_pos = POPPOS_TOPLEFT;
1720 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1721 wp->w_popup_close = POPCLOSE_BUTTON;
1722 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001723 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001724 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001725# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001726
Bram Moolenaarae943152019-06-16 22:54:14 +02001727 for (i = 0; i < 4; ++i)
1728 VIM_CLEAR(wp->w_border_highlight[i]);
1729 for (i = 0; i < 8; ++i)
1730 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001731 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001732 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001733
Bram Moolenaar79648732019-07-18 21:43:07 +02001734 if (d != NULL)
1735 // Deal with options.
1736 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001737
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001738#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001739 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1740 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001741#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001742
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001743 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001744
1745 wp->w_vsep_width = 0;
1746
1747 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001748 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001749
1750 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001751}
1752
1753/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001754 * popup_clear()
1755 */
1756 void
1757f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1758{
1759 close_all_popups();
1760}
1761
1762/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001763 * popup_create({text}, {options})
1764 */
1765 void
1766f_popup_create(typval_T *argvars, typval_T *rettv)
1767{
Bram Moolenaar17627312019-06-02 19:53:44 +02001768 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001769}
1770
1771/*
1772 * popup_atcursor({text}, {options})
1773 */
1774 void
1775f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1776{
Bram Moolenaar17627312019-06-02 19:53:44 +02001777 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001778}
1779
1780/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001781 * popup_beval({text}, {options})
1782 */
1783 void
1784f_popup_beval(typval_T *argvars, typval_T *rettv)
1785{
1786 popup_create(argvars, rettv, TYPE_BEVAL);
1787}
1788
1789/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001790 * Invoke the close callback for window "wp" with value "result".
1791 * Careful: The callback may make "wp" invalid!
1792 */
1793 static void
1794invoke_popup_callback(win_T *wp, typval_T *result)
1795{
1796 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001797 typval_T argv[3];
1798
1799 argv[0].v_type = VAR_NUMBER;
1800 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1801
1802 if (result != NULL && result->v_type != VAR_UNKNOWN)
1803 copy_tv(result, &argv[1]);
1804 else
1805 {
1806 argv[1].v_type = VAR_NUMBER;
1807 argv[1].vval.v_number = 0;
1808 }
1809
1810 argv[2].v_type = VAR_UNKNOWN;
1811
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001812 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001813 if (result != NULL)
1814 clear_tv(&argv[1]);
1815 clear_tv(&rettv);
1816}
1817
1818/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001819 * Close popup "wp" and invoke any close callback for it.
1820 */
1821 static void
1822popup_close_and_callback(win_T *wp, typval_T *arg)
1823{
1824 int id = wp->w_id;
1825
1826 if (wp->w_close_cb.cb_name != NULL)
1827 // Careful: This may make "wp" invalid.
1828 invoke_popup_callback(wp, arg);
1829
1830 popup_close(id);
1831}
1832
1833/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001834 * Close popup "wp" because of a mouse click.
1835 */
1836 void
1837popup_close_for_mouse_click(win_T *wp)
1838{
1839 typval_T res;
1840
1841 res.v_type = VAR_NUMBER;
1842 res.vval.v_number = -2;
1843 popup_close_and_callback(wp, &res);
1844}
1845
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001846 static void
1847check_mouse_moved(win_T *wp, win_T *mouse_wp)
1848{
1849 // Close the popup when all if these are true:
1850 // - the mouse is not on this popup
1851 // - "mousemoved" was used
1852 // - the mouse is no longer on the same screen row or the mouse column is
1853 // outside of the relevant text
1854 if (wp != mouse_wp
1855 && wp->w_popup_mouse_row != 0
1856 && (wp->w_popup_mouse_row != mouse_row
1857 || mouse_col < wp->w_popup_mouse_mincol
1858 || mouse_col > wp->w_popup_mouse_maxcol))
1859 {
1860 typval_T res;
1861
1862 res.v_type = VAR_NUMBER;
1863 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001864 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001865 popup_close_and_callback(wp, &res);
1866 }
1867}
1868
1869/*
1870 * Called when the mouse moved: may close a popup with "mousemoved".
1871 */
1872 void
1873popup_handle_mouse_moved(void)
1874{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001875 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001876 win_T *mouse_wp;
1877 int row = mouse_row;
1878 int col = mouse_col;
1879
1880 // find the window where the mouse is in
1881 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1882
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001883 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1884 {
1885 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001886 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001887 }
1888 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1889 {
1890 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001891 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001892 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001893}
1894
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001895/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001896 * In a filter: check if the typed key is a mouse event that is used for
1897 * dragging the popup.
1898 */
1899 static void
1900filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1901{
1902 int row = mouse_row;
1903 int col = mouse_col;
1904
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001905 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001906 && is_mouse_key(c)
1907 && (wp == popup_dragwin
1908 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1909 // do not consume the key, allow for dragging the popup
1910 rettv->vval.v_number = 0;
1911}
1912
Bram Moolenaara730e552019-06-16 19:05:31 +02001913/*
1914 * popup_filter_menu({text}, {options})
1915 */
1916 void
1917f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1918{
1919 int id = tv_get_number(&argvars[0]);
1920 win_T *wp = win_id2wp(id);
1921 char_u *key = tv_get_string(&argvars[1]);
1922 typval_T res;
1923 int c;
1924 linenr_T old_lnum;
1925
1926 // If the popup has been closed do not consume the key.
1927 if (wp == NULL)
1928 return;
1929
1930 c = *key;
1931 if (c == K_SPECIAL && key[1] != NUL)
1932 c = TO_SPECIAL(key[1], key[2]);
1933
1934 // consume all keys until done
1935 rettv->vval.v_number = 1;
1936 res.v_type = VAR_NUMBER;
1937
1938 old_lnum = wp->w_cursor.lnum;
1939 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1940 --wp->w_cursor.lnum;
1941 if ((c == 'j' || c == 'J' || c == K_DOWN)
1942 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1943 ++wp->w_cursor.lnum;
1944 if (old_lnum != wp->w_cursor.lnum)
1945 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001946 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001947 return;
1948 }
1949
1950 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1951 {
1952 // Cancelled, invoke callback with -1
1953 res.vval.v_number = -1;
1954 popup_close_and_callback(wp, &res);
1955 return;
1956 }
1957 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1958 {
1959 // Invoke callback with current index.
1960 res.vval.v_number = wp->w_cursor.lnum;
1961 popup_close_and_callback(wp, &res);
1962 return;
1963 }
1964
1965 filter_handle_drag(wp, c, rettv);
1966}
1967
1968/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001969 * popup_filter_yesno({text}, {options})
1970 */
1971 void
1972f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1973{
1974 int id = tv_get_number(&argvars[0]);
1975 win_T *wp = win_id2wp(id);
1976 char_u *key = tv_get_string(&argvars[1]);
1977 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001978 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001979
1980 // If the popup has been closed don't consume the key.
1981 if (wp == NULL)
1982 return;
1983
Bram Moolenaara730e552019-06-16 19:05:31 +02001984 c = *key;
1985 if (c == K_SPECIAL && key[1] != NUL)
1986 c = TO_SPECIAL(key[1], key[2]);
1987
Bram Moolenaara42d9452019-06-15 21:46:30 +02001988 // consume all keys until done
1989 rettv->vval.v_number = 1;
1990
Bram Moolenaara730e552019-06-16 19:05:31 +02001991 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001992 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001993 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001994 res.vval.v_number = 0;
1995 else
1996 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001997 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001998 return;
1999 }
2000
2001 // Invoke callback
2002 res.v_type = VAR_NUMBER;
2003 popup_close_and_callback(wp, &res);
2004}
2005
2006/*
2007 * popup_dialog({text}, {options})
2008 */
2009 void
2010f_popup_dialog(typval_T *argvars, typval_T *rettv)
2011{
2012 popup_create(argvars, rettv, TYPE_DIALOG);
2013}
2014
2015/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002016 * popup_menu({text}, {options})
2017 */
2018 void
2019f_popup_menu(typval_T *argvars, typval_T *rettv)
2020{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002021 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002022}
2023
2024/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002025 * popup_notification({text}, {options})
2026 */
2027 void
2028f_popup_notification(typval_T *argvars, typval_T *rettv)
2029{
2030 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2031}
2032
2033/*
2034 * Find the popup window with window-ID "id".
2035 * If the popup window does not exist NULL is returned.
2036 * If the window is not a popup window, and error message is given.
2037 */
2038 static win_T *
2039find_popup_win(int id)
2040{
2041 win_T *wp = win_id2wp(id);
2042
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002043 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002044 {
2045 semsg(_("E993: window %d is not a popup window"), id);
2046 return NULL;
2047 }
2048 return wp;
2049}
2050
2051/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002052 * popup_close({id})
2053 */
2054 void
2055f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2056{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002057 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002058 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002059
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002060 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002061 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002062}
2063
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002064 static void
2065popup_hide(win_T *wp)
2066{
2067 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2068 {
2069 wp->w_popup_flags |= POPF_HIDDEN;
2070 --wp->w_buffer->b_nwindows;
2071 redraw_all_later(NOT_VALID);
2072 popup_mask_refresh = TRUE;
2073 }
2074}
2075
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002076/*
2077 * popup_hide({id})
2078 */
2079 void
2080f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2081{
2082 int id = (int)tv_get_number(argvars);
2083 win_T *wp = find_popup_win(id);
2084
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002085 if (wp != NULL)
2086 popup_hide(wp);
2087}
2088
2089 void
2090popup_show(win_T *wp)
2091{
2092 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002093 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002094 wp->w_popup_flags &= ~POPF_HIDDEN;
2095 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002096 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002097 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002098 }
2099}
2100
2101/*
2102 * popup_show({id})
2103 */
2104 void
2105f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2106{
2107 int id = (int)tv_get_number(argvars);
2108 win_T *wp = find_popup_win(id);
2109
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002110 if (wp != NULL)
2111 popup_show(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002112}
2113
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002114/*
2115 * popup_settext({id}, {text})
2116 */
2117 void
2118f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2119{
2120 int id = (int)tv_get_number(&argvars[0]);
2121 win_T *wp = find_popup_win(id);
2122
2123 if (wp != NULL)
2124 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002125 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2126 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2127 else
2128 {
2129 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002130 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002131 popup_adjust_position(wp);
2132 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002133 }
2134}
2135
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002136 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002137popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002138{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002139 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002140 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002141 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002142 clear_cmdline = TRUE;
2143 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002144
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002145 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002146 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002147}
2148
Bram Moolenaarec583842019-05-26 14:11:23 +02002149/*
2150 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002151 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002152 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002153 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002154popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002155{
2156 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002157 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002158 win_T *prev = NULL;
2159
Bram Moolenaarec583842019-05-26 14:11:23 +02002160 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002161 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002162 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002163 {
2164 if (prev == NULL)
2165 first_popupwin = wp->w_next;
2166 else
2167 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002168 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002169 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002170 }
2171
Bram Moolenaarec583842019-05-26 14:11:23 +02002172 // go through tab-local popups
2173 FOR_ALL_TABPAGES(tp)
2174 popup_close_tabpage(tp, id);
2175}
2176
2177/*
2178 * Close a popup window with Window-id "id" in tabpage "tp".
2179 */
2180 void
2181popup_close_tabpage(tabpage_T *tp, int id)
2182{
2183 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002184 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002185 win_T *prev = NULL;
2186
Bram Moolenaarec583842019-05-26 14:11:23 +02002187 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2188 if (wp->w_id == id)
2189 {
2190 if (prev == NULL)
2191 *root = wp->w_next;
2192 else
2193 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002194 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002195 return;
2196 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002197}
2198
2199 void
2200close_all_popups(void)
2201{
2202 while (first_popupwin != NULL)
2203 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002204 while (curtab->tp_first_popupwin != NULL)
2205 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002206}
2207
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002208/*
2209 * popup_move({id}, {options})
2210 */
2211 void
2212f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2213{
Bram Moolenaarae943152019-06-16 22:54:14 +02002214 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002215 int id = (int)tv_get_number(argvars);
2216 win_T *wp = find_popup_win(id);
2217
2218 if (wp == NULL)
2219 return; // invalid {id}
2220
2221 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2222 {
2223 emsg(_(e_dictreq));
2224 return;
2225 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002226 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002227
Bram Moolenaarae943152019-06-16 22:54:14 +02002228 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002229
2230 if (wp->w_winrow + wp->w_height >= cmdline_row)
2231 clear_cmdline = TRUE;
2232 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002233}
2234
Bram Moolenaarbc133542019-05-29 20:26:46 +02002235/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002236 * popup_setoptions({id}, {options})
2237 */
2238 void
2239f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2240{
2241 dict_T *dict;
2242 int id = (int)tv_get_number(argvars);
2243 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002244 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002245
2246 if (wp == NULL)
2247 return; // invalid {id}
2248
2249 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2250 {
2251 emsg(_(e_dictreq));
2252 return;
2253 }
2254 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002256
2257 apply_move_options(wp, dict);
2258 apply_general_options(wp, dict);
2259
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002260 if (old_firstline != wp->w_firstline)
2261 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002262 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002263 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002264 popup_adjust_position(wp);
2265}
2266
2267/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002268 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002269 */
2270 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002271f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002272{
2273 dict_T *dict;
2274 int id = (int)tv_get_number(argvars);
2275 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002276 int top_extra;
2277 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002278
2279 if (rettv_dict_alloc(rettv) == OK)
2280 {
2281 if (wp == NULL)
2282 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002283 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002284 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2285
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002286 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002287 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002288 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002289
Bram Moolenaarbc133542019-05-29 20:26:46 +02002290 dict_add_number(dict, "line", wp->w_winrow + 1);
2291 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002292 dict_add_number(dict, "width", wp->w_width + left_extra
2293 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2294 dict_add_number(dict, "height", wp->w_height + top_extra
2295 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002296
2297 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2298 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2299 dict_add_number(dict, "core_width", wp->w_width);
2300 dict_add_number(dict, "core_height", wp->w_height);
2301
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002302 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002303 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002304 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002305 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002306
2307 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002308 }
2309}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002310/*
2311 * popup_locate({row}, {col})
2312 */
2313 void
2314f_popup_locate(typval_T *argvars, typval_T *rettv)
2315{
2316 int row = tv_get_number(&argvars[0]) - 1;
2317 int col = tv_get_number(&argvars[1]) - 1;
2318 win_T *wp;
2319
2320 wp = mouse_find_win(&row, &col, FIND_POPUP);
2321 if (WIN_IS_POPUP(wp))
2322 rettv->vval.v_number = wp->w_id;
2323}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002324
2325/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002326 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2327 */
2328 static void
2329get_padding_border(dict_T *dict, int *array, char *name)
2330{
2331 list_T *list;
2332 int i;
2333
2334 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2335 return;
2336
2337 list = list_alloc();
2338 if (list != NULL)
2339 {
2340 dict_add_list(dict, name, list);
2341 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2342 for (i = 0; i < 4; ++i)
2343 list_append_number(list, array[i]);
2344 }
2345}
2346
2347/*
2348 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2349 */
2350 static void
2351get_borderhighlight(dict_T *dict, win_T *wp)
2352{
2353 list_T *list;
2354 int i;
2355
2356 for (i = 0; i < 4; ++i)
2357 if (wp->w_border_highlight[i] != NULL)
2358 break;
2359 if (i == 4)
2360 return;
2361
2362 list = list_alloc();
2363 if (list != NULL)
2364 {
2365 dict_add_list(dict, "borderhighlight", list);
2366 for (i = 0; i < 4; ++i)
2367 list_append_string(list, wp->w_border_highlight[i], -1);
2368 }
2369}
2370
2371/*
2372 * For popup_getoptions(): add a "borderchars" entry to "dict".
2373 */
2374 static void
2375get_borderchars(dict_T *dict, win_T *wp)
2376{
2377 list_T *list;
2378 int i;
2379 char_u buf[NUMBUFLEN];
2380 int len;
2381
2382 for (i = 0; i < 8; ++i)
2383 if (wp->w_border_char[i] != 0)
2384 break;
2385 if (i == 8)
2386 return;
2387
2388 list = list_alloc();
2389 if (list != NULL)
2390 {
2391 dict_add_list(dict, "borderchars", list);
2392 for (i = 0; i < 8; ++i)
2393 {
2394 len = mb_char2bytes(wp->w_border_char[i], buf);
2395 list_append_string(list, buf, len);
2396 }
2397 }
2398}
2399
2400/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002401 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002402 */
2403 static void
2404get_moved_list(dict_T *dict, win_T *wp)
2405{
2406 list_T *list;
2407
2408 list = list_alloc();
2409 if (list != NULL)
2410 {
2411 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002412 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002413 list_append_number(list, wp->w_popup_mincol);
2414 list_append_number(list, wp->w_popup_maxcol);
2415 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002416 list = list_alloc();
2417 if (list != NULL)
2418 {
2419 dict_add_list(dict, "mousemoved", list);
2420 list_append_number(list, wp->w_popup_mouse_row);
2421 list_append_number(list, wp->w_popup_mouse_mincol);
2422 list_append_number(list, wp->w_popup_mouse_maxcol);
2423 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002424}
2425
2426/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002427 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002428 */
2429 void
2430f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2431{
2432 dict_T *dict;
2433 int id = (int)tv_get_number(argvars);
2434 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002435 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002436 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002437
2438 if (rettv_dict_alloc(rettv) == OK)
2439 {
2440 if (wp == NULL)
2441 return;
2442
2443 dict = rettv->vval.v_dict;
2444 dict_add_number(dict, "line", wp->w_wantline);
2445 dict_add_number(dict, "col", wp->w_wantcol);
2446 dict_add_number(dict, "minwidth", wp->w_minwidth);
2447 dict_add_number(dict, "minheight", wp->w_minheight);
2448 dict_add_number(dict, "maxheight", wp->w_maxheight);
2449 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002450 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002451 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002452 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002453 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002454 dict_add_string(dict, "title", wp->w_popup_title);
2455 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002456 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02002457 dict_add_number(dict, "mapping", (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002458 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002459 dict_add_number(dict, "cursorline",
2460 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002461 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002462 if (wp->w_scrollbar_highlight != NULL)
2463 dict_add_string(dict, "scrollbarhighlight",
2464 wp->w_scrollbar_highlight);
2465 if (wp->w_thumb_highlight != NULL)
2466 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002467
Bram Moolenaara3fce622019-06-20 02:31:49 +02002468 // find the tabpage that holds this popup
2469 i = 1;
2470 FOR_ALL_TABPAGES(tp)
2471 {
2472 win_T *p;
2473
2474 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2475 if (p->w_id == id)
2476 break;
2477 if (p != NULL)
2478 break;
2479 ++i;
2480 }
2481 if (tp == NULL)
2482 i = -1; // must be global
2483 else if (tp == curtab)
2484 i = 0;
2485 dict_add_number(dict, "tabpage", i);
2486
Bram Moolenaarae943152019-06-16 22:54:14 +02002487 get_padding_border(dict, wp->w_popup_padding, "padding");
2488 get_padding_border(dict, wp->w_popup_border, "border");
2489 get_borderhighlight(dict, wp);
2490 get_borderchars(dict, wp);
2491 get_moved_list(dict, wp);
2492
2493 if (wp->w_filter_cb.cb_name != NULL)
2494 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2495 if (wp->w_close_cb.cb_name != NULL)
2496 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002497
2498 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2499 ++i)
2500 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2501 {
2502 dict_add_string(dict, "pos",
2503 (char_u *)poppos_entries[i].pp_name);
2504 break;
2505 }
2506
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002507 dict_add_string(dict, "close", (char_u *)(
2508 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2509 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2510
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002511# if defined(FEAT_TIMERS)
2512 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2513 ? (long)wp->w_popup_timer->tr_interval : 0L);
2514# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002515 }
2516}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002517
2518 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002519error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002520{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002521 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002522 {
2523 emsg(_("E994: Not allowed in a popup window"));
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002529/*
2530 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002531 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002532 */
2533 void
2534popup_reset_handled()
2535{
2536 win_T *wp;
2537
2538 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2539 wp->w_popup_flags &= ~POPF_HANDLED;
2540 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2541 wp->w_popup_flags &= ~POPF_HANDLED;
2542}
2543
2544/*
2545 * Find the next visible popup where POPF_HANDLED is not set.
2546 * Must have called popup_reset_handled() first.
2547 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2548 * popup with the highest zindex.
2549 */
2550 win_T *
2551find_next_popup(int lowest)
2552{
2553 win_T *wp;
2554 win_T *found_wp;
2555 int found_zindex;
2556
2557 found_zindex = lowest ? INT_MAX : 0;
2558 found_wp = NULL;
2559 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2560 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2561 && (lowest ? wp->w_zindex < found_zindex
2562 : wp->w_zindex > found_zindex))
2563 {
2564 found_zindex = wp->w_zindex;
2565 found_wp = wp;
2566 }
2567 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2568 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2569 && (lowest ? wp->w_zindex < found_zindex
2570 : wp->w_zindex > found_zindex))
2571 {
2572 found_zindex = wp->w_zindex;
2573 found_wp = wp;
2574 }
2575
2576 if (found_wp != NULL)
2577 found_wp->w_popup_flags |= POPF_HANDLED;
2578 return found_wp;
2579}
2580
2581/*
2582 * Invoke the filter callback for window "wp" with typed character "c".
2583 * Uses the global "mod_mask" for modifiers.
2584 * Returns the return value of the filter.
2585 * Careful: The filter may make "wp" invalid!
2586 */
2587 static int
2588invoke_popup_filter(win_T *wp, int c)
2589{
2590 int res;
2591 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002592 typval_T argv[3];
2593 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002594 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002595
Bram Moolenaara42d9452019-06-15 21:46:30 +02002596 // Emergency exit: CTRL-C closes the popup.
2597 if (c == Ctrl_C)
2598 {
2599 rettv.v_type = VAR_NUMBER;
2600 rettv.vval.v_number = -1;
2601 popup_close_and_callback(wp, &rettv);
2602 return 1;
2603 }
2604
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002605 argv[0].v_type = VAR_NUMBER;
2606 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2607
2608 // Convert the number to a string, so that the function can use:
2609 // if a:c == "\<F2>"
2610 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2611 argv[1].v_type = VAR_STRING;
2612 argv[1].vval.v_string = vim_strsave(buf);
2613
2614 argv[2].v_type = VAR_UNKNOWN;
2615
Bram Moolenaara42d9452019-06-15 21:46:30 +02002616 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002617 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002618 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002619 popup_highlight_curline(wp);
2620
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002621 res = tv_get_number(&rettv);
2622 vim_free(argv[1].vval.v_string);
2623 clear_tv(&rettv);
2624 return res;
2625}
2626
2627/*
2628 * Called when "c" was typed: invoke popup filter callbacks.
2629 * Returns TRUE when the character was consumed,
2630 */
2631 int
2632popup_do_filter(int c)
2633{
2634 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002635 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002636
2637 popup_reset_handled();
2638
2639 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2640 if (wp->w_filter_cb.cb_name != NULL)
2641 res = invoke_popup_filter(wp, c);
2642
2643 return res;
2644}
2645
Bram Moolenaar3397f742019-06-02 18:40:06 +02002646/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002647 * Return TRUE if there is a popup visible with a filter callback and the
2648 * "mapping" property off.
2649 */
2650 int
2651popup_no_mapping(void)
2652{
2653 int round;
2654 win_T *wp;
2655
2656 for (round = 1; round <= 2; ++round)
2657 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2658 wp != NULL; wp = wp->w_next)
2659 if (wp->w_filter_cb.cb_name != NULL
2660 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2661 return TRUE;
2662 return FALSE;
2663}
2664
2665/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002666 * Called when the cursor moved: check if any popup needs to be closed if the
2667 * cursor moved far enough.
2668 */
2669 void
2670popup_check_cursor_pos()
2671{
2672 win_T *wp;
2673 typval_T tv;
2674
2675 popup_reset_handled();
2676 while ((wp = find_next_popup(TRUE)) != NULL)
2677 if (wp->w_popup_curwin != NULL
2678 && (curwin != wp->w_popup_curwin
2679 || curwin->w_cursor.lnum != wp->w_popup_lnum
2680 || curwin->w_cursor.col < wp->w_popup_mincol
2681 || curwin->w_cursor.col > wp->w_popup_maxcol))
2682 {
2683 tv.v_type = VAR_NUMBER;
2684 tv.vval.v_number = -1;
2685 popup_close_and_callback(wp, &tv);
2686 }
2687}
2688
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002689/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002690 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002691 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002692 static void
2693popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002694{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002695 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002696 char_u *cells;
2697 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002698
2699 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002700 return;
2701 if (wp->w_popup_mask_cells != NULL
2702 && wp->w_popup_mask_height == height
2703 && wp->w_popup_mask_width == width)
2704 return; // cache is still valid
2705
2706 vim_free(wp->w_popup_mask_cells);
2707 wp->w_popup_mask_cells = alloc_clear(width * height);
2708 if (wp->w_popup_mask_cells == NULL)
2709 return;
2710 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002711
2712 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2713 {
2714 int cols, cole;
2715 int lines, linee;
2716
2717 li = lio->li_tv.vval.v_list->lv_first;
2718 cols = tv_get_number(&li->li_tv);
2719 if (cols < 0)
2720 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002721 li = li->li_next;
2722 cole = tv_get_number(&li->li_tv);
2723 if (cole < 0)
2724 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002725 li = li->li_next;
2726 lines = tv_get_number(&li->li_tv);
2727 if (lines < 0)
2728 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002729 li = li->li_next;
2730 linee = tv_get_number(&li->li_tv);
2731 if (linee < 0)
2732 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002733
2734 for (row = lines - 1; row < linee && row < height; ++row)
2735 for (col = cols - 1; col < cole && col < width; ++col)
2736 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002737 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002738}
2739
2740/*
2741 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2742 * "col" and "line" are screen coordinates.
2743 */
2744 static int
2745popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2746{
2747 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2748 int line = screenline - wp->w_winrow;
2749
2750 return col >= 0 && col < width
2751 && line >= 0 && line < height
2752 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002753}
2754
2755/*
2756 * Set flags in popup_transparent[] for window "wp" to "val".
2757 */
2758 static void
2759update_popup_transparent(win_T *wp, int val)
2760{
2761 if (wp->w_popup_mask != NULL)
2762 {
2763 int width = popup_width(wp);
2764 int height = popup_height(wp);
2765 listitem_T *lio, *li;
2766 int cols, cole;
2767 int lines, linee;
2768 int col, line;
2769
2770 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2771 {
2772 li = lio->li_tv.vval.v_list->lv_first;
2773 cols = tv_get_number(&li->li_tv);
2774 if (cols < 0)
2775 cols = width + cols + 1;
2776 li = li->li_next;
2777 cole = tv_get_number(&li->li_tv);
2778 if (cole < 0)
2779 cole = width + cole + 1;
2780 li = li->li_next;
2781 lines = tv_get_number(&li->li_tv);
2782 if (lines < 0)
2783 lines = height + lines + 1;
2784 li = li->li_next;
2785 linee = tv_get_number(&li->li_tv);
2786 if (linee < 0)
2787 linee = height + linee + 1;
2788
2789 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002790 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002791 if (cols < 0)
2792 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002793 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002794 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002795 if (lines < 0)
2796 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002797 for (line = lines; line < linee
2798 && line + wp->w_winrow < screen_Rows; ++line)
2799 for (col = cols; col < cole
2800 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002801 popup_transparent[(line + wp->w_winrow) * screen_Columns
2802 + col + wp->w_wincol] = val;
2803 }
2804 }
2805}
2806
2807/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002808 * Update "popup_mask" if needed.
2809 * Also recomputes the popup size and positions.
2810 * Also updates "popup_visible".
2811 * Also marks window lines for redrawing.
2812 */
2813 void
2814may_update_popup_mask(int type)
2815{
2816 win_T *wp;
2817 short *mask;
2818 int line, col;
2819 int redraw_all = FALSE;
2820
2821 // Need to recompute when switching tabs.
2822 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2823 // (such as the screen size) must have changed.
2824 if (popup_mask_tab != curtab || type >= NOT_VALID)
2825 {
2826 popup_mask_refresh = TRUE;
2827 redraw_all = TRUE;
2828 }
2829 if (!popup_mask_refresh)
2830 {
2831 // Check if any buffer has changed.
2832 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2833 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2834 popup_mask_refresh = TRUE;
2835 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2836 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2837 popup_mask_refresh = TRUE;
2838 if (!popup_mask_refresh)
2839 return;
2840 }
2841
2842 // Need to update the mask, something has changed.
2843 popup_mask_refresh = FALSE;
2844 popup_mask_tab = curtab;
2845 popup_visible = FALSE;
2846
2847 // If redrawing everything, just update "popup_mask".
2848 // If redrawing only what is needed, update "popup_mask_next" and then
2849 // compare with "popup_mask" to see what changed.
2850 if (type >= SOME_VALID)
2851 mask = popup_mask;
2852 else
2853 mask = popup_mask_next;
2854 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2855
2856 // Find the window with the lowest zindex that hasn't been handled yet,
2857 // so that the window with a higher zindex overwrites the value in
2858 // popup_mask.
2859 popup_reset_handled();
2860 while ((wp = find_next_popup(TRUE)) != NULL)
2861 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002862 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002863 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002864
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002865 popup_visible = TRUE;
2866
2867 // Recompute the position if the text changed.
2868 if (redraw_all
2869 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2870 popup_adjust_position(wp);
2871
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002872 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002873 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002874 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002875 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002876 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002877 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002878 col < wp->w_wincol + width - wp->w_popup_leftoff
2879 && col < screen_Columns; ++col)
2880 if (wp->w_popup_mask_cells == NULL
2881 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002882 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002883 }
2884
2885 // Only check which lines are to be updated if not already
2886 // updating all lines.
2887 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002888 {
2889 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2890 win_T *prev_wp = NULL;
2891
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002892 for (line = 0; line < screen_Rows; ++line)
2893 {
2894 int col_done = 0;
2895
2896 for (col = 0; col < screen_Columns; ++col)
2897 {
2898 int off = line * screen_Columns + col;
2899
2900 if (popup_mask[off] != popup_mask_next[off])
2901 {
2902 popup_mask[off] = popup_mask_next[off];
2903
2904 if (line >= cmdline_row)
2905 {
2906 // the command line needs to be cleared if text below
2907 // the popup is now visible.
2908 if (!msg_scrolled && popup_mask_next[off] == 0)
2909 clear_cmdline = TRUE;
2910 }
2911 else if (col >= col_done)
2912 {
2913 linenr_T lnum;
2914 int line_cp = line;
2915 int col_cp = col;
2916
2917 // The screen position "line" / "col" needs to be
2918 // redrawn. Figure out what window that is and update
2919 // w_redraw_top and w_redr_bot. Only needs to be done
2920 // once for each window line.
2921 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2922 if (wp != NULL)
2923 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002924 if (wp != prev_wp)
2925 {
2926 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2927 prev_wp = wp;
2928 }
2929
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002930 if (line_cp >= wp->w_height)
2931 // In (or below) status line
2932 wp->w_redr_status = TRUE;
2933 // compute the position in the buffer line from the
2934 // position on the screen
2935 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002936 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002937 // past bottom
2938 wp->w_redr_status = TRUE;
2939 else
2940 redrawWinline(wp, lnum);
2941
2942 // This line is going to be redrawn, no need to
2943 // check until the right side of the window.
2944 col_done = wp->w_wincol + wp->w_width - 1;
2945 }
2946 }
2947 }
2948 }
2949 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002950
2951 vim_free(plines_cache);
2952 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002953}
2954
2955/*
2956 * Return a string of "len" spaces in IObuff.
2957 */
2958 static char_u *
2959get_spaces(int len)
2960{
2961 vim_memset(IObuff, ' ', (size_t)len);
2962 IObuff[len] = NUL;
2963 return IObuff;
2964}
2965
2966/*
2967 * Update popup windows. They are drawn on top of normal windows.
2968 * "win_update" is called for each popup window, lowest zindex first.
2969 */
2970 void
2971update_popups(void (*win_update)(win_T *wp))
2972{
2973 win_T *wp;
2974 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002975 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002976 int total_width;
2977 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002978 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002979 int popup_attr;
2980 int border_attr[4];
2981 int border_char[8];
2982 char_u buf[MB_MAXBYTES];
2983 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002984 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002985 int padcol = 0;
2986 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002987 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002988 int sb_thumb_top = 0;
2989 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002990 int attr_scroll = 0;
2991 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002992
2993 // Find the window with the lowest zindex that hasn't been updated yet,
2994 // so that the window with a higher zindex is drawn later, thus goes on
2995 // top.
2996 popup_reset_handled();
2997 while ((wp = find_next_popup(TRUE)) != NULL)
2998 {
2999 // This drawing uses the zindex of the popup window, so that it's on
3000 // top of the text but doesn't draw when another popup with higher
3001 // zindex is on top of the character.
3002 screen_zindex = wp->w_zindex;
3003
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003004 // Set flags in popup_transparent[] for masked cells.
3005 update_popup_transparent(wp, 1);
3006
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003007 // adjust w_winrow and w_wincol for border and padding, since
3008 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003009 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003010 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3011 - wp->w_popup_leftoff;
3012 if (wp->w_wincol + left_extra < 0)
3013 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003014 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003015 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003016
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003017 // Draw the popup text, unless it's off screen.
3018 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
3019 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003020
3021 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003022 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003023
Bram Moolenaard529ba52019-07-02 23:13:53 +02003024 total_width = popup_width(wp);
3025 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003026 popup_attr = get_wcr_attr(wp);
3027
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003028 if (wp->w_winrow + total_height > cmdline_row)
3029 wp->w_popup_flags |= POPF_ON_CMDLINE;
3030 else
3031 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3032
3033
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003034 // We can only use these line drawing characters when 'encoding' is
3035 // "utf-8" and 'ambiwidth' is "single".
3036 if (enc_utf8 && *p_ambw == 's')
3037 {
3038 border_char[0] = border_char[2] = 0x2550;
3039 border_char[1] = border_char[3] = 0x2551;
3040 border_char[4] = 0x2554;
3041 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003042 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3043 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003044 border_char[7] = 0x255a;
3045 }
3046 else
3047 {
3048 border_char[0] = border_char[2] = '-';
3049 border_char[1] = border_char[3] = '|';
3050 for (i = 4; i < 8; ++i)
3051 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003052 if (wp->w_popup_flags & POPF_RESIZE)
3053 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003054 }
3055 for (i = 0; i < 8; ++i)
3056 if (wp->w_border_char[i] != 0)
3057 border_char[i] = wp->w_border_char[i];
3058
3059 for (i = 0; i < 4; ++i)
3060 {
3061 border_attr[i] = popup_attr;
3062 if (wp->w_border_highlight[i] != NULL)
3063 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3064 }
3065
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003066 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003067 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003068 if (wp->w_popup_border[0] > 0)
3069 {
3070 // top border
3071 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003072 wincol < 0 ? 0 : wincol, wincol + total_width,
3073 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003074 ? border_char[4] : border_char[0],
3075 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003076 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003077 {
3078 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3079 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003080 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003081 }
3082 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003083 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3084 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003085
Bram Moolenaard529ba52019-07-02 23:13:53 +02003086 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3087 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003088 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003089 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3090 - wp->w_has_scrollbar;
3091 if (padcol < 0)
3092 {
3093 padwidth += padcol;
3094 padcol = 0;
3095 }
3096 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003097 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003098 {
3099 // top padding
3100 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003101 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003102 ' ', ' ', popup_attr);
3103 }
3104
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003105 // Title goes on top of border or padding.
3106 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003107 {
3108 int len = (int)STRLEN(wp->w_popup_title) + 1;
3109 char_u *title = alloc(len);
3110
3111 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3112 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003113 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003114 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003115
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003116 // Compute scrollbar thumb position and size.
3117 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003118 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003119 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3120
Bram Moolenaar68acb412019-06-26 03:40:36 +02003121 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3122 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003123 if (sb_thumb_height == 0)
3124 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003125 if (linecount <= wp->w_height)
3126 // it just fits, avoid divide by zero
3127 sb_thumb_top = 0;
3128 else
3129 sb_thumb_top = (wp->w_topline - 1
3130 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003131 * (wp->w_height - sb_thumb_height)
3132 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003133 if (wp->w_scrollbar_highlight != NULL)
3134 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3135 else
3136 attr_scroll = highlight_attr[HLF_PSB];
3137 if (wp->w_thumb_highlight != NULL)
3138 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3139 else
3140 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003141 }
3142
3143 for (i = wp->w_popup_border[0];
3144 i < total_height - wp->w_popup_border[2]; ++i)
3145 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003146 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003147 // left and right padding only needed next to the body
3148 int do_padding =
3149 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3150 && i < total_height - wp->w_popup_border[2]
3151 - wp->w_popup_padding[2];
3152
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003153 row = wp->w_winrow + i;
3154
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003155 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003156 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003157 {
3158 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003159 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003160 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003161 if (do_padding && wp->w_popup_padding[3] > 0)
3162 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003163 int col = wincol + wp->w_popup_border[3];
3164
Bram Moolenaard529ba52019-07-02 23:13:53 +02003165 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003166 pad_left = wp->w_popup_padding[3];
3167 if (col < 0)
3168 {
3169 pad_left += col;
3170 col = 0;
3171 }
3172 if (pad_left > 0)
3173 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3174 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003175 // scrollbar
3176 if (wp->w_has_scrollbar)
3177 {
3178 int line = i - top_off;
3179 int scroll_col = wp->w_wincol + total_width - 1
3180 - wp->w_popup_border[1];
3181
3182 if (line >= 0 && line < wp->w_height)
3183 screen_putchar(' ', row, scroll_col,
3184 line >= sb_thumb_top
3185 && line < sb_thumb_top + sb_thumb_height
3186 ? attr_thumb : attr_scroll);
3187 else
3188 screen_putchar(' ', row, scroll_col, popup_attr);
3189 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003190 // right border
3191 if (wp->w_popup_border[1] > 0)
3192 {
3193 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003194 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003195 }
3196 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003197 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003198 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003199 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003200 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3201 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003202 }
3203
3204 if (wp->w_popup_padding[2] > 0)
3205 {
3206 // bottom padding
3207 row = wp->w_winrow + wp->w_popup_border[0]
3208 + wp->w_popup_padding[0] + wp->w_height;
3209 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003210 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003211 }
3212
3213 if (wp->w_popup_border[2] > 0)
3214 {
3215 // bottom border
3216 row = wp->w_winrow + total_height - 1;
3217 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003218 wincol < 0 ? 0 : wincol,
3219 wincol + total_width,
3220 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003221 ? border_char[7] : border_char[2],
3222 border_char[2], border_attr[2]);
3223 if (wp->w_popup_border[1] > 0)
3224 {
3225 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003226 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003227 }
3228 }
3229
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003230 if (wp->w_popup_close == POPCLOSE_BUTTON)
3231 {
3232 // close button goes on top of anything at the top-right corner
3233 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003234 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003235 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3236 }
3237
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003238 update_popup_transparent(wp, 0);
3239
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003240 // Back to the normal zindex.
3241 screen_zindex = 0;
3242 }
3243}
3244
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003245/*
3246 * Mark references in callbacks of one popup window.
3247 */
3248 static int
3249set_ref_in_one_popup(win_T *wp, int copyID)
3250{
3251 int abort = FALSE;
3252 typval_T tv;
3253
3254 if (wp->w_close_cb.cb_partial != NULL)
3255 {
3256 tv.v_type = VAR_PARTIAL;
3257 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3258 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3259 }
3260 if (wp->w_filter_cb.cb_partial != NULL)
3261 {
3262 tv.v_type = VAR_PARTIAL;
3263 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3264 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3265 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003266 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003267 return abort;
3268}
3269
3270/*
3271 * Set reference in callbacks of popup windows.
3272 */
3273 int
3274set_ref_in_popups(int copyID)
3275{
3276 int abort = FALSE;
3277 win_T *wp;
3278 tabpage_T *tp;
3279
3280 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3281 abort = abort || set_ref_in_one_popup(wp, copyID);
3282
3283 FOR_ALL_TABPAGES(tp)
3284 {
3285 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3286 abort = abort || set_ref_in_one_popup(wp, copyID);
3287 if (abort)
3288 break;
3289 }
3290 return abort;
3291}
Bram Moolenaar79648732019-07-18 21:43:07 +02003292
3293/*
3294 * Find an existing popup used as the preview window, in the current tab page.
3295 * Return NULL if not found.
3296 */
3297 win_T *
3298popup_find_preview_window(void)
3299{
3300 win_T *wp;
3301
3302 // Preview window popup is always local to tab page.
3303 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3304 if (wp->w_p_pvw)
3305 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003306 return NULL;
3307}
3308
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003309 int
3310popup_is_popup(win_T *wp)
3311{
3312 return wp->w_popup_flags != 0;
3313}
3314
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003315#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003316/*
3317 * Find an existing popup used as the info window, in the current tab page.
3318 * Return NULL if not found.
3319 */
3320 win_T *
3321popup_find_info_window(void)
3322{
3323 win_T *wp;
3324
3325 // info window popup is always local to tab page.
3326 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3327 if (wp->w_popup_flags & POPF_INFO)
3328 return wp;
3329 return NULL;
3330}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003331#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003332
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003333 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003334f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3335{
3336 win_T *wp = popup_find_info_window();
3337
3338 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3339}
3340
3341 void
3342f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003343{
3344 win_T *wp = popup_find_preview_window();
3345
3346 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003347}
3348
Bram Moolenaar79648732019-07-18 21:43:07 +02003349/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003350 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003351 * NOTE: this makes the popup the current window, so that the file can be
3352 * edited. However, it must not remain to be the current window, the caller
3353 * must make sure of that.
3354 */
3355 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003356popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003357{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003358 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003359
3360 if (wp == NULL)
3361 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003362 if (info)
3363 wp->w_popup_flags |= POPF_INFO;
3364 else
3365 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003366
3367 // Set the width to a reasonable value, so that w_topline can be computed.
3368 if (wp->w_minwidth > 0)
3369 wp->w_width = wp->w_minwidth;
3370 else if (wp->w_maxwidth > 0)
3371 wp->w_width = wp->w_maxwidth;
3372 else
3373 wp->w_width = curwin->w_width;
3374
3375 // Will switch to another buffer soon, dummy one can be wiped.
3376 wp->w_buffer->b_locked = FALSE;
3377
3378 win_enter(wp, FALSE);
3379 return OK;
3380}
3381
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003382#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003383/*
3384 * Close any preview popup.
3385 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003386 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003387popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003388{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003389 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003390
3391 if (wp != NULL)
3392 {
3393 typval_T res;
3394
3395 res.v_type = VAR_NUMBER;
3396 res.vval.v_number = -1;
3397 popup_close_and_callback(wp, &res);
3398 }
3399}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003400
3401/*
3402 * Hide the info popup.
3403 */
3404 void
3405popup_hide_info(void)
3406{
3407 win_T *wp = popup_find_info_window();
3408
3409 if (wp != NULL)
3410 popup_hide(wp);
3411}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003412#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003413
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003414/*
3415 * Set the title of the popup window to the file name.
3416 */
3417 void
3418popup_set_title(win_T *wp)
3419{
3420 if (wp->w_buffer->b_fname != NULL)
3421 {
3422 char_u dirname[MAXPATHL];
3423 size_t len;
3424
3425 mch_dirname(dirname, MAXPATHL);
3426 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3427
3428 vim_free(wp->w_popup_title);
3429 len = STRLEN(wp->w_buffer->b_fname) + 3;
3430 wp->w_popup_title = alloc((int)len);
3431 if (wp->w_popup_title != NULL)
3432 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3433 wp->w_buffer->b_fname);
3434 redraw_win_later(wp, VALID);
3435 }
3436}
3437
3438/*
3439 * If there is a preview window, update the title.
3440 * Used after changing directory.
3441 */
3442 void
3443popup_update_preview_title(void)
3444{
3445 win_T *wp = popup_find_preview_window();
3446
3447 if (wp != NULL)
3448 popup_set_title(wp);
3449}
3450
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003451#endif // FEAT_TEXT_PROP