blob: 28ad48cfc850526f7ac6394d83a7a1b85ad2b887 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Implementation of popup windows. See ":help popup".
12 */
13
14#include "vim.h"
15
Bram Moolenaar79648732019-07-18 21:43:07 +020016#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020017
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020018typedef struct {
19 char *pp_name;
20 poppos_T pp_val;
21} poppos_entry_T;
22
23static poppos_entry_T poppos_entries[] = {
24 {"botleft", POPPOS_BOTLEFT},
25 {"topleft", POPPOS_TOPLEFT},
26 {"botright", POPPOS_BOTRIGHT},
27 {"topright", POPPOS_TOPRIGHT},
28 {"center", POPPOS_CENTER}
29};
30
Bram Moolenaar4d784b22019-05-25 19:51:39 +020031/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020032 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020033 * Handles "cursor+N" and "cursor-N".
34 */
35 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020036popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037{
38 dictitem_T *di;
39 char_u *val;
40 char_u *s;
41 char_u *endp;
42 int n = 0;
43
44 di = dict_find(dict, key, -1);
45 if (di == NULL)
46 return 0;
47
48 val = tv_get_string(&di->di_tv);
49 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020050 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020051
52 setcursor_mayforce(TRUE);
53 s = val + 6;
54 if (*s != NUL)
55 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020056 endp = s;
57 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
58 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020059 if (endp != NULL && *skipwhite(endp) != NUL)
60 {
61 semsg(_(e_invexpr2), val);
62 return 0;
63 }
64 }
65
66 if (STRCMP(key, "line") == 0)
67 n = screen_screenrow() + 1 + n;
68 else // "col"
69 n = screen_screencol() + 1 + n;
70
71 if (n < 1)
72 n = 1;
73 return n;
74}
75
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020076 static void
77get_pos_options(win_T *wp, dict_T *dict)
78{
79 char_u *str;
80 int nr;
Bram Moolenaar711d02c2019-06-28 04:06:50 +020081 dictitem_T *di;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020082
83 nr = popup_options_one(dict, (char_u *)"line");
84 if (nr > 0)
85 wp->w_wantline = nr;
86 nr = popup_options_one(dict, (char_u *)"col");
87 if (nr > 0)
88 wp->w_wantcol = nr;
89
Bram Moolenaar711d02c2019-06-28 04:06:50 +020090 di = dict_find(dict, (char_u *)"fixed", -1);
91 if (di != NULL)
92 wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +020093
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020094 str = dict_get_string(dict, (char_u *)"pos", FALSE);
95 if (str != NULL)
96 {
97 for (nr = 0;
98 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
99 ++nr)
100 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
101 {
102 wp->w_popup_pos = poppos_entries[nr].pp_val;
103 nr = -1;
104 break;
105 }
106 if (nr != -1)
107 semsg(_(e_invarg2), str);
108 }
109}
110
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200111 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200112set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113{
114 dictitem_T *di;
115
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200116 di = dict_find(dict, (char_u *)name, -1);
117 if (di != NULL)
118 {
119 if (di->di_tv.v_type != VAR_LIST)
120 emsg(_(e_listreq));
121 else
122 {
123 list_T *list = di->di_tv.vval.v_list;
124 listitem_T *li;
125 int i;
126 int nr;
127
128 for (i = 0; i < 4; ++i)
129 array[i] = 1;
130 if (list != NULL)
131 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
132 ++i, li = li->li_next)
133 {
134 nr = (int)tv_get_number(&li->li_tv);
135 if (nr >= 0)
136 array[i] = nr > max_val ? max_val : nr;
137 }
138 }
139 }
140}
141
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200142/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200143 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200144 */
145 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200146set_moved_values(win_T *wp)
147{
148 wp->w_popup_curwin = curwin;
149 wp->w_popup_lnum = curwin->w_cursor.lnum;
150 wp->w_popup_mincol = curwin->w_cursor.col;
151 wp->w_popup_maxcol = curwin->w_cursor.col;
152}
153
154/*
155 * Used when popup options contain "moved" with "word" or "WORD".
156 */
157 static void
158set_moved_columns(win_T *wp, int flags)
159{
160 char_u *ptr;
161 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
162
163 if (len > 0)
164 {
165 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
166 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
167 }
168}
169
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200170/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200171 * Used when popup options contain "mousemoved": set default moved values.
172 */
173 static void
174set_mousemoved_values(win_T *wp)
175{
176 wp->w_popup_mouse_row = mouse_row;
177 wp->w_popup_mouse_mincol = mouse_col;
178 wp->w_popup_mouse_maxcol = mouse_col;
179}
180
181/*
182 * Used when popup options contain "moved" with "word" or "WORD".
183 */
184 static void
185set_mousemoved_columns(win_T *wp, int flags)
186{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200187 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200188 char_u *text;
189 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200190 pos_T pos;
191 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200192
193 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200194 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200195 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200196 // convert text column to mouse column
197 pos.col = col;
198 pos.coladd = 0;
199 getvcol(textwp, &pos, &mcol, NULL, NULL);
200 wp->w_popup_mouse_mincol = mcol;
201
Bram Moolenaar10727682019-07-12 13:59:20 +0200202 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200203 getvcol(textwp, &pos, NULL, NULL, &mcol);
204 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200205 vim_free(text);
206 }
207}
208
209/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210 * Return TRUE if "row"/"col" is on the border of the popup.
211 * The values are relative to the top-left corner.
212 */
213 int
214popup_on_border(win_T *wp, int row, int col)
215{
216 return (row == 0 && wp->w_popup_border[0] > 0)
217 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
218 || (col == 0 && wp->w_popup_border[3] > 0)
219 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
220}
221
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200222/*
223 * Return TRUE if "row"/"col" is on the "X" button of the popup.
224 * The values are relative to the top-left corner.
225 * Caller should check w_popup_close is POPCLOSE_BUTTON.
226 */
227 int
228popup_on_X_button(win_T *wp, int row, int col)
229{
230 return row == 0 && col == popup_width(wp) - 1;
231}
232
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200233// Values set when dragging a popup window starts.
234static int drag_start_row;
235static int drag_start_col;
236static int drag_start_wantline;
237static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200238static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200239
240/*
241 * Mouse down on border of popup window: start dragging it.
242 * Uses mouse_col and mouse_row.
243 */
244 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200245popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200246{
247 drag_start_row = mouse_row;
248 drag_start_col = mouse_col;
249 // TODO: handle using different corner
250 if (wp->w_wantline == 0)
251 drag_start_wantline = wp->w_winrow + 1;
252 else
253 drag_start_wantline = wp->w_wantline;
254 if (wp->w_wantcol == 0)
255 drag_start_wantcol = wp->w_wincol + 1;
256 else
257 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200258
259 // Stop centering the popup
260 if (wp->w_popup_pos == POPPOS_CENTER)
261 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200262
263 drag_on_resize_handle = wp->w_popup_border[1] > 0
264 && wp->w_popup_border[2] > 0
265 && row == popup_height(wp) - 1
266 && col == popup_width(wp) - 1;
267
268 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
269 {
270 if (wp->w_popup_pos == POPPOS_TOPRIGHT
271 || wp->w_popup_pos == POPPOS_BOTRIGHT)
272 wp->w_wantcol = wp->w_wincol + 1;
273 if (wp->w_popup_pos == POPPOS_BOTLEFT)
274 wp->w_wantline = wp->w_winrow + 1;
275 wp->w_popup_pos = POPPOS_TOPLEFT;
276 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200277}
278
279/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200280 * Mouse moved while dragging a popup window: adjust the window popup position
281 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200282 */
283 void
284popup_drag(win_T *wp)
285{
286 // The popup may be closed before dragging stops.
287 if (!win_valid_popup(wp))
288 return;
289
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200290 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
291 {
292 int width_inc = mouse_col - drag_start_col;
293 int height_inc = mouse_row - drag_start_row;
294
295 if (width_inc != 0)
296 {
297 int width = wp->w_width + width_inc;
298
299 if (width < 1)
300 width = 1;
301 wp->w_minwidth = width;
302 wp->w_maxwidth = width;
303 drag_start_col = mouse_col;
304 }
305
306 if (height_inc != 0)
307 {
308 int height = wp->w_height + height_inc;
309
310 if (height < 1)
311 height = 1;
312 wp->w_minheight = height;
313 wp->w_maxheight = height;
314 drag_start_row = mouse_row;
315 }
316
317 popup_adjust_position(wp);
318 return;
319 }
320
321 if (!(wp->w_popup_flags & POPF_DRAG))
322 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200323 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
324 if (wp->w_wantline < 1)
325 wp->w_wantline = 1;
326 if (wp->w_wantline > Rows)
327 wp->w_wantline = Rows;
328 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
329 if (wp->w_wantcol < 1)
330 wp->w_wantcol = 1;
331 if (wp->w_wantcol > Columns)
332 wp->w_wantcol = Columns;
333
334 popup_adjust_position(wp);
335}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200336
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200337/*
338 * Set w_firstline to match the current "wp->w_topline".
339 */
340 void
341popup_set_firstline(win_T *wp)
342{
343 int height = wp->w_height;
344
345 wp->w_firstline = wp->w_topline;
346 popup_adjust_position(wp);
347
348 // we don't want the popup to get smaller, decrement the first line
349 // until it doesn't
350 while (wp->w_firstline > 1 && wp->w_height < height)
351 {
352 --wp->w_firstline;
353 popup_adjust_position(wp);
354 }
355}
356
357/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200358 * Return TRUE if the position is in the popup window scrollbar.
359 */
360 int
361popup_is_in_scrollbar(win_T *wp, int row, int col)
362{
363 return wp->w_has_scrollbar
364 && row >= wp->w_popup_border[0]
365 && row < popup_height(wp) - wp->w_popup_border[2]
366 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
367}
368
369
370/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200371 * Handle a click in a popup window, if it is in the scrollbar.
372 */
373 void
374popup_handle_scrollbar_click(win_T *wp, int row, int col)
375{
376 int height = popup_height(wp);
377 int old_topline = wp->w_topline;
378
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200379 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200380 {
381 if (row >= height / 2)
382 {
383 // Click in lower half, scroll down.
384 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
385 ++wp->w_topline;
386 }
387 else if (wp->w_topline > 1)
388 // click on upper half, scroll up.
389 --wp->w_topline;
390 if (wp->w_topline != old_topline)
391 {
392 popup_set_firstline(wp);
393 redraw_win_later(wp, NOT_VALID);
394 }
395 }
396}
397
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200398#if defined(FEAT_TIMERS)
399 static void
400popup_add_timeout(win_T *wp, int time)
401{
402 char_u cbbuf[50];
403 char_u *ptr = cbbuf;
404 typval_T tv;
405
406 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
407 "{_ -> popup_close(%d)}", wp->w_id);
408 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
409 {
410 wp->w_popup_timer = create_timer(time, 0);
411 wp->w_popup_timer->tr_callback = get_callback(&tv);
412 clear_tv(&tv);
413 }
414}
415#endif
416
Bram Moolenaar17627312019-06-02 19:53:44 +0200417/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200418 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200419 */
420 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200421apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200422{
Bram Moolenaarae943152019-06-16 22:54:14 +0200423 int nr;
424
425 if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
426 wp->w_minwidth = nr;
427 if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
428 wp->w_minheight = nr;
429 if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
430 wp->w_maxwidth = nr;
431 if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
432 wp->w_maxheight = nr;
433 get_pos_options(wp, d);
434}
435
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200436 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200437handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
438{
439 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
440 {
441 char_u *s = di->di_tv.vval.v_string;
442 int flags = 0;
443
444 if (STRCMP(s, "word") == 0)
445 flags = FIND_IDENT | FIND_STRING;
446 else if (STRCMP(s, "WORD") == 0)
447 flags = FIND_STRING;
448 else if (STRCMP(s, "expr") == 0)
449 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
450 else if (STRCMP(s, "any") != 0)
451 semsg(_(e_invarg2), s);
452 if (flags != 0)
453 {
454 if (mousemoved)
455 set_mousemoved_columns(wp, flags);
456 else
457 set_moved_columns(wp, flags);
458 }
459 }
460 else if (di->di_tv.v_type == VAR_LIST
461 && di->di_tv.vval.v_list != NULL
462 && di->di_tv.vval.v_list->lv_len == 2)
463 {
464 list_T *l = di->di_tv.vval.v_list;
465 int mincol = tv_get_number(&l->lv_first->li_tv);
466 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
467
468 if (mousemoved)
469 {
470 wp->w_popup_mouse_mincol = mincol;
471 wp->w_popup_mouse_maxcol = maxcol;
472 }
473 else
474 {
475 wp->w_popup_mincol = mincol;
476 wp->w_popup_maxcol = maxcol;
477 }
478 }
479 else
480 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
481}
482
483 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200484check_highlight(dict_T *dict, char *name, char_u **pval)
485{
486 dictitem_T *di;
487 char_u *str;
488
489 di = dict_find(dict, (char_u *)name, -1);
490 if (di != NULL)
491 {
492 if (di->di_tv.v_type != VAR_STRING)
493 semsg(_(e_invargval), name);
494 else
495 {
496 str = tv_get_string(&di->di_tv);
497 if (*str != NUL)
498 *pval = vim_strsave(str);
499 }
500 }
501}
502
Bram Moolenaarae943152019-06-16 22:54:14 +0200503/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200504 * Scroll to show the line with the cursor. This assumes lines don't wrap.
505 */
506 static void
507popup_show_curline(win_T *wp)
508{
509 if (wp->w_cursor.lnum < wp->w_topline)
510 wp->w_topline = wp->w_cursor.lnum;
511 else if (wp->w_cursor.lnum >= wp->w_botline)
512 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200513
514 // Don't use "firstline" now.
515 wp->w_firstline = 0;
516}
517
518/*
519 * Get the sign group name for window "wp".
520 * Returns a pointer to a static buffer, overwritten on the next call.
521 */
522 static char_u *
523popup_get_sign_name(win_T *wp)
524{
525 static char buf[30];
526
527 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
528 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200529}
530
531/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200532 * Highlight the line with the cursor.
533 * Also scrolls the text to put the cursor line in view.
534 */
535 static void
536popup_highlight_curline(win_T *wp)
537{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200538 int sign_id = 0;
539 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200540
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200541 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200542
543 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
544 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200545 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200546
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200547 if (!sign_exists_by_name(sign_name))
548 {
549 char *linehl = "PopupSelected";
550
551 if (syn_name2id((char_u *)linehl) == 0)
552 linehl = "PmenuSel";
553 sign_define_by_name(sign_name, NULL,
554 (char_u *)linehl, NULL, NULL);
555 }
556
557 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
558 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
559 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200560 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200561 else
562 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200563}
564
565/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200566 * Shared between popup_create() and f_popup_setoptions().
567 */
568 static void
569apply_general_options(win_T *wp, dict_T *dict)
570{
571 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200572 int nr;
573 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200574
Bram Moolenaarae943152019-06-16 22:54:14 +0200575 // TODO: flip
576
577 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200578 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200579 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
580 if (wp->w_firstline < 1)
581 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200582
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200583 di = dict_find(dict, (char_u *)"scrollbar", -1);
584 if (di != NULL)
585 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
586
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200587 str = dict_get_string(dict, (char_u *)"title", FALSE);
588 if (str != NULL)
589 {
590 vim_free(wp->w_popup_title);
591 wp->w_popup_title = vim_strsave(str);
592 }
593
Bram Moolenaar402502d2019-05-30 22:07:36 +0200594 di = dict_find(dict, (char_u *)"wrap", -1);
595 if (di != NULL)
596 {
597 nr = dict_get_number(dict, (char_u *)"wrap");
598 wp->w_p_wrap = nr != 0;
599 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200600
Bram Moolenaara42d9452019-06-15 21:46:30 +0200601 di = dict_find(dict, (char_u *)"drag", -1);
602 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200603 {
604 nr = dict_get_number(dict, (char_u *)"drag");
605 if (nr)
606 wp->w_popup_flags |= POPF_DRAG;
607 else
608 wp->w_popup_flags &= ~POPF_DRAG;
609 }
610
611 di = dict_find(dict, (char_u *)"resize", -1);
612 if (di != NULL)
613 {
614 nr = dict_get_number(dict, (char_u *)"resize");
615 if (nr)
616 wp->w_popup_flags |= POPF_RESIZE;
617 else
618 wp->w_popup_flags &= ~POPF_RESIZE;
619 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200620
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200621 di = dict_find(dict, (char_u *)"close", -1);
622 if (di != NULL)
623 {
624 int ok = TRUE;
625
626 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
627 {
628 char_u *s = di->di_tv.vval.v_string;
629
630 if (STRCMP(s, "none") == 0)
631 wp->w_popup_close = POPCLOSE_NONE;
632 else if (STRCMP(s, "button") == 0)
633 wp->w_popup_close = POPCLOSE_BUTTON;
634 else if (STRCMP(s, "click") == 0)
635 wp->w_popup_close = POPCLOSE_CLICK;
636 else
637 ok = FALSE;
638 }
639 else
640 ok = FALSE;
641 if (!ok)
642 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
643 }
644
Bram Moolenaarae943152019-06-16 22:54:14 +0200645 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
646 if (str != NULL)
647 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
648 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200649
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200650 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
651 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200652 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
653 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200654
Bram Moolenaar790498b2019-06-01 22:15:29 +0200655 di = dict_find(dict, (char_u *)"borderhighlight", -1);
656 if (di != NULL)
657 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200658 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200659 emsg(_(e_listreq));
660 else
661 {
662 list_T *list = di->di_tv.vval.v_list;
663 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200664 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200665
Bram Moolenaar403d0902019-07-17 21:37:32 +0200666 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
667 ++i, li = li->li_next)
668 {
669 str = tv_get_string(&li->li_tv);
670 if (*str != NUL)
671 wp->w_border_highlight[i] = vim_strsave(str);
672 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200673 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
674 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200675 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200676 vim_strsave(wp->w_border_highlight[0]);
677 }
678 }
679
Bram Moolenaar790498b2019-06-01 22:15:29 +0200680 di = dict_find(dict, (char_u *)"borderchars", -1);
681 if (di != NULL)
682 {
683 if (di->di_tv.v_type != VAR_LIST)
684 emsg(_(e_listreq));
685 else
686 {
687 list_T *list = di->di_tv.vval.v_list;
688 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200689 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200690
691 if (list != NULL)
692 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
693 ++i, li = li->li_next)
694 {
695 str = tv_get_string(&li->li_tv);
696 if (*str != NUL)
697 wp->w_border_char[i] = mb_ptr2char(str);
698 }
699 if (list->lv_len == 1)
700 for (i = 1; i < 8; ++i)
701 wp->w_border_char[i] = wp->w_border_char[0];
702 if (list->lv_len == 2)
703 {
704 for (i = 4; i < 8; ++i)
705 wp->w_border_char[i] = wp->w_border_char[1];
706 for (i = 1; i < 4; ++i)
707 wp->w_border_char[i] = wp->w_border_char[0];
708 }
709 }
710 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200711
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200712 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
713 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
714
Bram Moolenaarae943152019-06-16 22:54:14 +0200715 di = dict_find(dict, (char_u *)"zindex", -1);
716 if (di != NULL)
717 {
718 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
719 if (wp->w_zindex < 1)
720 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
721 if (wp->w_zindex > 32000)
722 wp->w_zindex = 32000;
723 }
724
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200725 di = dict_find(dict, (char_u *)"mask", -1);
726 if (di != NULL)
727 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200728 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200729
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200730 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200731 {
732 listitem_T *li;
733
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200734 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200735 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
736 li = li->li_next)
737 {
738 if (li->li_tv.v_type != VAR_LIST
739 || li->li_tv.vval.v_list == NULL
740 || li->li_tv.vval.v_list->lv_len != 4)
741 {
742 ok = FALSE;
743 break;
744 }
745 }
746 }
747 if (ok)
748 {
749 wp->w_popup_mask = di->di_tv.vval.v_list;
750 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200751 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200752 }
753 else
754 semsg(_(e_invargval), "mask");
755 }
756
Bram Moolenaarae943152019-06-16 22:54:14 +0200757#if defined(FEAT_TIMERS)
758 // Add timer to close the popup after some time.
759 nr = dict_get_number(dict, (char_u *)"time");
760 if (nr > 0)
761 popup_add_timeout(wp, nr);
762#endif
763
Bram Moolenaar3397f742019-06-02 18:40:06 +0200764 di = dict_find(dict, (char_u *)"moved", -1);
765 if (di != NULL)
766 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200767 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200768 handle_moved_argument(wp, di, FALSE);
769 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200770
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200771 di = dict_find(dict, (char_u *)"mousemoved", -1);
772 if (di != NULL)
773 {
774 set_mousemoved_values(wp);
775 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200776 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200777
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200778 di = dict_find(dict, (char_u *)"cursorline", -1);
779 if (di != NULL)
780 {
781 if (di->di_tv.v_type == VAR_NUMBER)
782 {
783 if (di->di_tv.vval.v_number != 0)
784 wp->w_popup_flags |= POPF_CURSORLINE;
785 else
786 wp->w_popup_flags &= ~POPF_CURSORLINE;
787 }
788 else
789 semsg(_(e_invargval), "cursorline");
790 }
791
Bram Moolenaarae943152019-06-16 22:54:14 +0200792 di = dict_find(dict, (char_u *)"filter", -1);
793 if (di != NULL)
794 {
795 callback_T callback = get_callback(&di->di_tv);
796
797 if (callback.cb_name != NULL)
798 {
799 free_callback(&wp->w_filter_cb);
800 set_callback(&wp->w_filter_cb, &callback);
801 }
802 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200803 di = dict_find(dict, (char_u *)"mapping", -1);
804 if (di != NULL)
805 {
806 nr = dict_get_number(dict, (char_u *)"mapping");
807 if (nr)
808 wp->w_popup_flags |= POPF_MAPPING;
809 else
810 wp->w_popup_flags &= ~POPF_MAPPING;
811 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200812
813 di = dict_find(dict, (char_u *)"callback", -1);
814 if (di != NULL)
815 {
816 callback_T callback = get_callback(&di->di_tv);
817
818 if (callback.cb_name != NULL)
819 {
820 free_callback(&wp->w_close_cb);
821 set_callback(&wp->w_close_cb, &callback);
822 }
823 }
824}
825
826/*
827 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200828 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200829 */
830 static void
831apply_options(win_T *wp, dict_T *dict)
832{
833 int nr;
834
835 apply_move_options(wp, dict);
836 apply_general_options(wp, dict);
837
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200838 nr = dict_get_number(dict, (char_u *)"hidden");
839 if (nr > 0)
840 {
841 wp->w_popup_flags |= POPF_HIDDEN;
842 --wp->w_buffer->b_nwindows;
843 }
844
Bram Moolenaar33796b32019-06-08 16:01:13 +0200845 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200846 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200847}
848
849/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200850 * Add lines to the popup from a list of strings.
851 */
852 static void
853add_popup_strings(buf_T *buf, list_T *l)
854{
855 listitem_T *li;
856 linenr_T lnum = 0;
857 char_u *p;
858
859 for (li = l->lv_first; li != NULL; li = li->li_next)
860 if (li->li_tv.v_type == VAR_STRING)
861 {
862 p = li->li_tv.vval.v_string;
863 ml_append_buf(buf, lnum++,
864 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
865 }
866}
867
868/*
869 * Add lines to the popup from a list of dictionaries.
870 */
871 static void
872add_popup_dicts(buf_T *buf, list_T *l)
873{
874 listitem_T *li;
875 listitem_T *pli;
876 linenr_T lnum = 0;
877 char_u *p;
878 dict_T *dict;
879
880 // first add the text lines
881 for (li = l->lv_first; li != NULL; li = li->li_next)
882 {
883 if (li->li_tv.v_type != VAR_DICT)
884 {
885 emsg(_(e_dictreq));
886 return;
887 }
888 dict = li->li_tv.vval.v_dict;
889 p = dict == NULL ? NULL
890 : dict_get_string(dict, (char_u *)"text", FALSE);
891 ml_append_buf(buf, lnum++,
892 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
893 }
894
895 // add the text properties
896 lnum = 1;
897 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
898 {
899 dictitem_T *di;
900 list_T *plist;
901
902 dict = li->li_tv.vval.v_dict;
903 di = dict_find(dict, (char_u *)"props", -1);
904 if (di != NULL)
905 {
906 if (di->di_tv.v_type != VAR_LIST)
907 {
908 emsg(_(e_listreq));
909 return;
910 }
911 plist = di->di_tv.vval.v_list;
912 if (plist != NULL)
913 {
914 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
915 {
916 if (pli->li_tv.v_type != VAR_DICT)
917 {
918 emsg(_(e_dictreq));
919 return;
920 }
921 dict = pli->li_tv.vval.v_dict;
922 if (dict != NULL)
923 {
924 int col = dict_get_number(dict, (char_u *)"col");
925
926 prop_add_common( lnum, col, dict, buf, NULL);
927 }
928 }
929 }
930 }
931 }
932}
933
934/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200935 * Get the padding plus border at the top, adjusted to 1 if there is a title.
936 */
937 static int
938popup_top_extra(win_T *wp)
939{
940 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
941
942 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
943 return 1;
944 return extra;
945}
946
947/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200948 * Return the height of popup window "wp", including border and padding.
949 */
950 int
951popup_height(win_T *wp)
952{
953 return wp->w_height
954 + popup_top_extra(wp)
955 + wp->w_popup_padding[2] + wp->w_popup_border[2];
956}
957
958/*
959 * Return the width of popup window "wp", including border, padding and
960 * scrollbar.
961 */
962 int
963popup_width(win_T *wp)
964{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200965 // w_leftcol is how many columns of the core are left of the screen
966 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200967 return wp->w_width + wp->w_leftcol
968 + wp->w_popup_padding[3] + wp->w_popup_border[3]
969 + wp->w_popup_padding[1] + wp->w_popup_border[1]
970 + wp->w_has_scrollbar
971 + wp->w_popup_rightoff;
972}
973
974/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200975 * Adjust the position and size of the popup to fit on the screen.
976 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200977 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200978popup_adjust_position(win_T *wp)
979{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200980 linenr_T lnum;
981 int wrapped = 0;
982 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200983 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200984 int center_vert = FALSE;
985 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200986 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200987 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200988 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
989 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
990 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
991 int extra_height = top_extra + bot_extra;
992 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200993 int org_winrow = wp->w_winrow;
994 int org_wincol = wp->w_wincol;
995 int org_width = wp->w_width;
996 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +0200997 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200998 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200999 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001000
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001001 wp->w_winrow = 0;
1002 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001003 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001004 wp->w_popup_leftoff = 0;
1005 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001006 if (wp->w_popup_pos == POPPOS_CENTER)
1007 {
1008 // center after computing the size
1009 center_vert = TRUE;
1010 center_hor = TRUE;
1011 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001012 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001013 {
1014 if (wp->w_wantline == 0)
1015 center_vert = TRUE;
1016 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1017 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1018 {
1019 wp->w_winrow = wp->w_wantline - 1;
1020 if (wp->w_winrow >= Rows)
1021 wp->w_winrow = Rows - 1;
1022 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001023
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001024 if (wp->w_wantcol == 0)
1025 center_hor = TRUE;
1026 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1027 || wp->w_popup_pos == POPPOS_BOTLEFT)
1028 {
1029 wp->w_wincol = wp->w_wantcol - 1;
1030 if (wp->w_wincol >= Columns - 3)
1031 wp->w_wincol = Columns - 3;
1032 }
1033 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001034
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001035 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001036 // When left aligned use the space available, but shift to the left when we
1037 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001038 maxspace = Columns - wp->w_wincol - left_extra;
1039 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001040 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001041 {
1042 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001043 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001044 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001045
Bram Moolenaar8d241042019-06-12 23:40:01 +02001046 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001047 if (wp->w_firstline != 0)
1048 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001049 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1050 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1051
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001052 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001053 // Use a minimum width of one, so that something shows when there is no
1054 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001055 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001056 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001057 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001058 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001059 int len;
1060 int w_width = wp->w_width;
1061
1062 // Count Tabs for what they are worth and compute the length based on
1063 // the maximum width (matters when 'showbreak' is set).
1064 if (wp->w_width < maxwidth)
1065 wp->w_width = maxwidth;
1066 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001067 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001068 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001069
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001070 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001071 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001072 while (len > maxwidth)
1073 {
1074 ++wrapped;
1075 len -= maxwidth;
1076 wp->w_width = maxwidth;
1077 }
1078 }
1079 else if (len > maxwidth
1080 && allow_adjust_left
1081 && (wp->w_popup_pos == POPPOS_TOPLEFT
1082 || wp->w_popup_pos == POPPOS_BOTLEFT))
1083 {
1084 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001085 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001086
Bram Moolenaar51c31312019-06-15 22:27:23 +02001087 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001088 {
1089 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001090
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001091 len -= truncate_shift;
1092 shift_by -= truncate_shift;
1093 }
1094
1095 wp->w_wincol -= shift_by;
1096 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001097 wp->w_width = maxwidth;
1098 }
1099 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001100 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001101 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001102 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1103 wp->w_width = wp->w_maxwidth;
1104 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001105 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001106 if (wp->w_maxheight > 0
1107 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001108 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001109 }
1110
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001111 wp->w_has_scrollbar = wp->w_want_scrollbar
1112 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001113 if (wp->w_has_scrollbar)
1114 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001115
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001116 minwidth = wp->w_minwidth;
1117 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1118 {
1119 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1120
1121 if (minwidth < title_len)
1122 minwidth = title_len;
1123 }
1124
1125 if (minwidth > 0 && wp->w_width < minwidth)
1126 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001127 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001128 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001129 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001130 // some columns cut off on the right
1131 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001132 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001133 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001134 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001135 {
1136 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1137 if (wp->w_wincol < 0)
1138 wp->w_wincol = 0;
1139 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001140 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1141 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1142 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001143 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1144
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001145 // Right aligned: move to the right if needed.
1146 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001147 if (leftoff >= 0)
1148 wp->w_wincol = leftoff;
1149 else if (wp->w_popup_fixed)
1150 {
1151 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001152 // columns when displaying the window, minus left border and
1153 // padding.
1154 if (-leftoff > left_extra)
1155 wp->w_leftcol = -leftoff - left_extra;
1156 wp->w_width -= wp->w_leftcol;
1157 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001158 if (wp->w_width < 0)
1159 wp->w_width = 0;
1160 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001161 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001162
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001163 if (wp->w_p_wrap || (!wp->w_popup_fixed
1164 && (wp->w_popup_pos == POPPOS_TOPLEFT
1165 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001166 {
1167 int want_col = 0;
1168
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001169 // try to show the right border and any scrollbar
1170 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001171 if (want_col > 0 && wp->w_wincol > 0
1172 && wp->w_wincol + want_col >= Columns)
1173 {
1174 wp->w_wincol = Columns - want_col;
1175 if (wp->w_wincol < 0)
1176 wp->w_wincol = 0;
1177 }
1178 }
1179
Bram Moolenaar8d241042019-06-12 23:40:01 +02001180 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1181 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001182 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1183 wp->w_height = wp->w_minheight;
1184 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1185 wp->w_height = wp->w_maxheight;
1186 if (wp->w_height > Rows - wp->w_winrow)
1187 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001188
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001189 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001190 {
1191 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1192 if (wp->w_winrow < 0)
1193 wp->w_winrow = 0;
1194 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001195 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1196 || wp->w_popup_pos == POPPOS_BOTLEFT)
1197 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001198 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001199 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001200 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001201 else
1202 // not enough space, make top aligned
1203 wp->w_winrow = wp->w_wantline + 1;
1204 }
1205
Bram Moolenaar17146962019-05-30 00:12:11 +02001206 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001207
1208 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001209 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001210 if (org_winrow != wp->w_winrow
1211 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001212 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001213 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001214 || org_width != wp->w_width
1215 || org_height != wp->w_height)
1216 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001217 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001218 redraw_win_later(wp, NOT_VALID);
1219 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1220 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001221 popup_mask_refresh = TRUE;
1222 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001223}
1224
Bram Moolenaar17627312019-06-02 19:53:44 +02001225typedef enum
1226{
1227 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001228 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001229 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001230 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001231 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001232 TYPE_MENU,
1233 TYPE_PREVIEW
Bram Moolenaar17627312019-06-02 19:53:44 +02001234} create_type_T;
1235
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001236/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001237 * Make "buf" empty and set the contents to "text".
1238 * Used by popup_create() and popup_settext().
1239 */
1240 static void
1241popup_set_buffer_text(buf_T *buf, typval_T text)
1242{
1243 int lnum;
1244
1245 // Clear the buffer, then replace the lines.
1246 curbuf = buf;
1247 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1248 ml_delete(lnum, FALSE);
1249 curbuf = curwin->w_buffer;
1250
1251 // Add text to the buffer.
1252 if (text.v_type == VAR_STRING)
1253 {
1254 // just a string
1255 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1256 }
1257 else
1258 {
1259 list_T *l = text.vval.v_list;
1260
1261 if (l->lv_len > 0)
1262 {
1263 if (l->lv_first->li_tv.v_type == VAR_STRING)
1264 // list of strings
1265 add_popup_strings(buf, l);
1266 else
1267 // list of dictionaries
1268 add_popup_dicts(buf, l);
1269 }
1270 }
1271
1272 // delete the line that was in the empty buffer
1273 curbuf = buf;
1274 ml_delete(buf->b_ml.ml_line_count, FALSE);
1275 curbuf = curwin->w_buffer;
1276}
1277
1278/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001279 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1280 * not NULL.
1281 * Return FAIL if the parsing fails.
1282 */
1283 int
1284parse_previewpopup(win_T *wp)
1285{
1286 char_u *p;
1287
1288 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1289 {
1290 char_u *e, *dig;
1291 char_u *s = p;
1292 int x;
1293
1294 e = vim_strchr(p, ':');
1295 if (e == NULL || e[1] == NUL)
1296 return FAIL;
1297
1298 p = vim_strchr(e, ',');
1299 if (p == NULL)
1300 p = e + STRLEN(e);
1301 dig = e + 1;
1302 x = getdigits(&dig);
1303 if (dig != p)
1304 return FAIL;
1305
1306 if (STRNCMP(s, "height:", 7) == 0)
1307 {
1308 if (wp != NULL)
1309 {
1310 wp->w_minheight = x;
1311 wp->w_maxheight = x;
1312 }
1313 }
1314 else if (STRNCMP(s, "width:", 6) == 0)
1315 {
1316 if (wp != NULL)
1317 {
1318 wp->w_minwidth = x;
1319 wp->w_maxwidth = x;
1320 }
1321 }
1322 else
1323 return FAIL;
1324 }
1325 return OK;
1326}
1327
1328/*
1329 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001330 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001331 */
1332 void
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001333popup_set_wantpos(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001334{
1335 setcursor_mayforce(TRUE);
1336 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1337 if (wp->w_wantline == 0) // cursor in first line
1338 {
1339 wp->w_wantline = 2;
1340 wp->w_popup_pos = POPPOS_TOPLEFT;
1341 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001342
Bram Moolenaar79648732019-07-18 21:43:07 +02001343 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001344 if (wp->w_wantcol > Columns - width)
1345 {
1346 wp->w_wantcol = Columns - width;
1347 if (wp->w_wantcol < 1)
1348 wp->w_wantcol = 1;
1349 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001350 popup_adjust_position(wp);
1351}
1352
1353/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001354 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001355 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001356 * etc.
1357 * When creating a preview window popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001358 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001359 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001360popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001361{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001362 win_T *wp;
1363 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001364 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001365 int new_buffer;
1366 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001367 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001368 int nr;
1369 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001370
Bram Moolenaar79648732019-07-18 21:43:07 +02001371 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001372 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001373 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001374 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001375 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001376 buf = buflist_findnr( argvars[0].vval.v_number);
1377 if (buf == NULL)
1378 {
1379 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1380 return NULL;
1381 }
1382 }
1383 else if (!(argvars[0].v_type == VAR_STRING
1384 && argvars[0].vval.v_string != NULL)
1385 && !(argvars[0].v_type == VAR_LIST
1386 && argvars[0].vval.v_list != NULL))
1387 {
1388 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001389 return NULL;
1390 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001391 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001392 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001393 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001394 return NULL;
1395 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001396 d = argvars[1].vval.v_dict;
1397 }
1398
1399 if (d != NULL)
1400 {
1401 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1402 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1403 else if (type == TYPE_NOTIFICATION)
1404 tabnr = -1; // notifications are global by default
1405 else
1406 tabnr = 0;
1407 if (tabnr > 0)
1408 {
1409 tp = find_tabpage(tabnr);
1410 if (tp == NULL)
1411 {
1412 semsg(_("E997: Tabpage not found: %d"), tabnr);
1413 return NULL;
1414 }
1415 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001416 }
1417
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001418 // Create the window and buffer.
1419 wp = win_alloc_popup_win();
1420 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001421 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001422 if (rettv != NULL)
1423 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001424 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001425 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001426
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001427 if (buf != NULL)
1428 {
1429 // use existing buffer
1430 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001431 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001432 buffer_ensure_loaded(buf);
1433 }
1434 else
1435 {
1436 // create a new buffer associated with the popup
1437 new_buffer = TRUE;
1438 buf = buflist_new(NULL, NULL, (linenr_T)0,
1439 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1440 if (buf == NULL)
1441 return NULL;
1442 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001443
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001444 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001445
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001446 set_local_options_default(wp);
1447 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001448 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001449 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001450 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001451 buf->b_p_ul = -1; // no undo
1452 buf->b_p_swf = FALSE; // no swap file
1453 buf->b_p_bl = FALSE; // unlisted buffer
1454 buf->b_locked = TRUE;
1455 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001456
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001457 // Avoid that 'buftype' is reset when this buffer is entered.
1458 buf->b_p_initialized = TRUE;
1459 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001460
Bram Moolenaara3fce622019-06-20 02:31:49 +02001461 if (tp != NULL)
1462 {
1463 // popup on specified tab page
1464 wp->w_next = tp->tp_first_popupwin;
1465 tp->tp_first_popupwin = wp;
1466 }
1467 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001468 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001469 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001470 wp->w_next = curtab->tp_first_popupwin;
1471 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001472 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001473 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001474 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001475 win_T *prev = first_popupwin;
1476
1477 // Global popup: add at the end, so that it gets displayed on top of
1478 // older ones with the same zindex. Matters for notifications.
1479 if (first_popupwin == NULL)
1480 first_popupwin = wp;
1481 else
1482 {
1483 while (prev->w_next != NULL)
1484 prev = prev->w_next;
1485 prev->w_next = wp;
1486 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001487 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001488
Bram Moolenaar79648732019-07-18 21:43:07 +02001489 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001490 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001491
Bram Moolenaar79648732019-07-18 21:43:07 +02001492 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001493 {
1494 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001495 }
1496 if (type == TYPE_ATCURSOR)
1497 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001498 popup_set_wantpos(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001499 set_moved_values(wp);
1500 set_moved_columns(wp, FIND_STRING);
1501 }
1502
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503 if (type == TYPE_BEVAL)
1504 {
1505 wp->w_popup_pos = POPPOS_BOTLEFT;
1506
1507 // by default use the mouse position
1508 wp->w_wantline = mouse_row;
1509 if (wp->w_wantline <= 0) // mouse on first line
1510 {
1511 wp->w_wantline = 2;
1512 wp->w_popup_pos = POPPOS_TOPLEFT;
1513 }
1514 wp->w_wantcol = mouse_col + 1;
1515 set_mousemoved_values(wp);
1516 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1517 }
1518
Bram Moolenaar33796b32019-06-08 16:01:13 +02001519 // set default values
1520 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001521 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001522
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001523 if (type == TYPE_NOTIFICATION)
1524 {
1525 win_T *twp, *nextwin;
1526 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001527
1528 // Try to not overlap with another global popup. Guess we need 3
1529 // more screen lines than buffer lines.
1530 wp->w_wantline = 1;
1531 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1532 {
1533 nextwin = twp->w_next;
1534 if (twp != wp
1535 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1536 && twp->w_winrow <= wp->w_wantline - 1 + height
1537 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1538 {
1539 // move to below this popup and restart the loop to check for
1540 // overlap with other popups
1541 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1542 nextwin = first_popupwin;
1543 }
1544 }
1545 if (wp->w_wantline + height > Rows)
1546 {
1547 // can't avoid overlap, put on top in the hope that message goes
1548 // away soon.
1549 wp->w_wantline = 1;
1550 }
1551
1552 wp->w_wantcol = 10;
1553 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001554 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001555 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001556 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001557 for (i = 0; i < 4; ++i)
1558 wp->w_popup_border[i] = 1;
1559 wp->w_popup_padding[1] = 1;
1560 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001561
1562 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001563 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001564 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1565 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001566 }
1567
Bram Moolenaara730e552019-06-16 19:05:31 +02001568 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001569 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001570 wp->w_popup_pos = POPPOS_CENTER;
1571 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001572 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001573 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001574 for (i = 0; i < 4; ++i)
1575 {
1576 wp->w_popup_border[i] = 1;
Bram Moolenaar03464132019-07-14 16:28:13 +02001577 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001578 }
1579 }
1580
Bram Moolenaara730e552019-06-16 19:05:31 +02001581 if (type == TYPE_MENU)
1582 {
1583 typval_T tv;
1584 callback_T callback;
1585
1586 tv.v_type = VAR_STRING;
1587 tv.vval.v_string = (char_u *)"popup_filter_menu";
1588 callback = get_callback(&tv);
1589 if (callback.cb_name != NULL)
1590 set_callback(&wp->w_filter_cb, &callback);
1591
1592 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001593 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001594 }
1595
Bram Moolenaar79648732019-07-18 21:43:07 +02001596 if (type == TYPE_PREVIEW)
1597 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001598 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001599 wp->w_popup_close = POPCLOSE_BUTTON;
1600 for (i = 0; i < 4; ++i)
1601 wp->w_popup_border[i] = 1;
1602 parse_previewpopup(wp);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001603 popup_set_wantpos(wp, wp->w_minwidth);
Bram Moolenaar79648732019-07-18 21:43:07 +02001604 }
1605
Bram Moolenaarae943152019-06-16 22:54:14 +02001606 for (i = 0; i < 4; ++i)
1607 VIM_CLEAR(wp->w_border_highlight[i]);
1608 for (i = 0; i < 8; ++i)
1609 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001610 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001611 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001612
Bram Moolenaar79648732019-07-18 21:43:07 +02001613 if (d != NULL)
1614 // Deal with options.
1615 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001616
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001617#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001618 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1619 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001620#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001621
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001622 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001623
1624 wp->w_vsep_width = 0;
1625
1626 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001627 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001628
1629 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001630}
1631
1632/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001633 * popup_clear()
1634 */
1635 void
1636f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1637{
1638 close_all_popups();
1639}
1640
1641/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001642 * popup_create({text}, {options})
1643 */
1644 void
1645f_popup_create(typval_T *argvars, typval_T *rettv)
1646{
Bram Moolenaar17627312019-06-02 19:53:44 +02001647 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001648}
1649
1650/*
1651 * popup_atcursor({text}, {options})
1652 */
1653 void
1654f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1655{
Bram Moolenaar17627312019-06-02 19:53:44 +02001656 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001657}
1658
1659/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001660 * popup_beval({text}, {options})
1661 */
1662 void
1663f_popup_beval(typval_T *argvars, typval_T *rettv)
1664{
1665 popup_create(argvars, rettv, TYPE_BEVAL);
1666}
1667
1668/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001669 * Invoke the close callback for window "wp" with value "result".
1670 * Careful: The callback may make "wp" invalid!
1671 */
1672 static void
1673invoke_popup_callback(win_T *wp, typval_T *result)
1674{
1675 typval_T rettv;
1676 int dummy;
1677 typval_T argv[3];
1678
1679 argv[0].v_type = VAR_NUMBER;
1680 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1681
1682 if (result != NULL && result->v_type != VAR_UNKNOWN)
1683 copy_tv(result, &argv[1]);
1684 else
1685 {
1686 argv[1].v_type = VAR_NUMBER;
1687 argv[1].vval.v_number = 0;
1688 }
1689
1690 argv[2].v_type = VAR_UNKNOWN;
1691
1692 call_callback(&wp->w_close_cb, -1,
1693 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
1694 if (result != NULL)
1695 clear_tv(&argv[1]);
1696 clear_tv(&rettv);
1697}
1698
1699/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001700 * Close popup "wp" and invoke any close callback for it.
1701 */
1702 static void
1703popup_close_and_callback(win_T *wp, typval_T *arg)
1704{
1705 int id = wp->w_id;
1706
1707 if (wp->w_close_cb.cb_name != NULL)
1708 // Careful: This may make "wp" invalid.
1709 invoke_popup_callback(wp, arg);
1710
1711 popup_close(id);
1712}
1713
1714/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001715 * Close popup "wp" because of a mouse click.
1716 */
1717 void
1718popup_close_for_mouse_click(win_T *wp)
1719{
1720 typval_T res;
1721
1722 res.v_type = VAR_NUMBER;
1723 res.vval.v_number = -2;
1724 popup_close_and_callback(wp, &res);
1725}
1726
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001727 static void
1728check_mouse_moved(win_T *wp, win_T *mouse_wp)
1729{
1730 // Close the popup when all if these are true:
1731 // - the mouse is not on this popup
1732 // - "mousemoved" was used
1733 // - the mouse is no longer on the same screen row or the mouse column is
1734 // outside of the relevant text
1735 if (wp != mouse_wp
1736 && wp->w_popup_mouse_row != 0
1737 && (wp->w_popup_mouse_row != mouse_row
1738 || mouse_col < wp->w_popup_mouse_mincol
1739 || mouse_col > wp->w_popup_mouse_maxcol))
1740 {
1741 typval_T res;
1742
1743 res.v_type = VAR_NUMBER;
1744 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001745 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001746 popup_close_and_callback(wp, &res);
1747 }
1748}
1749
1750/*
1751 * Called when the mouse moved: may close a popup with "mousemoved".
1752 */
1753 void
1754popup_handle_mouse_moved(void)
1755{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001756 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001757 win_T *mouse_wp;
1758 int row = mouse_row;
1759 int col = mouse_col;
1760
1761 // find the window where the mouse is in
1762 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1763
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001764 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1765 {
1766 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001767 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001768 }
1769 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1770 {
1771 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001772 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001773 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001774}
1775
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001776/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001777 * In a filter: check if the typed key is a mouse event that is used for
1778 * dragging the popup.
1779 */
1780 static void
1781filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1782{
1783 int row = mouse_row;
1784 int col = mouse_col;
1785
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001786 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001787 && is_mouse_key(c)
1788 && (wp == popup_dragwin
1789 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1790 // do not consume the key, allow for dragging the popup
1791 rettv->vval.v_number = 0;
1792}
1793
Bram Moolenaara730e552019-06-16 19:05:31 +02001794/*
1795 * popup_filter_menu({text}, {options})
1796 */
1797 void
1798f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1799{
1800 int id = tv_get_number(&argvars[0]);
1801 win_T *wp = win_id2wp(id);
1802 char_u *key = tv_get_string(&argvars[1]);
1803 typval_T res;
1804 int c;
1805 linenr_T old_lnum;
1806
1807 // If the popup has been closed do not consume the key.
1808 if (wp == NULL)
1809 return;
1810
1811 c = *key;
1812 if (c == K_SPECIAL && key[1] != NUL)
1813 c = TO_SPECIAL(key[1], key[2]);
1814
1815 // consume all keys until done
1816 rettv->vval.v_number = 1;
1817 res.v_type = VAR_NUMBER;
1818
1819 old_lnum = wp->w_cursor.lnum;
1820 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1821 --wp->w_cursor.lnum;
1822 if ((c == 'j' || c == 'J' || c == K_DOWN)
1823 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1824 ++wp->w_cursor.lnum;
1825 if (old_lnum != wp->w_cursor.lnum)
1826 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001827 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001828 return;
1829 }
1830
1831 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1832 {
1833 // Cancelled, invoke callback with -1
1834 res.vval.v_number = -1;
1835 popup_close_and_callback(wp, &res);
1836 return;
1837 }
1838 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1839 {
1840 // Invoke callback with current index.
1841 res.vval.v_number = wp->w_cursor.lnum;
1842 popup_close_and_callback(wp, &res);
1843 return;
1844 }
1845
1846 filter_handle_drag(wp, c, rettv);
1847}
1848
1849/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001850 * popup_filter_yesno({text}, {options})
1851 */
1852 void
1853f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1854{
1855 int id = tv_get_number(&argvars[0]);
1856 win_T *wp = win_id2wp(id);
1857 char_u *key = tv_get_string(&argvars[1]);
1858 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001859 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001860
1861 // If the popup has been closed don't consume the key.
1862 if (wp == NULL)
1863 return;
1864
Bram Moolenaara730e552019-06-16 19:05:31 +02001865 c = *key;
1866 if (c == K_SPECIAL && key[1] != NUL)
1867 c = TO_SPECIAL(key[1], key[2]);
1868
Bram Moolenaara42d9452019-06-15 21:46:30 +02001869 // consume all keys until done
1870 rettv->vval.v_number = 1;
1871
Bram Moolenaara730e552019-06-16 19:05:31 +02001872 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001873 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001874 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001875 res.vval.v_number = 0;
1876 else
1877 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001878 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001879 return;
1880 }
1881
1882 // Invoke callback
1883 res.v_type = VAR_NUMBER;
1884 popup_close_and_callback(wp, &res);
1885}
1886
1887/*
1888 * popup_dialog({text}, {options})
1889 */
1890 void
1891f_popup_dialog(typval_T *argvars, typval_T *rettv)
1892{
1893 popup_create(argvars, rettv, TYPE_DIALOG);
1894}
1895
1896/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001897 * popup_menu({text}, {options})
1898 */
1899 void
1900f_popup_menu(typval_T *argvars, typval_T *rettv)
1901{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001902 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001903}
1904
1905/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001906 * popup_notification({text}, {options})
1907 */
1908 void
1909f_popup_notification(typval_T *argvars, typval_T *rettv)
1910{
1911 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1912}
1913
1914/*
1915 * Find the popup window with window-ID "id".
1916 * If the popup window does not exist NULL is returned.
1917 * If the window is not a popup window, and error message is given.
1918 */
1919 static win_T *
1920find_popup_win(int id)
1921{
1922 win_T *wp = win_id2wp(id);
1923
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001924 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001925 {
1926 semsg(_("E993: window %d is not a popup window"), id);
1927 return NULL;
1928 }
1929 return wp;
1930}
1931
1932/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001933 * popup_close({id})
1934 */
1935 void
1936f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1937{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001938 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001939 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001940
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001941 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001942 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001943}
1944
1945/*
1946 * popup_hide({id})
1947 */
1948 void
1949f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1950{
1951 int id = (int)tv_get_number(argvars);
1952 win_T *wp = find_popup_win(id);
1953
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001954 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001955 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001956 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001957 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001958 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001959 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001960 }
1961}
1962
1963/*
1964 * popup_show({id})
1965 */
1966 void
1967f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1968{
1969 int id = (int)tv_get_number(argvars);
1970 win_T *wp = find_popup_win(id);
1971
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001972 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001973 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001974 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001975 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001976 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001977 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001978 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001979}
1980
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001981/*
1982 * popup_settext({id}, {text})
1983 */
1984 void
1985f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1986{
1987 int id = (int)tv_get_number(&argvars[0]);
1988 win_T *wp = find_popup_win(id);
1989
1990 if (wp != NULL)
1991 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001992 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1993 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1994 else
1995 {
1996 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001997 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001998 popup_adjust_position(wp);
1999 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002000 }
2001}
2002
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002003 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002004popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002005{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002006 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002007 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002008 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002009 clear_cmdline = TRUE;
2010 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002011
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002012 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002013 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002014}
2015
Bram Moolenaarec583842019-05-26 14:11:23 +02002016/*
2017 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002018 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002019 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002020 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002021popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002022{
2023 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002024 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002025 win_T *prev = NULL;
2026
Bram Moolenaarec583842019-05-26 14:11:23 +02002027 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002028 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002029 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002030 {
2031 if (prev == NULL)
2032 first_popupwin = wp->w_next;
2033 else
2034 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002035 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002036 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002037 }
2038
Bram Moolenaarec583842019-05-26 14:11:23 +02002039 // go through tab-local popups
2040 FOR_ALL_TABPAGES(tp)
2041 popup_close_tabpage(tp, id);
2042}
2043
2044/*
2045 * Close a popup window with Window-id "id" in tabpage "tp".
2046 */
2047 void
2048popup_close_tabpage(tabpage_T *tp, int id)
2049{
2050 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002051 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002052 win_T *prev = NULL;
2053
Bram Moolenaarec583842019-05-26 14:11:23 +02002054 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2055 if (wp->w_id == id)
2056 {
2057 if (prev == NULL)
2058 *root = wp->w_next;
2059 else
2060 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002061 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002062 return;
2063 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002064}
2065
2066 void
2067close_all_popups(void)
2068{
2069 while (first_popupwin != NULL)
2070 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002071 while (curtab->tp_first_popupwin != NULL)
2072 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002073}
2074
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002075/*
2076 * popup_move({id}, {options})
2077 */
2078 void
2079f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2080{
Bram Moolenaarae943152019-06-16 22:54:14 +02002081 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002082 int id = (int)tv_get_number(argvars);
2083 win_T *wp = find_popup_win(id);
2084
2085 if (wp == NULL)
2086 return; // invalid {id}
2087
2088 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2089 {
2090 emsg(_(e_dictreq));
2091 return;
2092 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002093 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002094
Bram Moolenaarae943152019-06-16 22:54:14 +02002095 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002096
2097 if (wp->w_winrow + wp->w_height >= cmdline_row)
2098 clear_cmdline = TRUE;
2099 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002100}
2101
Bram Moolenaarbc133542019-05-29 20:26:46 +02002102/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002103 * popup_setoptions({id}, {options})
2104 */
2105 void
2106f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2107{
2108 dict_T *dict;
2109 int id = (int)tv_get_number(argvars);
2110 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002111 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002112
2113 if (wp == NULL)
2114 return; // invalid {id}
2115
2116 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2117 {
2118 emsg(_(e_dictreq));
2119 return;
2120 }
2121 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002122 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002123
2124 apply_move_options(wp, dict);
2125 apply_general_options(wp, dict);
2126
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002127 if (old_firstline != wp->w_firstline)
2128 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002129 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002130 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002131 popup_adjust_position(wp);
2132}
2133
2134/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002135 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002136 */
2137 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002138f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002139{
2140 dict_T *dict;
2141 int id = (int)tv_get_number(argvars);
2142 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002143 int top_extra;
2144 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002145
2146 if (rettv_dict_alloc(rettv) == OK)
2147 {
2148 if (wp == NULL)
2149 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002150 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002151 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2152
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002153 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002154 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002155 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002156
Bram Moolenaarbc133542019-05-29 20:26:46 +02002157 dict_add_number(dict, "line", wp->w_winrow + 1);
2158 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002159 dict_add_number(dict, "width", wp->w_width + left_extra
2160 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2161 dict_add_number(dict, "height", wp->w_height + top_extra
2162 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002163
2164 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2165 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2166 dict_add_number(dict, "core_width", wp->w_width);
2167 dict_add_number(dict, "core_height", wp->w_height);
2168
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002169 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002170 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002171 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002172 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002173
2174 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002175 }
2176}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002177/*
2178 * popup_locate({row}, {col})
2179 */
2180 void
2181f_popup_locate(typval_T *argvars, typval_T *rettv)
2182{
2183 int row = tv_get_number(&argvars[0]) - 1;
2184 int col = tv_get_number(&argvars[1]) - 1;
2185 win_T *wp;
2186
2187 wp = mouse_find_win(&row, &col, FIND_POPUP);
2188 if (WIN_IS_POPUP(wp))
2189 rettv->vval.v_number = wp->w_id;
2190}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002191
2192/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002193 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2194 */
2195 static void
2196get_padding_border(dict_T *dict, int *array, char *name)
2197{
2198 list_T *list;
2199 int i;
2200
2201 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2202 return;
2203
2204 list = list_alloc();
2205 if (list != NULL)
2206 {
2207 dict_add_list(dict, name, list);
2208 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2209 for (i = 0; i < 4; ++i)
2210 list_append_number(list, array[i]);
2211 }
2212}
2213
2214/*
2215 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2216 */
2217 static void
2218get_borderhighlight(dict_T *dict, win_T *wp)
2219{
2220 list_T *list;
2221 int i;
2222
2223 for (i = 0; i < 4; ++i)
2224 if (wp->w_border_highlight[i] != NULL)
2225 break;
2226 if (i == 4)
2227 return;
2228
2229 list = list_alloc();
2230 if (list != NULL)
2231 {
2232 dict_add_list(dict, "borderhighlight", list);
2233 for (i = 0; i < 4; ++i)
2234 list_append_string(list, wp->w_border_highlight[i], -1);
2235 }
2236}
2237
2238/*
2239 * For popup_getoptions(): add a "borderchars" entry to "dict".
2240 */
2241 static void
2242get_borderchars(dict_T *dict, win_T *wp)
2243{
2244 list_T *list;
2245 int i;
2246 char_u buf[NUMBUFLEN];
2247 int len;
2248
2249 for (i = 0; i < 8; ++i)
2250 if (wp->w_border_char[i] != 0)
2251 break;
2252 if (i == 8)
2253 return;
2254
2255 list = list_alloc();
2256 if (list != NULL)
2257 {
2258 dict_add_list(dict, "borderchars", list);
2259 for (i = 0; i < 8; ++i)
2260 {
2261 len = mb_char2bytes(wp->w_border_char[i], buf);
2262 list_append_string(list, buf, len);
2263 }
2264 }
2265}
2266
2267/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002268 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002269 */
2270 static void
2271get_moved_list(dict_T *dict, win_T *wp)
2272{
2273 list_T *list;
2274
2275 list = list_alloc();
2276 if (list != NULL)
2277 {
2278 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002279 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002280 list_append_number(list, wp->w_popup_mincol);
2281 list_append_number(list, wp->w_popup_maxcol);
2282 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002283 list = list_alloc();
2284 if (list != NULL)
2285 {
2286 dict_add_list(dict, "mousemoved", list);
2287 list_append_number(list, wp->w_popup_mouse_row);
2288 list_append_number(list, wp->w_popup_mouse_mincol);
2289 list_append_number(list, wp->w_popup_mouse_maxcol);
2290 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002291}
2292
2293/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002294 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002295 */
2296 void
2297f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2298{
2299 dict_T *dict;
2300 int id = (int)tv_get_number(argvars);
2301 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002302 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002303 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002304
2305 if (rettv_dict_alloc(rettv) == OK)
2306 {
2307 if (wp == NULL)
2308 return;
2309
2310 dict = rettv->vval.v_dict;
2311 dict_add_number(dict, "line", wp->w_wantline);
2312 dict_add_number(dict, "col", wp->w_wantcol);
2313 dict_add_number(dict, "minwidth", wp->w_minwidth);
2314 dict_add_number(dict, "minheight", wp->w_minheight);
2315 dict_add_number(dict, "maxheight", wp->w_maxheight);
2316 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002317 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002318 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002319 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002320 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002321 dict_add_string(dict, "title", wp->w_popup_title);
2322 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002323 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
2324 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002325 dict_add_number(dict, "cursorline",
2326 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002327 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002328 if (wp->w_scrollbar_highlight != NULL)
2329 dict_add_string(dict, "scrollbarhighlight",
2330 wp->w_scrollbar_highlight);
2331 if (wp->w_thumb_highlight != NULL)
2332 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002333
Bram Moolenaara3fce622019-06-20 02:31:49 +02002334 // find the tabpage that holds this popup
2335 i = 1;
2336 FOR_ALL_TABPAGES(tp)
2337 {
2338 win_T *p;
2339
2340 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2341 if (p->w_id == id)
2342 break;
2343 if (p != NULL)
2344 break;
2345 ++i;
2346 }
2347 if (tp == NULL)
2348 i = -1; // must be global
2349 else if (tp == curtab)
2350 i = 0;
2351 dict_add_number(dict, "tabpage", i);
2352
Bram Moolenaarae943152019-06-16 22:54:14 +02002353 get_padding_border(dict, wp->w_popup_padding, "padding");
2354 get_padding_border(dict, wp->w_popup_border, "border");
2355 get_borderhighlight(dict, wp);
2356 get_borderchars(dict, wp);
2357 get_moved_list(dict, wp);
2358
2359 if (wp->w_filter_cb.cb_name != NULL)
2360 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2361 if (wp->w_close_cb.cb_name != NULL)
2362 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002363
2364 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2365 ++i)
2366 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2367 {
2368 dict_add_string(dict, "pos",
2369 (char_u *)poppos_entries[i].pp_name);
2370 break;
2371 }
2372
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002373 dict_add_string(dict, "close", (char_u *)(
2374 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2375 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2376
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002377# if defined(FEAT_TIMERS)
2378 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2379 ? (long)wp->w_popup_timer->tr_interval : 0L);
2380# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002381 }
2382}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002383
2384 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002385error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002386{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002387 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002388 {
2389 emsg(_("E994: Not allowed in a popup window"));
2390 return TRUE;
2391 }
2392 return FALSE;
2393}
2394
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002395/*
2396 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002397 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002398 */
2399 void
2400popup_reset_handled()
2401{
2402 win_T *wp;
2403
2404 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2405 wp->w_popup_flags &= ~POPF_HANDLED;
2406 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2407 wp->w_popup_flags &= ~POPF_HANDLED;
2408}
2409
2410/*
2411 * Find the next visible popup where POPF_HANDLED is not set.
2412 * Must have called popup_reset_handled() first.
2413 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2414 * popup with the highest zindex.
2415 */
2416 win_T *
2417find_next_popup(int lowest)
2418{
2419 win_T *wp;
2420 win_T *found_wp;
2421 int found_zindex;
2422
2423 found_zindex = lowest ? INT_MAX : 0;
2424 found_wp = NULL;
2425 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2426 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2427 && (lowest ? wp->w_zindex < found_zindex
2428 : wp->w_zindex > found_zindex))
2429 {
2430 found_zindex = wp->w_zindex;
2431 found_wp = wp;
2432 }
2433 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2434 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2435 && (lowest ? wp->w_zindex < found_zindex
2436 : wp->w_zindex > found_zindex))
2437 {
2438 found_zindex = wp->w_zindex;
2439 found_wp = wp;
2440 }
2441
2442 if (found_wp != NULL)
2443 found_wp->w_popup_flags |= POPF_HANDLED;
2444 return found_wp;
2445}
2446
2447/*
2448 * Invoke the filter callback for window "wp" with typed character "c".
2449 * Uses the global "mod_mask" for modifiers.
2450 * Returns the return value of the filter.
2451 * Careful: The filter may make "wp" invalid!
2452 */
2453 static int
2454invoke_popup_filter(win_T *wp, int c)
2455{
2456 int res;
2457 typval_T rettv;
2458 int dummy;
2459 typval_T argv[3];
2460 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002461 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002462
Bram Moolenaara42d9452019-06-15 21:46:30 +02002463 // Emergency exit: CTRL-C closes the popup.
2464 if (c == Ctrl_C)
2465 {
2466 rettv.v_type = VAR_NUMBER;
2467 rettv.vval.v_number = -1;
2468 popup_close_and_callback(wp, &rettv);
2469 return 1;
2470 }
2471
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002472 argv[0].v_type = VAR_NUMBER;
2473 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2474
2475 // Convert the number to a string, so that the function can use:
2476 // if a:c == "\<F2>"
2477 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2478 argv[1].v_type = VAR_STRING;
2479 argv[1].vval.v_string = vim_strsave(buf);
2480
2481 argv[2].v_type = VAR_UNKNOWN;
2482
Bram Moolenaara42d9452019-06-15 21:46:30 +02002483 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002484 call_callback(&wp->w_filter_cb, -1,
2485 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002486 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002487 popup_highlight_curline(wp);
2488
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002489 res = tv_get_number(&rettv);
2490 vim_free(argv[1].vval.v_string);
2491 clear_tv(&rettv);
2492 return res;
2493}
2494
2495/*
2496 * Called when "c" was typed: invoke popup filter callbacks.
2497 * Returns TRUE when the character was consumed,
2498 */
2499 int
2500popup_do_filter(int c)
2501{
2502 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002503 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002504
2505 popup_reset_handled();
2506
2507 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2508 if (wp->w_filter_cb.cb_name != NULL)
2509 res = invoke_popup_filter(wp, c);
2510
2511 return res;
2512}
2513
Bram Moolenaar3397f742019-06-02 18:40:06 +02002514/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002515 * Return TRUE if there is a popup visible with a filter callback and the
2516 * "mapping" property off.
2517 */
2518 int
2519popup_no_mapping(void)
2520{
2521 int round;
2522 win_T *wp;
2523
2524 for (round = 1; round <= 2; ++round)
2525 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2526 wp != NULL; wp = wp->w_next)
2527 if (wp->w_filter_cb.cb_name != NULL
2528 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2529 return TRUE;
2530 return FALSE;
2531}
2532
2533/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002534 * Called when the cursor moved: check if any popup needs to be closed if the
2535 * cursor moved far enough.
2536 */
2537 void
2538popup_check_cursor_pos()
2539{
2540 win_T *wp;
2541 typval_T tv;
2542
2543 popup_reset_handled();
2544 while ((wp = find_next_popup(TRUE)) != NULL)
2545 if (wp->w_popup_curwin != NULL
2546 && (curwin != wp->w_popup_curwin
2547 || curwin->w_cursor.lnum != wp->w_popup_lnum
2548 || curwin->w_cursor.col < wp->w_popup_mincol
2549 || curwin->w_cursor.col > wp->w_popup_maxcol))
2550 {
2551 tv.v_type = VAR_NUMBER;
2552 tv.vval.v_number = -1;
2553 popup_close_and_callback(wp, &tv);
2554 }
2555}
2556
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002557/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002558 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002559 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002560 static void
2561popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002562{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002563 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002564 char_u *cells;
2565 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002566
2567 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002568 return;
2569 if (wp->w_popup_mask_cells != NULL
2570 && wp->w_popup_mask_height == height
2571 && wp->w_popup_mask_width == width)
2572 return; // cache is still valid
2573
2574 vim_free(wp->w_popup_mask_cells);
2575 wp->w_popup_mask_cells = alloc_clear(width * height);
2576 if (wp->w_popup_mask_cells == NULL)
2577 return;
2578 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002579
2580 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2581 {
2582 int cols, cole;
2583 int lines, linee;
2584
2585 li = lio->li_tv.vval.v_list->lv_first;
2586 cols = tv_get_number(&li->li_tv);
2587 if (cols < 0)
2588 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002589 li = li->li_next;
2590 cole = tv_get_number(&li->li_tv);
2591 if (cole < 0)
2592 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002593 li = li->li_next;
2594 lines = tv_get_number(&li->li_tv);
2595 if (lines < 0)
2596 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002597 li = li->li_next;
2598 linee = tv_get_number(&li->li_tv);
2599 if (linee < 0)
2600 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002601
2602 for (row = lines - 1; row < linee && row < height; ++row)
2603 for (col = cols - 1; col < cole && col < width; ++col)
2604 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002605 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002606}
2607
2608/*
2609 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2610 * "col" and "line" are screen coordinates.
2611 */
2612 static int
2613popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2614{
2615 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2616 int line = screenline - wp->w_winrow;
2617
2618 return col >= 0 && col < width
2619 && line >= 0 && line < height
2620 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002621}
2622
2623/*
2624 * Set flags in popup_transparent[] for window "wp" to "val".
2625 */
2626 static void
2627update_popup_transparent(win_T *wp, int val)
2628{
2629 if (wp->w_popup_mask != NULL)
2630 {
2631 int width = popup_width(wp);
2632 int height = popup_height(wp);
2633 listitem_T *lio, *li;
2634 int cols, cole;
2635 int lines, linee;
2636 int col, line;
2637
2638 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2639 {
2640 li = lio->li_tv.vval.v_list->lv_first;
2641 cols = tv_get_number(&li->li_tv);
2642 if (cols < 0)
2643 cols = width + cols + 1;
2644 li = li->li_next;
2645 cole = tv_get_number(&li->li_tv);
2646 if (cole < 0)
2647 cole = width + cole + 1;
2648 li = li->li_next;
2649 lines = tv_get_number(&li->li_tv);
2650 if (lines < 0)
2651 lines = height + lines + 1;
2652 li = li->li_next;
2653 linee = tv_get_number(&li->li_tv);
2654 if (linee < 0)
2655 linee = height + linee + 1;
2656
2657 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002658 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002659 if (cols < 0)
2660 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002661 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002662 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002663 if (lines < 0)
2664 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002665 for (line = lines; line < linee
2666 && line + wp->w_winrow < screen_Rows; ++line)
2667 for (col = cols; col < cole
2668 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002669 popup_transparent[(line + wp->w_winrow) * screen_Columns
2670 + col + wp->w_wincol] = val;
2671 }
2672 }
2673}
2674
2675/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002676 * Update "popup_mask" if needed.
2677 * Also recomputes the popup size and positions.
2678 * Also updates "popup_visible".
2679 * Also marks window lines for redrawing.
2680 */
2681 void
2682may_update_popup_mask(int type)
2683{
2684 win_T *wp;
2685 short *mask;
2686 int line, col;
2687 int redraw_all = FALSE;
2688
2689 // Need to recompute when switching tabs.
2690 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2691 // (such as the screen size) must have changed.
2692 if (popup_mask_tab != curtab || type >= NOT_VALID)
2693 {
2694 popup_mask_refresh = TRUE;
2695 redraw_all = TRUE;
2696 }
2697 if (!popup_mask_refresh)
2698 {
2699 // Check if any buffer has changed.
2700 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2701 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2702 popup_mask_refresh = TRUE;
2703 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2704 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2705 popup_mask_refresh = TRUE;
2706 if (!popup_mask_refresh)
2707 return;
2708 }
2709
2710 // Need to update the mask, something has changed.
2711 popup_mask_refresh = FALSE;
2712 popup_mask_tab = curtab;
2713 popup_visible = FALSE;
2714
2715 // If redrawing everything, just update "popup_mask".
2716 // If redrawing only what is needed, update "popup_mask_next" and then
2717 // compare with "popup_mask" to see what changed.
2718 if (type >= SOME_VALID)
2719 mask = popup_mask;
2720 else
2721 mask = popup_mask_next;
2722 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2723
2724 // Find the window with the lowest zindex that hasn't been handled yet,
2725 // so that the window with a higher zindex overwrites the value in
2726 // popup_mask.
2727 popup_reset_handled();
2728 while ((wp = find_next_popup(TRUE)) != NULL)
2729 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002730 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002731 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002732
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002733 popup_visible = TRUE;
2734
2735 // Recompute the position if the text changed.
2736 if (redraw_all
2737 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2738 popup_adjust_position(wp);
2739
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002740 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002741 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002742 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002743 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002744 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002745 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002746 col < wp->w_wincol + width - wp->w_popup_leftoff
2747 && col < screen_Columns; ++col)
2748 if (wp->w_popup_mask_cells == NULL
2749 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002750 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002751 }
2752
2753 // Only check which lines are to be updated if not already
2754 // updating all lines.
2755 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002756 {
2757 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2758 win_T *prev_wp = NULL;
2759
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002760 for (line = 0; line < screen_Rows; ++line)
2761 {
2762 int col_done = 0;
2763
2764 for (col = 0; col < screen_Columns; ++col)
2765 {
2766 int off = line * screen_Columns + col;
2767
2768 if (popup_mask[off] != popup_mask_next[off])
2769 {
2770 popup_mask[off] = popup_mask_next[off];
2771
2772 if (line >= cmdline_row)
2773 {
2774 // the command line needs to be cleared if text below
2775 // the popup is now visible.
2776 if (!msg_scrolled && popup_mask_next[off] == 0)
2777 clear_cmdline = TRUE;
2778 }
2779 else if (col >= col_done)
2780 {
2781 linenr_T lnum;
2782 int line_cp = line;
2783 int col_cp = col;
2784
2785 // The screen position "line" / "col" needs to be
2786 // redrawn. Figure out what window that is and update
2787 // w_redraw_top and w_redr_bot. Only needs to be done
2788 // once for each window line.
2789 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2790 if (wp != NULL)
2791 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002792 if (wp != prev_wp)
2793 {
2794 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2795 prev_wp = wp;
2796 }
2797
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002798 if (line_cp >= wp->w_height)
2799 // In (or below) status line
2800 wp->w_redr_status = TRUE;
2801 // compute the position in the buffer line from the
2802 // position on the screen
2803 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002804 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002805 // past bottom
2806 wp->w_redr_status = TRUE;
2807 else
2808 redrawWinline(wp, lnum);
2809
2810 // This line is going to be redrawn, no need to
2811 // check until the right side of the window.
2812 col_done = wp->w_wincol + wp->w_width - 1;
2813 }
2814 }
2815 }
2816 }
2817 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002818
2819 vim_free(plines_cache);
2820 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002821}
2822
2823/*
2824 * Return a string of "len" spaces in IObuff.
2825 */
2826 static char_u *
2827get_spaces(int len)
2828{
2829 vim_memset(IObuff, ' ', (size_t)len);
2830 IObuff[len] = NUL;
2831 return IObuff;
2832}
2833
2834/*
2835 * Update popup windows. They are drawn on top of normal windows.
2836 * "win_update" is called for each popup window, lowest zindex first.
2837 */
2838 void
2839update_popups(void (*win_update)(win_T *wp))
2840{
2841 win_T *wp;
2842 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002843 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002844 int total_width;
2845 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002846 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002847 int popup_attr;
2848 int border_attr[4];
2849 int border_char[8];
2850 char_u buf[MB_MAXBYTES];
2851 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002852 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002853 int padcol = 0;
2854 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002855 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002856 int sb_thumb_top = 0;
2857 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002858 int attr_scroll = 0;
2859 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002860
2861 // Find the window with the lowest zindex that hasn't been updated yet,
2862 // so that the window with a higher zindex is drawn later, thus goes on
2863 // top.
2864 popup_reset_handled();
2865 while ((wp = find_next_popup(TRUE)) != NULL)
2866 {
2867 // This drawing uses the zindex of the popup window, so that it's on
2868 // top of the text but doesn't draw when another popup with higher
2869 // zindex is on top of the character.
2870 screen_zindex = wp->w_zindex;
2871
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002872 // Set flags in popup_transparent[] for masked cells.
2873 update_popup_transparent(wp, 1);
2874
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002875 // adjust w_winrow and w_wincol for border and padding, since
2876 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002877 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002878 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2879 - wp->w_popup_leftoff;
2880 if (wp->w_wincol + left_extra < 0)
2881 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002882 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002883 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002884
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002885 // Draw the popup text, unless it's off screen.
2886 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2887 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002888
2889 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002890 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002891
Bram Moolenaard529ba52019-07-02 23:13:53 +02002892 total_width = popup_width(wp);
2893 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002894 popup_attr = get_wcr_attr(wp);
2895
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002896 if (wp->w_winrow + total_height > cmdline_row)
2897 wp->w_popup_flags |= POPF_ON_CMDLINE;
2898 else
2899 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2900
2901
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002902 // We can only use these line drawing characters when 'encoding' is
2903 // "utf-8" and 'ambiwidth' is "single".
2904 if (enc_utf8 && *p_ambw == 's')
2905 {
2906 border_char[0] = border_char[2] = 0x2550;
2907 border_char[1] = border_char[3] = 0x2551;
2908 border_char[4] = 0x2554;
2909 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002910 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
2911 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002912 border_char[7] = 0x255a;
2913 }
2914 else
2915 {
2916 border_char[0] = border_char[2] = '-';
2917 border_char[1] = border_char[3] = '|';
2918 for (i = 4; i < 8; ++i)
2919 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002920 if (wp->w_popup_flags & POPF_RESIZE)
2921 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002922 }
2923 for (i = 0; i < 8; ++i)
2924 if (wp->w_border_char[i] != 0)
2925 border_char[i] = wp->w_border_char[i];
2926
2927 for (i = 0; i < 4; ++i)
2928 {
2929 border_attr[i] = popup_attr;
2930 if (wp->w_border_highlight[i] != NULL)
2931 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2932 }
2933
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002934 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002935 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002936 if (wp->w_popup_border[0] > 0)
2937 {
2938 // top border
2939 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002940 wincol < 0 ? 0 : wincol, wincol + total_width,
2941 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002942 ? border_char[4] : border_char[0],
2943 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002944 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002945 {
2946 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2947 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002948 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002949 }
2950 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002951 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2952 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002953
Bram Moolenaard529ba52019-07-02 23:13:53 +02002954 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2955 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002956 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002957 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2958 - wp->w_has_scrollbar;
2959 if (padcol < 0)
2960 {
2961 padwidth += padcol;
2962 padcol = 0;
2963 }
2964 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002965 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002966 {
2967 // top padding
2968 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002969 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002970 ' ', ' ', popup_attr);
2971 }
2972
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002973 // Title goes on top of border or padding.
2974 if (wp->w_popup_title != NULL)
2975 screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
2976 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
2977
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002978 // Compute scrollbar thumb position and size.
2979 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002980 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002981 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2982
Bram Moolenaar68acb412019-06-26 03:40:36 +02002983 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2984 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002985 if (sb_thumb_height == 0)
2986 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002987 if (linecount <= wp->w_height)
2988 // it just fits, avoid divide by zero
2989 sb_thumb_top = 0;
2990 else
2991 sb_thumb_top = (wp->w_topline - 1
2992 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002993 * (wp->w_height - sb_thumb_height)
2994 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002995 if (wp->w_scrollbar_highlight != NULL)
2996 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
2997 else
2998 attr_scroll = highlight_attr[HLF_PSB];
2999 if (wp->w_thumb_highlight != NULL)
3000 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3001 else
3002 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003003 }
3004
3005 for (i = wp->w_popup_border[0];
3006 i < total_height - wp->w_popup_border[2]; ++i)
3007 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003008 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003009 // left and right padding only needed next to the body
3010 int do_padding =
3011 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3012 && i < total_height - wp->w_popup_border[2]
3013 - wp->w_popup_padding[2];
3014
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003015 row = wp->w_winrow + i;
3016
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003017 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003018 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003019 {
3020 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003021 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003022 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003023 if (do_padding && wp->w_popup_padding[3] > 0)
3024 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003025 int col = wincol + wp->w_popup_border[3];
3026
Bram Moolenaard529ba52019-07-02 23:13:53 +02003027 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003028 pad_left = wp->w_popup_padding[3];
3029 if (col < 0)
3030 {
3031 pad_left += col;
3032 col = 0;
3033 }
3034 if (pad_left > 0)
3035 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3036 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003037 // scrollbar
3038 if (wp->w_has_scrollbar)
3039 {
3040 int line = i - top_off;
3041 int scroll_col = wp->w_wincol + total_width - 1
3042 - wp->w_popup_border[1];
3043
3044 if (line >= 0 && line < wp->w_height)
3045 screen_putchar(' ', row, scroll_col,
3046 line >= sb_thumb_top
3047 && line < sb_thumb_top + sb_thumb_height
3048 ? attr_thumb : attr_scroll);
3049 else
3050 screen_putchar(' ', row, scroll_col, popup_attr);
3051 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003052 // right border
3053 if (wp->w_popup_border[1] > 0)
3054 {
3055 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003056 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003057 }
3058 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003059 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003060 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003061 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003062 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3063 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003064 }
3065
3066 if (wp->w_popup_padding[2] > 0)
3067 {
3068 // bottom padding
3069 row = wp->w_winrow + wp->w_popup_border[0]
3070 + wp->w_popup_padding[0] + wp->w_height;
3071 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003072 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003073 }
3074
3075 if (wp->w_popup_border[2] > 0)
3076 {
3077 // bottom border
3078 row = wp->w_winrow + total_height - 1;
3079 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003080 wincol < 0 ? 0 : wincol,
3081 wincol + total_width,
3082 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003083 ? border_char[7] : border_char[2],
3084 border_char[2], border_attr[2]);
3085 if (wp->w_popup_border[1] > 0)
3086 {
3087 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003088 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003089 }
3090 }
3091
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003092 if (wp->w_popup_close == POPCLOSE_BUTTON)
3093 {
3094 // close button goes on top of anything at the top-right corner
3095 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003096 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003097 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3098 }
3099
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003100 update_popup_transparent(wp, 0);
3101
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003102 // Back to the normal zindex.
3103 screen_zindex = 0;
3104 }
3105}
3106
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003107/*
3108 * Mark references in callbacks of one popup window.
3109 */
3110 static int
3111set_ref_in_one_popup(win_T *wp, int copyID)
3112{
3113 int abort = FALSE;
3114 typval_T tv;
3115
3116 if (wp->w_close_cb.cb_partial != NULL)
3117 {
3118 tv.v_type = VAR_PARTIAL;
3119 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3120 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3121 }
3122 if (wp->w_filter_cb.cb_partial != NULL)
3123 {
3124 tv.v_type = VAR_PARTIAL;
3125 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3126 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3127 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003128 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003129 return abort;
3130}
3131
3132/*
3133 * Set reference in callbacks of popup windows.
3134 */
3135 int
3136set_ref_in_popups(int copyID)
3137{
3138 int abort = FALSE;
3139 win_T *wp;
3140 tabpage_T *tp;
3141
3142 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3143 abort = abort || set_ref_in_one_popup(wp, copyID);
3144
3145 FOR_ALL_TABPAGES(tp)
3146 {
3147 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3148 abort = abort || set_ref_in_one_popup(wp, copyID);
3149 if (abort)
3150 break;
3151 }
3152 return abort;
3153}
Bram Moolenaar79648732019-07-18 21:43:07 +02003154
3155/*
3156 * Find an existing popup used as the preview window, in the current tab page.
3157 * Return NULL if not found.
3158 */
3159 win_T *
3160popup_find_preview_window(void)
3161{
3162 win_T *wp;
3163
3164 // Preview window popup is always local to tab page.
3165 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3166 if (wp->w_p_pvw)
3167 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003168 return NULL;
3169}
3170
3171 void
3172f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3173{
3174 win_T *wp = popup_find_preview_window();
3175
3176 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003177}
3178
3179 int
3180popup_is_popup(win_T *wp)
3181{
3182 return wp->w_popup_flags != 0;
3183}
3184
3185/*
3186 * Create a popup to be used as the preview window.
3187 * NOTE: this makes the popup the current window, so that the file can be
3188 * edited. However, it must not remain to be the current window, the caller
3189 * must make sure of that.
3190 */
3191 int
3192popup_create_preview_window(void)
3193{
3194 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
3195
3196 if (wp == NULL)
3197 return FAIL;
3198 wp->w_p_pvw = TRUE;
3199
3200 // Set the width to a reasonable value, so that w_topline can be computed.
3201 if (wp->w_minwidth > 0)
3202 wp->w_width = wp->w_minwidth;
3203 else if (wp->w_maxwidth > 0)
3204 wp->w_width = wp->w_maxwidth;
3205 else
3206 wp->w_width = curwin->w_width;
3207
3208 // Will switch to another buffer soon, dummy one can be wiped.
3209 wp->w_buffer->b_locked = FALSE;
3210
3211 win_enter(wp, FALSE);
3212 return OK;
3213}
3214
3215 void
3216popup_close_preview()
3217{
3218 win_T *wp = popup_find_preview_window();
3219
3220 if (wp != NULL)
3221 {
3222 typval_T res;
3223
3224 res.v_type = VAR_NUMBER;
3225 res.vval.v_number = -1;
3226 popup_close_and_callback(wp, &res);
3227 }
3228}
3229
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003230/*
3231 * Set the title of the popup window to the file name.
3232 */
3233 void
3234popup_set_title(win_T *wp)
3235{
3236 if (wp->w_buffer->b_fname != NULL)
3237 {
3238 char_u dirname[MAXPATHL];
3239 size_t len;
3240
3241 mch_dirname(dirname, MAXPATHL);
3242 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3243
3244 vim_free(wp->w_popup_title);
3245 len = STRLEN(wp->w_buffer->b_fname) + 3;
3246 wp->w_popup_title = alloc((int)len);
3247 if (wp->w_popup_title != NULL)
3248 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3249 wp->w_buffer->b_fname);
3250 redraw_win_later(wp, VALID);
3251 }
3252}
3253
3254/*
3255 * If there is a preview window, update the title.
3256 * Used after changing directory.
3257 */
3258 void
3259popup_update_preview_title(void)
3260{
3261 win_T *wp = popup_find_preview_window();
3262
3263 if (wp != NULL)
3264 popup_set_title(wp);
3265}
3266
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003267#endif // FEAT_TEXT_PROP