blob: ff7aa01aa9c10580bd0167dfbc98587622bbcaed [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";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200553 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200554 }
555
556 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
557 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
558 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200559 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200560 else
561 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200562}
563
564/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200565 * Shared between popup_create() and f_popup_setoptions().
566 */
567 static void
568apply_general_options(win_T *wp, dict_T *dict)
569{
570 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200571 int nr;
572 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200573
Bram Moolenaarae943152019-06-16 22:54:14 +0200574 // TODO: flip
575
576 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200577 if (di != NULL)
Bram Moolenaarae943152019-06-16 22:54:14 +0200578 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
579 if (wp->w_firstline < 1)
580 wp->w_firstline = 1;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200581
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200582 di = dict_find(dict, (char_u *)"scrollbar", -1);
583 if (di != NULL)
584 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
585
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200586 str = dict_get_string(dict, (char_u *)"title", FALSE);
587 if (str != NULL)
588 {
589 vim_free(wp->w_popup_title);
590 wp->w_popup_title = vim_strsave(str);
591 }
592
Bram Moolenaar402502d2019-05-30 22:07:36 +0200593 di = dict_find(dict, (char_u *)"wrap", -1);
594 if (di != NULL)
595 {
596 nr = dict_get_number(dict, (char_u *)"wrap");
597 wp->w_p_wrap = nr != 0;
598 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200599
Bram Moolenaara42d9452019-06-15 21:46:30 +0200600 di = dict_find(dict, (char_u *)"drag", -1);
601 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200602 {
603 nr = dict_get_number(dict, (char_u *)"drag");
604 if (nr)
605 wp->w_popup_flags |= POPF_DRAG;
606 else
607 wp->w_popup_flags &= ~POPF_DRAG;
608 }
609
610 di = dict_find(dict, (char_u *)"resize", -1);
611 if (di != NULL)
612 {
613 nr = dict_get_number(dict, (char_u *)"resize");
614 if (nr)
615 wp->w_popup_flags |= POPF_RESIZE;
616 else
617 wp->w_popup_flags &= ~POPF_RESIZE;
618 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200619
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200620 di = dict_find(dict, (char_u *)"close", -1);
621 if (di != NULL)
622 {
623 int ok = TRUE;
624
625 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
626 {
627 char_u *s = di->di_tv.vval.v_string;
628
629 if (STRCMP(s, "none") == 0)
630 wp->w_popup_close = POPCLOSE_NONE;
631 else if (STRCMP(s, "button") == 0)
632 wp->w_popup_close = POPCLOSE_BUTTON;
633 else if (STRCMP(s, "click") == 0)
634 wp->w_popup_close = POPCLOSE_CLICK;
635 else
636 ok = FALSE;
637 }
638 else
639 ok = FALSE;
640 if (!ok)
641 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
642 }
643
Bram Moolenaarae943152019-06-16 22:54:14 +0200644 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
645 if (str != NULL)
646 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
647 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200648
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200649 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
650 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200651 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
652 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200653
Bram Moolenaar790498b2019-06-01 22:15:29 +0200654 di = dict_find(dict, (char_u *)"borderhighlight", -1);
655 if (di != NULL)
656 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200657 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200658 emsg(_(e_listreq));
659 else
660 {
661 list_T *list = di->di_tv.vval.v_list;
662 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200663 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200664
Bram Moolenaar403d0902019-07-17 21:37:32 +0200665 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
666 ++i, li = li->li_next)
667 {
668 str = tv_get_string(&li->li_tv);
669 if (*str != NUL)
670 wp->w_border_highlight[i] = vim_strsave(str);
671 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200672 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
673 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200674 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200675 vim_strsave(wp->w_border_highlight[0]);
676 }
677 }
678
Bram Moolenaar790498b2019-06-01 22:15:29 +0200679 di = dict_find(dict, (char_u *)"borderchars", -1);
680 if (di != NULL)
681 {
682 if (di->di_tv.v_type != VAR_LIST)
683 emsg(_(e_listreq));
684 else
685 {
686 list_T *list = di->di_tv.vval.v_list;
687 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200688 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200689
690 if (list != NULL)
691 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
692 ++i, li = li->li_next)
693 {
694 str = tv_get_string(&li->li_tv);
695 if (*str != NUL)
696 wp->w_border_char[i] = mb_ptr2char(str);
697 }
698 if (list->lv_len == 1)
699 for (i = 1; i < 8; ++i)
700 wp->w_border_char[i] = wp->w_border_char[0];
701 if (list->lv_len == 2)
702 {
703 for (i = 4; i < 8; ++i)
704 wp->w_border_char[i] = wp->w_border_char[1];
705 for (i = 1; i < 4; ++i)
706 wp->w_border_char[i] = wp->w_border_char[0];
707 }
708 }
709 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200710
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200711 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
712 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
713
Bram Moolenaarae943152019-06-16 22:54:14 +0200714 di = dict_find(dict, (char_u *)"zindex", -1);
715 if (di != NULL)
716 {
717 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
718 if (wp->w_zindex < 1)
719 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
720 if (wp->w_zindex > 32000)
721 wp->w_zindex = 32000;
722 }
723
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200724 di = dict_find(dict, (char_u *)"mask", -1);
725 if (di != NULL)
726 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200727 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200728
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200729 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200730 {
731 listitem_T *li;
732
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200733 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200734 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
735 li = li->li_next)
736 {
737 if (li->li_tv.v_type != VAR_LIST
738 || li->li_tv.vval.v_list == NULL
739 || li->li_tv.vval.v_list->lv_len != 4)
740 {
741 ok = FALSE;
742 break;
743 }
744 }
745 }
746 if (ok)
747 {
748 wp->w_popup_mask = di->di_tv.vval.v_list;
749 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200750 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200751 }
752 else
753 semsg(_(e_invargval), "mask");
754 }
755
Bram Moolenaarae943152019-06-16 22:54:14 +0200756#if defined(FEAT_TIMERS)
757 // Add timer to close the popup after some time.
758 nr = dict_get_number(dict, (char_u *)"time");
759 if (nr > 0)
760 popup_add_timeout(wp, nr);
761#endif
762
Bram Moolenaar3397f742019-06-02 18:40:06 +0200763 di = dict_find(dict, (char_u *)"moved", -1);
764 if (di != NULL)
765 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200766 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200767 handle_moved_argument(wp, di, FALSE);
768 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200769
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200770 di = dict_find(dict, (char_u *)"mousemoved", -1);
771 if (di != NULL)
772 {
773 set_mousemoved_values(wp);
774 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200775 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200776
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200777 di = dict_find(dict, (char_u *)"cursorline", -1);
778 if (di != NULL)
779 {
780 if (di->di_tv.v_type == VAR_NUMBER)
781 {
782 if (di->di_tv.vval.v_number != 0)
783 wp->w_popup_flags |= POPF_CURSORLINE;
784 else
785 wp->w_popup_flags &= ~POPF_CURSORLINE;
786 }
787 else
788 semsg(_(e_invargval), "cursorline");
789 }
790
Bram Moolenaarae943152019-06-16 22:54:14 +0200791 di = dict_find(dict, (char_u *)"filter", -1);
792 if (di != NULL)
793 {
794 callback_T callback = get_callback(&di->di_tv);
795
796 if (callback.cb_name != NULL)
797 {
798 free_callback(&wp->w_filter_cb);
799 set_callback(&wp->w_filter_cb, &callback);
800 }
801 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200802 di = dict_find(dict, (char_u *)"mapping", -1);
803 if (di != NULL)
804 {
805 nr = dict_get_number(dict, (char_u *)"mapping");
806 if (nr)
807 wp->w_popup_flags |= POPF_MAPPING;
808 else
809 wp->w_popup_flags &= ~POPF_MAPPING;
810 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200811
812 di = dict_find(dict, (char_u *)"callback", -1);
813 if (di != NULL)
814 {
815 callback_T callback = get_callback(&di->di_tv);
816
817 if (callback.cb_name != NULL)
818 {
819 free_callback(&wp->w_close_cb);
820 set_callback(&wp->w_close_cb, &callback);
821 }
822 }
823}
824
825/*
826 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200827 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200828 */
829 static void
830apply_options(win_T *wp, dict_T *dict)
831{
832 int nr;
833
834 apply_move_options(wp, dict);
835 apply_general_options(wp, dict);
836
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200837 nr = dict_get_number(dict, (char_u *)"hidden");
838 if (nr > 0)
839 {
840 wp->w_popup_flags |= POPF_HIDDEN;
841 --wp->w_buffer->b_nwindows;
842 }
843
Bram Moolenaar33796b32019-06-08 16:01:13 +0200844 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200845 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200846}
847
848/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200849 * Add lines to the popup from a list of strings.
850 */
851 static void
852add_popup_strings(buf_T *buf, list_T *l)
853{
854 listitem_T *li;
855 linenr_T lnum = 0;
856 char_u *p;
857
858 for (li = l->lv_first; li != NULL; li = li->li_next)
859 if (li->li_tv.v_type == VAR_STRING)
860 {
861 p = li->li_tv.vval.v_string;
862 ml_append_buf(buf, lnum++,
863 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
864 }
865}
866
867/*
868 * Add lines to the popup from a list of dictionaries.
869 */
870 static void
871add_popup_dicts(buf_T *buf, list_T *l)
872{
873 listitem_T *li;
874 listitem_T *pli;
875 linenr_T lnum = 0;
876 char_u *p;
877 dict_T *dict;
878
879 // first add the text lines
880 for (li = l->lv_first; li != NULL; li = li->li_next)
881 {
882 if (li->li_tv.v_type != VAR_DICT)
883 {
884 emsg(_(e_dictreq));
885 return;
886 }
887 dict = li->li_tv.vval.v_dict;
888 p = dict == NULL ? NULL
889 : dict_get_string(dict, (char_u *)"text", FALSE);
890 ml_append_buf(buf, lnum++,
891 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
892 }
893
894 // add the text properties
895 lnum = 1;
896 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
897 {
898 dictitem_T *di;
899 list_T *plist;
900
901 dict = li->li_tv.vval.v_dict;
902 di = dict_find(dict, (char_u *)"props", -1);
903 if (di != NULL)
904 {
905 if (di->di_tv.v_type != VAR_LIST)
906 {
907 emsg(_(e_listreq));
908 return;
909 }
910 plist = di->di_tv.vval.v_list;
911 if (plist != NULL)
912 {
913 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
914 {
915 if (pli->li_tv.v_type != VAR_DICT)
916 {
917 emsg(_(e_dictreq));
918 return;
919 }
920 dict = pli->li_tv.vval.v_dict;
921 if (dict != NULL)
922 {
923 int col = dict_get_number(dict, (char_u *)"col");
924
925 prop_add_common( lnum, col, dict, buf, NULL);
926 }
927 }
928 }
929 }
930 }
931}
932
933/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200934 * Get the padding plus border at the top, adjusted to 1 if there is a title.
935 */
936 static int
937popup_top_extra(win_T *wp)
938{
939 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
940
941 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
942 return 1;
943 return extra;
944}
945
946/*
Bram Moolenaard529ba52019-07-02 23:13:53 +0200947 * Return the height of popup window "wp", including border and padding.
948 */
949 int
950popup_height(win_T *wp)
951{
952 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200953 + popup_top_extra(wp)
954 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +0200955}
956
957/*
958 * Return the width of popup window "wp", including border, padding and
959 * scrollbar.
960 */
961 int
962popup_width(win_T *wp)
963{
Bram Moolenaar017c2692019-07-13 14:17:51 +0200964 // w_leftcol is how many columns of the core are left of the screen
965 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +0200966 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200967 + popup_extra_width(wp)
968 + wp->w_popup_rightoff;
969}
970
971/*
972 * Return the extra width of popup window "wp": border, padding and scrollbar.
973 */
974 int
975popup_extra_width(win_T *wp)
976{
977 return wp->w_popup_padding[3] + wp->w_popup_border[3]
978 + wp->w_popup_padding[1] + wp->w_popup_border[1]
979 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200980}
981
982/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200983 * Adjust the position and size of the popup to fit on the screen.
984 */
Bram Moolenaar17146962019-05-30 00:12:11 +0200985 void
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200986popup_adjust_position(win_T *wp)
987{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200988 linenr_T lnum;
989 int wrapped = 0;
990 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +0200991 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +0200992 int center_vert = FALSE;
993 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200994 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200995 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +0200996 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
997 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
998 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
999 int extra_height = top_extra + bot_extra;
1000 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001001 int org_winrow = wp->w_winrow;
1002 int org_wincol = wp->w_wincol;
1003 int org_width = wp->w_width;
1004 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001005 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001006 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001007 int minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001008
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001009 wp->w_winrow = 0;
1010 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001011 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001012 wp->w_popup_leftoff = 0;
1013 wp->w_popup_rightoff = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001014 if (wp->w_popup_pos == POPPOS_CENTER)
1015 {
1016 // center after computing the size
1017 center_vert = TRUE;
1018 center_hor = TRUE;
1019 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001020 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001021 {
1022 if (wp->w_wantline == 0)
1023 center_vert = TRUE;
1024 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1025 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1026 {
1027 wp->w_winrow = wp->w_wantline - 1;
1028 if (wp->w_winrow >= Rows)
1029 wp->w_winrow = Rows - 1;
1030 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001031
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001032 if (wp->w_wantcol == 0)
1033 center_hor = TRUE;
1034 else if (wp->w_popup_pos == POPPOS_TOPLEFT
1035 || wp->w_popup_pos == POPPOS_BOTLEFT)
1036 {
1037 wp->w_wincol = wp->w_wantcol - 1;
1038 if (wp->w_wincol >= Columns - 3)
1039 wp->w_wincol = Columns - 3;
1040 }
1041 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001042
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001043 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001044 // When left aligned use the space available, but shift to the left when we
1045 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001046 maxspace = Columns - wp->w_wincol - left_extra;
1047 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001048 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001049 {
1050 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001051 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001052 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001053
Bram Moolenaar8d241042019-06-12 23:40:01 +02001054 // start at the desired first line
Bram Moolenaar79648732019-07-18 21:43:07 +02001055 if (wp->w_firstline != 0)
1056 wp->w_topline = wp->w_firstline;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001057 if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
1058 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1059
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001060 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001061 // Use a minimum width of one, so that something shows when there is no
1062 // text.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001063 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001064 wp->w_width = 1;
Bram Moolenaar8d241042019-06-12 23:40:01 +02001065 for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001066 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001067 int len;
1068 int w_width = wp->w_width;
1069
1070 // Count Tabs for what they are worth and compute the length based on
1071 // the maximum width (matters when 'showbreak' is set).
1072 if (wp->w_width < maxwidth)
1073 wp->w_width = maxwidth;
1074 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001075 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001076 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001077
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001078 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001079 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001080 while (len > maxwidth)
1081 {
1082 ++wrapped;
1083 len -= maxwidth;
1084 wp->w_width = maxwidth;
1085 }
1086 }
1087 else if (len > maxwidth
1088 && allow_adjust_left
1089 && (wp->w_popup_pos == POPPOS_TOPLEFT
1090 || wp->w_popup_pos == POPPOS_BOTLEFT))
1091 {
1092 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001093 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001094
Bram Moolenaar51c31312019-06-15 22:27:23 +02001095 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001096 {
1097 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001098
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001099 len -= truncate_shift;
1100 shift_by -= truncate_shift;
1101 }
1102
1103 wp->w_wincol -= shift_by;
1104 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001105 wp->w_width = maxwidth;
1106 }
1107 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001108 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001109 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001110 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1111 wp->w_width = wp->w_maxwidth;
1112 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001113 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001114 if (wp->w_maxheight > 0
1115 && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001116 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001117 }
1118
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001119 wp->w_has_scrollbar = wp->w_want_scrollbar
1120 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001121 if (wp->w_has_scrollbar)
1122 ++right_extra;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001123
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001124 minwidth = wp->w_minwidth;
1125 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1126 {
1127 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1128
1129 if (minwidth < title_len)
1130 minwidth = title_len;
1131 }
1132
1133 if (minwidth > 0 && wp->w_width < minwidth)
1134 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001135 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001136 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001137 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001138 // some columns cut off on the right
1139 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001140 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001141 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001142 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001143 {
1144 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1145 if (wp->w_wincol < 0)
1146 wp->w_wincol = 0;
1147 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001148 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1149 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1150 {
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001151 int leftoff = wp->w_wantcol - (wp->w_width + extra_width);
1152
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001153 // Right aligned: move to the right if needed.
1154 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001155 if (leftoff >= 0)
1156 wp->w_wincol = leftoff;
1157 else if (wp->w_popup_fixed)
1158 {
1159 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001160 // columns when displaying the window, minus left border and
1161 // padding.
1162 if (-leftoff > left_extra)
1163 wp->w_leftcol = -leftoff - left_extra;
1164 wp->w_width -= wp->w_leftcol;
1165 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001166 if (wp->w_width < 0)
1167 wp->w_width = 0;
1168 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001169 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001170
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001171 if (wp->w_p_wrap || (!wp->w_popup_fixed
1172 && (wp->w_popup_pos == POPPOS_TOPLEFT
1173 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001174 {
1175 int want_col = 0;
1176
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001177 // try to show the right border and any scrollbar
1178 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001179 if (want_col > 0 && wp->w_wincol > 0
1180 && wp->w_wincol + want_col >= Columns)
1181 {
1182 wp->w_wincol = Columns - want_col;
1183 if (wp->w_wincol < 0)
1184 wp->w_wincol = 0;
1185 }
1186 }
1187
Bram Moolenaar8d241042019-06-12 23:40:01 +02001188 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1189 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001190 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1191 wp->w_height = wp->w_minheight;
1192 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1193 wp->w_height = wp->w_maxheight;
1194 if (wp->w_height > Rows - wp->w_winrow)
1195 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001196
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001197 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001198 {
1199 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1200 if (wp->w_winrow < 0)
1201 wp->w_winrow = 0;
1202 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001203 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1204 || wp->w_popup_pos == POPPOS_BOTLEFT)
1205 {
Bram Moolenaar399d8982019-06-02 15:34:29 +02001206 if ((wp->w_height + extra_height) <= wp->w_wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001207 // bottom aligned: may move down
Bram Moolenaar399d8982019-06-02 15:34:29 +02001208 wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001209 else
1210 // not enough space, make top aligned
1211 wp->w_winrow = wp->w_wantline + 1;
1212 }
1213
Bram Moolenaar17146962019-05-30 00:12:11 +02001214 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001215
1216 // Need to update popup_mask if the position or size changed.
Bram Moolenaaracc682b2019-06-08 17:15:51 +02001217 // And redraw windows that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001218 if (org_winrow != wp->w_winrow
1219 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001220 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001221 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001222 || org_width != wp->w_width
1223 || org_height != wp->w_height)
1224 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001225 redraw_all_later(VALID);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001226 redraw_win_later(wp, NOT_VALID);
1227 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1228 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001229 popup_mask_refresh = TRUE;
1230 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001231}
1232
Bram Moolenaar17627312019-06-02 19:53:44 +02001233typedef enum
1234{
1235 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001236 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001237 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001238 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001239 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001240 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001241 TYPE_PREVIEW, // preview window
1242 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001243} create_type_T;
1244
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001245/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001246 * Make "buf" empty and set the contents to "text".
1247 * Used by popup_create() and popup_settext().
1248 */
1249 static void
1250popup_set_buffer_text(buf_T *buf, typval_T text)
1251{
1252 int lnum;
1253
1254 // Clear the buffer, then replace the lines.
1255 curbuf = buf;
1256 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1257 ml_delete(lnum, FALSE);
1258 curbuf = curwin->w_buffer;
1259
1260 // Add text to the buffer.
1261 if (text.v_type == VAR_STRING)
1262 {
1263 // just a string
1264 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1265 }
1266 else
1267 {
1268 list_T *l = text.vval.v_list;
1269
1270 if (l->lv_len > 0)
1271 {
1272 if (l->lv_first->li_tv.v_type == VAR_STRING)
1273 // list of strings
1274 add_popup_strings(buf, l);
1275 else
1276 // list of dictionaries
1277 add_popup_dicts(buf, l);
1278 }
1279 }
1280
1281 // delete the line that was in the empty buffer
1282 curbuf = buf;
1283 ml_delete(buf->b_ml.ml_line_count, FALSE);
1284 curbuf = curwin->w_buffer;
1285}
1286
1287/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001288 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1289 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001290 * Return FAIL if the parsing fails.
1291 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001292 static int
1293parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001294{
1295 char_u *p;
1296
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001297 for (p = is_preview ? p_pvp : p_cpp; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001298 {
1299 char_u *e, *dig;
1300 char_u *s = p;
1301 int x;
1302
1303 e = vim_strchr(p, ':');
1304 if (e == NULL || e[1] == NUL)
1305 return FAIL;
1306
1307 p = vim_strchr(e, ',');
1308 if (p == NULL)
1309 p = e + STRLEN(e);
1310 dig = e + 1;
1311 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001312
1313 if (STRNCMP(s, "height:", 7) == 0)
1314 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001315 if (dig != p)
1316 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001317 if (wp != NULL)
1318 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001319 if (is_preview)
1320 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001321 wp->w_maxheight = x;
1322 }
1323 }
1324 else if (STRNCMP(s, "width:", 6) == 0)
1325 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001326 if (dig != p)
1327 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001328 if (wp != NULL)
1329 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001330 if (is_preview)
1331 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001332 wp->w_maxwidth = x;
1333 }
1334 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001335 else if (STRNCMP(s, "highlight:", 10) == 0)
1336 {
1337 if (wp != NULL)
1338 {
1339 int c = *p;
1340
1341 *p = NUL;
1342 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1343 s + 10, OPT_FREE|OPT_LOCAL, 0);
1344 *p = c;
1345 }
1346 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001347 else
1348 return FAIL;
1349 }
1350 return OK;
1351}
1352
1353/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001354 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1355 * is not NULL.
1356 * Return FAIL if the parsing fails.
1357 */
1358 int
1359parse_previewpopup(win_T *wp)
1360{
1361 return parse_popup_option(wp, TRUE);
1362}
1363
1364/*
1365 * Parse the 'completepopup' option and apply the values to window "wp" if it
1366 * is not NULL.
1367 * Return FAIL if the parsing fails.
1368 */
1369 int
1370parse_completepopup(win_T *wp)
1371{
1372 return parse_popup_option(wp, FALSE);
1373}
1374
1375/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001376 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001377 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001378 */
1379 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001380popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001381{
1382 setcursor_mayforce(TRUE);
1383 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1384 if (wp->w_wantline == 0) // cursor in first line
1385 {
1386 wp->w_wantline = 2;
1387 wp->w_popup_pos = POPPOS_TOPLEFT;
1388 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001389
Bram Moolenaar79648732019-07-18 21:43:07 +02001390 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001391 if (wp->w_wantcol > Columns - width)
1392 {
1393 wp->w_wantcol = Columns - width;
1394 if (wp->w_wantcol < 1)
1395 wp->w_wantcol = 1;
1396 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001397 popup_adjust_position(wp);
1398}
1399
1400/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001401 * Set w_wantline and w_wantcol for the a given screen position.
1402 * Caller must take care of running into the window border.
1403 */
1404 void
1405popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1406{
1407 wp->w_wantline = row;
1408 wp->w_wantcol = col;
1409 popup_adjust_position(wp);
1410}
1411
1412/*
1413 * Add a border and lef&right padding.
1414 */
1415 static void
1416add_border_left_right_padding(win_T *wp)
1417{
1418 int i;
1419
1420 for (i = 0; i < 4; ++i)
1421 {
1422 wp->w_popup_border[i] = 1;
1423 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1424 }
1425}
1426
1427/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001428 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001429 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001430 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001431 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001432 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001433 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001434popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001435{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001436 win_T *wp;
1437 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001438 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001439 int new_buffer;
1440 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001441 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001442 int nr;
1443 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001444
Bram Moolenaar79648732019-07-18 21:43:07 +02001445 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001446 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001447 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001448 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001449 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001450 buf = buflist_findnr( argvars[0].vval.v_number);
1451 if (buf == NULL)
1452 {
1453 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1454 return NULL;
1455 }
1456 }
1457 else if (!(argvars[0].v_type == VAR_STRING
1458 && argvars[0].vval.v_string != NULL)
1459 && !(argvars[0].v_type == VAR_LIST
1460 && argvars[0].vval.v_list != NULL))
1461 {
1462 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001463 return NULL;
1464 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001465 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001466 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001467 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001468 return NULL;
1469 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001470 d = argvars[1].vval.v_dict;
1471 }
1472
1473 if (d != NULL)
1474 {
1475 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1476 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1477 else if (type == TYPE_NOTIFICATION)
1478 tabnr = -1; // notifications are global by default
1479 else
1480 tabnr = 0;
1481 if (tabnr > 0)
1482 {
1483 tp = find_tabpage(tabnr);
1484 if (tp == NULL)
1485 {
1486 semsg(_("E997: Tabpage not found: %d"), tabnr);
1487 return NULL;
1488 }
1489 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001490 }
1491
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001492 // Create the window and buffer.
1493 wp = win_alloc_popup_win();
1494 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001495 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001496 if (rettv != NULL)
1497 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001498 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001499 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001500
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001501 if (buf != NULL)
1502 {
1503 // use existing buffer
1504 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001505 win_init_popup_win(wp, buf);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001506 buffer_ensure_loaded(buf);
1507 }
1508 else
1509 {
1510 // create a new buffer associated with the popup
1511 new_buffer = TRUE;
1512 buf = buflist_new(NULL, NULL, (linenr_T)0,
1513 BLN_NEW|BLN_LISTED|BLN_DUMMY);
1514 if (buf == NULL)
1515 return NULL;
1516 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001517
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001518 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001519
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001520 set_local_options_default(wp);
1521 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001522 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001523 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001524 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001525 buf->b_p_ul = -1; // no undo
1526 buf->b_p_swf = FALSE; // no swap file
1527 buf->b_p_bl = FALSE; // unlisted buffer
1528 buf->b_locked = TRUE;
1529 wp->w_p_wrap = TRUE; // 'wrap' is default on
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001530
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001531 // Avoid that 'buftype' is reset when this buffer is entered.
1532 buf->b_p_initialized = TRUE;
1533 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001534
Bram Moolenaara3fce622019-06-20 02:31:49 +02001535 if (tp != NULL)
1536 {
1537 // popup on specified tab page
1538 wp->w_next = tp->tp_first_popupwin;
1539 tp->tp_first_popupwin = wp;
1540 }
1541 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001542 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001543 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001544 wp->w_next = curtab->tp_first_popupwin;
1545 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001546 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001547 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001548 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001549 win_T *prev = first_popupwin;
1550
1551 // Global popup: add at the end, so that it gets displayed on top of
1552 // older ones with the same zindex. Matters for notifications.
1553 if (first_popupwin == NULL)
1554 first_popupwin = wp;
1555 else
1556 {
1557 while (prev->w_next != NULL)
1558 prev = prev->w_next;
1559 prev->w_next = wp;
1560 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001561 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001562
Bram Moolenaar79648732019-07-18 21:43:07 +02001563 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001564 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001565
Bram Moolenaar79648732019-07-18 21:43:07 +02001566 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001567 {
1568 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001569 }
1570 if (type == TYPE_ATCURSOR)
1571 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001572 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001573 set_moved_values(wp);
1574 set_moved_columns(wp, FIND_STRING);
1575 }
1576
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001577 if (type == TYPE_BEVAL)
1578 {
1579 wp->w_popup_pos = POPPOS_BOTLEFT;
1580
1581 // by default use the mouse position
1582 wp->w_wantline = mouse_row;
1583 if (wp->w_wantline <= 0) // mouse on first line
1584 {
1585 wp->w_wantline = 2;
1586 wp->w_popup_pos = POPPOS_TOPLEFT;
1587 }
1588 wp->w_wantcol = mouse_col + 1;
1589 set_mousemoved_values(wp);
1590 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1591 }
1592
Bram Moolenaar33796b32019-06-08 16:01:13 +02001593 // set default values
1594 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001595 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001596
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001597 if (type == TYPE_NOTIFICATION)
1598 {
1599 win_T *twp, *nextwin;
1600 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001601
1602 // Try to not overlap with another global popup. Guess we need 3
1603 // more screen lines than buffer lines.
1604 wp->w_wantline = 1;
1605 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1606 {
1607 nextwin = twp->w_next;
1608 if (twp != wp
1609 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1610 && twp->w_winrow <= wp->w_wantline - 1 + height
1611 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1612 {
1613 // move to below this popup and restart the loop to check for
1614 // overlap with other popups
1615 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1616 nextwin = first_popupwin;
1617 }
1618 }
1619 if (wp->w_wantline + height > Rows)
1620 {
1621 // can't avoid overlap, put on top in the hope that message goes
1622 // away soon.
1623 wp->w_wantline = 1;
1624 }
1625
1626 wp->w_wantcol = 10;
1627 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001628 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001629 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001630 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001631 for (i = 0; i < 4; ++i)
1632 wp->w_popup_border[i] = 1;
1633 wp->w_popup_padding[1] = 1;
1634 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001635
1636 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001637 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001638 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1639 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001640 }
1641
Bram Moolenaara730e552019-06-16 19:05:31 +02001642 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001643 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001644 wp->w_popup_pos = POPPOS_CENTER;
1645 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001646 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001647 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001648 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001649 }
1650
Bram Moolenaara730e552019-06-16 19:05:31 +02001651 if (type == TYPE_MENU)
1652 {
1653 typval_T tv;
1654 callback_T callback;
1655
1656 tv.v_type = VAR_STRING;
1657 tv.vval.v_string = (char_u *)"popup_filter_menu";
1658 callback = get_callback(&tv);
1659 if (callback.cb_name != NULL)
1660 set_callback(&wp->w_filter_cb, &callback);
1661
1662 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001663 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001664 }
1665
Bram Moolenaar79648732019-07-18 21:43:07 +02001666 if (type == TYPE_PREVIEW)
1667 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001668 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001669 wp->w_popup_close = POPCLOSE_BUTTON;
1670 for (i = 0; i < 4; ++i)
1671 wp->w_popup_border[i] = 1;
1672 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001673 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1674 }
1675 if (type == TYPE_INFO)
1676 {
1677 wp->w_popup_pos = POPPOS_TOPLEFT;
1678 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1679 wp->w_popup_close = POPCLOSE_BUTTON;
1680 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001681 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001682 }
1683
Bram Moolenaarae943152019-06-16 22:54:14 +02001684 for (i = 0; i < 4; ++i)
1685 VIM_CLEAR(wp->w_border_highlight[i]);
1686 for (i = 0; i < 8; ++i)
1687 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001688 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001689 wp->w_popup_fixed = 0;
Bram Moolenaarae943152019-06-16 22:54:14 +02001690
Bram Moolenaar79648732019-07-18 21:43:07 +02001691 if (d != NULL)
1692 // Deal with options.
1693 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001694
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001695#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001696 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1697 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001698#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001699
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001700 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001701
1702 wp->w_vsep_width = 0;
1703
1704 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001705 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001706
1707 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001708}
1709
1710/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001711 * popup_clear()
1712 */
1713 void
1714f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1715{
1716 close_all_popups();
1717}
1718
1719/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001720 * popup_create({text}, {options})
1721 */
1722 void
1723f_popup_create(typval_T *argvars, typval_T *rettv)
1724{
Bram Moolenaar17627312019-06-02 19:53:44 +02001725 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001726}
1727
1728/*
1729 * popup_atcursor({text}, {options})
1730 */
1731 void
1732f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1733{
Bram Moolenaar17627312019-06-02 19:53:44 +02001734 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001735}
1736
1737/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001738 * popup_beval({text}, {options})
1739 */
1740 void
1741f_popup_beval(typval_T *argvars, typval_T *rettv)
1742{
1743 popup_create(argvars, rettv, TYPE_BEVAL);
1744}
1745
1746/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001747 * Invoke the close callback for window "wp" with value "result".
1748 * Careful: The callback may make "wp" invalid!
1749 */
1750 static void
1751invoke_popup_callback(win_T *wp, typval_T *result)
1752{
1753 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001754 typval_T argv[3];
1755
1756 argv[0].v_type = VAR_NUMBER;
1757 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1758
1759 if (result != NULL && result->v_type != VAR_UNKNOWN)
1760 copy_tv(result, &argv[1]);
1761 else
1762 {
1763 argv[1].v_type = VAR_NUMBER;
1764 argv[1].vval.v_number = 0;
1765 }
1766
1767 argv[2].v_type = VAR_UNKNOWN;
1768
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001769 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001770 if (result != NULL)
1771 clear_tv(&argv[1]);
1772 clear_tv(&rettv);
1773}
1774
1775/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001776 * Close popup "wp" and invoke any close callback for it.
1777 */
1778 static void
1779popup_close_and_callback(win_T *wp, typval_T *arg)
1780{
1781 int id = wp->w_id;
1782
1783 if (wp->w_close_cb.cb_name != NULL)
1784 // Careful: This may make "wp" invalid.
1785 invoke_popup_callback(wp, arg);
1786
1787 popup_close(id);
1788}
1789
1790/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001791 * Close popup "wp" because of a mouse click.
1792 */
1793 void
1794popup_close_for_mouse_click(win_T *wp)
1795{
1796 typval_T res;
1797
1798 res.v_type = VAR_NUMBER;
1799 res.vval.v_number = -2;
1800 popup_close_and_callback(wp, &res);
1801}
1802
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001803 static void
1804check_mouse_moved(win_T *wp, win_T *mouse_wp)
1805{
1806 // Close the popup when all if these are true:
1807 // - the mouse is not on this popup
1808 // - "mousemoved" was used
1809 // - the mouse is no longer on the same screen row or the mouse column is
1810 // outside of the relevant text
1811 if (wp != mouse_wp
1812 && wp->w_popup_mouse_row != 0
1813 && (wp->w_popup_mouse_row != mouse_row
1814 || mouse_col < wp->w_popup_mouse_mincol
1815 || mouse_col > wp->w_popup_mouse_maxcol))
1816 {
1817 typval_T res;
1818
1819 res.v_type = VAR_NUMBER;
1820 res.vval.v_number = -2;
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001821 // Careful: this makes "wp" invalid.
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001822 popup_close_and_callback(wp, &res);
1823 }
1824}
1825
1826/*
1827 * Called when the mouse moved: may close a popup with "mousemoved".
1828 */
1829 void
1830popup_handle_mouse_moved(void)
1831{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001832 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001833 win_T *mouse_wp;
1834 int row = mouse_row;
1835 int col = mouse_col;
1836
1837 // find the window where the mouse is in
1838 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
1839
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001840 for (wp = first_popupwin; wp != NULL; wp = nextwp)
1841 {
1842 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001843 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001844 }
1845 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
1846 {
1847 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001848 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02001849 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001850}
1851
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001852/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001853 * In a filter: check if the typed key is a mouse event that is used for
1854 * dragging the popup.
1855 */
1856 static void
1857filter_handle_drag(win_T *wp, int c, typval_T *rettv)
1858{
1859 int row = mouse_row;
1860 int col = mouse_col;
1861
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001862 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02001863 && is_mouse_key(c)
1864 && (wp == popup_dragwin
1865 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
1866 // do not consume the key, allow for dragging the popup
1867 rettv->vval.v_number = 0;
1868}
1869
Bram Moolenaara730e552019-06-16 19:05:31 +02001870/*
1871 * popup_filter_menu({text}, {options})
1872 */
1873 void
1874f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
1875{
1876 int id = tv_get_number(&argvars[0]);
1877 win_T *wp = win_id2wp(id);
1878 char_u *key = tv_get_string(&argvars[1]);
1879 typval_T res;
1880 int c;
1881 linenr_T old_lnum;
1882
1883 // If the popup has been closed do not consume the key.
1884 if (wp == NULL)
1885 return;
1886
1887 c = *key;
1888 if (c == K_SPECIAL && key[1] != NUL)
1889 c = TO_SPECIAL(key[1], key[2]);
1890
1891 // consume all keys until done
1892 rettv->vval.v_number = 1;
1893 res.v_type = VAR_NUMBER;
1894
1895 old_lnum = wp->w_cursor.lnum;
1896 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
1897 --wp->w_cursor.lnum;
1898 if ((c == 'j' || c == 'J' || c == K_DOWN)
1899 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
1900 ++wp->w_cursor.lnum;
1901 if (old_lnum != wp->w_cursor.lnum)
1902 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001903 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02001904 return;
1905 }
1906
1907 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
1908 {
1909 // Cancelled, invoke callback with -1
1910 res.vval.v_number = -1;
1911 popup_close_and_callback(wp, &res);
1912 return;
1913 }
1914 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
1915 {
1916 // Invoke callback with current index.
1917 res.vval.v_number = wp->w_cursor.lnum;
1918 popup_close_and_callback(wp, &res);
1919 return;
1920 }
1921
1922 filter_handle_drag(wp, c, rettv);
1923}
1924
1925/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001926 * popup_filter_yesno({text}, {options})
1927 */
1928 void
1929f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
1930{
1931 int id = tv_get_number(&argvars[0]);
1932 win_T *wp = win_id2wp(id);
1933 char_u *key = tv_get_string(&argvars[1]);
1934 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02001935 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02001936
1937 // If the popup has been closed don't consume the key.
1938 if (wp == NULL)
1939 return;
1940
Bram Moolenaara730e552019-06-16 19:05:31 +02001941 c = *key;
1942 if (c == K_SPECIAL && key[1] != NUL)
1943 c = TO_SPECIAL(key[1], key[2]);
1944
Bram Moolenaara42d9452019-06-15 21:46:30 +02001945 // consume all keys until done
1946 rettv->vval.v_number = 1;
1947
Bram Moolenaara730e552019-06-16 19:05:31 +02001948 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02001949 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02001950 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001951 res.vval.v_number = 0;
1952 else
1953 {
Bram Moolenaara730e552019-06-16 19:05:31 +02001954 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001955 return;
1956 }
1957
1958 // Invoke callback
1959 res.v_type = VAR_NUMBER;
1960 popup_close_and_callback(wp, &res);
1961}
1962
1963/*
1964 * popup_dialog({text}, {options})
1965 */
1966 void
1967f_popup_dialog(typval_T *argvars, typval_T *rettv)
1968{
1969 popup_create(argvars, rettv, TYPE_DIALOG);
1970}
1971
1972/*
Bram Moolenaara730e552019-06-16 19:05:31 +02001973 * popup_menu({text}, {options})
1974 */
1975 void
1976f_popup_menu(typval_T *argvars, typval_T *rettv)
1977{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001978 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02001979}
1980
1981/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02001982 * popup_notification({text}, {options})
1983 */
1984 void
1985f_popup_notification(typval_T *argvars, typval_T *rettv)
1986{
1987 popup_create(argvars, rettv, TYPE_NOTIFICATION);
1988}
1989
1990/*
1991 * Find the popup window with window-ID "id".
1992 * If the popup window does not exist NULL is returned.
1993 * If the window is not a popup window, and error message is given.
1994 */
1995 static win_T *
1996find_popup_win(int id)
1997{
1998 win_T *wp = win_id2wp(id);
1999
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002000 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002001 {
2002 semsg(_("E993: window %d is not a popup window"), id);
2003 return NULL;
2004 }
2005 return wp;
2006}
2007
2008/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002009 * popup_close({id})
2010 */
2011 void
2012f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2013{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002014 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002015 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002016
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002017 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002018 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002019}
2020
2021/*
2022 * popup_hide({id})
2023 */
2024 void
2025f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2026{
2027 int id = (int)tv_get_number(argvars);
2028 win_T *wp = find_popup_win(id);
2029
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002030 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002031 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002032 wp->w_popup_flags |= POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02002033 --wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002034 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002035 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002036 }
2037}
2038
2039/*
2040 * popup_show({id})
2041 */
2042 void
2043f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2044{
2045 int id = (int)tv_get_number(argvars);
2046 win_T *wp = find_popup_win(id);
2047
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002048 if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002049 {
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02002050 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarc6896e22019-05-30 22:32:34 +02002051 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002052 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002053 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002054 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002055}
2056
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002057/*
2058 * popup_settext({id}, {text})
2059 */
2060 void
2061f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2062{
2063 int id = (int)tv_get_number(&argvars[0]);
2064 win_T *wp = find_popup_win(id);
2065
2066 if (wp != NULL)
2067 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002068 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2069 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2070 else
2071 {
2072 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002073 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 popup_adjust_position(wp);
2075 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002076 }
2077}
2078
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002079 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002080popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002081{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002082 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002083 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002084 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002085 clear_cmdline = TRUE;
2086 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002087
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002088 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002089 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002090}
2091
Bram Moolenaarec583842019-05-26 14:11:23 +02002092/*
2093 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002094 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002095 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002096 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002097popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002098{
2099 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002100 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002101 win_T *prev = NULL;
2102
Bram Moolenaarec583842019-05-26 14:11:23 +02002103 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002104 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002105 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002106 {
2107 if (prev == NULL)
2108 first_popupwin = wp->w_next;
2109 else
2110 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002111 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002112 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002113 }
2114
Bram Moolenaarec583842019-05-26 14:11:23 +02002115 // go through tab-local popups
2116 FOR_ALL_TABPAGES(tp)
2117 popup_close_tabpage(tp, id);
2118}
2119
2120/*
2121 * Close a popup window with Window-id "id" in tabpage "tp".
2122 */
2123 void
2124popup_close_tabpage(tabpage_T *tp, int id)
2125{
2126 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002127 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002128 win_T *prev = NULL;
2129
Bram Moolenaarec583842019-05-26 14:11:23 +02002130 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2131 if (wp->w_id == id)
2132 {
2133 if (prev == NULL)
2134 *root = wp->w_next;
2135 else
2136 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002137 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002138 return;
2139 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002140}
2141
2142 void
2143close_all_popups(void)
2144{
2145 while (first_popupwin != NULL)
2146 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002147 while (curtab->tp_first_popupwin != NULL)
2148 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002149}
2150
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002151/*
2152 * popup_move({id}, {options})
2153 */
2154 void
2155f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2156{
Bram Moolenaarae943152019-06-16 22:54:14 +02002157 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002158 int id = (int)tv_get_number(argvars);
2159 win_T *wp = find_popup_win(id);
2160
2161 if (wp == NULL)
2162 return; // invalid {id}
2163
2164 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2165 {
2166 emsg(_(e_dictreq));
2167 return;
2168 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002169 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002170
Bram Moolenaarae943152019-06-16 22:54:14 +02002171 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002172
2173 if (wp->w_winrow + wp->w_height >= cmdline_row)
2174 clear_cmdline = TRUE;
2175 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002176}
2177
Bram Moolenaarbc133542019-05-29 20:26:46 +02002178/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002179 * popup_setoptions({id}, {options})
2180 */
2181 void
2182f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2183{
2184 dict_T *dict;
2185 int id = (int)tv_get_number(argvars);
2186 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002187 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002188
2189 if (wp == NULL)
2190 return; // invalid {id}
2191
2192 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2193 {
2194 emsg(_(e_dictreq));
2195 return;
2196 }
2197 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002198 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002199
2200 apply_move_options(wp, dict);
2201 apply_general_options(wp, dict);
2202
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002203 if (old_firstline != wp->w_firstline)
2204 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002205 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002206 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002207 popup_adjust_position(wp);
2208}
2209
2210/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002211 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002212 */
2213 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002214f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002215{
2216 dict_T *dict;
2217 int id = (int)tv_get_number(argvars);
2218 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002219 int top_extra;
2220 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002221
2222 if (rettv_dict_alloc(rettv) == OK)
2223 {
2224 if (wp == NULL)
2225 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002226 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002227 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2228
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002229 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002230 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002231 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002232
Bram Moolenaarbc133542019-05-29 20:26:46 +02002233 dict_add_number(dict, "line", wp->w_winrow + 1);
2234 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002235 dict_add_number(dict, "width", wp->w_width + left_extra
2236 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2237 dict_add_number(dict, "height", wp->w_height + top_extra
2238 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002239
2240 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2241 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2242 dict_add_number(dict, "core_width", wp->w_width);
2243 dict_add_number(dict, "core_height", wp->w_height);
2244
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002245 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002246 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002247 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002248 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002249
2250 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002251 }
2252}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002253/*
2254 * popup_locate({row}, {col})
2255 */
2256 void
2257f_popup_locate(typval_T *argvars, typval_T *rettv)
2258{
2259 int row = tv_get_number(&argvars[0]) - 1;
2260 int col = tv_get_number(&argvars[1]) - 1;
2261 win_T *wp;
2262
2263 wp = mouse_find_win(&row, &col, FIND_POPUP);
2264 if (WIN_IS_POPUP(wp))
2265 rettv->vval.v_number = wp->w_id;
2266}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002267
2268/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002269 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2270 */
2271 static void
2272get_padding_border(dict_T *dict, int *array, char *name)
2273{
2274 list_T *list;
2275 int i;
2276
2277 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2278 return;
2279
2280 list = list_alloc();
2281 if (list != NULL)
2282 {
2283 dict_add_list(dict, name, list);
2284 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2285 for (i = 0; i < 4; ++i)
2286 list_append_number(list, array[i]);
2287 }
2288}
2289
2290/*
2291 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2292 */
2293 static void
2294get_borderhighlight(dict_T *dict, win_T *wp)
2295{
2296 list_T *list;
2297 int i;
2298
2299 for (i = 0; i < 4; ++i)
2300 if (wp->w_border_highlight[i] != NULL)
2301 break;
2302 if (i == 4)
2303 return;
2304
2305 list = list_alloc();
2306 if (list != NULL)
2307 {
2308 dict_add_list(dict, "borderhighlight", list);
2309 for (i = 0; i < 4; ++i)
2310 list_append_string(list, wp->w_border_highlight[i], -1);
2311 }
2312}
2313
2314/*
2315 * For popup_getoptions(): add a "borderchars" entry to "dict".
2316 */
2317 static void
2318get_borderchars(dict_T *dict, win_T *wp)
2319{
2320 list_T *list;
2321 int i;
2322 char_u buf[NUMBUFLEN];
2323 int len;
2324
2325 for (i = 0; i < 8; ++i)
2326 if (wp->w_border_char[i] != 0)
2327 break;
2328 if (i == 8)
2329 return;
2330
2331 list = list_alloc();
2332 if (list != NULL)
2333 {
2334 dict_add_list(dict, "borderchars", list);
2335 for (i = 0; i < 8; ++i)
2336 {
2337 len = mb_char2bytes(wp->w_border_char[i], buf);
2338 list_append_string(list, buf, len);
2339 }
2340 }
2341}
2342
2343/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002344 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002345 */
2346 static void
2347get_moved_list(dict_T *dict, win_T *wp)
2348{
2349 list_T *list;
2350
2351 list = list_alloc();
2352 if (list != NULL)
2353 {
2354 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002355 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002356 list_append_number(list, wp->w_popup_mincol);
2357 list_append_number(list, wp->w_popup_maxcol);
2358 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002359 list = list_alloc();
2360 if (list != NULL)
2361 {
2362 dict_add_list(dict, "mousemoved", list);
2363 list_append_number(list, wp->w_popup_mouse_row);
2364 list_append_number(list, wp->w_popup_mouse_mincol);
2365 list_append_number(list, wp->w_popup_mouse_maxcol);
2366 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002367}
2368
2369/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002370 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002371 */
2372 void
2373f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2374{
2375 dict_T *dict;
2376 int id = (int)tv_get_number(argvars);
2377 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002378 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002379 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002380
2381 if (rettv_dict_alloc(rettv) == OK)
2382 {
2383 if (wp == NULL)
2384 return;
2385
2386 dict = rettv->vval.v_dict;
2387 dict_add_number(dict, "line", wp->w_wantline);
2388 dict_add_number(dict, "col", wp->w_wantcol);
2389 dict_add_number(dict, "minwidth", wp->w_minwidth);
2390 dict_add_number(dict, "minheight", wp->w_minheight);
2391 dict_add_number(dict, "maxheight", wp->w_maxheight);
2392 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002393 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002394 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002395 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002396 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaarae943152019-06-16 22:54:14 +02002397 dict_add_string(dict, "title", wp->w_popup_title);
2398 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002399 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02002400 dict_add_number(dict, "mapping", (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002401 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002402 dict_add_number(dict, "cursorline",
2403 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002404 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002405 if (wp->w_scrollbar_highlight != NULL)
2406 dict_add_string(dict, "scrollbarhighlight",
2407 wp->w_scrollbar_highlight);
2408 if (wp->w_thumb_highlight != NULL)
2409 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002410
Bram Moolenaara3fce622019-06-20 02:31:49 +02002411 // find the tabpage that holds this popup
2412 i = 1;
2413 FOR_ALL_TABPAGES(tp)
2414 {
2415 win_T *p;
2416
2417 for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
2418 if (p->w_id == id)
2419 break;
2420 if (p != NULL)
2421 break;
2422 ++i;
2423 }
2424 if (tp == NULL)
2425 i = -1; // must be global
2426 else if (tp == curtab)
2427 i = 0;
2428 dict_add_number(dict, "tabpage", i);
2429
Bram Moolenaarae943152019-06-16 22:54:14 +02002430 get_padding_border(dict, wp->w_popup_padding, "padding");
2431 get_padding_border(dict, wp->w_popup_border, "border");
2432 get_borderhighlight(dict, wp);
2433 get_borderchars(dict, wp);
2434 get_moved_list(dict, wp);
2435
2436 if (wp->w_filter_cb.cb_name != NULL)
2437 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2438 if (wp->w_close_cb.cb_name != NULL)
2439 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002440
2441 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2442 ++i)
2443 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2444 {
2445 dict_add_string(dict, "pos",
2446 (char_u *)poppos_entries[i].pp_name);
2447 break;
2448 }
2449
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002450 dict_add_string(dict, "close", (char_u *)(
2451 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2452 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2453
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002454# if defined(FEAT_TIMERS)
2455 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2456 ? (long)wp->w_popup_timer->tr_interval : 0L);
2457# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002458 }
2459}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002460
2461 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002462error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002463{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002464 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002465 {
2466 emsg(_("E994: Not allowed in a popup window"));
2467 return TRUE;
2468 }
2469 return FALSE;
2470}
2471
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002472/*
2473 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002474 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002475 */
2476 void
2477popup_reset_handled()
2478{
2479 win_T *wp;
2480
2481 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2482 wp->w_popup_flags &= ~POPF_HANDLED;
2483 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2484 wp->w_popup_flags &= ~POPF_HANDLED;
2485}
2486
2487/*
2488 * Find the next visible popup where POPF_HANDLED is not set.
2489 * Must have called popup_reset_handled() first.
2490 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2491 * popup with the highest zindex.
2492 */
2493 win_T *
2494find_next_popup(int lowest)
2495{
2496 win_T *wp;
2497 win_T *found_wp;
2498 int found_zindex;
2499
2500 found_zindex = lowest ? INT_MAX : 0;
2501 found_wp = NULL;
2502 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2503 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2504 && (lowest ? wp->w_zindex < found_zindex
2505 : wp->w_zindex > found_zindex))
2506 {
2507 found_zindex = wp->w_zindex;
2508 found_wp = wp;
2509 }
2510 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2511 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2512 && (lowest ? wp->w_zindex < found_zindex
2513 : wp->w_zindex > found_zindex))
2514 {
2515 found_zindex = wp->w_zindex;
2516 found_wp = wp;
2517 }
2518
2519 if (found_wp != NULL)
2520 found_wp->w_popup_flags |= POPF_HANDLED;
2521 return found_wp;
2522}
2523
2524/*
2525 * Invoke the filter callback for window "wp" with typed character "c".
2526 * Uses the global "mod_mask" for modifiers.
2527 * Returns the return value of the filter.
2528 * Careful: The filter may make "wp" invalid!
2529 */
2530 static int
2531invoke_popup_filter(win_T *wp, int c)
2532{
2533 int res;
2534 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002535 typval_T argv[3];
2536 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002537 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002538
Bram Moolenaara42d9452019-06-15 21:46:30 +02002539 // Emergency exit: CTRL-C closes the popup.
2540 if (c == Ctrl_C)
2541 {
2542 rettv.v_type = VAR_NUMBER;
2543 rettv.vval.v_number = -1;
2544 popup_close_and_callback(wp, &rettv);
2545 return 1;
2546 }
2547
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002548 argv[0].v_type = VAR_NUMBER;
2549 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2550
2551 // Convert the number to a string, so that the function can use:
2552 // if a:c == "\<F2>"
2553 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2554 argv[1].v_type = VAR_STRING;
2555 argv[1].vval.v_string = vim_strsave(buf);
2556
2557 argv[2].v_type = VAR_UNKNOWN;
2558
Bram Moolenaara42d9452019-06-15 21:46:30 +02002559 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002560 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002561 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002562 popup_highlight_curline(wp);
2563
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002564 res = tv_get_number(&rettv);
2565 vim_free(argv[1].vval.v_string);
2566 clear_tv(&rettv);
2567 return res;
2568}
2569
2570/*
2571 * Called when "c" was typed: invoke popup filter callbacks.
2572 * Returns TRUE when the character was consumed,
2573 */
2574 int
2575popup_do_filter(int c)
2576{
2577 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002578 win_T *wp;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002579
2580 popup_reset_handled();
2581
2582 while (!res && (wp = find_next_popup(FALSE)) != NULL)
2583 if (wp->w_filter_cb.cb_name != NULL)
2584 res = invoke_popup_filter(wp, c);
2585
2586 return res;
2587}
2588
Bram Moolenaar3397f742019-06-02 18:40:06 +02002589/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002590 * Return TRUE if there is a popup visible with a filter callback and the
2591 * "mapping" property off.
2592 */
2593 int
2594popup_no_mapping(void)
2595{
2596 int round;
2597 win_T *wp;
2598
2599 for (round = 1; round <= 2; ++round)
2600 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2601 wp != NULL; wp = wp->w_next)
2602 if (wp->w_filter_cb.cb_name != NULL
2603 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2604 return TRUE;
2605 return FALSE;
2606}
2607
2608/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002609 * Called when the cursor moved: check if any popup needs to be closed if the
2610 * cursor moved far enough.
2611 */
2612 void
2613popup_check_cursor_pos()
2614{
2615 win_T *wp;
2616 typval_T tv;
2617
2618 popup_reset_handled();
2619 while ((wp = find_next_popup(TRUE)) != NULL)
2620 if (wp->w_popup_curwin != NULL
2621 && (curwin != wp->w_popup_curwin
2622 || curwin->w_cursor.lnum != wp->w_popup_lnum
2623 || curwin->w_cursor.col < wp->w_popup_mincol
2624 || curwin->w_cursor.col > wp->w_popup_maxcol))
2625 {
2626 tv.v_type = VAR_NUMBER;
2627 tv.vval.v_number = -1;
2628 popup_close_and_callback(wp, &tv);
2629 }
2630}
2631
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002632/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002633 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002634 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002635 static void
2636popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002637{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002638 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002639 char_u *cells;
2640 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002641
2642 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002643 return;
2644 if (wp->w_popup_mask_cells != NULL
2645 && wp->w_popup_mask_height == height
2646 && wp->w_popup_mask_width == width)
2647 return; // cache is still valid
2648
2649 vim_free(wp->w_popup_mask_cells);
2650 wp->w_popup_mask_cells = alloc_clear(width * height);
2651 if (wp->w_popup_mask_cells == NULL)
2652 return;
2653 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002654
2655 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2656 {
2657 int cols, cole;
2658 int lines, linee;
2659
2660 li = lio->li_tv.vval.v_list->lv_first;
2661 cols = tv_get_number(&li->li_tv);
2662 if (cols < 0)
2663 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002664 li = li->li_next;
2665 cole = tv_get_number(&li->li_tv);
2666 if (cole < 0)
2667 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002668 li = li->li_next;
2669 lines = tv_get_number(&li->li_tv);
2670 if (lines < 0)
2671 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002672 li = li->li_next;
2673 linee = tv_get_number(&li->li_tv);
2674 if (linee < 0)
2675 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002676
2677 for (row = lines - 1; row < linee && row < height; ++row)
2678 for (col = cols - 1; col < cole && col < width; ++col)
2679 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002680 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002681}
2682
2683/*
2684 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2685 * "col" and "line" are screen coordinates.
2686 */
2687 static int
2688popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2689{
2690 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2691 int line = screenline - wp->w_winrow;
2692
2693 return col >= 0 && col < width
2694 && line >= 0 && line < height
2695 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002696}
2697
2698/*
2699 * Set flags in popup_transparent[] for window "wp" to "val".
2700 */
2701 static void
2702update_popup_transparent(win_T *wp, int val)
2703{
2704 if (wp->w_popup_mask != NULL)
2705 {
2706 int width = popup_width(wp);
2707 int height = popup_height(wp);
2708 listitem_T *lio, *li;
2709 int cols, cole;
2710 int lines, linee;
2711 int col, line;
2712
2713 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2714 {
2715 li = lio->li_tv.vval.v_list->lv_first;
2716 cols = tv_get_number(&li->li_tv);
2717 if (cols < 0)
2718 cols = width + cols + 1;
2719 li = li->li_next;
2720 cole = tv_get_number(&li->li_tv);
2721 if (cole < 0)
2722 cole = width + cole + 1;
2723 li = li->li_next;
2724 lines = tv_get_number(&li->li_tv);
2725 if (lines < 0)
2726 lines = height + lines + 1;
2727 li = li->li_next;
2728 linee = tv_get_number(&li->li_tv);
2729 if (linee < 0)
2730 linee = height + linee + 1;
2731
2732 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002733 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002734 if (cols < 0)
2735 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002736 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002737 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002738 if (lines < 0)
2739 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002740 for (line = lines; line < linee
2741 && line + wp->w_winrow < screen_Rows; ++line)
2742 for (col = cols; col < cole
2743 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002744 popup_transparent[(line + wp->w_winrow) * screen_Columns
2745 + col + wp->w_wincol] = val;
2746 }
2747 }
2748}
2749
2750/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002751 * Update "popup_mask" if needed.
2752 * Also recomputes the popup size and positions.
2753 * Also updates "popup_visible".
2754 * Also marks window lines for redrawing.
2755 */
2756 void
2757may_update_popup_mask(int type)
2758{
2759 win_T *wp;
2760 short *mask;
2761 int line, col;
2762 int redraw_all = FALSE;
2763
2764 // Need to recompute when switching tabs.
2765 // Also recompute when the type is CLEAR or NOT_VALID, something basic
2766 // (such as the screen size) must have changed.
2767 if (popup_mask_tab != curtab || type >= NOT_VALID)
2768 {
2769 popup_mask_refresh = TRUE;
2770 redraw_all = TRUE;
2771 }
2772 if (!popup_mask_refresh)
2773 {
2774 // Check if any buffer has changed.
2775 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2776 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2777 popup_mask_refresh = TRUE;
2778 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2779 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2780 popup_mask_refresh = TRUE;
2781 if (!popup_mask_refresh)
2782 return;
2783 }
2784
2785 // Need to update the mask, something has changed.
2786 popup_mask_refresh = FALSE;
2787 popup_mask_tab = curtab;
2788 popup_visible = FALSE;
2789
2790 // If redrawing everything, just update "popup_mask".
2791 // If redrawing only what is needed, update "popup_mask_next" and then
2792 // compare with "popup_mask" to see what changed.
2793 if (type >= SOME_VALID)
2794 mask = popup_mask;
2795 else
2796 mask = popup_mask_next;
2797 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
2798
2799 // Find the window with the lowest zindex that hasn't been handled yet,
2800 // so that the window with a higher zindex overwrites the value in
2801 // popup_mask.
2802 popup_reset_handled();
2803 while ((wp = find_next_popup(TRUE)) != NULL)
2804 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02002805 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002806 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002807
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002808 popup_visible = TRUE;
2809
2810 // Recompute the position if the text changed.
2811 if (redraw_all
2812 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
2813 popup_adjust_position(wp);
2814
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002815 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002816 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002817 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002818 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002819 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002820 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002821 col < wp->w_wincol + width - wp->w_popup_leftoff
2822 && col < screen_Columns; ++col)
2823 if (wp->w_popup_mask_cells == NULL
2824 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002825 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002826 }
2827
2828 // Only check which lines are to be updated if not already
2829 // updating all lines.
2830 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002831 {
2832 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
2833 win_T *prev_wp = NULL;
2834
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002835 for (line = 0; line < screen_Rows; ++line)
2836 {
2837 int col_done = 0;
2838
2839 for (col = 0; col < screen_Columns; ++col)
2840 {
2841 int off = line * screen_Columns + col;
2842
2843 if (popup_mask[off] != popup_mask_next[off])
2844 {
2845 popup_mask[off] = popup_mask_next[off];
2846
2847 if (line >= cmdline_row)
2848 {
2849 // the command line needs to be cleared if text below
2850 // the popup is now visible.
2851 if (!msg_scrolled && popup_mask_next[off] == 0)
2852 clear_cmdline = TRUE;
2853 }
2854 else if (col >= col_done)
2855 {
2856 linenr_T lnum;
2857 int line_cp = line;
2858 int col_cp = col;
2859
2860 // The screen position "line" / "col" needs to be
2861 // redrawn. Figure out what window that is and update
2862 // w_redraw_top and w_redr_bot. Only needs to be done
2863 // once for each window line.
2864 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
2865 if (wp != NULL)
2866 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002867 if (wp != prev_wp)
2868 {
2869 vim_memset(plines_cache, 0, sizeof(int) * Rows);
2870 prev_wp = wp;
2871 }
2872
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002873 if (line_cp >= wp->w_height)
2874 // In (or below) status line
2875 wp->w_redr_status = TRUE;
2876 // compute the position in the buffer line from the
2877 // position on the screen
2878 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002879 &lnum, plines_cache))
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002880 // past bottom
2881 wp->w_redr_status = TRUE;
2882 else
2883 redrawWinline(wp, lnum);
2884
2885 // This line is going to be redrawn, no need to
2886 // check until the right side of the window.
2887 col_done = wp->w_wincol + wp->w_width - 1;
2888 }
2889 }
2890 }
2891 }
2892 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02002893
2894 vim_free(plines_cache);
2895 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002896}
2897
2898/*
2899 * Return a string of "len" spaces in IObuff.
2900 */
2901 static char_u *
2902get_spaces(int len)
2903{
2904 vim_memset(IObuff, ' ', (size_t)len);
2905 IObuff[len] = NUL;
2906 return IObuff;
2907}
2908
2909/*
2910 * Update popup windows. They are drawn on top of normal windows.
2911 * "win_update" is called for each popup window, lowest zindex first.
2912 */
2913 void
2914update_popups(void (*win_update)(win_T *wp))
2915{
2916 win_T *wp;
2917 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002918 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002919 int total_width;
2920 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002921 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002922 int popup_attr;
2923 int border_attr[4];
2924 int border_char[8];
2925 char_u buf[MB_MAXBYTES];
2926 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02002927 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002928 int padcol = 0;
2929 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002930 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02002931 int sb_thumb_top = 0;
2932 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002933 int attr_scroll = 0;
2934 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002935
2936 // Find the window with the lowest zindex that hasn't been updated yet,
2937 // so that the window with a higher zindex is drawn later, thus goes on
2938 // top.
2939 popup_reset_handled();
2940 while ((wp = find_next_popup(TRUE)) != NULL)
2941 {
2942 // This drawing uses the zindex of the popup window, so that it's on
2943 // top of the text but doesn't draw when another popup with higher
2944 // zindex is on top of the character.
2945 screen_zindex = wp->w_zindex;
2946
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002947 // Set flags in popup_transparent[] for masked cells.
2948 update_popup_transparent(wp, 1);
2949
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002950 // adjust w_winrow and w_wincol for border and padding, since
2951 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002952 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02002953 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
2954 - wp->w_popup_leftoff;
2955 if (wp->w_wincol + left_extra < 0)
2956 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002957 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002958 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002959
Bram Moolenaarc2a43162019-06-26 01:03:53 +02002960 // Draw the popup text, unless it's off screen.
2961 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
2962 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002963
2964 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002965 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002966
Bram Moolenaard529ba52019-07-02 23:13:53 +02002967 total_width = popup_width(wp);
2968 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002969 popup_attr = get_wcr_attr(wp);
2970
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002971 if (wp->w_winrow + total_height > cmdline_row)
2972 wp->w_popup_flags |= POPF_ON_CMDLINE;
2973 else
2974 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
2975
2976
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002977 // We can only use these line drawing characters when 'encoding' is
2978 // "utf-8" and 'ambiwidth' is "single".
2979 if (enc_utf8 && *p_ambw == 's')
2980 {
2981 border_char[0] = border_char[2] = 0x2550;
2982 border_char[1] = border_char[3] = 0x2551;
2983 border_char[4] = 0x2554;
2984 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002985 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
2986 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002987 border_char[7] = 0x255a;
2988 }
2989 else
2990 {
2991 border_char[0] = border_char[2] = '-';
2992 border_char[1] = border_char[3] = '|';
2993 for (i = 4; i < 8; ++i)
2994 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002995 if (wp->w_popup_flags & POPF_RESIZE)
2996 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002997 }
2998 for (i = 0; i < 8; ++i)
2999 if (wp->w_border_char[i] != 0)
3000 border_char[i] = wp->w_border_char[i];
3001
3002 for (i = 0; i < 4; ++i)
3003 {
3004 border_attr[i] = popup_attr;
3005 if (wp->w_border_highlight[i] != NULL)
3006 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3007 }
3008
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003009 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003010 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003011 if (wp->w_popup_border[0] > 0)
3012 {
3013 // top border
3014 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003015 wincol < 0 ? 0 : wincol, wincol + total_width,
3016 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003017 ? border_char[4] : border_char[0],
3018 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003019 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003020 {
3021 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3022 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003023 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003024 }
3025 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003026 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3027 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003028
Bram Moolenaard529ba52019-07-02 23:13:53 +02003029 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3030 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003031 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003032 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3033 - wp->w_has_scrollbar;
3034 if (padcol < 0)
3035 {
3036 padwidth += padcol;
3037 padcol = 0;
3038 }
3039 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003040 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003041 {
3042 // top padding
3043 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003044 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003045 ' ', ' ', popup_attr);
3046 }
3047
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003048 // Title goes on top of border or padding.
3049 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003050 {
3051 int len = (int)STRLEN(wp->w_popup_title) + 1;
3052 char_u *title = alloc(len);
3053
3054 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3055 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003056 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003057 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003058
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003059 // Compute scrollbar thumb position and size.
3060 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003061 {
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003062 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3063
Bram Moolenaar68acb412019-06-26 03:40:36 +02003064 sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2)
3065 / linecount;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003066 if (sb_thumb_height == 0)
3067 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003068 if (linecount <= wp->w_height)
3069 // it just fits, avoid divide by zero
3070 sb_thumb_top = 0;
3071 else
3072 sb_thumb_top = (wp->w_topline - 1
3073 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003074 * (wp->w_height - sb_thumb_height)
3075 / (linecount - wp->w_height);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003076 if (wp->w_scrollbar_highlight != NULL)
3077 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3078 else
3079 attr_scroll = highlight_attr[HLF_PSB];
3080 if (wp->w_thumb_highlight != NULL)
3081 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3082 else
3083 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003084 }
3085
3086 for (i = wp->w_popup_border[0];
3087 i < total_height - wp->w_popup_border[2]; ++i)
3088 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003089 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003090 // left and right padding only needed next to the body
3091 int do_padding =
3092 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3093 && i < total_height - wp->w_popup_border[2]
3094 - wp->w_popup_padding[2];
3095
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003096 row = wp->w_winrow + i;
3097
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003098 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003099 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003100 {
3101 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003102 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003103 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003104 if (do_padding && wp->w_popup_padding[3] > 0)
3105 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003106 int col = wincol + wp->w_popup_border[3];
3107
Bram Moolenaard529ba52019-07-02 23:13:53 +02003108 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003109 pad_left = wp->w_popup_padding[3];
3110 if (col < 0)
3111 {
3112 pad_left += col;
3113 col = 0;
3114 }
3115 if (pad_left > 0)
3116 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3117 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003118 // scrollbar
3119 if (wp->w_has_scrollbar)
3120 {
3121 int line = i - top_off;
3122 int scroll_col = wp->w_wincol + total_width - 1
3123 - wp->w_popup_border[1];
3124
3125 if (line >= 0 && line < wp->w_height)
3126 screen_putchar(' ', row, scroll_col,
3127 line >= sb_thumb_top
3128 && line < sb_thumb_top + sb_thumb_height
3129 ? attr_thumb : attr_scroll);
3130 else
3131 screen_putchar(' ', row, scroll_col, popup_attr);
3132 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003133 // right border
3134 if (wp->w_popup_border[1] > 0)
3135 {
3136 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003137 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003138 }
3139 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003140 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003141 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003142 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003143 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3144 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003145 }
3146
3147 if (wp->w_popup_padding[2] > 0)
3148 {
3149 // bottom padding
3150 row = wp->w_winrow + wp->w_popup_border[0]
3151 + wp->w_popup_padding[0] + wp->w_height;
3152 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003153 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003154 }
3155
3156 if (wp->w_popup_border[2] > 0)
3157 {
3158 // bottom border
3159 row = wp->w_winrow + total_height - 1;
3160 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003161 wincol < 0 ? 0 : wincol,
3162 wincol + total_width,
3163 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003164 ? border_char[7] : border_char[2],
3165 border_char[2], border_attr[2]);
3166 if (wp->w_popup_border[1] > 0)
3167 {
3168 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003169 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003170 }
3171 }
3172
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003173 if (wp->w_popup_close == POPCLOSE_BUTTON)
3174 {
3175 // close button goes on top of anything at the top-right corner
3176 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003177 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003178 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3179 }
3180
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003181 update_popup_transparent(wp, 0);
3182
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003183 // Back to the normal zindex.
3184 screen_zindex = 0;
3185 }
3186}
3187
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003188/*
3189 * Mark references in callbacks of one popup window.
3190 */
3191 static int
3192set_ref_in_one_popup(win_T *wp, int copyID)
3193{
3194 int abort = FALSE;
3195 typval_T tv;
3196
3197 if (wp->w_close_cb.cb_partial != NULL)
3198 {
3199 tv.v_type = VAR_PARTIAL;
3200 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3201 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3202 }
3203 if (wp->w_filter_cb.cb_partial != NULL)
3204 {
3205 tv.v_type = VAR_PARTIAL;
3206 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3207 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3208 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003209 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003210 return abort;
3211}
3212
3213/*
3214 * Set reference in callbacks of popup windows.
3215 */
3216 int
3217set_ref_in_popups(int copyID)
3218{
3219 int abort = FALSE;
3220 win_T *wp;
3221 tabpage_T *tp;
3222
3223 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3224 abort = abort || set_ref_in_one_popup(wp, copyID);
3225
3226 FOR_ALL_TABPAGES(tp)
3227 {
3228 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3229 abort = abort || set_ref_in_one_popup(wp, copyID);
3230 if (abort)
3231 break;
3232 }
3233 return abort;
3234}
Bram Moolenaar79648732019-07-18 21:43:07 +02003235
3236/*
3237 * Find an existing popup used as the preview window, in the current tab page.
3238 * Return NULL if not found.
3239 */
3240 win_T *
3241popup_find_preview_window(void)
3242{
3243 win_T *wp;
3244
3245 // Preview window popup is always local to tab page.
3246 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3247 if (wp->w_p_pvw)
3248 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003249 return NULL;
3250}
3251
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003252 int
3253popup_is_popup(win_T *wp)
3254{
3255 return wp->w_popup_flags != 0;
3256}
3257
3258/*
3259 * Find an existing popup used as the info window, in the current tab page.
3260 * Return NULL if not found.
3261 */
3262 win_T *
3263popup_find_info_window(void)
3264{
3265 win_T *wp;
3266
3267 // info window popup is always local to tab page.
3268 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3269 if (wp->w_popup_flags & POPF_INFO)
3270 return wp;
3271 return NULL;
3272}
3273
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003274 void
3275f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv)
3276{
3277 win_T *wp = popup_find_preview_window();
3278
3279 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003280}
3281
Bram Moolenaar79648732019-07-18 21:43:07 +02003282/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003283 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003284 * NOTE: this makes the popup the current window, so that the file can be
3285 * edited. However, it must not remain to be the current window, the caller
3286 * must make sure of that.
3287 */
3288 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003289popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003290{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003291 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003292
3293 if (wp == NULL)
3294 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003295 if (info)
3296 wp->w_popup_flags |= POPF_INFO;
3297 else
3298 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003299
3300 // Set the width to a reasonable value, so that w_topline can be computed.
3301 if (wp->w_minwidth > 0)
3302 wp->w_width = wp->w_minwidth;
3303 else if (wp->w_maxwidth > 0)
3304 wp->w_width = wp->w_maxwidth;
3305 else
3306 wp->w_width = curwin->w_width;
3307
3308 // Will switch to another buffer soon, dummy one can be wiped.
3309 wp->w_buffer->b_locked = FALSE;
3310
3311 win_enter(wp, FALSE);
3312 return OK;
3313}
3314
3315 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003316popup_close_preview(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003317{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003318 win_T *wp = info ? popup_find_info_window() : popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003319
3320 if (wp != NULL)
3321 {
3322 typval_T res;
3323
3324 res.v_type = VAR_NUMBER;
3325 res.vval.v_number = -1;
3326 popup_close_and_callback(wp, &res);
3327 }
3328}
3329
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003330/*
3331 * Set the title of the popup window to the file name.
3332 */
3333 void
3334popup_set_title(win_T *wp)
3335{
3336 if (wp->w_buffer->b_fname != NULL)
3337 {
3338 char_u dirname[MAXPATHL];
3339 size_t len;
3340
3341 mch_dirname(dirname, MAXPATHL);
3342 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3343
3344 vim_free(wp->w_popup_title);
3345 len = STRLEN(wp->w_buffer->b_fname) + 3;
3346 wp->w_popup_title = alloc((int)len);
3347 if (wp->w_popup_title != NULL)
3348 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3349 wp->w_buffer->b_fname);
3350 redraw_win_later(wp, VALID);
3351 }
3352}
3353
3354/*
3355 * If there is a preview window, update the title.
3356 * Used after changing directory.
3357 */
3358 void
3359popup_update_preview_title(void)
3360{
3361 win_T *wp = popup_find_preview_window();
3362
3363 if (wp != NULL)
3364 popup_set_title(wp);
3365}
3366
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003367#endif // FEAT_TEXT_PROP