blob: 9d801ea4bc92d8551769a968940b15abdf1df121 [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;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001676 typval_T argv[3];
1677
1678 argv[0].v_type = VAR_NUMBER;
1679 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1680
1681 if (result != NULL && result->v_type != VAR_UNKNOWN)
1682 copy_tv(result, &argv[1]);
1683 else
1684 {
1685 argv[1].v_type = VAR_NUMBER;
1686 argv[1].vval.v_number = 0;
1687 }
1688
1689 argv[2].v_type = VAR_UNKNOWN;
1690
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001691 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001692 if (result != NULL)
1693 clear_tv(&argv[1]);
1694 clear_tv(&rettv);
1695}
1696
1697/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001698 * Close popup "wp" and invoke any close callback for it.
1699 */
1700 static void
1701popup_close_and_callback(win_T *wp, typval_T *arg)
1702{
1703 int id = wp->w_id;
1704
1705 if (wp->w_close_cb.cb_name != NULL)
1706 // Careful: This may make "wp" invalid.
1707 invoke_popup_callback(wp, arg);
1708
1709 popup_close(id);
1710}
1711
1712/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001713 * Close popup "wp" because of a mouse click.
1714 */
1715 void
1716popup_close_for_mouse_click(win_T *wp)
1717{
1718 typval_T res;
1719
1720 res.v_type = VAR_NUMBER;
1721 res.vval.v_number = -2;
1722 popup_close_and_callback(wp, &res);
1723}
1724
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001725 static void
1726check_mouse_moved(win_T *wp, win_T *mouse_wp)
1727{
1728 // Close the popup when all if these are true:
1729 // - the mouse is not on this popup
1730 // - "mousemoved" was used
1731 // - the mouse is no longer on the same screen row or the mouse column is
1732 // outside of the relevant text
1733 if (wp != mouse_wp
1734 && wp->w_popup_mouse_row != 0
1735 && (wp->w_popup_mouse_row != mouse_row
1736 || mouse_col < wp->w_popup_mouse_mincol
1737 || mouse_col > wp->w_popup_mouse_maxcol))
1738 {
1739 typval_T res;
1740
1741 res.v_type = VAR_NUMBER;
1742 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001743 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001744 popup_close_and_callback(wp, &res);
1745 }
1746}
1747
1748/*
1749 * Called when the mouse moved: may close a popup with "mousemoved".
1750 */
1751 void
1752popup_handle_mouse_moved(void)
1753{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001754 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001755 win_T *mouse_wp;
1756 int row = mouse_row;
1757 int col = mouse_col;
1758
1759 // find the window where the mouse is in
1760 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1761
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001762 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1763 {
1764 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001765 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001766 }
1767 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1768 {
1769 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001770 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001771 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001772}
1773
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001774/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001775 * In a filter: check if the typed key is a mouse event that is used for
1776 * dragging the popup.
1777 */
1778 static void
1779filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1780{
1781 int row = mouse_row;
1782 int col = mouse_col;
1783
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001784 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001785 && is_mouse_key(c)
1786 && (wp == popup_dragwin
1787 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1788 // do not consume the key, allow for dragging the popup
1789 rettv->vval.v_number = 0;
1790}
1791
Bram Moolenaara730e552019-06-16 19:05:31 +02001792/*
1793 * popup_filter_menu({text}, {options})
1794 */
1795 void
1796f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1797{
1798 int id = tv_get_number(&argvars[0]);
1799 win_T *wp = win_id2wp(id);
1800 char_u *key = tv_get_string(&argvars[1]);
1801 typval_T res;
1802 int c;
1803 linenr_T old_lnum;
1804
1805 // If the popup has been closed do not consume the key.
1806 if (wp == NULL)
1807 return;
1808
1809 c = *key;
1810 if (c == K_SPECIAL && key[1] != NUL)
1811 c = TO_SPECIAL(key[1], key[2]);
1812
1813 // consume all keys until done
1814 rettv->vval.v_number = 1;
1815 res.v_type = VAR_NUMBER;
1816
1817 old_lnum = wp->w_cursor.lnum;
1818 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1819 --wp->w_cursor.lnum;
1820 if ((c == 'j' || c == 'J' || c == K_DOWN)
1821 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1822 ++wp->w_cursor.lnum;
1823 if (old_lnum != wp->w_cursor.lnum)
1824 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001825 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001826 return;
1827 }
1828
1829 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1830 {
1831 // Cancelled, invoke callback with -1
1832 res.vval.v_number = -1;
1833 popup_close_and_callback(wp, &res);
1834 return;
1835 }
1836 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1837 {
1838 // Invoke callback with current index.
1839 res.vval.v_number = wp->w_cursor.lnum;
1840 popup_close_and_callback(wp, &res);
1841 return;
1842 }
1843
1844 filter_handle_drag(wp, c, rettv);
1845}
1846
1847/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001848 * popup_filter_yesno({text}, {options})
1849 */
1850 void
1851f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1852{
1853 int id = tv_get_number(&argvars[0]);
1854 win_T *wp = win_id2wp(id);
1855 char_u *key = tv_get_string(&argvars[1]);
1856 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001857 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001858
1859 // If the popup has been closed don't consume the key.
1860 if (wp == NULL)
1861 return;
1862
Bram Moolenaara730e552019-06-16 19:05:31 +02001863 c = *key;
1864 if (c == K_SPECIAL && key[1] != NUL)
1865 c = TO_SPECIAL(key[1], key[2]);
1866
Bram Moolenaara42d9452019-06-15 21:46:30 +02001867 // consume all keys until done
1868 rettv->vval.v_number = 1;
1869
Bram Moolenaara730e552019-06-16 19:05:31 +02001870 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001871 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001872 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001873 res.vval.v_number = 0;
1874 else
1875 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001876 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001877 return;
1878 }
1879
1880 // Invoke callback
1881 res.v_type = VAR_NUMBER;
1882 popup_close_and_callback(wp, &res);
1883}
1884
1885/*
1886 * popup_dialog({text}, {options})
1887 */
1888 void
1889f_popup_dialog(typval_T *argvars, typval_T *rettv)
1890{
1891 popup_create(argvars, rettv, TYPE_DIALOG);
1892}
1893
1894/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001895 * popup_menu({text}, {options})
1896 */
1897 void
1898f_popup_menu(typval_T *argvars, typval_T *rettv)
1899{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001900 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001901}
1902
1903/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001904 * popup_notification({text}, {options})
1905 */
1906 void
1907f_popup_notification(typval_T *argvars, typval_T *rettv)
1908{
1909 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1910}
1911
1912/*
1913 * Find the popup window with window-ID "id".
1914 * If the popup window does not exist NULL is returned.
1915 * If the window is not a popup window, and error message is given.
1916 */
1917 static win_T *
1918find_popup_win(int id)
1919{
1920 win_T *wp = win_id2wp(id);
1921
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001922 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001923 {
1924 semsg(_("E993: window %d is not a popup window"), id);
1925 return NULL;
1926 }
1927 return wp;
1928}
1929
1930/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001931 * popup_close({id})
1932 */
1933 void
1934f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1935{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001936 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001937 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001938
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001939 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001940 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001941}
1942
1943/*
1944 * popup_hide({id})
1945 */
1946 void
1947f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1948{
1949 int id = (int)tv_get_number(argvars);
1950 win_T *wp = find_popup_win(id);
1951
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001952 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001953 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001954 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001955 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001956 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001957 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001958 }
1959}
1960
1961/*
1962 * popup_show({id})
1963 */
1964 void
1965f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
1966{
1967 int id = (int)tv_get_number(argvars);
1968 win_T *wp = find_popup_win(id);
1969
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001970 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001971 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001972 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001973 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001974 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001975 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001976 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001977}
1978
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001979/*
1980 * popup_settext({id}, {text})
1981 */
1982 void
1983f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
1984{
1985 int id = (int)tv_get_number(&argvars[0]);
1986 win_T *wp = find_popup_win(id);
1987
1988 if (wp != NULL)
1989 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001990 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
1991 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
1992 else
1993 {
1994 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001995 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001996 popup_adjust_position(wp);
1997 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001998 }
1999}
2000
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002001 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002002popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002003{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002004 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002005 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002006 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002007 clear_cmdline = TRUE;
2008 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002009
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002010 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002011 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002012}
2013
Bram Moolenaarec583842019-05-26 14:11:23 +02002014/*
2015 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002016 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002017 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002019popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002020{
2021 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002022 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002023 win_T *prev = NULL;
2024
Bram Moolenaarec583842019-05-26 14:11:23 +02002025 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002026 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002027 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002028 {
2029 if (prev == NULL)
2030 first_popupwin = wp->w_next;
2031 else
2032 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002033 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002034 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002035 }
2036
Bram Moolenaarec583842019-05-26 14:11:23 +02002037 // go through tab-local popups
2038 FOR_ALL_TABPAGES(tp)
2039 popup_close_tabpage(tp, id);
2040}
2041
2042/*
2043 * Close a popup window with Window-id "id" in tabpage "tp".
2044 */
2045 void
2046popup_close_tabpage(tabpage_T *tp, int id)
2047{
2048 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002049 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002050 win_T *prev = NULL;
2051
Bram Moolenaarec583842019-05-26 14:11:23 +02002052 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2053 if (wp->w_id == id)
2054 {
2055 if (prev == NULL)
2056 *root = wp->w_next;
2057 else
2058 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002059 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002060 return;
2061 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002062}
2063
2064 void
2065close_all_popups(void)
2066{
2067 while (first_popupwin != NULL)
2068 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002069 while (curtab->tp_first_popupwin != NULL)
2070 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002071}
2072
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002073/*
2074 * popup_move({id}, {options})
2075 */
2076 void
2077f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2078{
Bram Moolenaarae943152019-06-16 22:54:14 +02002079 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002080 int id = (int)tv_get_number(argvars);
2081 win_T *wp = find_popup_win(id);
2082
2083 if (wp == NULL)
2084 return; // invalid {id}
2085
2086 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2087 {
2088 emsg(_(e_dictreq));
2089 return;
2090 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002091 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002092
Bram Moolenaarae943152019-06-16 22:54:14 +02002093 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002094
2095 if (wp->w_winrow + wp->w_height >= cmdline_row)
2096 clear_cmdline = TRUE;
2097 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002098}
2099
Bram Moolenaarbc133542019-05-29 20:26:46 +02002100/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002101 * popup_setoptions({id}, {options})
2102 */
2103 void
2104f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2105{
2106 dict_T *dict;
2107 int id = (int)tv_get_number(argvars);
2108 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002109 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002110
2111 if (wp == NULL)
2112 return; // invalid {id}
2113
2114 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2115 {
2116 emsg(_(e_dictreq));
2117 return;
2118 }
2119 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002120 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002121
2122 apply_move_options(wp, dict);
2123 apply_general_options(wp, dict);
2124
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002125 if (old_firstline != wp->w_firstline)
2126 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002127 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002128 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002129 popup_adjust_position(wp);
2130}
2131
2132/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002133 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002134 */
2135 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002136f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002137{
2138 dict_T *dict;
2139 int id = (int)tv_get_number(argvars);
2140 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002141 int top_extra;
2142 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002143
2144 if (rettv_dict_alloc(rettv) == OK)
2145 {
2146 if (wp == NULL)
2147 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002148 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002149 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2150
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002151 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002152 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002153 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002154
Bram Moolenaarbc133542019-05-29 20:26:46 +02002155 dict_add_number(dict, "line", wp->w_winrow + 1);
2156 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002157 dict_add_number(dict, "width", wp->w_width + left_extra
2158 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2159 dict_add_number(dict, "height", wp->w_height + top_extra
2160 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002161
2162 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2163 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2164 dict_add_number(dict, "core_width", wp->w_width);
2165 dict_add_number(dict, "core_height", wp->w_height);
2166
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002167 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002168 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002169 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002170 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002171
2172 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002173 }
2174}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002175/*
2176 * popup_locate({row}, {col})
2177 */
2178 void
2179f_popup_locate(typval_T *argvars, typval_T *rettv)
2180{
2181 int row = tv_get_number(&argvars[0]) - 1;
2182 int col = tv_get_number(&argvars[1]) - 1;
2183 win_T *wp;
2184
2185 wp = mouse_find_win(&row, &col, FIND_POPUP);
2186 if (WIN_IS_POPUP(wp))
2187 rettv->vval.v_number = wp->w_id;
2188}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002189
2190/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002191 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2192 */
2193 static void
2194get_padding_border(dict_T *dict, int *array, char *name)
2195{
2196 list_T *list;
2197 int i;
2198
2199 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2200 return;
2201
2202 list = list_alloc();
2203 if (list != NULL)
2204 {
2205 dict_add_list(dict, name, list);
2206 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2207 for (i = 0; i < 4; ++i)
2208 list_append_number(list, array[i]);
2209 }
2210}
2211
2212/*
2213 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2214 */
2215 static void
2216get_borderhighlight(dict_T *dict, win_T *wp)
2217{
2218 list_T *list;
2219 int i;
2220
2221 for (i = 0; i < 4; ++i)
2222 if (wp->w_border_highlight[i] != NULL)
2223 break;
2224 if (i == 4)
2225 return;
2226
2227 list = list_alloc();
2228 if (list != NULL)
2229 {
2230 dict_add_list(dict, "borderhighlight", list);
2231 for (i = 0; i < 4; ++i)
2232 list_append_string(list, wp->w_border_highlight[i], -1);
2233 }
2234}
2235
2236/*
2237 * For popup_getoptions(): add a "borderchars" entry to "dict".
2238 */
2239 static void
2240get_borderchars(dict_T *dict, win_T *wp)
2241{
2242 list_T *list;
2243 int i;
2244 char_u buf[NUMBUFLEN];
2245 int len;
2246
2247 for (i = 0; i < 8; ++i)
2248 if (wp->w_border_char[i] != 0)
2249 break;
2250 if (i == 8)
2251 return;
2252
2253 list = list_alloc();
2254 if (list != NULL)
2255 {
2256 dict_add_list(dict, "borderchars", list);
2257 for (i = 0; i < 8; ++i)
2258 {
2259 len = mb_char2bytes(wp->w_border_char[i], buf);
2260 list_append_string(list, buf, len);
2261 }
2262 }
2263}
2264
2265/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002266 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002267 */
2268 static void
2269get_moved_list(dict_T *dict, win_T *wp)
2270{
2271 list_T *list;
2272
2273 list = list_alloc();
2274 if (list != NULL)
2275 {
2276 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002277 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002278 list_append_number(list, wp->w_popup_mincol);
2279 list_append_number(list, wp->w_popup_maxcol);
2280 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002281 list = list_alloc();
2282 if (list != NULL)
2283 {
2284 dict_add_list(dict, "mousemoved", list);
2285 list_append_number(list, wp->w_popup_mouse_row);
2286 list_append_number(list, wp->w_popup_mouse_mincol);
2287 list_append_number(list, wp->w_popup_mouse_maxcol);
2288 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002289}
2290
2291/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002292 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002293 */
2294 void
2295f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2296{
2297 dict_T *dict;
2298 int id = (int)tv_get_number(argvars);
2299 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002300 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002301 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002302
2303 if (rettv_dict_alloc(rettv) == OK)
2304 {
2305 if (wp == NULL)
2306 return;
2307
2308 dict = rettv->vval.v_dict;
2309 dict_add_number(dict, "line", wp->w_wantline);
2310 dict_add_number(dict, "col", wp->w_wantcol);
2311 dict_add_number(dict, "minwidth", wp->w_minwidth);
2312 dict_add_number(dict, "minheight", wp->w_minheight);
2313 dict_add_number(dict, "maxheight", wp->w_maxheight);
2314 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002315 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002316 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002317 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002318 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002319 dict_add_string(dict, "title", wp->w_popup_title);
2320 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002321 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02002322 dict_add_number(dict, "mapping", (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002323 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002324 dict_add_number(dict, "cursorline",
2325 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002326 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002327 if (wp->w_scrollbar_highlight != NULL)
2328 dict_add_string(dict, "scrollbarhighlight",
2329 wp->w_scrollbar_highlight);
2330 if (wp->w_thumb_highlight != NULL)
2331 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002332
Bram Moolenaara3fce622019-06-20 02:31:49 +02002333 // find the tabpage that holds this popup
2334 i = 1;
2335 FOR_ALL_TABPAGES(tp)
2336 {
2337 win_T *p;
2338
2339 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2340 if (p->w_id == id)
2341 break;
2342 if (p != NULL)
2343 break;
2344 ++i;
2345 }
2346 if (tp == NULL)
2347 i = -1; // must be global
2348 else if (tp == curtab)
2349 i = 0;
2350 dict_add_number(dict, "tabpage", i);
2351
Bram Moolenaarae943152019-06-16 22:54:14 +02002352 get_padding_border(dict, wp->w_popup_padding, "padding");
2353 get_padding_border(dict, wp->w_popup_border, "border");
2354 get_borderhighlight(dict, wp);
2355 get_borderchars(dict, wp);
2356 get_moved_list(dict, wp);
2357
2358 if (wp->w_filter_cb.cb_name != NULL)
2359 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2360 if (wp->w_close_cb.cb_name != NULL)
2361 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002362
2363 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2364 ++i)
2365 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2366 {
2367 dict_add_string(dict, "pos",
2368 (char_u *)poppos_entries[i].pp_name);
2369 break;
2370 }
2371
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002372 dict_add_string(dict, "close", (char_u *)(
2373 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2374 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2375
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002376# if defined(FEAT_TIMERS)
2377 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2378 ? (long)wp->w_popup_timer->tr_interval : 0L);
2379# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002380 }
2381}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002382
2383 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002384error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002385{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002386 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002387 {
2388 emsg(_("E994: Not allowed in a popup window"));
2389 return TRUE;
2390 }
2391 return FALSE;
2392}
2393
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002394/*
2395 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002396 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002397 */
2398 void
2399popup_reset_handled()
2400{
2401 win_T *wp;
2402
2403 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2404 wp->w_popup_flags &= ~POPF_HANDLED;
2405 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2406 wp->w_popup_flags &= ~POPF_HANDLED;
2407}
2408
2409/*
2410 * Find the next visible popup where POPF_HANDLED is not set.
2411 * Must have called popup_reset_handled() first.
2412 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2413 * popup with the highest zindex.
2414 */
2415 win_T *
2416find_next_popup(int lowest)
2417{
2418 win_T *wp;
2419 win_T *found_wp;
2420 int found_zindex;
2421
2422 found_zindex = lowest ? INT_MAX : 0;
2423 found_wp = NULL;
2424 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2425 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2426 && (lowest ? wp->w_zindex < found_zindex
2427 : wp->w_zindex > found_zindex))
2428 {
2429 found_zindex = wp->w_zindex;
2430 found_wp = wp;
2431 }
2432 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2433 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2434 && (lowest ? wp->w_zindex < found_zindex
2435 : wp->w_zindex > found_zindex))
2436 {
2437 found_zindex = wp->w_zindex;
2438 found_wp = wp;
2439 }
2440
2441 if (found_wp != NULL)
2442 found_wp->w_popup_flags |= POPF_HANDLED;
2443 return found_wp;
2444}
2445
2446/*
2447 * Invoke the filter callback for window "wp" with typed character "c".
2448 * Uses the global "mod_mask" for modifiers.
2449 * Returns the return value of the filter.
2450 * Careful: The filter may make "wp" invalid!
2451 */
2452 static int
2453invoke_popup_filter(win_T *wp, int c)
2454{
2455 int res;
2456 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002457 typval_T argv[3];
2458 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002459 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002460
Bram Moolenaara42d9452019-06-15 21:46:30 +02002461 // Emergency exit: CTRL-C closes the popup.
2462 if (c == Ctrl_C)
2463 {
2464 rettv.v_type = VAR_NUMBER;
2465 rettv.vval.v_number = -1;
2466 popup_close_and_callback(wp, &rettv);
2467 return 1;
2468 }
2469
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002470 argv[0].v_type = VAR_NUMBER;
2471 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2472
2473 // Convert the number to a string, so that the function can use:
2474 // if a:c == "\<F2>"
2475 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2476 argv[1].v_type = VAR_STRING;
2477 argv[1].vval.v_string = vim_strsave(buf);
2478
2479 argv[2].v_type = VAR_UNKNOWN;
2480
Bram Moolenaara42d9452019-06-15 21:46:30 +02002481 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002482 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002483 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002484 popup_highlight_curline(wp);
2485
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002486 res = tv_get_number(&rettv);
2487 vim_free(argv[1].vval.v_string);
2488 clear_tv(&rettv);
2489 return res;
2490}
2491
2492/*
2493 * Called when "c" was typed: invoke popup filter callbacks.
2494 * Returns TRUE when the character was consumed,
2495 */
2496 int
2497popup_do_filter(int c)
2498{
2499 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002500 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002501
2502 popup_reset_handled();
2503
2504 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2505 if (wp->w_filter_cb.cb_name != NULL)
2506 res = invoke_popup_filter(wp, c);
2507
2508 return res;
2509}
2510
Bram Moolenaar3397f742019-06-02 18:40:06 +02002511/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002512 * Return TRUE if there is a popup visible with a filter callback and the
2513 * "mapping" property off.
2514 */
2515 int
2516popup_no_mapping(void)
2517{
2518 int round;
2519 win_T *wp;
2520
2521 for (round = 1; round <= 2; ++round)
2522 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2523 wp != NULL; wp = wp->w_next)
2524 if (wp->w_filter_cb.cb_name != NULL
2525 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2526 return TRUE;
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002531 * Called when the cursor moved: check if any popup needs to be closed if the
2532 * cursor moved far enough.
2533 */
2534 void
2535popup_check_cursor_pos()
2536{
2537 win_T *wp;
2538 typval_T tv;
2539
2540 popup_reset_handled();
2541 while ((wp = find_next_popup(TRUE)) != NULL)
2542 if (wp->w_popup_curwin != NULL
2543 && (curwin != wp->w_popup_curwin
2544 || curwin->w_cursor.lnum != wp->w_popup_lnum
2545 || curwin->w_cursor.col < wp->w_popup_mincol
2546 || curwin->w_cursor.col > wp->w_popup_maxcol))
2547 {
2548 tv.v_type = VAR_NUMBER;
2549 tv.vval.v_number = -1;
2550 popup_close_and_callback(wp, &tv);
2551 }
2552}
2553
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002554/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002555 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002556 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002557 static void
2558popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002559{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002560 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002561 char_u *cells;
2562 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002563
2564 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002565 return;
2566 if (wp->w_popup_mask_cells != NULL
2567 && wp->w_popup_mask_height == height
2568 && wp->w_popup_mask_width == width)
2569 return; // cache is still valid
2570
2571 vim_free(wp->w_popup_mask_cells);
2572 wp->w_popup_mask_cells = alloc_clear(width * height);
2573 if (wp->w_popup_mask_cells == NULL)
2574 return;
2575 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002576
2577 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2578 {
2579 int cols, cole;
2580 int lines, linee;
2581
2582 li = lio->li_tv.vval.v_list->lv_first;
2583 cols = tv_get_number(&li->li_tv);
2584 if (cols < 0)
2585 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002586 li = li->li_next;
2587 cole = tv_get_number(&li->li_tv);
2588 if (cole < 0)
2589 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002590 li = li->li_next;
2591 lines = tv_get_number(&li->li_tv);
2592 if (lines < 0)
2593 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002594 li = li->li_next;
2595 linee = tv_get_number(&li->li_tv);
2596 if (linee < 0)
2597 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002598
2599 for (row = lines - 1; row < linee && row < height; ++row)
2600 for (col = cols - 1; col < cole && col < width; ++col)
2601 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002602 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002603}
2604
2605/*
2606 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2607 * "col" and "line" are screen coordinates.
2608 */
2609 static int
2610popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2611{
2612 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2613 int line = screenline - wp->w_winrow;
2614
2615 return col >= 0 && col < width
2616 && line >= 0 && line < height
2617 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002618}
2619
2620/*
2621 * Set flags in popup_transparent[] for window "wp" to "val".
2622 */
2623 static void
2624update_popup_transparent(win_T *wp, int val)
2625{
2626 if (wp->w_popup_mask != NULL)
2627 {
2628 int width = popup_width(wp);
2629 int height = popup_height(wp);
2630 listitem_T *lio, *li;
2631 int cols, cole;
2632 int lines, linee;
2633 int col, line;
2634
2635 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2636 {
2637 li = lio->li_tv.vval.v_list->lv_first;
2638 cols = tv_get_number(&li->li_tv);
2639 if (cols < 0)
2640 cols = width + cols + 1;
2641 li = li->li_next;
2642 cole = tv_get_number(&li->li_tv);
2643 if (cole < 0)
2644 cole = width + cole + 1;
2645 li = li->li_next;
2646 lines = tv_get_number(&li->li_tv);
2647 if (lines < 0)
2648 lines = height + lines + 1;
2649 li = li->li_next;
2650 linee = tv_get_number(&li->li_tv);
2651 if (linee < 0)
2652 linee = height + linee + 1;
2653
2654 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002655 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002656 if (cols < 0)
2657 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002658 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002659 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002660 if (lines < 0)
2661 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002662 for (line = lines; line < linee
2663 && line + wp->w_winrow < screen_Rows; ++line)
2664 for (col = cols; col < cole
2665 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002666 popup_transparent[(line + wp->w_winrow) * screen_Columns
2667 + col + wp->w_wincol] = val;
2668 }
2669 }
2670}
2671
2672/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002673 * Update "popup_mask" if needed.
2674 * Also recomputes the popup size and positions.
2675 * Also updates "popup_visible".
2676 * Also marks window lines for redrawing.
2677 */
2678 void
2679may_update_popup_mask(int type)
2680{
2681 win_T *wp;
2682 short *mask;
2683 int line, col;
2684 int redraw_all = FALSE;
2685
2686 // Need to recompute when switching tabs.
2687 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2688 // (such as the screen size) must have changed.
2689 if (popup_mask_tab != curtab || type >= NOT_VALID)
2690 {
2691 popup_mask_refresh = TRUE;
2692 redraw_all = TRUE;
2693 }
2694 if (!popup_mask_refresh)
2695 {
2696 // Check if any buffer has changed.
2697 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2698 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2699 popup_mask_refresh = TRUE;
2700 for (wp = curtab->tp_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 if (!popup_mask_refresh)
2704 return;
2705 }
2706
2707 // Need to update the mask, something has changed.
2708 popup_mask_refresh = FALSE;
2709 popup_mask_tab = curtab;
2710 popup_visible = FALSE;
2711
2712 // If redrawing everything, just update "popup_mask".
2713 // If redrawing only what is needed, update "popup_mask_next" and then
2714 // compare with "popup_mask" to see what changed.
2715 if (type >= SOME_VALID)
2716 mask = popup_mask;
2717 else
2718 mask = popup_mask_next;
2719 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2720
2721 // Find the window with the lowest zindex that hasn't been handled yet,
2722 // so that the window with a higher zindex overwrites the value in
2723 // popup_mask.
2724 popup_reset_handled();
2725 while ((wp = find_next_popup(TRUE)) != NULL)
2726 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002727 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002728 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002729
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002730 popup_visible = TRUE;
2731
2732 // Recompute the position if the text changed.
2733 if (redraw_all
2734 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2735 popup_adjust_position(wp);
2736
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002737 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002738 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002739 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002740 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002741 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002742 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002743 col < wp->w_wincol + width - wp->w_popup_leftoff
2744 && col < screen_Columns; ++col)
2745 if (wp->w_popup_mask_cells == NULL
2746 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002747 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002748 }
2749
2750 // Only check which lines are to be updated if not already
2751 // updating all lines.
2752 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002753 {
2754 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2755 win_T *prev_wp = NULL;
2756
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002757 for (line = 0; line < screen_Rows; ++line)
2758 {
2759 int col_done = 0;
2760
2761 for (col = 0; col < screen_Columns; ++col)
2762 {
2763 int off = line * screen_Columns + col;
2764
2765 if (popup_mask[off] != popup_mask_next[off])
2766 {
2767 popup_mask[off] = popup_mask_next[off];
2768
2769 if (line >= cmdline_row)
2770 {
2771 // the command line needs to be cleared if text below
2772 // the popup is now visible.
2773 if (!msg_scrolled && popup_mask_next[off] == 0)
2774 clear_cmdline = TRUE;
2775 }
2776 else if (col >= col_done)
2777 {
2778 linenr_T lnum;
2779 int line_cp = line;
2780 int col_cp = col;
2781
2782 // The screen position "line" / "col" needs to be
2783 // redrawn. Figure out what window that is and update
2784 // w_redraw_top and w_redr_bot. Only needs to be done
2785 // once for each window line.
2786 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2787 if (wp != NULL)
2788 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002789 if (wp != prev_wp)
2790 {
2791 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2792 prev_wp = wp;
2793 }
2794
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002795 if (line_cp >= wp->w_height)
2796 // In (or below) status line
2797 wp->w_redr_status = TRUE;
2798 // compute the position in the buffer line from the
2799 // position on the screen
2800 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002801 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002802 // past bottom
2803 wp->w_redr_status = TRUE;
2804 else
2805 redrawWinline(wp, lnum);
2806
2807 // This line is going to be redrawn, no need to
2808 // check until the right side of the window.
2809 col_done = wp->w_wincol + wp->w_width - 1;
2810 }
2811 }
2812 }
2813 }
2814 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002815
2816 vim_free(plines_cache);
2817 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002818}
2819
2820/*
2821 * Return a string of "len" spaces in IObuff.
2822 */
2823 static char_u *
2824get_spaces(int len)
2825{
2826 vim_memset(IObuff, ' ', (size_t)len);
2827 IObuff[len] = NUL;
2828 return IObuff;
2829}
2830
2831/*
2832 * Update popup windows. They are drawn on top of normal windows.
2833 * "win_update" is called for each popup window, lowest zindex first.
2834 */
2835 void
2836update_popups(void (*win_update)(win_T *wp))
2837{
2838 win_T *wp;
2839 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002840 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002841 int total_width;
2842 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002843 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002844 int popup_attr;
2845 int border_attr[4];
2846 int border_char[8];
2847 char_u buf[MB_MAXBYTES];
2848 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002849 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002850 int padcol = 0;
2851 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002852 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002853 int sb_thumb_top = 0;
2854 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002855 int attr_scroll = 0;
2856 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002857
2858 // Find the window with the lowest zindex that hasn't been updated yet,
2859 // so that the window with a higher zindex is drawn later, thus goes on
2860 // top.
2861 popup_reset_handled();
2862 while ((wp = find_next_popup(TRUE)) != NULL)
2863 {
2864 // This drawing uses the zindex of the popup window, so that it's on
2865 // top of the text but doesn't draw when another popup with higher
2866 // zindex is on top of the character.
2867 screen_zindex = wp->w_zindex;
2868
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002869 // Set flags in popup_transparent[] for masked cells.
2870 update_popup_transparent(wp, 1);
2871
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002872 // adjust w_winrow and w_wincol for border and padding, since
2873 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002874 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002875 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2876 - wp->w_popup_leftoff;
2877 if (wp->w_wincol + left_extra < 0)
2878 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002879 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002880 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002881
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002882 // Draw the popup text, unless it's off screen.
2883 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2884 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002885
2886 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002887 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002888
Bram Moolenaard529ba52019-07-02 23:13:53 +02002889 total_width = popup_width(wp);
2890 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002891 popup_attr = get_wcr_attr(wp);
2892
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002893 if (wp->w_winrow + total_height > cmdline_row)
2894 wp->w_popup_flags |= POPF_ON_CMDLINE;
2895 else
2896 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2897
2898
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002899 // We can only use these line drawing characters when 'encoding' is
2900 // "utf-8" and 'ambiwidth' is "single".
2901 if (enc_utf8 && *p_ambw == 's')
2902 {
2903 border_char[0] = border_char[2] = 0x2550;
2904 border_char[1] = border_char[3] = 0x2551;
2905 border_char[4] = 0x2554;
2906 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002907 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
2908 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002909 border_char[7] = 0x255a;
2910 }
2911 else
2912 {
2913 border_char[0] = border_char[2] = '-';
2914 border_char[1] = border_char[3] = '|';
2915 for (i = 4; i < 8; ++i)
2916 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002917 if (wp->w_popup_flags & POPF_RESIZE)
2918 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002919 }
2920 for (i = 0; i < 8; ++i)
2921 if (wp->w_border_char[i] != 0)
2922 border_char[i] = wp->w_border_char[i];
2923
2924 for (i = 0; i < 4; ++i)
2925 {
2926 border_attr[i] = popup_attr;
2927 if (wp->w_border_highlight[i] != NULL)
2928 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2929 }
2930
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002931 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002932 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002933 if (wp->w_popup_border[0] > 0)
2934 {
2935 // top border
2936 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002937 wincol < 0 ? 0 : wincol, wincol + total_width,
2938 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002939 ? border_char[4] : border_char[0],
2940 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002941 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002942 {
2943 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2944 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002945 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002946 }
2947 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002948 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2949 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002950
Bram Moolenaard529ba52019-07-02 23:13:53 +02002951 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2952 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002953 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002954 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2955 - wp->w_has_scrollbar;
2956 if (padcol < 0)
2957 {
2958 padwidth += padcol;
2959 padcol = 0;
2960 }
2961 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002962 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002963 {
2964 // top padding
2965 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002966 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002967 ' ', ' ', popup_attr);
2968 }
2969
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002970 // Title goes on top of border or padding.
2971 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02002972 {
2973 int len = (int)STRLEN(wp->w_popup_title) + 1;
2974 char_u *title = alloc(len);
2975
2976 trunc_string(wp->w_popup_title, title, total_width - 2, len);
2977 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002978 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02002979 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002980
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002981 // Compute scrollbar thumb position and size.
2982 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002983 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002984 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
2985
Bram Moolenaar68acb412019-06-26 03:40:36 +02002986 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
2987 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002988 if (sb_thumb_height == 0)
2989 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02002990 if (linecount <= wp->w_height)
2991 // it just fits, avoid divide by zero
2992 sb_thumb_top = 0;
2993 else
2994 sb_thumb_top = (wp->w_topline - 1
2995 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02002996 * (wp->w_height - sb_thumb_height)
2997 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002998 if (wp->w_scrollbar_highlight != NULL)
2999 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3000 else
3001 attr_scroll = highlight_attr[HLF_PSB];
3002 if (wp->w_thumb_highlight != NULL)
3003 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3004 else
3005 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003006 }
3007
3008 for (i = wp->w_popup_border[0];
3009 i < total_height - wp->w_popup_border[2]; ++i)
3010 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003011 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003012 // left and right padding only needed next to the body
3013 int do_padding =
3014 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3015 && i < total_height - wp->w_popup_border[2]
3016 - wp->w_popup_padding[2];
3017
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003018 row = wp->w_winrow + i;
3019
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003020 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003021 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003022 {
3023 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003024 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003025 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003026 if (do_padding && wp->w_popup_padding[3] > 0)
3027 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003028 int col = wincol + wp->w_popup_border[3];
3029
Bram Moolenaard529ba52019-07-02 23:13:53 +02003030 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003031 pad_left = wp->w_popup_padding[3];
3032 if (col < 0)
3033 {
3034 pad_left += col;
3035 col = 0;
3036 }
3037 if (pad_left > 0)
3038 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3039 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003040 // scrollbar
3041 if (wp->w_has_scrollbar)
3042 {
3043 int line = i - top_off;
3044 int scroll_col = wp->w_wincol + total_width - 1
3045 - wp->w_popup_border[1];
3046
3047 if (line >= 0 && line < wp->w_height)
3048 screen_putchar(' ', row, scroll_col,
3049 line >= sb_thumb_top
3050 && line < sb_thumb_top + sb_thumb_height
3051 ? attr_thumb : attr_scroll);
3052 else
3053 screen_putchar(' ', row, scroll_col, popup_attr);
3054 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003055 // right border
3056 if (wp->w_popup_border[1] > 0)
3057 {
3058 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003059 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003060 }
3061 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003062 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003063 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003064 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003065 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3066 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003067 }
3068
3069 if (wp->w_popup_padding[2] > 0)
3070 {
3071 // bottom padding
3072 row = wp->w_winrow + wp->w_popup_border[0]
3073 + wp->w_popup_padding[0] + wp->w_height;
3074 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003075 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003076 }
3077
3078 if (wp->w_popup_border[2] > 0)
3079 {
3080 // bottom border
3081 row = wp->w_winrow + total_height - 1;
3082 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003083 wincol < 0 ? 0 : wincol,
3084 wincol + total_width,
3085 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003086 ? border_char[7] : border_char[2],
3087 border_char[2], border_attr[2]);
3088 if (wp->w_popup_border[1] > 0)
3089 {
3090 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003091 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003092 }
3093 }
3094
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003095 if (wp->w_popup_close == POPCLOSE_BUTTON)
3096 {
3097 // close button goes on top of anything at the top-right corner
3098 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003099 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003100 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3101 }
3102
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003103 update_popup_transparent(wp, 0);
3104
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003105 // Back to the normal zindex.
3106 screen_zindex = 0;
3107 }
3108}
3109
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003110/*
3111 * Mark references in callbacks of one popup window.
3112 */
3113 static int
3114set_ref_in_one_popup(win_T *wp, int copyID)
3115{
3116 int abort = FALSE;
3117 typval_T tv;
3118
3119 if (wp->w_close_cb.cb_partial != NULL)
3120 {
3121 tv.v_type = VAR_PARTIAL;
3122 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3123 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3124 }
3125 if (wp->w_filter_cb.cb_partial != NULL)
3126 {
3127 tv.v_type = VAR_PARTIAL;
3128 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3129 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3130 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003131 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003132 return abort;
3133}
3134
3135/*
3136 * Set reference in callbacks of popup windows.
3137 */
3138 int
3139set_ref_in_popups(int copyID)
3140{
3141 int abort = FALSE;
3142 win_T *wp;
3143 tabpage_T *tp;
3144
3145 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3146 abort = abort || set_ref_in_one_popup(wp, copyID);
3147
3148 FOR_ALL_TABPAGES(tp)
3149 {
3150 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3151 abort = abort || set_ref_in_one_popup(wp, copyID);
3152 if (abort)
3153 break;
3154 }
3155 return abort;
3156}
Bram Moolenaar79648732019-07-18 21:43:07 +02003157
3158/*
3159 * Find an existing popup used as the preview window, in the current tab page.
3160 * Return NULL if not found.
3161 */
3162 win_T *
3163popup_find_preview_window(void)
3164{
3165 win_T *wp;
3166
3167 // Preview window popup is always local to tab page.
3168 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3169 if (wp->w_p_pvw)
3170 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003171 return NULL;
3172}
3173
3174 void
3175f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3176{
3177 win_T *wp = popup_find_preview_window();
3178
3179 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003180}
3181
3182 int
3183popup_is_popup(win_T *wp)
3184{
3185 return wp->w_popup_flags != 0;
3186}
3187
3188/*
3189 * Create a popup to be used as the preview window.
3190 * NOTE: this makes the popup the current window, so that the file can be
3191 * edited. However, it must not remain to be the current window, the caller
3192 * must make sure of that.
3193 */
3194 int
3195popup_create_preview_window(void)
3196{
3197 win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW);
3198
3199 if (wp == NULL)
3200 return FAIL;
3201 wp->w_p_pvw = TRUE;
3202
3203 // Set the width to a reasonable value, so that w_topline can be computed.
3204 if (wp->w_minwidth > 0)
3205 wp->w_width = wp->w_minwidth;
3206 else if (wp->w_maxwidth > 0)
3207 wp->w_width = wp->w_maxwidth;
3208 else
3209 wp->w_width = curwin->w_width;
3210
3211 // Will switch to another buffer soon, dummy one can be wiped.
3212 wp->w_buffer->b_locked = FALSE;
3213
3214 win_enter(wp, FALSE);
3215 return OK;
3216}
3217
3218 void
3219popup_close_preview()
3220{
3221 win_T *wp = popup_find_preview_window();
3222
3223 if (wp != NULL)
3224 {
3225 typval_T res;
3226
3227 res.v_type = VAR_NUMBER;
3228 res.vval.v_number = -1;
3229 popup_close_and_callback(wp, &res);
3230 }
3231}
3232
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003233/*
3234 * Set the title of the popup window to the file name.
3235 */
3236 void
3237popup_set_title(win_T *wp)
3238{
3239 if (wp->w_buffer->b_fname != NULL)
3240 {
3241 char_u dirname[MAXPATHL];
3242 size_t len;
3243
3244 mch_dirname(dirname, MAXPATHL);
3245 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3246
3247 vim_free(wp->w_popup_title);
3248 len = STRLEN(wp->w_buffer->b_fname) + 3;
3249 wp->w_popup_title = alloc((int)len);
3250 if (wp->w_popup_title != NULL)
3251 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3252 wp->w_buffer->b_fname);
3253 redraw_win_later(wp, VALID);
3254 }
3255}
3256
3257/*
3258 * If there is a preview window, update the title.
3259 * Used after changing directory.
3260 */
3261 void
3262popup_update_preview_title(void)
3263{
3264 win_T *wp = popup_find_preview_window();
3265
3266 if (wp != NULL)
3267 popup_set_title(wp);
3268}
3269
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003270#endif // FEAT_TEXT_PROP