blob: d22c00dcdbec6257ead1c96cfd41baca2bc2091d [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/*
308 * Handle a click in a popup window, if it is in the scrollbar.
309 */
310 void
311popup_handle_scrollbar_click(win_T *wp, int row, int col)
312{
313 int height = popup_height(wp);
314 int old_topline = wp->w_topline;
315
316 if (wp->w_has_scrollbar == 0)
317 return;
318 if (row >= wp->w_popup_border[0]
319 && row < height - wp->w_popup_border[2]
Bram Moolenaarbd42b312019-07-12 16:35:34 +0200320 && col == popup_width(wp) - wp->w_popup_border[1] - 1)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200321 {
322 if (row >= height / 2)
323 {
324 // Click in lower half, scroll down.
325 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
326 ++wp->w_topline;
327 }
328 else if (wp->w_topline > 1)
329 // click on upper half, scroll up.
330 --wp->w_topline;
331 if (wp->w_topline != old_topline)
332 {
333 popup_set_firstline(wp);
334 redraw_win_later(wp, NOT_VALID);
335 }
336 }
337}
338
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200339#if defined(FEAT_TIMERS)
340 static void
341popup_add_timeout(win_T *wp, int time)
342{
343 char_u cbbuf[50];
344 char_u *ptr = cbbuf;
345 typval_T tv;
346
347 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
348 "{_ -> popup_close(%d)}", wp->w_id);
349 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
350 {
351 wp->w_popup_timer = create_timer(time, 0);
352 wp->w_popup_timer->tr_callback = get_callback(&tv);
353 clear_tv(&tv);
354 }
355}
356#endif
357
Bram Moolenaar17627312019-06-02 19:53:44 +0200358/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200359 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200360 */
361 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200362apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200363{
Bram Moolenaarae943152019-06-16 22:54:14 +0200364 int nr;
365
366 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
367 wp->w_minwidth = nr;
368 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
369 wp->w_minheight = nr;
370 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
371 wp->w_maxwidth = nr;
372 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
373 wp->w_maxheight = nr;
374 get_pos_options(wp, d);
375}
376
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200377 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200378handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
379{
380 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
381 {
382 char_u *s = di->di_tv.vval.v_string;
383 int flags = 0;
384
385 if (STRCMP(s, "word") == 0)
386 flags = FIND_IDENT | FIND_STRING;
387 else if (STRCMP(s, "WORD") == 0)
388 flags = FIND_STRING;
389 else if (STRCMP(s, "expr") == 0)
390 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
391 else if (STRCMP(s, "any") != 0)
392 semsg(_(e_invarg2), s);
393 if (flags != 0)
394 {
395 if (mousemoved)
396 set_mousemoved_columns(wp, flags);
397 else
398 set_moved_columns(wp, flags);
399 }
400 }
401 else if (di->di_tv.v_type == VAR_LIST
402 && di->di_tv.vval.v_list != NULL
403 && di->di_tv.vval.v_list->lv_len == 2)
404 {
405 list_T *l = di->di_tv.vval.v_list;
406 int mincol = tv_get_number(&l->lv_first->li_tv);
407 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
408
409 if (mousemoved)
410 {
411 wp->w_popup_mouse_mincol = mincol;
412 wp->w_popup_mouse_maxcol = maxcol;
413 }
414 else
415 {
416 wp->w_popup_mincol = mincol;
417 wp->w_popup_maxcol = maxcol;
418 }
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422}
423
424 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200425check_highlight(dict_T *dict, char *name, char_u **pval)
426{
427 dictitem_T *di;
428 char_u *str;
429
430 di = dict_find(dict, (char_u *)name, -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_STRING)
434 semsg(_(e_invargval), name);
435 else
436 {
437 str = tv_get_string(&di->di_tv);
438 if (*str != NUL)
439 *pval = vim_strsave(str);
440 }
441 }
442}
443
Bram Moolenaarae943152019-06-16 22:54:14 +0200444/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200445 * Scroll to show the line with the cursor. This assumes lines don't wrap.
446 */
447 static void
448popup_show_curline(win_T *wp)
449{
450 if (wp->w_cursor.lnum < wp->w_topline)
451 wp->w_topline = wp->w_cursor.lnum;
452 else if (wp->w_cursor.lnum >= wp->w_botline)
453 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200454
455 // Don't use "firstline" now.
456 wp->w_firstline = 0;
457}
458
459/*
460 * Get the sign group name for window "wp".
461 * Returns a pointer to a static buffer, overwritten on the next call.
462 */
463 static char_u *
464popup_get_sign_name(win_T *wp)
465{
466 static char buf[30];
467
468 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
469 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200470}
471
472/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200473 * Highlight the line with the cursor.
474 * Also scrolls the text to put the cursor line in view.
475 */
476 static void
477popup_highlight_curline(win_T *wp)
478{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200479 int sign_id = 0;
480 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200481
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200482 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200483
484 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
485 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200486 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200487
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200488 if (!sign_exists_by_name(sign_name))
489 {
490 char *linehl = "PopupSelected";
491
492 if (syn_name2id((char_u *)linehl) == 0)
493 linehl = "PmenuSel";
494 sign_define_by_name(sign_name, NULL,
495 (char_u *)linehl, NULL, NULL);
496 }
497
498 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
499 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
500 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200501 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200502 else
503 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200504}
505
506/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200507 * Shared between popup_create() and f_popup_setoptions().
508 */
509 static void
510apply_general_options(win_T *wp, dict_T *dict)
511{
512 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200513 int nr;
514 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 // TODO: flip
517
518 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200519 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200520 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
521 if (wp->w_firstline < 1)
522 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200523
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200524 di = dict_find(dict, (char_u *)"scrollbar", -1);
525 if (di != NULL)
526 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
527
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200528 str = dict_get_string(dict, (char_u *)"title", FALSE);
529 if (str != NULL)
530 {
531 vim_free(wp->w_popup_title);
532 wp->w_popup_title = vim_strsave(str);
533 }
534
Bram Moolenaar402502d2019-05-30 22:07:36 +0200535 di = dict_find(dict, (char_u *)"wrap", -1);
536 if (di != NULL)
537 {
538 nr = dict_get_number(dict, (char_u *)"wrap");
539 wp->w_p_wrap = nr != 0;
540 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200541
Bram Moolenaara42d9452019-06-15 21:46:30 +0200542 di = dict_find(dict, (char_u *)"drag", -1);
543 if (di != NULL)
544 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200545
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200546 di = dict_find(dict, (char_u *)"close", -1);
547 if (di != NULL)
548 {
549 int ok = TRUE;
550
551 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
552 {
553 char_u *s = di->di_tv.vval.v_string;
554
555 if (STRCMP(s, "none") == 0)
556 wp->w_popup_close = POPCLOSE_NONE;
557 else if (STRCMP(s, "button") == 0)
558 wp->w_popup_close = POPCLOSE_BUTTON;
559 else if (STRCMP(s, "click") == 0)
560 wp->w_popup_close = POPCLOSE_CLICK;
561 else
562 ok = FALSE;
563 }
564 else
565 ok = FALSE;
566 if (!ok)
567 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
568 }
569
Bram Moolenaarae943152019-06-16 22:54:14 +0200570 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
571 if (str != NULL)
572 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
573 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200574
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200575 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
576 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200577 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
578 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200579
Bram Moolenaar790498b2019-06-01 22:15:29 +0200580 di = dict_find(dict, (char_u *)"borderhighlight", -1);
581 if (di != NULL)
582 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200583 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200584 emsg(_(e_listreq));
585 else
586 {
587 list_T *list = di->di_tv.vval.v_list;
588 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200589 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200590
Bram Moolenaar403d0902019-07-17 21:37:32 +0200591 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
592 ++i, li = li->li_next)
593 {
594 str = tv_get_string(&li->li_tv);
595 if (*str != NUL)
596 wp->w_border_highlight[i] = vim_strsave(str);
597 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200598 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
599 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200600 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200601 vim_strsave(wp->w_border_highlight[0]);
602 }
603 }
604
Bram Moolenaar790498b2019-06-01 22:15:29 +0200605 di = dict_find(dict, (char_u *)"borderchars", -1);
606 if (di != NULL)
607 {
608 if (di->di_tv.v_type != VAR_LIST)
609 emsg(_(e_listreq));
610 else
611 {
612 list_T *list = di->di_tv.vval.v_list;
613 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200614 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200615
616 if (list != NULL)
617 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
618 ++i, li = li->li_next)
619 {
620 str = tv_get_string(&li->li_tv);
621 if (*str != NUL)
622 wp->w_border_char[i] = mb_ptr2char(str);
623 }
624 if (list->lv_len == 1)
625 for (i = 1; i < 8; ++i)
626 wp->w_border_char[i] = wp->w_border_char[0];
627 if (list->lv_len == 2)
628 {
629 for (i = 4; i < 8; ++i)
630 wp->w_border_char[i] = wp->w_border_char[1];
631 for (i = 1; i < 4; ++i)
632 wp->w_border_char[i] = wp->w_border_char[0];
633 }
634 }
635 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200636
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200637 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
638 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
639
Bram Moolenaarae943152019-06-16 22:54:14 +0200640 di = dict_find(dict, (char_u *)"zindex", -1);
641 if (di != NULL)
642 {
643 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
644 if (wp->w_zindex < 1)
645 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
646 if (wp->w_zindex > 32000)
647 wp->w_zindex = 32000;
648 }
649
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200650 di = dict_find(dict, (char_u *)"mask", -1);
651 if (di != NULL)
652 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200653 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200654
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200655 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200656 {
657 listitem_T *li;
658
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200659 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200660 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
661 li = li->li_next)
662 {
663 if (li->li_tv.v_type != VAR_LIST
664 || li->li_tv.vval.v_list == NULL
665 || li->li_tv.vval.v_list->lv_len != 4)
666 {
667 ok = FALSE;
668 break;
669 }
670 }
671 }
672 if (ok)
673 {
674 wp->w_popup_mask = di->di_tv.vval.v_list;
675 ++wp->w_popup_mask->lv_refcount;
676 }
677 else
678 semsg(_(e_invargval), "mask");
679 }
680
Bram Moolenaarae943152019-06-16 22:54:14 +0200681#if defined(FEAT_TIMERS)
682 // Add timer to close the popup after some time.
683 nr = dict_get_number(dict, (char_u *)"time");
684 if (nr > 0)
685 popup_add_timeout(wp, nr);
686#endif
687
Bram Moolenaar3397f742019-06-02 18:40:06 +0200688 di = dict_find(dict, (char_u *)"moved", -1);
689 if (di != NULL)
690 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200691 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200692 handle_moved_argument(wp, di, FALSE);
693 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200694
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200695 di = dict_find(dict, (char_u *)"mousemoved", -1);
696 if (di != NULL)
697 {
698 set_mousemoved_values(wp);
699 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200700 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200701
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200702 di = dict_find(dict, (char_u *)"cursorline", -1);
703 if (di != NULL)
704 {
705 if (di->di_tv.v_type == VAR_NUMBER)
706 {
707 if (di->di_tv.vval.v_number != 0)
708 wp->w_popup_flags |= POPF_CURSORLINE;
709 else
710 wp->w_popup_flags &= ~POPF_CURSORLINE;
711 }
712 else
713 semsg(_(e_invargval), "cursorline");
714 }
715
Bram Moolenaarae943152019-06-16 22:54:14 +0200716 di = dict_find(dict, (char_u *)"filter", -1);
717 if (di != NULL)
718 {
719 callback_T callback = get_callback(&di->di_tv);
720
721 if (callback.cb_name != NULL)
722 {
723 free_callback(&wp->w_filter_cb);
724 set_callback(&wp->w_filter_cb, &callback);
725 }
726 }
727
728 di = dict_find(dict, (char_u *)"callback", -1);
729 if (di != NULL)
730 {
731 callback_T callback = get_callback(&di->di_tv);
732
733 if (callback.cb_name != NULL)
734 {
735 free_callback(&wp->w_close_cb);
736 set_callback(&wp->w_close_cb, &callback);
737 }
738 }
739}
740
741/*
742 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200743 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200744 */
745 static void
746apply_options(win_T *wp, dict_T *dict)
747{
748 int nr;
749
750 apply_move_options(wp, dict);
751 apply_general_options(wp, dict);
752
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200753 nr = dict_get_number(dict, (char_u *)"hidden");
754 if (nr > 0)
755 {
756 wp->w_popup_flags |= POPF_HIDDEN;
757 --wp->w_buffer->b_nwindows;
758 }
759
Bram Moolenaar33796b32019-06-08 16:01:13 +0200760 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200761 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200762}
763
764/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200765 * Add lines to the popup from a list of strings.
766 */
767 static void
768add_popup_strings(buf_T *buf, list_T *l)
769{
770 listitem_T *li;
771 linenr_T lnum = 0;
772 char_u *p;
773
774 for (li = l->lv_first; li != NULL; li = li->li_next)
775 if (li->li_tv.v_type == VAR_STRING)
776 {
777 p = li->li_tv.vval.v_string;
778 ml_append_buf(buf, lnum++,
779 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
780 }
781}
782
783/*
784 * Add lines to the popup from a list of dictionaries.
785 */
786 static void
787add_popup_dicts(buf_T *buf, list_T *l)
788{
789 listitem_T *li;
790 listitem_T *pli;
791 linenr_T lnum = 0;
792 char_u *p;
793 dict_T *dict;
794
795 // first add the text lines
796 for (li = l->lv_first; li != NULL; li = li->li_next)
797 {
798 if (li->li_tv.v_type != VAR_DICT)
799 {
800 emsg(_(e_dictreq));
801 return;
802 }
803 dict = li->li_tv.vval.v_dict;
804 p = dict == NULL ? NULL
805 : dict_get_string(dict, (char_u *)"text", FALSE);
806 ml_append_buf(buf, lnum++,
807 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
808 }
809
810 // add the text properties
811 lnum = 1;
812 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
813 {
814 dictitem_T *di;
815 list_T *plist;
816
817 dict = li->li_tv.vval.v_dict;
818 di = dict_find(dict, (char_u *)"props", -1);
819 if (di != NULL)
820 {
821 if (di->di_tv.v_type != VAR_LIST)
822 {
823 emsg(_(e_listreq));
824 return;
825 }
826 plist = di->di_tv.vval.v_list;
827 if (plist != NULL)
828 {
829 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
830 {
831 if (pli->li_tv.v_type != VAR_DICT)
832 {
833 emsg(_(e_dictreq));
834 return;
835 }
836 dict = pli->li_tv.vval.v_dict;
837 if (dict != NULL)
838 {
839 int col = dict_get_number(dict, (char_u *)"col");
840
841 prop_add_common( lnum, col, dict, buf, NULL);
842 }
843 }
844 }
845 }
846 }
847}
848
849/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200850 * Get the padding plus border at the top, adjusted to 1 if there is a title.
851 */
852 static int
853popup_top_extra(win_T *wp)
854{
855 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
856
857 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
858 return 1;
859 return extra;
860}
861
862/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200863 * Return the height of popup window "wp", including border and padding.
864 */
865 int
866popup_height(win_T *wp)
867{
868 return wp->w_height
869 + popup_top_extra(wp)
870 + wp->w_popup_padding[2] + wp->w_popup_border[2];
871}
872
873/*
874 * Return the width of popup window "wp", including border, padding and
875 * scrollbar.
876 */
877 int
878popup_width(win_T *wp)
879{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200880 // w_leftcol is how many columns of the core are left of the screen
881 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200882 return wp->w_width + wp->w_leftcol
883 + wp->w_popup_padding[3] + wp->w_popup_border[3]
884 + wp->w_popup_padding[1] + wp->w_popup_border[1]
885 + wp->w_has_scrollbar
886 + wp->w_popup_rightoff;
887}
888
889/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200890 * Adjust the position and size of the popup to fit on the screen.
891 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200892 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200893popup_adjust_position(win_T *wp)
894{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200895 linenr_T lnum;
896 int wrapped = 0;
897 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200898 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200899 int center_vert = FALSE;
900 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200901 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200902 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200903 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
904 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
905 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
906 int extra_height = top_extra + bot_extra;
907 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200908 int org_winrow = wp->w_winrow;
909 int org_wincol = wp->w_wincol;
910 int org_width = wp->w_width;
911 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200912 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200913 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200914 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200915
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200916 wp->w_winrow = 0;
917 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200918 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200919 wp->w_popup_leftoff = 0;
920 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200921 if (wp->w_popup_pos == POPPOS_CENTER)
922 {
923 // center after computing the size
924 center_vert = TRUE;
925 center_hor = TRUE;
926 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200927 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200928 {
929 if (wp->w_wantline == 0)
930 center_vert = TRUE;
931 else if (wp->w_popup_pos == POPPOS_TOPLEFT
932 || wp->w_popup_pos == POPPOS_TOPRIGHT)
933 {
934 wp->w_winrow = wp->w_wantline - 1;
935 if (wp->w_winrow >= Rows)
936 wp->w_winrow = Rows - 1;
937 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200938
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200939 if (wp->w_wantcol == 0)
940 center_hor = TRUE;
941 else if (wp->w_popup_pos == POPPOS_TOPLEFT
942 || wp->w_popup_pos == POPPOS_BOTLEFT)
943 {
944 wp->w_wincol = wp->w_wantcol - 1;
945 if (wp->w_wincol >= Columns - 3)
946 wp->w_wincol = Columns - 3;
947 }
948 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200949
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200950 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200951 // When left aligned use the space available, but shift to the left when we
952 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200953 maxspace = Columns - wp->w_wincol - left_extra;
954 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200955 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200956 {
957 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200958 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200959 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200960
Bram Moolenaar8d241042019-06-12 23:40:01 +0200961 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +0200962 if (wp->w_firstline != 0)
963 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200964 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
965 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
966
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200967 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200968 // Use a minimum width of one, so that something shows when there is no
969 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200970 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200971 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200972 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200973 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200974 // count Tabs for what they are worth
975 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
976 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200977
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200978 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200979 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200980 while (len > maxwidth)
981 {
982 ++wrapped;
983 len -= maxwidth;
984 wp->w_width = maxwidth;
985 }
986 }
987 else if (len > maxwidth
988 && allow_adjust_left
989 && (wp->w_popup_pos == POPPOS_TOPLEFT
990 || wp->w_popup_pos == POPPOS_BOTLEFT))
991 {
992 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200993 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200994
Bram Moolenaar51c31312019-06-15 22:27:23 +0200995 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200996 {
997 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200998
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200999 len -= truncate_shift;
1000 shift_by -= truncate_shift;
1001 }
1002
1003 wp->w_wincol -= shift_by;
1004 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001005 wp->w_width = maxwidth;
1006 }
1007 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001008 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001009 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001010 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1011 wp->w_width = wp->w_maxwidth;
1012 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001013 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001014 if (wp->w_maxheight > 0
1015 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001016 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001017 }
1018
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001019 wp->w_has_scrollbar = wp->w_want_scrollbar
1020 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
1021
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001022 minwidth = wp->w_minwidth;
1023 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1024 {
1025 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1026
1027 if (minwidth < title_len)
1028 minwidth = title_len;
1029 }
1030
1031 if (minwidth > 0 && wp->w_width < minwidth)
1032 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001033 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001034 {
1035 if (wp->w_width > maxspace)
1036 // some columns cut off on the right
1037 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001038 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001039 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001040 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001041 {
1042 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1043 if (wp->w_wincol < 0)
1044 wp->w_wincol = 0;
1045 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001046 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1047 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1048 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001049 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1050
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001051 // Right aligned: move to the right if needed.
1052 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001053 if (leftoff >= 0)
1054 wp->w_wincol = leftoff;
1055 else if (wp->w_popup_fixed)
1056 {
1057 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001058 // columns when displaying the window, minus left border and
1059 // padding.
1060 if (-leftoff > left_extra)
1061 wp->w_leftcol = -leftoff - left_extra;
1062 wp->w_width -= wp->w_leftcol;
1063 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001064 if (wp->w_width < 0)
1065 wp->w_width = 0;
1066 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001067 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001068
Bram Moolenaar8d241042019-06-12 23:40:01 +02001069 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1070 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001071 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1072 wp->w_height = wp->w_minheight;
1073 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1074 wp->w_height = wp->w_maxheight;
1075 if (wp->w_height > Rows - wp->w_winrow)
1076 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001077
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001078 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001079 {
1080 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1081 if (wp->w_winrow < 0)
1082 wp->w_winrow = 0;
1083 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001084 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1085 || wp->w_popup_pos == POPPOS_BOTLEFT)
1086 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001087 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001088 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001089 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001090 else
1091 // not enough space, make top aligned
1092 wp->w_winrow = wp->w_wantline + 1;
1093 }
1094
Bram Moolenaar17146962019-05-30 00:12:11 +02001095 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001096
1097 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001098 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001099 if (org_winrow != wp->w_winrow
1100 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001101 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001102 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001103 || org_width != wp->w_width
1104 || org_height != wp->w_height)
1105 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001106 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001107 popup_mask_refresh = TRUE;
1108 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001109}
1110
Bram Moolenaar17627312019-06-02 19:53:44 +02001111typedef enum
1112{
1113 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001114 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001115 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001116 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001117 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001118 TYPE_MENU,
1119 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001120} create_type_T;
1121
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001122/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001123 * Make "buf" empty and set the contents to "text".
1124 * Used by popup_create() and popup_settext().
1125 */
1126 static void
1127popup_set_buffer_text(buf_T *buf, typval_T text)
1128{
1129 int lnum;
1130
1131 // Clear the buffer, then replace the lines.
1132 curbuf = buf;
1133 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1134 ml_delete(lnum, FALSE);
1135 curbuf = curwin->w_buffer;
1136
1137 // Add text to the buffer.
1138 if (text.v_type == VAR_STRING)
1139 {
1140 // just a string
1141 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1142 }
1143 else
1144 {
1145 list_T *l = text.vval.v_list;
1146
1147 if (l->lv_len > 0)
1148 {
1149 if (l->lv_first->li_tv.v_type == VAR_STRING)
1150 // list of strings
1151 add_popup_strings(buf, l);
1152 else
1153 // list of dictionaries
1154 add_popup_dicts(buf, l);
1155 }
1156 }
1157
1158 // delete the line that was in the empty buffer
1159 curbuf = buf;
1160 ml_delete(buf->b_ml.ml_line_count, FALSE);
1161 curbuf = curwin->w_buffer;
1162}
1163
1164/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001165 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1166 * not NULL.
1167 * Return FAIL if the parsing fails.
1168 */
1169 int
1170parse_previewpopup(win_T *wp)
1171{
1172 char_u *p;
1173
1174 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1175 {
1176 char_u *e, *dig;
1177 char_u *s = p;
1178 int x;
1179
1180 e = vim_strchr(p, ':');
1181 if (e == NULL || e[1] == NUL)
1182 return FAIL;
1183
1184 p = vim_strchr(e, ',');
1185 if (p == NULL)
1186 p = e + STRLEN(e);
1187 dig = e + 1;
1188 x = getdigits(&dig);
1189 if (dig != p)
1190 return FAIL;
1191
1192 if (STRNCMP(s, "height:", 7) == 0)
1193 {
1194 if (wp != NULL)
1195 {
1196 wp->w_minheight = x;
1197 wp->w_maxheight = x;
1198 }
1199 }
1200 else if (STRNCMP(s, "width:", 6) == 0)
1201 {
1202 if (wp != NULL)
1203 {
1204 wp->w_minwidth = x;
1205 wp->w_maxwidth = x;
1206 }
1207 }
1208 else
1209 return FAIL;
1210 }
1211 return OK;
1212}
1213
1214/*
1215 * Set w_wantline and w_wantcol for the cursor position in the current window.
1216 */
1217 void
1218popup_set_wantpos(win_T *wp)
1219{
1220 setcursor_mayforce(TRUE);
1221 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1222 if (wp->w_wantline == 0) // cursor in first line
1223 {
1224 wp->w_wantline = 2;
1225 wp->w_popup_pos = POPPOS_TOPLEFT;
1226 }
1227 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
1228 popup_adjust_position(wp);
1229}
1230
1231/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001232 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001233 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001234 * etc.
1235 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001236 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001237 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001238popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001239{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001240 win_T *wp;
1241 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001242 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001243 int new_buffer;
1244 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001245 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001246 int nr;
1247 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001248
Bram Moolenaar79648732019-07-18 21:43:07 +02001249 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001250 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001251 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001252 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001253 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001254 buf = buflist_findnr( argvars[0].vval.v_number);
1255 if (buf == NULL)
1256 {
1257 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1258 return NULL;
1259 }
1260 }
1261 else if (!(argvars[0].v_type == VAR_STRING
1262 && argvars[0].vval.v_string != NULL)
1263 && !(argvars[0].v_type == VAR_LIST
1264 && argvars[0].vval.v_list != NULL))
1265 {
1266 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001267 return NULL;
1268 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001269 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001270 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001271 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001272 return NULL;
1273 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001274 d = argvars[1].vval.v_dict;
1275 }
1276
1277 if (d != NULL)
1278 {
1279 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1280 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1281 else if (type == TYPE_NOTIFICATION)
1282 tabnr = -1; // notifications are global by default
1283 else
1284 tabnr = 0;
1285 if (tabnr > 0)
1286 {
1287 tp = find_tabpage(tabnr);
1288 if (tp == NULL)
1289 {
1290 semsg(_("E997: Tabpage not found: %d"), tabnr);
1291 return NULL;
1292 }
1293 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001294 }
1295
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001296 // Create the window and buffer.
1297 wp = win_alloc_popup_win();
1298 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001299 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001300 if (rettv != NULL)
1301 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001302 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001303 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001304
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001305 if (buf != NULL)
1306 {
1307 // use existing buffer
1308 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001309 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001310 buffer_ensure_loaded(buf);
1311 }
1312 else
1313 {
1314 // create a new buffer associated with the popup
1315 new_buffer = TRUE;
1316 buf = buflist_new(NULL, NULL, (linenr_T)0,
1317 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1318 if (buf == NULL)
1319 return NULL;
1320 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001321
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001322 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001323
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001324 set_local_options_default(wp);
1325 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001326 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001327 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001328 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001329 buf->b_p_ul = -1; // no undo
1330 buf->b_p_swf = FALSE; // no swap file
1331 buf->b_p_bl = FALSE; // unlisted buffer
1332 buf->b_locked = TRUE;
1333 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001334
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001335 // Avoid that 'buftype' is reset when this buffer is entered.
1336 buf->b_p_initialized = TRUE;
1337 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001338
Bram Moolenaara3fce622019-06-20 02:31:49 +02001339 if (tp != NULL)
1340 {
1341 // popup on specified tab page
1342 wp->w_next = tp->tp_first_popupwin;
1343 tp->tp_first_popupwin = wp;
1344 }
1345 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001346 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001347 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001348 wp->w_next = curtab->tp_first_popupwin;
1349 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001350 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001351 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001352 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001353 win_T *prev = first_popupwin;
1354
1355 // Global popup: add at the end, so that it gets displayed on top of
1356 // older ones with the same zindex. Matters for notifications.
1357 if (first_popupwin == NULL)
1358 first_popupwin = wp;
1359 else
1360 {
1361 while (prev->w_next != NULL)
1362 prev = prev->w_next;
1363 prev->w_next = wp;
1364 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001365 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001366
Bram Moolenaar79648732019-07-18 21:43:07 +02001367 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001368 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001369
Bram Moolenaar79648732019-07-18 21:43:07 +02001370 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001371 {
1372 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001373 popup_set_wantpos(wp);
1374 }
1375 if (type == TYPE_ATCURSOR)
1376 {
Bram Moolenaar17627312019-06-02 19:53:44 +02001377 set_moved_values(wp);
1378 set_moved_columns(wp, FIND_STRING);
1379 }
1380
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001381 if (type == TYPE_BEVAL)
1382 {
1383 wp->w_popup_pos = POPPOS_BOTLEFT;
1384
1385 // by default use the mouse position
1386 wp->w_wantline = mouse_row;
1387 if (wp->w_wantline <= 0) // mouse on first line
1388 {
1389 wp->w_wantline = 2;
1390 wp->w_popup_pos = POPPOS_TOPLEFT;
1391 }
1392 wp->w_wantcol = mouse_col + 1;
1393 set_mousemoved_values(wp);
1394 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1395 }
1396
Bram Moolenaar33796b32019-06-08 16:01:13 +02001397 // set default values
1398 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001399 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001400
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001401 if (type == TYPE_NOTIFICATION)
1402 {
1403 win_T *twp, *nextwin;
1404 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001405
1406 // Try to not overlap with another global popup. Guess we need 3
1407 // more screen lines than buffer lines.
1408 wp->w_wantline = 1;
1409 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1410 {
1411 nextwin = twp->w_next;
1412 if (twp != wp
1413 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1414 && twp->w_winrow <= wp->w_wantline - 1 + height
1415 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1416 {
1417 // move to below this popup and restart the loop to check for
1418 // overlap with other popups
1419 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1420 nextwin = first_popupwin;
1421 }
1422 }
1423 if (wp->w_wantline + height > Rows)
1424 {
1425 // can't avoid overlap, put on top in the hope that message goes
1426 // away soon.
1427 wp->w_wantline = 1;
1428 }
1429
1430 wp->w_wantcol = 10;
1431 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001432 wp->w_minwidth = 20;
1433 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001434 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001435 for (i = 0; i < 4; ++i)
1436 wp->w_popup_border[i] = 1;
1437 wp->w_popup_padding[1] = 1;
1438 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001439
1440 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001441 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001442 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1443 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001444 }
1445
Bram Moolenaara730e552019-06-16 19:05:31 +02001446 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001447 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001448 wp->w_popup_pos = POPPOS_CENTER;
1449 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1450 wp->w_popup_drag = 1;
1451 for (i = 0; i < 4; ++i)
1452 {
1453 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001454 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001455 }
1456 }
1457
Bram Moolenaara730e552019-06-16 19:05:31 +02001458 if (type == TYPE_MENU)
1459 {
1460 typval_T tv;
1461 callback_T callback;
1462
1463 tv.v_type = VAR_STRING;
1464 tv.vval.v_string = (char_u *)"popup_filter_menu";
1465 callback = get_callback(&tv);
1466 if (callback.cb_name != NULL)
1467 set_callback(&wp->w_filter_cb, &callback);
1468
1469 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001470 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001471 }
1472
Bram Moolenaar79648732019-07-18 21:43:07 +02001473 if (type == TYPE_PREVIEW)
1474 {
1475 wp->w_popup_drag = 1;
1476 wp->w_popup_close = POPCLOSE_BUTTON;
1477 for (i = 0; i < 4; ++i)
1478 wp->w_popup_border[i] = 1;
1479 parse_previewpopup(wp);
1480 }
1481
Bram Moolenaarae943152019-06-16 22:54:14 +02001482 for (i = 0; i < 4; ++i)
1483 VIM_CLEAR(wp->w_border_highlight[i]);
1484 for (i = 0; i < 8; ++i)
1485 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001486 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001487 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001488
Bram Moolenaar79648732019-07-18 21:43:07 +02001489 if (d != NULL)
1490 // Deal with options.
1491 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001492
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001493#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001494 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1495 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001496#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001497
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001498 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001499
1500 wp->w_vsep_width = 0;
1501
1502 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001503 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001504
1505 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001506}
1507
1508/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001509 * popup_clear()
1510 */
1511 void
1512f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1513{
1514 close_all_popups();
1515}
1516
1517/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001518 * popup_create({text}, {options})
1519 */
1520 void
1521f_popup_create(typval_T *argvars, typval_T *rettv)
1522{
Bram Moolenaar17627312019-06-02 19:53:44 +02001523 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001524}
1525
1526/*
1527 * popup_atcursor({text}, {options})
1528 */
1529 void
1530f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1531{
Bram Moolenaar17627312019-06-02 19:53:44 +02001532 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001533}
1534
1535/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001536 * popup_beval({text}, {options})
1537 */
1538 void
1539f_popup_beval(typval_T *argvars, typval_T *rettv)
1540{
1541 popup_create(argvars, rettv, TYPE_BEVAL);
1542}
1543
1544/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001545 * Invoke the close callback for window "wp" with value "result".
1546 * Careful: The callback may make "wp" invalid!
1547 */
1548 static void
1549invoke_popup_callback(win_T *wp, typval_T *result)
1550{
1551 typval_T rettv;
1552 int dummy;
1553 typval_T argv[3];
1554
1555 argv[0].v_type = VAR_NUMBER;
1556 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1557
1558 if (result != NULL && result->v_type != VAR_UNKNOWN)
1559 copy_tv(result, &argv[1]);
1560 else
1561 {
1562 argv[1].v_type = VAR_NUMBER;
1563 argv[1].vval.v_number = 0;
1564 }
1565
1566 argv[2].v_type = VAR_UNKNOWN;
1567
1568 call_callback(&wp->w_close_cb, -1,
1569 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1570 if (result != NULL)
1571 clear_tv(&argv[1]);
1572 clear_tv(&rettv);
1573}
1574
1575/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001576 * Close popup "wp" and invoke any close callback for it.
1577 */
1578 static void
1579popup_close_and_callback(win_T *wp, typval_T *arg)
1580{
1581 int id = wp->w_id;
1582
1583 if (wp->w_close_cb.cb_name != NULL)
1584 // Careful: This may make "wp" invalid.
1585 invoke_popup_callback(wp, arg);
1586
1587 popup_close(id);
1588}
1589
1590/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001591 * Close popup "wp" because of a mouse click.
1592 */
1593 void
1594popup_close_for_mouse_click(win_T *wp)
1595{
1596 typval_T res;
1597
1598 res.v_type = VAR_NUMBER;
1599 res.vval.v_number = -2;
1600 popup_close_and_callback(wp, &res);
1601}
1602
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001603 static void
1604check_mouse_moved(win_T *wp, win_T *mouse_wp)
1605{
1606 // Close the popup when all if these are true:
1607 // - the mouse is not on this popup
1608 // - "mousemoved" was used
1609 // - the mouse is no longer on the same screen row or the mouse column is
1610 // outside of the relevant text
1611 if (wp != mouse_wp
1612 && wp->w_popup_mouse_row != 0
1613 && (wp->w_popup_mouse_row != mouse_row
1614 || mouse_col < wp->w_popup_mouse_mincol
1615 || mouse_col > wp->w_popup_mouse_maxcol))
1616 {
1617 typval_T res;
1618
1619 res.v_type = VAR_NUMBER;
1620 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001621 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001622 popup_close_and_callback(wp, &res);
1623 }
1624}
1625
1626/*
1627 * Called when the mouse moved: may close a popup with "mousemoved".
1628 */
1629 void
1630popup_handle_mouse_moved(void)
1631{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001632 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001633 win_T *mouse_wp;
1634 int row = mouse_row;
1635 int col = mouse_col;
1636
1637 // find the window where the mouse is in
1638 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1639
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001640 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1641 {
1642 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001643 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001644 }
1645 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1646 {
1647 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001648 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001649 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001650}
1651
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001652/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001653 * In a filter: check if the typed key is a mouse event that is used for
1654 * dragging the popup.
1655 */
1656 static void
1657filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1658{
1659 int row = mouse_row;
1660 int col = mouse_col;
1661
1662 if (wp->w_popup_drag
1663 && is_mouse_key(c)
1664 && (wp == popup_dragwin
1665 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1666 // do not consume the key, allow for dragging the popup
1667 rettv->vval.v_number = 0;
1668}
1669
Bram Moolenaara730e552019-06-16 19:05:31 +02001670/*
1671 * popup_filter_menu({text}, {options})
1672 */
1673 void
1674f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1675{
1676 int id = tv_get_number(&argvars[0]);
1677 win_T *wp = win_id2wp(id);
1678 char_u *key = tv_get_string(&argvars[1]);
1679 typval_T res;
1680 int c;
1681 linenr_T old_lnum;
1682
1683 // If the popup has been closed do not consume the key.
1684 if (wp == NULL)
1685 return;
1686
1687 c = *key;
1688 if (c == K_SPECIAL && key[1] != NUL)
1689 c = TO_SPECIAL(key[1], key[2]);
1690
1691 // consume all keys until done
1692 rettv->vval.v_number = 1;
1693 res.v_type = VAR_NUMBER;
1694
1695 old_lnum = wp->w_cursor.lnum;
1696 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1697 --wp->w_cursor.lnum;
1698 if ((c == 'j' || c == 'J' || c == K_DOWN)
1699 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1700 ++wp->w_cursor.lnum;
1701 if (old_lnum != wp->w_cursor.lnum)
1702 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001703 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001704 return;
1705 }
1706
1707 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1708 {
1709 // Cancelled, invoke callback with -1
1710 res.vval.v_number = -1;
1711 popup_close_and_callback(wp, &res);
1712 return;
1713 }
1714 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1715 {
1716 // Invoke callback with current index.
1717 res.vval.v_number = wp->w_cursor.lnum;
1718 popup_close_and_callback(wp, &res);
1719 return;
1720 }
1721
1722 filter_handle_drag(wp, c, rettv);
1723}
1724
1725/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001726 * popup_filter_yesno({text}, {options})
1727 */
1728 void
1729f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1730{
1731 int id = tv_get_number(&argvars[0]);
1732 win_T *wp = win_id2wp(id);
1733 char_u *key = tv_get_string(&argvars[1]);
1734 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001735 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001736
1737 // If the popup has been closed don't consume the key.
1738 if (wp == NULL)
1739 return;
1740
Bram Moolenaara730e552019-06-16 19:05:31 +02001741 c = *key;
1742 if (c == K_SPECIAL && key[1] != NUL)
1743 c = TO_SPECIAL(key[1], key[2]);
1744
Bram Moolenaara42d9452019-06-15 21:46:30 +02001745 // consume all keys until done
1746 rettv->vval.v_number = 1;
1747
Bram Moolenaara730e552019-06-16 19:05:31 +02001748 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001749 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001750 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001751 res.vval.v_number = 0;
1752 else
1753 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001754 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001755 return;
1756 }
1757
1758 // Invoke callback
1759 res.v_type = VAR_NUMBER;
1760 popup_close_and_callback(wp, &res);
1761}
1762
1763/*
1764 * popup_dialog({text}, {options})
1765 */
1766 void
1767f_popup_dialog(typval_T *argvars, typval_T *rettv)
1768{
1769 popup_create(argvars, rettv, TYPE_DIALOG);
1770}
1771
1772/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001773 * popup_menu({text}, {options})
1774 */
1775 void
1776f_popup_menu(typval_T *argvars, typval_T *rettv)
1777{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001778 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001779}
1780
1781/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001782 * popup_notification({text}, {options})
1783 */
1784 void
1785f_popup_notification(typval_T *argvars, typval_T *rettv)
1786{
1787 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1788}
1789
1790/*
1791 * Find the popup window with window-ID "id".
1792 * If the popup window does not exist NULL is returned.
1793 * If the window is not a popup window, and error message is given.
1794 */
1795 static win_T *
1796find_popup_win(int id)
1797{
1798 win_T *wp = win_id2wp(id);
1799
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001800 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001801 {
1802 semsg(_("E993: window %d is not a popup window"), id);
1803 return NULL;
1804 }
1805 return wp;
1806}
1807
1808/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001809 * popup_close({id})
1810 */
1811 void
1812f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1813{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001814 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001815 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001816
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001817 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001818 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001819}
1820
1821/*
1822 * popup_hide({id})
1823 */
1824 void
1825f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1826{
1827 int id = (int)tv_get_number(argvars);
1828 win_T *wp = find_popup_win(id);
1829
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001830 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001831 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001832 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001833 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001834 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001835 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001836 }
1837}
1838
1839/*
1840 * popup_show({id})
1841 */
1842 void
1843f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1844{
1845 int id = (int)tv_get_number(argvars);
1846 win_T *wp = find_popup_win(id);
1847
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001848 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001849 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001850 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001851 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001852 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001853 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001854 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001855}
1856
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001857/*
1858 * popup_settext({id}, {text})
1859 */
1860 void
1861f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1862{
1863 int id = (int)tv_get_number(&argvars[0]);
1864 win_T *wp = find_popup_win(id);
1865
1866 if (wp != NULL)
1867 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001868 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1869 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1870 else
1871 {
1872 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1873 popup_adjust_position(wp);
1874 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001875 }
1876}
1877
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001878 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001879popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001880{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001881 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001882 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001883 if (wp->w_winrow + wp->w_height >= cmdline_row)
1884 clear_cmdline = TRUE;
1885 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001886
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001887 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001888 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001889}
1890
Bram Moolenaarec583842019-05-26 14:11:23 +02001891/*
1892 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001893 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001894 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001895 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001896popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001897{
1898 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001899 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001900 win_T *prev = NULL;
1901
Bram Moolenaarec583842019-05-26 14:11:23 +02001902 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001903 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001904 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001905 {
1906 if (prev == NULL)
1907 first_popupwin = wp->w_next;
1908 else
1909 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001910 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001911 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001912 }
1913
Bram Moolenaarec583842019-05-26 14:11:23 +02001914 // go through tab-local popups
1915 FOR_ALL_TABPAGES(tp)
1916 popup_close_tabpage(tp, id);
1917}
1918
1919/*
1920 * Close a popup window with Window-id "id" in tabpage "tp".
1921 */
1922 void
1923popup_close_tabpage(tabpage_T *tp, int id)
1924{
1925 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001926 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001927 win_T *prev = NULL;
1928
Bram Moolenaarec583842019-05-26 14:11:23 +02001929 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1930 if (wp->w_id == id)
1931 {
1932 if (prev == NULL)
1933 *root = wp->w_next;
1934 else
1935 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001936 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001937 return;
1938 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001939}
1940
1941 void
1942close_all_popups(void)
1943{
1944 while (first_popupwin != NULL)
1945 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001946 while (curtab->tp_first_popupwin != NULL)
1947 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001948}
1949
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001950/*
1951 * popup_move({id}, {options})
1952 */
1953 void
1954f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1955{
Bram Moolenaarae943152019-06-16 22:54:14 +02001956 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001957 int id = (int)tv_get_number(argvars);
1958 win_T *wp = find_popup_win(id);
1959
1960 if (wp == NULL)
1961 return; // invalid {id}
1962
1963 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1964 {
1965 emsg(_(e_dictreq));
1966 return;
1967 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001968 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001969
Bram Moolenaarae943152019-06-16 22:54:14 +02001970 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001971
1972 if (wp->w_winrow + wp->w_height >= cmdline_row)
1973 clear_cmdline = TRUE;
1974 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001975}
1976
Bram Moolenaarbc133542019-05-29 20:26:46 +02001977/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001978 * popup_setoptions({id}, {options})
1979 */
1980 void
1981f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1982{
1983 dict_T *dict;
1984 int id = (int)tv_get_number(argvars);
1985 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001986 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001987
1988 if (wp == NULL)
1989 return; // invalid {id}
1990
1991 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1992 {
1993 emsg(_(e_dictreq));
1994 return;
1995 }
1996 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001997 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001998
1999 apply_move_options(wp, dict);
2000 apply_general_options(wp, dict);
2001
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002002 if (old_firstline != wp->w_firstline)
2003 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002004 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002005 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002006 popup_adjust_position(wp);
2007}
2008
2009/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002010 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002011 */
2012 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002013f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002014{
2015 dict_T *dict;
2016 int id = (int)tv_get_number(argvars);
2017 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002018 int top_extra;
2019 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002020
2021 if (rettv_dict_alloc(rettv) == OK)
2022 {
2023 if (wp == NULL)
2024 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002025 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002026 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2027
Bram Moolenaarbc133542019-05-29 20:26:46 +02002028 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002029
Bram Moolenaarbc133542019-05-29 20:26:46 +02002030 dict_add_number(dict, "line", wp->w_winrow + 1);
2031 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002032 dict_add_number(dict, "width", wp->w_width + left_extra
2033 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2034 dict_add_number(dict, "height", wp->w_height + top_extra
2035 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002036
2037 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2038 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2039 dict_add_number(dict, "core_width", wp->w_width);
2040 dict_add_number(dict, "core_height", wp->w_height);
2041
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002042 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002043 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002044 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002045 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002046 }
2047}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002048/*
2049 * popup_locate({row}, {col})
2050 */
2051 void
2052f_popup_locate(typval_T *argvars, typval_T *rettv)
2053{
2054 int row = tv_get_number(&argvars[0]) - 1;
2055 int col = tv_get_number(&argvars[1]) - 1;
2056 win_T *wp;
2057
2058 wp = mouse_find_win(&row, &col, FIND_POPUP);
2059 if (WIN_IS_POPUP(wp))
2060 rettv->vval.v_number = wp->w_id;
2061}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002062
2063/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002064 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2065 */
2066 static void
2067get_padding_border(dict_T *dict, int *array, char *name)
2068{
2069 list_T *list;
2070 int i;
2071
2072 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2073 return;
2074
2075 list = list_alloc();
2076 if (list != NULL)
2077 {
2078 dict_add_list(dict, name, list);
2079 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2080 for (i = 0; i < 4; ++i)
2081 list_append_number(list, array[i]);
2082 }
2083}
2084
2085/*
2086 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2087 */
2088 static void
2089get_borderhighlight(dict_T *dict, win_T *wp)
2090{
2091 list_T *list;
2092 int i;
2093
2094 for (i = 0; i < 4; ++i)
2095 if (wp->w_border_highlight[i] != NULL)
2096 break;
2097 if (i == 4)
2098 return;
2099
2100 list = list_alloc();
2101 if (list != NULL)
2102 {
2103 dict_add_list(dict, "borderhighlight", list);
2104 for (i = 0; i < 4; ++i)
2105 list_append_string(list, wp->w_border_highlight[i], -1);
2106 }
2107}
2108
2109/*
2110 * For popup_getoptions(): add a "borderchars" entry to "dict".
2111 */
2112 static void
2113get_borderchars(dict_T *dict, win_T *wp)
2114{
2115 list_T *list;
2116 int i;
2117 char_u buf[NUMBUFLEN];
2118 int len;
2119
2120 for (i = 0; i < 8; ++i)
2121 if (wp->w_border_char[i] != 0)
2122 break;
2123 if (i == 8)
2124 return;
2125
2126 list = list_alloc();
2127 if (list != NULL)
2128 {
2129 dict_add_list(dict, "borderchars", list);
2130 for (i = 0; i < 8; ++i)
2131 {
2132 len = mb_char2bytes(wp->w_border_char[i], buf);
2133 list_append_string(list, buf, len);
2134 }
2135 }
2136}
2137
2138/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002139 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002140 */
2141 static void
2142get_moved_list(dict_T *dict, win_T *wp)
2143{
2144 list_T *list;
2145
2146 list = list_alloc();
2147 if (list != NULL)
2148 {
2149 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002150 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002151 list_append_number(list, wp->w_popup_mincol);
2152 list_append_number(list, wp->w_popup_maxcol);
2153 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002154 list = list_alloc();
2155 if (list != NULL)
2156 {
2157 dict_add_list(dict, "mousemoved", list);
2158 list_append_number(list, wp->w_popup_mouse_row);
2159 list_append_number(list, wp->w_popup_mouse_mincol);
2160 list_append_number(list, wp->w_popup_mouse_maxcol);
2161 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002162}
2163
2164/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002165 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002166 */
2167 void
2168f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2169{
2170 dict_T *dict;
2171 int id = (int)tv_get_number(argvars);
2172 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002173 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002174 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002175
2176 if (rettv_dict_alloc(rettv) == OK)
2177 {
2178 if (wp == NULL)
2179 return;
2180
2181 dict = rettv->vval.v_dict;
2182 dict_add_number(dict, "line", wp->w_wantline);
2183 dict_add_number(dict, "col", wp->w_wantcol);
2184 dict_add_number(dict, "minwidth", wp->w_minwidth);
2185 dict_add_number(dict, "minheight", wp->w_minheight);
2186 dict_add_number(dict, "maxheight", wp->w_maxheight);
2187 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002188 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002189 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002190 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002191 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002192 dict_add_string(dict, "title", wp->w_popup_title);
2193 dict_add_number(dict, "wrap", wp->w_p_wrap);
2194 dict_add_number(dict, "drag", wp->w_popup_drag);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002195 dict_add_number(dict, "cursorline",
2196 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002197 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002198 if (wp->w_scrollbar_highlight != NULL)
2199 dict_add_string(dict, "scrollbarhighlight",
2200 wp->w_scrollbar_highlight);
2201 if (wp->w_thumb_highlight != NULL)
2202 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002203
Bram Moolenaara3fce622019-06-20 02:31:49 +02002204 // find the tabpage that holds this popup
2205 i = 1;
2206 FOR_ALL_TABPAGES(tp)
2207 {
2208 win_T *p;
2209
2210 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2211 if (p->w_id == id)
2212 break;
2213 if (p != NULL)
2214 break;
2215 ++i;
2216 }
2217 if (tp == NULL)
2218 i = -1; // must be global
2219 else if (tp == curtab)
2220 i = 0;
2221 dict_add_number(dict, "tabpage", i);
2222
Bram Moolenaarae943152019-06-16 22:54:14 +02002223 get_padding_border(dict, wp->w_popup_padding, "padding");
2224 get_padding_border(dict, wp->w_popup_border, "border");
2225 get_borderhighlight(dict, wp);
2226 get_borderchars(dict, wp);
2227 get_moved_list(dict, wp);
2228
2229 if (wp->w_filter_cb.cb_name != NULL)
2230 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2231 if (wp->w_close_cb.cb_name != NULL)
2232 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002233
2234 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2235 ++i)
2236 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2237 {
2238 dict_add_string(dict, "pos",
2239 (char_u *)poppos_entries[i].pp_name);
2240 break;
2241 }
2242
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002243 dict_add_string(dict, "close", (char_u *)(
2244 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2245 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2246
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002247# if defined(FEAT_TIMERS)
2248 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2249 ? (long)wp->w_popup_timer->tr_interval : 0L);
2250# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002251 }
2252}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002253
2254 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002255error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002256{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002257 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002258 {
2259 emsg(_("E994: Not allowed in a popup window"));
2260 return TRUE;
2261 }
2262 return FALSE;
2263}
2264
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002265/*
2266 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002267 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002268 */
2269 void
2270popup_reset_handled()
2271{
2272 win_T *wp;
2273
2274 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2275 wp->w_popup_flags &= ~POPF_HANDLED;
2276 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2277 wp->w_popup_flags &= ~POPF_HANDLED;
2278}
2279
2280/*
2281 * Find the next visible popup where POPF_HANDLED is not set.
2282 * Must have called popup_reset_handled() first.
2283 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2284 * popup with the highest zindex.
2285 */
2286 win_T *
2287find_next_popup(int lowest)
2288{
2289 win_T *wp;
2290 win_T *found_wp;
2291 int found_zindex;
2292
2293 found_zindex = lowest ? INT_MAX : 0;
2294 found_wp = NULL;
2295 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2296 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2297 && (lowest ? wp->w_zindex < found_zindex
2298 : wp->w_zindex > found_zindex))
2299 {
2300 found_zindex = wp->w_zindex;
2301 found_wp = wp;
2302 }
2303 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2304 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2305 && (lowest ? wp->w_zindex < found_zindex
2306 : wp->w_zindex > found_zindex))
2307 {
2308 found_zindex = wp->w_zindex;
2309 found_wp = wp;
2310 }
2311
2312 if (found_wp != NULL)
2313 found_wp->w_popup_flags |= POPF_HANDLED;
2314 return found_wp;
2315}
2316
2317/*
2318 * Invoke the filter callback for window "wp" with typed character "c".
2319 * Uses the global "mod_mask" for modifiers.
2320 * Returns the return value of the filter.
2321 * Careful: The filter may make "wp" invalid!
2322 */
2323 static int
2324invoke_popup_filter(win_T *wp, int c)
2325{
2326 int res;
2327 typval_T rettv;
2328 int dummy;
2329 typval_T argv[3];
2330 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002331 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002332
Bram Moolenaara42d9452019-06-15 21:46:30 +02002333 // Emergency exit: CTRL-C closes the popup.
2334 if (c == Ctrl_C)
2335 {
2336 rettv.v_type = VAR_NUMBER;
2337 rettv.vval.v_number = -1;
2338 popup_close_and_callback(wp, &rettv);
2339 return 1;
2340 }
2341
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002342 argv[0].v_type = VAR_NUMBER;
2343 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2344
2345 // Convert the number to a string, so that the function can use:
2346 // if a:c == "\<F2>"
2347 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2348 argv[1].v_type = VAR_STRING;
2349 argv[1].vval.v_string = vim_strsave(buf);
2350
2351 argv[2].v_type = VAR_UNKNOWN;
2352
Bram Moolenaara42d9452019-06-15 21:46:30 +02002353 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002354 call_callback(&wp->w_filter_cb, -1,
2355 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002356 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002357 popup_highlight_curline(wp);
2358
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002359 res = tv_get_number(&rettv);
2360 vim_free(argv[1].vval.v_string);
2361 clear_tv(&rettv);
2362 return res;
2363}
2364
2365/*
2366 * Called when "c" was typed: invoke popup filter callbacks.
2367 * Returns TRUE when the character was consumed,
2368 */
2369 int
2370popup_do_filter(int c)
2371{
2372 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002373 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002374
2375 popup_reset_handled();
2376
2377 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2378 if (wp->w_filter_cb.cb_name != NULL)
2379 res = invoke_popup_filter(wp, c);
2380
2381 return res;
2382}
2383
Bram Moolenaar3397f742019-06-02 18:40:06 +02002384/*
2385 * Called when the cursor moved: check if any popup needs to be closed if the
2386 * cursor moved far enough.
2387 */
2388 void
2389popup_check_cursor_pos()
2390{
2391 win_T *wp;
2392 typval_T tv;
2393
2394 popup_reset_handled();
2395 while ((wp = find_next_popup(TRUE)) != NULL)
2396 if (wp->w_popup_curwin != NULL
2397 && (curwin != wp->w_popup_curwin
2398 || curwin->w_cursor.lnum != wp->w_popup_lnum
2399 || curwin->w_cursor.col < wp->w_popup_mincol
2400 || curwin->w_cursor.col > wp->w_popup_maxcol))
2401 {
2402 tv.v_type = VAR_NUMBER;
2403 tv.vval.v_number = -1;
2404 popup_close_and_callback(wp, &tv);
2405 }
2406}
2407
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002408/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002409 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2410 * "col" and "line" are screen coordinates.
2411 */
2412 static int
2413popup_masked(win_T *wp, int screencol, int screenline)
2414{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002415 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002416 int line = screenline - wp->w_winrow + 1;
2417 listitem_T *lio, *li;
2418 int width, height;
2419
2420 if (wp->w_popup_mask == NULL)
2421 return FALSE;
2422 width = popup_width(wp);
2423 height = popup_height(wp);
2424
2425 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2426 {
2427 int cols, cole;
2428 int lines, linee;
2429
2430 li = lio->li_tv.vval.v_list->lv_first;
2431 cols = tv_get_number(&li->li_tv);
2432 if (cols < 0)
2433 cols = width + cols + 1;
2434 if (col < cols)
2435 continue;
2436 li = li->li_next;
2437 cole = tv_get_number(&li->li_tv);
2438 if (cole < 0)
2439 cole = width + cole + 1;
2440 if (col > cole)
2441 continue;
2442 li = li->li_next;
2443 lines = tv_get_number(&li->li_tv);
2444 if (lines < 0)
2445 lines = height + lines + 1;
2446 if (line < lines)
2447 continue;
2448 li = li->li_next;
2449 linee = tv_get_number(&li->li_tv);
2450 if (linee < 0)
2451 linee = height + linee + 1;
2452 if (line > linee)
2453 continue;
2454 return TRUE;
2455 }
2456 return FALSE;
2457}
2458
2459/*
2460 * Set flags in popup_transparent[] for window "wp" to "val".
2461 */
2462 static void
2463update_popup_transparent(win_T *wp, int val)
2464{
2465 if (wp->w_popup_mask != NULL)
2466 {
2467 int width = popup_width(wp);
2468 int height = popup_height(wp);
2469 listitem_T *lio, *li;
2470 int cols, cole;
2471 int lines, linee;
2472 int col, line;
2473
2474 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2475 {
2476 li = lio->li_tv.vval.v_list->lv_first;
2477 cols = tv_get_number(&li->li_tv);
2478 if (cols < 0)
2479 cols = width + cols + 1;
2480 li = li->li_next;
2481 cole = tv_get_number(&li->li_tv);
2482 if (cole < 0)
2483 cole = width + cole + 1;
2484 li = li->li_next;
2485 lines = tv_get_number(&li->li_tv);
2486 if (lines < 0)
2487 lines = height + lines + 1;
2488 li = li->li_next;
2489 linee = tv_get_number(&li->li_tv);
2490 if (linee < 0)
2491 linee = height + linee + 1;
2492
2493 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002494 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002495 if (cols < 0)
2496 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002497 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002498 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002499 if (lines < 0)
2500 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002501 for (line = lines; line < linee
2502 && line + wp->w_winrow < screen_Rows; ++line)
2503 for (col = cols; col < cole
2504 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002505 popup_transparent[(line + wp->w_winrow) * screen_Columns
2506 + col + wp->w_wincol] = val;
2507 }
2508 }
2509}
2510
2511/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002512 * Update "popup_mask" if needed.
2513 * Also recomputes the popup size and positions.
2514 * Also updates "popup_visible".
2515 * Also marks window lines for redrawing.
2516 */
2517 void
2518may_update_popup_mask(int type)
2519{
2520 win_T *wp;
2521 short *mask;
2522 int line, col;
2523 int redraw_all = FALSE;
2524
2525 // Need to recompute when switching tabs.
2526 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2527 // (such as the screen size) must have changed.
2528 if (popup_mask_tab != curtab || type >= NOT_VALID)
2529 {
2530 popup_mask_refresh = TRUE;
2531 redraw_all = TRUE;
2532 }
2533 if (!popup_mask_refresh)
2534 {
2535 // Check if any buffer has changed.
2536 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2537 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2538 popup_mask_refresh = TRUE;
2539 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2540 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2541 popup_mask_refresh = TRUE;
2542 if (!popup_mask_refresh)
2543 return;
2544 }
2545
2546 // Need to update the mask, something has changed.
2547 popup_mask_refresh = FALSE;
2548 popup_mask_tab = curtab;
2549 popup_visible = FALSE;
2550
2551 // If redrawing everything, just update "popup_mask".
2552 // If redrawing only what is needed, update "popup_mask_next" and then
2553 // compare with "popup_mask" to see what changed.
2554 if (type >= SOME_VALID)
2555 mask = popup_mask;
2556 else
2557 mask = popup_mask_next;
2558 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2559
2560 // Find the window with the lowest zindex that hasn't been handled yet,
2561 // so that the window with a higher zindex overwrites the value in
2562 // popup_mask.
2563 popup_reset_handled();
2564 while ((wp = find_next_popup(TRUE)) != NULL)
2565 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002566 int height;
2567 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002568
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002569 popup_visible = TRUE;
2570
2571 // Recompute the position if the text changed.
2572 if (redraw_all
2573 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2574 popup_adjust_position(wp);
2575
Bram Moolenaard529ba52019-07-02 23:13:53 +02002576 height = popup_height(wp);
2577 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002578 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002579 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002580 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002581 col < wp->w_wincol + width && col < screen_Columns; ++col)
2582 if (!popup_masked(wp, col, line))
2583 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002584 }
2585
2586 // Only check which lines are to be updated if not already
2587 // updating all lines.
2588 if (mask == popup_mask_next)
2589 for (line = 0; line < screen_Rows; ++line)
2590 {
2591 int col_done = 0;
2592
2593 for (col = 0; col < screen_Columns; ++col)
2594 {
2595 int off = line * screen_Columns + col;
2596
2597 if (popup_mask[off] != popup_mask_next[off])
2598 {
2599 popup_mask[off] = popup_mask_next[off];
2600
2601 if (line >= cmdline_row)
2602 {
2603 // the command line needs to be cleared if text below
2604 // the popup is now visible.
2605 if (!msg_scrolled && popup_mask_next[off] == 0)
2606 clear_cmdline = TRUE;
2607 }
2608 else if (col >= col_done)
2609 {
2610 linenr_T lnum;
2611 int line_cp = line;
2612 int col_cp = col;
2613
2614 // The screen position "line" / "col" needs to be
2615 // redrawn. Figure out what window that is and update
2616 // w_redraw_top and w_redr_bot. Only needs to be done
2617 // once for each window line.
2618 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2619 if (wp != NULL)
2620 {
2621 if (line_cp >= wp->w_height)
2622 // In (or below) status line
2623 wp->w_redr_status = TRUE;
2624 // compute the position in the buffer line from the
2625 // position on the screen
2626 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2627 &lnum))
2628 // past bottom
2629 wp->w_redr_status = TRUE;
2630 else
2631 redrawWinline(wp, lnum);
2632
2633 // This line is going to be redrawn, no need to
2634 // check until the right side of the window.
2635 col_done = wp->w_wincol + wp->w_width - 1;
2636 }
2637 }
2638 }
2639 }
2640 }
2641}
2642
2643/*
2644 * Return a string of "len" spaces in IObuff.
2645 */
2646 static char_u *
2647get_spaces(int len)
2648{
2649 vim_memset(IObuff, ' ', (size_t)len);
2650 IObuff[len] = NUL;
2651 return IObuff;
2652}
2653
2654/*
2655 * Update popup windows. They are drawn on top of normal windows.
2656 * "win_update" is called for each popup window, lowest zindex first.
2657 */
2658 void
2659update_popups(void (*win_update)(win_T *wp))
2660{
2661 win_T *wp;
2662 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002663 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002664 int total_width;
2665 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002666 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002667 int popup_attr;
2668 int border_attr[4];
2669 int border_char[8];
2670 char_u buf[MB_MAXBYTES];
2671 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002672 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002673 int padcol = 0;
2674 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002675 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002676 int sb_thumb_top = 0;
2677 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002678 int attr_scroll = 0;
2679 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002680
2681 // Find the window with the lowest zindex that hasn't been updated yet,
2682 // so that the window with a higher zindex is drawn later, thus goes on
2683 // top.
2684 popup_reset_handled();
2685 while ((wp = find_next_popup(TRUE)) != NULL)
2686 {
2687 // This drawing uses the zindex of the popup window, so that it's on
2688 // top of the text but doesn't draw when another popup with higher
2689 // zindex is on top of the character.
2690 screen_zindex = wp->w_zindex;
2691
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002692 // Set flags in popup_transparent[] for masked cells.
2693 update_popup_transparent(wp, 1);
2694
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002695 // adjust w_winrow and w_wincol for border and padding, since
2696 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002697 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002698 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2699 - wp->w_popup_leftoff;
2700 if (wp->w_wincol + left_extra < 0)
2701 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002702 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002703 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002704
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002705 // Draw the popup text, unless it's off screen.
2706 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2707 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002708
2709 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002710 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002711
Bram Moolenaard529ba52019-07-02 23:13:53 +02002712 total_width = popup_width(wp);
2713 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002714 popup_attr = get_wcr_attr(wp);
2715
2716 // We can only use these line drawing characters when 'encoding' is
2717 // "utf-8" and 'ambiwidth' is "single".
2718 if (enc_utf8 && *p_ambw == 's')
2719 {
2720 border_char[0] = border_char[2] = 0x2550;
2721 border_char[1] = border_char[3] = 0x2551;
2722 border_char[4] = 0x2554;
2723 border_char[5] = 0x2557;
2724 border_char[6] = 0x255d;
2725 border_char[7] = 0x255a;
2726 }
2727 else
2728 {
2729 border_char[0] = border_char[2] = '-';
2730 border_char[1] = border_char[3] = '|';
2731 for (i = 4; i < 8; ++i)
2732 border_char[i] = '+';
2733 }
2734 for (i = 0; i < 8; ++i)
2735 if (wp->w_border_char[i] != 0)
2736 border_char[i] = wp->w_border_char[i];
2737
2738 for (i = 0; i < 4; ++i)
2739 {
2740 border_attr[i] = popup_attr;
2741 if (wp->w_border_highlight[i] != NULL)
2742 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2743 }
2744
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002745 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002746 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002747 if (wp->w_popup_border[0] > 0)
2748 {
2749 // top border
2750 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002751 wincol < 0 ? 0 : wincol, wincol + total_width,
2752 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002753 ? border_char[4] : border_char[0],
2754 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002755 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002756 {
2757 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2758 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002759 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002760 }
2761 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002762 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2763 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002764
Bram Moolenaard529ba52019-07-02 23:13:53 +02002765 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2766 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002767 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002768 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2769 - wp->w_has_scrollbar;
2770 if (padcol < 0)
2771 {
2772 padwidth += padcol;
2773 padcol = 0;
2774 }
2775 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002776 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002777 {
2778 // top padding
2779 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002780 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002781 ' ', ' ', popup_attr);
2782 }
2783
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002784 // Title goes on top of border or padding.
2785 if (wp->w_popup_title != NULL)
2786 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2787 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2788
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002789 // Compute scrollbar thumb position and size.
2790 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002791 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002792 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2793
Bram Moolenaar68acb412019-06-26 03:40:36 +02002794 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2795 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002796 if (sb_thumb_height == 0)
2797 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002798 if (linecount <= wp->w_height)
2799 // it just fits, avoid divide by zero
2800 sb_thumb_top = 0;
2801 else
2802 sb_thumb_top = (wp->w_topline - 1
2803 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002804 * (wp->w_height - sb_thumb_height)
2805 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002806 if (wp->w_scrollbar_highlight != NULL)
2807 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2808 else
2809 attr_scroll = highlight_attr[HLF_PSB];
2810 if (wp->w_thumb_highlight != NULL)
2811 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2812 else
2813 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002814 }
2815
2816 for (i = wp->w_popup_border[0];
2817 i < total_height - wp->w_popup_border[2]; ++i)
2818 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002819 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002820 // left and right padding only needed next to the body
2821 int do_padding =
2822 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2823 && i < total_height - wp->w_popup_border[2]
2824 - wp->w_popup_padding[2];
2825
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002826 row = wp->w_winrow + i;
2827
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002828 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002829 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002830 {
2831 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002832 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002833 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002834 if (do_padding && wp->w_popup_padding[3] > 0)
2835 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002836 int col = wincol + wp->w_popup_border[3];
2837
Bram Moolenaard529ba52019-07-02 23:13:53 +02002838 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002839 pad_left = wp->w_popup_padding[3];
2840 if (col < 0)
2841 {
2842 pad_left += col;
2843 col = 0;
2844 }
2845 if (pad_left > 0)
2846 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2847 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002848 // scrollbar
2849 if (wp->w_has_scrollbar)
2850 {
2851 int line = i - top_off;
2852 int scroll_col = wp->w_wincol + total_width - 1
2853 - wp->w_popup_border[1];
2854
2855 if (line >= 0 && line < wp->w_height)
2856 screen_putchar(' ', row, scroll_col,
2857 line >= sb_thumb_top
2858 && line < sb_thumb_top + sb_thumb_height
2859 ? attr_thumb : attr_scroll);
2860 else
2861 screen_putchar(' ', row, scroll_col, popup_attr);
2862 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002863 // right border
2864 if (wp->w_popup_border[1] > 0)
2865 {
2866 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002867 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002868 }
2869 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002870 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002871 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002872 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002873 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2874 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002875 }
2876
2877 if (wp->w_popup_padding[2] > 0)
2878 {
2879 // bottom padding
2880 row = wp->w_winrow + wp->w_popup_border[0]
2881 + wp->w_popup_padding[0] + wp->w_height;
2882 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002883 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002884 }
2885
2886 if (wp->w_popup_border[2] > 0)
2887 {
2888 // bottom border
2889 row = wp->w_winrow + total_height - 1;
2890 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002891 wincol < 0 ? 0 : wincol,
2892 wincol + total_width,
2893 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002894 ? border_char[7] : border_char[2],
2895 border_char[2], border_attr[2]);
2896 if (wp->w_popup_border[1] > 0)
2897 {
2898 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002899 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002900 }
2901 }
2902
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002903 if (wp->w_popup_close == POPCLOSE_BUTTON)
2904 {
2905 // close button goes on top of anything at the top-right corner
2906 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002907 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002908 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2909 }
2910
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002911 update_popup_transparent(wp, 0);
2912
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002913 // Back to the normal zindex.
2914 screen_zindex = 0;
2915 }
2916}
2917
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002918/*
2919 * Mark references in callbacks of one popup window.
2920 */
2921 static int
2922set_ref_in_one_popup(win_T *wp, int copyID)
2923{
2924 int abort = FALSE;
2925 typval_T tv;
2926
2927 if (wp->w_close_cb.cb_partial != NULL)
2928 {
2929 tv.v_type = VAR_PARTIAL;
2930 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2931 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2932 }
2933 if (wp->w_filter_cb.cb_partial != NULL)
2934 {
2935 tv.v_type = VAR_PARTIAL;
2936 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2937 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2938 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002939 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002940 return abort;
2941}
2942
2943/*
2944 * Set reference in callbacks of popup windows.
2945 */
2946 int
2947set_ref_in_popups(int copyID)
2948{
2949 int abort = FALSE;
2950 win_T *wp;
2951 tabpage_T *tp;
2952
2953 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2954 abort = abort || set_ref_in_one_popup(wp, copyID);
2955
2956 FOR_ALL_TABPAGES(tp)
2957 {
2958 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2959 abort = abort || set_ref_in_one_popup(wp, copyID);
2960 if (abort)
2961 break;
2962 }
2963 return abort;
2964}
Bram Moolenaar79648732019-07-18 21:43:07 +02002965
2966/*
2967 * Find an existing popup used as the preview window, in the current tab page.
2968 * Return NULL if not found.
2969 */
2970 win_T *
2971popup_find_preview_window(void)
2972{
2973 win_T *wp;
2974
2975 // Preview window popup is always local to tab page.
2976 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2977 if (wp->w_p_pvw)
2978 return wp;
2979 return wp;
2980}
2981
2982 int
2983popup_is_popup(win_T *wp)
2984{
2985 return wp->w_popup_flags != 0;
2986}
2987
2988/*
2989 * Create a popup to be used as the preview window.
2990 * NOTE: this makes the popup the current window, so that the file can be
2991 * edited. However, it must not remain to be the current window, the caller
2992 * must make sure of that.
2993 */
2994 int
2995popup_create_preview_window(void)
2996{
2997 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
2998
2999 if (wp == NULL)
3000 return FAIL;
3001 wp->w_p_pvw = TRUE;
3002
3003 // Set the width to a reasonable value, so that w_topline can be computed.
3004 if (wp->w_minwidth > 0)
3005 wp->w_width = wp->w_minwidth;
3006 else if (wp->w_maxwidth > 0)
3007 wp->w_width = wp->w_maxwidth;
3008 else
3009 wp->w_width = curwin->w_width;
3010
3011 // Will switch to another buffer soon, dummy one can be wiped.
3012 wp->w_buffer->b_locked = FALSE;
3013
3014 win_enter(wp, FALSE);
3015 return OK;
3016}
3017
3018 void
3019popup_close_preview()
3020{
3021 win_T *wp = popup_find_preview_window();
3022
3023 if (wp != NULL)
3024 {
3025 typval_T res;
3026
3027 res.v_type = VAR_NUMBER;
3028 res.vval.v_number = -1;
3029 popup_close_and_callback(wp, &res);
3030 }
3031}
3032
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003033#endif // FEAT_TEXT_PROP