blob: ed01693d51a40bc9a4c144705ab456ab0969a47a [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 Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200171 * Used when popup options contain "mousemoved": set default moved values.
172 */
173 static void
174set_mousemoved_values(win_T *wp)
175{
176 wp->w_popup_mouse_row = mouse_row;
177 wp->w_popup_mouse_mincol = mouse_col;
178 wp->w_popup_mouse_maxcol = mouse_col;
179}
180
181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
223 * Return TRUE if "row"/"col" is on the "X" button of the popup.
224 * The values are relative to the top-left corner.
225 * Caller should check w_popup_close is POPCLOSE_BUTTON.
226 */
227 int
228popup_on_X_button(win_T *wp, int row, int col)
229{
230 return row == 0 && col == popup_width(wp) - 1;
231}
232
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200233// Values set when dragging a popup window starts.
234static int drag_start_row;
235static int drag_start_col;
236static int drag_start_wantline;
237static int drag_start_wantcol;
238
239/*
240 * Mouse down on border of popup window: start dragging it.
241 * Uses mouse_col and mouse_row.
242 */
243 void
244popup_start_drag(win_T *wp)
245{
246 drag_start_row = mouse_row;
247 drag_start_col = mouse_col;
248 // TODO: handle using different corner
249 if (wp->w_wantline == 0)
250 drag_start_wantline = wp->w_winrow + 1;
251 else
252 drag_start_wantline = wp->w_wantline;
253 if (wp->w_wantcol == 0)
254 drag_start_wantcol = wp->w_wincol + 1;
255 else
256 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200257
258 // Stop centering the popup
259 if (wp->w_popup_pos == POPPOS_CENTER)
260 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261}
262
263/*
264 * Mouse moved while dragging a popup window: adjust the window popup position.
265 */
266 void
267popup_drag(win_T *wp)
268{
269 // The popup may be closed before dragging stops.
270 if (!win_valid_popup(wp))
271 return;
272
273 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
274 if (wp->w_wantline < 1)
275 wp->w_wantline = 1;
276 if (wp->w_wantline > Rows)
277 wp->w_wantline = Rows;
278 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
279 if (wp->w_wantcol < 1)
280 wp->w_wantcol = 1;
281 if (wp->w_wantcol > Columns)
282 wp->w_wantcol = Columns;
283
284 popup_adjust_position(wp);
285}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200286
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200287/*
288 * Set w_firstline to match the current "wp->w_topline".
289 */
290 void
291popup_set_firstline(win_T *wp)
292{
293 int height = wp->w_height;
294
295 wp->w_firstline = wp->w_topline;
296 popup_adjust_position(wp);
297
298 // we don't want the popup to get smaller, decrement the first line
299 // until it doesn't
300 while (wp->w_firstline > 1 && wp->w_height < height)
301 {
302 --wp->w_firstline;
303 popup_adjust_position(wp);
304 }
305}
306
307/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200308 * Return TRUE if the position is in the popup window scrollbar.
309 */
310 int
311popup_is_in_scrollbar(win_T *wp, int row, int col)
312{
313 return wp->w_has_scrollbar
314 && row >= wp->w_popup_border[0]
315 && row < popup_height(wp) - wp->w_popup_border[2]
316 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
317}
318
319
320/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200321 * Handle a click in a popup window, if it is in the scrollbar.
322 */
323 void
324popup_handle_scrollbar_click(win_T *wp, int row, int col)
325{
326 int height = popup_height(wp);
327 int old_topline = wp->w_topline;
328
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200329 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200330 {
331 if (row >= height / 2)
332 {
333 // Click in lower half, scroll down.
334 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
335 ++wp->w_topline;
336 }
337 else if (wp->w_topline > 1)
338 // click on upper half, scroll up.
339 --wp->w_topline;
340 if (wp->w_topline != old_topline)
341 {
342 popup_set_firstline(wp);
343 redraw_win_later(wp, NOT_VALID);
344 }
345 }
346}
347
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200348#if defined(FEAT_TIMERS)
349 static void
350popup_add_timeout(win_T *wp, int time)
351{
352 char_u cbbuf[50];
353 char_u *ptr = cbbuf;
354 typval_T tv;
355
356 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
357 "{_ -> popup_close(%d)}", wp->w_id);
358 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
359 {
360 wp->w_popup_timer = create_timer(time, 0);
361 wp->w_popup_timer->tr_callback = get_callback(&tv);
362 clear_tv(&tv);
363 }
364}
365#endif
366
Bram Moolenaar17627312019-06-02 19:53:44 +0200367/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200368 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200369 */
370 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200371apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200372{
Bram Moolenaarae943152019-06-16 22:54:14 +0200373 int nr;
374
375 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
376 wp->w_minwidth = nr;
377 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
378 wp->w_minheight = nr;
379 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
380 wp->w_maxwidth = nr;
381 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
382 wp->w_maxheight = nr;
383 get_pos_options(wp, d);
384}
385
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200386 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200387handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
388{
389 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
390 {
391 char_u *s = di->di_tv.vval.v_string;
392 int flags = 0;
393
394 if (STRCMP(s, "word") == 0)
395 flags = FIND_IDENT | FIND_STRING;
396 else if (STRCMP(s, "WORD") == 0)
397 flags = FIND_STRING;
398 else if (STRCMP(s, "expr") == 0)
399 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
400 else if (STRCMP(s, "any") != 0)
401 semsg(_(e_invarg2), s);
402 if (flags != 0)
403 {
404 if (mousemoved)
405 set_mousemoved_columns(wp, flags);
406 else
407 set_moved_columns(wp, flags);
408 }
409 }
410 else if (di->di_tv.v_type == VAR_LIST
411 && di->di_tv.vval.v_list != NULL
412 && di->di_tv.vval.v_list->lv_len == 2)
413 {
414 list_T *l = di->di_tv.vval.v_list;
415 int mincol = tv_get_number(&l->lv_first->li_tv);
416 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
417
418 if (mousemoved)
419 {
420 wp->w_popup_mouse_mincol = mincol;
421 wp->w_popup_mouse_maxcol = maxcol;
422 }
423 else
424 {
425 wp->w_popup_mincol = mincol;
426 wp->w_popup_maxcol = maxcol;
427 }
428 }
429 else
430 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
431}
432
433 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200434check_highlight(dict_T *dict, char *name, char_u **pval)
435{
436 dictitem_T *di;
437 char_u *str;
438
439 di = dict_find(dict, (char_u *)name, -1);
440 if (di != NULL)
441 {
442 if (di->di_tv.v_type != VAR_STRING)
443 semsg(_(e_invargval), name);
444 else
445 {
446 str = tv_get_string(&di->di_tv);
447 if (*str != NUL)
448 *pval = vim_strsave(str);
449 }
450 }
451}
452
Bram Moolenaarae943152019-06-16 22:54:14 +0200453/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200454 * Scroll to show the line with the cursor. This assumes lines don't wrap.
455 */
456 static void
457popup_show_curline(win_T *wp)
458{
459 if (wp->w_cursor.lnum < wp->w_topline)
460 wp->w_topline = wp->w_cursor.lnum;
461 else if (wp->w_cursor.lnum >= wp->w_botline)
462 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200463
464 // Don't use "firstline" now.
465 wp->w_firstline = 0;
466}
467
468/*
469 * Get the sign group name for window "wp".
470 * Returns a pointer to a static buffer, overwritten on the next call.
471 */
472 static char_u *
473popup_get_sign_name(win_T *wp)
474{
475 static char buf[30];
476
477 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
478 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200479}
480
481/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200482 * Highlight the line with the cursor.
483 * Also scrolls the text to put the cursor line in view.
484 */
485 static void
486popup_highlight_curline(win_T *wp)
487{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200488 int sign_id = 0;
489 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200490
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200491 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200492
493 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
494 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200495 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200496
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200497 if (!sign_exists_by_name(sign_name))
498 {
499 char *linehl = "PopupSelected";
500
501 if (syn_name2id((char_u *)linehl) == 0)
502 linehl = "PmenuSel";
503 sign_define_by_name(sign_name, NULL,
504 (char_u *)linehl, NULL, NULL);
505 }
506
507 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
508 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
509 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200510 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200511 else
512 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200513}
514
515/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 * Shared between popup_create() and f_popup_setoptions().
517 */
518 static void
519apply_general_options(win_T *wp, dict_T *dict)
520{
521 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200522 int nr;
523 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200524
Bram Moolenaarae943152019-06-16 22:54:14 +0200525 // TODO: flip
526
527 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200528 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200529 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
530 if (wp->w_firstline < 1)
531 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200532
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200533 di = dict_find(dict, (char_u *)"scrollbar", -1);
534 if (di != NULL)
535 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
536
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200537 str = dict_get_string(dict, (char_u *)"title", FALSE);
538 if (str != NULL)
539 {
540 vim_free(wp->w_popup_title);
541 wp->w_popup_title = vim_strsave(str);
542 }
543
Bram Moolenaar402502d2019-05-30 22:07:36 +0200544 di = dict_find(dict, (char_u *)"wrap", -1);
545 if (di != NULL)
546 {
547 nr = dict_get_number(dict, (char_u *)"wrap");
548 wp->w_p_wrap = nr != 0;
549 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200550
Bram Moolenaara42d9452019-06-15 21:46:30 +0200551 di = dict_find(dict, (char_u *)"drag", -1);
552 if (di != NULL)
553 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200554
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200555 di = dict_find(dict, (char_u *)"close", -1);
556 if (di != NULL)
557 {
558 int ok = TRUE;
559
560 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
561 {
562 char_u *s = di->di_tv.vval.v_string;
563
564 if (STRCMP(s, "none") == 0)
565 wp->w_popup_close = POPCLOSE_NONE;
566 else if (STRCMP(s, "button") == 0)
567 wp->w_popup_close = POPCLOSE_BUTTON;
568 else if (STRCMP(s, "click") == 0)
569 wp->w_popup_close = POPCLOSE_CLICK;
570 else
571 ok = FALSE;
572 }
573 else
574 ok = FALSE;
575 if (!ok)
576 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
577 }
578
Bram Moolenaarae943152019-06-16 22:54:14 +0200579 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
580 if (str != NULL)
581 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
582 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200583
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200584 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
585 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200586 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
587 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200588
Bram Moolenaar790498b2019-06-01 22:15:29 +0200589 di = dict_find(dict, (char_u *)"borderhighlight", -1);
590 if (di != NULL)
591 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200592 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200593 emsg(_(e_listreq));
594 else
595 {
596 list_T *list = di->di_tv.vval.v_list;
597 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200598 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200599
Bram Moolenaar403d0902019-07-17 21:37:32 +0200600 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
601 ++i, li = li->li_next)
602 {
603 str = tv_get_string(&li->li_tv);
604 if (*str != NUL)
605 wp->w_border_highlight[i] = vim_strsave(str);
606 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200607 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
608 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200609 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200610 vim_strsave(wp->w_border_highlight[0]);
611 }
612 }
613
Bram Moolenaar790498b2019-06-01 22:15:29 +0200614 di = dict_find(dict, (char_u *)"borderchars", -1);
615 if (di != NULL)
616 {
617 if (di->di_tv.v_type != VAR_LIST)
618 emsg(_(e_listreq));
619 else
620 {
621 list_T *list = di->di_tv.vval.v_list;
622 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200623 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200624
625 if (list != NULL)
626 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
627 ++i, li = li->li_next)
628 {
629 str = tv_get_string(&li->li_tv);
630 if (*str != NUL)
631 wp->w_border_char[i] = mb_ptr2char(str);
632 }
633 if (list->lv_len == 1)
634 for (i = 1; i < 8; ++i)
635 wp->w_border_char[i] = wp->w_border_char[0];
636 if (list->lv_len == 2)
637 {
638 for (i = 4; i < 8; ++i)
639 wp->w_border_char[i] = wp->w_border_char[1];
640 for (i = 1; i < 4; ++i)
641 wp->w_border_char[i] = wp->w_border_char[0];
642 }
643 }
644 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200645
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200646 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
647 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
648
Bram Moolenaarae943152019-06-16 22:54:14 +0200649 di = dict_find(dict, (char_u *)"zindex", -1);
650 if (di != NULL)
651 {
652 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
653 if (wp->w_zindex < 1)
654 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
655 if (wp->w_zindex > 32000)
656 wp->w_zindex = 32000;
657 }
658
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200659 di = dict_find(dict, (char_u *)"mask", -1);
660 if (di != NULL)
661 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200662 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200663
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200664 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200665 {
666 listitem_T *li;
667
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200668 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200669 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
670 li = li->li_next)
671 {
672 if (li->li_tv.v_type != VAR_LIST
673 || li->li_tv.vval.v_list == NULL
674 || li->li_tv.vval.v_list->lv_len != 4)
675 {
676 ok = FALSE;
677 break;
678 }
679 }
680 }
681 if (ok)
682 {
683 wp->w_popup_mask = di->di_tv.vval.v_list;
684 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200685 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200686 }
687 else
688 semsg(_(e_invargval), "mask");
689 }
690
Bram Moolenaarae943152019-06-16 22:54:14 +0200691#if defined(FEAT_TIMERS)
692 // Add timer to close the popup after some time.
693 nr = dict_get_number(dict, (char_u *)"time");
694 if (nr > 0)
695 popup_add_timeout(wp, nr);
696#endif
697
Bram Moolenaar3397f742019-06-02 18:40:06 +0200698 di = dict_find(dict, (char_u *)"moved", -1);
699 if (di != NULL)
700 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200701 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200702 handle_moved_argument(wp, di, FALSE);
703 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200704
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200705 di = dict_find(dict, (char_u *)"mousemoved", -1);
706 if (di != NULL)
707 {
708 set_mousemoved_values(wp);
709 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200710 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200711
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200712 di = dict_find(dict, (char_u *)"cursorline", -1);
713 if (di != NULL)
714 {
715 if (di->di_tv.v_type == VAR_NUMBER)
716 {
717 if (di->di_tv.vval.v_number != 0)
718 wp->w_popup_flags |= POPF_CURSORLINE;
719 else
720 wp->w_popup_flags &= ~POPF_CURSORLINE;
721 }
722 else
723 semsg(_(e_invargval), "cursorline");
724 }
725
Bram Moolenaarae943152019-06-16 22:54:14 +0200726 di = dict_find(dict, (char_u *)"filter", -1);
727 if (di != NULL)
728 {
729 callback_T callback = get_callback(&di->di_tv);
730
731 if (callback.cb_name != NULL)
732 {
733 free_callback(&wp->w_filter_cb);
734 set_callback(&wp->w_filter_cb, &callback);
735 }
736 }
737
738 di = dict_find(dict, (char_u *)"callback", -1);
739 if (di != NULL)
740 {
741 callback_T callback = get_callback(&di->di_tv);
742
743 if (callback.cb_name != NULL)
744 {
745 free_callback(&wp->w_close_cb);
746 set_callback(&wp->w_close_cb, &callback);
747 }
748 }
749}
750
751/*
752 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200753 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200754 */
755 static void
756apply_options(win_T *wp, dict_T *dict)
757{
758 int nr;
759
760 apply_move_options(wp, dict);
761 apply_general_options(wp, dict);
762
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200763 nr = dict_get_number(dict, (char_u *)"hidden");
764 if (nr > 0)
765 {
766 wp->w_popup_flags |= POPF_HIDDEN;
767 --wp->w_buffer->b_nwindows;
768 }
769
Bram Moolenaar33796b32019-06-08 16:01:13 +0200770 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200771 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772}
773
774/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200775 * Add lines to the popup from a list of strings.
776 */
777 static void
778add_popup_strings(buf_T *buf, list_T *l)
779{
780 listitem_T *li;
781 linenr_T lnum = 0;
782 char_u *p;
783
784 for (li = l->lv_first; li != NULL; li = li->li_next)
785 if (li->li_tv.v_type == VAR_STRING)
786 {
787 p = li->li_tv.vval.v_string;
788 ml_append_buf(buf, lnum++,
789 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
790 }
791}
792
793/*
794 * Add lines to the popup from a list of dictionaries.
795 */
796 static void
797add_popup_dicts(buf_T *buf, list_T *l)
798{
799 listitem_T *li;
800 listitem_T *pli;
801 linenr_T lnum = 0;
802 char_u *p;
803 dict_T *dict;
804
805 // first add the text lines
806 for (li = l->lv_first; li != NULL; li = li->li_next)
807 {
808 if (li->li_tv.v_type != VAR_DICT)
809 {
810 emsg(_(e_dictreq));
811 return;
812 }
813 dict = li->li_tv.vval.v_dict;
814 p = dict == NULL ? NULL
815 : dict_get_string(dict, (char_u *)"text", FALSE);
816 ml_append_buf(buf, lnum++,
817 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
818 }
819
820 // add the text properties
821 lnum = 1;
822 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
823 {
824 dictitem_T *di;
825 list_T *plist;
826
827 dict = li->li_tv.vval.v_dict;
828 di = dict_find(dict, (char_u *)"props", -1);
829 if (di != NULL)
830 {
831 if (di->di_tv.v_type != VAR_LIST)
832 {
833 emsg(_(e_listreq));
834 return;
835 }
836 plist = di->di_tv.vval.v_list;
837 if (plist != NULL)
838 {
839 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
840 {
841 if (pli->li_tv.v_type != VAR_DICT)
842 {
843 emsg(_(e_dictreq));
844 return;
845 }
846 dict = pli->li_tv.vval.v_dict;
847 if (dict != NULL)
848 {
849 int col = dict_get_number(dict, (char_u *)"col");
850
851 prop_add_common( lnum, col, dict, buf, NULL);
852 }
853 }
854 }
855 }
856 }
857}
858
859/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200860 * Get the padding plus border at the top, adjusted to 1 if there is a title.
861 */
862 static int
863popup_top_extra(win_T *wp)
864{
865 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
866
867 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
868 return 1;
869 return extra;
870}
871
872/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200873 * Return the height of popup window "wp", including border and padding.
874 */
875 int
876popup_height(win_T *wp)
877{
878 return wp->w_height
879 + popup_top_extra(wp)
880 + wp->w_popup_padding[2] + wp->w_popup_border[2];
881}
882
883/*
884 * Return the width of popup window "wp", including border, padding and
885 * scrollbar.
886 */
887 int
888popup_width(win_T *wp)
889{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200890 // w_leftcol is how many columns of the core are left of the screen
891 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200892 return wp->w_width + wp->w_leftcol
893 + wp->w_popup_padding[3] + wp->w_popup_border[3]
894 + wp->w_popup_padding[1] + wp->w_popup_border[1]
895 + wp->w_has_scrollbar
896 + wp->w_popup_rightoff;
897}
898
899/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200900 * Adjust the position and size of the popup to fit on the screen.
901 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200902 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200903popup_adjust_position(win_T *wp)
904{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200905 linenr_T lnum;
906 int wrapped = 0;
907 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200908 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200909 int center_vert = FALSE;
910 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200911 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200912 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200913 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
914 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
915 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
916 int extra_height = top_extra + bot_extra;
917 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200918 int org_winrow = wp->w_winrow;
919 int org_wincol = wp->w_wincol;
920 int org_width = wp->w_width;
921 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200922 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200923 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200924 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200925
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200926 wp->w_winrow = 0;
927 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200928 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200929 wp->w_popup_leftoff = 0;
930 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200931 if (wp->w_popup_pos == POPPOS_CENTER)
932 {
933 // center after computing the size
934 center_vert = TRUE;
935 center_hor = TRUE;
936 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200937 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200938 {
939 if (wp->w_wantline == 0)
940 center_vert = TRUE;
941 else if (wp->w_popup_pos == POPPOS_TOPLEFT
942 || wp->w_popup_pos == POPPOS_TOPRIGHT)
943 {
944 wp->w_winrow = wp->w_wantline - 1;
945 if (wp->w_winrow >= Rows)
946 wp->w_winrow = Rows - 1;
947 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200948
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200949 if (wp->w_wantcol == 0)
950 center_hor = TRUE;
951 else if (wp->w_popup_pos == POPPOS_TOPLEFT
952 || wp->w_popup_pos == POPPOS_BOTLEFT)
953 {
954 wp->w_wincol = wp->w_wantcol - 1;
955 if (wp->w_wincol >= Columns - 3)
956 wp->w_wincol = Columns - 3;
957 }
958 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200959
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200960 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200961 // When left aligned use the space available, but shift to the left when we
962 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200963 maxspace = Columns - wp->w_wincol - left_extra;
964 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200965 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200966 {
967 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200968 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200969 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200970
Bram Moolenaar8d241042019-06-12 23:40:01 +0200971 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +0200972 if (wp->w_firstline != 0)
973 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200974 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
975 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
976
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200977 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200978 // Use a minimum width of one, so that something shows when there is no
979 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200980 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200981 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200982 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200983 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200984 int len;
985 int w_width = wp->w_width;
986
987 // Count Tabs for what they are worth and compute the length based on
988 // the maximum width (matters when 'showbreak' is set).
989 if (wp->w_width < maxwidth)
990 wp->w_width = maxwidth;
991 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200992 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200993 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200994
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200995 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200996 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200997 while (len > maxwidth)
998 {
999 ++wrapped;
1000 len -= maxwidth;
1001 wp->w_width = maxwidth;
1002 }
1003 }
1004 else if (len > maxwidth
1005 && allow_adjust_left
1006 && (wp->w_popup_pos == POPPOS_TOPLEFT
1007 || wp->w_popup_pos == POPPOS_BOTLEFT))
1008 {
1009 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001010 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001011
Bram Moolenaar51c31312019-06-15 22:27:23 +02001012 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001013 {
1014 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001015
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001016 len -= truncate_shift;
1017 shift_by -= truncate_shift;
1018 }
1019
1020 wp->w_wincol -= shift_by;
1021 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001022 wp->w_width = maxwidth;
1023 }
1024 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001025 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001026 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001027 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1028 wp->w_width = wp->w_maxwidth;
1029 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001030 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001031 if (wp->w_maxheight > 0
1032 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001033 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001034 }
1035
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001036 wp->w_has_scrollbar = wp->w_want_scrollbar
1037 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001038 if (wp->w_has_scrollbar)
1039 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001040
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001041 minwidth = wp->w_minwidth;
1042 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1043 {
1044 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1045
1046 if (minwidth < title_len)
1047 minwidth = title_len;
1048 }
1049
1050 if (minwidth > 0 && wp->w_width < minwidth)
1051 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001052 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001053 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001054 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001055 // some columns cut off on the right
1056 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001057 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001058 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001059 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001060 {
1061 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1062 if (wp->w_wincol < 0)
1063 wp->w_wincol = 0;
1064 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001065 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1066 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1067 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001068 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1069
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001070 // Right aligned: move to the right if needed.
1071 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001072 if (leftoff >= 0)
1073 wp->w_wincol = leftoff;
1074 else if (wp->w_popup_fixed)
1075 {
1076 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001077 // columns when displaying the window, minus left border and
1078 // padding.
1079 if (-leftoff > left_extra)
1080 wp->w_leftcol = -leftoff - left_extra;
1081 wp->w_width -= wp->w_leftcol;
1082 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001083 if (wp->w_width < 0)
1084 wp->w_width = 0;
1085 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001086 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001087
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001088 if (wp->w_p_wrap || (!wp->w_popup_fixed
1089 && (wp->w_popup_pos == POPPOS_TOPLEFT
1090 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001091 {
1092 int want_col = 0;
1093
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001094 // try to show the right border and any scrollbar
1095 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001096 if (want_col > 0 && wp->w_wincol > 0
1097 && wp->w_wincol + want_col >= Columns)
1098 {
1099 wp->w_wincol = Columns - want_col;
1100 if (wp->w_wincol < 0)
1101 wp->w_wincol = 0;
1102 }
1103 }
1104
Bram Moolenaar8d241042019-06-12 23:40:01 +02001105 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1106 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001107 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1108 wp->w_height = wp->w_minheight;
1109 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1110 wp->w_height = wp->w_maxheight;
1111 if (wp->w_height > Rows - wp->w_winrow)
1112 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001113
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001114 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001115 {
1116 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1117 if (wp->w_winrow < 0)
1118 wp->w_winrow = 0;
1119 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001120 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1121 || wp->w_popup_pos == POPPOS_BOTLEFT)
1122 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001123 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001124 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001125 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001126 else
1127 // not enough space, make top aligned
1128 wp->w_winrow = wp->w_wantline + 1;
1129 }
1130
Bram Moolenaar17146962019-05-30 00:12:11 +02001131 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001132
1133 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001134 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001135 if (org_winrow != wp->w_winrow
1136 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001137 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001138 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001139 || org_width != wp->w_width
1140 || org_height != wp->w_height)
1141 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001142 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001143 redraw_win_later(wp, NOT_VALID);
1144 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1145 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001146 popup_mask_refresh = TRUE;
1147 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001148}
1149
Bram Moolenaar17627312019-06-02 19:53:44 +02001150typedef enum
1151{
1152 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001153 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001154 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001155 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001156 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001157 TYPE_MENU,
1158 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001159} create_type_T;
1160
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001161/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001162 * Make "buf" empty and set the contents to "text".
1163 * Used by popup_create() and popup_settext().
1164 */
1165 static void
1166popup_set_buffer_text(buf_T *buf, typval_T text)
1167{
1168 int lnum;
1169
1170 // Clear the buffer, then replace the lines.
1171 curbuf = buf;
1172 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1173 ml_delete(lnum, FALSE);
1174 curbuf = curwin->w_buffer;
1175
1176 // Add text to the buffer.
1177 if (text.v_type == VAR_STRING)
1178 {
1179 // just a string
1180 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1181 }
1182 else
1183 {
1184 list_T *l = text.vval.v_list;
1185
1186 if (l->lv_len > 0)
1187 {
1188 if (l->lv_first->li_tv.v_type == VAR_STRING)
1189 // list of strings
1190 add_popup_strings(buf, l);
1191 else
1192 // list of dictionaries
1193 add_popup_dicts(buf, l);
1194 }
1195 }
1196
1197 // delete the line that was in the empty buffer
1198 curbuf = buf;
1199 ml_delete(buf->b_ml.ml_line_count, FALSE);
1200 curbuf = curwin->w_buffer;
1201}
1202
1203/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001204 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1205 * not NULL.
1206 * Return FAIL if the parsing fails.
1207 */
1208 int
1209parse_previewpopup(win_T *wp)
1210{
1211 char_u *p;
1212
1213 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1214 {
1215 char_u *e, *dig;
1216 char_u *s = p;
1217 int x;
1218
1219 e = vim_strchr(p, ':');
1220 if (e == NULL || e[1] == NUL)
1221 return FAIL;
1222
1223 p = vim_strchr(e, ',');
1224 if (p == NULL)
1225 p = e + STRLEN(e);
1226 dig = e + 1;
1227 x = getdigits(&dig);
1228 if (dig != p)
1229 return FAIL;
1230
1231 if (STRNCMP(s, "height:", 7) == 0)
1232 {
1233 if (wp != NULL)
1234 {
1235 wp->w_minheight = x;
1236 wp->w_maxheight = x;
1237 }
1238 }
1239 else if (STRNCMP(s, "width:", 6) == 0)
1240 {
1241 if (wp != NULL)
1242 {
1243 wp->w_minwidth = x;
1244 wp->w_maxwidth = x;
1245 }
1246 }
1247 else
1248 return FAIL;
1249 }
1250 return OK;
1251}
1252
1253/*
1254 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001255 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001256 */
1257 void
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001258popup_set_wantpos(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001259{
1260 setcursor_mayforce(TRUE);
1261 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1262 if (wp->w_wantline == 0) // cursor in first line
1263 {
1264 wp->w_wantline = 2;
1265 wp->w_popup_pos = POPPOS_TOPLEFT;
1266 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001267
Bram Moolenaar79648732019-07-18 21:43:07 +02001268 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001269 if (wp->w_wantcol > Columns - width)
1270 {
1271 wp->w_wantcol = Columns - width;
1272 if (wp->w_wantcol < 1)
1273 wp->w_wantcol = 1;
1274 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001275 popup_adjust_position(wp);
1276}
1277
1278/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001279 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001280 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001281 * etc.
1282 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001283 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001284 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001285popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001286{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001287 win_T *wp;
1288 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001289 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001290 int new_buffer;
1291 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001292 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001293 int nr;
1294 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001295
Bram Moolenaar79648732019-07-18 21:43:07 +02001296 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001297 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001298 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001299 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001300 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001301 buf = buflist_findnr( argvars[0].vval.v_number);
1302 if (buf == NULL)
1303 {
1304 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1305 return NULL;
1306 }
1307 }
1308 else if (!(argvars[0].v_type == VAR_STRING
1309 && argvars[0].vval.v_string != NULL)
1310 && !(argvars[0].v_type == VAR_LIST
1311 && argvars[0].vval.v_list != NULL))
1312 {
1313 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001314 return NULL;
1315 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001316 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001317 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001318 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001319 return NULL;
1320 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001321 d = argvars[1].vval.v_dict;
1322 }
1323
1324 if (d != NULL)
1325 {
1326 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1327 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1328 else if (type == TYPE_NOTIFICATION)
1329 tabnr = -1; // notifications are global by default
1330 else
1331 tabnr = 0;
1332 if (tabnr > 0)
1333 {
1334 tp = find_tabpage(tabnr);
1335 if (tp == NULL)
1336 {
1337 semsg(_("E997: Tabpage not found: %d"), tabnr);
1338 return NULL;
1339 }
1340 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001341 }
1342
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001343 // Create the window and buffer.
1344 wp = win_alloc_popup_win();
1345 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001346 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001347 if (rettv != NULL)
1348 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001349 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001350 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001351
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001352 if (buf != NULL)
1353 {
1354 // use existing buffer
1355 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001356 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001357 buffer_ensure_loaded(buf);
1358 }
1359 else
1360 {
1361 // create a new buffer associated with the popup
1362 new_buffer = TRUE;
1363 buf = buflist_new(NULL, NULL, (linenr_T)0,
1364 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1365 if (buf == NULL)
1366 return NULL;
1367 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001368
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001369 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001370
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001371 set_local_options_default(wp);
1372 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001373 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001374 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001375 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001376 buf->b_p_ul = -1; // no undo
1377 buf->b_p_swf = FALSE; // no swap file
1378 buf->b_p_bl = FALSE; // unlisted buffer
1379 buf->b_locked = TRUE;
1380 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001381
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001382 // Avoid that 'buftype' is reset when this buffer is entered.
1383 buf->b_p_initialized = TRUE;
1384 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001385
Bram Moolenaara3fce622019-06-20 02:31:49 +02001386 if (tp != NULL)
1387 {
1388 // popup on specified tab page
1389 wp->w_next = tp->tp_first_popupwin;
1390 tp->tp_first_popupwin = wp;
1391 }
1392 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001393 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001394 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001395 wp->w_next = curtab->tp_first_popupwin;
1396 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001397 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001398 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001399 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001400 win_T *prev = first_popupwin;
1401
1402 // Global popup: add at the end, so that it gets displayed on top of
1403 // older ones with the same zindex. Matters for notifications.
1404 if (first_popupwin == NULL)
1405 first_popupwin = wp;
1406 else
1407 {
1408 while (prev->w_next != NULL)
1409 prev = prev->w_next;
1410 prev->w_next = wp;
1411 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001412 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001413
Bram Moolenaar79648732019-07-18 21:43:07 +02001414 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001415 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001416
Bram Moolenaar79648732019-07-18 21:43:07 +02001417 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001418 {
1419 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001420 }
1421 if (type == TYPE_ATCURSOR)
1422 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001423 popup_set_wantpos(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001424 set_moved_values(wp);
1425 set_moved_columns(wp, FIND_STRING);
1426 }
1427
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001428 if (type == TYPE_BEVAL)
1429 {
1430 wp->w_popup_pos = POPPOS_BOTLEFT;
1431
1432 // by default use the mouse position
1433 wp->w_wantline = mouse_row;
1434 if (wp->w_wantline <= 0) // mouse on first line
1435 {
1436 wp->w_wantline = 2;
1437 wp->w_popup_pos = POPPOS_TOPLEFT;
1438 }
1439 wp->w_wantcol = mouse_col + 1;
1440 set_mousemoved_values(wp);
1441 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1442 }
1443
Bram Moolenaar33796b32019-06-08 16:01:13 +02001444 // set default values
1445 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001446 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001447
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001448 if (type == TYPE_NOTIFICATION)
1449 {
1450 win_T *twp, *nextwin;
1451 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001452
1453 // Try to not overlap with another global popup. Guess we need 3
1454 // more screen lines than buffer lines.
1455 wp->w_wantline = 1;
1456 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1457 {
1458 nextwin = twp->w_next;
1459 if (twp != wp
1460 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1461 && twp->w_winrow <= wp->w_wantline - 1 + height
1462 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1463 {
1464 // move to below this popup and restart the loop to check for
1465 // overlap with other popups
1466 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1467 nextwin = first_popupwin;
1468 }
1469 }
1470 if (wp->w_wantline + height > Rows)
1471 {
1472 // can't avoid overlap, put on top in the hope that message goes
1473 // away soon.
1474 wp->w_wantline = 1;
1475 }
1476
1477 wp->w_wantcol = 10;
1478 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001479 wp->w_minwidth = 20;
1480 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001481 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001482 for (i = 0; i < 4; ++i)
1483 wp->w_popup_border[i] = 1;
1484 wp->w_popup_padding[1] = 1;
1485 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001486
1487 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001488 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001489 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1490 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001491 }
1492
Bram Moolenaara730e552019-06-16 19:05:31 +02001493 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001494 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001495 wp->w_popup_pos = POPPOS_CENTER;
1496 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1497 wp->w_popup_drag = 1;
1498 for (i = 0; i < 4; ++i)
1499 {
1500 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001501 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001502 }
1503 }
1504
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 if (type == TYPE_MENU)
1506 {
1507 typval_T tv;
1508 callback_T callback;
1509
1510 tv.v_type = VAR_STRING;
1511 tv.vval.v_string = (char_u *)"popup_filter_menu";
1512 callback = get_callback(&tv);
1513 if (callback.cb_name != NULL)
1514 set_callback(&wp->w_filter_cb, &callback);
1515
1516 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001517 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001518 }
1519
Bram Moolenaar79648732019-07-18 21:43:07 +02001520 if (type == TYPE_PREVIEW)
1521 {
1522 wp->w_popup_drag = 1;
1523 wp->w_popup_close = POPCLOSE_BUTTON;
1524 for (i = 0; i < 4; ++i)
1525 wp->w_popup_border[i] = 1;
1526 parse_previewpopup(wp);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001527 popup_set_wantpos(wp, wp->w_minwidth);
Bram Moolenaar79648732019-07-18 21:43:07 +02001528 }
1529
Bram Moolenaarae943152019-06-16 22:54:14 +02001530 for (i = 0; i < 4; ++i)
1531 VIM_CLEAR(wp->w_border_highlight[i]);
1532 for (i = 0; i < 8; ++i)
1533 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001534 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001535 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001536
Bram Moolenaar79648732019-07-18 21:43:07 +02001537 if (d != NULL)
1538 // Deal with options.
1539 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001540
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001541#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001542 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1543 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001544#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001545
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001546 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001547
1548 wp->w_vsep_width = 0;
1549
1550 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001551 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001552
1553 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001554}
1555
1556/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001557 * popup_clear()
1558 */
1559 void
1560f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1561{
1562 close_all_popups();
1563}
1564
1565/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001566 * popup_create({text}, {options})
1567 */
1568 void
1569f_popup_create(typval_T *argvars, typval_T *rettv)
1570{
Bram Moolenaar17627312019-06-02 19:53:44 +02001571 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001572}
1573
1574/*
1575 * popup_atcursor({text}, {options})
1576 */
1577 void
1578f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1579{
Bram Moolenaar17627312019-06-02 19:53:44 +02001580 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001581}
1582
1583/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001584 * popup_beval({text}, {options})
1585 */
1586 void
1587f_popup_beval(typval_T *argvars, typval_T *rettv)
1588{
1589 popup_create(argvars, rettv, TYPE_BEVAL);
1590}
1591
1592/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001593 * Invoke the close callback for window "wp" with value "result".
1594 * Careful: The callback may make "wp" invalid!
1595 */
1596 static void
1597invoke_popup_callback(win_T *wp, typval_T *result)
1598{
1599 typval_T rettv;
1600 int dummy;
1601 typval_T argv[3];
1602
1603 argv[0].v_type = VAR_NUMBER;
1604 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1605
1606 if (result != NULL && result->v_type != VAR_UNKNOWN)
1607 copy_tv(result, &argv[1]);
1608 else
1609 {
1610 argv[1].v_type = VAR_NUMBER;
1611 argv[1].vval.v_number = 0;
1612 }
1613
1614 argv[2].v_type = VAR_UNKNOWN;
1615
1616 call_callback(&wp->w_close_cb, -1,
1617 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1618 if (result != NULL)
1619 clear_tv(&argv[1]);
1620 clear_tv(&rettv);
1621}
1622
1623/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001624 * Close popup "wp" and invoke any close callback for it.
1625 */
1626 static void
1627popup_close_and_callback(win_T *wp, typval_T *arg)
1628{
1629 int id = wp->w_id;
1630
1631 if (wp->w_close_cb.cb_name != NULL)
1632 // Careful: This may make "wp" invalid.
1633 invoke_popup_callback(wp, arg);
1634
1635 popup_close(id);
1636}
1637
1638/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001639 * Close popup "wp" because of a mouse click.
1640 */
1641 void
1642popup_close_for_mouse_click(win_T *wp)
1643{
1644 typval_T res;
1645
1646 res.v_type = VAR_NUMBER;
1647 res.vval.v_number = -2;
1648 popup_close_and_callback(wp, &res);
1649}
1650
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001651 static void
1652check_mouse_moved(win_T *wp, win_T *mouse_wp)
1653{
1654 // Close the popup when all if these are true:
1655 // - the mouse is not on this popup
1656 // - "mousemoved" was used
1657 // - the mouse is no longer on the same screen row or the mouse column is
1658 // outside of the relevant text
1659 if (wp != mouse_wp
1660 && wp->w_popup_mouse_row != 0
1661 && (wp->w_popup_mouse_row != mouse_row
1662 || mouse_col < wp->w_popup_mouse_mincol
1663 || mouse_col > wp->w_popup_mouse_maxcol))
1664 {
1665 typval_T res;
1666
1667 res.v_type = VAR_NUMBER;
1668 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001669 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001670 popup_close_and_callback(wp, &res);
1671 }
1672}
1673
1674/*
1675 * Called when the mouse moved: may close a popup with "mousemoved".
1676 */
1677 void
1678popup_handle_mouse_moved(void)
1679{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001680 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001681 win_T *mouse_wp;
1682 int row = mouse_row;
1683 int col = mouse_col;
1684
1685 // find the window where the mouse is in
1686 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1687
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001688 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1689 {
1690 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001691 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001692 }
1693 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1694 {
1695 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001696 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001697 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001698}
1699
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001700/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001701 * In a filter: check if the typed key is a mouse event that is used for
1702 * dragging the popup.
1703 */
1704 static void
1705filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1706{
1707 int row = mouse_row;
1708 int col = mouse_col;
1709
1710 if (wp->w_popup_drag
1711 && is_mouse_key(c)
1712 && (wp == popup_dragwin
1713 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1714 // do not consume the key, allow for dragging the popup
1715 rettv->vval.v_number = 0;
1716}
1717
Bram Moolenaara730e552019-06-16 19:05:31 +02001718/*
1719 * popup_filter_menu({text}, {options})
1720 */
1721 void
1722f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1723{
1724 int id = tv_get_number(&argvars[0]);
1725 win_T *wp = win_id2wp(id);
1726 char_u *key = tv_get_string(&argvars[1]);
1727 typval_T res;
1728 int c;
1729 linenr_T old_lnum;
1730
1731 // If the popup has been closed do not consume the key.
1732 if (wp == NULL)
1733 return;
1734
1735 c = *key;
1736 if (c == K_SPECIAL && key[1] != NUL)
1737 c = TO_SPECIAL(key[1], key[2]);
1738
1739 // consume all keys until done
1740 rettv->vval.v_number = 1;
1741 res.v_type = VAR_NUMBER;
1742
1743 old_lnum = wp->w_cursor.lnum;
1744 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1745 --wp->w_cursor.lnum;
1746 if ((c == 'j' || c == 'J' || c == K_DOWN)
1747 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1748 ++wp->w_cursor.lnum;
1749 if (old_lnum != wp->w_cursor.lnum)
1750 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001751 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001752 return;
1753 }
1754
1755 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1756 {
1757 // Cancelled, invoke callback with -1
1758 res.vval.v_number = -1;
1759 popup_close_and_callback(wp, &res);
1760 return;
1761 }
1762 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1763 {
1764 // Invoke callback with current index.
1765 res.vval.v_number = wp->w_cursor.lnum;
1766 popup_close_and_callback(wp, &res);
1767 return;
1768 }
1769
1770 filter_handle_drag(wp, c, rettv);
1771}
1772
1773/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001774 * popup_filter_yesno({text}, {options})
1775 */
1776 void
1777f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1778{
1779 int id = tv_get_number(&argvars[0]);
1780 win_T *wp = win_id2wp(id);
1781 char_u *key = tv_get_string(&argvars[1]);
1782 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001783 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001784
1785 // If the popup has been closed don't consume the key.
1786 if (wp == NULL)
1787 return;
1788
Bram Moolenaara730e552019-06-16 19:05:31 +02001789 c = *key;
1790 if (c == K_SPECIAL && key[1] != NUL)
1791 c = TO_SPECIAL(key[1], key[2]);
1792
Bram Moolenaara42d9452019-06-15 21:46:30 +02001793 // consume all keys until done
1794 rettv->vval.v_number = 1;
1795
Bram Moolenaara730e552019-06-16 19:05:31 +02001796 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001797 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001798 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001799 res.vval.v_number = 0;
1800 else
1801 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001802 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001803 return;
1804 }
1805
1806 // Invoke callback
1807 res.v_type = VAR_NUMBER;
1808 popup_close_and_callback(wp, &res);
1809}
1810
1811/*
1812 * popup_dialog({text}, {options})
1813 */
1814 void
1815f_popup_dialog(typval_T *argvars, typval_T *rettv)
1816{
1817 popup_create(argvars, rettv, TYPE_DIALOG);
1818}
1819
1820/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001821 * popup_menu({text}, {options})
1822 */
1823 void
1824f_popup_menu(typval_T *argvars, typval_T *rettv)
1825{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001826 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001827}
1828
1829/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001830 * popup_notification({text}, {options})
1831 */
1832 void
1833f_popup_notification(typval_T *argvars, typval_T *rettv)
1834{
1835 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1836}
1837
1838/*
1839 * Find the popup window with window-ID "id".
1840 * If the popup window does not exist NULL is returned.
1841 * If the window is not a popup window, and error message is given.
1842 */
1843 static win_T *
1844find_popup_win(int id)
1845{
1846 win_T *wp = win_id2wp(id);
1847
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001849 {
1850 semsg(_("E993: window %d is not a popup window"), id);
1851 return NULL;
1852 }
1853 return wp;
1854}
1855
1856/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001857 * popup_close({id})
1858 */
1859 void
1860f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1861{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001862 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001863 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001864
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001865 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001866 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001867}
1868
1869/*
1870 * popup_hide({id})
1871 */
1872 void
1873f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1874{
1875 int id = (int)tv_get_number(argvars);
1876 win_T *wp = find_popup_win(id);
1877
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001878 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001879 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001880 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001881 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001882 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001883 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001884 }
1885}
1886
1887/*
1888 * popup_show({id})
1889 */
1890 void
1891f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1892{
1893 int id = (int)tv_get_number(argvars);
1894 win_T *wp = find_popup_win(id);
1895
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001896 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001897 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001898 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001899 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001900 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001901 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001902 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001903}
1904
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001905/*
1906 * popup_settext({id}, {text})
1907 */
1908 void
1909f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1910{
1911 int id = (int)tv_get_number(&argvars[0]);
1912 win_T *wp = find_popup_win(id);
1913
1914 if (wp != NULL)
1915 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001916 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1917 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1918 else
1919 {
1920 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001921 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001922 popup_adjust_position(wp);
1923 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001924 }
1925}
1926
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001927 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001928popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001929{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001930 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001931 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001932 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001933 clear_cmdline = TRUE;
1934 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001935
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001936 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001937 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001938}
1939
Bram Moolenaarec583842019-05-26 14:11:23 +02001940/*
1941 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001942 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001943 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001944 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001945popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001946{
1947 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001948 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001949 win_T *prev = NULL;
1950
Bram Moolenaarec583842019-05-26 14:11:23 +02001951 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001952 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001953 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001954 {
1955 if (prev == NULL)
1956 first_popupwin = wp->w_next;
1957 else
1958 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001959 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001960 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001961 }
1962
Bram Moolenaarec583842019-05-26 14:11:23 +02001963 // go through tab-local popups
1964 FOR_ALL_TABPAGES(tp)
1965 popup_close_tabpage(tp, id);
1966}
1967
1968/*
1969 * Close a popup window with Window-id "id" in tabpage "tp".
1970 */
1971 void
1972popup_close_tabpage(tabpage_T *tp, int id)
1973{
1974 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001975 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001976 win_T *prev = NULL;
1977
Bram Moolenaarec583842019-05-26 14:11:23 +02001978 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1979 if (wp->w_id == id)
1980 {
1981 if (prev == NULL)
1982 *root = wp->w_next;
1983 else
1984 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001985 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001986 return;
1987 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001988}
1989
1990 void
1991close_all_popups(void)
1992{
1993 while (first_popupwin != NULL)
1994 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001995 while (curtab->tp_first_popupwin != NULL)
1996 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001997}
1998
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001999/*
2000 * popup_move({id}, {options})
2001 */
2002 void
2003f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2004{
Bram Moolenaarae943152019-06-16 22:54:14 +02002005 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002006 int id = (int)tv_get_number(argvars);
2007 win_T *wp = find_popup_win(id);
2008
2009 if (wp == NULL)
2010 return; // invalid {id}
2011
2012 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2013 {
2014 emsg(_(e_dictreq));
2015 return;
2016 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002017 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002018
Bram Moolenaarae943152019-06-16 22:54:14 +02002019 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002020
2021 if (wp->w_winrow + wp->w_height >= cmdline_row)
2022 clear_cmdline = TRUE;
2023 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002024}
2025
Bram Moolenaarbc133542019-05-29 20:26:46 +02002026/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002027 * popup_setoptions({id}, {options})
2028 */
2029 void
2030f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2031{
2032 dict_T *dict;
2033 int id = (int)tv_get_number(argvars);
2034 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002035 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002036
2037 if (wp == NULL)
2038 return; // invalid {id}
2039
2040 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2041 {
2042 emsg(_(e_dictreq));
2043 return;
2044 }
2045 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002046 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002047
2048 apply_move_options(wp, dict);
2049 apply_general_options(wp, dict);
2050
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002051 if (old_firstline != wp->w_firstline)
2052 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002053 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002054 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002055 popup_adjust_position(wp);
2056}
2057
2058/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002059 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002060 */
2061 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002062f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002063{
2064 dict_T *dict;
2065 int id = (int)tv_get_number(argvars);
2066 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002067 int top_extra;
2068 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002069
2070 if (rettv_dict_alloc(rettv) == OK)
2071 {
2072 if (wp == NULL)
2073 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002074 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002075 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2076
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002077 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002078 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002079 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002080
Bram Moolenaarbc133542019-05-29 20:26:46 +02002081 dict_add_number(dict, "line", wp->w_winrow + 1);
2082 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002083 dict_add_number(dict, "width", wp->w_width + left_extra
2084 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2085 dict_add_number(dict, "height", wp->w_height + top_extra
2086 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002087
2088 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2089 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2090 dict_add_number(dict, "core_width", wp->w_width);
2091 dict_add_number(dict, "core_height", wp->w_height);
2092
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002093 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002094 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002095 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002096 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002097
2098 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002099 }
2100}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002101/*
2102 * popup_locate({row}, {col})
2103 */
2104 void
2105f_popup_locate(typval_T *argvars, typval_T *rettv)
2106{
2107 int row = tv_get_number(&argvars[0]) - 1;
2108 int col = tv_get_number(&argvars[1]) - 1;
2109 win_T *wp;
2110
2111 wp = mouse_find_win(&row, &col, FIND_POPUP);
2112 if (WIN_IS_POPUP(wp))
2113 rettv->vval.v_number = wp->w_id;
2114}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002115
2116/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002117 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2118 */
2119 static void
2120get_padding_border(dict_T *dict, int *array, char *name)
2121{
2122 list_T *list;
2123 int i;
2124
2125 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2126 return;
2127
2128 list = list_alloc();
2129 if (list != NULL)
2130 {
2131 dict_add_list(dict, name, list);
2132 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2133 for (i = 0; i < 4; ++i)
2134 list_append_number(list, array[i]);
2135 }
2136}
2137
2138/*
2139 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2140 */
2141 static void
2142get_borderhighlight(dict_T *dict, win_T *wp)
2143{
2144 list_T *list;
2145 int i;
2146
2147 for (i = 0; i < 4; ++i)
2148 if (wp->w_border_highlight[i] != NULL)
2149 break;
2150 if (i == 4)
2151 return;
2152
2153 list = list_alloc();
2154 if (list != NULL)
2155 {
2156 dict_add_list(dict, "borderhighlight", list);
2157 for (i = 0; i < 4; ++i)
2158 list_append_string(list, wp->w_border_highlight[i], -1);
2159 }
2160}
2161
2162/*
2163 * For popup_getoptions(): add a "borderchars" entry to "dict".
2164 */
2165 static void
2166get_borderchars(dict_T *dict, win_T *wp)
2167{
2168 list_T *list;
2169 int i;
2170 char_u buf[NUMBUFLEN];
2171 int len;
2172
2173 for (i = 0; i < 8; ++i)
2174 if (wp->w_border_char[i] != 0)
2175 break;
2176 if (i == 8)
2177 return;
2178
2179 list = list_alloc();
2180 if (list != NULL)
2181 {
2182 dict_add_list(dict, "borderchars", list);
2183 for (i = 0; i < 8; ++i)
2184 {
2185 len = mb_char2bytes(wp->w_border_char[i], buf);
2186 list_append_string(list, buf, len);
2187 }
2188 }
2189}
2190
2191/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002192 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002193 */
2194 static void
2195get_moved_list(dict_T *dict, win_T *wp)
2196{
2197 list_T *list;
2198
2199 list = list_alloc();
2200 if (list != NULL)
2201 {
2202 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002203 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002204 list_append_number(list, wp->w_popup_mincol);
2205 list_append_number(list, wp->w_popup_maxcol);
2206 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002207 list = list_alloc();
2208 if (list != NULL)
2209 {
2210 dict_add_list(dict, "mousemoved", list);
2211 list_append_number(list, wp->w_popup_mouse_row);
2212 list_append_number(list, wp->w_popup_mouse_mincol);
2213 list_append_number(list, wp->w_popup_mouse_maxcol);
2214 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002215}
2216
2217/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002218 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002219 */
2220 void
2221f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2222{
2223 dict_T *dict;
2224 int id = (int)tv_get_number(argvars);
2225 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002226 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002227 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002228
2229 if (rettv_dict_alloc(rettv) == OK)
2230 {
2231 if (wp == NULL)
2232 return;
2233
2234 dict = rettv->vval.v_dict;
2235 dict_add_number(dict, "line", wp->w_wantline);
2236 dict_add_number(dict, "col", wp->w_wantcol);
2237 dict_add_number(dict, "minwidth", wp->w_minwidth);
2238 dict_add_number(dict, "minheight", wp->w_minheight);
2239 dict_add_number(dict, "maxheight", wp->w_maxheight);
2240 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002241 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002242 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002243 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002244 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002245 dict_add_string(dict, "title", wp->w_popup_title);
2246 dict_add_number(dict, "wrap", wp->w_p_wrap);
2247 dict_add_number(dict, "drag", wp->w_popup_drag);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002248 dict_add_number(dict, "cursorline",
2249 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002250 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002251 if (wp->w_scrollbar_highlight != NULL)
2252 dict_add_string(dict, "scrollbarhighlight",
2253 wp->w_scrollbar_highlight);
2254 if (wp->w_thumb_highlight != NULL)
2255 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002256
Bram Moolenaara3fce622019-06-20 02:31:49 +02002257 // find the tabpage that holds this popup
2258 i = 1;
2259 FOR_ALL_TABPAGES(tp)
2260 {
2261 win_T *p;
2262
2263 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2264 if (p->w_id == id)
2265 break;
2266 if (p != NULL)
2267 break;
2268 ++i;
2269 }
2270 if (tp == NULL)
2271 i = -1; // must be global
2272 else if (tp == curtab)
2273 i = 0;
2274 dict_add_number(dict, "tabpage", i);
2275
Bram Moolenaarae943152019-06-16 22:54:14 +02002276 get_padding_border(dict, wp->w_popup_padding, "padding");
2277 get_padding_border(dict, wp->w_popup_border, "border");
2278 get_borderhighlight(dict, wp);
2279 get_borderchars(dict, wp);
2280 get_moved_list(dict, wp);
2281
2282 if (wp->w_filter_cb.cb_name != NULL)
2283 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2284 if (wp->w_close_cb.cb_name != NULL)
2285 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002286
2287 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2288 ++i)
2289 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2290 {
2291 dict_add_string(dict, "pos",
2292 (char_u *)poppos_entries[i].pp_name);
2293 break;
2294 }
2295
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002296 dict_add_string(dict, "close", (char_u *)(
2297 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2298 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2299
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002300# if defined(FEAT_TIMERS)
2301 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2302 ? (long)wp->w_popup_timer->tr_interval : 0L);
2303# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002304 }
2305}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002306
2307 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002308error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002309{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002310 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002311 {
2312 emsg(_("E994: Not allowed in a popup window"));
2313 return TRUE;
2314 }
2315 return FALSE;
2316}
2317
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002318/*
2319 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002320 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002321 */
2322 void
2323popup_reset_handled()
2324{
2325 win_T *wp;
2326
2327 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2328 wp->w_popup_flags &= ~POPF_HANDLED;
2329 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2330 wp->w_popup_flags &= ~POPF_HANDLED;
2331}
2332
2333/*
2334 * Find the next visible popup where POPF_HANDLED is not set.
2335 * Must have called popup_reset_handled() first.
2336 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2337 * popup with the highest zindex.
2338 */
2339 win_T *
2340find_next_popup(int lowest)
2341{
2342 win_T *wp;
2343 win_T *found_wp;
2344 int found_zindex;
2345
2346 found_zindex = lowest ? INT_MAX : 0;
2347 found_wp = NULL;
2348 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2349 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2350 && (lowest ? wp->w_zindex < found_zindex
2351 : wp->w_zindex > found_zindex))
2352 {
2353 found_zindex = wp->w_zindex;
2354 found_wp = wp;
2355 }
2356 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2357 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2358 && (lowest ? wp->w_zindex < found_zindex
2359 : wp->w_zindex > found_zindex))
2360 {
2361 found_zindex = wp->w_zindex;
2362 found_wp = wp;
2363 }
2364
2365 if (found_wp != NULL)
2366 found_wp->w_popup_flags |= POPF_HANDLED;
2367 return found_wp;
2368}
2369
2370/*
2371 * Invoke the filter callback for window "wp" with typed character "c".
2372 * Uses the global "mod_mask" for modifiers.
2373 * Returns the return value of the filter.
2374 * Careful: The filter may make "wp" invalid!
2375 */
2376 static int
2377invoke_popup_filter(win_T *wp, int c)
2378{
2379 int res;
2380 typval_T rettv;
2381 int dummy;
2382 typval_T argv[3];
2383 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002384 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002385
Bram Moolenaara42d9452019-06-15 21:46:30 +02002386 // Emergency exit: CTRL-C closes the popup.
2387 if (c == Ctrl_C)
2388 {
2389 rettv.v_type = VAR_NUMBER;
2390 rettv.vval.v_number = -1;
2391 popup_close_and_callback(wp, &rettv);
2392 return 1;
2393 }
2394
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002395 argv[0].v_type = VAR_NUMBER;
2396 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2397
2398 // Convert the number to a string, so that the function can use:
2399 // if a:c == "\<F2>"
2400 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2401 argv[1].v_type = VAR_STRING;
2402 argv[1].vval.v_string = vim_strsave(buf);
2403
2404 argv[2].v_type = VAR_UNKNOWN;
2405
Bram Moolenaara42d9452019-06-15 21:46:30 +02002406 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002407 call_callback(&wp->w_filter_cb, -1,
2408 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002409 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002410 popup_highlight_curline(wp);
2411
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002412 res = tv_get_number(&rettv);
2413 vim_free(argv[1].vval.v_string);
2414 clear_tv(&rettv);
2415 return res;
2416}
2417
2418/*
2419 * Called when "c" was typed: invoke popup filter callbacks.
2420 * Returns TRUE when the character was consumed,
2421 */
2422 int
2423popup_do_filter(int c)
2424{
2425 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002426 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002427
2428 popup_reset_handled();
2429
2430 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2431 if (wp->w_filter_cb.cb_name != NULL)
2432 res = invoke_popup_filter(wp, c);
2433
2434 return res;
2435}
2436
Bram Moolenaar3397f742019-06-02 18:40:06 +02002437/*
2438 * Called when the cursor moved: check if any popup needs to be closed if the
2439 * cursor moved far enough.
2440 */
2441 void
2442popup_check_cursor_pos()
2443{
2444 win_T *wp;
2445 typval_T tv;
2446
2447 popup_reset_handled();
2448 while ((wp = find_next_popup(TRUE)) != NULL)
2449 if (wp->w_popup_curwin != NULL
2450 && (curwin != wp->w_popup_curwin
2451 || curwin->w_cursor.lnum != wp->w_popup_lnum
2452 || curwin->w_cursor.col < wp->w_popup_mincol
2453 || curwin->w_cursor.col > wp->w_popup_maxcol))
2454 {
2455 tv.v_type = VAR_NUMBER;
2456 tv.vval.v_number = -1;
2457 popup_close_and_callback(wp, &tv);
2458 }
2459}
2460
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002461/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002462 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002463 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002464 static void
2465popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002466{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002467 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002468 char_u *cells;
2469 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002470
2471 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002472 return;
2473 if (wp->w_popup_mask_cells != NULL
2474 && wp->w_popup_mask_height == height
2475 && wp->w_popup_mask_width == width)
2476 return; // cache is still valid
2477
2478 vim_free(wp->w_popup_mask_cells);
2479 wp->w_popup_mask_cells = alloc_clear(width * height);
2480 if (wp->w_popup_mask_cells == NULL)
2481 return;
2482 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002483
2484 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2485 {
2486 int cols, cole;
2487 int lines, linee;
2488
2489 li = lio->li_tv.vval.v_list->lv_first;
2490 cols = tv_get_number(&li->li_tv);
2491 if (cols < 0)
2492 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002493 li = li->li_next;
2494 cole = tv_get_number(&li->li_tv);
2495 if (cole < 0)
2496 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002497 li = li->li_next;
2498 lines = tv_get_number(&li->li_tv);
2499 if (lines < 0)
2500 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002501 li = li->li_next;
2502 linee = tv_get_number(&li->li_tv);
2503 if (linee < 0)
2504 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002505
2506 for (row = lines - 1; row < linee && row < height; ++row)
2507 for (col = cols - 1; col < cole && col < width; ++col)
2508 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002509 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002510}
2511
2512/*
2513 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2514 * "col" and "line" are screen coordinates.
2515 */
2516 static int
2517popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2518{
2519 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2520 int line = screenline - wp->w_winrow;
2521
2522 return col >= 0 && col < width
2523 && line >= 0 && line < height
2524 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002525}
2526
2527/*
2528 * Set flags in popup_transparent[] for window "wp" to "val".
2529 */
2530 static void
2531update_popup_transparent(win_T *wp, int val)
2532{
2533 if (wp->w_popup_mask != NULL)
2534 {
2535 int width = popup_width(wp);
2536 int height = popup_height(wp);
2537 listitem_T *lio, *li;
2538 int cols, cole;
2539 int lines, linee;
2540 int col, line;
2541
2542 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2543 {
2544 li = lio->li_tv.vval.v_list->lv_first;
2545 cols = tv_get_number(&li->li_tv);
2546 if (cols < 0)
2547 cols = width + cols + 1;
2548 li = li->li_next;
2549 cole = tv_get_number(&li->li_tv);
2550 if (cole < 0)
2551 cole = width + cole + 1;
2552 li = li->li_next;
2553 lines = tv_get_number(&li->li_tv);
2554 if (lines < 0)
2555 lines = height + lines + 1;
2556 li = li->li_next;
2557 linee = tv_get_number(&li->li_tv);
2558 if (linee < 0)
2559 linee = height + linee + 1;
2560
2561 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002562 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002563 if (cols < 0)
2564 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002565 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002566 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002567 if (lines < 0)
2568 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002569 for (line = lines; line < linee
2570 && line + wp->w_winrow < screen_Rows; ++line)
2571 for (col = cols; col < cole
2572 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002573 popup_transparent[(line + wp->w_winrow) * screen_Columns
2574 + col + wp->w_wincol] = val;
2575 }
2576 }
2577}
2578
2579/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002580 * Update "popup_mask" if needed.
2581 * Also recomputes the popup size and positions.
2582 * Also updates "popup_visible".
2583 * Also marks window lines for redrawing.
2584 */
2585 void
2586may_update_popup_mask(int type)
2587{
2588 win_T *wp;
2589 short *mask;
2590 int line, col;
2591 int redraw_all = FALSE;
2592
2593 // Need to recompute when switching tabs.
2594 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2595 // (such as the screen size) must have changed.
2596 if (popup_mask_tab != curtab || type >= NOT_VALID)
2597 {
2598 popup_mask_refresh = TRUE;
2599 redraw_all = TRUE;
2600 }
2601 if (!popup_mask_refresh)
2602 {
2603 // Check if any buffer has changed.
2604 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2605 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2606 popup_mask_refresh = TRUE;
2607 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2608 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2609 popup_mask_refresh = TRUE;
2610 if (!popup_mask_refresh)
2611 return;
2612 }
2613
2614 // Need to update the mask, something has changed.
2615 popup_mask_refresh = FALSE;
2616 popup_mask_tab = curtab;
2617 popup_visible = FALSE;
2618
2619 // If redrawing everything, just update "popup_mask".
2620 // If redrawing only what is needed, update "popup_mask_next" and then
2621 // compare with "popup_mask" to see what changed.
2622 if (type >= SOME_VALID)
2623 mask = popup_mask;
2624 else
2625 mask = popup_mask_next;
2626 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2627
2628 // Find the window with the lowest zindex that hasn't been handled yet,
2629 // so that the window with a higher zindex overwrites the value in
2630 // popup_mask.
2631 popup_reset_handled();
2632 while ((wp = find_next_popup(TRUE)) != NULL)
2633 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002634 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002635 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002636
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002637 popup_visible = TRUE;
2638
2639 // Recompute the position if the text changed.
2640 if (redraw_all
2641 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2642 popup_adjust_position(wp);
2643
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002644 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002645 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002646 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002647 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002648 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002649 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002650 col < wp->w_wincol + width - wp->w_popup_leftoff
2651 && col < screen_Columns; ++col)
2652 if (wp->w_popup_mask_cells == NULL
2653 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002654 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002655 }
2656
2657 // Only check which lines are to be updated if not already
2658 // updating all lines.
2659 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002660 {
2661 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2662 win_T *prev_wp = NULL;
2663
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002664 for (line = 0; line < screen_Rows; ++line)
2665 {
2666 int col_done = 0;
2667
2668 for (col = 0; col < screen_Columns; ++col)
2669 {
2670 int off = line * screen_Columns + col;
2671
2672 if (popup_mask[off] != popup_mask_next[off])
2673 {
2674 popup_mask[off] = popup_mask_next[off];
2675
2676 if (line >= cmdline_row)
2677 {
2678 // the command line needs to be cleared if text below
2679 // the popup is now visible.
2680 if (!msg_scrolled && popup_mask_next[off] == 0)
2681 clear_cmdline = TRUE;
2682 }
2683 else if (col >= col_done)
2684 {
2685 linenr_T lnum;
2686 int line_cp = line;
2687 int col_cp = col;
2688
2689 // The screen position "line" / "col" needs to be
2690 // redrawn. Figure out what window that is and update
2691 // w_redraw_top and w_redr_bot. Only needs to be done
2692 // once for each window line.
2693 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2694 if (wp != NULL)
2695 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002696 if (wp != prev_wp)
2697 {
2698 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2699 prev_wp = wp;
2700 }
2701
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002702 if (line_cp >= wp->w_height)
2703 // In (or below) status line
2704 wp->w_redr_status = TRUE;
2705 // compute the position in the buffer line from the
2706 // position on the screen
2707 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002708 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002709 // past bottom
2710 wp->w_redr_status = TRUE;
2711 else
2712 redrawWinline(wp, lnum);
2713
2714 // This line is going to be redrawn, no need to
2715 // check until the right side of the window.
2716 col_done = wp->w_wincol + wp->w_width - 1;
2717 }
2718 }
2719 }
2720 }
2721 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002722
2723 vim_free(plines_cache);
2724 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002725}
2726
2727/*
2728 * Return a string of "len" spaces in IObuff.
2729 */
2730 static char_u *
2731get_spaces(int len)
2732{
2733 vim_memset(IObuff, ' ', (size_t)len);
2734 IObuff[len] = NUL;
2735 return IObuff;
2736}
2737
2738/*
2739 * Update popup windows. They are drawn on top of normal windows.
2740 * "win_update" is called for each popup window, lowest zindex first.
2741 */
2742 void
2743update_popups(void (*win_update)(win_T *wp))
2744{
2745 win_T *wp;
2746 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002747 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002748 int total_width;
2749 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002750 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002751 int popup_attr;
2752 int border_attr[4];
2753 int border_char[8];
2754 char_u buf[MB_MAXBYTES];
2755 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002756 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002757 int padcol = 0;
2758 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002759 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002760 int sb_thumb_top = 0;
2761 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002762 int attr_scroll = 0;
2763 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002764
2765 // Find the window with the lowest zindex that hasn't been updated yet,
2766 // so that the window with a higher zindex is drawn later, thus goes on
2767 // top.
2768 popup_reset_handled();
2769 while ((wp = find_next_popup(TRUE)) != NULL)
2770 {
2771 // This drawing uses the zindex of the popup window, so that it's on
2772 // top of the text but doesn't draw when another popup with higher
2773 // zindex is on top of the character.
2774 screen_zindex = wp->w_zindex;
2775
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002776 // Set flags in popup_transparent[] for masked cells.
2777 update_popup_transparent(wp, 1);
2778
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002779 // adjust w_winrow and w_wincol for border and padding, since
2780 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002781 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002782 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2783 - wp->w_popup_leftoff;
2784 if (wp->w_wincol + left_extra < 0)
2785 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002786 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002787 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002788
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002789 // Draw the popup text, unless it's off screen.
2790 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2791 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002792
2793 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002794 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002795
Bram Moolenaard529ba52019-07-02 23:13:53 +02002796 total_width = popup_width(wp);
2797 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002798 popup_attr = get_wcr_attr(wp);
2799
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002800 if (wp->w_winrow + total_height > cmdline_row)
2801 wp->w_popup_flags |= POPF_ON_CMDLINE;
2802 else
2803 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2804
2805
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002806 // We can only use these line drawing characters when 'encoding' is
2807 // "utf-8" and 'ambiwidth' is "single".
2808 if (enc_utf8 && *p_ambw == 's')
2809 {
2810 border_char[0] = border_char[2] = 0x2550;
2811 border_char[1] = border_char[3] = 0x2551;
2812 border_char[4] = 0x2554;
2813 border_char[5] = 0x2557;
2814 border_char[6] = 0x255d;
2815 border_char[7] = 0x255a;
2816 }
2817 else
2818 {
2819 border_char[0] = border_char[2] = '-';
2820 border_char[1] = border_char[3] = '|';
2821 for (i = 4; i < 8; ++i)
2822 border_char[i] = '+';
2823 }
2824 for (i = 0; i < 8; ++i)
2825 if (wp->w_border_char[i] != 0)
2826 border_char[i] = wp->w_border_char[i];
2827
2828 for (i = 0; i < 4; ++i)
2829 {
2830 border_attr[i] = popup_attr;
2831 if (wp->w_border_highlight[i] != NULL)
2832 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2833 }
2834
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002835 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002836 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002837 if (wp->w_popup_border[0] > 0)
2838 {
2839 // top border
2840 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002841 wincol < 0 ? 0 : wincol, wincol + total_width,
2842 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002843 ? border_char[4] : border_char[0],
2844 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002845 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002846 {
2847 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2848 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002849 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002850 }
2851 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002852 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2853 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002854
Bram Moolenaard529ba52019-07-02 23:13:53 +02002855 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2856 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002857 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002858 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2859 - wp->w_has_scrollbar;
2860 if (padcol < 0)
2861 {
2862 padwidth += padcol;
2863 padcol = 0;
2864 }
2865 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002866 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002867 {
2868 // top padding
2869 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002870 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002871 ' ', ' ', popup_attr);
2872 }
2873
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002874 // Title goes on top of border or padding.
2875 if (wp->w_popup_title != NULL)
2876 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2877 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2878
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002879 // Compute scrollbar thumb position and size.
2880 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002881 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002882 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2883
Bram Moolenaar68acb412019-06-26 03:40:36 +02002884 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2885 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002886 if (sb_thumb_height == 0)
2887 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002888 if (linecount <= wp->w_height)
2889 // it just fits, avoid divide by zero
2890 sb_thumb_top = 0;
2891 else
2892 sb_thumb_top = (wp->w_topline - 1
2893 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002894 * (wp->w_height - sb_thumb_height)
2895 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002896 if (wp->w_scrollbar_highlight != NULL)
2897 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2898 else
2899 attr_scroll = highlight_attr[HLF_PSB];
2900 if (wp->w_thumb_highlight != NULL)
2901 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2902 else
2903 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002904 }
2905
2906 for (i = wp->w_popup_border[0];
2907 i < total_height - wp->w_popup_border[2]; ++i)
2908 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002909 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002910 // left and right padding only needed next to the body
2911 int do_padding =
2912 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2913 && i < total_height - wp->w_popup_border[2]
2914 - wp->w_popup_padding[2];
2915
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002916 row = wp->w_winrow + i;
2917
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002918 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002919 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002920 {
2921 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002922 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002923 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002924 if (do_padding && wp->w_popup_padding[3] > 0)
2925 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002926 int col = wincol + wp->w_popup_border[3];
2927
Bram Moolenaard529ba52019-07-02 23:13:53 +02002928 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002929 pad_left = wp->w_popup_padding[3];
2930 if (col < 0)
2931 {
2932 pad_left += col;
2933 col = 0;
2934 }
2935 if (pad_left > 0)
2936 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2937 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002938 // scrollbar
2939 if (wp->w_has_scrollbar)
2940 {
2941 int line = i - top_off;
2942 int scroll_col = wp->w_wincol + total_width - 1
2943 - wp->w_popup_border[1];
2944
2945 if (line >= 0 && line < wp->w_height)
2946 screen_putchar(' ', row, scroll_col,
2947 line >= sb_thumb_top
2948 && line < sb_thumb_top + sb_thumb_height
2949 ? attr_thumb : attr_scroll);
2950 else
2951 screen_putchar(' ', row, scroll_col, popup_attr);
2952 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002953 // right border
2954 if (wp->w_popup_border[1] > 0)
2955 {
2956 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002957 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002958 }
2959 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002960 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002961 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002962 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002963 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2964 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002965 }
2966
2967 if (wp->w_popup_padding[2] > 0)
2968 {
2969 // bottom padding
2970 row = wp->w_winrow + wp->w_popup_border[0]
2971 + wp->w_popup_padding[0] + wp->w_height;
2972 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002973 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002974 }
2975
2976 if (wp->w_popup_border[2] > 0)
2977 {
2978 // bottom border
2979 row = wp->w_winrow + total_height - 1;
2980 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002981 wincol < 0 ? 0 : wincol,
2982 wincol + total_width,
2983 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002984 ? border_char[7] : border_char[2],
2985 border_char[2], border_attr[2]);
2986 if (wp->w_popup_border[1] > 0)
2987 {
2988 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002989 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002990 }
2991 }
2992
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002993 if (wp->w_popup_close == POPCLOSE_BUTTON)
2994 {
2995 // close button goes on top of anything at the top-right corner
2996 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002997 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002998 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2999 }
3000
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003001 update_popup_transparent(wp, 0);
3002
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003003 // Back to the normal zindex.
3004 screen_zindex = 0;
3005 }
3006}
3007
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003008/*
3009 * Mark references in callbacks of one popup window.
3010 */
3011 static int
3012set_ref_in_one_popup(win_T *wp, int copyID)
3013{
3014 int abort = FALSE;
3015 typval_T tv;
3016
3017 if (wp->w_close_cb.cb_partial != NULL)
3018 {
3019 tv.v_type = VAR_PARTIAL;
3020 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3021 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3022 }
3023 if (wp->w_filter_cb.cb_partial != NULL)
3024 {
3025 tv.v_type = VAR_PARTIAL;
3026 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3027 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3028 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003029 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003030 return abort;
3031}
3032
3033/*
3034 * Set reference in callbacks of popup windows.
3035 */
3036 int
3037set_ref_in_popups(int copyID)
3038{
3039 int abort = FALSE;
3040 win_T *wp;
3041 tabpage_T *tp;
3042
3043 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3044 abort = abort || set_ref_in_one_popup(wp, copyID);
3045
3046 FOR_ALL_TABPAGES(tp)
3047 {
3048 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3049 abort = abort || set_ref_in_one_popup(wp, copyID);
3050 if (abort)
3051 break;
3052 }
3053 return abort;
3054}
Bram Moolenaar79648732019-07-18 21:43:07 +02003055
3056/*
3057 * Find an existing popup used as the preview window, in the current tab page.
3058 * Return NULL if not found.
3059 */
3060 win_T *
3061popup_find_preview_window(void)
3062{
3063 win_T *wp;
3064
3065 // Preview window popup is always local to tab page.
3066 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3067 if (wp->w_p_pvw)
3068 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003069 return NULL;
3070}
3071
3072 void
3073f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3074{
3075 win_T *wp = popup_find_preview_window();
3076
3077 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003078}
3079
3080 int
3081popup_is_popup(win_T *wp)
3082{
3083 return wp->w_popup_flags != 0;
3084}
3085
3086/*
3087 * Create a popup to be used as the preview window.
3088 * NOTE: this makes the popup the current window, so that the file can be
3089 * edited. However, it must not remain to be the current window, the caller
3090 * must make sure of that.
3091 */
3092 int
3093popup_create_preview_window(void)
3094{
3095 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
3096
3097 if (wp == NULL)
3098 return FAIL;
3099 wp->w_p_pvw = TRUE;
3100
3101 // Set the width to a reasonable value, so that w_topline can be computed.
3102 if (wp->w_minwidth > 0)
3103 wp->w_width = wp->w_minwidth;
3104 else if (wp->w_maxwidth > 0)
3105 wp->w_width = wp->w_maxwidth;
3106 else
3107 wp->w_width = curwin->w_width;
3108
3109 // Will switch to another buffer soon, dummy one can be wiped.
3110 wp->w_buffer->b_locked = FALSE;
3111
3112 win_enter(wp, FALSE);
3113 return OK;
3114}
3115
3116 void
3117popup_close_preview()
3118{
3119 win_T *wp = popup_find_preview_window();
3120
3121 if (wp != NULL)
3122 {
3123 typval_T res;
3124
3125 res.v_type = VAR_NUMBER;
3126 res.vval.v_number = -1;
3127 popup_close_and_callback(wp, &res);
3128 }
3129}
3130
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003131#endif // FEAT_TEXT_PROP