blob: 0198fb56977cc06c027ac2d2c4229429bd8bd142 [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
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200954 + popup_top_extra(wp)
955 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +0200956}
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
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200968 + popup_extra_width(wp)
969 + wp->w_popup_rightoff;
970}
971
972/*
973 * Return the extra width of popup window "wp": border, padding and scrollbar.
974 */
975 int
976popup_extra_width(win_T *wp)
977{
978 return wp->w_popup_padding[3] + wp->w_popup_border[3]
979 + wp->w_popup_padding[1] + wp->w_popup_border[1]
980 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200981}
982
983/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200984 * Adjust the position and size of the popup to fit on the screen.
985 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200986 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200987popup_adjust_position(win_T *wp)
988{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200989 linenr_T lnum;
990 int wrapped = 0;
991 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200992 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200993 int center_vert = FALSE;
994 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200995 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200996 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200997 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
998 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
999 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1000 int extra_height = top_extra + bot_extra;
1001 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001002 int org_winrow = wp->w_winrow;
1003 int org_wincol = wp->w_wincol;
1004 int org_width = wp->w_width;
1005 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001006 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001007 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001008 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001009
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001010 wp->w_winrow = 0;
1011 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001012 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001013 wp->w_popup_leftoff = 0;
1014 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001015 if (wp->w_popup_pos == POPPOS_CENTER)
1016 {
1017 // center after computing the size
1018 center_vert = TRUE;
1019 center_hor = TRUE;
1020 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001021 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001022 {
1023 if (wp->w_wantline == 0)
1024 center_vert = TRUE;
1025 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1026 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1027 {
1028 wp->w_winrow = wp->w_wantline - 1;
1029 if (wp->w_winrow >= Rows)
1030 wp->w_winrow = Rows - 1;
1031 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001032
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001033 if (wp->w_wantcol == 0)
1034 center_hor = TRUE;
1035 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1036 || wp->w_popup_pos == POPPOS_BOTLEFT)
1037 {
1038 wp->w_wincol = wp->w_wantcol - 1;
1039 if (wp->w_wincol >= Columns - 3)
1040 wp->w_wincol = Columns - 3;
1041 }
1042 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001043
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001044 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001045 // When left aligned use the space available, but shift to the left when we
1046 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001047 maxspace = Columns - wp->w_wincol - left_extra;
1048 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001049 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001050 {
1051 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001052 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001053 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001054
Bram Moolenaar8d241042019-06-12 23:40:01 +02001055 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001056 if (wp->w_firstline != 0)
1057 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001058 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1059 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1060
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001061 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001062 // Use a minimum width of one, so that something shows when there is no
1063 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001064 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001065 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001066 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001067 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001068 int len;
1069 int w_width = wp->w_width;
1070
1071 // Count Tabs for what they are worth and compute the length based on
1072 // the maximum width (matters when 'showbreak' is set).
1073 if (wp->w_width < maxwidth)
1074 wp->w_width = maxwidth;
1075 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001076 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001077 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001078
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001079 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001080 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001081 while (len > maxwidth)
1082 {
1083 ++wrapped;
1084 len -= maxwidth;
1085 wp->w_width = maxwidth;
1086 }
1087 }
1088 else if (len > maxwidth
1089 && allow_adjust_left
1090 && (wp->w_popup_pos == POPPOS_TOPLEFT
1091 || wp->w_popup_pos == POPPOS_BOTLEFT))
1092 {
1093 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001094 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001095
Bram Moolenaar51c31312019-06-15 22:27:23 +02001096 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001097 {
1098 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001099
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001100 len -= truncate_shift;
1101 shift_by -= truncate_shift;
1102 }
1103
1104 wp->w_wincol -= shift_by;
1105 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001106 wp->w_width = maxwidth;
1107 }
1108 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001109 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001110 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001111 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1112 wp->w_width = wp->w_maxwidth;
1113 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001114 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001115 if (wp->w_maxheight > 0
1116 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001117 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001118 }
1119
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001120 wp->w_has_scrollbar = wp->w_want_scrollbar
1121 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001122 if (wp->w_has_scrollbar)
1123 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001124
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001125 minwidth = wp->w_minwidth;
1126 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1127 {
1128 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1129
1130 if (minwidth < title_len)
1131 minwidth = title_len;
1132 }
1133
1134 if (minwidth > 0 && wp->w_width < minwidth)
1135 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001136 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001137 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001138 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001139 // some columns cut off on the right
1140 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001141 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001142 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001143 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001144 {
1145 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1146 if (wp->w_wincol < 0)
1147 wp->w_wincol = 0;
1148 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001149 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1150 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1151 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001152 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1153
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001154 // Right aligned: move to the right if needed.
1155 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001156 if (leftoff >= 0)
1157 wp->w_wincol = leftoff;
1158 else if (wp->w_popup_fixed)
1159 {
1160 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001161 // columns when displaying the window, minus left border and
1162 // padding.
1163 if (-leftoff > left_extra)
1164 wp->w_leftcol = -leftoff - left_extra;
1165 wp->w_width -= wp->w_leftcol;
1166 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001167 if (wp->w_width < 0)
1168 wp->w_width = 0;
1169 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001170 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001172 if (wp->w_p_wrap || (!wp->w_popup_fixed
1173 && (wp->w_popup_pos == POPPOS_TOPLEFT
1174 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001175 {
1176 int want_col = 0;
1177
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001178 // try to show the right border and any scrollbar
1179 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001180 if (want_col > 0 && wp->w_wincol > 0
1181 && wp->w_wincol + want_col >= Columns)
1182 {
1183 wp->w_wincol = Columns - want_col;
1184 if (wp->w_wincol < 0)
1185 wp->w_wincol = 0;
1186 }
1187 }
1188
Bram Moolenaar8d241042019-06-12 23:40:01 +02001189 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1190 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001191 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1192 wp->w_height = wp->w_minheight;
1193 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1194 wp->w_height = wp->w_maxheight;
1195 if (wp->w_height > Rows - wp->w_winrow)
1196 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001197
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001198 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001199 {
1200 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1201 if (wp->w_winrow < 0)
1202 wp->w_winrow = 0;
1203 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1205 || wp->w_popup_pos == POPPOS_BOTLEFT)
1206 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001207 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001208 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001209 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001210 else
1211 // not enough space, make top aligned
1212 wp->w_winrow = wp->w_wantline + 1;
1213 }
1214
Bram Moolenaar17146962019-05-30 00:12:11 +02001215 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001216
1217 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001218 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001219 if (org_winrow != wp->w_winrow
1220 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001221 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001222 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001223 || org_width != wp->w_width
1224 || org_height != wp->w_height)
1225 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001226 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001227 redraw_win_later(wp, NOT_VALID);
1228 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1229 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001230 popup_mask_refresh = TRUE;
1231 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001232}
1233
Bram Moolenaar17627312019-06-02 19:53:44 +02001234typedef enum
1235{
1236 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001237 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001238 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001239 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001240 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001241 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001242 TYPE_PREVIEW, // preview window
1243 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001244} create_type_T;
1245
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001246/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001247 * Make "buf" empty and set the contents to "text".
1248 * Used by popup_create() and popup_settext().
1249 */
1250 static void
1251popup_set_buffer_text(buf_T *buf, typval_T text)
1252{
1253 int lnum;
1254
1255 // Clear the buffer, then replace the lines.
1256 curbuf = buf;
1257 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1258 ml_delete(lnum, FALSE);
1259 curbuf = curwin->w_buffer;
1260
1261 // Add text to the buffer.
1262 if (text.v_type == VAR_STRING)
1263 {
1264 // just a string
1265 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1266 }
1267 else
1268 {
1269 list_T *l = text.vval.v_list;
1270
1271 if (l->lv_len > 0)
1272 {
1273 if (l->lv_first->li_tv.v_type == VAR_STRING)
1274 // list of strings
1275 add_popup_strings(buf, l);
1276 else
1277 // list of dictionaries
1278 add_popup_dicts(buf, l);
1279 }
1280 }
1281
1282 // delete the line that was in the empty buffer
1283 curbuf = buf;
1284 ml_delete(buf->b_ml.ml_line_count, FALSE);
1285 curbuf = curwin->w_buffer;
1286}
1287
1288/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001289 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1290 * not NULL.
1291 * Return FAIL if the parsing fails.
1292 */
1293 int
1294parse_previewpopup(win_T *wp)
1295{
1296 char_u *p;
1297
1298 for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0))
1299 {
1300 char_u *e, *dig;
1301 char_u *s = p;
1302 int x;
1303
1304 e = vim_strchr(p, ':');
1305 if (e == NULL || e[1] == NUL)
1306 return FAIL;
1307
1308 p = vim_strchr(e, ',');
1309 if (p == NULL)
1310 p = e + STRLEN(e);
1311 dig = e + 1;
1312 x = getdigits(&dig);
1313 if (dig != p)
1314 return FAIL;
1315
1316 if (STRNCMP(s, "height:", 7) == 0)
1317 {
1318 if (wp != NULL)
1319 {
1320 wp->w_minheight = x;
1321 wp->w_maxheight = x;
1322 }
1323 }
1324 else if (STRNCMP(s, "width:", 6) == 0)
1325 {
1326 if (wp != NULL)
1327 {
1328 wp->w_minwidth = x;
1329 wp->w_maxwidth = x;
1330 }
1331 }
1332 else
1333 return FAIL;
1334 }
1335 return OK;
1336}
1337
1338/*
1339 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001340 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001341 */
1342 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001343popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001344{
1345 setcursor_mayforce(TRUE);
1346 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1347 if (wp->w_wantline == 0) // cursor in first line
1348 {
1349 wp->w_wantline = 2;
1350 wp->w_popup_pos = POPPOS_TOPLEFT;
1351 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001352
Bram Moolenaar79648732019-07-18 21:43:07 +02001353 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001354 if (wp->w_wantcol > Columns - width)
1355 {
1356 wp->w_wantcol = Columns - width;
1357 if (wp->w_wantcol < 1)
1358 wp->w_wantcol = 1;
1359 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001360 popup_adjust_position(wp);
1361}
1362
1363/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001364 * Set w_wantline and w_wantcol for the a given screen position.
1365 * Caller must take care of running into the window border.
1366 */
1367 void
1368popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1369{
1370 wp->w_wantline = row;
1371 wp->w_wantcol = col;
1372 popup_adjust_position(wp);
1373}
1374
1375/*
1376 * Add a border and lef&right padding.
1377 */
1378 static void
1379add_border_left_right_padding(win_T *wp)
1380{
1381 int i;
1382
1383 for (i = 0; i < 4; ++i)
1384 {
1385 wp->w_popup_border[i] = 1;
1386 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1387 }
1388}
1389
1390/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001391 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001392 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001393 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001394 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001395 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001396 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001397popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001398{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001399 win_T *wp;
1400 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001401 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001402 int new_buffer;
1403 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001404 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001405 int nr;
1406 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001407
Bram Moolenaar79648732019-07-18 21:43:07 +02001408 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001409 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001410 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001411 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001412 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001413 buf = buflist_findnr( argvars[0].vval.v_number);
1414 if (buf == NULL)
1415 {
1416 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1417 return NULL;
1418 }
1419 }
1420 else if (!(argvars[0].v_type == VAR_STRING
1421 && argvars[0].vval.v_string != NULL)
1422 && !(argvars[0].v_type == VAR_LIST
1423 && argvars[0].vval.v_list != NULL))
1424 {
1425 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001426 return NULL;
1427 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001428 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001429 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001430 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001431 return NULL;
1432 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001433 d = argvars[1].vval.v_dict;
1434 }
1435
1436 if (d != NULL)
1437 {
1438 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1439 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1440 else if (type == TYPE_NOTIFICATION)
1441 tabnr = -1; // notifications are global by default
1442 else
1443 tabnr = 0;
1444 if (tabnr > 0)
1445 {
1446 tp = find_tabpage(tabnr);
1447 if (tp == NULL)
1448 {
1449 semsg(_("E997: Tabpage not found: %d"), tabnr);
1450 return NULL;
1451 }
1452 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001453 }
1454
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001455 // Create the window and buffer.
1456 wp = win_alloc_popup_win();
1457 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001458 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001459 if (rettv != NULL)
1460 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001461 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001462 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001463
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001464 if (buf != NULL)
1465 {
1466 // use existing buffer
1467 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001468 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001469 buffer_ensure_loaded(buf);
1470 }
1471 else
1472 {
1473 // create a new buffer associated with the popup
1474 new_buffer = TRUE;
1475 buf = buflist_new(NULL, NULL, (linenr_T)0,
1476 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1477 if (buf == NULL)
1478 return NULL;
1479 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001480
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001481 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001482
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001483 set_local_options_default(wp);
1484 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001485 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001486 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001487 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001488 buf->b_p_ul = -1; // no undo
1489 buf->b_p_swf = FALSE; // no swap file
1490 buf->b_p_bl = FALSE; // unlisted buffer
1491 buf->b_locked = TRUE;
1492 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001493
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001494 // Avoid that 'buftype' is reset when this buffer is entered.
1495 buf->b_p_initialized = TRUE;
1496 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001497
Bram Moolenaara3fce622019-06-20 02:31:49 +02001498 if (tp != NULL)
1499 {
1500 // popup on specified tab page
1501 wp->w_next = tp->tp_first_popupwin;
1502 tp->tp_first_popupwin = wp;
1503 }
1504 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001505 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001506 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001507 wp->w_next = curtab->tp_first_popupwin;
1508 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001509 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001510 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001511 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001512 win_T *prev = first_popupwin;
1513
1514 // Global popup: add at the end, so that it gets displayed on top of
1515 // older ones with the same zindex. Matters for notifications.
1516 if (first_popupwin == NULL)
1517 first_popupwin = wp;
1518 else
1519 {
1520 while (prev->w_next != NULL)
1521 prev = prev->w_next;
1522 prev->w_next = wp;
1523 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001524 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001525
Bram Moolenaar79648732019-07-18 21:43:07 +02001526 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001527 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001528
Bram Moolenaar79648732019-07-18 21:43:07 +02001529 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001530 {
1531 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001532 }
1533 if (type == TYPE_ATCURSOR)
1534 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001535 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001536 set_moved_values(wp);
1537 set_moved_columns(wp, FIND_STRING);
1538 }
1539
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001540 if (type == TYPE_BEVAL)
1541 {
1542 wp->w_popup_pos = POPPOS_BOTLEFT;
1543
1544 // by default use the mouse position
1545 wp->w_wantline = mouse_row;
1546 if (wp->w_wantline <= 0) // mouse on first line
1547 {
1548 wp->w_wantline = 2;
1549 wp->w_popup_pos = POPPOS_TOPLEFT;
1550 }
1551 wp->w_wantcol = mouse_col + 1;
1552 set_mousemoved_values(wp);
1553 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1554 }
1555
Bram Moolenaar33796b32019-06-08 16:01:13 +02001556 // set default values
1557 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001558 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001559
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001560 if (type == TYPE_NOTIFICATION)
1561 {
1562 win_T *twp, *nextwin;
1563 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001564
1565 // Try to not overlap with another global popup. Guess we need 3
1566 // more screen lines than buffer lines.
1567 wp->w_wantline = 1;
1568 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1569 {
1570 nextwin = twp->w_next;
1571 if (twp != wp
1572 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1573 && twp->w_winrow <= wp->w_wantline - 1 + height
1574 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1575 {
1576 // move to below this popup and restart the loop to check for
1577 // overlap with other popups
1578 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1579 nextwin = first_popupwin;
1580 }
1581 }
1582 if (wp->w_wantline + height > Rows)
1583 {
1584 // can't avoid overlap, put on top in the hope that message goes
1585 // away soon.
1586 wp->w_wantline = 1;
1587 }
1588
1589 wp->w_wantcol = 10;
1590 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001591 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001592 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001593 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001594 for (i = 0; i < 4; ++i)
1595 wp->w_popup_border[i] = 1;
1596 wp->w_popup_padding[1] = 1;
1597 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001598
1599 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001600 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001601 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1602 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001603 }
1604
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001606 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001607 wp->w_popup_pos = POPPOS_CENTER;
1608 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001609 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001610 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001611 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001612 }
1613
Bram Moolenaara730e552019-06-16 19:05:31 +02001614 if (type == TYPE_MENU)
1615 {
1616 typval_T tv;
1617 callback_T callback;
1618
1619 tv.v_type = VAR_STRING;
1620 tv.vval.v_string = (char_u *)"popup_filter_menu";
1621 callback = get_callback(&tv);
1622 if (callback.cb_name != NULL)
1623 set_callback(&wp->w_filter_cb, &callback);
1624
1625 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001626 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001627 }
1628
Bram Moolenaar79648732019-07-18 21:43:07 +02001629 if (type == TYPE_PREVIEW)
1630 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001631 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001632 wp->w_popup_close = POPCLOSE_BUTTON;
1633 for (i = 0; i < 4; ++i)
1634 wp->w_popup_border[i] = 1;
1635 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001636 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1637 }
1638 if (type == TYPE_INFO)
1639 {
1640 wp->w_popup_pos = POPPOS_TOPLEFT;
1641 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1642 wp->w_popup_close = POPCLOSE_BUTTON;
1643 add_border_left_right_padding(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001644 }
1645
Bram Moolenaarae943152019-06-16 22:54:14 +02001646 for (i = 0; i < 4; ++i)
1647 VIM_CLEAR(wp->w_border_highlight[i]);
1648 for (i = 0; i < 8; ++i)
1649 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001650 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001651 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001652
Bram Moolenaar79648732019-07-18 21:43:07 +02001653 if (d != NULL)
1654 // Deal with options.
1655 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001656
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001657#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001658 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1659 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001660#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001661
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001662 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001663
1664 wp->w_vsep_width = 0;
1665
1666 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001667 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001668
1669 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001670}
1671
1672/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001673 * popup_clear()
1674 */
1675 void
1676f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1677{
1678 close_all_popups();
1679}
1680
1681/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001682 * popup_create({text}, {options})
1683 */
1684 void
1685f_popup_create(typval_T *argvars, typval_T *rettv)
1686{
Bram Moolenaar17627312019-06-02 19:53:44 +02001687 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001688}
1689
1690/*
1691 * popup_atcursor({text}, {options})
1692 */
1693 void
1694f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1695{
Bram Moolenaar17627312019-06-02 19:53:44 +02001696 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001697}
1698
1699/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001700 * popup_beval({text}, {options})
1701 */
1702 void
1703f_popup_beval(typval_T *argvars, typval_T *rettv)
1704{
1705 popup_create(argvars, rettv, TYPE_BEVAL);
1706}
1707
1708/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001709 * Invoke the close callback for window "wp" with value "result".
1710 * Careful: The callback may make "wp" invalid!
1711 */
1712 static void
1713invoke_popup_callback(win_T *wp, typval_T *result)
1714{
1715 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001716 typval_T argv[3];
1717
1718 argv[0].v_type = VAR_NUMBER;
1719 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1720
1721 if (result != NULL && result->v_type != VAR_UNKNOWN)
1722 copy_tv(result, &argv[1]);
1723 else
1724 {
1725 argv[1].v_type = VAR_NUMBER;
1726 argv[1].vval.v_number = 0;
1727 }
1728
1729 argv[2].v_type = VAR_UNKNOWN;
1730
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001731 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001732 if (result != NULL)
1733 clear_tv(&argv[1]);
1734 clear_tv(&rettv);
1735}
1736
1737/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001738 * Close popup "wp" and invoke any close callback for it.
1739 */
1740 static void
1741popup_close_and_callback(win_T *wp, typval_T *arg)
1742{
1743 int id = wp->w_id;
1744
1745 if (wp->w_close_cb.cb_name != NULL)
1746 // Careful: This may make "wp" invalid.
1747 invoke_popup_callback(wp, arg);
1748
1749 popup_close(id);
1750}
1751
1752/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001753 * Close popup "wp" because of a mouse click.
1754 */
1755 void
1756popup_close_for_mouse_click(win_T *wp)
1757{
1758 typval_T res;
1759
1760 res.v_type = VAR_NUMBER;
1761 res.vval.v_number = -2;
1762 popup_close_and_callback(wp, &res);
1763}
1764
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001765 static void
1766check_mouse_moved(win_T *wp, win_T *mouse_wp)
1767{
1768 // Close the popup when all if these are true:
1769 // - the mouse is not on this popup
1770 // - "mousemoved" was used
1771 // - the mouse is no longer on the same screen row or the mouse column is
1772 // outside of the relevant text
1773 if (wp != mouse_wp
1774 && wp->w_popup_mouse_row != 0
1775 && (wp->w_popup_mouse_row != mouse_row
1776 || mouse_col < wp->w_popup_mouse_mincol
1777 || mouse_col > wp->w_popup_mouse_maxcol))
1778 {
1779 typval_T res;
1780
1781 res.v_type = VAR_NUMBER;
1782 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001783 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001784 popup_close_and_callback(wp, &res);
1785 }
1786}
1787
1788/*
1789 * Called when the mouse moved: may close a popup with "mousemoved".
1790 */
1791 void
1792popup_handle_mouse_moved(void)
1793{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001794 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001795 win_T *mouse_wp;
1796 int row = mouse_row;
1797 int col = mouse_col;
1798
1799 // find the window where the mouse is in
1800 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1801
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001802 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1803 {
1804 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001805 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001806 }
1807 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1808 {
1809 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001810 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001811 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001812}
1813
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001814/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001815 * In a filter: check if the typed key is a mouse event that is used for
1816 * dragging the popup.
1817 */
1818 static void
1819filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1820{
1821 int row = mouse_row;
1822 int col = mouse_col;
1823
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001824 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001825 && is_mouse_key(c)
1826 && (wp == popup_dragwin
1827 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1828 // do not consume the key, allow for dragging the popup
1829 rettv->vval.v_number = 0;
1830}
1831
Bram Moolenaara730e552019-06-16 19:05:31 +02001832/*
1833 * popup_filter_menu({text}, {options})
1834 */
1835 void
1836f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1837{
1838 int id = tv_get_number(&argvars[0]);
1839 win_T *wp = win_id2wp(id);
1840 char_u *key = tv_get_string(&argvars[1]);
1841 typval_T res;
1842 int c;
1843 linenr_T old_lnum;
1844
1845 // If the popup has been closed do not consume the key.
1846 if (wp == NULL)
1847 return;
1848
1849 c = *key;
1850 if (c == K_SPECIAL && key[1] != NUL)
1851 c = TO_SPECIAL(key[1], key[2]);
1852
1853 // consume all keys until done
1854 rettv->vval.v_number = 1;
1855 res.v_type = VAR_NUMBER;
1856
1857 old_lnum = wp->w_cursor.lnum;
1858 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1859 --wp->w_cursor.lnum;
1860 if ((c == 'j' || c == 'J' || c == K_DOWN)
1861 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1862 ++wp->w_cursor.lnum;
1863 if (old_lnum != wp->w_cursor.lnum)
1864 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001865 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001866 return;
1867 }
1868
1869 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1870 {
1871 // Cancelled, invoke callback with -1
1872 res.vval.v_number = -1;
1873 popup_close_and_callback(wp, &res);
1874 return;
1875 }
1876 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1877 {
1878 // Invoke callback with current index.
1879 res.vval.v_number = wp->w_cursor.lnum;
1880 popup_close_and_callback(wp, &res);
1881 return;
1882 }
1883
1884 filter_handle_drag(wp, c, rettv);
1885}
1886
1887/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001888 * popup_filter_yesno({text}, {options})
1889 */
1890 void
1891f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1892{
1893 int id = tv_get_number(&argvars[0]);
1894 win_T *wp = win_id2wp(id);
1895 char_u *key = tv_get_string(&argvars[1]);
1896 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001897 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001898
1899 // If the popup has been closed don't consume the key.
1900 if (wp == NULL)
1901 return;
1902
Bram Moolenaara730e552019-06-16 19:05:31 +02001903 c = *key;
1904 if (c == K_SPECIAL && key[1] != NUL)
1905 c = TO_SPECIAL(key[1], key[2]);
1906
Bram Moolenaara42d9452019-06-15 21:46:30 +02001907 // consume all keys until done
1908 rettv->vval.v_number = 1;
1909
Bram Moolenaara730e552019-06-16 19:05:31 +02001910 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001911 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001912 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001913 res.vval.v_number = 0;
1914 else
1915 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001916 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001917 return;
1918 }
1919
1920 // Invoke callback
1921 res.v_type = VAR_NUMBER;
1922 popup_close_and_callback(wp, &res);
1923}
1924
1925/*
1926 * popup_dialog({text}, {options})
1927 */
1928 void
1929f_popup_dialog(typval_T *argvars, typval_T *rettv)
1930{
1931 popup_create(argvars, rettv, TYPE_DIALOG);
1932}
1933
1934/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001935 * popup_menu({text}, {options})
1936 */
1937 void
1938f_popup_menu(typval_T *argvars, typval_T *rettv)
1939{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001940 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001941}
1942
1943/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001944 * popup_notification({text}, {options})
1945 */
1946 void
1947f_popup_notification(typval_T *argvars, typval_T *rettv)
1948{
1949 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1950}
1951
1952/*
1953 * Find the popup window with window-ID "id".
1954 * If the popup window does not exist NULL is returned.
1955 * If the window is not a popup window, and error message is given.
1956 */
1957 static win_T *
1958find_popup_win(int id)
1959{
1960 win_T *wp = win_id2wp(id);
1961
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001962 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02001963 {
1964 semsg(_("E993: window %d is not a popup window"), id);
1965 return NULL;
1966 }
1967 return wp;
1968}
1969
1970/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001971 * popup_close({id})
1972 */
1973 void
1974f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
1975{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001976 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001977 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001978
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001979 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001980 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001981}
1982
1983/*
1984 * popup_hide({id})
1985 */
1986 void
1987f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
1988{
1989 int id = (int)tv_get_number(argvars);
1990 win_T *wp = find_popup_win(id);
1991
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001992 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001993 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001994 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001995 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001996 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001997 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001998 }
1999}
2000
2001/*
2002 * popup_show({id})
2003 */
2004 void
2005f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2006{
2007 int id = (int)tv_get_number(argvars);
2008 win_T *wp = find_popup_win(id);
2009
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002010 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002011 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002012 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02002013 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002014 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002015 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002016 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002017}
2018
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002019/*
2020 * popup_settext({id}, {text})
2021 */
2022 void
2023f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2024{
2025 int id = (int)tv_get_number(&argvars[0]);
2026 win_T *wp = find_popup_win(id);
2027
2028 if (wp != NULL)
2029 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002030 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2031 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2032 else
2033 {
2034 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002035 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002036 popup_adjust_position(wp);
2037 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002038 }
2039}
2040
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002041 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002042popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002043{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002044 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002045 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002046 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002047 clear_cmdline = TRUE;
2048 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002049
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002050 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002051 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002052}
2053
Bram Moolenaarec583842019-05-26 14:11:23 +02002054/*
2055 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002056 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002057 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002058 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002059popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002060{
2061 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002062 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002063 win_T *prev = NULL;
2064
Bram Moolenaarec583842019-05-26 14:11:23 +02002065 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002066 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002067 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002068 {
2069 if (prev == NULL)
2070 first_popupwin = wp->w_next;
2071 else
2072 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002073 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002074 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002075 }
2076
Bram Moolenaarec583842019-05-26 14:11:23 +02002077 // go through tab-local popups
2078 FOR_ALL_TABPAGES(tp)
2079 popup_close_tabpage(tp, id);
2080}
2081
2082/*
2083 * Close a popup window with Window-id "id" in tabpage "tp".
2084 */
2085 void
2086popup_close_tabpage(tabpage_T *tp, int id)
2087{
2088 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002089 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002090 win_T *prev = NULL;
2091
Bram Moolenaarec583842019-05-26 14:11:23 +02002092 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2093 if (wp->w_id == id)
2094 {
2095 if (prev == NULL)
2096 *root = wp->w_next;
2097 else
2098 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002099 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002100 return;
2101 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002102}
2103
2104 void
2105close_all_popups(void)
2106{
2107 while (first_popupwin != NULL)
2108 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002109 while (curtab->tp_first_popupwin != NULL)
2110 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002111}
2112
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002113/*
2114 * popup_move({id}, {options})
2115 */
2116 void
2117f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2118{
Bram Moolenaarae943152019-06-16 22:54:14 +02002119 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002120 int id = (int)tv_get_number(argvars);
2121 win_T *wp = find_popup_win(id);
2122
2123 if (wp == NULL)
2124 return; // invalid {id}
2125
2126 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2127 {
2128 emsg(_(e_dictreq));
2129 return;
2130 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002131 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002132
Bram Moolenaarae943152019-06-16 22:54:14 +02002133 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002134
2135 if (wp->w_winrow + wp->w_height >= cmdline_row)
2136 clear_cmdline = TRUE;
2137 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002138}
2139
Bram Moolenaarbc133542019-05-29 20:26:46 +02002140/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002141 * popup_setoptions({id}, {options})
2142 */
2143 void
2144f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2145{
2146 dict_T *dict;
2147 int id = (int)tv_get_number(argvars);
2148 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002149 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002150
2151 if (wp == NULL)
2152 return; // invalid {id}
2153
2154 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2155 {
2156 emsg(_(e_dictreq));
2157 return;
2158 }
2159 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002160 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002161
2162 apply_move_options(wp, dict);
2163 apply_general_options(wp, dict);
2164
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002165 if (old_firstline != wp->w_firstline)
2166 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002167 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002168 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002169 popup_adjust_position(wp);
2170}
2171
2172/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002173 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002174 */
2175 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002176f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002177{
2178 dict_T *dict;
2179 int id = (int)tv_get_number(argvars);
2180 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002181 int top_extra;
2182 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002183
2184 if (rettv_dict_alloc(rettv) == OK)
2185 {
2186 if (wp == NULL)
2187 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002188 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002189 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2190
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002191 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002192 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002193 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002194
Bram Moolenaarbc133542019-05-29 20:26:46 +02002195 dict_add_number(dict, "line", wp->w_winrow + 1);
2196 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002197 dict_add_number(dict, "width", wp->w_width + left_extra
2198 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2199 dict_add_number(dict, "height", wp->w_height + top_extra
2200 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002201
2202 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2203 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2204 dict_add_number(dict, "core_width", wp->w_width);
2205 dict_add_number(dict, "core_height", wp->w_height);
2206
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002207 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002208 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002209 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002210 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002211
2212 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002213 }
2214}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002215/*
2216 * popup_locate({row}, {col})
2217 */
2218 void
2219f_popup_locate(typval_T *argvars, typval_T *rettv)
2220{
2221 int row = tv_get_number(&argvars[0]) - 1;
2222 int col = tv_get_number(&argvars[1]) - 1;
2223 win_T *wp;
2224
2225 wp = mouse_find_win(&row, &col, FIND_POPUP);
2226 if (WIN_IS_POPUP(wp))
2227 rettv->vval.v_number = wp->w_id;
2228}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002229
2230/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002231 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2232 */
2233 static void
2234get_padding_border(dict_T *dict, int *array, char *name)
2235{
2236 list_T *list;
2237 int i;
2238
2239 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2240 return;
2241
2242 list = list_alloc();
2243 if (list != NULL)
2244 {
2245 dict_add_list(dict, name, list);
2246 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2247 for (i = 0; i < 4; ++i)
2248 list_append_number(list, array[i]);
2249 }
2250}
2251
2252/*
2253 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2254 */
2255 static void
2256get_borderhighlight(dict_T *dict, win_T *wp)
2257{
2258 list_T *list;
2259 int i;
2260
2261 for (i = 0; i < 4; ++i)
2262 if (wp->w_border_highlight[i] != NULL)
2263 break;
2264 if (i == 4)
2265 return;
2266
2267 list = list_alloc();
2268 if (list != NULL)
2269 {
2270 dict_add_list(dict, "borderhighlight", list);
2271 for (i = 0; i < 4; ++i)
2272 list_append_string(list, wp->w_border_highlight[i], -1);
2273 }
2274}
2275
2276/*
2277 * For popup_getoptions(): add a "borderchars" entry to "dict".
2278 */
2279 static void
2280get_borderchars(dict_T *dict, win_T *wp)
2281{
2282 list_T *list;
2283 int i;
2284 char_u buf[NUMBUFLEN];
2285 int len;
2286
2287 for (i = 0; i < 8; ++i)
2288 if (wp->w_border_char[i] != 0)
2289 break;
2290 if (i == 8)
2291 return;
2292
2293 list = list_alloc();
2294 if (list != NULL)
2295 {
2296 dict_add_list(dict, "borderchars", list);
2297 for (i = 0; i < 8; ++i)
2298 {
2299 len = mb_char2bytes(wp->w_border_char[i], buf);
2300 list_append_string(list, buf, len);
2301 }
2302 }
2303}
2304
2305/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002306 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002307 */
2308 static void
2309get_moved_list(dict_T *dict, win_T *wp)
2310{
2311 list_T *list;
2312
2313 list = list_alloc();
2314 if (list != NULL)
2315 {
2316 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002317 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002318 list_append_number(list, wp->w_popup_mincol);
2319 list_append_number(list, wp->w_popup_maxcol);
2320 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002321 list = list_alloc();
2322 if (list != NULL)
2323 {
2324 dict_add_list(dict, "mousemoved", list);
2325 list_append_number(list, wp->w_popup_mouse_row);
2326 list_append_number(list, wp->w_popup_mouse_mincol);
2327 list_append_number(list, wp->w_popup_mouse_maxcol);
2328 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002329}
2330
2331/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002332 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002333 */
2334 void
2335f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2336{
2337 dict_T *dict;
2338 int id = (int)tv_get_number(argvars);
2339 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002340 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002341 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002342
2343 if (rettv_dict_alloc(rettv) == OK)
2344 {
2345 if (wp == NULL)
2346 return;
2347
2348 dict = rettv->vval.v_dict;
2349 dict_add_number(dict, "line", wp->w_wantline);
2350 dict_add_number(dict, "col", wp->w_wantcol);
2351 dict_add_number(dict, "minwidth", wp->w_minwidth);
2352 dict_add_number(dict, "minheight", wp->w_minheight);
2353 dict_add_number(dict, "maxheight", wp->w_maxheight);
2354 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002355 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002356 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002357 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002358 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002359 dict_add_string(dict, "title", wp->w_popup_title);
2360 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002361 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02002362 dict_add_number(dict, "mapping", (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002363 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002364 dict_add_number(dict, "cursorline",
2365 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002366 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002367 if (wp->w_scrollbar_highlight != NULL)
2368 dict_add_string(dict, "scrollbarhighlight",
2369 wp->w_scrollbar_highlight);
2370 if (wp->w_thumb_highlight != NULL)
2371 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002372
Bram Moolenaara3fce622019-06-20 02:31:49 +02002373 // find the tabpage that holds this popup
2374 i = 1;
2375 FOR_ALL_TABPAGES(tp)
2376 {
2377 win_T *p;
2378
2379 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2380 if (p->w_id == id)
2381 break;
2382 if (p != NULL)
2383 break;
2384 ++i;
2385 }
2386 if (tp == NULL)
2387 i = -1; // must be global
2388 else if (tp == curtab)
2389 i = 0;
2390 dict_add_number(dict, "tabpage", i);
2391
Bram Moolenaarae943152019-06-16 22:54:14 +02002392 get_padding_border(dict, wp->w_popup_padding, "padding");
2393 get_padding_border(dict, wp->w_popup_border, "border");
2394 get_borderhighlight(dict, wp);
2395 get_borderchars(dict, wp);
2396 get_moved_list(dict, wp);
2397
2398 if (wp->w_filter_cb.cb_name != NULL)
2399 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2400 if (wp->w_close_cb.cb_name != NULL)
2401 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002402
2403 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2404 ++i)
2405 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2406 {
2407 dict_add_string(dict, "pos",
2408 (char_u *)poppos_entries[i].pp_name);
2409 break;
2410 }
2411
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002412 dict_add_string(dict, "close", (char_u *)(
2413 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2414 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2415
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002416# if defined(FEAT_TIMERS)
2417 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2418 ? (long)wp->w_popup_timer->tr_interval : 0L);
2419# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002420 }
2421}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002422
2423 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002424error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002425{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002426 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002427 {
2428 emsg(_("E994: Not allowed in a popup window"));
2429 return TRUE;
2430 }
2431 return FALSE;
2432}
2433
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002434/*
2435 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002436 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002437 */
2438 void
2439popup_reset_handled()
2440{
2441 win_T *wp;
2442
2443 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2444 wp->w_popup_flags &= ~POPF_HANDLED;
2445 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2446 wp->w_popup_flags &= ~POPF_HANDLED;
2447}
2448
2449/*
2450 * Find the next visible popup where POPF_HANDLED is not set.
2451 * Must have called popup_reset_handled() first.
2452 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2453 * popup with the highest zindex.
2454 */
2455 win_T *
2456find_next_popup(int lowest)
2457{
2458 win_T *wp;
2459 win_T *found_wp;
2460 int found_zindex;
2461
2462 found_zindex = lowest ? INT_MAX : 0;
2463 found_wp = NULL;
2464 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2465 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2466 && (lowest ? wp->w_zindex < found_zindex
2467 : wp->w_zindex > found_zindex))
2468 {
2469 found_zindex = wp->w_zindex;
2470 found_wp = wp;
2471 }
2472 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2473 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2474 && (lowest ? wp->w_zindex < found_zindex
2475 : wp->w_zindex > found_zindex))
2476 {
2477 found_zindex = wp->w_zindex;
2478 found_wp = wp;
2479 }
2480
2481 if (found_wp != NULL)
2482 found_wp->w_popup_flags |= POPF_HANDLED;
2483 return found_wp;
2484}
2485
2486/*
2487 * Invoke the filter callback for window "wp" with typed character "c".
2488 * Uses the global "mod_mask" for modifiers.
2489 * Returns the return value of the filter.
2490 * Careful: The filter may make "wp" invalid!
2491 */
2492 static int
2493invoke_popup_filter(win_T *wp, int c)
2494{
2495 int res;
2496 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002497 typval_T argv[3];
2498 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002499 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002500
Bram Moolenaara42d9452019-06-15 21:46:30 +02002501 // Emergency exit: CTRL-C closes the popup.
2502 if (c == Ctrl_C)
2503 {
2504 rettv.v_type = VAR_NUMBER;
2505 rettv.vval.v_number = -1;
2506 popup_close_and_callback(wp, &rettv);
2507 return 1;
2508 }
2509
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002510 argv[0].v_type = VAR_NUMBER;
2511 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2512
2513 // Convert the number to a string, so that the function can use:
2514 // if a:c == "\<F2>"
2515 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2516 argv[1].v_type = VAR_STRING;
2517 argv[1].vval.v_string = vim_strsave(buf);
2518
2519 argv[2].v_type = VAR_UNKNOWN;
2520
Bram Moolenaara42d9452019-06-15 21:46:30 +02002521 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002522 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002523 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002524 popup_highlight_curline(wp);
2525
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002526 res = tv_get_number(&rettv);
2527 vim_free(argv[1].vval.v_string);
2528 clear_tv(&rettv);
2529 return res;
2530}
2531
2532/*
2533 * Called when "c" was typed: invoke popup filter callbacks.
2534 * Returns TRUE when the character was consumed,
2535 */
2536 int
2537popup_do_filter(int c)
2538{
2539 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002540 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002541
2542 popup_reset_handled();
2543
2544 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2545 if (wp->w_filter_cb.cb_name != NULL)
2546 res = invoke_popup_filter(wp, c);
2547
2548 return res;
2549}
2550
Bram Moolenaar3397f742019-06-02 18:40:06 +02002551/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002552 * Return TRUE if there is a popup visible with a filter callback and the
2553 * "mapping" property off.
2554 */
2555 int
2556popup_no_mapping(void)
2557{
2558 int round;
2559 win_T *wp;
2560
2561 for (round = 1; round <= 2; ++round)
2562 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2563 wp != NULL; wp = wp->w_next)
2564 if (wp->w_filter_cb.cb_name != NULL
2565 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2566 return TRUE;
2567 return FALSE;
2568}
2569
2570/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002571 * Called when the cursor moved: check if any popup needs to be closed if the
2572 * cursor moved far enough.
2573 */
2574 void
2575popup_check_cursor_pos()
2576{
2577 win_T *wp;
2578 typval_T tv;
2579
2580 popup_reset_handled();
2581 while ((wp = find_next_popup(TRUE)) != NULL)
2582 if (wp->w_popup_curwin != NULL
2583 && (curwin != wp->w_popup_curwin
2584 || curwin->w_cursor.lnum != wp->w_popup_lnum
2585 || curwin->w_cursor.col < wp->w_popup_mincol
2586 || curwin->w_cursor.col > wp->w_popup_maxcol))
2587 {
2588 tv.v_type = VAR_NUMBER;
2589 tv.vval.v_number = -1;
2590 popup_close_and_callback(wp, &tv);
2591 }
2592}
2593
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002594/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002595 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002596 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002597 static void
2598popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002599{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002600 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002601 char_u *cells;
2602 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002603
2604 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002605 return;
2606 if (wp->w_popup_mask_cells != NULL
2607 && wp->w_popup_mask_height == height
2608 && wp->w_popup_mask_width == width)
2609 return; // cache is still valid
2610
2611 vim_free(wp->w_popup_mask_cells);
2612 wp->w_popup_mask_cells = alloc_clear(width * height);
2613 if (wp->w_popup_mask_cells == NULL)
2614 return;
2615 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002616
2617 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2618 {
2619 int cols, cole;
2620 int lines, linee;
2621
2622 li = lio->li_tv.vval.v_list->lv_first;
2623 cols = tv_get_number(&li->li_tv);
2624 if (cols < 0)
2625 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002626 li = li->li_next;
2627 cole = tv_get_number(&li->li_tv);
2628 if (cole < 0)
2629 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002630 li = li->li_next;
2631 lines = tv_get_number(&li->li_tv);
2632 if (lines < 0)
2633 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002634 li = li->li_next;
2635 linee = tv_get_number(&li->li_tv);
2636 if (linee < 0)
2637 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002638
2639 for (row = lines - 1; row < linee && row < height; ++row)
2640 for (col = cols - 1; col < cole && col < width; ++col)
2641 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002642 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002643}
2644
2645/*
2646 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2647 * "col" and "line" are screen coordinates.
2648 */
2649 static int
2650popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2651{
2652 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2653 int line = screenline - wp->w_winrow;
2654
2655 return col >= 0 && col < width
2656 && line >= 0 && line < height
2657 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002658}
2659
2660/*
2661 * Set flags in popup_transparent[] for window "wp" to "val".
2662 */
2663 static void
2664update_popup_transparent(win_T *wp, int val)
2665{
2666 if (wp->w_popup_mask != NULL)
2667 {
2668 int width = popup_width(wp);
2669 int height = popup_height(wp);
2670 listitem_T *lio, *li;
2671 int cols, cole;
2672 int lines, linee;
2673 int col, line;
2674
2675 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2676 {
2677 li = lio->li_tv.vval.v_list->lv_first;
2678 cols = tv_get_number(&li->li_tv);
2679 if (cols < 0)
2680 cols = width + cols + 1;
2681 li = li->li_next;
2682 cole = tv_get_number(&li->li_tv);
2683 if (cole < 0)
2684 cole = width + cole + 1;
2685 li = li->li_next;
2686 lines = tv_get_number(&li->li_tv);
2687 if (lines < 0)
2688 lines = height + lines + 1;
2689 li = li->li_next;
2690 linee = tv_get_number(&li->li_tv);
2691 if (linee < 0)
2692 linee = height + linee + 1;
2693
2694 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002695 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002696 if (cols < 0)
2697 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002698 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002699 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002700 if (lines < 0)
2701 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002702 for (line = lines; line < linee
2703 && line + wp->w_winrow < screen_Rows; ++line)
2704 for (col = cols; col < cole
2705 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002706 popup_transparent[(line + wp->w_winrow) * screen_Columns
2707 + col + wp->w_wincol] = val;
2708 }
2709 }
2710}
2711
2712/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002713 * Update "popup_mask" if needed.
2714 * Also recomputes the popup size and positions.
2715 * Also updates "popup_visible".
2716 * Also marks window lines for redrawing.
2717 */
2718 void
2719may_update_popup_mask(int type)
2720{
2721 win_T *wp;
2722 short *mask;
2723 int line, col;
2724 int redraw_all = FALSE;
2725
2726 // Need to recompute when switching tabs.
2727 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2728 // (such as the screen size) must have changed.
2729 if (popup_mask_tab != curtab || type >= NOT_VALID)
2730 {
2731 popup_mask_refresh = TRUE;
2732 redraw_all = TRUE;
2733 }
2734 if (!popup_mask_refresh)
2735 {
2736 // Check if any buffer has changed.
2737 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2738 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2739 popup_mask_refresh = TRUE;
2740 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2741 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2742 popup_mask_refresh = TRUE;
2743 if (!popup_mask_refresh)
2744 return;
2745 }
2746
2747 // Need to update the mask, something has changed.
2748 popup_mask_refresh = FALSE;
2749 popup_mask_tab = curtab;
2750 popup_visible = FALSE;
2751
2752 // If redrawing everything, just update "popup_mask".
2753 // If redrawing only what is needed, update "popup_mask_next" and then
2754 // compare with "popup_mask" to see what changed.
2755 if (type >= SOME_VALID)
2756 mask = popup_mask;
2757 else
2758 mask = popup_mask_next;
2759 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2760
2761 // Find the window with the lowest zindex that hasn't been handled yet,
2762 // so that the window with a higher zindex overwrites the value in
2763 // popup_mask.
2764 popup_reset_handled();
2765 while ((wp = find_next_popup(TRUE)) != NULL)
2766 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002767 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002768 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002769
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002770 popup_visible = TRUE;
2771
2772 // Recompute the position if the text changed.
2773 if (redraw_all
2774 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2775 popup_adjust_position(wp);
2776
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002777 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002778 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002779 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002780 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002781 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002782 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002783 col < wp->w_wincol + width - wp->w_popup_leftoff
2784 && col < screen_Columns; ++col)
2785 if (wp->w_popup_mask_cells == NULL
2786 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002787 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002788 }
2789
2790 // Only check which lines are to be updated if not already
2791 // updating all lines.
2792 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002793 {
2794 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2795 win_T *prev_wp = NULL;
2796
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002797 for (line = 0; line < screen_Rows; ++line)
2798 {
2799 int col_done = 0;
2800
2801 for (col = 0; col < screen_Columns; ++col)
2802 {
2803 int off = line * screen_Columns + col;
2804
2805 if (popup_mask[off] != popup_mask_next[off])
2806 {
2807 popup_mask[off] = popup_mask_next[off];
2808
2809 if (line >= cmdline_row)
2810 {
2811 // the command line needs to be cleared if text below
2812 // the popup is now visible.
2813 if (!msg_scrolled && popup_mask_next[off] == 0)
2814 clear_cmdline = TRUE;
2815 }
2816 else if (col >= col_done)
2817 {
2818 linenr_T lnum;
2819 int line_cp = line;
2820 int col_cp = col;
2821
2822 // The screen position "line" / "col" needs to be
2823 // redrawn. Figure out what window that is and update
2824 // w_redraw_top and w_redr_bot. Only needs to be done
2825 // once for each window line.
2826 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2827 if (wp != NULL)
2828 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002829 if (wp != prev_wp)
2830 {
2831 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2832 prev_wp = wp;
2833 }
2834
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002835 if (line_cp >= wp->w_height)
2836 // In (or below) status line
2837 wp->w_redr_status = TRUE;
2838 // compute the position in the buffer line from the
2839 // position on the screen
2840 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002841 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002842 // past bottom
2843 wp->w_redr_status = TRUE;
2844 else
2845 redrawWinline(wp, lnum);
2846
2847 // This line is going to be redrawn, no need to
2848 // check until the right side of the window.
2849 col_done = wp->w_wincol + wp->w_width - 1;
2850 }
2851 }
2852 }
2853 }
2854 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002855
2856 vim_free(plines_cache);
2857 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002858}
2859
2860/*
2861 * Return a string of "len" spaces in IObuff.
2862 */
2863 static char_u *
2864get_spaces(int len)
2865{
2866 vim_memset(IObuff, ' ', (size_t)len);
2867 IObuff[len] = NUL;
2868 return IObuff;
2869}
2870
2871/*
2872 * Update popup windows. They are drawn on top of normal windows.
2873 * "win_update" is called for each popup window, lowest zindex first.
2874 */
2875 void
2876update_popups(void (*win_update)(win_T *wp))
2877{
2878 win_T *wp;
2879 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002880 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002881 int total_width;
2882 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002883 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002884 int popup_attr;
2885 int border_attr[4];
2886 int border_char[8];
2887 char_u buf[MB_MAXBYTES];
2888 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002889 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002890 int padcol = 0;
2891 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002892 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002893 int sb_thumb_top = 0;
2894 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002895 int attr_scroll = 0;
2896 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002897
2898 // Find the window with the lowest zindex that hasn't been updated yet,
2899 // so that the window with a higher zindex is drawn later, thus goes on
2900 // top.
2901 popup_reset_handled();
2902 while ((wp = find_next_popup(TRUE)) != NULL)
2903 {
2904 // This drawing uses the zindex of the popup window, so that it's on
2905 // top of the text but doesn't draw when another popup with higher
2906 // zindex is on top of the character.
2907 screen_zindex = wp->w_zindex;
2908
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002909 // Set flags in popup_transparent[] for masked cells.
2910 update_popup_transparent(wp, 1);
2911
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002912 // adjust w_winrow and w_wincol for border and padding, since
2913 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002914 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002915 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2916 - wp->w_popup_leftoff;
2917 if (wp->w_wincol + left_extra < 0)
2918 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002919 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002920 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002921
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002922 // Draw the popup text, unless it's off screen.
2923 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2924 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002925
2926 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002927 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002928
Bram Moolenaard529ba52019-07-02 23:13:53 +02002929 total_width = popup_width(wp);
2930 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002931 popup_attr = get_wcr_attr(wp);
2932
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002933 if (wp->w_winrow + total_height > cmdline_row)
2934 wp->w_popup_flags |= POPF_ON_CMDLINE;
2935 else
2936 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2937
2938
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002939 // We can only use these line drawing characters when 'encoding' is
2940 // "utf-8" and 'ambiwidth' is "single".
2941 if (enc_utf8 && *p_ambw == 's')
2942 {
2943 border_char[0] = border_char[2] = 0x2550;
2944 border_char[1] = border_char[3] = 0x2551;
2945 border_char[4] = 0x2554;
2946 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002947 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
2948 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002949 border_char[7] = 0x255a;
2950 }
2951 else
2952 {
2953 border_char[0] = border_char[2] = '-';
2954 border_char[1] = border_char[3] = '|';
2955 for (i = 4; i < 8; ++i)
2956 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002957 if (wp->w_popup_flags & POPF_RESIZE)
2958 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002959 }
2960 for (i = 0; i < 8; ++i)
2961 if (wp->w_border_char[i] != 0)
2962 border_char[i] = wp->w_border_char[i];
2963
2964 for (i = 0; i < 4; ++i)
2965 {
2966 border_attr[i] = popup_attr;
2967 if (wp->w_border_highlight[i] != NULL)
2968 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
2969 }
2970
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002971 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002972 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002973 if (wp->w_popup_border[0] > 0)
2974 {
2975 // top border
2976 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002977 wincol < 0 ? 0 : wincol, wincol + total_width,
2978 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002979 ? border_char[4] : border_char[0],
2980 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002981 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002982 {
2983 buf[mb_char2bytes(border_char[5], buf)] = NUL;
2984 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002985 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002986 }
2987 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002988 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
2989 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002990
Bram Moolenaard529ba52019-07-02 23:13:53 +02002991 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
2992 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002993 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02002994 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
2995 - wp->w_has_scrollbar;
2996 if (padcol < 0)
2997 {
2998 padwidth += padcol;
2999 padcol = 0;
3000 }
3001 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003002 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003003 {
3004 // top padding
3005 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003006 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003007 ' ', ' ', popup_attr);
3008 }
3009
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003010 // Title goes on top of border or padding.
3011 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003012 {
3013 int len = (int)STRLEN(wp->w_popup_title) + 1;
3014 char_u *title = alloc(len);
3015
3016 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3017 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003018 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003019 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003020
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003021 // Compute scrollbar thumb position and size.
3022 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003023 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003024 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3025
Bram Moolenaar68acb412019-06-26 03:40:36 +02003026 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3027 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003028 if (sb_thumb_height == 0)
3029 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003030 if (linecount <= wp->w_height)
3031 // it just fits, avoid divide by zero
3032 sb_thumb_top = 0;
3033 else
3034 sb_thumb_top = (wp->w_topline - 1
3035 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003036 * (wp->w_height - sb_thumb_height)
3037 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003038 if (wp->w_scrollbar_highlight != NULL)
3039 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3040 else
3041 attr_scroll = highlight_attr[HLF_PSB];
3042 if (wp->w_thumb_highlight != NULL)
3043 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3044 else
3045 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003046 }
3047
3048 for (i = wp->w_popup_border[0];
3049 i < total_height - wp->w_popup_border[2]; ++i)
3050 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003051 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003052 // left and right padding only needed next to the body
3053 int do_padding =
3054 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3055 && i < total_height - wp->w_popup_border[2]
3056 - wp->w_popup_padding[2];
3057
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003058 row = wp->w_winrow + i;
3059
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003060 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003061 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003062 {
3063 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003064 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003065 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003066 if (do_padding && wp->w_popup_padding[3] > 0)
3067 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003068 int col = wincol + wp->w_popup_border[3];
3069
Bram Moolenaard529ba52019-07-02 23:13:53 +02003070 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003071 pad_left = wp->w_popup_padding[3];
3072 if (col < 0)
3073 {
3074 pad_left += col;
3075 col = 0;
3076 }
3077 if (pad_left > 0)
3078 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3079 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003080 // scrollbar
3081 if (wp->w_has_scrollbar)
3082 {
3083 int line = i - top_off;
3084 int scroll_col = wp->w_wincol + total_width - 1
3085 - wp->w_popup_border[1];
3086
3087 if (line >= 0 && line < wp->w_height)
3088 screen_putchar(' ', row, scroll_col,
3089 line >= sb_thumb_top
3090 && line < sb_thumb_top + sb_thumb_height
3091 ? attr_thumb : attr_scroll);
3092 else
3093 screen_putchar(' ', row, scroll_col, popup_attr);
3094 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003095 // right border
3096 if (wp->w_popup_border[1] > 0)
3097 {
3098 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003099 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003100 }
3101 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003102 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003103 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003104 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003105 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3106 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003107 }
3108
3109 if (wp->w_popup_padding[2] > 0)
3110 {
3111 // bottom padding
3112 row = wp->w_winrow + wp->w_popup_border[0]
3113 + wp->w_popup_padding[0] + wp->w_height;
3114 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003115 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003116 }
3117
3118 if (wp->w_popup_border[2] > 0)
3119 {
3120 // bottom border
3121 row = wp->w_winrow + total_height - 1;
3122 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003123 wincol < 0 ? 0 : wincol,
3124 wincol + total_width,
3125 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003126 ? border_char[7] : border_char[2],
3127 border_char[2], border_attr[2]);
3128 if (wp->w_popup_border[1] > 0)
3129 {
3130 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003131 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003132 }
3133 }
3134
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003135 if (wp->w_popup_close == POPCLOSE_BUTTON)
3136 {
3137 // close button goes on top of anything at the top-right corner
3138 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003139 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003140 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3141 }
3142
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003143 update_popup_transparent(wp, 0);
3144
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003145 // Back to the normal zindex.
3146 screen_zindex = 0;
3147 }
3148}
3149
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003150/*
3151 * Mark references in callbacks of one popup window.
3152 */
3153 static int
3154set_ref_in_one_popup(win_T *wp, int copyID)
3155{
3156 int abort = FALSE;
3157 typval_T tv;
3158
3159 if (wp->w_close_cb.cb_partial != NULL)
3160 {
3161 tv.v_type = VAR_PARTIAL;
3162 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3163 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3164 }
3165 if (wp->w_filter_cb.cb_partial != NULL)
3166 {
3167 tv.v_type = VAR_PARTIAL;
3168 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3169 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3170 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003171 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003172 return abort;
3173}
3174
3175/*
3176 * Set reference in callbacks of popup windows.
3177 */
3178 int
3179set_ref_in_popups(int copyID)
3180{
3181 int abort = FALSE;
3182 win_T *wp;
3183 tabpage_T *tp;
3184
3185 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3186 abort = abort || set_ref_in_one_popup(wp, copyID);
3187
3188 FOR_ALL_TABPAGES(tp)
3189 {
3190 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3191 abort = abort || set_ref_in_one_popup(wp, copyID);
3192 if (abort)
3193 break;
3194 }
3195 return abort;
3196}
Bram Moolenaar79648732019-07-18 21:43:07 +02003197
3198/*
3199 * Find an existing popup used as the preview window, in the current tab page.
3200 * Return NULL if not found.
3201 */
3202 win_T *
3203popup_find_preview_window(void)
3204{
3205 win_T *wp;
3206
3207 // Preview window popup is always local to tab page.
3208 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3209 if (wp->w_p_pvw)
3210 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003211 return NULL;
3212}
3213
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003214 int
3215popup_is_popup(win_T *wp)
3216{
3217 return wp->w_popup_flags != 0;
3218}
3219
3220/*
3221 * Find an existing popup used as the info window, in the current tab page.
3222 * Return NULL if not found.
3223 */
3224 win_T *
3225popup_find_info_window(void)
3226{
3227 win_T *wp;
3228
3229 // info window popup is always local to tab page.
3230 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3231 if (wp->w_popup_flags & POPF_INFO)
3232 return wp;
3233 return NULL;
3234}
3235
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003236 void
3237f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3238{
3239 win_T *wp = popup_find_preview_window();
3240
3241 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003242}
3243
Bram Moolenaar79648732019-07-18 21:43:07 +02003244/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003245 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003246 * NOTE: this makes the popup the current window, so that the file can be
3247 * edited. However, it must not remain to be the current window, the caller
3248 * must make sure of that.
3249 */
3250 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003251popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003252{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003253 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003254
3255 if (wp == NULL)
3256 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003257 if (info)
3258 wp->w_popup_flags |= POPF_INFO;
3259 else
3260 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003261
3262 // Set the width to a reasonable value, so that w_topline can be computed.
3263 if (wp->w_minwidth > 0)
3264 wp->w_width = wp->w_minwidth;
3265 else if (wp->w_maxwidth > 0)
3266 wp->w_width = wp->w_maxwidth;
3267 else
3268 wp->w_width = curwin->w_width;
3269
3270 // Will switch to another buffer soon, dummy one can be wiped.
3271 wp->w_buffer->b_locked = FALSE;
3272
3273 win_enter(wp, FALSE);
3274 return OK;
3275}
3276
3277 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003278popup_close_preview(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003279{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003280 win_T *wp = info ? popup_find_info_window() : popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003281
3282 if (wp != NULL)
3283 {
3284 typval_T res;
3285
3286 res.v_type = VAR_NUMBER;
3287 res.vval.v_number = -1;
3288 popup_close_and_callback(wp, &res);
3289 }
3290}
3291
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003292/*
3293 * Set the title of the popup window to the file name.
3294 */
3295 void
3296popup_set_title(win_T *wp)
3297{
3298 if (wp->w_buffer->b_fname != NULL)
3299 {
3300 char_u dirname[MAXPATHL];
3301 size_t len;
3302
3303 mch_dirname(dirname, MAXPATHL);
3304 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3305
3306 vim_free(wp->w_popup_title);
3307 len = STRLEN(wp->w_buffer->b_fname) + 3;
3308 wp->w_popup_title = alloc((int)len);
3309 if (wp->w_popup_title != NULL)
3310 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3311 wp->w_buffer->b_fname);
3312 redraw_win_later(wp, VALID);
3313 }
3314}
3315
3316/*
3317 * If there is a preview window, update the title.
3318 * Used after changing directory.
3319 */
3320 void
3321popup_update_preview_title(void)
3322{
3323 win_T *wp = popup_find_preview_window();
3324
3325 if (wp != NULL)
3326 popup_set_title(wp);
3327}
3328
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003329#endif // FEAT_TEXT_PROP