blob: 9a39693d0a8b98d76e9a163fc140e340bf982860 [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;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200238static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200239
240/*
241 * Mouse down on border of popup window: start dragging it.
242 * Uses mouse_col and mouse_row.
243 */
244 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200245popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200246{
247 drag_start_row = mouse_row;
248 drag_start_col = mouse_col;
249 // TODO: handle using different corner
250 if (wp->w_wantline == 0)
251 drag_start_wantline = wp->w_winrow + 1;
252 else
253 drag_start_wantline = wp->w_wantline;
254 if (wp->w_wantcol == 0)
255 drag_start_wantcol = wp->w_wincol + 1;
256 else
257 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200258
259 // Stop centering the popup
260 if (wp->w_popup_pos == POPPOS_CENTER)
261 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200262
263 drag_on_resize_handle = wp->w_popup_border[1] > 0
264 && wp->w_popup_border[2] > 0
265 && row == popup_height(wp) - 1
266 && col == popup_width(wp) - 1;
267
268 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
269 {
270 if (wp->w_popup_pos == POPPOS_TOPRIGHT
271 || wp->w_popup_pos == POPPOS_BOTRIGHT)
272 wp->w_wantcol = wp->w_wincol + 1;
273 if (wp->w_popup_pos == POPPOS_BOTLEFT)
274 wp->w_wantline = wp->w_winrow + 1;
275 wp->w_popup_pos = POPPOS_TOPLEFT;
276 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200277}
278
279/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200280 * Mouse moved while dragging a popup window: adjust the window popup position
281 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200282 */
283 void
284popup_drag(win_T *wp)
285{
286 // The popup may be closed before dragging stops.
287 if (!win_valid_popup(wp))
288 return;
289
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200290 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
291 {
292 int width_inc = mouse_col - drag_start_col;
293 int height_inc = mouse_row - drag_start_row;
294
295 if (width_inc != 0)
296 {
297 int width = wp->w_width + width_inc;
298
299 if (width < 1)
300 width = 1;
301 wp->w_minwidth = width;
302 wp->w_maxwidth = width;
303 drag_start_col = mouse_col;
304 }
305
306 if (height_inc != 0)
307 {
308 int height = wp->w_height + height_inc;
309
310 if (height < 1)
311 height = 1;
312 wp->w_minheight = height;
313 wp->w_maxheight = height;
314 drag_start_row = mouse_row;
315 }
316
317 popup_adjust_position(wp);
318 return;
319 }
320
321 if (!(wp->w_popup_flags & POPF_DRAG))
322 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200323 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
324 if (wp->w_wantline < 1)
325 wp->w_wantline = 1;
326 if (wp->w_wantline > Rows)
327 wp->w_wantline = Rows;
328 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
329 if (wp->w_wantcol < 1)
330 wp->w_wantcol = 1;
331 if (wp->w_wantcol > Columns)
332 wp->w_wantcol = Columns;
333
334 popup_adjust_position(wp);
335}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200336
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200337/*
338 * Set w_firstline to match the current "wp->w_topline".
339 */
340 void
341popup_set_firstline(win_T *wp)
342{
343 int height = wp->w_height;
344
345 wp->w_firstline = wp->w_topline;
346 popup_adjust_position(wp);
347
348 // we don't want the popup to get smaller, decrement the first line
349 // until it doesn't
350 while (wp->w_firstline > 1 && wp->w_height < height)
351 {
352 --wp->w_firstline;
353 popup_adjust_position(wp);
354 }
355}
356
357/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 * Return TRUE if the position is in the popup window scrollbar.
359 */
360 int
361popup_is_in_scrollbar(win_T *wp, int row, int col)
362{
363 return wp->w_has_scrollbar
364 && row >= wp->w_popup_border[0]
365 && row < popup_height(wp) - wp->w_popup_border[2]
366 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
367}
368
369
370/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200371 * Handle a click in a popup window, if it is in the scrollbar.
372 */
373 void
374popup_handle_scrollbar_click(win_T *wp, int row, int col)
375{
376 int height = popup_height(wp);
377 int old_topline = wp->w_topline;
378
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200379 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200380 {
381 if (row >= height / 2)
382 {
383 // Click in lower half, scroll down.
384 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
385 ++wp->w_topline;
386 }
387 else if (wp->w_topline > 1)
388 // click on upper half, scroll up.
389 --wp->w_topline;
390 if (wp->w_topline != old_topline)
391 {
392 popup_set_firstline(wp);
393 redraw_win_later(wp, NOT_VALID);
394 }
395 }
396}
397
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200398#if defined(FEAT_TIMERS)
399 static void
400popup_add_timeout(win_T *wp, int time)
401{
402 char_u cbbuf[50];
403 char_u *ptr = cbbuf;
404 typval_T tv;
405
406 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
407 "{_ -> popup_close(%d)}", wp->w_id);
408 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
409 {
410 wp->w_popup_timer = create_timer(time, 0);
411 wp->w_popup_timer->tr_callback = get_callback(&tv);
412 clear_tv(&tv);
413 }
414}
415#endif
416
Bram Moolenaar17627312019-06-02 19:53:44 +0200417/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200418 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200419 */
420 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200421apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200422{
Bram Moolenaarae943152019-06-16 22:54:14 +0200423 int nr;
424
425 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
426 wp->w_minwidth = nr;
427 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
428 wp->w_minheight = nr;
429 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
430 wp->w_maxwidth = nr;
431 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
432 wp->w_maxheight = nr;
433 get_pos_options(wp, d);
434}
435
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200436 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200437handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
438{
439 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
440 {
441 char_u *s = di->di_tv.vval.v_string;
442 int flags = 0;
443
444 if (STRCMP(s, "word") == 0)
445 flags = FIND_IDENT | FIND_STRING;
446 else if (STRCMP(s, "WORD") == 0)
447 flags = FIND_STRING;
448 else if (STRCMP(s, "expr") == 0)
449 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
450 else if (STRCMP(s, "any") != 0)
451 semsg(_(e_invarg2), s);
452 if (flags != 0)
453 {
454 if (mousemoved)
455 set_mousemoved_columns(wp, flags);
456 else
457 set_moved_columns(wp, flags);
458 }
459 }
460 else if (di->di_tv.v_type == VAR_LIST
461 && di->di_tv.vval.v_list != NULL
462 && di->di_tv.vval.v_list->lv_len == 2)
463 {
464 list_T *l = di->di_tv.vval.v_list;
465 int mincol = tv_get_number(&l->lv_first->li_tv);
466 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
467
468 if (mousemoved)
469 {
470 wp->w_popup_mouse_mincol = mincol;
471 wp->w_popup_mouse_maxcol = maxcol;
472 }
473 else
474 {
475 wp->w_popup_mincol = mincol;
476 wp->w_popup_maxcol = maxcol;
477 }
478 }
479 else
480 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
481}
482
483 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200484check_highlight(dict_T *dict, char *name, char_u **pval)
485{
486 dictitem_T *di;
487 char_u *str;
488
489 di = dict_find(dict, (char_u *)name, -1);
490 if (di != NULL)
491 {
492 if (di->di_tv.v_type != VAR_STRING)
493 semsg(_(e_invargval), name);
494 else
495 {
496 str = tv_get_string(&di->di_tv);
497 if (*str != NUL)
498 *pval = vim_strsave(str);
499 }
500 }
501}
502
Bram Moolenaarae943152019-06-16 22:54:14 +0200503/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200504 * Scroll to show the line with the cursor. This assumes lines don't wrap.
505 */
506 static void
507popup_show_curline(win_T *wp)
508{
509 if (wp->w_cursor.lnum < wp->w_topline)
510 wp->w_topline = wp->w_cursor.lnum;
511 else if (wp->w_cursor.lnum >= wp->w_botline)
512 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200513
514 // Don't use "firstline" now.
515 wp->w_firstline = 0;
516}
517
518/*
519 * Get the sign group name for window "wp".
520 * Returns a pointer to a static buffer, overwritten on the next call.
521 */
522 static char_u *
523popup_get_sign_name(win_T *wp)
524{
525 static char buf[30];
526
527 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
528 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200529}
530
531/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200532 * Highlight the line with the cursor.
533 * Also scrolls the text to put the cursor line in view.
534 */
535 static void
536popup_highlight_curline(win_T *wp)
537{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200538 int sign_id = 0;
539 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200540
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200541 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200542
543 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
544 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200545 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200546
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200547 if (!sign_exists_by_name(sign_name))
548 {
549 char *linehl = "PopupSelected";
550
551 if (syn_name2id((char_u *)linehl) == 0)
552 linehl = "PmenuSel";
553 sign_define_by_name(sign_name, NULL,
554 (char_u *)linehl, NULL, NULL);
555 }
556
557 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
558 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
559 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200560 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200561 else
562 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200563}
564
565/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200566 * Shared between popup_create() and f_popup_setoptions().
567 */
568 static void
569apply_general_options(win_T *wp, dict_T *dict)
570{
571 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200572 int nr;
573 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200574
Bram Moolenaarae943152019-06-16 22:54:14 +0200575 // TODO: flip
576
577 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200578 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200579 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
580 if (wp->w_firstline < 1)
581 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200582
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200583 di = dict_find(dict, (char_u *)"scrollbar", -1);
584 if (di != NULL)
585 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
586
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200587 str = dict_get_string(dict, (char_u *)"title", FALSE);
588 if (str != NULL)
589 {
590 vim_free(wp->w_popup_title);
591 wp->w_popup_title = vim_strsave(str);
592 }
593
Bram Moolenaar402502d2019-05-30 22:07:36 +0200594 di = dict_find(dict, (char_u *)"wrap", -1);
595 if (di != NULL)
596 {
597 nr = dict_get_number(dict, (char_u *)"wrap");
598 wp->w_p_wrap = nr != 0;
599 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200600
Bram Moolenaara42d9452019-06-15 21:46:30 +0200601 di = dict_find(dict, (char_u *)"drag", -1);
602 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200603 {
604 nr = dict_get_number(dict, (char_u *)"drag");
605 if (nr)
606 wp->w_popup_flags |= POPF_DRAG;
607 else
608 wp->w_popup_flags &= ~POPF_DRAG;
609 }
610
611 di = dict_find(dict, (char_u *)"resize", -1);
612 if (di != NULL)
613 {
614 nr = dict_get_number(dict, (char_u *)"resize");
615 if (nr)
616 wp->w_popup_flags |= POPF_RESIZE;
617 else
618 wp->w_popup_flags &= ~POPF_RESIZE;
619 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200620
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200621 di = dict_find(dict, (char_u *)"close", -1);
622 if (di != NULL)
623 {
624 int ok = TRUE;
625
626 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
627 {
628 char_u *s = di->di_tv.vval.v_string;
629
630 if (STRCMP(s, "none") == 0)
631 wp->w_popup_close = POPCLOSE_NONE;
632 else if (STRCMP(s, "button") == 0)
633 wp->w_popup_close = POPCLOSE_BUTTON;
634 else if (STRCMP(s, "click") == 0)
635 wp->w_popup_close = POPCLOSE_CLICK;
636 else
637 ok = FALSE;
638 }
639 else
640 ok = FALSE;
641 if (!ok)
642 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
643 }
644
Bram Moolenaarae943152019-06-16 22:54:14 +0200645 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
646 if (str != NULL)
647 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
648 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200649
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200650 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
651 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200652 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
653 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200654
Bram Moolenaar790498b2019-06-01 22:15:29 +0200655 di = dict_find(dict, (char_u *)"borderhighlight", -1);
656 if (di != NULL)
657 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200658 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200659 emsg(_(e_listreq));
660 else
661 {
662 list_T *list = di->di_tv.vval.v_list;
663 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200664 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200665
Bram Moolenaar403d0902019-07-17 21:37:32 +0200666 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
667 ++i, li = li->li_next)
668 {
669 str = tv_get_string(&li->li_tv);
670 if (*str != NUL)
671 wp->w_border_highlight[i] = vim_strsave(str);
672 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200673 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
674 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200675 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200676 vim_strsave(wp->w_border_highlight[0]);
677 }
678 }
679
Bram Moolenaar790498b2019-06-01 22:15:29 +0200680 di = dict_find(dict, (char_u *)"borderchars", -1);
681 if (di != NULL)
682 {
683 if (di->di_tv.v_type != VAR_LIST)
684 emsg(_(e_listreq));
685 else
686 {
687 list_T *list = di->di_tv.vval.v_list;
688 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200689 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200690
691 if (list != NULL)
692 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
693 ++i, li = li->li_next)
694 {
695 str = tv_get_string(&li->li_tv);
696 if (*str != NUL)
697 wp->w_border_char[i] = mb_ptr2char(str);
698 }
699 if (list->lv_len == 1)
700 for (i = 1; i < 8; ++i)
701 wp->w_border_char[i] = wp->w_border_char[0];
702 if (list->lv_len == 2)
703 {
704 for (i = 4; i < 8; ++i)
705 wp->w_border_char[i] = wp->w_border_char[1];
706 for (i = 1; i < 4; ++i)
707 wp->w_border_char[i] = wp->w_border_char[0];
708 }
709 }
710 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200711
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200712 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
713 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
714
Bram Moolenaarae943152019-06-16 22:54:14 +0200715 di = dict_find(dict, (char_u *)"zindex", -1);
716 if (di != NULL)
717 {
718 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
719 if (wp->w_zindex < 1)
720 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
721 if (wp->w_zindex > 32000)
722 wp->w_zindex = 32000;
723 }
724
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200725 di = dict_find(dict, (char_u *)"mask", -1);
726 if (di != NULL)
727 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200728 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200729
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200730 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200731 {
732 listitem_T *li;
733
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200734 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200735 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
736 li = li->li_next)
737 {
738 if (li->li_tv.v_type != VAR_LIST
739 || li->li_tv.vval.v_list == NULL
740 || li->li_tv.vval.v_list->lv_len != 4)
741 {
742 ok = FALSE;
743 break;
744 }
745 }
746 }
747 if (ok)
748 {
749 wp->w_popup_mask = di->di_tv.vval.v_list;
750 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200751 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200752 }
753 else
754 semsg(_(e_invargval), "mask");
755 }
756
Bram Moolenaarae943152019-06-16 22:54:14 +0200757#if defined(FEAT_TIMERS)
758 // Add timer to close the popup after some time.
759 nr = dict_get_number(dict, (char_u *)"time");
760 if (nr > 0)
761 popup_add_timeout(wp, nr);
762#endif
763
Bram Moolenaar3397f742019-06-02 18:40:06 +0200764 di = dict_find(dict, (char_u *)"moved", -1);
765 if (di != NULL)
766 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200767 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200768 handle_moved_argument(wp, di, FALSE);
769 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200770
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200771 di = dict_find(dict, (char_u *)"mousemoved", -1);
772 if (di != NULL)
773 {
774 set_mousemoved_values(wp);
775 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200776 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200777
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200778 di = dict_find(dict, (char_u *)"cursorline", -1);
779 if (di != NULL)
780 {
781 if (di->di_tv.v_type == VAR_NUMBER)
782 {
783 if (di->di_tv.vval.v_number != 0)
784 wp->w_popup_flags |= POPF_CURSORLINE;
785 else
786 wp->w_popup_flags &= ~POPF_CURSORLINE;
787 }
788 else
789 semsg(_(e_invargval), "cursorline");
790 }
791
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 di = dict_find(dict, (char_u *)"filter", -1);
793 if (di != NULL)
794 {
795 callback_T callback = get_callback(&di->di_tv);
796
797 if (callback.cb_name != NULL)
798 {
799 free_callback(&wp->w_filter_cb);
800 set_callback(&wp->w_filter_cb, &callback);
801 }
802 }
803
804 di = dict_find(dict, (char_u *)"callback", -1);
805 if (di != NULL)
806 {
807 callback_T callback = get_callback(&di->di_tv);
808
809 if (callback.cb_name != NULL)
810 {
811 free_callback(&wp->w_close_cb);
812 set_callback(&wp->w_close_cb, &callback);
813 }
814 }
815}
816
817/*
818 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200819 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200820 */
821 static void
822apply_options(win_T *wp, dict_T *dict)
823{
824 int nr;
825
826 apply_move_options(wp, dict);
827 apply_general_options(wp, dict);
828
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200829 nr = dict_get_number(dict, (char_u *)"hidden");
830 if (nr > 0)
831 {
832 wp->w_popup_flags |= POPF_HIDDEN;
833 --wp->w_buffer->b_nwindows;
834 }
835
Bram Moolenaar33796b32019-06-08 16:01:13 +0200836 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200837 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200838}
839
840/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200841 * Add lines to the popup from a list of strings.
842 */
843 static void
844add_popup_strings(buf_T *buf, list_T *l)
845{
846 listitem_T *li;
847 linenr_T lnum = 0;
848 char_u *p;
849
850 for (li = l->lv_first; li != NULL; li = li->li_next)
851 if (li->li_tv.v_type == VAR_STRING)
852 {
853 p = li->li_tv.vval.v_string;
854 ml_append_buf(buf, lnum++,
855 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
856 }
857}
858
859/*
860 * Add lines to the popup from a list of dictionaries.
861 */
862 static void
863add_popup_dicts(buf_T *buf, list_T *l)
864{
865 listitem_T *li;
866 listitem_T *pli;
867 linenr_T lnum = 0;
868 char_u *p;
869 dict_T *dict;
870
871 // first add the text lines
872 for (li = l->lv_first; li != NULL; li = li->li_next)
873 {
874 if (li->li_tv.v_type != VAR_DICT)
875 {
876 emsg(_(e_dictreq));
877 return;
878 }
879 dict = li->li_tv.vval.v_dict;
880 p = dict == NULL ? NULL
881 : dict_get_string(dict, (char_u *)"text", FALSE);
882 ml_append_buf(buf, lnum++,
883 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
884 }
885
886 // add the text properties
887 lnum = 1;
888 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
889 {
890 dictitem_T *di;
891 list_T *plist;
892
893 dict = li->li_tv.vval.v_dict;
894 di = dict_find(dict, (char_u *)"props", -1);
895 if (di != NULL)
896 {
897 if (di->di_tv.v_type != VAR_LIST)
898 {
899 emsg(_(e_listreq));
900 return;
901 }
902 plist = di->di_tv.vval.v_list;
903 if (plist != NULL)
904 {
905 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
906 {
907 if (pli->li_tv.v_type != VAR_DICT)
908 {
909 emsg(_(e_dictreq));
910 return;
911 }
912 dict = pli->li_tv.vval.v_dict;
913 if (dict != NULL)
914 {
915 int col = dict_get_number(dict, (char_u *)"col");
916
917 prop_add_common( lnum, col, dict, buf, NULL);
918 }
919 }
920 }
921 }
922 }
923}
924
925/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200926 * Get the padding plus border at the top, adjusted to 1 if there is a title.
927 */
928 static int
929popup_top_extra(win_T *wp)
930{
931 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
932
933 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
934 return 1;
935 return extra;
936}
937
938/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200939 * Return the height of popup window "wp", including border and padding.
940 */
941 int
942popup_height(win_T *wp)
943{
944 return wp->w_height
945 + popup_top_extra(wp)
946 + wp->w_popup_padding[2] + wp->w_popup_border[2];
947}
948
949/*
950 * Return the width of popup window "wp", including border, padding and
951 * scrollbar.
952 */
953 int
954popup_width(win_T *wp)
955{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200956 // w_leftcol is how many columns of the core are left of the screen
957 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200958 return wp->w_width + wp->w_leftcol
959 + wp->w_popup_padding[3] + wp->w_popup_border[3]
960 + wp->w_popup_padding[1] + wp->w_popup_border[1]
961 + wp->w_has_scrollbar
962 + wp->w_popup_rightoff;
963}
964
965/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200966 * Adjust the position and size of the popup to fit on the screen.
967 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200968 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200969popup_adjust_position(win_T *wp)
970{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200971 linenr_T lnum;
972 int wrapped = 0;
973 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200974 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200975 int center_vert = FALSE;
976 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200977 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200978 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200979 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
980 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
981 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
982 int extra_height = top_extra + bot_extra;
983 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200984 int org_winrow = wp->w_winrow;
985 int org_wincol = wp->w_wincol;
986 int org_width = wp->w_width;
987 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200988 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200989 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200990 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200991
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200992 wp->w_winrow = 0;
993 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200994 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200995 wp->w_popup_leftoff = 0;
996 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200997 if (wp->w_popup_pos == POPPOS_CENTER)
998 {
999 // center after computing the size
1000 center_vert = TRUE;
1001 center_hor = TRUE;
1002 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001003 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001004 {
1005 if (wp->w_wantline == 0)
1006 center_vert = TRUE;
1007 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1008 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1009 {
1010 wp->w_winrow = wp->w_wantline - 1;
1011 if (wp->w_winrow >= Rows)
1012 wp->w_winrow = Rows - 1;
1013 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001014
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001015 if (wp->w_wantcol == 0)
1016 center_hor = TRUE;
1017 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1018 || wp->w_popup_pos == POPPOS_BOTLEFT)
1019 {
1020 wp->w_wincol = wp->w_wantcol - 1;
1021 if (wp->w_wincol >= Columns - 3)
1022 wp->w_wincol = Columns - 3;
1023 }
1024 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001025
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001026 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001027 // When left aligned use the space available, but shift to the left when we
1028 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001029 maxspace = Columns - wp->w_wincol - left_extra;
1030 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001031 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001032 {
1033 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001034 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001035 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001036
Bram Moolenaar8d241042019-06-12 23:40:01 +02001037 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001038 if (wp->w_firstline != 0)
1039 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001040 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1041 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1042
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001043 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001044 // Use a minimum width of one, so that something shows when there is no
1045 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001046 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001047 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001048 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001049 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001050 int len;
1051 int w_width = wp->w_width;
1052
1053 // Count Tabs for what they are worth and compute the length based on
1054 // the maximum width (matters when 'showbreak' is set).
1055 if (wp->w_width < maxwidth)
1056 wp->w_width = maxwidth;
1057 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001058 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001059 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001060
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001061 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001062 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001063 while (len > maxwidth)
1064 {
1065 ++wrapped;
1066 len -= maxwidth;
1067 wp->w_width = maxwidth;
1068 }
1069 }
1070 else if (len > maxwidth
1071 && allow_adjust_left
1072 && (wp->w_popup_pos == POPPOS_TOPLEFT
1073 || wp->w_popup_pos == POPPOS_BOTLEFT))
1074 {
1075 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001076 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001077
Bram Moolenaar51c31312019-06-15 22:27:23 +02001078 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001079 {
1080 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001081
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001082 len -= truncate_shift;
1083 shift_by -= truncate_shift;
1084 }
1085
1086 wp->w_wincol -= shift_by;
1087 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001088 wp->w_width = maxwidth;
1089 }
1090 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001091 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001092 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001093 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1094 wp->w_width = wp->w_maxwidth;
1095 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001096 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001097 if (wp->w_maxheight > 0
1098 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001099 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001100 }
1101
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001102 wp->w_has_scrollbar = wp->w_want_scrollbar
1103 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001104 if (wp->w_has_scrollbar)
1105 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001106
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001107 minwidth = wp->w_minwidth;
1108 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1109 {
1110 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1111
1112 if (minwidth < title_len)
1113 minwidth = title_len;
1114 }
1115
1116 if (minwidth > 0 && wp->w_width < minwidth)
1117 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001118 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001119 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001120 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001121 // some columns cut off on the right
1122 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001123 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001124 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001125 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001126 {
1127 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1128 if (wp->w_wincol < 0)
1129 wp->w_wincol = 0;
1130 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001131 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1132 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1133 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001134 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1135
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001136 // Right aligned: move to the right if needed.
1137 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001138 if (leftoff >= 0)
1139 wp->w_wincol = leftoff;
1140 else if (wp->w_popup_fixed)
1141 {
1142 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001143 // columns when displaying the window, minus left border and
1144 // padding.
1145 if (-leftoff > left_extra)
1146 wp->w_leftcol = -leftoff - left_extra;
1147 wp->w_width -= wp->w_leftcol;
1148 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001149 if (wp->w_width < 0)
1150 wp->w_width = 0;
1151 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001152 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001153
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001154 if (wp->w_p_wrap || (!wp->w_popup_fixed
1155 && (wp->w_popup_pos == POPPOS_TOPLEFT
1156 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001157 {
1158 int want_col = 0;
1159
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001160 // try to show the right border and any scrollbar
1161 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001162 if (want_col > 0 && wp->w_wincol > 0
1163 && wp->w_wincol + want_col >= Columns)
1164 {
1165 wp->w_wincol = Columns - want_col;
1166 if (wp->w_wincol < 0)
1167 wp->w_wincol = 0;
1168 }
1169 }
1170
Bram Moolenaar8d241042019-06-12 23:40:01 +02001171 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1172 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001173 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1174 wp->w_height = wp->w_minheight;
1175 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1176 wp->w_height = wp->w_maxheight;
1177 if (wp->w_height > Rows - wp->w_winrow)
1178 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001179
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001180 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001181 {
1182 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1183 if (wp->w_winrow < 0)
1184 wp->w_winrow = 0;
1185 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001186 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1187 || wp->w_popup_pos == POPPOS_BOTLEFT)
1188 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001189 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001190 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001191 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001192 else
1193 // not enough space, make top aligned
1194 wp->w_winrow = wp->w_wantline + 1;
1195 }
1196
Bram Moolenaar17146962019-05-30 00:12:11 +02001197 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001198
1199 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001200 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001201 if (org_winrow != wp->w_winrow
1202 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001203 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001204 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001205 || org_width != wp->w_width
1206 || org_height != wp->w_height)
1207 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001208 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001209 redraw_win_later(wp, NOT_VALID);
1210 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1211 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001212 popup_mask_refresh = TRUE;
1213 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001214}
1215
Bram Moolenaar17627312019-06-02 19:53:44 +02001216typedef enum
1217{
1218 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001219 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001220 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001221 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001222 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001223 TYPE_MENU,
1224 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001225} create_type_T;
1226
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001227/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001228 * Make "buf" empty and set the contents to "text".
1229 * Used by popup_create() and popup_settext().
1230 */
1231 static void
1232popup_set_buffer_text(buf_T *buf, typval_T text)
1233{
1234 int lnum;
1235
1236 // Clear the buffer, then replace the lines.
1237 curbuf = buf;
1238 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1239 ml_delete(lnum, FALSE);
1240 curbuf = curwin->w_buffer;
1241
1242 // Add text to the buffer.
1243 if (text.v_type == VAR_STRING)
1244 {
1245 // just a string
1246 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1247 }
1248 else
1249 {
1250 list_T *l = text.vval.v_list;
1251
1252 if (l->lv_len > 0)
1253 {
1254 if (l->lv_first->li_tv.v_type == VAR_STRING)
1255 // list of strings
1256 add_popup_strings(buf, l);
1257 else
1258 // list of dictionaries
1259 add_popup_dicts(buf, l);
1260 }
1261 }
1262
1263 // delete the line that was in the empty buffer
1264 curbuf = buf;
1265 ml_delete(buf->b_ml.ml_line_count, FALSE);
1266 curbuf = curwin->w_buffer;
1267}
1268
1269/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001270 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1271 * not NULL.
1272 * Return FAIL if the parsing fails.
1273 */
1274 int
1275parse_previewpopup(win_T *wp)
1276{
1277 char_u *p;
1278
1279 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1280 {
1281 char_u *e, *dig;
1282 char_u *s = p;
1283 int x;
1284
1285 e = vim_strchr(p, ':');
1286 if (e == NULL || e[1] == NUL)
1287 return FAIL;
1288
1289 p = vim_strchr(e, ',');
1290 if (p == NULL)
1291 p = e + STRLEN(e);
1292 dig = e + 1;
1293 x = getdigits(&dig);
1294 if (dig != p)
1295 return FAIL;
1296
1297 if (STRNCMP(s, "height:", 7) == 0)
1298 {
1299 if (wp != NULL)
1300 {
1301 wp->w_minheight = x;
1302 wp->w_maxheight = x;
1303 }
1304 }
1305 else if (STRNCMP(s, "width:", 6) == 0)
1306 {
1307 if (wp != NULL)
1308 {
1309 wp->w_minwidth = x;
1310 wp->w_maxwidth = x;
1311 }
1312 }
1313 else
1314 return FAIL;
1315 }
1316 return OK;
1317}
1318
1319/*
1320 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001321 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001322 */
1323 void
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001324popup_set_wantpos(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001325{
1326 setcursor_mayforce(TRUE);
1327 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1328 if (wp->w_wantline == 0) // cursor in first line
1329 {
1330 wp->w_wantline = 2;
1331 wp->w_popup_pos = POPPOS_TOPLEFT;
1332 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001333
Bram Moolenaar79648732019-07-18 21:43:07 +02001334 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001335 if (wp->w_wantcol > Columns - width)
1336 {
1337 wp->w_wantcol = Columns - width;
1338 if (wp->w_wantcol < 1)
1339 wp->w_wantcol = 1;
1340 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001341 popup_adjust_position(wp);
1342}
1343
1344/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001345 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001346 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001347 * etc.
1348 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001349 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001350 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001351popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001352{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001353 win_T *wp;
1354 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001355 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001356 int new_buffer;
1357 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001358 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001359 int nr;
1360 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001361
Bram Moolenaar79648732019-07-18 21:43:07 +02001362 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001363 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001364 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001365 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001366 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001367 buf = buflist_findnr( argvars[0].vval.v_number);
1368 if (buf == NULL)
1369 {
1370 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1371 return NULL;
1372 }
1373 }
1374 else if (!(argvars[0].v_type == VAR_STRING
1375 && argvars[0].vval.v_string != NULL)
1376 && !(argvars[0].v_type == VAR_LIST
1377 && argvars[0].vval.v_list != NULL))
1378 {
1379 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001380 return NULL;
1381 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001382 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001383 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001384 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001385 return NULL;
1386 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001387 d = argvars[1].vval.v_dict;
1388 }
1389
1390 if (d != NULL)
1391 {
1392 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1393 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1394 else if (type == TYPE_NOTIFICATION)
1395 tabnr = -1; // notifications are global by default
1396 else
1397 tabnr = 0;
1398 if (tabnr > 0)
1399 {
1400 tp = find_tabpage(tabnr);
1401 if (tp == NULL)
1402 {
1403 semsg(_("E997: Tabpage not found: %d"), tabnr);
1404 return NULL;
1405 }
1406 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001407 }
1408
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001409 // Create the window and buffer.
1410 wp = win_alloc_popup_win();
1411 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001412 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001413 if (rettv != NULL)
1414 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001415 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001416 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001417
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001418 if (buf != NULL)
1419 {
1420 // use existing buffer
1421 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001422 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001423 buffer_ensure_loaded(buf);
1424 }
1425 else
1426 {
1427 // create a new buffer associated with the popup
1428 new_buffer = TRUE;
1429 buf = buflist_new(NULL, NULL, (linenr_T)0,
1430 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1431 if (buf == NULL)
1432 return NULL;
1433 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001434
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001435 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001436
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001437 set_local_options_default(wp);
1438 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001439 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001440 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001441 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001442 buf->b_p_ul = -1; // no undo
1443 buf->b_p_swf = FALSE; // no swap file
1444 buf->b_p_bl = FALSE; // unlisted buffer
1445 buf->b_locked = TRUE;
1446 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001447
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001448 // Avoid that 'buftype' is reset when this buffer is entered.
1449 buf->b_p_initialized = TRUE;
1450 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001451
Bram Moolenaara3fce622019-06-20 02:31:49 +02001452 if (tp != NULL)
1453 {
1454 // popup on specified tab page
1455 wp->w_next = tp->tp_first_popupwin;
1456 tp->tp_first_popupwin = wp;
1457 }
1458 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001459 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001460 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001461 wp->w_next = curtab->tp_first_popupwin;
1462 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001463 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001464 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001465 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001466 win_T *prev = first_popupwin;
1467
1468 // Global popup: add at the end, so that it gets displayed on top of
1469 // older ones with the same zindex. Matters for notifications.
1470 if (first_popupwin == NULL)
1471 first_popupwin = wp;
1472 else
1473 {
1474 while (prev->w_next != NULL)
1475 prev = prev->w_next;
1476 prev->w_next = wp;
1477 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001478 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001479
Bram Moolenaar79648732019-07-18 21:43:07 +02001480 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001481 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001482
Bram Moolenaar79648732019-07-18 21:43:07 +02001483 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001484 {
1485 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001486 }
1487 if (type == TYPE_ATCURSOR)
1488 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001489 popup_set_wantpos(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001490 set_moved_values(wp);
1491 set_moved_columns(wp, FIND_STRING);
1492 }
1493
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001494 if (type == TYPE_BEVAL)
1495 {
1496 wp->w_popup_pos = POPPOS_BOTLEFT;
1497
1498 // by default use the mouse position
1499 wp->w_wantline = mouse_row;
1500 if (wp->w_wantline <= 0) // mouse on first line
1501 {
1502 wp->w_wantline = 2;
1503 wp->w_popup_pos = POPPOS_TOPLEFT;
1504 }
1505 wp->w_wantcol = mouse_col + 1;
1506 set_mousemoved_values(wp);
1507 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1508 }
1509
Bram Moolenaar33796b32019-06-08 16:01:13 +02001510 // set default values
1511 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001512 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001513
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001514 if (type == TYPE_NOTIFICATION)
1515 {
1516 win_T *twp, *nextwin;
1517 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001518
1519 // Try to not overlap with another global popup. Guess we need 3
1520 // more screen lines than buffer lines.
1521 wp->w_wantline = 1;
1522 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1523 {
1524 nextwin = twp->w_next;
1525 if (twp != wp
1526 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1527 && twp->w_winrow <= wp->w_wantline - 1 + height
1528 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1529 {
1530 // move to below this popup and restart the loop to check for
1531 // overlap with other popups
1532 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1533 nextwin = first_popupwin;
1534 }
1535 }
1536 if (wp->w_wantline + height > Rows)
1537 {
1538 // can't avoid overlap, put on top in the hope that message goes
1539 // away soon.
1540 wp->w_wantline = 1;
1541 }
1542
1543 wp->w_wantcol = 10;
1544 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001545 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001546 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001547 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001548 for (i = 0; i < 4; ++i)
1549 wp->w_popup_border[i] = 1;
1550 wp->w_popup_padding[1] = 1;
1551 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001552
1553 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001554 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001555 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1556 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001557 }
1558
Bram Moolenaara730e552019-06-16 19:05:31 +02001559 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001560 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001561 wp->w_popup_pos = POPPOS_CENTER;
1562 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001563 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001564 for (i = 0; i < 4; ++i)
1565 {
1566 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001567 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001568 }
1569 }
1570
Bram Moolenaara730e552019-06-16 19:05:31 +02001571 if (type == TYPE_MENU)
1572 {
1573 typval_T tv;
1574 callback_T callback;
1575
1576 tv.v_type = VAR_STRING;
1577 tv.vval.v_string = (char_u *)"popup_filter_menu";
1578 callback = get_callback(&tv);
1579 if (callback.cb_name != NULL)
1580 set_callback(&wp->w_filter_cb, &callback);
1581
1582 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001583 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001584 }
1585
Bram Moolenaar79648732019-07-18 21:43:07 +02001586 if (type == TYPE_PREVIEW)
1587 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001588 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001589 wp->w_popup_close = POPCLOSE_BUTTON;
1590 for (i = 0; i < 4; ++i)
1591 wp->w_popup_border[i] = 1;
1592 parse_previewpopup(wp);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001593 popup_set_wantpos(wp, wp->w_minwidth);
Bram Moolenaar79648732019-07-18 21:43:07 +02001594 }
1595
Bram Moolenaarae943152019-06-16 22:54:14 +02001596 for (i = 0; i < 4; ++i)
1597 VIM_CLEAR(wp->w_border_highlight[i]);
1598 for (i = 0; i < 8; ++i)
1599 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001600 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001601 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001602
Bram Moolenaar79648732019-07-18 21:43:07 +02001603 if (d != NULL)
1604 // Deal with options.
1605 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001606
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001607#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001608 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1609 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001610#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001611
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001612 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001613
1614 wp->w_vsep_width = 0;
1615
1616 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001617 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001618
1619 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001620}
1621
1622/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001623 * popup_clear()
1624 */
1625 void
1626f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1627{
1628 close_all_popups();
1629}
1630
1631/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001632 * popup_create({text}, {options})
1633 */
1634 void
1635f_popup_create(typval_T *argvars, typval_T *rettv)
1636{
Bram Moolenaar17627312019-06-02 19:53:44 +02001637 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001638}
1639
1640/*
1641 * popup_atcursor({text}, {options})
1642 */
1643 void
1644f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1645{
Bram Moolenaar17627312019-06-02 19:53:44 +02001646 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001647}
1648
1649/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001650 * popup_beval({text}, {options})
1651 */
1652 void
1653f_popup_beval(typval_T *argvars, typval_T *rettv)
1654{
1655 popup_create(argvars, rettv, TYPE_BEVAL);
1656}
1657
1658/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001659 * Invoke the close callback for window "wp" with value "result".
1660 * Careful: The callback may make "wp" invalid!
1661 */
1662 static void
1663invoke_popup_callback(win_T *wp, typval_T *result)
1664{
1665 typval_T rettv;
1666 int dummy;
1667 typval_T argv[3];
1668
1669 argv[0].v_type = VAR_NUMBER;
1670 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1671
1672 if (result != NULL && result->v_type != VAR_UNKNOWN)
1673 copy_tv(result, &argv[1]);
1674 else
1675 {
1676 argv[1].v_type = VAR_NUMBER;
1677 argv[1].vval.v_number = 0;
1678 }
1679
1680 argv[2].v_type = VAR_UNKNOWN;
1681
1682 call_callback(&wp->w_close_cb, -1,
1683 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1684 if (result != NULL)
1685 clear_tv(&argv[1]);
1686 clear_tv(&rettv);
1687}
1688
1689/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001690 * Close popup "wp" and invoke any close callback for it.
1691 */
1692 static void
1693popup_close_and_callback(win_T *wp, typval_T *arg)
1694{
1695 int id = wp->w_id;
1696
1697 if (wp->w_close_cb.cb_name != NULL)
1698 // Careful: This may make "wp" invalid.
1699 invoke_popup_callback(wp, arg);
1700
1701 popup_close(id);
1702}
1703
1704/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001705 * Close popup "wp" because of a mouse click.
1706 */
1707 void
1708popup_close_for_mouse_click(win_T *wp)
1709{
1710 typval_T res;
1711
1712 res.v_type = VAR_NUMBER;
1713 res.vval.v_number = -2;
1714 popup_close_and_callback(wp, &res);
1715}
1716
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001717 static void
1718check_mouse_moved(win_T *wp, win_T *mouse_wp)
1719{
1720 // Close the popup when all if these are true:
1721 // - the mouse is not on this popup
1722 // - "mousemoved" was used
1723 // - the mouse is no longer on the same screen row or the mouse column is
1724 // outside of the relevant text
1725 if (wp != mouse_wp
1726 && wp->w_popup_mouse_row != 0
1727 && (wp->w_popup_mouse_row != mouse_row
1728 || mouse_col < wp->w_popup_mouse_mincol
1729 || mouse_col > wp->w_popup_mouse_maxcol))
1730 {
1731 typval_T res;
1732
1733 res.v_type = VAR_NUMBER;
1734 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001735 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001736 popup_close_and_callback(wp, &res);
1737 }
1738}
1739
1740/*
1741 * Called when the mouse moved: may close a popup with "mousemoved".
1742 */
1743 void
1744popup_handle_mouse_moved(void)
1745{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001746 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001747 win_T *mouse_wp;
1748 int row = mouse_row;
1749 int col = mouse_col;
1750
1751 // find the window where the mouse is in
1752 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1753
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001754 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1755 {
1756 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001757 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001758 }
1759 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1760 {
1761 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001762 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001763 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001764}
1765
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001766/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001767 * In a filter: check if the typed key is a mouse event that is used for
1768 * dragging the popup.
1769 */
1770 static void
1771filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1772{
1773 int row = mouse_row;
1774 int col = mouse_col;
1775
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001776 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001777 && is_mouse_key(c)
1778 && (wp == popup_dragwin
1779 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1780 // do not consume the key, allow for dragging the popup
1781 rettv->vval.v_number = 0;
1782}
1783
Bram Moolenaara730e552019-06-16 19:05:31 +02001784/*
1785 * popup_filter_menu({text}, {options})
1786 */
1787 void
1788f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1789{
1790 int id = tv_get_number(&argvars[0]);
1791 win_T *wp = win_id2wp(id);
1792 char_u *key = tv_get_string(&argvars[1]);
1793 typval_T res;
1794 int c;
1795 linenr_T old_lnum;
1796
1797 // If the popup has been closed do not consume the key.
1798 if (wp == NULL)
1799 return;
1800
1801 c = *key;
1802 if (c == K_SPECIAL && key[1] != NUL)
1803 c = TO_SPECIAL(key[1], key[2]);
1804
1805 // consume all keys until done
1806 rettv->vval.v_number = 1;
1807 res.v_type = VAR_NUMBER;
1808
1809 old_lnum = wp->w_cursor.lnum;
1810 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1811 --wp->w_cursor.lnum;
1812 if ((c == 'j' || c == 'J' || c == K_DOWN)
1813 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1814 ++wp->w_cursor.lnum;
1815 if (old_lnum != wp->w_cursor.lnum)
1816 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001817 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001818 return;
1819 }
1820
1821 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1822 {
1823 // Cancelled, invoke callback with -1
1824 res.vval.v_number = -1;
1825 popup_close_and_callback(wp, &res);
1826 return;
1827 }
1828 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1829 {
1830 // Invoke callback with current index.
1831 res.vval.v_number = wp->w_cursor.lnum;
1832 popup_close_and_callback(wp, &res);
1833 return;
1834 }
1835
1836 filter_handle_drag(wp, c, rettv);
1837}
1838
1839/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001840 * popup_filter_yesno({text}, {options})
1841 */
1842 void
1843f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1844{
1845 int id = tv_get_number(&argvars[0]);
1846 win_T *wp = win_id2wp(id);
1847 char_u *key = tv_get_string(&argvars[1]);
1848 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001849 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001850
1851 // If the popup has been closed don't consume the key.
1852 if (wp == NULL)
1853 return;
1854
Bram Moolenaara730e552019-06-16 19:05:31 +02001855 c = *key;
1856 if (c == K_SPECIAL && key[1] != NUL)
1857 c = TO_SPECIAL(key[1], key[2]);
1858
Bram Moolenaara42d9452019-06-15 21:46:30 +02001859 // consume all keys until done
1860 rettv->vval.v_number = 1;
1861
Bram Moolenaara730e552019-06-16 19:05:31 +02001862 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001863 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001864 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001865 res.vval.v_number = 0;
1866 else
1867 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001868 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001869 return;
1870 }
1871
1872 // Invoke callback
1873 res.v_type = VAR_NUMBER;
1874 popup_close_and_callback(wp, &res);
1875}
1876
1877/*
1878 * popup_dialog({text}, {options})
1879 */
1880 void
1881f_popup_dialog(typval_T *argvars, typval_T *rettv)
1882{
1883 popup_create(argvars, rettv, TYPE_DIALOG);
1884}
1885
1886/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001887 * popup_menu({text}, {options})
1888 */
1889 void
1890f_popup_menu(typval_T *argvars, typval_T *rettv)
1891{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001892 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001893}
1894
1895/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001896 * popup_notification({text}, {options})
1897 */
1898 void
1899f_popup_notification(typval_T *argvars, typval_T *rettv)
1900{
1901 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1902}
1903
1904/*
1905 * Find the popup window with window-ID "id".
1906 * If the popup window does not exist NULL is returned.
1907 * If the window is not a popup window, and error message is given.
1908 */
1909 static win_T *
1910find_popup_win(int id)
1911{
1912 win_T *wp = win_id2wp(id);
1913
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001914 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001915 {
1916 semsg(_("E993: window %d is not a popup window"), id);
1917 return NULL;
1918 }
1919 return wp;
1920}
1921
1922/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001923 * popup_close({id})
1924 */
1925 void
1926f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1927{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001928 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001929 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001930
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001931 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001932 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001933}
1934
1935/*
1936 * popup_hide({id})
1937 */
1938 void
1939f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1940{
1941 int id = (int)tv_get_number(argvars);
1942 win_T *wp = find_popup_win(id);
1943
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001944 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001945 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001946 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001947 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001948 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001949 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001950 }
1951}
1952
1953/*
1954 * popup_show({id})
1955 */
1956 void
1957f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1958{
1959 int id = (int)tv_get_number(argvars);
1960 win_T *wp = find_popup_win(id);
1961
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001962 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001963 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001964 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001965 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001966 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001967 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001968 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001969}
1970
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001971/*
1972 * popup_settext({id}, {text})
1973 */
1974 void
1975f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1976{
1977 int id = (int)tv_get_number(&argvars[0]);
1978 win_T *wp = find_popup_win(id);
1979
1980 if (wp != NULL)
1981 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001982 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1983 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1984 else
1985 {
1986 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001987 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001988 popup_adjust_position(wp);
1989 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001990 }
1991}
1992
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001993 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001994popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001995{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001996 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001997 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001998 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001999 clear_cmdline = TRUE;
2000 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002001
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002002 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002003 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002004}
2005
Bram Moolenaarec583842019-05-26 14:11:23 +02002006/*
2007 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002008 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002009 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002010 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002011popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002012{
2013 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002014 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002015 win_T *prev = NULL;
2016
Bram Moolenaarec583842019-05-26 14:11:23 +02002017 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002019 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002020 {
2021 if (prev == NULL)
2022 first_popupwin = wp->w_next;
2023 else
2024 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002025 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002026 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002027 }
2028
Bram Moolenaarec583842019-05-26 14:11:23 +02002029 // go through tab-local popups
2030 FOR_ALL_TABPAGES(tp)
2031 popup_close_tabpage(tp, id);
2032}
2033
2034/*
2035 * Close a popup window with Window-id "id" in tabpage "tp".
2036 */
2037 void
2038popup_close_tabpage(tabpage_T *tp, int id)
2039{
2040 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002041 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002042 win_T *prev = NULL;
2043
Bram Moolenaarec583842019-05-26 14:11:23 +02002044 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2045 if (wp->w_id == id)
2046 {
2047 if (prev == NULL)
2048 *root = wp->w_next;
2049 else
2050 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002051 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002052 return;
2053 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002054}
2055
2056 void
2057close_all_popups(void)
2058{
2059 while (first_popupwin != NULL)
2060 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002061 while (curtab->tp_first_popupwin != NULL)
2062 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002063}
2064
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002065/*
2066 * popup_move({id}, {options})
2067 */
2068 void
2069f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2070{
Bram Moolenaarae943152019-06-16 22:54:14 +02002071 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002072 int id = (int)tv_get_number(argvars);
2073 win_T *wp = find_popup_win(id);
2074
2075 if (wp == NULL)
2076 return; // invalid {id}
2077
2078 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2079 {
2080 emsg(_(e_dictreq));
2081 return;
2082 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002083 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002084
Bram Moolenaarae943152019-06-16 22:54:14 +02002085 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002086
2087 if (wp->w_winrow + wp->w_height >= cmdline_row)
2088 clear_cmdline = TRUE;
2089 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002090}
2091
Bram Moolenaarbc133542019-05-29 20:26:46 +02002092/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002093 * popup_setoptions({id}, {options})
2094 */
2095 void
2096f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2097{
2098 dict_T *dict;
2099 int id = (int)tv_get_number(argvars);
2100 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002101 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002102
2103 if (wp == NULL)
2104 return; // invalid {id}
2105
2106 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2107 {
2108 emsg(_(e_dictreq));
2109 return;
2110 }
2111 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002112 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002113
2114 apply_move_options(wp, dict);
2115 apply_general_options(wp, dict);
2116
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002117 if (old_firstline != wp->w_firstline)
2118 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002119 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002120 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002121 popup_adjust_position(wp);
2122}
2123
2124/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002125 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002126 */
2127 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002128f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002129{
2130 dict_T *dict;
2131 int id = (int)tv_get_number(argvars);
2132 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002133 int top_extra;
2134 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002135
2136 if (rettv_dict_alloc(rettv) == OK)
2137 {
2138 if (wp == NULL)
2139 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002140 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002141 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2142
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002143 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002144 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002145 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002146
Bram Moolenaarbc133542019-05-29 20:26:46 +02002147 dict_add_number(dict, "line", wp->w_winrow + 1);
2148 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002149 dict_add_number(dict, "width", wp->w_width + left_extra
2150 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2151 dict_add_number(dict, "height", wp->w_height + top_extra
2152 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002153
2154 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2155 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2156 dict_add_number(dict, "core_width", wp->w_width);
2157 dict_add_number(dict, "core_height", wp->w_height);
2158
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002159 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002160 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002161 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002162 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002163
2164 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002165 }
2166}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002167/*
2168 * popup_locate({row}, {col})
2169 */
2170 void
2171f_popup_locate(typval_T *argvars, typval_T *rettv)
2172{
2173 int row = tv_get_number(&argvars[0]) - 1;
2174 int col = tv_get_number(&argvars[1]) - 1;
2175 win_T *wp;
2176
2177 wp = mouse_find_win(&row, &col, FIND_POPUP);
2178 if (WIN_IS_POPUP(wp))
2179 rettv->vval.v_number = wp->w_id;
2180}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002181
2182/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002183 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2184 */
2185 static void
2186get_padding_border(dict_T *dict, int *array, char *name)
2187{
2188 list_T *list;
2189 int i;
2190
2191 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2192 return;
2193
2194 list = list_alloc();
2195 if (list != NULL)
2196 {
2197 dict_add_list(dict, name, list);
2198 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2199 for (i = 0; i < 4; ++i)
2200 list_append_number(list, array[i]);
2201 }
2202}
2203
2204/*
2205 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2206 */
2207 static void
2208get_borderhighlight(dict_T *dict, win_T *wp)
2209{
2210 list_T *list;
2211 int i;
2212
2213 for (i = 0; i < 4; ++i)
2214 if (wp->w_border_highlight[i] != NULL)
2215 break;
2216 if (i == 4)
2217 return;
2218
2219 list = list_alloc();
2220 if (list != NULL)
2221 {
2222 dict_add_list(dict, "borderhighlight", list);
2223 for (i = 0; i < 4; ++i)
2224 list_append_string(list, wp->w_border_highlight[i], -1);
2225 }
2226}
2227
2228/*
2229 * For popup_getoptions(): add a "borderchars" entry to "dict".
2230 */
2231 static void
2232get_borderchars(dict_T *dict, win_T *wp)
2233{
2234 list_T *list;
2235 int i;
2236 char_u buf[NUMBUFLEN];
2237 int len;
2238
2239 for (i = 0; i < 8; ++i)
2240 if (wp->w_border_char[i] != 0)
2241 break;
2242 if (i == 8)
2243 return;
2244
2245 list = list_alloc();
2246 if (list != NULL)
2247 {
2248 dict_add_list(dict, "borderchars", list);
2249 for (i = 0; i < 8; ++i)
2250 {
2251 len = mb_char2bytes(wp->w_border_char[i], buf);
2252 list_append_string(list, buf, len);
2253 }
2254 }
2255}
2256
2257/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002258 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002259 */
2260 static void
2261get_moved_list(dict_T *dict, win_T *wp)
2262{
2263 list_T *list;
2264
2265 list = list_alloc();
2266 if (list != NULL)
2267 {
2268 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002269 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002270 list_append_number(list, wp->w_popup_mincol);
2271 list_append_number(list, wp->w_popup_maxcol);
2272 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002273 list = list_alloc();
2274 if (list != NULL)
2275 {
2276 dict_add_list(dict, "mousemoved", list);
2277 list_append_number(list, wp->w_popup_mouse_row);
2278 list_append_number(list, wp->w_popup_mouse_mincol);
2279 list_append_number(list, wp->w_popup_mouse_maxcol);
2280 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002281}
2282
2283/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002284 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002285 */
2286 void
2287f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2288{
2289 dict_T *dict;
2290 int id = (int)tv_get_number(argvars);
2291 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002292 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002293 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002294
2295 if (rettv_dict_alloc(rettv) == OK)
2296 {
2297 if (wp == NULL)
2298 return;
2299
2300 dict = rettv->vval.v_dict;
2301 dict_add_number(dict, "line", wp->w_wantline);
2302 dict_add_number(dict, "col", wp->w_wantcol);
2303 dict_add_number(dict, "minwidth", wp->w_minwidth);
2304 dict_add_number(dict, "minheight", wp->w_minheight);
2305 dict_add_number(dict, "maxheight", wp->w_maxheight);
2306 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002307 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002308 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002309 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002310 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002311 dict_add_string(dict, "title", wp->w_popup_title);
2312 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002313 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
2314 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002315 dict_add_number(dict, "cursorline",
2316 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002317 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002318 if (wp->w_scrollbar_highlight != NULL)
2319 dict_add_string(dict, "scrollbarhighlight",
2320 wp->w_scrollbar_highlight);
2321 if (wp->w_thumb_highlight != NULL)
2322 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002323
Bram Moolenaara3fce622019-06-20 02:31:49 +02002324 // find the tabpage that holds this popup
2325 i = 1;
2326 FOR_ALL_TABPAGES(tp)
2327 {
2328 win_T *p;
2329
2330 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2331 if (p->w_id == id)
2332 break;
2333 if (p != NULL)
2334 break;
2335 ++i;
2336 }
2337 if (tp == NULL)
2338 i = -1; // must be global
2339 else if (tp == curtab)
2340 i = 0;
2341 dict_add_number(dict, "tabpage", i);
2342
Bram Moolenaarae943152019-06-16 22:54:14 +02002343 get_padding_border(dict, wp->w_popup_padding, "padding");
2344 get_padding_border(dict, wp->w_popup_border, "border");
2345 get_borderhighlight(dict, wp);
2346 get_borderchars(dict, wp);
2347 get_moved_list(dict, wp);
2348
2349 if (wp->w_filter_cb.cb_name != NULL)
2350 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2351 if (wp->w_close_cb.cb_name != NULL)
2352 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002353
2354 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2355 ++i)
2356 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2357 {
2358 dict_add_string(dict, "pos",
2359 (char_u *)poppos_entries[i].pp_name);
2360 break;
2361 }
2362
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002363 dict_add_string(dict, "close", (char_u *)(
2364 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2365 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2366
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002367# if defined(FEAT_TIMERS)
2368 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2369 ? (long)wp->w_popup_timer->tr_interval : 0L);
2370# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002371 }
2372}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002373
2374 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002375error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002376{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002377 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002378 {
2379 emsg(_("E994: Not allowed in a popup window"));
2380 return TRUE;
2381 }
2382 return FALSE;
2383}
2384
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002385/*
2386 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002387 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002388 */
2389 void
2390popup_reset_handled()
2391{
2392 win_T *wp;
2393
2394 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2395 wp->w_popup_flags &= ~POPF_HANDLED;
2396 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2397 wp->w_popup_flags &= ~POPF_HANDLED;
2398}
2399
2400/*
2401 * Find the next visible popup where POPF_HANDLED is not set.
2402 * Must have called popup_reset_handled() first.
2403 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2404 * popup with the highest zindex.
2405 */
2406 win_T *
2407find_next_popup(int lowest)
2408{
2409 win_T *wp;
2410 win_T *found_wp;
2411 int found_zindex;
2412
2413 found_zindex = lowest ? INT_MAX : 0;
2414 found_wp = NULL;
2415 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2416 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2417 && (lowest ? wp->w_zindex < found_zindex
2418 : wp->w_zindex > found_zindex))
2419 {
2420 found_zindex = wp->w_zindex;
2421 found_wp = wp;
2422 }
2423 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2424 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2425 && (lowest ? wp->w_zindex < found_zindex
2426 : wp->w_zindex > found_zindex))
2427 {
2428 found_zindex = wp->w_zindex;
2429 found_wp = wp;
2430 }
2431
2432 if (found_wp != NULL)
2433 found_wp->w_popup_flags |= POPF_HANDLED;
2434 return found_wp;
2435}
2436
2437/*
2438 * Invoke the filter callback for window "wp" with typed character "c".
2439 * Uses the global "mod_mask" for modifiers.
2440 * Returns the return value of the filter.
2441 * Careful: The filter may make "wp" invalid!
2442 */
2443 static int
2444invoke_popup_filter(win_T *wp, int c)
2445{
2446 int res;
2447 typval_T rettv;
2448 int dummy;
2449 typval_T argv[3];
2450 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002451 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002452
Bram Moolenaara42d9452019-06-15 21:46:30 +02002453 // Emergency exit: CTRL-C closes the popup.
2454 if (c == Ctrl_C)
2455 {
2456 rettv.v_type = VAR_NUMBER;
2457 rettv.vval.v_number = -1;
2458 popup_close_and_callback(wp, &rettv);
2459 return 1;
2460 }
2461
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002462 argv[0].v_type = VAR_NUMBER;
2463 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2464
2465 // Convert the number to a string, so that the function can use:
2466 // if a:c == "\<F2>"
2467 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2468 argv[1].v_type = VAR_STRING;
2469 argv[1].vval.v_string = vim_strsave(buf);
2470
2471 argv[2].v_type = VAR_UNKNOWN;
2472
Bram Moolenaara42d9452019-06-15 21:46:30 +02002473 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002474 call_callback(&wp->w_filter_cb, -1,
2475 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002476 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002477 popup_highlight_curline(wp);
2478
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002479 res = tv_get_number(&rettv);
2480 vim_free(argv[1].vval.v_string);
2481 clear_tv(&rettv);
2482 return res;
2483}
2484
2485/*
2486 * Called when "c" was typed: invoke popup filter callbacks.
2487 * Returns TRUE when the character was consumed,
2488 */
2489 int
2490popup_do_filter(int c)
2491{
2492 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002493 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002494
2495 popup_reset_handled();
2496
2497 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2498 if (wp->w_filter_cb.cb_name != NULL)
2499 res = invoke_popup_filter(wp, c);
2500
2501 return res;
2502}
2503
Bram Moolenaar3397f742019-06-02 18:40:06 +02002504/*
2505 * Called when the cursor moved: check if any popup needs to be closed if the
2506 * cursor moved far enough.
2507 */
2508 void
2509popup_check_cursor_pos()
2510{
2511 win_T *wp;
2512 typval_T tv;
2513
2514 popup_reset_handled();
2515 while ((wp = find_next_popup(TRUE)) != NULL)
2516 if (wp->w_popup_curwin != NULL
2517 && (curwin != wp->w_popup_curwin
2518 || curwin->w_cursor.lnum != wp->w_popup_lnum
2519 || curwin->w_cursor.col < wp->w_popup_mincol
2520 || curwin->w_cursor.col > wp->w_popup_maxcol))
2521 {
2522 tv.v_type = VAR_NUMBER;
2523 tv.vval.v_number = -1;
2524 popup_close_and_callback(wp, &tv);
2525 }
2526}
2527
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002528/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002529 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002530 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002531 static void
2532popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002533{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002534 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002535 char_u *cells;
2536 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002537
2538 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002539 return;
2540 if (wp->w_popup_mask_cells != NULL
2541 && wp->w_popup_mask_height == height
2542 && wp->w_popup_mask_width == width)
2543 return; // cache is still valid
2544
2545 vim_free(wp->w_popup_mask_cells);
2546 wp->w_popup_mask_cells = alloc_clear(width * height);
2547 if (wp->w_popup_mask_cells == NULL)
2548 return;
2549 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002550
2551 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2552 {
2553 int cols, cole;
2554 int lines, linee;
2555
2556 li = lio->li_tv.vval.v_list->lv_first;
2557 cols = tv_get_number(&li->li_tv);
2558 if (cols < 0)
2559 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002560 li = li->li_next;
2561 cole = tv_get_number(&li->li_tv);
2562 if (cole < 0)
2563 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002564 li = li->li_next;
2565 lines = tv_get_number(&li->li_tv);
2566 if (lines < 0)
2567 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002568 li = li->li_next;
2569 linee = tv_get_number(&li->li_tv);
2570 if (linee < 0)
2571 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002572
2573 for (row = lines - 1; row < linee && row < height; ++row)
2574 for (col = cols - 1; col < cole && col < width; ++col)
2575 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002576 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002577}
2578
2579/*
2580 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2581 * "col" and "line" are screen coordinates.
2582 */
2583 static int
2584popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2585{
2586 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2587 int line = screenline - wp->w_winrow;
2588
2589 return col >= 0 && col < width
2590 && line >= 0 && line < height
2591 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002592}
2593
2594/*
2595 * Set flags in popup_transparent[] for window "wp" to "val".
2596 */
2597 static void
2598update_popup_transparent(win_T *wp, int val)
2599{
2600 if (wp->w_popup_mask != NULL)
2601 {
2602 int width = popup_width(wp);
2603 int height = popup_height(wp);
2604 listitem_T *lio, *li;
2605 int cols, cole;
2606 int lines, linee;
2607 int col, line;
2608
2609 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2610 {
2611 li = lio->li_tv.vval.v_list->lv_first;
2612 cols = tv_get_number(&li->li_tv);
2613 if (cols < 0)
2614 cols = width + cols + 1;
2615 li = li->li_next;
2616 cole = tv_get_number(&li->li_tv);
2617 if (cole < 0)
2618 cole = width + cole + 1;
2619 li = li->li_next;
2620 lines = tv_get_number(&li->li_tv);
2621 if (lines < 0)
2622 lines = height + lines + 1;
2623 li = li->li_next;
2624 linee = tv_get_number(&li->li_tv);
2625 if (linee < 0)
2626 linee = height + linee + 1;
2627
2628 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002629 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002630 if (cols < 0)
2631 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002632 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002633 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002634 if (lines < 0)
2635 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002636 for (line = lines; line < linee
2637 && line + wp->w_winrow < screen_Rows; ++line)
2638 for (col = cols; col < cole
2639 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002640 popup_transparent[(line + wp->w_winrow) * screen_Columns
2641 + col + wp->w_wincol] = val;
2642 }
2643 }
2644}
2645
2646/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002647 * Update "popup_mask" if needed.
2648 * Also recomputes the popup size and positions.
2649 * Also updates "popup_visible".
2650 * Also marks window lines for redrawing.
2651 */
2652 void
2653may_update_popup_mask(int type)
2654{
2655 win_T *wp;
2656 short *mask;
2657 int line, col;
2658 int redraw_all = FALSE;
2659
2660 // Need to recompute when switching tabs.
2661 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2662 // (such as the screen size) must have changed.
2663 if (popup_mask_tab != curtab || type >= NOT_VALID)
2664 {
2665 popup_mask_refresh = TRUE;
2666 redraw_all = TRUE;
2667 }
2668 if (!popup_mask_refresh)
2669 {
2670 // Check if any buffer has changed.
2671 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2672 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2673 popup_mask_refresh = TRUE;
2674 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2675 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2676 popup_mask_refresh = TRUE;
2677 if (!popup_mask_refresh)
2678 return;
2679 }
2680
2681 // Need to update the mask, something has changed.
2682 popup_mask_refresh = FALSE;
2683 popup_mask_tab = curtab;
2684 popup_visible = FALSE;
2685
2686 // If redrawing everything, just update "popup_mask".
2687 // If redrawing only what is needed, update "popup_mask_next" and then
2688 // compare with "popup_mask" to see what changed.
2689 if (type >= SOME_VALID)
2690 mask = popup_mask;
2691 else
2692 mask = popup_mask_next;
2693 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2694
2695 // Find the window with the lowest zindex that hasn't been handled yet,
2696 // so that the window with a higher zindex overwrites the value in
2697 // popup_mask.
2698 popup_reset_handled();
2699 while ((wp = find_next_popup(TRUE)) != NULL)
2700 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002701 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002702 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002703
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002704 popup_visible = TRUE;
2705
2706 // Recompute the position if the text changed.
2707 if (redraw_all
2708 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2709 popup_adjust_position(wp);
2710
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002711 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002712 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002713 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002714 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002715 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002716 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002717 col < wp->w_wincol + width - wp->w_popup_leftoff
2718 && col < screen_Columns; ++col)
2719 if (wp->w_popup_mask_cells == NULL
2720 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002721 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002722 }
2723
2724 // Only check which lines are to be updated if not already
2725 // updating all lines.
2726 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002727 {
2728 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2729 win_T *prev_wp = NULL;
2730
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002731 for (line = 0; line < screen_Rows; ++line)
2732 {
2733 int col_done = 0;
2734
2735 for (col = 0; col < screen_Columns; ++col)
2736 {
2737 int off = line * screen_Columns + col;
2738
2739 if (popup_mask[off] != popup_mask_next[off])
2740 {
2741 popup_mask[off] = popup_mask_next[off];
2742
2743 if (line >= cmdline_row)
2744 {
2745 // the command line needs to be cleared if text below
2746 // the popup is now visible.
2747 if (!msg_scrolled && popup_mask_next[off] == 0)
2748 clear_cmdline = TRUE;
2749 }
2750 else if (col >= col_done)
2751 {
2752 linenr_T lnum;
2753 int line_cp = line;
2754 int col_cp = col;
2755
2756 // The screen position "line" / "col" needs to be
2757 // redrawn. Figure out what window that is and update
2758 // w_redraw_top and w_redr_bot. Only needs to be done
2759 // once for each window line.
2760 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2761 if (wp != NULL)
2762 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002763 if (wp != prev_wp)
2764 {
2765 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2766 prev_wp = wp;
2767 }
2768
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002769 if (line_cp >= wp->w_height)
2770 // In (or below) status line
2771 wp->w_redr_status = TRUE;
2772 // compute the position in the buffer line from the
2773 // position on the screen
2774 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002775 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002776 // past bottom
2777 wp->w_redr_status = TRUE;
2778 else
2779 redrawWinline(wp, lnum);
2780
2781 // This line is going to be redrawn, no need to
2782 // check until the right side of the window.
2783 col_done = wp->w_wincol + wp->w_width - 1;
2784 }
2785 }
2786 }
2787 }
2788 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002789
2790 vim_free(plines_cache);
2791 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002792}
2793
2794/*
2795 * Return a string of "len" spaces in IObuff.
2796 */
2797 static char_u *
2798get_spaces(int len)
2799{
2800 vim_memset(IObuff, ' ', (size_t)len);
2801 IObuff[len] = NUL;
2802 return IObuff;
2803}
2804
2805/*
2806 * Update popup windows. They are drawn on top of normal windows.
2807 * "win_update" is called for each popup window, lowest zindex first.
2808 */
2809 void
2810update_popups(void (*win_update)(win_T *wp))
2811{
2812 win_T *wp;
2813 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002814 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002815 int total_width;
2816 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002817 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002818 int popup_attr;
2819 int border_attr[4];
2820 int border_char[8];
2821 char_u buf[MB_MAXBYTES];
2822 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002823 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002824 int padcol = 0;
2825 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002826 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002827 int sb_thumb_top = 0;
2828 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002829 int attr_scroll = 0;
2830 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002831
2832 // Find the window with the lowest zindex that hasn't been updated yet,
2833 // so that the window with a higher zindex is drawn later, thus goes on
2834 // top.
2835 popup_reset_handled();
2836 while ((wp = find_next_popup(TRUE)) != NULL)
2837 {
2838 // This drawing uses the zindex of the popup window, so that it's on
2839 // top of the text but doesn't draw when another popup with higher
2840 // zindex is on top of the character.
2841 screen_zindex = wp->w_zindex;
2842
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002843 // Set flags in popup_transparent[] for masked cells.
2844 update_popup_transparent(wp, 1);
2845
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002846 // adjust w_winrow and w_wincol for border and padding, since
2847 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002848 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002849 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2850 - wp->w_popup_leftoff;
2851 if (wp->w_wincol + left_extra < 0)
2852 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002853 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002854 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002855
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002856 // Draw the popup text, unless it's off screen.
2857 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2858 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002859
2860 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002861 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002862
Bram Moolenaard529ba52019-07-02 23:13:53 +02002863 total_width = popup_width(wp);
2864 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002865 popup_attr = get_wcr_attr(wp);
2866
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002867 if (wp->w_winrow + total_height > cmdline_row)
2868 wp->w_popup_flags |= POPF_ON_CMDLINE;
2869 else
2870 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2871
2872
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002873 // We can only use these line drawing characters when 'encoding' is
2874 // "utf-8" and 'ambiwidth' is "single".
2875 if (enc_utf8 && *p_ambw == 's')
2876 {
2877 border_char[0] = border_char[2] = 0x2550;
2878 border_char[1] = border_char[3] = 0x2551;
2879 border_char[4] = 0x2554;
2880 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002881 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
2882 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002883 border_char[7] = 0x255a;
2884 }
2885 else
2886 {
2887 border_char[0] = border_char[2] = '-';
2888 border_char[1] = border_char[3] = '|';
2889 for (i = 4; i < 8; ++i)
2890 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002891 if (wp->w_popup_flags & POPF_RESIZE)
2892 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002893 }
2894 for (i = 0; i < 8; ++i)
2895 if (wp->w_border_char[i] != 0)
2896 border_char[i] = wp->w_border_char[i];
2897
2898 for (i = 0; i < 4; ++i)
2899 {
2900 border_attr[i] = popup_attr;
2901 if (wp->w_border_highlight[i] != NULL)
2902 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2903 }
2904
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002905 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002906 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002907 if (wp->w_popup_border[0] > 0)
2908 {
2909 // top border
2910 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002911 wincol < 0 ? 0 : wincol, wincol + total_width,
2912 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002913 ? border_char[4] : border_char[0],
2914 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002915 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002916 {
2917 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2918 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002919 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002920 }
2921 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002922 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2923 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002924
Bram Moolenaard529ba52019-07-02 23:13:53 +02002925 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2926 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002927 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002928 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2929 - wp->w_has_scrollbar;
2930 if (padcol < 0)
2931 {
2932 padwidth += padcol;
2933 padcol = 0;
2934 }
2935 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002936 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002937 {
2938 // top padding
2939 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002940 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002941 ' ', ' ', popup_attr);
2942 }
2943
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002944 // Title goes on top of border or padding.
2945 if (wp->w_popup_title != NULL)
2946 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2947 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2948
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002949 // Compute scrollbar thumb position and size.
2950 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002951 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002952 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2953
Bram Moolenaar68acb412019-06-26 03:40:36 +02002954 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2955 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002956 if (sb_thumb_height == 0)
2957 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002958 if (linecount <= wp->w_height)
2959 // it just fits, avoid divide by zero
2960 sb_thumb_top = 0;
2961 else
2962 sb_thumb_top = (wp->w_topline - 1
2963 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002964 * (wp->w_height - sb_thumb_height)
2965 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002966 if (wp->w_scrollbar_highlight != NULL)
2967 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2968 else
2969 attr_scroll = highlight_attr[HLF_PSB];
2970 if (wp->w_thumb_highlight != NULL)
2971 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2972 else
2973 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002974 }
2975
2976 for (i = wp->w_popup_border[0];
2977 i < total_height - wp->w_popup_border[2]; ++i)
2978 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002979 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002980 // left and right padding only needed next to the body
2981 int do_padding =
2982 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2983 && i < total_height - wp->w_popup_border[2]
2984 - wp->w_popup_padding[2];
2985
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002986 row = wp->w_winrow + i;
2987
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002988 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002989 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002990 {
2991 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002992 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002993 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002994 if (do_padding && wp->w_popup_padding[3] > 0)
2995 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002996 int col = wincol + wp->w_popup_border[3];
2997
Bram Moolenaard529ba52019-07-02 23:13:53 +02002998 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002999 pad_left = wp->w_popup_padding[3];
3000 if (col < 0)
3001 {
3002 pad_left += col;
3003 col = 0;
3004 }
3005 if (pad_left > 0)
3006 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3007 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003008 // scrollbar
3009 if (wp->w_has_scrollbar)
3010 {
3011 int line = i - top_off;
3012 int scroll_col = wp->w_wincol + total_width - 1
3013 - wp->w_popup_border[1];
3014
3015 if (line >= 0 && line < wp->w_height)
3016 screen_putchar(' ', row, scroll_col,
3017 line >= sb_thumb_top
3018 && line < sb_thumb_top + sb_thumb_height
3019 ? attr_thumb : attr_scroll);
3020 else
3021 screen_putchar(' ', row, scroll_col, popup_attr);
3022 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003023 // right border
3024 if (wp->w_popup_border[1] > 0)
3025 {
3026 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003027 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003028 }
3029 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003030 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003031 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003032 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003033 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3034 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003035 }
3036
3037 if (wp->w_popup_padding[2] > 0)
3038 {
3039 // bottom padding
3040 row = wp->w_winrow + wp->w_popup_border[0]
3041 + wp->w_popup_padding[0] + wp->w_height;
3042 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003043 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003044 }
3045
3046 if (wp->w_popup_border[2] > 0)
3047 {
3048 // bottom border
3049 row = wp->w_winrow + total_height - 1;
3050 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003051 wincol < 0 ? 0 : wincol,
3052 wincol + total_width,
3053 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003054 ? border_char[7] : border_char[2],
3055 border_char[2], border_attr[2]);
3056 if (wp->w_popup_border[1] > 0)
3057 {
3058 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003059 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003060 }
3061 }
3062
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003063 if (wp->w_popup_close == POPCLOSE_BUTTON)
3064 {
3065 // close button goes on top of anything at the top-right corner
3066 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003067 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003068 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3069 }
3070
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003071 update_popup_transparent(wp, 0);
3072
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003073 // Back to the normal zindex.
3074 screen_zindex = 0;
3075 }
3076}
3077
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003078/*
3079 * Mark references in callbacks of one popup window.
3080 */
3081 static int
3082set_ref_in_one_popup(win_T *wp, int copyID)
3083{
3084 int abort = FALSE;
3085 typval_T tv;
3086
3087 if (wp->w_close_cb.cb_partial != NULL)
3088 {
3089 tv.v_type = VAR_PARTIAL;
3090 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3091 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3092 }
3093 if (wp->w_filter_cb.cb_partial != NULL)
3094 {
3095 tv.v_type = VAR_PARTIAL;
3096 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3097 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3098 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003099 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003100 return abort;
3101}
3102
3103/*
3104 * Set reference in callbacks of popup windows.
3105 */
3106 int
3107set_ref_in_popups(int copyID)
3108{
3109 int abort = FALSE;
3110 win_T *wp;
3111 tabpage_T *tp;
3112
3113 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3114 abort = abort || set_ref_in_one_popup(wp, copyID);
3115
3116 FOR_ALL_TABPAGES(tp)
3117 {
3118 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3119 abort = abort || set_ref_in_one_popup(wp, copyID);
3120 if (abort)
3121 break;
3122 }
3123 return abort;
3124}
Bram Moolenaar79648732019-07-18 21:43:07 +02003125
3126/*
3127 * Find an existing popup used as the preview window, in the current tab page.
3128 * Return NULL if not found.
3129 */
3130 win_T *
3131popup_find_preview_window(void)
3132{
3133 win_T *wp;
3134
3135 // Preview window popup is always local to tab page.
3136 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3137 if (wp->w_p_pvw)
3138 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003139 return NULL;
3140}
3141
3142 void
3143f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3144{
3145 win_T *wp = popup_find_preview_window();
3146
3147 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003148}
3149
3150 int
3151popup_is_popup(win_T *wp)
3152{
3153 return wp->w_popup_flags != 0;
3154}
3155
3156/*
3157 * Create a popup to be used as the preview window.
3158 * NOTE: this makes the popup the current window, so that the file can be
3159 * edited. However, it must not remain to be the current window, the caller
3160 * must make sure of that.
3161 */
3162 int
3163popup_create_preview_window(void)
3164{
3165 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
3166
3167 if (wp == NULL)
3168 return FAIL;
3169 wp->w_p_pvw = TRUE;
3170
3171 // Set the width to a reasonable value, so that w_topline can be computed.
3172 if (wp->w_minwidth > 0)
3173 wp->w_width = wp->w_minwidth;
3174 else if (wp->w_maxwidth > 0)
3175 wp->w_width = wp->w_maxwidth;
3176 else
3177 wp->w_width = curwin->w_width;
3178
3179 // Will switch to another buffer soon, dummy one can be wiped.
3180 wp->w_buffer->b_locked = FALSE;
3181
3182 win_enter(wp, FALSE);
3183 return OK;
3184}
3185
3186 void
3187popup_close_preview()
3188{
3189 win_T *wp = popup_find_preview_window();
3190
3191 if (wp != NULL)
3192 {
3193 typval_T res;
3194
3195 res.v_type = VAR_NUMBER;
3196 res.vval.v_number = -1;
3197 popup_close_and_callback(wp, &res);
3198 }
3199}
3200
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003201#endif // FEAT_TEXT_PROP