blob: 4fe64fd237fcafaa0b6318490e4be20905e8579c [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
16#ifdef FEAT_TEXT_PROP
17
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200171 * Used when popup options contain "mousemoved": set default moved values.
172 */
173 static void
174set_mousemoved_values(win_T *wp)
175{
176 wp->w_popup_mouse_row = mouse_row;
177 wp->w_popup_mouse_mincol = mouse_col;
178 wp->w_popup_mouse_maxcol = mouse_col;
179}
180
181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
223 * Return TRUE if "row"/"col" is on the "X" button of the popup.
224 * The values are relative to the top-left corner.
225 * Caller should check w_popup_close is POPCLOSE_BUTTON.
226 */
227 int
228popup_on_X_button(win_T *wp, int row, int col)
229{
230 return row == 0 && col == popup_width(wp) - 1;
231}
232
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200233// Values set when dragging a popup window starts.
234static int drag_start_row;
235static int drag_start_col;
236static int drag_start_wantline;
237static int drag_start_wantcol;
238
239/*
240 * Mouse down on border of popup window: start dragging it.
241 * Uses mouse_col and mouse_row.
242 */
243 void
244popup_start_drag(win_T *wp)
245{
246 drag_start_row = mouse_row;
247 drag_start_col = mouse_col;
248 // TODO: handle using different corner
249 if (wp->w_wantline == 0)
250 drag_start_wantline = wp->w_winrow + 1;
251 else
252 drag_start_wantline = wp->w_wantline;
253 if (wp->w_wantcol == 0)
254 drag_start_wantcol = wp->w_wincol + 1;
255 else
256 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200257
258 // Stop centering the popup
259 if (wp->w_popup_pos == POPPOS_CENTER)
260 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200261}
262
263/*
264 * Mouse moved while dragging a popup window: adjust the window popup position.
265 */
266 void
267popup_drag(win_T *wp)
268{
269 // The popup may be closed before dragging stops.
270 if (!win_valid_popup(wp))
271 return;
272
273 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
274 if (wp->w_wantline < 1)
275 wp->w_wantline = 1;
276 if (wp->w_wantline > Rows)
277 wp->w_wantline = Rows;
278 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
279 if (wp->w_wantcol < 1)
280 wp->w_wantcol = 1;
281 if (wp->w_wantcol > Columns)
282 wp->w_wantcol = Columns;
283
284 popup_adjust_position(wp);
285}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200286
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200287/*
288 * Set w_firstline to match the current "wp->w_topline".
289 */
290 void
291popup_set_firstline(win_T *wp)
292{
293 int height = wp->w_height;
294
295 wp->w_firstline = wp->w_topline;
296 popup_adjust_position(wp);
297
298 // we don't want the popup to get smaller, decrement the first line
299 // until it doesn't
300 while (wp->w_firstline > 1 && wp->w_height < height)
301 {
302 --wp->w_firstline;
303 popup_adjust_position(wp);
304 }
305}
306
307/*
308 * Handle a click in a popup window, if it is in the scrollbar.
309 */
310 void
311popup_handle_scrollbar_click(win_T *wp, int row, int col)
312{
313 int height = popup_height(wp);
314 int old_topline = wp->w_topline;
315
316 if (wp->w_has_scrollbar == 0)
317 return;
318 if (row >= wp->w_popup_border[0]
319 && row < height - wp->w_popup_border[2]
Bram Moolenaarbd42b312019-07-12 16:35:34 +0200320 && col == popup_width(wp) - wp->w_popup_border[1] - 1)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200321 {
322 if (row >= height / 2)
323 {
324 // Click in lower half, scroll down.
325 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
326 ++wp->w_topline;
327 }
328 else if (wp->w_topline > 1)
329 // click on upper half, scroll up.
330 --wp->w_topline;
331 if (wp->w_topline != old_topline)
332 {
333 popup_set_firstline(wp);
334 redraw_win_later(wp, NOT_VALID);
335 }
336 }
337}
338
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200339#if defined(FEAT_TIMERS)
340 static void
341popup_add_timeout(win_T *wp, int time)
342{
343 char_u cbbuf[50];
344 char_u *ptr = cbbuf;
345 typval_T tv;
346
347 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
348 "{_ -> popup_close(%d)}", wp->w_id);
349 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
350 {
351 wp->w_popup_timer = create_timer(time, 0);
352 wp->w_popup_timer->tr_callback = get_callback(&tv);
353 clear_tv(&tv);
354 }
355}
356#endif
357
Bram Moolenaar17627312019-06-02 19:53:44 +0200358/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200359 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200360 */
361 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200362apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200363{
Bram Moolenaarae943152019-06-16 22:54:14 +0200364 int nr;
365
366 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
367 wp->w_minwidth = nr;
368 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
369 wp->w_minheight = nr;
370 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
371 wp->w_maxwidth = nr;
372 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
373 wp->w_maxheight = nr;
374 get_pos_options(wp, d);
375}
376
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200377 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200378handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
379{
380 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
381 {
382 char_u *s = di->di_tv.vval.v_string;
383 int flags = 0;
384
385 if (STRCMP(s, "word") == 0)
386 flags = FIND_IDENT | FIND_STRING;
387 else if (STRCMP(s, "WORD") == 0)
388 flags = FIND_STRING;
389 else if (STRCMP(s, "expr") == 0)
390 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
391 else if (STRCMP(s, "any") != 0)
392 semsg(_(e_invarg2), s);
393 if (flags != 0)
394 {
395 if (mousemoved)
396 set_mousemoved_columns(wp, flags);
397 else
398 set_moved_columns(wp, flags);
399 }
400 }
401 else if (di->di_tv.v_type == VAR_LIST
402 && di->di_tv.vval.v_list != NULL
403 && di->di_tv.vval.v_list->lv_len == 2)
404 {
405 list_T *l = di->di_tv.vval.v_list;
406 int mincol = tv_get_number(&l->lv_first->li_tv);
407 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
408
409 if (mousemoved)
410 {
411 wp->w_popup_mouse_mincol = mincol;
412 wp->w_popup_mouse_maxcol = maxcol;
413 }
414 else
415 {
416 wp->w_popup_mincol = mincol;
417 wp->w_popup_maxcol = maxcol;
418 }
419 }
420 else
421 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
422}
423
424 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200425check_highlight(dict_T *dict, char *name, char_u **pval)
426{
427 dictitem_T *di;
428 char_u *str;
429
430 di = dict_find(dict, (char_u *)name, -1);
431 if (di != NULL)
432 {
433 if (di->di_tv.v_type != VAR_STRING)
434 semsg(_(e_invargval), name);
435 else
436 {
437 str = tv_get_string(&di->di_tv);
438 if (*str != NUL)
439 *pval = vim_strsave(str);
440 }
441 }
442}
443
Bram Moolenaarae943152019-06-16 22:54:14 +0200444/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200445 * Highlight the line with the cursor.
446 * Also scrolls the text to put the cursor line in view.
447 */
448 static void
449popup_highlight_curline(win_T *wp)
450{
451 int id;
452 char buf[100];
453
454 match_delete(wp, 1, FALSE);
455
456 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
457 {
458 // Scroll to show the line with the cursor. This assumes lines don't
459 // wrap.
460 while (wp->w_topline + wp->w_height - 1 < wp->w_cursor.lnum)
461 wp->w_topline++;
462 while (wp->w_cursor.lnum < wp->w_topline)
463 wp->w_topline--;
464
465 id = syn_name2id((char_u *)"PopupSelected");
466 vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
467 match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
468 (char_u *)buf, 10, 1, NULL, NULL);
469 }
470}
471
472/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200473 * Shared between popup_create() and f_popup_setoptions().
474 */
475 static void
476apply_general_options(win_T *wp, dict_T *dict)
477{
478 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200479 int nr;
480 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200481
Bram Moolenaarae943152019-06-16 22:54:14 +0200482 // TODO: flip
483
484 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200485 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200486 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
487 if (wp->w_firstline < 1)
488 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200489
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200490 di = dict_find(dict, (char_u *)"scrollbar", -1);
491 if (di != NULL)
492 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
493
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200494 str = dict_get_string(dict, (char_u *)"title", FALSE);
495 if (str != NULL)
496 {
497 vim_free(wp->w_popup_title);
498 wp->w_popup_title = vim_strsave(str);
499 }
500
Bram Moolenaar402502d2019-05-30 22:07:36 +0200501 di = dict_find(dict, (char_u *)"wrap", -1);
502 if (di != NULL)
503 {
504 nr = dict_get_number(dict, (char_u *)"wrap");
505 wp->w_p_wrap = nr != 0;
506 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200507
Bram Moolenaara42d9452019-06-15 21:46:30 +0200508 di = dict_find(dict, (char_u *)"drag", -1);
509 if (di != NULL)
510 wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200511
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200512 di = dict_find(dict, (char_u *)"close", -1);
513 if (di != NULL)
514 {
515 int ok = TRUE;
516
517 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
518 {
519 char_u *s = di->di_tv.vval.v_string;
520
521 if (STRCMP(s, "none") == 0)
522 wp->w_popup_close = POPCLOSE_NONE;
523 else if (STRCMP(s, "button") == 0)
524 wp->w_popup_close = POPCLOSE_BUTTON;
525 else if (STRCMP(s, "click") == 0)
526 wp->w_popup_close = POPCLOSE_CLICK;
527 else
528 ok = FALSE;
529 }
530 else
531 ok = FALSE;
532 if (!ok)
533 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
534 }
535
Bram Moolenaarae943152019-06-16 22:54:14 +0200536 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
537 if (str != NULL)
538 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
539 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200540
Bram Moolenaarae943152019-06-16 22:54:14 +0200541 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
542 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200543
Bram Moolenaar790498b2019-06-01 22:15:29 +0200544 di = dict_find(dict, (char_u *)"borderhighlight", -1);
545 if (di != NULL)
546 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200547 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200548 emsg(_(e_listreq));
549 else
550 {
551 list_T *list = di->di_tv.vval.v_list;
552 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200553 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200554
Bram Moolenaar403d0902019-07-17 21:37:32 +0200555 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
556 ++i, li = li->li_next)
557 {
558 str = tv_get_string(&li->li_tv);
559 if (*str != NUL)
560 wp->w_border_highlight[i] = vim_strsave(str);
561 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200562 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
563 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200564 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200565 vim_strsave(wp->w_border_highlight[0]);
566 }
567 }
568
Bram Moolenaar790498b2019-06-01 22:15:29 +0200569 di = dict_find(dict, (char_u *)"borderchars", -1);
570 if (di != NULL)
571 {
572 if (di->di_tv.v_type != VAR_LIST)
573 emsg(_(e_listreq));
574 else
575 {
576 list_T *list = di->di_tv.vval.v_list;
577 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200578 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200579
580 if (list != NULL)
581 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
582 ++i, li = li->li_next)
583 {
584 str = tv_get_string(&li->li_tv);
585 if (*str != NUL)
586 wp->w_border_char[i] = mb_ptr2char(str);
587 }
588 if (list->lv_len == 1)
589 for (i = 1; i < 8; ++i)
590 wp->w_border_char[i] = wp->w_border_char[0];
591 if (list->lv_len == 2)
592 {
593 for (i = 4; i < 8; ++i)
594 wp->w_border_char[i] = wp->w_border_char[1];
595 for (i = 1; i < 4; ++i)
596 wp->w_border_char[i] = wp->w_border_char[0];
597 }
598 }
599 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200600
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200601 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
602 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
603
Bram Moolenaarae943152019-06-16 22:54:14 +0200604 di = dict_find(dict, (char_u *)"zindex", -1);
605 if (di != NULL)
606 {
607 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
608 if (wp->w_zindex < 1)
609 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
610 if (wp->w_zindex > 32000)
611 wp->w_zindex = 32000;
612 }
613
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200614 di = dict_find(dict, (char_u *)"mask", -1);
615 if (di != NULL)
616 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200617 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200618
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200619 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200620 {
621 listitem_T *li;
622
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200623 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200624 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
625 li = li->li_next)
626 {
627 if (li->li_tv.v_type != VAR_LIST
628 || li->li_tv.vval.v_list == NULL
629 || li->li_tv.vval.v_list->lv_len != 4)
630 {
631 ok = FALSE;
632 break;
633 }
634 }
635 }
636 if (ok)
637 {
638 wp->w_popup_mask = di->di_tv.vval.v_list;
639 ++wp->w_popup_mask->lv_refcount;
640 }
641 else
642 semsg(_(e_invargval), "mask");
643 }
644
Bram Moolenaarae943152019-06-16 22:54:14 +0200645#if defined(FEAT_TIMERS)
646 // Add timer to close the popup after some time.
647 nr = dict_get_number(dict, (char_u *)"time");
648 if (nr > 0)
649 popup_add_timeout(wp, nr);
650#endif
651
Bram Moolenaar3397f742019-06-02 18:40:06 +0200652 di = dict_find(dict, (char_u *)"moved", -1);
653 if (di != NULL)
654 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200655 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200656 handle_moved_argument(wp, di, FALSE);
657 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200658
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200659 di = dict_find(dict, (char_u *)"mousemoved", -1);
660 if (di != NULL)
661 {
662 set_mousemoved_values(wp);
663 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200664 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200665
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200666 di = dict_find(dict, (char_u *)"cursorline", -1);
667 if (di != NULL)
668 {
669 if (di->di_tv.v_type == VAR_NUMBER)
670 {
671 if (di->di_tv.vval.v_number != 0)
672 wp->w_popup_flags |= POPF_CURSORLINE;
673 else
674 wp->w_popup_flags &= ~POPF_CURSORLINE;
675 }
676 else
677 semsg(_(e_invargval), "cursorline");
678 }
679
Bram Moolenaarae943152019-06-16 22:54:14 +0200680 di = dict_find(dict, (char_u *)"filter", -1);
681 if (di != NULL)
682 {
683 callback_T callback = get_callback(&di->di_tv);
684
685 if (callback.cb_name != NULL)
686 {
687 free_callback(&wp->w_filter_cb);
688 set_callback(&wp->w_filter_cb, &callback);
689 }
690 }
691
692 di = dict_find(dict, (char_u *)"callback", -1);
693 if (di != NULL)
694 {
695 callback_T callback = get_callback(&di->di_tv);
696
697 if (callback.cb_name != NULL)
698 {
699 free_callback(&wp->w_close_cb);
700 set_callback(&wp->w_close_cb, &callback);
701 }
702 }
703}
704
705/*
706 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200707 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200708 */
709 static void
710apply_options(win_T *wp, dict_T *dict)
711{
712 int nr;
713
714 apply_move_options(wp, dict);
715 apply_general_options(wp, dict);
716
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200717 nr = dict_get_number(dict, (char_u *)"hidden");
718 if (nr > 0)
719 {
720 wp->w_popup_flags |= POPF_HIDDEN;
721 --wp->w_buffer->b_nwindows;
722 }
723
Bram Moolenaar33796b32019-06-08 16:01:13 +0200724 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200725 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200726}
727
728/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200729 * Add lines to the popup from a list of strings.
730 */
731 static void
732add_popup_strings(buf_T *buf, list_T *l)
733{
734 listitem_T *li;
735 linenr_T lnum = 0;
736 char_u *p;
737
738 for (li = l->lv_first; li != NULL; li = li->li_next)
739 if (li->li_tv.v_type == VAR_STRING)
740 {
741 p = li->li_tv.vval.v_string;
742 ml_append_buf(buf, lnum++,
743 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
744 }
745}
746
747/*
748 * Add lines to the popup from a list of dictionaries.
749 */
750 static void
751add_popup_dicts(buf_T *buf, list_T *l)
752{
753 listitem_T *li;
754 listitem_T *pli;
755 linenr_T lnum = 0;
756 char_u *p;
757 dict_T *dict;
758
759 // first add the text lines
760 for (li = l->lv_first; li != NULL; li = li->li_next)
761 {
762 if (li->li_tv.v_type != VAR_DICT)
763 {
764 emsg(_(e_dictreq));
765 return;
766 }
767 dict = li->li_tv.vval.v_dict;
768 p = dict == NULL ? NULL
769 : dict_get_string(dict, (char_u *)"text", FALSE);
770 ml_append_buf(buf, lnum++,
771 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
772 }
773
774 // add the text properties
775 lnum = 1;
776 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
777 {
778 dictitem_T *di;
779 list_T *plist;
780
781 dict = li->li_tv.vval.v_dict;
782 di = dict_find(dict, (char_u *)"props", -1);
783 if (di != NULL)
784 {
785 if (di->di_tv.v_type != VAR_LIST)
786 {
787 emsg(_(e_listreq));
788 return;
789 }
790 plist = di->di_tv.vval.v_list;
791 if (plist != NULL)
792 {
793 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
794 {
795 if (pli->li_tv.v_type != VAR_DICT)
796 {
797 emsg(_(e_dictreq));
798 return;
799 }
800 dict = pli->li_tv.vval.v_dict;
801 if (dict != NULL)
802 {
803 int col = dict_get_number(dict, (char_u *)"col");
804
805 prop_add_common( lnum, col, dict, buf, NULL);
806 }
807 }
808 }
809 }
810 }
811}
812
813/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200814 * Get the padding plus border at the top, adjusted to 1 if there is a title.
815 */
816 static int
817popup_top_extra(win_T *wp)
818{
819 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
820
821 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
822 return 1;
823 return extra;
824}
825
826/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200827 * Return the height of popup window "wp", including border and padding.
828 */
829 int
830popup_height(win_T *wp)
831{
832 return wp->w_height
833 + popup_top_extra(wp)
834 + wp->w_popup_padding[2] + wp->w_popup_border[2];
835}
836
837/*
838 * Return the width of popup window "wp", including border, padding and
839 * scrollbar.
840 */
841 int
842popup_width(win_T *wp)
843{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200844 // w_leftcol is how many columns of the core are left of the screen
845 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200846 return wp->w_width + wp->w_leftcol
847 + wp->w_popup_padding[3] + wp->w_popup_border[3]
848 + wp->w_popup_padding[1] + wp->w_popup_border[1]
849 + wp->w_has_scrollbar
850 + wp->w_popup_rightoff;
851}
852
853/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200854 * Adjust the position and size of the popup to fit on the screen.
855 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200856 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200857popup_adjust_position(win_T *wp)
858{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200859 linenr_T lnum;
860 int wrapped = 0;
861 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200862 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200863 int center_vert = FALSE;
864 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200865 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200866 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200867 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
868 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
869 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
870 int extra_height = top_extra + bot_extra;
871 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200872 int org_winrow = wp->w_winrow;
873 int org_wincol = wp->w_wincol;
874 int org_width = wp->w_width;
875 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200876 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200877 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200878 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200879
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200880 wp->w_winrow = 0;
881 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200882 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200883 wp->w_popup_leftoff = 0;
884 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200885 if (wp->w_popup_pos == POPPOS_CENTER)
886 {
887 // center after computing the size
888 center_vert = TRUE;
889 center_hor = TRUE;
890 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200891 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200892 {
893 if (wp->w_wantline == 0)
894 center_vert = TRUE;
895 else if (wp->w_popup_pos == POPPOS_TOPLEFT
896 || wp->w_popup_pos == POPPOS_TOPRIGHT)
897 {
898 wp->w_winrow = wp->w_wantline - 1;
899 if (wp->w_winrow >= Rows)
900 wp->w_winrow = Rows - 1;
901 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200902
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200903 if (wp->w_wantcol == 0)
904 center_hor = TRUE;
905 else if (wp->w_popup_pos == POPPOS_TOPLEFT
906 || wp->w_popup_pos == POPPOS_BOTLEFT)
907 {
908 wp->w_wincol = wp->w_wantcol - 1;
909 if (wp->w_wincol >= Columns - 3)
910 wp->w_wincol = Columns - 3;
911 }
912 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200913
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200914 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200915 // When left aligned use the space available, but shift to the left when we
916 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +0200917 maxspace = Columns - wp->w_wincol - left_extra;
918 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200919 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200920 {
921 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200922 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200923 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924
Bram Moolenaar8d241042019-06-12 23:40:01 +0200925 // start at the desired first line
926 wp->w_topline = wp->w_firstline;
927 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
928 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
929
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200930 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200931 // Use a minimum width of one, so that something shows when there is no
932 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200933 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200934 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +0200935 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200936 {
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200937 // count Tabs for what they are worth
938 int len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
939 (colnr_T)MAXCOL);
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200940
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200941 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200942 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200943 while (len > maxwidth)
944 {
945 ++wrapped;
946 len -= maxwidth;
947 wp->w_width = maxwidth;
948 }
949 }
950 else if (len > maxwidth
951 && allow_adjust_left
952 && (wp->w_popup_pos == POPPOS_TOPLEFT
953 || wp->w_popup_pos == POPPOS_BOTLEFT))
954 {
955 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +0200956 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200957
Bram Moolenaar51c31312019-06-15 22:27:23 +0200958 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200959 {
960 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +0200961
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200962 len -= truncate_shift;
963 shift_by -= truncate_shift;
964 }
965
966 wp->w_wincol -= shift_by;
967 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200968 wp->w_width = maxwidth;
969 }
970 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +0200971 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200972 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +0200973 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
974 wp->w_width = wp->w_maxwidth;
975 }
Bram Moolenaar8d241042019-06-12 23:40:01 +0200976 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +0200977 if (wp->w_maxheight > 0
978 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200979 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200980 }
981
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200982 wp->w_has_scrollbar = wp->w_want_scrollbar
983 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
984
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200985 minwidth = wp->w_minwidth;
986 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
987 {
988 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
989
990 if (minwidth < title_len)
991 minwidth = title_len;
992 }
993
994 if (minwidth > 0 && wp->w_width < minwidth)
995 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200996 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +0200997 {
998 if (wp->w_width > maxspace)
999 // some columns cut off on the right
1000 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001001 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001002 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001003 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001004 {
1005 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1006 if (wp->w_wincol < 0)
1007 wp->w_wincol = 0;
1008 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001009 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1010 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1011 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001012 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1013
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001014 // Right aligned: move to the right if needed.
1015 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001016 if (leftoff >= 0)
1017 wp->w_wincol = leftoff;
1018 else if (wp->w_popup_fixed)
1019 {
1020 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001021 // columns when displaying the window, minus left border and
1022 // padding.
1023 if (-leftoff > left_extra)
1024 wp->w_leftcol = -leftoff - left_extra;
1025 wp->w_width -= wp->w_leftcol;
1026 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001027 if (wp->w_width < 0)
1028 wp->w_width = 0;
1029 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001030 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001031
Bram Moolenaar8d241042019-06-12 23:40:01 +02001032 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1033 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001034 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1035 wp->w_height = wp->w_minheight;
1036 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1037 wp->w_height = wp->w_maxheight;
1038 if (wp->w_height > Rows - wp->w_winrow)
1039 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001040
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001041 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001042 {
1043 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1044 if (wp->w_winrow < 0)
1045 wp->w_winrow = 0;
1046 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001047 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1048 || wp->w_popup_pos == POPPOS_BOTLEFT)
1049 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001050 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001051 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001052 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001053 else
1054 // not enough space, make top aligned
1055 wp->w_winrow = wp->w_wantline + 1;
1056 }
1057
Bram Moolenaar17146962019-05-30 00:12:11 +02001058 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001059
1060 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001061 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001062 if (org_winrow != wp->w_winrow
1063 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001064 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001065 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001066 || org_width != wp->w_width
1067 || org_height != wp->w_height)
1068 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001069 redraw_all_later(VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001070 popup_mask_refresh = TRUE;
1071 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001072}
1073
Bram Moolenaar17627312019-06-02 19:53:44 +02001074typedef enum
1075{
1076 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001077 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001078 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001079 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001080 TYPE_DIALOG,
1081 TYPE_MENU
Bram Moolenaar17627312019-06-02 19:53:44 +02001082} create_type_T;
1083
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001084/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001085 * Make "buf" empty and set the contents to "text".
1086 * Used by popup_create() and popup_settext().
1087 */
1088 static void
1089popup_set_buffer_text(buf_T *buf, typval_T text)
1090{
1091 int lnum;
1092
1093 // Clear the buffer, then replace the lines.
1094 curbuf = buf;
1095 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1096 ml_delete(lnum, FALSE);
1097 curbuf = curwin->w_buffer;
1098
1099 // Add text to the buffer.
1100 if (text.v_type == VAR_STRING)
1101 {
1102 // just a string
1103 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1104 }
1105 else
1106 {
1107 list_T *l = text.vval.v_list;
1108
1109 if (l->lv_len > 0)
1110 {
1111 if (l->lv_first->li_tv.v_type == VAR_STRING)
1112 // list of strings
1113 add_popup_strings(buf, l);
1114 else
1115 // list of dictionaries
1116 add_popup_dicts(buf, l);
1117 }
1118 }
1119
1120 // delete the line that was in the empty buffer
1121 curbuf = buf;
1122 ml_delete(buf->b_ml.ml_line_count, FALSE);
1123 curbuf = curwin->w_buffer;
1124}
1125
1126/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001127 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001128 * popup_atcursor({text}, {options})
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001129 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001130 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001131popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001132{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001133 win_T *wp;
1134 tabpage_T *tp = NULL;
1135 int tabnr;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001136 int new_buffer;
1137 buf_T *buf = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001138 dict_T *d;
1139 int nr;
1140 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001141
1142 // Check arguments look OK.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001143 if (argvars[0].v_type == VAR_NUMBER)
1144 {
1145 buf = buflist_findnr( argvars[0].vval.v_number);
1146 if (buf == NULL)
1147 {
1148 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1149 return NULL;
1150 }
1151 }
1152 else if (!(argvars[0].v_type == VAR_STRING
1153 && argvars[0].vval.v_string != NULL)
1154 && !(argvars[0].v_type == VAR_LIST
1155 && argvars[0].vval.v_list != NULL))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001156 {
1157 emsg(_(e_listreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001158 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001159 }
1160 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1161 {
1162 emsg(_(e_dictreq));
Bram Moolenaara730e552019-06-16 19:05:31 +02001163 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001164 }
1165 d = argvars[1].vval.v_dict;
1166
Bram Moolenaara3fce622019-06-20 02:31:49 +02001167 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1168 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1169 else if (type == TYPE_NOTIFICATION)
1170 tabnr = -1; // notifications are global by default
1171 else
1172 tabnr = 0;
1173 if (tabnr > 0)
1174 {
1175 tp = find_tabpage(tabnr);
1176 if (tp == NULL)
1177 {
Bram Moolenaarb2cda0d2019-06-24 05:06:36 +02001178 semsg(_("E997: Tabpage not found: %d"), tabnr);
Bram Moolenaara3fce622019-06-20 02:31:49 +02001179 return NULL;
1180 }
1181 }
1182
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001183 // Create the window and buffer.
1184 wp = win_alloc_popup_win();
1185 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001186 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001187 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001188 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001189 wp->w_popup_flags = POPF_IS_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001190
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001191 if (buf != NULL)
1192 {
1193 // use existing buffer
1194 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001195 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001196 buffer_ensure_loaded(buf);
1197 }
1198 else
1199 {
1200 // create a new buffer associated with the popup
1201 new_buffer = TRUE;
1202 buf = buflist_new(NULL, NULL, (linenr_T)0,
1203 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1204 if (buf == NULL)
1205 return NULL;
1206 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001207
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001208 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001209
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001210 set_local_options_default(wp);
1211 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001212 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001213 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
1214 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
1215 buf->b_p_ul = -1; // no undo
1216 buf->b_p_swf = FALSE; // no swap file
1217 buf->b_p_bl = FALSE; // unlisted buffer
1218 buf->b_locked = TRUE;
1219 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001220
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001221 // Avoid that 'buftype' is reset when this buffer is entered.
1222 buf->b_p_initialized = TRUE;
1223 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001224
Bram Moolenaara3fce622019-06-20 02:31:49 +02001225 if (tp != NULL)
1226 {
1227 // popup on specified tab page
1228 wp->w_next = tp->tp_first_popupwin;
1229 tp->tp_first_popupwin = wp;
1230 }
1231 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001232 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001233 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001234 wp->w_next = curtab->tp_first_popupwin;
1235 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001236 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001237 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001238 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001239 win_T *prev = first_popupwin;
1240
1241 // Global popup: add at the end, so that it gets displayed on top of
1242 // older ones with the same zindex. Matters for notifications.
1243 if (first_popupwin == NULL)
1244 first_popupwin = wp;
1245 else
1246 {
1247 while (prev->w_next != NULL)
1248 prev = prev->w_next;
1249 prev->w_next = wp;
1250 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001251 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001252
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001253 if (new_buffer)
1254 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001255
Bram Moolenaar17627312019-06-02 19:53:44 +02001256 if (type == TYPE_ATCURSOR)
1257 {
1258 wp->w_popup_pos = POPPOS_BOTLEFT;
1259 setcursor_mayforce(TRUE);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001260 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
Bram Moolenaar17627312019-06-02 19:53:44 +02001261 if (wp->w_wantline == 0) // cursor in first line
1262 {
1263 wp->w_wantline = 2;
1264 wp->w_popup_pos = POPPOS_TOPLEFT;
1265 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001266 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar17627312019-06-02 19:53:44 +02001267 set_moved_values(wp);
1268 set_moved_columns(wp, FIND_STRING);
1269 }
1270
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001271 if (type == TYPE_BEVAL)
1272 {
1273 wp->w_popup_pos = POPPOS_BOTLEFT;
1274
1275 // by default use the mouse position
1276 wp->w_wantline = mouse_row;
1277 if (wp->w_wantline <= 0) // mouse on first line
1278 {
1279 wp->w_wantline = 2;
1280 wp->w_popup_pos = POPPOS_TOPLEFT;
1281 }
1282 wp->w_wantcol = mouse_col + 1;
1283 set_mousemoved_values(wp);
1284 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1285 }
1286
Bram Moolenaar33796b32019-06-08 16:01:13 +02001287 // set default values
1288 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001289 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001290
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001291 if (type == TYPE_NOTIFICATION)
1292 {
1293 win_T *twp, *nextwin;
1294 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001295
1296 // Try to not overlap with another global popup. Guess we need 3
1297 // more screen lines than buffer lines.
1298 wp->w_wantline = 1;
1299 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1300 {
1301 nextwin = twp->w_next;
1302 if (twp != wp
1303 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1304 && twp->w_winrow <= wp->w_wantline - 1 + height
1305 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1306 {
1307 // move to below this popup and restart the loop to check for
1308 // overlap with other popups
1309 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1310 nextwin = first_popupwin;
1311 }
1312 }
1313 if (wp->w_wantline + height > Rows)
1314 {
1315 // can't avoid overlap, put on top in the hope that message goes
1316 // away soon.
1317 wp->w_wantline = 1;
1318 }
1319
1320 wp->w_wantcol = 10;
1321 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001322 wp->w_minwidth = 20;
1323 wp->w_popup_drag = 1;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001324 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001325 for (i = 0; i < 4; ++i)
1326 wp->w_popup_border[i] = 1;
1327 wp->w_popup_padding[1] = 1;
1328 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001329
1330 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001331 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001332 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1333 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001334 }
1335
Bram Moolenaara730e552019-06-16 19:05:31 +02001336 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001337 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001338 wp->w_popup_pos = POPPOS_CENTER;
1339 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
1340 wp->w_popup_drag = 1;
1341 for (i = 0; i < 4; ++i)
1342 {
1343 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001344 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001345 }
1346 }
1347
Bram Moolenaara730e552019-06-16 19:05:31 +02001348 if (type == TYPE_MENU)
1349 {
1350 typval_T tv;
1351 callback_T callback;
1352
1353 tv.v_type = VAR_STRING;
1354 tv.vval.v_string = (char_u *)"popup_filter_menu";
1355 callback = get_callback(&tv);
1356 if (callback.cb_name != NULL)
1357 set_callback(&wp->w_filter_cb, &callback);
1358
1359 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001360 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001361 }
1362
Bram Moolenaarae943152019-06-16 22:54:14 +02001363 for (i = 0; i < 4; ++i)
1364 VIM_CLEAR(wp->w_border_highlight[i]);
1365 for (i = 0; i < 8; ++i)
1366 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001367 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001368 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001369
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001370 // Deal with options.
Bram Moolenaarae943152019-06-16 22:54:14 +02001371 apply_options(wp, argvars[1].vval.v_dict);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001372
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001373#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001374 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1375 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001376#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001377
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001378 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001379
1380 wp->w_vsep_width = 0;
1381
1382 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001383 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001384
1385 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001386}
1387
1388/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001389 * popup_clear()
1390 */
1391 void
1392f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1393{
1394 close_all_popups();
1395}
1396
1397/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001398 * popup_create({text}, {options})
1399 */
1400 void
1401f_popup_create(typval_T *argvars, typval_T *rettv)
1402{
Bram Moolenaar17627312019-06-02 19:53:44 +02001403 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001404}
1405
1406/*
1407 * popup_atcursor({text}, {options})
1408 */
1409 void
1410f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1411{
Bram Moolenaar17627312019-06-02 19:53:44 +02001412 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001413}
1414
1415/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001416 * popup_beval({text}, {options})
1417 */
1418 void
1419f_popup_beval(typval_T *argvars, typval_T *rettv)
1420{
1421 popup_create(argvars, rettv, TYPE_BEVAL);
1422}
1423
1424/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001425 * Invoke the close callback for window "wp" with value "result".
1426 * Careful: The callback may make "wp" invalid!
1427 */
1428 static void
1429invoke_popup_callback(win_T *wp, typval_T *result)
1430{
1431 typval_T rettv;
1432 int dummy;
1433 typval_T argv[3];
1434
1435 argv[0].v_type = VAR_NUMBER;
1436 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1437
1438 if (result != NULL && result->v_type != VAR_UNKNOWN)
1439 copy_tv(result, &argv[1]);
1440 else
1441 {
1442 argv[1].v_type = VAR_NUMBER;
1443 argv[1].vval.v_number = 0;
1444 }
1445
1446 argv[2].v_type = VAR_UNKNOWN;
1447
1448 call_callback(&wp->w_close_cb, -1,
1449 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1450 if (result != NULL)
1451 clear_tv(&argv[1]);
1452 clear_tv(&rettv);
1453}
1454
1455/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001456 * Close popup "wp" and invoke any close callback for it.
1457 */
1458 static void
1459popup_close_and_callback(win_T *wp, typval_T *arg)
1460{
1461 int id = wp->w_id;
1462
1463 if (wp->w_close_cb.cb_name != NULL)
1464 // Careful: This may make "wp" invalid.
1465 invoke_popup_callback(wp, arg);
1466
1467 popup_close(id);
1468}
1469
1470/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001471 * Close popup "wp" because of a mouse click.
1472 */
1473 void
1474popup_close_for_mouse_click(win_T *wp)
1475{
1476 typval_T res;
1477
1478 res.v_type = VAR_NUMBER;
1479 res.vval.v_number = -2;
1480 popup_close_and_callback(wp, &res);
1481}
1482
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001483 static void
1484check_mouse_moved(win_T *wp, win_T *mouse_wp)
1485{
1486 // Close the popup when all if these are true:
1487 // - the mouse is not on this popup
1488 // - "mousemoved" was used
1489 // - the mouse is no longer on the same screen row or the mouse column is
1490 // outside of the relevant text
1491 if (wp != mouse_wp
1492 && wp->w_popup_mouse_row != 0
1493 && (wp->w_popup_mouse_row != mouse_row
1494 || mouse_col < wp->w_popup_mouse_mincol
1495 || mouse_col > wp->w_popup_mouse_maxcol))
1496 {
1497 typval_T res;
1498
1499 res.v_type = VAR_NUMBER;
1500 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001501 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001502 popup_close_and_callback(wp, &res);
1503 }
1504}
1505
1506/*
1507 * Called when the mouse moved: may close a popup with "mousemoved".
1508 */
1509 void
1510popup_handle_mouse_moved(void)
1511{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001512 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001513 win_T *mouse_wp;
1514 int row = mouse_row;
1515 int col = mouse_col;
1516
1517 // find the window where the mouse is in
1518 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1519
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001520 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1521 {
1522 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001523 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001524 }
1525 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1526 {
1527 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001528 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001529 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001530}
1531
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001532/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001533 * In a filter: check if the typed key is a mouse event that is used for
1534 * dragging the popup.
1535 */
1536 static void
1537filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1538{
1539 int row = mouse_row;
1540 int col = mouse_col;
1541
1542 if (wp->w_popup_drag
1543 && is_mouse_key(c)
1544 && (wp == popup_dragwin
1545 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1546 // do not consume the key, allow for dragging the popup
1547 rettv->vval.v_number = 0;
1548}
1549
Bram Moolenaara730e552019-06-16 19:05:31 +02001550/*
1551 * popup_filter_menu({text}, {options})
1552 */
1553 void
1554f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1555{
1556 int id = tv_get_number(&argvars[0]);
1557 win_T *wp = win_id2wp(id);
1558 char_u *key = tv_get_string(&argvars[1]);
1559 typval_T res;
1560 int c;
1561 linenr_T old_lnum;
1562
1563 // If the popup has been closed do not consume the key.
1564 if (wp == NULL)
1565 return;
1566
1567 c = *key;
1568 if (c == K_SPECIAL && key[1] != NUL)
1569 c = TO_SPECIAL(key[1], key[2]);
1570
1571 // consume all keys until done
1572 rettv->vval.v_number = 1;
1573 res.v_type = VAR_NUMBER;
1574
1575 old_lnum = wp->w_cursor.lnum;
1576 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1577 --wp->w_cursor.lnum;
1578 if ((c == 'j' || c == 'J' || c == K_DOWN)
1579 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1580 ++wp->w_cursor.lnum;
1581 if (old_lnum != wp->w_cursor.lnum)
1582 {
1583 popup_highlight_curline(wp);
1584 return;
1585 }
1586
1587 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1588 {
1589 // Cancelled, invoke callback with -1
1590 res.vval.v_number = -1;
1591 popup_close_and_callback(wp, &res);
1592 return;
1593 }
1594 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1595 {
1596 // Invoke callback with current index.
1597 res.vval.v_number = wp->w_cursor.lnum;
1598 popup_close_and_callback(wp, &res);
1599 return;
1600 }
1601
1602 filter_handle_drag(wp, c, rettv);
1603}
1604
1605/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001606 * popup_filter_yesno({text}, {options})
1607 */
1608 void
1609f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1610{
1611 int id = tv_get_number(&argvars[0]);
1612 win_T *wp = win_id2wp(id);
1613 char_u *key = tv_get_string(&argvars[1]);
1614 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001615 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001616
1617 // If the popup has been closed don't consume the key.
1618 if (wp == NULL)
1619 return;
1620
Bram Moolenaara730e552019-06-16 19:05:31 +02001621 c = *key;
1622 if (c == K_SPECIAL && key[1] != NUL)
1623 c = TO_SPECIAL(key[1], key[2]);
1624
Bram Moolenaara42d9452019-06-15 21:46:30 +02001625 // consume all keys until done
1626 rettv->vval.v_number = 1;
1627
Bram Moolenaara730e552019-06-16 19:05:31 +02001628 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001629 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001630 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001631 res.vval.v_number = 0;
1632 else
1633 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001634 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001635 return;
1636 }
1637
1638 // Invoke callback
1639 res.v_type = VAR_NUMBER;
1640 popup_close_and_callback(wp, &res);
1641}
1642
1643/*
1644 * popup_dialog({text}, {options})
1645 */
1646 void
1647f_popup_dialog(typval_T *argvars, typval_T *rettv)
1648{
1649 popup_create(argvars, rettv, TYPE_DIALOG);
1650}
1651
1652/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001653 * popup_menu({text}, {options})
1654 */
1655 void
1656f_popup_menu(typval_T *argvars, typval_T *rettv)
1657{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001658 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001659}
1660
1661/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001662 * popup_notification({text}, {options})
1663 */
1664 void
1665f_popup_notification(typval_T *argvars, typval_T *rettv)
1666{
1667 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1668}
1669
1670/*
1671 * Find the popup window with window-ID "id".
1672 * If the popup window does not exist NULL is returned.
1673 * If the window is not a popup window, and error message is given.
1674 */
1675 static win_T *
1676find_popup_win(int id)
1677{
1678 win_T *wp = win_id2wp(id);
1679
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001680 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001681 {
1682 semsg(_("E993: window %d is not a popup window"), id);
1683 return NULL;
1684 }
1685 return wp;
1686}
1687
1688/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001689 * popup_close({id})
1690 */
1691 void
1692f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1693{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001694 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001695 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001696
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001697 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001698 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001699}
1700
1701/*
1702 * popup_hide({id})
1703 */
1704 void
1705f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1706{
1707 int id = (int)tv_get_number(argvars);
1708 win_T *wp = find_popup_win(id);
1709
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001710 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001711 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001712 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001713 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001714 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001715 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001716 }
1717}
1718
1719/*
1720 * popup_show({id})
1721 */
1722 void
1723f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1724{
1725 int id = (int)tv_get_number(argvars);
1726 win_T *wp = find_popup_win(id);
1727
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001728 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001729 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001730 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001731 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001732 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001733 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001734 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001735}
1736
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001737/*
1738 * popup_settext({id}, {text})
1739 */
1740 void
1741f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1742{
1743 int id = (int)tv_get_number(&argvars[0]);
1744 win_T *wp = find_popup_win(id);
1745
1746 if (wp != NULL)
1747 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001748 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1749 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1750 else
1751 {
1752 popup_set_buffer_text(wp->w_buffer, argvars[1]);
1753 popup_adjust_position(wp);
1754 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001755 }
1756}
1757
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001758 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001759popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001760{
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001761 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001762 if (wp->w_winrow + wp->w_height >= cmdline_row)
1763 clear_cmdline = TRUE;
1764 win_free_popup(wp);
1765 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001766 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001767}
1768
Bram Moolenaarec583842019-05-26 14:11:23 +02001769/*
1770 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001771 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02001772 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001773 void
Bram Moolenaarec583842019-05-26 14:11:23 +02001774popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001775{
1776 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02001777 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001778 win_T *prev = NULL;
1779
Bram Moolenaarec583842019-05-26 14:11:23 +02001780 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001781 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02001782 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001783 {
1784 if (prev == NULL)
1785 first_popupwin = wp->w_next;
1786 else
1787 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001788 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001789 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001790 }
1791
Bram Moolenaarec583842019-05-26 14:11:23 +02001792 // go through tab-local popups
1793 FOR_ALL_TABPAGES(tp)
1794 popup_close_tabpage(tp, id);
1795}
1796
1797/*
1798 * Close a popup window with Window-id "id" in tabpage "tp".
1799 */
1800 void
1801popup_close_tabpage(tabpage_T *tp, int id)
1802{
1803 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001804 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02001805 win_T *prev = NULL;
1806
Bram Moolenaarec583842019-05-26 14:11:23 +02001807 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
1808 if (wp->w_id == id)
1809 {
1810 if (prev == NULL)
1811 *root = wp->w_next;
1812 else
1813 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001814 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02001815 return;
1816 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001817}
1818
1819 void
1820close_all_popups(void)
1821{
1822 while (first_popupwin != NULL)
1823 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001824 while (curtab->tp_first_popupwin != NULL)
1825 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001826}
1827
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001828/*
1829 * popup_move({id}, {options})
1830 */
1831 void
1832f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
1833{
Bram Moolenaarae943152019-06-16 22:54:14 +02001834 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001835 int id = (int)tv_get_number(argvars);
1836 win_T *wp = find_popup_win(id);
1837
1838 if (wp == NULL)
1839 return; // invalid {id}
1840
1841 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1842 {
1843 emsg(_(e_dictreq));
1844 return;
1845 }
Bram Moolenaarae943152019-06-16 22:54:14 +02001846 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001847
Bram Moolenaarae943152019-06-16 22:54:14 +02001848 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001849
1850 if (wp->w_winrow + wp->w_height >= cmdline_row)
1851 clear_cmdline = TRUE;
1852 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001853}
1854
Bram Moolenaarbc133542019-05-29 20:26:46 +02001855/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001856 * popup_setoptions({id}, {options})
1857 */
1858 void
1859f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1860{
1861 dict_T *dict;
1862 int id = (int)tv_get_number(argvars);
1863 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001864 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001865
1866 if (wp == NULL)
1867 return; // invalid {id}
1868
1869 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
1870 {
1871 emsg(_(e_dictreq));
1872 return;
1873 }
1874 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001875 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02001876
1877 apply_move_options(wp, dict);
1878 apply_general_options(wp, dict);
1879
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001880 if (old_firstline != wp->w_firstline)
1881 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02001882 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001883 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02001884 popup_adjust_position(wp);
1885}
1886
1887/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001888 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02001889 */
1890 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001891f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001892{
1893 dict_T *dict;
1894 int id = (int)tv_get_number(argvars);
1895 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001896 int top_extra;
1897 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02001898
1899 if (rettv_dict_alloc(rettv) == OK)
1900 {
1901 if (wp == NULL)
1902 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001903 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001904 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1905
Bram Moolenaarbc133542019-05-29 20:26:46 +02001906 dict = rettv->vval.v_dict;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001907
Bram Moolenaarbc133542019-05-29 20:26:46 +02001908 dict_add_number(dict, "line", wp->w_winrow + 1);
1909 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001910 dict_add_number(dict, "width", wp->w_width + left_extra
1911 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
1912 dict_add_number(dict, "height", wp->w_height + top_extra
1913 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001914
1915 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
1916 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
1917 dict_add_number(dict, "core_width", wp->w_width);
1918 dict_add_number(dict, "core_height", wp->w_height);
1919
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001920 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02001921 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001922 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001923 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001924 }
1925}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001926/*
1927 * popup_locate({row}, {col})
1928 */
1929 void
1930f_popup_locate(typval_T *argvars, typval_T *rettv)
1931{
1932 int row = tv_get_number(&argvars[0]) - 1;
1933 int col = tv_get_number(&argvars[1]) - 1;
1934 win_T *wp;
1935
1936 wp = mouse_find_win(&row, &col, FIND_POPUP);
1937 if (WIN_IS_POPUP(wp))
1938 rettv->vval.v_number = wp->w_id;
1939}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001940
1941/*
Bram Moolenaarae943152019-06-16 22:54:14 +02001942 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
1943 */
1944 static void
1945get_padding_border(dict_T *dict, int *array, char *name)
1946{
1947 list_T *list;
1948 int i;
1949
1950 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
1951 return;
1952
1953 list = list_alloc();
1954 if (list != NULL)
1955 {
1956 dict_add_list(dict, name, list);
1957 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
1958 for (i = 0; i < 4; ++i)
1959 list_append_number(list, array[i]);
1960 }
1961}
1962
1963/*
1964 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
1965 */
1966 static void
1967get_borderhighlight(dict_T *dict, win_T *wp)
1968{
1969 list_T *list;
1970 int i;
1971
1972 for (i = 0; i < 4; ++i)
1973 if (wp->w_border_highlight[i] != NULL)
1974 break;
1975 if (i == 4)
1976 return;
1977
1978 list = list_alloc();
1979 if (list != NULL)
1980 {
1981 dict_add_list(dict, "borderhighlight", list);
1982 for (i = 0; i < 4; ++i)
1983 list_append_string(list, wp->w_border_highlight[i], -1);
1984 }
1985}
1986
1987/*
1988 * For popup_getoptions(): add a "borderchars" entry to "dict".
1989 */
1990 static void
1991get_borderchars(dict_T *dict, win_T *wp)
1992{
1993 list_T *list;
1994 int i;
1995 char_u buf[NUMBUFLEN];
1996 int len;
1997
1998 for (i = 0; i < 8; ++i)
1999 if (wp->w_border_char[i] != 0)
2000 break;
2001 if (i == 8)
2002 return;
2003
2004 list = list_alloc();
2005 if (list != NULL)
2006 {
2007 dict_add_list(dict, "borderchars", list);
2008 for (i = 0; i < 8; ++i)
2009 {
2010 len = mb_char2bytes(wp->w_border_char[i], buf);
2011 list_append_string(list, buf, len);
2012 }
2013 }
2014}
2015
2016/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002017 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002018 */
2019 static void
2020get_moved_list(dict_T *dict, win_T *wp)
2021{
2022 list_T *list;
2023
2024 list = list_alloc();
2025 if (list != NULL)
2026 {
2027 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002028 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002029 list_append_number(list, wp->w_popup_mincol);
2030 list_append_number(list, wp->w_popup_maxcol);
2031 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002032 list = list_alloc();
2033 if (list != NULL)
2034 {
2035 dict_add_list(dict, "mousemoved", list);
2036 list_append_number(list, wp->w_popup_mouse_row);
2037 list_append_number(list, wp->w_popup_mouse_mincol);
2038 list_append_number(list, wp->w_popup_mouse_maxcol);
2039 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002040}
2041
2042/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002043 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002044 */
2045 void
2046f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2047{
2048 dict_T *dict;
2049 int id = (int)tv_get_number(argvars);
2050 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002051 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002052 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002053
2054 if (rettv_dict_alloc(rettv) == OK)
2055 {
2056 if (wp == NULL)
2057 return;
2058
2059 dict = rettv->vval.v_dict;
2060 dict_add_number(dict, "line", wp->w_wantline);
2061 dict_add_number(dict, "col", wp->w_wantcol);
2062 dict_add_number(dict, "minwidth", wp->w_minwidth);
2063 dict_add_number(dict, "minheight", wp->w_minheight);
2064 dict_add_number(dict, "maxheight", wp->w_maxheight);
2065 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002066 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002067 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002068 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002069 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002070 dict_add_string(dict, "title", wp->w_popup_title);
2071 dict_add_number(dict, "wrap", wp->w_p_wrap);
2072 dict_add_number(dict, "drag", wp->w_popup_drag);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002073 dict_add_number(dict, "cursorline", (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002074 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002075 if (wp->w_scrollbar_highlight != NULL)
2076 dict_add_string(dict, "scrollbarhighlight",
2077 wp->w_scrollbar_highlight);
2078 if (wp->w_thumb_highlight != NULL)
2079 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002080
Bram Moolenaara3fce622019-06-20 02:31:49 +02002081 // find the tabpage that holds this popup
2082 i = 1;
2083 FOR_ALL_TABPAGES(tp)
2084 {
2085 win_T *p;
2086
2087 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2088 if (p->w_id == id)
2089 break;
2090 if (p != NULL)
2091 break;
2092 ++i;
2093 }
2094 if (tp == NULL)
2095 i = -1; // must be global
2096 else if (tp == curtab)
2097 i = 0;
2098 dict_add_number(dict, "tabpage", i);
2099
Bram Moolenaarae943152019-06-16 22:54:14 +02002100 get_padding_border(dict, wp->w_popup_padding, "padding");
2101 get_padding_border(dict, wp->w_popup_border, "border");
2102 get_borderhighlight(dict, wp);
2103 get_borderchars(dict, wp);
2104 get_moved_list(dict, wp);
2105
2106 if (wp->w_filter_cb.cb_name != NULL)
2107 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2108 if (wp->w_close_cb.cb_name != NULL)
2109 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002110
2111 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2112 ++i)
2113 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2114 {
2115 dict_add_string(dict, "pos",
2116 (char_u *)poppos_entries[i].pp_name);
2117 break;
2118 }
2119
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002120 dict_add_string(dict, "close", (char_u *)(
2121 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2122 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2123
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002124# if defined(FEAT_TIMERS)
2125 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2126 ? (long)wp->w_popup_timer->tr_interval : 0L);
2127# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002128 }
2129}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002130
2131 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002132error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002133{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002134 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002135 {
2136 emsg(_("E994: Not allowed in a popup window"));
2137 return TRUE;
2138 }
2139 return FALSE;
2140}
2141
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002142/*
2143 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002144 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002145 */
2146 void
2147popup_reset_handled()
2148{
2149 win_T *wp;
2150
2151 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2152 wp->w_popup_flags &= ~POPF_HANDLED;
2153 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2154 wp->w_popup_flags &= ~POPF_HANDLED;
2155}
2156
2157/*
2158 * Find the next visible popup where POPF_HANDLED is not set.
2159 * Must have called popup_reset_handled() first.
2160 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2161 * popup with the highest zindex.
2162 */
2163 win_T *
2164find_next_popup(int lowest)
2165{
2166 win_T *wp;
2167 win_T *found_wp;
2168 int found_zindex;
2169
2170 found_zindex = lowest ? INT_MAX : 0;
2171 found_wp = NULL;
2172 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2173 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2174 && (lowest ? wp->w_zindex < found_zindex
2175 : wp->w_zindex > found_zindex))
2176 {
2177 found_zindex = wp->w_zindex;
2178 found_wp = wp;
2179 }
2180 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2181 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2182 && (lowest ? wp->w_zindex < found_zindex
2183 : wp->w_zindex > found_zindex))
2184 {
2185 found_zindex = wp->w_zindex;
2186 found_wp = wp;
2187 }
2188
2189 if (found_wp != NULL)
2190 found_wp->w_popup_flags |= POPF_HANDLED;
2191 return found_wp;
2192}
2193
2194/*
2195 * Invoke the filter callback for window "wp" with typed character "c".
2196 * Uses the global "mod_mask" for modifiers.
2197 * Returns the return value of the filter.
2198 * Careful: The filter may make "wp" invalid!
2199 */
2200 static int
2201invoke_popup_filter(win_T *wp, int c)
2202{
2203 int res;
2204 typval_T rettv;
2205 int dummy;
2206 typval_T argv[3];
2207 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002208 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002209
Bram Moolenaara42d9452019-06-15 21:46:30 +02002210 // Emergency exit: CTRL-C closes the popup.
2211 if (c == Ctrl_C)
2212 {
2213 rettv.v_type = VAR_NUMBER;
2214 rettv.vval.v_number = -1;
2215 popup_close_and_callback(wp, &rettv);
2216 return 1;
2217 }
2218
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002219 argv[0].v_type = VAR_NUMBER;
2220 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2221
2222 // Convert the number to a string, so that the function can use:
2223 // if a:c == "\<F2>"
2224 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2225 argv[1].v_type = VAR_STRING;
2226 argv[1].vval.v_string = vim_strsave(buf);
2227
2228 argv[2].v_type = VAR_UNKNOWN;
2229
Bram Moolenaara42d9452019-06-15 21:46:30 +02002230 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002231 call_callback(&wp->w_filter_cb, -1,
2232 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002233 if (old_lnum != wp->w_cursor.lnum)
2234 popup_highlight_curline(wp);
2235
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002236 res = tv_get_number(&rettv);
2237 vim_free(argv[1].vval.v_string);
2238 clear_tv(&rettv);
2239 return res;
2240}
2241
2242/*
2243 * Called when "c" was typed: invoke popup filter callbacks.
2244 * Returns TRUE when the character was consumed,
2245 */
2246 int
2247popup_do_filter(int c)
2248{
2249 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002250 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002251
2252 popup_reset_handled();
2253
2254 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2255 if (wp->w_filter_cb.cb_name != NULL)
2256 res = invoke_popup_filter(wp, c);
2257
2258 return res;
2259}
2260
Bram Moolenaar3397f742019-06-02 18:40:06 +02002261/*
2262 * Called when the cursor moved: check if any popup needs to be closed if the
2263 * cursor moved far enough.
2264 */
2265 void
2266popup_check_cursor_pos()
2267{
2268 win_T *wp;
2269 typval_T tv;
2270
2271 popup_reset_handled();
2272 while ((wp = find_next_popup(TRUE)) != NULL)
2273 if (wp->w_popup_curwin != NULL
2274 && (curwin != wp->w_popup_curwin
2275 || curwin->w_cursor.lnum != wp->w_popup_lnum
2276 || curwin->w_cursor.col < wp->w_popup_mincol
2277 || curwin->w_cursor.col > wp->w_popup_maxcol))
2278 {
2279 tv.v_type = VAR_NUMBER;
2280 tv.vval.v_number = -1;
2281 popup_close_and_callback(wp, &tv);
2282 }
2283}
2284
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002285/*
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002286 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2287 * "col" and "line" are screen coordinates.
2288 */
2289 static int
2290popup_masked(win_T *wp, int screencol, int screenline)
2291{
Bram Moolenaard529ba52019-07-02 23:13:53 +02002292 int col = screencol - wp->w_wincol + 1 + wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002293 int line = screenline - wp->w_winrow + 1;
2294 listitem_T *lio, *li;
2295 int width, height;
2296
2297 if (wp->w_popup_mask == NULL)
2298 return FALSE;
2299 width = popup_width(wp);
2300 height = popup_height(wp);
2301
2302 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2303 {
2304 int cols, cole;
2305 int lines, linee;
2306
2307 li = lio->li_tv.vval.v_list->lv_first;
2308 cols = tv_get_number(&li->li_tv);
2309 if (cols < 0)
2310 cols = width + cols + 1;
2311 if (col < cols)
2312 continue;
2313 li = li->li_next;
2314 cole = tv_get_number(&li->li_tv);
2315 if (cole < 0)
2316 cole = width + cole + 1;
2317 if (col > cole)
2318 continue;
2319 li = li->li_next;
2320 lines = tv_get_number(&li->li_tv);
2321 if (lines < 0)
2322 lines = height + lines + 1;
2323 if (line < lines)
2324 continue;
2325 li = li->li_next;
2326 linee = tv_get_number(&li->li_tv);
2327 if (linee < 0)
2328 linee = height + linee + 1;
2329 if (line > linee)
2330 continue;
2331 return TRUE;
2332 }
2333 return FALSE;
2334}
2335
2336/*
2337 * Set flags in popup_transparent[] for window "wp" to "val".
2338 */
2339 static void
2340update_popup_transparent(win_T *wp, int val)
2341{
2342 if (wp->w_popup_mask != NULL)
2343 {
2344 int width = popup_width(wp);
2345 int height = popup_height(wp);
2346 listitem_T *lio, *li;
2347 int cols, cole;
2348 int lines, linee;
2349 int col, line;
2350
2351 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2352 {
2353 li = lio->li_tv.vval.v_list->lv_first;
2354 cols = tv_get_number(&li->li_tv);
2355 if (cols < 0)
2356 cols = width + cols + 1;
2357 li = li->li_next;
2358 cole = tv_get_number(&li->li_tv);
2359 if (cole < 0)
2360 cole = width + cole + 1;
2361 li = li->li_next;
2362 lines = tv_get_number(&li->li_tv);
2363 if (lines < 0)
2364 lines = height + lines + 1;
2365 li = li->li_next;
2366 linee = tv_get_number(&li->li_tv);
2367 if (linee < 0)
2368 linee = height + linee + 1;
2369
2370 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002371 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002372 if (cols < 0)
2373 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002374 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002375 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002376 if (lines < 0)
2377 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002378 for (line = lines; line < linee
2379 && line + wp->w_winrow < screen_Rows; ++line)
2380 for (col = cols; col < cole
2381 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002382 popup_transparent[(line + wp->w_winrow) * screen_Columns
2383 + col + wp->w_wincol] = val;
2384 }
2385 }
2386}
2387
2388/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002389 * Update "popup_mask" if needed.
2390 * Also recomputes the popup size and positions.
2391 * Also updates "popup_visible".
2392 * Also marks window lines for redrawing.
2393 */
2394 void
2395may_update_popup_mask(int type)
2396{
2397 win_T *wp;
2398 short *mask;
2399 int line, col;
2400 int redraw_all = FALSE;
2401
2402 // Need to recompute when switching tabs.
2403 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2404 // (such as the screen size) must have changed.
2405 if (popup_mask_tab != curtab || type >= NOT_VALID)
2406 {
2407 popup_mask_refresh = TRUE;
2408 redraw_all = TRUE;
2409 }
2410 if (!popup_mask_refresh)
2411 {
2412 // Check if any buffer has changed.
2413 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2414 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2415 popup_mask_refresh = TRUE;
2416 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2417 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2418 popup_mask_refresh = TRUE;
2419 if (!popup_mask_refresh)
2420 return;
2421 }
2422
2423 // Need to update the mask, something has changed.
2424 popup_mask_refresh = FALSE;
2425 popup_mask_tab = curtab;
2426 popup_visible = FALSE;
2427
2428 // If redrawing everything, just update "popup_mask".
2429 // If redrawing only what is needed, update "popup_mask_next" and then
2430 // compare with "popup_mask" to see what changed.
2431 if (type >= SOME_VALID)
2432 mask = popup_mask;
2433 else
2434 mask = popup_mask_next;
2435 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2436
2437 // Find the window with the lowest zindex that hasn't been handled yet,
2438 // so that the window with a higher zindex overwrites the value in
2439 // popup_mask.
2440 popup_reset_handled();
2441 while ((wp = find_next_popup(TRUE)) != NULL)
2442 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002443 int height;
2444 int width;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002445
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002446 popup_visible = TRUE;
2447
2448 // Recompute the position if the text changed.
2449 if (redraw_all
2450 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2451 popup_adjust_position(wp);
2452
Bram Moolenaard529ba52019-07-02 23:13:53 +02002453 height = popup_height(wp);
2454 width = popup_width(wp) - wp->w_popup_leftoff;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002455 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002456 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002457 for (col = wp->w_wincol;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002458 col < wp->w_wincol + width && col < screen_Columns; ++col)
2459 if (!popup_masked(wp, col, line))
2460 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002461 }
2462
2463 // Only check which lines are to be updated if not already
2464 // updating all lines.
2465 if (mask == popup_mask_next)
2466 for (line = 0; line < screen_Rows; ++line)
2467 {
2468 int col_done = 0;
2469
2470 for (col = 0; col < screen_Columns; ++col)
2471 {
2472 int off = line * screen_Columns + col;
2473
2474 if (popup_mask[off] != popup_mask_next[off])
2475 {
2476 popup_mask[off] = popup_mask_next[off];
2477
2478 if (line >= cmdline_row)
2479 {
2480 // the command line needs to be cleared if text below
2481 // the popup is now visible.
2482 if (!msg_scrolled && popup_mask_next[off] == 0)
2483 clear_cmdline = TRUE;
2484 }
2485 else if (col >= col_done)
2486 {
2487 linenr_T lnum;
2488 int line_cp = line;
2489 int col_cp = col;
2490
2491 // The screen position "line" / "col" needs to be
2492 // redrawn. Figure out what window that is and update
2493 // w_redraw_top and w_redr_bot. Only needs to be done
2494 // once for each window line.
2495 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2496 if (wp != NULL)
2497 {
2498 if (line_cp >= wp->w_height)
2499 // In (or below) status line
2500 wp->w_redr_status = TRUE;
2501 // compute the position in the buffer line from the
2502 // position on the screen
2503 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
2504 &lnum))
2505 // past bottom
2506 wp->w_redr_status = TRUE;
2507 else
2508 redrawWinline(wp, lnum);
2509
2510 // This line is going to be redrawn, no need to
2511 // check until the right side of the window.
2512 col_done = wp->w_wincol + wp->w_width - 1;
2513 }
2514 }
2515 }
2516 }
2517 }
2518}
2519
2520/*
2521 * Return a string of "len" spaces in IObuff.
2522 */
2523 static char_u *
2524get_spaces(int len)
2525{
2526 vim_memset(IObuff, ' ', (size_t)len);
2527 IObuff[len] = NUL;
2528 return IObuff;
2529}
2530
2531/*
2532 * Update popup windows. They are drawn on top of normal windows.
2533 * "win_update" is called for each popup window, lowest zindex first.
2534 */
2535 void
2536update_popups(void (*win_update)(win_T *wp))
2537{
2538 win_T *wp;
2539 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002540 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002541 int total_width;
2542 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002543 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002544 int popup_attr;
2545 int border_attr[4];
2546 int border_char[8];
2547 char_u buf[MB_MAXBYTES];
2548 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002549 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002550 int padcol = 0;
2551 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002552 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002553 int sb_thumb_top = 0;
2554 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002555 int attr_scroll = 0;
2556 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002557
2558 // Find the window with the lowest zindex that hasn't been updated yet,
2559 // so that the window with a higher zindex is drawn later, thus goes on
2560 // top.
2561 popup_reset_handled();
2562 while ((wp = find_next_popup(TRUE)) != NULL)
2563 {
2564 // This drawing uses the zindex of the popup window, so that it's on
2565 // top of the text but doesn't draw when another popup with higher
2566 // zindex is on top of the character.
2567 screen_zindex = wp->w_zindex;
2568
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002569 // Set flags in popup_transparent[] for masked cells.
2570 update_popup_transparent(wp, 1);
2571
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002572 // adjust w_winrow and w_wincol for border and padding, since
2573 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002574 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002575 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2576 - wp->w_popup_leftoff;
2577 if (wp->w_wincol + left_extra < 0)
2578 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002579 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002580 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002581
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002582 // Draw the popup text, unless it's off screen.
2583 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2584 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002585
2586 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002587 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002588
Bram Moolenaard529ba52019-07-02 23:13:53 +02002589 total_width = popup_width(wp);
2590 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002591 popup_attr = get_wcr_attr(wp);
2592
2593 // We can only use these line drawing characters when 'encoding' is
2594 // "utf-8" and 'ambiwidth' is "single".
2595 if (enc_utf8 && *p_ambw == 's')
2596 {
2597 border_char[0] = border_char[2] = 0x2550;
2598 border_char[1] = border_char[3] = 0x2551;
2599 border_char[4] = 0x2554;
2600 border_char[5] = 0x2557;
2601 border_char[6] = 0x255d;
2602 border_char[7] = 0x255a;
2603 }
2604 else
2605 {
2606 border_char[0] = border_char[2] = '-';
2607 border_char[1] = border_char[3] = '|';
2608 for (i = 4; i < 8; ++i)
2609 border_char[i] = '+';
2610 }
2611 for (i = 0; i < 8; ++i)
2612 if (wp->w_border_char[i] != 0)
2613 border_char[i] = wp->w_border_char[i];
2614
2615 for (i = 0; i < 4; ++i)
2616 {
2617 border_attr[i] = popup_attr;
2618 if (wp->w_border_highlight[i] != NULL)
2619 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2620 }
2621
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002622 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002623 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002624 if (wp->w_popup_border[0] > 0)
2625 {
2626 // top border
2627 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002628 wincol < 0 ? 0 : wincol, wincol + total_width,
2629 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002630 ? border_char[4] : border_char[0],
2631 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002632 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002633 {
2634 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2635 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002636 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002637 }
2638 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002639 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2640 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002641
Bram Moolenaard529ba52019-07-02 23:13:53 +02002642 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2643 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002644 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002645 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2646 - wp->w_has_scrollbar;
2647 if (padcol < 0)
2648 {
2649 padwidth += padcol;
2650 padcol = 0;
2651 }
2652 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002653 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002654 {
2655 // top padding
2656 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002657 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002658 ' ', ' ', popup_attr);
2659 }
2660
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002661 // Title goes on top of border or padding.
2662 if (wp->w_popup_title != NULL)
2663 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2664 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2665
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002666 // Compute scrollbar thumb position and size.
2667 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002668 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002669 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2670
Bram Moolenaar68acb412019-06-26 03:40:36 +02002671 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2672 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002673 if (sb_thumb_height == 0)
2674 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002675 if (linecount <= wp->w_height)
2676 // it just fits, avoid divide by zero
2677 sb_thumb_top = 0;
2678 else
2679 sb_thumb_top = (wp->w_topline - 1
2680 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002681 * (wp->w_height - sb_thumb_height)
2682 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002683 if (wp->w_scrollbar_highlight != NULL)
2684 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2685 else
2686 attr_scroll = highlight_attr[HLF_PSB];
2687 if (wp->w_thumb_highlight != NULL)
2688 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
2689 else
2690 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002691 }
2692
2693 for (i = wp->w_popup_border[0];
2694 i < total_height - wp->w_popup_border[2]; ++i)
2695 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002696 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002697 // left and right padding only needed next to the body
2698 int do_padding =
2699 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
2700 && i < total_height - wp->w_popup_border[2]
2701 - wp->w_popup_padding[2];
2702
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002703 row = wp->w_winrow + i;
2704
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002705 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002706 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002707 {
2708 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002709 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002710 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02002711 if (do_padding && wp->w_popup_padding[3] > 0)
2712 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002713 int col = wincol + wp->w_popup_border[3];
2714
Bram Moolenaard529ba52019-07-02 23:13:53 +02002715 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002716 pad_left = wp->w_popup_padding[3];
2717 if (col < 0)
2718 {
2719 pad_left += col;
2720 col = 0;
2721 }
2722 if (pad_left > 0)
2723 screen_puts(get_spaces(pad_left), row, col, popup_attr);
2724 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002725 // scrollbar
2726 if (wp->w_has_scrollbar)
2727 {
2728 int line = i - top_off;
2729 int scroll_col = wp->w_wincol + total_width - 1
2730 - wp->w_popup_border[1];
2731
2732 if (line >= 0 && line < wp->w_height)
2733 screen_putchar(' ', row, scroll_col,
2734 line >= sb_thumb_top
2735 && line < sb_thumb_top + sb_thumb_height
2736 ? attr_thumb : attr_scroll);
2737 else
2738 screen_putchar(' ', row, scroll_col, popup_attr);
2739 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002740 // right border
2741 if (wp->w_popup_border[1] > 0)
2742 {
2743 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002744 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002745 }
2746 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02002747 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002748 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002749 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02002750 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
2751 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002752 }
2753
2754 if (wp->w_popup_padding[2] > 0)
2755 {
2756 // bottom padding
2757 row = wp->w_winrow + wp->w_popup_border[0]
2758 + wp->w_popup_padding[0] + wp->w_height;
2759 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02002760 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002761 }
2762
2763 if (wp->w_popup_border[2] > 0)
2764 {
2765 // bottom border
2766 row = wp->w_winrow + total_height - 1;
2767 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002768 wincol < 0 ? 0 : wincol,
2769 wincol + total_width,
2770 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002771 ? border_char[7] : border_char[2],
2772 border_char[2], border_attr[2]);
2773 if (wp->w_popup_border[1] > 0)
2774 {
2775 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002776 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002777 }
2778 }
2779
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002780 if (wp->w_popup_close == POPCLOSE_BUTTON)
2781 {
2782 // close button goes on top of anything at the top-right corner
2783 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002784 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002785 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2786 }
2787
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002788 update_popup_transparent(wp, 0);
2789
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002790 // Back to the normal zindex.
2791 screen_zindex = 0;
2792 }
2793}
2794
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002795/*
2796 * Mark references in callbacks of one popup window.
2797 */
2798 static int
2799set_ref_in_one_popup(win_T *wp, int copyID)
2800{
2801 int abort = FALSE;
2802 typval_T tv;
2803
2804 if (wp->w_close_cb.cb_partial != NULL)
2805 {
2806 tv.v_type = VAR_PARTIAL;
2807 tv.vval.v_partial = wp->w_close_cb.cb_partial;
2808 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2809 }
2810 if (wp->w_filter_cb.cb_partial != NULL)
2811 {
2812 tv.v_type = VAR_PARTIAL;
2813 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
2814 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2815 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002816 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002817 return abort;
2818}
2819
2820/*
2821 * Set reference in callbacks of popup windows.
2822 */
2823 int
2824set_ref_in_popups(int copyID)
2825{
2826 int abort = FALSE;
2827 win_T *wp;
2828 tabpage_T *tp;
2829
2830 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2831 abort = abort || set_ref_in_one_popup(wp, copyID);
2832
2833 FOR_ALL_TABPAGES(tp)
2834 {
2835 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
2836 abort = abort || set_ref_in_one_popup(wp, copyID);
2837 if (abort)
2838 break;
2839 }
2840 return abort;
2841}
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002842#endif // FEAT_TEXT_PROP