blob: 06812073e60b269c50a9ab10945f120ddd30df44 [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 Moolenaar5843f5f2019-08-20 20:13:45 +020031static void popup_adjust_position(win_T *wp);
32
Bram Moolenaar4d784b22019-05-25 19:51:39 +020033/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020034 * Get option value for "key", which is "line" or "col".
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020035 * Handles "cursor+N" and "cursor-N".
Bram Moolenaar1fb08312019-08-29 20:02:11 +020036 * Returns MAXCOL if the entry is not present.
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020037 */
38 static int
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020039popup_options_one(dict_T *dict, char_u *key)
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020040{
41 dictitem_T *di;
42 char_u *val;
43 char_u *s;
44 char_u *endp;
45 int n = 0;
46
47 di = dict_find(dict, key, -1);
48 if (di == NULL)
Bram Moolenaar1fb08312019-08-29 20:02:11 +020049 return MAXCOL;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020050
51 val = tv_get_string(&di->di_tv);
52 if (STRNCMP(val, "cursor", 6) != 0)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020053 return dict_get_number_check(dict, key);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020054
55 setcursor_mayforce(TRUE);
56 s = val + 6;
57 if (*s != NUL)
58 {
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +020059 endp = s;
60 if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
61 n = strtol((char *)s, (char **)&endp, 10);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020062 if (endp != NULL && *skipwhite(endp) != NUL)
63 {
64 semsg(_(e_invexpr2), val);
65 return 0;
66 }
67 }
68
69 if (STRCMP(key, "line") == 0)
70 n = screen_screenrow() + 1 + n;
71 else // "col"
72 n = screen_screencol() + 1 + n;
73
74 if (n < 1)
75 n = 1;
76 return n;
77}
78
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020079 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020080set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081{
82 dictitem_T *di;
83
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020084 di = dict_find(dict, (char_u *)name, -1);
85 if (di != NULL)
86 {
87 if (di->di_tv.v_type != VAR_LIST)
88 emsg(_(e_listreq));
89 else
90 {
91 list_T *list = di->di_tv.vval.v_list;
92 listitem_T *li;
93 int i;
94 int nr;
95
96 for (i = 0; i < 4; ++i)
97 array[i] = 1;
98 if (list != NULL)
99 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
100 ++i, li = li->li_next)
101 {
102 nr = (int)tv_get_number(&li->li_tv);
103 if (nr >= 0)
104 array[i] = nr > max_val ? max_val : nr;
105 }
106 }
107 }
108}
109
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200110/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200111 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200112 */
113 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200114set_moved_values(win_T *wp)
115{
116 wp->w_popup_curwin = curwin;
117 wp->w_popup_lnum = curwin->w_cursor.lnum;
118 wp->w_popup_mincol = curwin->w_cursor.col;
119 wp->w_popup_maxcol = curwin->w_cursor.col;
120}
121
122/*
123 * Used when popup options contain "moved" with "word" or "WORD".
124 */
125 static void
126set_moved_columns(win_T *wp, int flags)
127{
128 char_u *ptr;
129 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
130
131 if (len > 0)
132 {
133 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
134 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
135 }
136}
137
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200138/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200139 * Used when popup options contain "mousemoved": set default moved values.
140 */
141 static void
142set_mousemoved_values(win_T *wp)
143{
144 wp->w_popup_mouse_row = mouse_row;
145 wp->w_popup_mouse_mincol = mouse_col;
146 wp->w_popup_mouse_maxcol = mouse_col;
147}
148
149/*
150 * Used when popup options contain "moved" with "word" or "WORD".
151 */
152 static void
153set_mousemoved_columns(win_T *wp, int flags)
154{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200155 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200156 char_u *text;
157 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200158 pos_T pos;
159 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200160
161 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200162 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200163 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200164 // convert text column to mouse column
165 pos.col = col;
166 pos.coladd = 0;
167 getvcol(textwp, &pos, &mcol, NULL, NULL);
168 wp->w_popup_mouse_mincol = mcol;
169
Bram Moolenaar10727682019-07-12 13:59:20 +0200170 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200171 getvcol(textwp, &pos, NULL, NULL, &mcol);
172 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200173 vim_free(text);
174 }
175}
176
177/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200178 * Return TRUE if "row"/"col" is on the border of the popup.
179 * The values are relative to the top-left corner.
180 */
181 int
182popup_on_border(win_T *wp, int row, int col)
183{
184 return (row == 0 && wp->w_popup_border[0] > 0)
185 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
186 || (col == 0 && wp->w_popup_border[3] > 0)
187 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
188}
189
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200190/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200191 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
192 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200193 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200194 * Caller should check the left mouse button was clicked.
195 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200196 */
197 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200198popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200199{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200200 if (wp->w_popup_close == POPCLOSE_BUTTON
201 && row == 0 && col == popup_width(wp) - 1)
202 {
203 popup_close_for_mouse_click(wp);
204 return TRUE;
205 }
206 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200207}
208
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200209// Values set when dragging a popup window starts.
210static int drag_start_row;
211static int drag_start_col;
212static int drag_start_wantline;
213static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200214static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200215
216/*
217 * Mouse down on border of popup window: start dragging it.
218 * Uses mouse_col and mouse_row.
219 */
220 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200221popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200222{
223 drag_start_row = mouse_row;
224 drag_start_col = mouse_col;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200225 if (wp->w_wantline == 0)
226 drag_start_wantline = wp->w_winrow + 1;
227 else
228 drag_start_wantline = wp->w_wantline;
229 if (wp->w_wantcol == 0)
230 drag_start_wantcol = wp->w_wincol + 1;
231 else
232 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200233
234 // Stop centering the popup
235 if (wp->w_popup_pos == POPPOS_CENTER)
236 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200237
238 drag_on_resize_handle = wp->w_popup_border[1] > 0
239 && wp->w_popup_border[2] > 0
240 && row == popup_height(wp) - 1
241 && col == popup_width(wp) - 1;
242
243 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
244 {
245 if (wp->w_popup_pos == POPPOS_TOPRIGHT
246 || wp->w_popup_pos == POPPOS_BOTRIGHT)
247 wp->w_wantcol = wp->w_wincol + 1;
248 if (wp->w_popup_pos == POPPOS_BOTLEFT)
249 wp->w_wantline = wp->w_winrow + 1;
250 wp->w_popup_pos = POPPOS_TOPLEFT;
251 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200252}
253
254/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200255 * Mouse moved while dragging a popup window: adjust the window popup position
256 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200257 */
258 void
259popup_drag(win_T *wp)
260{
261 // The popup may be closed before dragging stops.
262 if (!win_valid_popup(wp))
263 return;
264
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200265 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
266 {
267 int width_inc = mouse_col - drag_start_col;
268 int height_inc = mouse_row - drag_start_row;
269
270 if (width_inc != 0)
271 {
272 int width = wp->w_width + width_inc;
273
274 if (width < 1)
275 width = 1;
276 wp->w_minwidth = width;
277 wp->w_maxwidth = width;
278 drag_start_col = mouse_col;
279 }
280
281 if (height_inc != 0)
282 {
283 int height = wp->w_height + height_inc;
284
285 if (height < 1)
286 height = 1;
287 wp->w_minheight = height;
288 wp->w_maxheight = height;
289 drag_start_row = mouse_row;
290 }
291
292 popup_adjust_position(wp);
293 return;
294 }
295
296 if (!(wp->w_popup_flags & POPF_DRAG))
297 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200298 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
299 if (wp->w_wantline < 1)
300 wp->w_wantline = 1;
301 if (wp->w_wantline > Rows)
302 wp->w_wantline = Rows;
303 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
304 if (wp->w_wantcol < 1)
305 wp->w_wantcol = 1;
306 if (wp->w_wantcol > Columns)
307 wp->w_wantcol = Columns;
308
309 popup_adjust_position(wp);
310}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200311
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200312/*
313 * Set w_firstline to match the current "wp->w_topline".
314 */
315 void
316popup_set_firstline(win_T *wp)
317{
318 int height = wp->w_height;
319
320 wp->w_firstline = wp->w_topline;
321 popup_adjust_position(wp);
322
323 // we don't want the popup to get smaller, decrement the first line
324 // until it doesn't
325 while (wp->w_firstline > 1 && wp->w_height < height)
326 {
327 --wp->w_firstline;
328 popup_adjust_position(wp);
329 }
330}
331
332/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200333 * Return TRUE if the position is in the popup window scrollbar.
334 */
335 int
336popup_is_in_scrollbar(win_T *wp, int row, int col)
337{
338 return wp->w_has_scrollbar
339 && row >= wp->w_popup_border[0]
340 && row < popup_height(wp) - wp->w_popup_border[2]
341 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
342}
343
344
345/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200346 * Handle a click in a popup window, if it is in the scrollbar.
347 */
348 void
349popup_handle_scrollbar_click(win_T *wp, int row, int col)
350{
351 int height = popup_height(wp);
352 int old_topline = wp->w_topline;
353
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200354 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200355 {
356 if (row >= height / 2)
357 {
358 // Click in lower half, scroll down.
359 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
360 ++wp->w_topline;
361 }
362 else if (wp->w_topline > 1)
363 // click on upper half, scroll up.
364 --wp->w_topline;
365 if (wp->w_topline != old_topline)
366 {
367 popup_set_firstline(wp);
368 redraw_win_later(wp, NOT_VALID);
369 }
370 }
371}
372
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200373#if defined(FEAT_TIMERS)
374 static void
375popup_add_timeout(win_T *wp, int time)
376{
377 char_u cbbuf[50];
378 char_u *ptr = cbbuf;
379 typval_T tv;
380
381 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
382 "{_ -> popup_close(%d)}", wp->w_id);
383 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
384 {
385 wp->w_popup_timer = create_timer(time, 0);
386 wp->w_popup_timer->tr_callback = get_callback(&tv);
387 clear_tv(&tv);
388 }
389}
390#endif
391
Bram Moolenaar17627312019-06-02 19:53:44 +0200392/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200393 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200394 */
395 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200396apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200397{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200398 int nr;
399 char_u *str;
400 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200401
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200402 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200403 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200404 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200405 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200406 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200407 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200408 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200409 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200410
411 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200412 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200413 wp->w_wantline = nr;
414 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200415 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200416 wp->w_wantcol = nr;
417
418 di = dict_find(d, (char_u *)"fixed", -1);
419 if (di != NULL)
420 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
421
422 str = dict_get_string(d, (char_u *)"pos", FALSE);
423 if (str != NULL)
424 {
425 for (nr = 0;
426 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
427 ++nr)
428 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
429 {
430 wp->w_popup_pos = poppos_entries[nr].pp_val;
431 nr = -1;
432 break;
433 }
434 if (nr != -1)
435 semsg(_(e_invarg2), str);
436 }
437
438 str = dict_get_string(d, (char_u *)"textprop", FALSE);
439 if (str != NULL)
440 {
441 wp->w_popup_prop_type = 0;
442 if (*str != NUL)
443 {
444 nr = find_prop_type_id(str, wp->w_buffer);
445 if (nr <= 0)
446 nr = find_prop_type_id(str, NULL);
447 if (nr <= 0)
448 semsg(_(e_invarg2), str);
449 else
450 {
451 wp->w_popup_prop_type = nr;
452 wp->w_popup_prop_win = curwin;
453
454 di = dict_find(d, (char_u *)"textpropwin", -1);
455 if (di != NULL)
456 {
457 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
458 if (win_valid(wp->w_popup_prop_win))
459 wp->w_popup_prop_win = curwin;
460 }
461 }
462 }
463 }
464
465 di = dict_find(d, (char_u *)"textpropid", -1);
466 if (di != NULL)
467 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200468}
469
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200470 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200471handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
472{
473 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
474 {
475 char_u *s = di->di_tv.vval.v_string;
476 int flags = 0;
477
478 if (STRCMP(s, "word") == 0)
479 flags = FIND_IDENT | FIND_STRING;
480 else if (STRCMP(s, "WORD") == 0)
481 flags = FIND_STRING;
482 else if (STRCMP(s, "expr") == 0)
483 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
484 else if (STRCMP(s, "any") != 0)
485 semsg(_(e_invarg2), s);
486 if (flags != 0)
487 {
488 if (mousemoved)
489 set_mousemoved_columns(wp, flags);
490 else
491 set_moved_columns(wp, flags);
492 }
493 }
494 else if (di->di_tv.v_type == VAR_LIST
495 && di->di_tv.vval.v_list != NULL
496 && di->di_tv.vval.v_list->lv_len == 2)
497 {
498 list_T *l = di->di_tv.vval.v_list;
499 int mincol = tv_get_number(&l->lv_first->li_tv);
500 int maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
501
502 if (mousemoved)
503 {
504 wp->w_popup_mouse_mincol = mincol;
505 wp->w_popup_mouse_maxcol = maxcol;
506 }
507 else
508 {
509 wp->w_popup_mincol = mincol;
510 wp->w_popup_maxcol = maxcol;
511 }
512 }
513 else
514 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
515}
516
517 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200518check_highlight(dict_T *dict, char *name, char_u **pval)
519{
520 dictitem_T *di;
521 char_u *str;
522
523 di = dict_find(dict, (char_u *)name, -1);
524 if (di != NULL)
525 {
526 if (di->di_tv.v_type != VAR_STRING)
527 semsg(_(e_invargval), name);
528 else
529 {
530 str = tv_get_string(&di->di_tv);
531 if (*str != NUL)
532 *pval = vim_strsave(str);
533 }
534 }
535}
536
Bram Moolenaarae943152019-06-16 22:54:14 +0200537/*
Bram Moolenaar79648732019-07-18 21:43:07 +0200538 * Scroll to show the line with the cursor. This assumes lines don't wrap.
539 */
540 static void
541popup_show_curline(win_T *wp)
542{
543 if (wp->w_cursor.lnum < wp->w_topline)
544 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200545 else if (wp->w_cursor.lnum >= wp->w_botline
546 && (curwin->w_valid & VALID_BOTLINE))
547 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200548 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200549 if (wp->w_topline < 1)
550 wp->w_topline = 1;
551 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
552 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
553 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200554
555 // Don't use "firstline" now.
556 wp->w_firstline = 0;
557}
558
559/*
560 * Get the sign group name for window "wp".
561 * Returns a pointer to a static buffer, overwritten on the next call.
562 */
563 static char_u *
564popup_get_sign_name(win_T *wp)
565{
566 static char buf[30];
567
568 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
569 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200570}
571
572/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200573 * Highlight the line with the cursor.
574 * Also scrolls the text to put the cursor line in view.
575 */
576 static void
577popup_highlight_curline(win_T *wp)
578{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200579 int sign_id = 0;
580 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200581
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200582 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200583
584 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
585 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200586 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200587
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200588 if (!sign_exists_by_name(sign_name))
589 {
590 char *linehl = "PopupSelected";
591
592 if (syn_name2id((char_u *)linehl) == 0)
593 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200594 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200595 }
596
597 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
598 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
599 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200600 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200601 else
602 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200603 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200604}
605
606/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200607 * Shared between popup_create() and f_popup_setoptions().
608 */
609 static void
610apply_general_options(win_T *wp, dict_T *dict)
611{
612 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200613 int nr;
614 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200615
Bram Moolenaarae943152019-06-16 22:54:14 +0200616 // TODO: flip
617
618 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200619 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200620 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200621 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200622 if (wp->w_firstline < 0)
623 wp->w_firstline = -1;
624 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200625
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200626 di = dict_find(dict, (char_u *)"scrollbar", -1);
627 if (di != NULL)
628 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
629
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200630 str = dict_get_string(dict, (char_u *)"title", FALSE);
631 if (str != NULL)
632 {
633 vim_free(wp->w_popup_title);
634 wp->w_popup_title = vim_strsave(str);
635 }
636
Bram Moolenaar402502d2019-05-30 22:07:36 +0200637 di = dict_find(dict, (char_u *)"wrap", -1);
638 if (di != NULL)
639 {
640 nr = dict_get_number(dict, (char_u *)"wrap");
641 wp->w_p_wrap = nr != 0;
642 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200643
Bram Moolenaara42d9452019-06-15 21:46:30 +0200644 di = dict_find(dict, (char_u *)"drag", -1);
645 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200646 {
647 nr = dict_get_number(dict, (char_u *)"drag");
648 if (nr)
649 wp->w_popup_flags |= POPF_DRAG;
650 else
651 wp->w_popup_flags &= ~POPF_DRAG;
652 }
653
654 di = dict_find(dict, (char_u *)"resize", -1);
655 if (di != NULL)
656 {
657 nr = dict_get_number(dict, (char_u *)"resize");
658 if (nr)
659 wp->w_popup_flags |= POPF_RESIZE;
660 else
661 wp->w_popup_flags &= ~POPF_RESIZE;
662 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200663
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200664 di = dict_find(dict, (char_u *)"close", -1);
665 if (di != NULL)
666 {
667 int ok = TRUE;
668
669 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
670 {
671 char_u *s = di->di_tv.vval.v_string;
672
673 if (STRCMP(s, "none") == 0)
674 wp->w_popup_close = POPCLOSE_NONE;
675 else if (STRCMP(s, "button") == 0)
676 wp->w_popup_close = POPCLOSE_BUTTON;
677 else if (STRCMP(s, "click") == 0)
678 wp->w_popup_close = POPCLOSE_CLICK;
679 else
680 ok = FALSE;
681 }
682 else
683 ok = FALSE;
684 if (!ok)
685 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
686 }
687
Bram Moolenaarae943152019-06-16 22:54:14 +0200688 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
689 if (str != NULL)
690 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
691 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200692
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200693 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
694 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200695 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
696 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200697
Bram Moolenaar790498b2019-06-01 22:15:29 +0200698 di = dict_find(dict, (char_u *)"borderhighlight", -1);
699 if (di != NULL)
700 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200701 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200702 emsg(_(e_listreq));
703 else
704 {
705 list_T *list = di->di_tv.vval.v_list;
706 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200707 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200708
Bram Moolenaar403d0902019-07-17 21:37:32 +0200709 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
710 ++i, li = li->li_next)
711 {
712 str = tv_get_string(&li->li_tv);
713 if (*str != NUL)
714 wp->w_border_highlight[i] = vim_strsave(str);
715 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200716 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
717 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200718 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200719 vim_strsave(wp->w_border_highlight[0]);
720 }
721 }
722
Bram Moolenaar790498b2019-06-01 22:15:29 +0200723 di = dict_find(dict, (char_u *)"borderchars", -1);
724 if (di != NULL)
725 {
726 if (di->di_tv.v_type != VAR_LIST)
727 emsg(_(e_listreq));
728 else
729 {
730 list_T *list = di->di_tv.vval.v_list;
731 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200732 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200733
734 if (list != NULL)
735 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
736 ++i, li = li->li_next)
737 {
738 str = tv_get_string(&li->li_tv);
739 if (*str != NUL)
740 wp->w_border_char[i] = mb_ptr2char(str);
741 }
742 if (list->lv_len == 1)
743 for (i = 1; i < 8; ++i)
744 wp->w_border_char[i] = wp->w_border_char[0];
745 if (list->lv_len == 2)
746 {
747 for (i = 4; i < 8; ++i)
748 wp->w_border_char[i] = wp->w_border_char[1];
749 for (i = 1; i < 4; ++i)
750 wp->w_border_char[i] = wp->w_border_char[0];
751 }
752 }
753 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200754
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200755 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
756 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
757
Bram Moolenaarae943152019-06-16 22:54:14 +0200758 di = dict_find(dict, (char_u *)"zindex", -1);
759 if (di != NULL)
760 {
761 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
762 if (wp->w_zindex < 1)
763 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
764 if (wp->w_zindex > 32000)
765 wp->w_zindex = 32000;
766 }
767
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200768 di = dict_find(dict, (char_u *)"mask", -1);
769 if (di != NULL)
770 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200771 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200772
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200773 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200774 {
775 listitem_T *li;
776
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200777 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200778 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
779 li = li->li_next)
780 {
781 if (li->li_tv.v_type != VAR_LIST
782 || li->li_tv.vval.v_list == NULL
783 || li->li_tv.vval.v_list->lv_len != 4)
784 {
785 ok = FALSE;
786 break;
787 }
788 }
789 }
790 if (ok)
791 {
792 wp->w_popup_mask = di->di_tv.vval.v_list;
793 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200794 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200795 }
796 else
797 semsg(_(e_invargval), "mask");
798 }
799
Bram Moolenaarae943152019-06-16 22:54:14 +0200800#if defined(FEAT_TIMERS)
801 // Add timer to close the popup after some time.
802 nr = dict_get_number(dict, (char_u *)"time");
803 if (nr > 0)
804 popup_add_timeout(wp, nr);
805#endif
806
Bram Moolenaar3397f742019-06-02 18:40:06 +0200807 di = dict_find(dict, (char_u *)"moved", -1);
808 if (di != NULL)
809 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200810 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200811 handle_moved_argument(wp, di, FALSE);
812 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200813
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200814 di = dict_find(dict, (char_u *)"mousemoved", -1);
815 if (di != NULL)
816 {
817 set_mousemoved_values(wp);
818 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200819 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200820
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200821 di = dict_find(dict, (char_u *)"cursorline", -1);
822 if (di != NULL)
823 {
824 if (di->di_tv.v_type == VAR_NUMBER)
825 {
826 if (di->di_tv.vval.v_number != 0)
827 wp->w_popup_flags |= POPF_CURSORLINE;
828 else
829 wp->w_popup_flags &= ~POPF_CURSORLINE;
830 }
831 else
832 semsg(_(e_invargval), "cursorline");
833 }
834
Bram Moolenaarae943152019-06-16 22:54:14 +0200835 di = dict_find(dict, (char_u *)"filter", -1);
836 if (di != NULL)
837 {
838 callback_T callback = get_callback(&di->di_tv);
839
840 if (callback.cb_name != NULL)
841 {
842 free_callback(&wp->w_filter_cb);
843 set_callback(&wp->w_filter_cb, &callback);
844 }
845 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200846 di = dict_find(dict, (char_u *)"mapping", -1);
847 if (di != NULL)
848 {
849 nr = dict_get_number(dict, (char_u *)"mapping");
850 if (nr)
851 wp->w_popup_flags |= POPF_MAPPING;
852 else
853 wp->w_popup_flags &= ~POPF_MAPPING;
854 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200855
Bram Moolenaar581ba392019-09-03 22:08:33 +0200856 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
857 if (str != NULL)
858 {
859 if (STRCMP(str, "a") == 0)
860 wp->w_filter_mode = MODE_ALL;
861 else
862 wp->w_filter_mode = mode_str2flags(str);
863 }
864
Bram Moolenaarae943152019-06-16 22:54:14 +0200865 di = dict_find(dict, (char_u *)"callback", -1);
866 if (di != NULL)
867 {
868 callback_T callback = get_callback(&di->di_tv);
869
870 if (callback.cb_name != NULL)
871 {
872 free_callback(&wp->w_close_cb);
873 set_callback(&wp->w_close_cb, &callback);
874 }
875 }
876}
877
878/*
879 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200880 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200881 */
882 static void
883apply_options(win_T *wp, dict_T *dict)
884{
885 int nr;
886
887 apply_move_options(wp, dict);
888 apply_general_options(wp, dict);
889
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200890 nr = dict_get_number(dict, (char_u *)"hidden");
891 if (nr > 0)
892 {
893 wp->w_popup_flags |= POPF_HIDDEN;
894 --wp->w_buffer->b_nwindows;
895 }
896
Bram Moolenaar33796b32019-06-08 16:01:13 +0200897 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200898 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200899}
900
901/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200902 * Add lines to the popup from a list of strings.
903 */
904 static void
905add_popup_strings(buf_T *buf, list_T *l)
906{
907 listitem_T *li;
908 linenr_T lnum = 0;
909 char_u *p;
910
911 for (li = l->lv_first; li != NULL; li = li->li_next)
912 if (li->li_tv.v_type == VAR_STRING)
913 {
914 p = li->li_tv.vval.v_string;
915 ml_append_buf(buf, lnum++,
916 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
917 }
918}
919
920/*
921 * Add lines to the popup from a list of dictionaries.
922 */
923 static void
924add_popup_dicts(buf_T *buf, list_T *l)
925{
926 listitem_T *li;
927 listitem_T *pli;
928 linenr_T lnum = 0;
929 char_u *p;
930 dict_T *dict;
931
932 // first add the text lines
933 for (li = l->lv_first; li != NULL; li = li->li_next)
934 {
935 if (li->li_tv.v_type != VAR_DICT)
936 {
937 emsg(_(e_dictreq));
938 return;
939 }
940 dict = li->li_tv.vval.v_dict;
941 p = dict == NULL ? NULL
942 : dict_get_string(dict, (char_u *)"text", FALSE);
943 ml_append_buf(buf, lnum++,
944 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
945 }
946
947 // add the text properties
948 lnum = 1;
949 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
950 {
951 dictitem_T *di;
952 list_T *plist;
953
954 dict = li->li_tv.vval.v_dict;
955 di = dict_find(dict, (char_u *)"props", -1);
956 if (di != NULL)
957 {
958 if (di->di_tv.v_type != VAR_LIST)
959 {
960 emsg(_(e_listreq));
961 return;
962 }
963 plist = di->di_tv.vval.v_list;
964 if (plist != NULL)
965 {
966 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
967 {
968 if (pli->li_tv.v_type != VAR_DICT)
969 {
970 emsg(_(e_dictreq));
971 return;
972 }
973 dict = pli->li_tv.vval.v_dict;
974 if (dict != NULL)
975 {
976 int col = dict_get_number(dict, (char_u *)"col");
977
978 prop_add_common( lnum, col, dict, buf, NULL);
979 }
980 }
981 }
982 }
983 }
984}
985
986/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200987 * Get the padding plus border at the top, adjusted to 1 if there is a title.
988 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +0200989 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200990popup_top_extra(win_T *wp)
991{
992 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
993
994 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
995 return 1;
996 return extra;
997}
998
999/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001000 * Return the height of popup window "wp", including border and padding.
1001 */
1002 int
1003popup_height(win_T *wp)
1004{
1005 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001006 + popup_top_extra(wp)
1007 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001008}
1009
1010/*
1011 * Return the width of popup window "wp", including border, padding and
1012 * scrollbar.
1013 */
1014 int
1015popup_width(win_T *wp)
1016{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001017 // w_leftcol is how many columns of the core are left of the screen
1018 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001019 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001020 + popup_extra_width(wp)
1021 + wp->w_popup_rightoff;
1022}
1023
1024/*
1025 * Return the extra width of popup window "wp": border, padding and scrollbar.
1026 */
1027 int
1028popup_extra_width(win_T *wp)
1029{
1030 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1031 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1032 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001033}
1034
1035/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001036 * Adjust the position and size of the popup to fit on the screen.
1037 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001038 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001039popup_adjust_position(win_T *wp)
1040{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001041 linenr_T lnum;
1042 int wrapped = 0;
1043 int maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001044 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001045 int center_vert = FALSE;
1046 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001047 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001048 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001049 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1050 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1051 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1052 int extra_height = top_extra + bot_extra;
1053 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001054 int org_winrow = wp->w_winrow;
1055 int org_wincol = wp->w_wincol;
1056 int org_width = wp->w_width;
1057 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001058 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001059 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001060 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001061 int wantline = wp->w_wantline; // adjusted for textprop
1062 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001063
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001064 wp->w_winrow = 0;
1065 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001066 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001067 wp->w_popup_leftoff = 0;
1068 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001069
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001070 // May need to update the "cursorline" highlighting, which may also change
1071 // "topline"
1072 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1073 popup_highlight_curline(wp);
1074
Bram Moolenaar12034e22019-08-25 22:25:02 +02001075 // If no line was specified default to vertical centering.
1076 if (wantline == 0)
1077 center_vert = TRUE;
1078
1079 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1080 {
1081 win_T *prop_win = wp->w_popup_prop_win;
1082 textprop_T prop;
1083 linenr_T prop_lnum;
1084 pos_T pos;
1085 int screen_row;
1086 int screen_scol;
1087 int screen_ccol;
1088 int screen_ecol;
1089
1090 // Popup window is positioned relative to a text property.
1091 if (find_visible_prop(prop_win,
1092 wp->w_popup_prop_type, wp->w_popup_prop_id,
1093 &prop, &prop_lnum) == FAIL)
1094 {
1095 // Text property is no longer visible, hide the popup.
1096 // Unhiding the popup is done in check_popup_unhidden().
1097 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1098 {
1099 wp->w_popup_flags |= POPF_HIDDEN;
1100 --wp->w_buffer->b_nwindows;
1101 if (win_valid(wp->w_popup_prop_win))
1102 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1103 }
1104 return;
1105 }
1106
1107 // Compute the desired position from the position of the text
1108 // property. Use "wantline" and "wantcol" as offsets.
1109 pos.lnum = prop_lnum;
1110 pos.col = prop.tp_col;
1111 if (wp->w_popup_pos == POPPOS_TOPLEFT
1112 || wp->w_popup_pos == POPPOS_BOTLEFT)
1113 pos.col += prop.tp_len - 1;
1114 textpos2screenpos(prop_win, &pos, &screen_row,
1115 &screen_scol, &screen_ccol, &screen_ecol);
1116
1117 if (wp->w_popup_pos == POPPOS_TOPLEFT
1118 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1119 // below the text
1120 wantline = screen_row + wantline + 1;
1121 else
1122 // above the text
1123 wantline = screen_row + wantline - 1;
1124 center_vert = FALSE;
1125 if (wp->w_popup_pos == POPPOS_TOPLEFT
1126 || wp->w_popup_pos == POPPOS_BOTLEFT)
1127 // right of the text
1128 wantcol = screen_ecol + wantcol;
1129 else
1130 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001131 wantcol = screen_scol + wantcol - 2;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001132 }
1133
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001134 if (wp->w_popup_pos == POPPOS_CENTER)
1135 {
1136 // center after computing the size
1137 center_vert = TRUE;
1138 center_hor = TRUE;
1139 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001140 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001141 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001142 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
Bram Moolenaar12034e22019-08-25 22:25:02 +02001143 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001144 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001145 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001146 if (wp->w_winrow >= Rows)
1147 wp->w_winrow = Rows - 1;
1148 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001149
Bram Moolenaar12034e22019-08-25 22:25:02 +02001150 if (wantcol == 0)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001151 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001152 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1153 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001154 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001155 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001156 if (wp->w_wincol >= Columns - 3)
1157 wp->w_wincol = Columns - 3;
1158 }
1159 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001160
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001161 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001162 // When left aligned use the space available, but shift to the left when we
1163 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001164 maxspace = Columns - wp->w_wincol - left_extra;
1165 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001166 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001167 {
1168 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001169 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001170 }
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001171
Bram Moolenaar8d241042019-06-12 23:40:01 +02001172 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001173 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001174 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001175 if (wp->w_topline < 1)
1176 wp->w_topline = 1;
1177 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001178 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1179
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001180 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001181 // Use a minimum width of one, so that something shows when there is no
1182 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001183 // When "firstline" is -1 then start with the last buffer line and go
1184 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001185 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001186 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001187 if (wp->w_firstline < 0)
1188 lnum = wp->w_buffer->b_ml.ml_line_count;
1189 else
1190 lnum = wp->w_topline;
1191 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001192 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001193 int len;
1194 int w_width = wp->w_width;
1195
1196 // Count Tabs for what they are worth and compute the length based on
1197 // the maximum width (matters when 'showbreak' is set).
1198 if (wp->w_width < maxwidth)
1199 wp->w_width = maxwidth;
1200 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001201 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001202 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001204 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001205 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001206 while (len > maxwidth)
1207 {
1208 ++wrapped;
1209 len -= maxwidth;
1210 wp->w_width = maxwidth;
1211 }
1212 }
1213 else if (len > maxwidth
1214 && allow_adjust_left
1215 && (wp->w_popup_pos == POPPOS_TOPLEFT
1216 || wp->w_popup_pos == POPPOS_BOTLEFT))
1217 {
1218 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001219 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001220
Bram Moolenaar51c31312019-06-15 22:27:23 +02001221 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001222 {
1223 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001224
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001225 len -= truncate_shift;
1226 shift_by -= truncate_shift;
1227 }
1228
1229 wp->w_wincol -= shift_by;
1230 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001231 wp->w_width = maxwidth;
1232 }
1233 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001234 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001235 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001236 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1237 wp->w_width = wp->w_maxwidth;
1238 }
Bram Moolenaar8d241042019-06-12 23:40:01 +02001239 // do not use the width of lines we're not going to show
Bram Moolenaare296e312019-07-03 23:20:18 +02001240 if (wp->w_maxheight > 0
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001241 && (wp->w_firstline >= 0
1242 ? lnum - wp->w_topline
1243 : wp->w_buffer->b_ml.ml_line_count - lnum)
1244 + 1 + wrapped > wp->w_maxheight)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001245 break;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001246
1247 if (wp->w_firstline < 0)
1248 --lnum;
1249 else
1250 ++lnum;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001251 }
1252
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001253 if (wp->w_firstline < 0)
1254 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1255
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001256 wp->w_has_scrollbar = wp->w_want_scrollbar
1257 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001258 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001259 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001260 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001261 ++extra_width;
1262 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001263
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001264 minwidth = wp->w_minwidth;
1265 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1266 {
1267 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1268
1269 if (minwidth < title_len)
1270 minwidth = title_len;
1271 }
1272
1273 if (minwidth > 0 && wp->w_width < minwidth)
1274 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001275 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001276 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001277 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001278 // some columns cut off on the right
1279 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001280 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001281 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001282 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001283 {
1284 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1285 if (wp->w_wincol < 0)
1286 wp->w_wincol = 0;
1287 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001288 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1289 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1290 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001291 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001292
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001293 // Right aligned: move to the right if needed.
1294 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001295 if (leftoff >= 0)
1296 wp->w_wincol = leftoff;
1297 else if (wp->w_popup_fixed)
1298 {
1299 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001300 // columns when displaying the window, minus left border and
1301 // padding.
1302 if (-leftoff > left_extra)
1303 wp->w_leftcol = -leftoff - left_extra;
1304 wp->w_width -= wp->w_leftcol;
1305 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001306 if (wp->w_width < 0)
1307 wp->w_width = 0;
1308 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001309 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001310
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001311 if (wp->w_p_wrap || (!wp->w_popup_fixed
1312 && (wp->w_popup_pos == POPPOS_TOPLEFT
1313 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001314 {
1315 int want_col = 0;
1316
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001317 // try to show the right border and any scrollbar
1318 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001319 if (want_col > 0 && wp->w_wincol > 0
1320 && wp->w_wincol + want_col >= Columns)
1321 {
1322 wp->w_wincol = Columns - want_col;
1323 if (wp->w_wincol < 0)
1324 wp->w_wincol = 0;
1325 }
1326 }
1327
Bram Moolenaar8d241042019-06-12 23:40:01 +02001328 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1329 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001330 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1331 wp->w_height = wp->w_minheight;
1332 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1333 wp->w_height = wp->w_maxheight;
1334 if (wp->w_height > Rows - wp->w_winrow)
1335 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar17146962019-05-30 00:12:11 +02001336
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001337 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001338 {
1339 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1340 if (wp->w_winrow < 0)
1341 wp->w_winrow = 0;
1342 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001343 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1344 || wp->w_popup_pos == POPPOS_BOTLEFT)
1345 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001346 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001347 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001348 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001349 else
1350 // not enough space, make top aligned
Bram Moolenaar12034e22019-08-25 22:25:02 +02001351 wp->w_winrow = wantline + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001352 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001353 if (wp->w_winrow >= Rows)
1354 wp->w_winrow = Rows - 1;
1355 else if (wp->w_winrow < 0)
1356 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001357
Bram Moolenaar17146962019-05-30 00:12:11 +02001358 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001359 if (win_valid(wp->w_popup_prop_win))
1360 {
1361 wp->w_popup_prop_changedtick =
1362 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1363 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1364 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001365
1366 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001367 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001368 if (org_winrow != wp->w_winrow
1369 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001370 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001371 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001372 || org_width != wp->w_width
1373 || org_height != wp->w_height)
1374 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001375 redraw_win_later(wp, NOT_VALID);
1376 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1377 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001378 popup_mask_refresh = TRUE;
1379 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001380}
1381
Bram Moolenaar17627312019-06-02 19:53:44 +02001382typedef enum
1383{
1384 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001385 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001386 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001387 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001388 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001389 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001390 TYPE_PREVIEW, // preview window
1391 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001392} create_type_T;
1393
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001394/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001395 * Make "buf" empty and set the contents to "text".
1396 * Used by popup_create() and popup_settext().
1397 */
1398 static void
1399popup_set_buffer_text(buf_T *buf, typval_T text)
1400{
1401 int lnum;
1402
1403 // Clear the buffer, then replace the lines.
1404 curbuf = buf;
1405 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1406 ml_delete(lnum, FALSE);
1407 curbuf = curwin->w_buffer;
1408
1409 // Add text to the buffer.
1410 if (text.v_type == VAR_STRING)
1411 {
1412 // just a string
1413 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1414 }
1415 else
1416 {
1417 list_T *l = text.vval.v_list;
1418
1419 if (l->lv_len > 0)
1420 {
1421 if (l->lv_first->li_tv.v_type == VAR_STRING)
1422 // list of strings
1423 add_popup_strings(buf, l);
1424 else
1425 // list of dictionaries
1426 add_popup_dicts(buf, l);
1427 }
1428 }
1429
1430 // delete the line that was in the empty buffer
1431 curbuf = buf;
1432 ml_delete(buf->b_ml.ml_line_count, FALSE);
1433 curbuf = curwin->w_buffer;
1434}
1435
1436/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001437 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1438 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001439 * Return FAIL if the parsing fails.
1440 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001441 static int
1442parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001443{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001444 char_u *p =
1445#ifdef FEAT_QUICKFIX
1446 !is_preview ? p_cpp :
1447#endif
1448 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001449
Bram Moolenaar258cef52019-08-21 17:29:29 +02001450 if (wp != NULL)
1451 wp->w_popup_flags &= ~POPF_INFO_MENU;
1452
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001453 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001454 {
1455 char_u *e, *dig;
1456 char_u *s = p;
1457 int x;
1458
1459 e = vim_strchr(p, ':');
1460 if (e == NULL || e[1] == NUL)
1461 return FAIL;
1462
1463 p = vim_strchr(e, ',');
1464 if (p == NULL)
1465 p = e + STRLEN(e);
1466 dig = e + 1;
1467 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001468
1469 if (STRNCMP(s, "height:", 7) == 0)
1470 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001471 if (dig != p)
1472 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001473 if (wp != NULL)
1474 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001475 if (is_preview)
1476 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001477 wp->w_maxheight = x;
1478 }
1479 }
1480 else if (STRNCMP(s, "width:", 6) == 0)
1481 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001482 if (dig != p)
1483 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001484 if (wp != NULL)
1485 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001486 if (is_preview)
1487 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001488 wp->w_maxwidth = x;
1489 }
1490 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001491 else if (STRNCMP(s, "highlight:", 10) == 0)
1492 {
1493 if (wp != NULL)
1494 {
1495 int c = *p;
1496
1497 *p = NUL;
1498 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1499 s + 10, OPT_FREE|OPT_LOCAL, 0);
1500 *p = c;
1501 }
1502 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001503 else if (STRNCMP(s, "border:", 7) == 0)
1504 {
1505 char_u *arg = s + 7;
1506 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1507 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1508 int i;
1509
1510 if (!on && !off)
1511 return FAIL;
1512 if (wp != NULL)
1513 {
1514 for (i = 0; i < 4; ++i)
1515 wp->w_popup_border[i] = on ? 1 : 0;
1516 if (off)
1517 // only show the X for close when there is a border
1518 wp->w_popup_close = POPCLOSE_NONE;
1519 }
1520 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001521 else if (STRNCMP(s, "align:", 6) == 0)
1522 {
1523 char_u *arg = s + 6;
1524 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1525 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1526
1527 if (!menu && !item)
1528 return FAIL;
1529 if (wp != NULL && menu)
1530 wp->w_popup_flags |= POPF_INFO_MENU;
1531 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001532 else
1533 return FAIL;
1534 }
1535 return OK;
1536}
1537
1538/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001539 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1540 * is not NULL.
1541 * Return FAIL if the parsing fails.
1542 */
1543 int
1544parse_previewpopup(win_T *wp)
1545{
1546 return parse_popup_option(wp, TRUE);
1547}
1548
1549/*
1550 * Parse the 'completepopup' option and apply the values to window "wp" if it
1551 * is not NULL.
1552 * Return FAIL if the parsing fails.
1553 */
1554 int
1555parse_completepopup(win_T *wp)
1556{
1557 return parse_popup_option(wp, FALSE);
1558}
1559
1560/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001561 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001562 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001563 */
1564 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001565popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001566{
1567 setcursor_mayforce(TRUE);
1568 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1569 if (wp->w_wantline == 0) // cursor in first line
1570 {
1571 wp->w_wantline = 2;
1572 wp->w_popup_pos = POPPOS_TOPLEFT;
1573 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001574
Bram Moolenaar79648732019-07-18 21:43:07 +02001575 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001576 if (wp->w_wantcol > Columns - width)
1577 {
1578 wp->w_wantcol = Columns - width;
1579 if (wp->w_wantcol < 1)
1580 wp->w_wantcol = 1;
1581 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001582 popup_adjust_position(wp);
1583}
1584
1585/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001586 * Set w_wantline and w_wantcol for the a given screen position.
1587 * Caller must take care of running into the window border.
1588 */
1589 void
1590popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1591{
1592 wp->w_wantline = row;
1593 wp->w_wantcol = col;
1594 popup_adjust_position(wp);
1595}
1596
1597/*
1598 * Add a border and lef&right padding.
1599 */
1600 static void
1601add_border_left_right_padding(win_T *wp)
1602{
1603 int i;
1604
1605 for (i = 0; i < 4; ++i)
1606 {
1607 wp->w_popup_border[i] = 1;
1608 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1609 }
1610}
1611
1612/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001613 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001614 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001615 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001616 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001617 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001618 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001619popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001620{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001621 win_T *wp;
1622 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001623 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001624 int new_buffer;
1625 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001626 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001627 int nr;
1628 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001629
Bram Moolenaar79648732019-07-18 21:43:07 +02001630 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001631 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001632 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001633 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001634 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001635 buf = buflist_findnr( argvars[0].vval.v_number);
1636 if (buf == NULL)
1637 {
1638 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1639 return NULL;
1640 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001641#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001642 if (buf->b_term != NULL)
1643 {
1644 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1645 return NULL;
1646 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001647#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001648 }
1649 else if (!(argvars[0].v_type == VAR_STRING
1650 && argvars[0].vval.v_string != NULL)
1651 && !(argvars[0].v_type == VAR_LIST
1652 && argvars[0].vval.v_list != NULL))
1653 {
1654 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001655 return NULL;
1656 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001657 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001658 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001659 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001660 return NULL;
1661 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001662 d = argvars[1].vval.v_dict;
1663 }
1664
1665 if (d != NULL)
1666 {
1667 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1668 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1669 else if (type == TYPE_NOTIFICATION)
1670 tabnr = -1; // notifications are global by default
1671 else
1672 tabnr = 0;
1673 if (tabnr > 0)
1674 {
1675 tp = find_tabpage(tabnr);
1676 if (tp == NULL)
1677 {
1678 semsg(_("E997: Tabpage not found: %d"), tabnr);
1679 return NULL;
1680 }
1681 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001682 }
1683
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001684 // Create the window and buffer.
1685 wp = win_alloc_popup_win();
1686 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001687 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001688 if (rettv != NULL)
1689 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001690 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001691 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001692
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001693 if (buf != NULL)
1694 {
1695 // use existing buffer
1696 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001697 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001698 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001699 buffer_ensure_loaded(buf);
1700 }
1701 else
1702 {
1703 // create a new buffer associated with the popup
1704 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001705 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001706 if (buf == NULL)
1707 return NULL;
1708 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001709
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001710 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001711
Bram Moolenaar46451042019-08-24 15:50:46 +02001712 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001713 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001714 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001715 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001716 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001717 buf->b_p_ul = -1; // no undo
1718 buf->b_p_swf = FALSE; // no swap file
1719 buf->b_p_bl = FALSE; // unlisted buffer
1720 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001721
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001722 // Avoid that 'buftype' is reset when this buffer is entered.
1723 buf->b_p_initialized = TRUE;
1724 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001725 wp->w_p_wrap = TRUE; // 'wrap' is default on
1726 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001727
Bram Moolenaara3fce622019-06-20 02:31:49 +02001728 if (tp != NULL)
1729 {
1730 // popup on specified tab page
1731 wp->w_next = tp->tp_first_popupwin;
1732 tp->tp_first_popupwin = wp;
1733 }
1734 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001735 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001736 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001737 wp->w_next = curtab->tp_first_popupwin;
1738 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001739 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001740 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001741 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001742 win_T *prev = first_popupwin;
1743
1744 // Global popup: add at the end, so that it gets displayed on top of
1745 // older ones with the same zindex. Matters for notifications.
1746 if (first_popupwin == NULL)
1747 first_popupwin = wp;
1748 else
1749 {
1750 while (prev->w_next != NULL)
1751 prev = prev->w_next;
1752 prev->w_next = wp;
1753 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001754 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001755
Bram Moolenaar79648732019-07-18 21:43:07 +02001756 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001757 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001758
Bram Moolenaar79648732019-07-18 21:43:07 +02001759 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001760 {
1761 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001762 }
1763 if (type == TYPE_ATCURSOR)
1764 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001765 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001766 set_moved_values(wp);
1767 set_moved_columns(wp, FIND_STRING);
1768 }
1769
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001770 if (type == TYPE_BEVAL)
1771 {
1772 wp->w_popup_pos = POPPOS_BOTLEFT;
1773
1774 // by default use the mouse position
1775 wp->w_wantline = mouse_row;
1776 if (wp->w_wantline <= 0) // mouse on first line
1777 {
1778 wp->w_wantline = 2;
1779 wp->w_popup_pos = POPPOS_TOPLEFT;
1780 }
1781 wp->w_wantcol = mouse_col + 1;
1782 set_mousemoved_values(wp);
1783 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1784 }
1785
Bram Moolenaar33796b32019-06-08 16:01:13 +02001786 // set default values
1787 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001788 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001789
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001790 if (type == TYPE_NOTIFICATION)
1791 {
1792 win_T *twp, *nextwin;
1793 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001794
1795 // Try to not overlap with another global popup. Guess we need 3
1796 // more screen lines than buffer lines.
1797 wp->w_wantline = 1;
1798 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1799 {
1800 nextwin = twp->w_next;
1801 if (twp != wp
1802 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1803 && twp->w_winrow <= wp->w_wantline - 1 + height
1804 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1805 {
1806 // move to below this popup and restart the loop to check for
1807 // overlap with other popups
1808 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1809 nextwin = first_popupwin;
1810 }
1811 }
1812 if (wp->w_wantline + height > Rows)
1813 {
1814 // can't avoid overlap, put on top in the hope that message goes
1815 // away soon.
1816 wp->w_wantline = 1;
1817 }
1818
1819 wp->w_wantcol = 10;
1820 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001821 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001822 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001823 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001824 for (i = 0; i < 4; ++i)
1825 wp->w_popup_border[i] = 1;
1826 wp->w_popup_padding[1] = 1;
1827 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001828
1829 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001830 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001831 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1832 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001833 }
1834
Bram Moolenaara730e552019-06-16 19:05:31 +02001835 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001836 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001837 wp->w_popup_pos = POPPOS_CENTER;
1838 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001839 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001840 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001841 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001842 }
1843
Bram Moolenaara730e552019-06-16 19:05:31 +02001844 if (type == TYPE_MENU)
1845 {
1846 typval_T tv;
1847 callback_T callback;
1848
1849 tv.v_type = VAR_STRING;
1850 tv.vval.v_string = (char_u *)"popup_filter_menu";
1851 callback = get_callback(&tv);
1852 if (callback.cb_name != NULL)
1853 set_callback(&wp->w_filter_cb, &callback);
1854
1855 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001856 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001857 }
1858
Bram Moolenaar79648732019-07-18 21:43:07 +02001859 if (type == TYPE_PREVIEW)
1860 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001861 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001862 wp->w_popup_close = POPCLOSE_BUTTON;
1863 for (i = 0; i < 4; ++i)
1864 wp->w_popup_border[i] = 1;
1865 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001866 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1867 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001868# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001869 if (type == TYPE_INFO)
1870 {
1871 wp->w_popup_pos = POPPOS_TOPLEFT;
1872 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1873 wp->w_popup_close = POPCLOSE_BUTTON;
1874 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001875 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001876 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001877# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001878
Bram Moolenaarae943152019-06-16 22:54:14 +02001879 for (i = 0; i < 4; ++i)
1880 VIM_CLEAR(wp->w_border_highlight[i]);
1881 for (i = 0; i < 8; ++i)
1882 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001883 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001884 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001885 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001886
Bram Moolenaar79648732019-07-18 21:43:07 +02001887 if (d != NULL)
1888 // Deal with options.
1889 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001890
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001891#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001892 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1893 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001894#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001895
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001896 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001897
1898 wp->w_vsep_width = 0;
1899
1900 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001901 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001902
1903 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001904}
1905
1906/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001907 * popup_clear()
1908 */
1909 void
1910f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1911{
1912 close_all_popups();
1913}
1914
1915/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001916 * popup_create({text}, {options})
1917 */
1918 void
1919f_popup_create(typval_T *argvars, typval_T *rettv)
1920{
Bram Moolenaar17627312019-06-02 19:53:44 +02001921 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001922}
1923
1924/*
1925 * popup_atcursor({text}, {options})
1926 */
1927 void
1928f_popup_atcursor(typval_T *argvars, typval_T *rettv)
1929{
Bram Moolenaar17627312019-06-02 19:53:44 +02001930 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001931}
1932
1933/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001934 * popup_beval({text}, {options})
1935 */
1936 void
1937f_popup_beval(typval_T *argvars, typval_T *rettv)
1938{
1939 popup_create(argvars, rettv, TYPE_BEVAL);
1940}
1941
1942/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001943 * Invoke the close callback for window "wp" with value "result".
1944 * Careful: The callback may make "wp" invalid!
1945 */
1946 static void
1947invoke_popup_callback(win_T *wp, typval_T *result)
1948{
1949 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001950 typval_T argv[3];
1951
1952 argv[0].v_type = VAR_NUMBER;
1953 argv[0].vval.v_number = (varnumber_T)wp->w_id;
1954
1955 if (result != NULL && result->v_type != VAR_UNKNOWN)
1956 copy_tv(result, &argv[1]);
1957 else
1958 {
1959 argv[1].v_type = VAR_NUMBER;
1960 argv[1].vval.v_number = 0;
1961 }
1962
1963 argv[2].v_type = VAR_UNKNOWN;
1964
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001965 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001966 if (result != NULL)
1967 clear_tv(&argv[1]);
1968 clear_tv(&rettv);
1969}
1970
1971/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02001972 * Close popup "wp" and invoke any close callback for it.
1973 */
1974 static void
1975popup_close_and_callback(win_T *wp, typval_T *arg)
1976{
1977 int id = wp->w_id;
1978
1979 if (wp->w_close_cb.cb_name != NULL)
1980 // Careful: This may make "wp" invalid.
1981 invoke_popup_callback(wp, arg);
1982
1983 popup_close(id);
1984}
1985
Bram Moolenaar12034e22019-08-25 22:25:02 +02001986 static void
1987popup_close_with_retval(win_T *wp, int retval)
1988{
1989 typval_T res;
1990
1991 res.v_type = VAR_NUMBER;
1992 res.vval.v_number = retval;
1993 popup_close_and_callback(wp, &res);
1994}
1995
Bram Moolenaar3397f742019-06-02 18:40:06 +02001996/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001997 * Close popup "wp" because of a mouse click.
1998 */
1999 void
2000popup_close_for_mouse_click(win_T *wp)
2001{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002002 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002003}
2004
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002005 static void
2006check_mouse_moved(win_T *wp, win_T *mouse_wp)
2007{
2008 // Close the popup when all if these are true:
2009 // - the mouse is not on this popup
2010 // - "mousemoved" was used
2011 // - the mouse is no longer on the same screen row or the mouse column is
2012 // outside of the relevant text
2013 if (wp != mouse_wp
2014 && wp->w_popup_mouse_row != 0
2015 && (wp->w_popup_mouse_row != mouse_row
2016 || mouse_col < wp->w_popup_mouse_mincol
2017 || mouse_col > wp->w_popup_mouse_maxcol))
2018 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002019 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002020 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002021 }
2022}
2023
2024/*
2025 * Called when the mouse moved: may close a popup with "mousemoved".
2026 */
2027 void
2028popup_handle_mouse_moved(void)
2029{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002030 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002031 win_T *mouse_wp;
2032 int row = mouse_row;
2033 int col = mouse_col;
2034
2035 // find the window where the mouse is in
2036 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2037
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002038 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2039 {
2040 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002041 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002042 }
2043 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2044 {
2045 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002046 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002047 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002048}
2049
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002050/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002051 * In a filter: check if the typed key is a mouse event that is used for
2052 * dragging the popup.
2053 */
2054 static void
2055filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2056{
2057 int row = mouse_row;
2058 int col = mouse_col;
2059
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002060 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002061 && is_mouse_key(c)
2062 && (wp == popup_dragwin
2063 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2064 // do not consume the key, allow for dragging the popup
2065 rettv->vval.v_number = 0;
2066}
2067
Bram Moolenaara730e552019-06-16 19:05:31 +02002068/*
2069 * popup_filter_menu({text}, {options})
2070 */
2071 void
2072f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2073{
2074 int id = tv_get_number(&argvars[0]);
2075 win_T *wp = win_id2wp(id);
2076 char_u *key = tv_get_string(&argvars[1]);
2077 typval_T res;
2078 int c;
2079 linenr_T old_lnum;
2080
2081 // If the popup has been closed do not consume the key.
2082 if (wp == NULL)
2083 return;
2084
2085 c = *key;
2086 if (c == K_SPECIAL && key[1] != NUL)
2087 c = TO_SPECIAL(key[1], key[2]);
2088
2089 // consume all keys until done
2090 rettv->vval.v_number = 1;
2091 res.v_type = VAR_NUMBER;
2092
2093 old_lnum = wp->w_cursor.lnum;
2094 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2095 --wp->w_cursor.lnum;
2096 if ((c == 'j' || c == 'J' || c == K_DOWN)
2097 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2098 ++wp->w_cursor.lnum;
2099 if (old_lnum != wp->w_cursor.lnum)
2100 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002101 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002102 return;
2103 }
2104
2105 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2106 {
2107 // Cancelled, invoke callback with -1
2108 res.vval.v_number = -1;
2109 popup_close_and_callback(wp, &res);
2110 return;
2111 }
2112 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2113 {
2114 // Invoke callback with current index.
2115 res.vval.v_number = wp->w_cursor.lnum;
2116 popup_close_and_callback(wp, &res);
2117 return;
2118 }
2119
2120 filter_handle_drag(wp, c, rettv);
2121}
2122
2123/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002124 * popup_filter_yesno({text}, {options})
2125 */
2126 void
2127f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2128{
2129 int id = tv_get_number(&argvars[0]);
2130 win_T *wp = win_id2wp(id);
2131 char_u *key = tv_get_string(&argvars[1]);
2132 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002133 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002134
2135 // If the popup has been closed don't consume the key.
2136 if (wp == NULL)
2137 return;
2138
Bram Moolenaara730e552019-06-16 19:05:31 +02002139 c = *key;
2140 if (c == K_SPECIAL && key[1] != NUL)
2141 c = TO_SPECIAL(key[1], key[2]);
2142
Bram Moolenaara42d9452019-06-15 21:46:30 +02002143 // consume all keys until done
2144 rettv->vval.v_number = 1;
2145
Bram Moolenaara730e552019-06-16 19:05:31 +02002146 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002147 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002148 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002149 res.vval.v_number = 0;
2150 else
2151 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002152 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002153 return;
2154 }
2155
2156 // Invoke callback
2157 res.v_type = VAR_NUMBER;
2158 popup_close_and_callback(wp, &res);
2159}
2160
2161/*
2162 * popup_dialog({text}, {options})
2163 */
2164 void
2165f_popup_dialog(typval_T *argvars, typval_T *rettv)
2166{
2167 popup_create(argvars, rettv, TYPE_DIALOG);
2168}
2169
2170/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002171 * popup_menu({text}, {options})
2172 */
2173 void
2174f_popup_menu(typval_T *argvars, typval_T *rettv)
2175{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002176 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002177}
2178
2179/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002180 * popup_notification({text}, {options})
2181 */
2182 void
2183f_popup_notification(typval_T *argvars, typval_T *rettv)
2184{
2185 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2186}
2187
2188/*
2189 * Find the popup window with window-ID "id".
2190 * If the popup window does not exist NULL is returned.
2191 * If the window is not a popup window, and error message is given.
2192 */
2193 static win_T *
2194find_popup_win(int id)
2195{
2196 win_T *wp = win_id2wp(id);
2197
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002198 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002199 {
2200 semsg(_("E993: window %d is not a popup window"), id);
2201 return NULL;
2202 }
2203 return wp;
2204}
2205
2206/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002207 * popup_close({id})
2208 */
2209 void
2210f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2211{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002212 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002213 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002214
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002215 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002216 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002217}
2218
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002219 static void
2220popup_hide(win_T *wp)
2221{
2222 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2223 {
2224 wp->w_popup_flags |= POPF_HIDDEN;
2225 --wp->w_buffer->b_nwindows;
2226 redraw_all_later(NOT_VALID);
2227 popup_mask_refresh = TRUE;
2228 }
2229}
2230
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002231/*
2232 * popup_hide({id})
2233 */
2234 void
2235f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2236{
2237 int id = (int)tv_get_number(argvars);
2238 win_T *wp = find_popup_win(id);
2239
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002240 if (wp != NULL)
2241 popup_hide(wp);
2242}
2243
2244 void
2245popup_show(win_T *wp)
2246{
2247 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002248 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002249 wp->w_popup_flags &= ~POPF_HIDDEN;
2250 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002251 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002252 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002253 }
2254}
2255
2256/*
2257 * popup_show({id})
2258 */
2259 void
2260f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2261{
2262 int id = (int)tv_get_number(argvars);
2263 win_T *wp = find_popup_win(id);
2264
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002265 if (wp != NULL)
2266 popup_show(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002267}
2268
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002269/*
2270 * popup_settext({id}, {text})
2271 */
2272 void
2273f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2274{
2275 int id = (int)tv_get_number(&argvars[0]);
2276 win_T *wp = find_popup_win(id);
2277
2278 if (wp != NULL)
2279 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002280 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2281 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2282 else
2283 {
2284 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002285 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002286 popup_adjust_position(wp);
2287 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002288 }
2289}
2290
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002291 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002292popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002293{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002294 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002295 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002296 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002297 clear_cmdline = TRUE;
2298 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002299
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002300 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002301 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002302}
2303
Bram Moolenaarec583842019-05-26 14:11:23 +02002304/*
2305 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002306 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002307 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002308 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002309popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002310{
2311 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002312 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002313 win_T *prev = NULL;
2314
Bram Moolenaarec583842019-05-26 14:11:23 +02002315 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002316 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002317 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002318 {
2319 if (prev == NULL)
2320 first_popupwin = wp->w_next;
2321 else
2322 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002323 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002324 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002325 }
2326
Bram Moolenaarec583842019-05-26 14:11:23 +02002327 // go through tab-local popups
2328 FOR_ALL_TABPAGES(tp)
2329 popup_close_tabpage(tp, id);
2330}
2331
2332/*
2333 * Close a popup window with Window-id "id" in tabpage "tp".
2334 */
2335 void
2336popup_close_tabpage(tabpage_T *tp, int id)
2337{
2338 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002339 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002340 win_T *prev = NULL;
2341
Bram Moolenaarec583842019-05-26 14:11:23 +02002342 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2343 if (wp->w_id == id)
2344 {
2345 if (prev == NULL)
2346 *root = wp->w_next;
2347 else
2348 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002349 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002350 return;
2351 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002352}
2353
2354 void
2355close_all_popups(void)
2356{
2357 while (first_popupwin != NULL)
2358 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002359 while (curtab->tp_first_popupwin != NULL)
2360 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002361}
2362
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002363/*
2364 * popup_move({id}, {options})
2365 */
2366 void
2367f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2368{
Bram Moolenaarae943152019-06-16 22:54:14 +02002369 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002370 int id = (int)tv_get_number(argvars);
2371 win_T *wp = find_popup_win(id);
2372
2373 if (wp == NULL)
2374 return; // invalid {id}
2375
2376 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2377 {
2378 emsg(_(e_dictreq));
2379 return;
2380 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002381 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002382
Bram Moolenaarae943152019-06-16 22:54:14 +02002383 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002384
2385 if (wp->w_winrow + wp->w_height >= cmdline_row)
2386 clear_cmdline = TRUE;
2387 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002388}
2389
Bram Moolenaarbc133542019-05-29 20:26:46 +02002390/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002391 * popup_setoptions({id}, {options})
2392 */
2393 void
2394f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2395{
2396 dict_T *dict;
2397 int id = (int)tv_get_number(argvars);
2398 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002399 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002400
2401 if (wp == NULL)
2402 return; // invalid {id}
2403
2404 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2405 {
2406 emsg(_(e_dictreq));
2407 return;
2408 }
2409 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002410 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002411
2412 apply_move_options(wp, dict);
2413 apply_general_options(wp, dict);
2414
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002415 if (old_firstline != wp->w_firstline)
2416 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002417 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002418 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002419 popup_adjust_position(wp);
2420}
2421
2422/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002423 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002424 */
2425 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002426f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002427{
2428 dict_T *dict;
2429 int id = (int)tv_get_number(argvars);
2430 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002431 int top_extra;
2432 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002433
2434 if (rettv_dict_alloc(rettv) == OK)
2435 {
2436 if (wp == NULL)
2437 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002438 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002439 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2440
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002441 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002442 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002443 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002444
Bram Moolenaarbc133542019-05-29 20:26:46 +02002445 dict_add_number(dict, "line", wp->w_winrow + 1);
2446 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002447 dict_add_number(dict, "width", wp->w_width + left_extra
2448 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2449 dict_add_number(dict, "height", wp->w_height + top_extra
2450 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002451
2452 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2453 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2454 dict_add_number(dict, "core_width", wp->w_width);
2455 dict_add_number(dict, "core_height", wp->w_height);
2456
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002457 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002458 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002459 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002460 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002461
2462 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002463 }
2464}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002465/*
2466 * popup_locate({row}, {col})
2467 */
2468 void
2469f_popup_locate(typval_T *argvars, typval_T *rettv)
2470{
2471 int row = tv_get_number(&argvars[0]) - 1;
2472 int col = tv_get_number(&argvars[1]) - 1;
2473 win_T *wp;
2474
2475 wp = mouse_find_win(&row, &col, FIND_POPUP);
2476 if (WIN_IS_POPUP(wp))
2477 rettv->vval.v_number = wp->w_id;
2478}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002479
2480/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002481 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2482 */
2483 static void
2484get_padding_border(dict_T *dict, int *array, char *name)
2485{
2486 list_T *list;
2487 int i;
2488
2489 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2490 return;
2491
2492 list = list_alloc();
2493 if (list != NULL)
2494 {
2495 dict_add_list(dict, name, list);
2496 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2497 for (i = 0; i < 4; ++i)
2498 list_append_number(list, array[i]);
2499 }
2500}
2501
2502/*
2503 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2504 */
2505 static void
2506get_borderhighlight(dict_T *dict, win_T *wp)
2507{
2508 list_T *list;
2509 int i;
2510
2511 for (i = 0; i < 4; ++i)
2512 if (wp->w_border_highlight[i] != NULL)
2513 break;
2514 if (i == 4)
2515 return;
2516
2517 list = list_alloc();
2518 if (list != NULL)
2519 {
2520 dict_add_list(dict, "borderhighlight", list);
2521 for (i = 0; i < 4; ++i)
2522 list_append_string(list, wp->w_border_highlight[i], -1);
2523 }
2524}
2525
2526/*
2527 * For popup_getoptions(): add a "borderchars" entry to "dict".
2528 */
2529 static void
2530get_borderchars(dict_T *dict, win_T *wp)
2531{
2532 list_T *list;
2533 int i;
2534 char_u buf[NUMBUFLEN];
2535 int len;
2536
2537 for (i = 0; i < 8; ++i)
2538 if (wp->w_border_char[i] != 0)
2539 break;
2540 if (i == 8)
2541 return;
2542
2543 list = list_alloc();
2544 if (list != NULL)
2545 {
2546 dict_add_list(dict, "borderchars", list);
2547 for (i = 0; i < 8; ++i)
2548 {
2549 len = mb_char2bytes(wp->w_border_char[i], buf);
2550 list_append_string(list, buf, len);
2551 }
2552 }
2553}
2554
2555/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002556 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002557 */
2558 static void
2559get_moved_list(dict_T *dict, win_T *wp)
2560{
2561 list_T *list;
2562
2563 list = list_alloc();
2564 if (list != NULL)
2565 {
2566 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002567 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002568 list_append_number(list, wp->w_popup_mincol);
2569 list_append_number(list, wp->w_popup_maxcol);
2570 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002571 list = list_alloc();
2572 if (list != NULL)
2573 {
2574 dict_add_list(dict, "mousemoved", list);
2575 list_append_number(list, wp->w_popup_mouse_row);
2576 list_append_number(list, wp->w_popup_mouse_mincol);
2577 list_append_number(list, wp->w_popup_mouse_maxcol);
2578 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002579}
2580
2581/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002582 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002583 */
2584 void
2585f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2586{
2587 dict_T *dict;
2588 int id = (int)tv_get_number(argvars);
2589 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002590 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002591 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002592
2593 if (rettv_dict_alloc(rettv) == OK)
2594 {
2595 if (wp == NULL)
2596 return;
2597
2598 dict = rettv->vval.v_dict;
2599 dict_add_number(dict, "line", wp->w_wantline);
2600 dict_add_number(dict, "col", wp->w_wantcol);
2601 dict_add_number(dict, "minwidth", wp->w_minwidth);
2602 dict_add_number(dict, "minheight", wp->w_minheight);
2603 dict_add_number(dict, "maxheight", wp->w_maxheight);
2604 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002605 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002606 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002607 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002608 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002609 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2610 {
2611 proptype_T *pt = text_prop_type_by_id(
2612 wp->w_popup_prop_win->w_buffer,
2613 wp->w_popup_prop_type);
2614
2615 if (pt != NULL)
2616 dict_add_string(dict, "textprop", pt->pt_name);
2617 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2618 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2619 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002620 dict_add_string(dict, "title", wp->w_popup_title);
2621 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002622 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002623 dict_add_number(dict, "mapping",
2624 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002625 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002626 dict_add_number(dict, "cursorline",
2627 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002628 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002629 if (wp->w_scrollbar_highlight != NULL)
2630 dict_add_string(dict, "scrollbarhighlight",
2631 wp->w_scrollbar_highlight);
2632 if (wp->w_thumb_highlight != NULL)
2633 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002634
Bram Moolenaara3fce622019-06-20 02:31:49 +02002635 // find the tabpage that holds this popup
2636 i = 1;
2637 FOR_ALL_TABPAGES(tp)
2638 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002639 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002640
Bram Moolenaar1824f452019-10-02 23:06:46 +02002641 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2642 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002643 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002644 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002645 break;
2646 ++i;
2647 }
2648 if (tp == NULL)
2649 i = -1; // must be global
2650 else if (tp == curtab)
2651 i = 0;
2652 dict_add_number(dict, "tabpage", i);
2653
Bram Moolenaarae943152019-06-16 22:54:14 +02002654 get_padding_border(dict, wp->w_popup_padding, "padding");
2655 get_padding_border(dict, wp->w_popup_border, "border");
2656 get_borderhighlight(dict, wp);
2657 get_borderchars(dict, wp);
2658 get_moved_list(dict, wp);
2659
2660 if (wp->w_filter_cb.cb_name != NULL)
2661 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2662 if (wp->w_close_cb.cb_name != NULL)
2663 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002664
2665 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2666 ++i)
2667 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2668 {
2669 dict_add_string(dict, "pos",
2670 (char_u *)poppos_entries[i].pp_name);
2671 break;
2672 }
2673
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002674 dict_add_string(dict, "close", (char_u *)(
2675 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2676 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2677
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002678# if defined(FEAT_TIMERS)
2679 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2680 ? (long)wp->w_popup_timer->tr_interval : 0L);
2681# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002682 }
2683}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002684
2685 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002686error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002687{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002688 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002689 {
2690 emsg(_("E994: Not allowed in a popup window"));
2691 return TRUE;
2692 }
2693 return FALSE;
2694}
2695
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002696/*
2697 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002698 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002699 */
2700 void
2701popup_reset_handled()
2702{
2703 win_T *wp;
2704
2705 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2706 wp->w_popup_flags &= ~POPF_HANDLED;
2707 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2708 wp->w_popup_flags &= ~POPF_HANDLED;
2709}
2710
2711/*
2712 * Find the next visible popup where POPF_HANDLED is not set.
2713 * Must have called popup_reset_handled() first.
2714 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2715 * popup with the highest zindex.
2716 */
2717 win_T *
2718find_next_popup(int lowest)
2719{
2720 win_T *wp;
2721 win_T *found_wp;
2722 int found_zindex;
2723
2724 found_zindex = lowest ? INT_MAX : 0;
2725 found_wp = NULL;
2726 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2727 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2728 && (lowest ? wp->w_zindex < found_zindex
2729 : wp->w_zindex > found_zindex))
2730 {
2731 found_zindex = wp->w_zindex;
2732 found_wp = wp;
2733 }
2734 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2735 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2736 && (lowest ? wp->w_zindex < found_zindex
2737 : wp->w_zindex > found_zindex))
2738 {
2739 found_zindex = wp->w_zindex;
2740 found_wp = wp;
2741 }
2742
2743 if (found_wp != NULL)
2744 found_wp->w_popup_flags |= POPF_HANDLED;
2745 return found_wp;
2746}
2747
2748/*
2749 * Invoke the filter callback for window "wp" with typed character "c".
2750 * Uses the global "mod_mask" for modifiers.
2751 * Returns the return value of the filter.
2752 * Careful: The filter may make "wp" invalid!
2753 */
2754 static int
2755invoke_popup_filter(win_T *wp, int c)
2756{
2757 int res;
2758 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002759 typval_T argv[3];
2760 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002761 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002762
Bram Moolenaara42d9452019-06-15 21:46:30 +02002763 // Emergency exit: CTRL-C closes the popup.
2764 if (c == Ctrl_C)
2765 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02002766 popup_close_with_retval(wp, -1);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002767 return 1;
2768 }
2769
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002770 argv[0].v_type = VAR_NUMBER;
2771 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2772
2773 // Convert the number to a string, so that the function can use:
2774 // if a:c == "\<F2>"
2775 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2776 argv[1].v_type = VAR_STRING;
2777 argv[1].vval.v_string = vim_strsave(buf);
2778
2779 argv[2].v_type = VAR_UNKNOWN;
2780
Bram Moolenaara42d9452019-06-15 21:46:30 +02002781 // NOTE: The callback might close the popup, thus make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002782 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002783 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002784 popup_highlight_curline(wp);
2785
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002786 res = tv_get_number(&rettv);
2787 vim_free(argv[1].vval.v_string);
2788 clear_tv(&rettv);
2789 return res;
2790}
2791
2792/*
2793 * Called when "c" was typed: invoke popup filter callbacks.
2794 * Returns TRUE when the character was consumed,
2795 */
2796 int
2797popup_do_filter(int c)
2798{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002799 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002800 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002801 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002802 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002803 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002804 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002805
2806 if (recursive)
2807 return FALSE;
2808 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002809
2810 popup_reset_handled();
2811
Bram Moolenaarf6396232019-08-24 19:36:00 +02002812 if (c == K_LEFTMOUSE)
2813 {
2814 int row = mouse_row;
2815 int col = mouse_col;
2816
2817 wp = mouse_find_win(&row, &col, FIND_POPUP);
2818 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002819 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002820 }
2821
Bram Moolenaar581ba392019-09-03 22:08:33 +02002822 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002823 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002824 if (wp->w_filter_cb.cb_name != NULL
2825 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002826 res = invoke_popup_filter(wp, c);
2827
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002828 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002829 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002830 recursive = FALSE;
2831 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002832 return res;
2833}
2834
Bram Moolenaar3397f742019-06-02 18:40:06 +02002835/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002836 * Return TRUE if there is a popup visible with a filter callback and the
2837 * "mapping" property off.
2838 */
2839 int
2840popup_no_mapping(void)
2841{
2842 int round;
2843 win_T *wp;
2844
2845 for (round = 1; round <= 2; ++round)
2846 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2847 wp != NULL; wp = wp->w_next)
2848 if (wp->w_filter_cb.cb_name != NULL
2849 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2850 return TRUE;
2851 return FALSE;
2852}
2853
2854/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002855 * Called when the cursor moved: check if any popup needs to be closed if the
2856 * cursor moved far enough.
2857 */
2858 void
2859popup_check_cursor_pos()
2860{
2861 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002862
2863 popup_reset_handled();
2864 while ((wp = find_next_popup(TRUE)) != NULL)
2865 if (wp->w_popup_curwin != NULL
2866 && (curwin != wp->w_popup_curwin
2867 || curwin->w_cursor.lnum != wp->w_popup_lnum
2868 || curwin->w_cursor.col < wp->w_popup_mincol
2869 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002870 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002871}
2872
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002873/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002874 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002875 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002876 static void
2877popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002878{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002879 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002880 char_u *cells;
2881 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002882
2883 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002884 return;
2885 if (wp->w_popup_mask_cells != NULL
2886 && wp->w_popup_mask_height == height
2887 && wp->w_popup_mask_width == width)
2888 return; // cache is still valid
2889
2890 vim_free(wp->w_popup_mask_cells);
2891 wp->w_popup_mask_cells = alloc_clear(width * height);
2892 if (wp->w_popup_mask_cells == NULL)
2893 return;
2894 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002895
2896 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2897 {
2898 int cols, cole;
2899 int lines, linee;
2900
2901 li = lio->li_tv.vval.v_list->lv_first;
2902 cols = tv_get_number(&li->li_tv);
2903 if (cols < 0)
2904 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002905 li = li->li_next;
2906 cole = tv_get_number(&li->li_tv);
2907 if (cole < 0)
2908 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002909 li = li->li_next;
2910 lines = tv_get_number(&li->li_tv);
2911 if (lines < 0)
2912 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002913 li = li->li_next;
2914 linee = tv_get_number(&li->li_tv);
2915 if (linee < 0)
2916 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002917
2918 for (row = lines - 1; row < linee && row < height; ++row)
2919 for (col = cols - 1; col < cole && col < width; ++col)
2920 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002921 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002922}
2923
2924/*
2925 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
2926 * "col" and "line" are screen coordinates.
2927 */
2928 static int
2929popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
2930{
2931 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
2932 int line = screenline - wp->w_winrow;
2933
2934 return col >= 0 && col < width
2935 && line >= 0 && line < height
2936 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002937}
2938
2939/*
2940 * Set flags in popup_transparent[] for window "wp" to "val".
2941 */
2942 static void
2943update_popup_transparent(win_T *wp, int val)
2944{
2945 if (wp->w_popup_mask != NULL)
2946 {
2947 int width = popup_width(wp);
2948 int height = popup_height(wp);
2949 listitem_T *lio, *li;
2950 int cols, cole;
2951 int lines, linee;
2952 int col, line;
2953
2954 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2955 {
2956 li = lio->li_tv.vval.v_list->lv_first;
2957 cols = tv_get_number(&li->li_tv);
2958 if (cols < 0)
2959 cols = width + cols + 1;
2960 li = li->li_next;
2961 cole = tv_get_number(&li->li_tv);
2962 if (cole < 0)
2963 cole = width + cole + 1;
2964 li = li->li_next;
2965 lines = tv_get_number(&li->li_tv);
2966 if (lines < 0)
2967 lines = height + lines + 1;
2968 li = li->li_next;
2969 linee = tv_get_number(&li->li_tv);
2970 if (linee < 0)
2971 linee = height + linee + 1;
2972
2973 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002974 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002975 if (cols < 0)
2976 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02002977 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002978 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02002979 if (lines < 0)
2980 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02002981 for (line = lines; line < linee
2982 && line + wp->w_winrow < screen_Rows; ++line)
2983 for (col = cols; col < cole
2984 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002985 popup_transparent[(line + wp->w_winrow) * screen_Columns
2986 + col + wp->w_wincol] = val;
2987 }
2988 }
2989}
2990
2991/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02002992 * Only called when popup window "wp" is hidden: If the window is positioned
2993 * next to a text property, and it is now visible, then unhide the popup.
2994 * We don't check if visible popups become hidden, that is done in
2995 * popup_adjust_position().
2996 * Return TRUE if the popup became unhidden.
2997 */
2998 static int
2999check_popup_unhidden(win_T *wp)
3000{
3001 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3002 {
3003 textprop_T prop;
3004 linenr_T lnum;
3005
3006 if (find_visible_prop(wp->w_popup_prop_win,
3007 wp->w_popup_prop_type, wp->w_popup_prop_id,
3008 &prop, &lnum) == OK)
3009 {
3010 wp->w_popup_flags &= ~POPF_HIDDEN;
3011 ++wp->w_buffer->b_nwindows;
3012 wp->w_popup_prop_topline = 0; // force repositioning
3013 return TRUE;
3014 }
3015 }
3016 return FALSE;
3017}
3018
3019/*
3020 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3021 * That is when the buffer in the popup was changed, or the popup is following
3022 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003023 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003024 */
3025 static int
3026popup_need_position_adjust(win_T *wp)
3027{
3028 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3029 return TRUE;
3030 if (win_valid(wp->w_popup_prop_win))
3031 return wp->w_popup_prop_changedtick
3032 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003033 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3034 || ((wp->w_popup_flags & POPF_CURSORLINE)
3035 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003036 return FALSE;
3037}
3038
3039/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003040 * Update "popup_mask" if needed.
3041 * Also recomputes the popup size and positions.
3042 * Also updates "popup_visible".
3043 * Also marks window lines for redrawing.
3044 */
3045 void
3046may_update_popup_mask(int type)
3047{
3048 win_T *wp;
3049 short *mask;
3050 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003051 int redraw_all_popups = FALSE;
3052 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003053
3054 // Need to recompute when switching tabs.
3055 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3056 // (such as the screen size) must have changed.
3057 if (popup_mask_tab != curtab || type >= NOT_VALID)
3058 {
3059 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003060 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003061 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003062
3063 // Check if any popup window buffer has changed and if any popup connected
3064 // to a text property has become visible.
3065 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3066 if (wp->w_popup_flags & POPF_HIDDEN)
3067 popup_mask_refresh |= check_popup_unhidden(wp);
3068 else if (popup_need_position_adjust(wp))
3069 popup_mask_refresh = TRUE;
3070 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3071 if (wp->w_popup_flags & POPF_HIDDEN)
3072 popup_mask_refresh |= check_popup_unhidden(wp);
3073 else if (popup_need_position_adjust(wp))
3074 popup_mask_refresh = TRUE;
3075
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003076 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003077 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003078
3079 // Need to update the mask, something has changed.
3080 popup_mask_refresh = FALSE;
3081 popup_mask_tab = curtab;
3082 popup_visible = FALSE;
3083
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003084 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003085 // If redrawing only what is needed, update "popup_mask_next" and then
3086 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003087 redrawing_all_win = TRUE;
3088 FOR_ALL_WINDOWS(wp)
3089 if (wp->w_redr_type < SOME_VALID)
3090 redrawing_all_win = FALSE;
3091 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003092 mask = popup_mask;
3093 else
3094 mask = popup_mask_next;
3095 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3096
3097 // Find the window with the lowest zindex that hasn't been handled yet,
3098 // so that the window with a higher zindex overwrites the value in
3099 // popup_mask.
3100 popup_reset_handled();
3101 while ((wp = find_next_popup(TRUE)) != NULL)
3102 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003103 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003104 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003105
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003106 popup_visible = TRUE;
3107
Bram Moolenaar12034e22019-08-25 22:25:02 +02003108 // Recompute the position if the text changed. It may make the popup
3109 // hidden if it's attach to a text property that is no longer visible.
3110 if (redraw_all_popups || popup_need_position_adjust(wp))
3111 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003112 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003113 if (wp->w_popup_flags & POPF_HIDDEN)
3114 continue;
3115 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003116
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003117 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003118 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003119 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003120 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003121 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003122 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003123 col < wp->w_wincol + width - wp->w_popup_leftoff
3124 && col < screen_Columns; ++col)
3125 if (wp->w_popup_mask_cells == NULL
3126 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003127 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003128 }
3129
3130 // Only check which lines are to be updated if not already
3131 // updating all lines.
3132 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003133 {
3134 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3135 win_T *prev_wp = NULL;
3136
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003137 for (line = 0; line < screen_Rows; ++line)
3138 {
3139 int col_done = 0;
3140
3141 for (col = 0; col < screen_Columns; ++col)
3142 {
3143 int off = line * screen_Columns + col;
3144
3145 if (popup_mask[off] != popup_mask_next[off])
3146 {
3147 popup_mask[off] = popup_mask_next[off];
3148
3149 if (line >= cmdline_row)
3150 {
3151 // the command line needs to be cleared if text below
3152 // the popup is now visible.
3153 if (!msg_scrolled && popup_mask_next[off] == 0)
3154 clear_cmdline = TRUE;
3155 }
3156 else if (col >= col_done)
3157 {
3158 linenr_T lnum;
3159 int line_cp = line;
3160 int col_cp = col;
3161
3162 // The screen position "line" / "col" needs to be
3163 // redrawn. Figure out what window that is and update
3164 // w_redraw_top and w_redr_bot. Only needs to be done
3165 // once for each window line.
3166 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3167 if (wp != NULL)
3168 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003169 if (wp != prev_wp)
3170 {
3171 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3172 prev_wp = wp;
3173 }
3174
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003175 if (line_cp >= wp->w_height)
3176 // In (or below) status line
3177 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003178 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003179 {
3180 // compute the position in the buffer line from
3181 // the position in the window
3182 mouse_comp_pos(wp, &line_cp, &col_cp,
3183 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003184 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003185 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003186
3187 // This line is going to be redrawn, no need to
3188 // check until the right side of the window.
3189 col_done = wp->w_wincol + wp->w_width - 1;
3190 }
3191 }
3192 }
3193 }
3194 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003195
3196 vim_free(plines_cache);
3197 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003198}
3199
3200/*
3201 * Return a string of "len" spaces in IObuff.
3202 */
3203 static char_u *
3204get_spaces(int len)
3205{
3206 vim_memset(IObuff, ' ', (size_t)len);
3207 IObuff[len] = NUL;
3208 return IObuff;
3209}
3210
3211/*
3212 * Update popup windows. They are drawn on top of normal windows.
3213 * "win_update" is called for each popup window, lowest zindex first.
3214 */
3215 void
3216update_popups(void (*win_update)(win_T *wp))
3217{
3218 win_T *wp;
3219 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003220 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003221 int total_width;
3222 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003223 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003224 int popup_attr;
3225 int border_attr[4];
3226 int border_char[8];
3227 char_u buf[MB_MAXBYTES];
3228 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003229 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003230 int padcol = 0;
3231 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003232 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003233 int sb_thumb_top = 0;
3234 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003235 int attr_scroll = 0;
3236 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003237
3238 // Find the window with the lowest zindex that hasn't been updated yet,
3239 // so that the window with a higher zindex is drawn later, thus goes on
3240 // top.
3241 popup_reset_handled();
3242 while ((wp = find_next_popup(TRUE)) != NULL)
3243 {
3244 // This drawing uses the zindex of the popup window, so that it's on
3245 // top of the text but doesn't draw when another popup with higher
3246 // zindex is on top of the character.
3247 screen_zindex = wp->w_zindex;
3248
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003249 // Set flags in popup_transparent[] for masked cells.
3250 update_popup_transparent(wp, 1);
3251
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003252 // adjust w_winrow and w_wincol for border and padding, since
3253 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003254 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003255 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3256 - wp->w_popup_leftoff;
3257 if (wp->w_wincol + left_extra < 0)
3258 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003259 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003260 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003261
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003262 // Draw the popup text, unless it's off screen.
3263 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003264 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003265 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003266
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003267 // move the cursor into the visible lines, otherwise executing
3268 // commands with win_execute() may cause the text to jump.
3269 if (wp->w_cursor.lnum < wp->w_topline)
3270 wp->w_cursor.lnum = wp->w_topline;
3271 else if (wp->w_cursor.lnum >= wp->w_botline)
3272 wp->w_cursor.lnum = wp->w_botline - 1;
3273 }
3274
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003275 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003276 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003277
Bram Moolenaard529ba52019-07-02 23:13:53 +02003278 total_width = popup_width(wp);
3279 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003280 popup_attr = get_wcr_attr(wp);
3281
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003282 if (wp->w_winrow + total_height > cmdline_row)
3283 wp->w_popup_flags |= POPF_ON_CMDLINE;
3284 else
3285 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3286
3287
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003288 // We can only use these line drawing characters when 'encoding' is
3289 // "utf-8" and 'ambiwidth' is "single".
3290 if (enc_utf8 && *p_ambw == 's')
3291 {
3292 border_char[0] = border_char[2] = 0x2550;
3293 border_char[1] = border_char[3] = 0x2551;
3294 border_char[4] = 0x2554;
3295 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003296 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3297 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003298 border_char[7] = 0x255a;
3299 }
3300 else
3301 {
3302 border_char[0] = border_char[2] = '-';
3303 border_char[1] = border_char[3] = '|';
3304 for (i = 4; i < 8; ++i)
3305 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003306 if (wp->w_popup_flags & POPF_RESIZE)
3307 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003308 }
3309 for (i = 0; i < 8; ++i)
3310 if (wp->w_border_char[i] != 0)
3311 border_char[i] = wp->w_border_char[i];
3312
3313 for (i = 0; i < 4; ++i)
3314 {
3315 border_attr[i] = popup_attr;
3316 if (wp->w_border_highlight[i] != NULL)
3317 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3318 }
3319
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003320 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003321 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003322 if (wp->w_popup_border[0] > 0)
3323 {
3324 // top border
3325 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003326 wincol < 0 ? 0 : wincol, wincol + total_width,
3327 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003328 ? border_char[4] : border_char[0],
3329 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003330 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003331 {
3332 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3333 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003334 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003335 }
3336 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003337 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3338 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003339
Bram Moolenaard529ba52019-07-02 23:13:53 +02003340 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3341 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003342 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003343 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3344 - wp->w_has_scrollbar;
3345 if (padcol < 0)
3346 {
3347 padwidth += padcol;
3348 padcol = 0;
3349 }
3350 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003351 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003352 {
3353 // top padding
3354 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003355 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003356 ' ', ' ', popup_attr);
3357 }
3358
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003359 // Title goes on top of border or padding.
3360 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003361 {
3362 int len = (int)STRLEN(wp->w_popup_title) + 1;
3363 char_u *title = alloc(len);
3364
3365 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3366 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003367 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003368 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003369 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003370
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003371 // Compute scrollbar thumb position and size.
3372 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003373 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003374 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3375 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003376
Bram Moolenaar076d9882019-09-14 22:23:29 +02003377 sb_thumb_height = (height * height + linecount / 2) / linecount;
3378 if (wp->w_topline > 1 && sb_thumb_height == height)
3379 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003380 if (sb_thumb_height == 0)
3381 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003382 if (linecount <= wp->w_height)
3383 // it just fits, avoid divide by zero
3384 sb_thumb_top = 0;
3385 else
3386 sb_thumb_top = (wp->w_topline - 1
3387 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003388 * (wp->w_height - sb_thumb_height)
3389 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003390 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3391 sb_thumb_top = 1; // show it's scrolled
3392
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003393 if (wp->w_scrollbar_highlight != NULL)
3394 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3395 else
3396 attr_scroll = highlight_attr[HLF_PSB];
3397 if (wp->w_thumb_highlight != NULL)
3398 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3399 else
3400 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003401 }
3402
3403 for (i = wp->w_popup_border[0];
3404 i < total_height - wp->w_popup_border[2]; ++i)
3405 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003406 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003407 // left and right padding only needed next to the body
3408 int do_padding =
3409 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3410 && i < total_height - wp->w_popup_border[2]
3411 - wp->w_popup_padding[2];
3412
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003413 row = wp->w_winrow + i;
3414
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003415 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003416 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003417 {
3418 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003419 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003420 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003421 if (do_padding && wp->w_popup_padding[3] > 0)
3422 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003423 int col = wincol + wp->w_popup_border[3];
3424
Bram Moolenaard529ba52019-07-02 23:13:53 +02003425 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003426 pad_left = wp->w_popup_padding[3];
3427 if (col < 0)
3428 {
3429 pad_left += col;
3430 col = 0;
3431 }
3432 if (pad_left > 0)
3433 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3434 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003435 // scrollbar
3436 if (wp->w_has_scrollbar)
3437 {
3438 int line = i - top_off;
3439 int scroll_col = wp->w_wincol + total_width - 1
3440 - wp->w_popup_border[1];
3441
3442 if (line >= 0 && line < wp->w_height)
3443 screen_putchar(' ', row, scroll_col,
3444 line >= sb_thumb_top
3445 && line < sb_thumb_top + sb_thumb_height
3446 ? attr_thumb : attr_scroll);
3447 else
3448 screen_putchar(' ', row, scroll_col, popup_attr);
3449 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003450 // right border
3451 if (wp->w_popup_border[1] > 0)
3452 {
3453 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003454 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003455 }
3456 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003457 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003458 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003459 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003460 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3461 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003462 }
3463
3464 if (wp->w_popup_padding[2] > 0)
3465 {
3466 // bottom padding
3467 row = wp->w_winrow + wp->w_popup_border[0]
3468 + wp->w_popup_padding[0] + wp->w_height;
3469 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003470 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003471 }
3472
3473 if (wp->w_popup_border[2] > 0)
3474 {
3475 // bottom border
3476 row = wp->w_winrow + total_height - 1;
3477 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003478 wincol < 0 ? 0 : wincol,
3479 wincol + total_width,
3480 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003481 ? border_char[7] : border_char[2],
3482 border_char[2], border_attr[2]);
3483 if (wp->w_popup_border[1] > 0)
3484 {
3485 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003486 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003487 }
3488 }
3489
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003490 if (wp->w_popup_close == POPCLOSE_BUTTON)
3491 {
3492 // close button goes on top of anything at the top-right corner
3493 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003494 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003495 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3496 }
3497
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003498 update_popup_transparent(wp, 0);
3499
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003500 // Back to the normal zindex.
3501 screen_zindex = 0;
3502 }
3503}
3504
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003505/*
3506 * Mark references in callbacks of one popup window.
3507 */
3508 static int
3509set_ref_in_one_popup(win_T *wp, int copyID)
3510{
3511 int abort = FALSE;
3512 typval_T tv;
3513
3514 if (wp->w_close_cb.cb_partial != NULL)
3515 {
3516 tv.v_type = VAR_PARTIAL;
3517 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3518 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3519 }
3520 if (wp->w_filter_cb.cb_partial != NULL)
3521 {
3522 tv.v_type = VAR_PARTIAL;
3523 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3524 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3525 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003526 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003527 return abort;
3528}
3529
3530/*
3531 * Set reference in callbacks of popup windows.
3532 */
3533 int
3534set_ref_in_popups(int copyID)
3535{
3536 int abort = FALSE;
3537 win_T *wp;
3538 tabpage_T *tp;
3539
3540 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3541 abort = abort || set_ref_in_one_popup(wp, copyID);
3542
3543 FOR_ALL_TABPAGES(tp)
3544 {
3545 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3546 abort = abort || set_ref_in_one_popup(wp, copyID);
3547 if (abort)
3548 break;
3549 }
3550 return abort;
3551}
Bram Moolenaar79648732019-07-18 21:43:07 +02003552
3553/*
3554 * Find an existing popup used as the preview window, in the current tab page.
3555 * Return NULL if not found.
3556 */
3557 win_T *
3558popup_find_preview_window(void)
3559{
3560 win_T *wp;
3561
3562 // Preview window popup is always local to tab page.
3563 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3564 if (wp->w_p_pvw)
3565 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003566 return NULL;
3567}
3568
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003569 int
3570popup_is_popup(win_T *wp)
3571{
3572 return wp->w_popup_flags != 0;
3573}
3574
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003575#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003576/*
3577 * Find an existing popup used as the info window, in the current tab page.
3578 * Return NULL if not found.
3579 */
3580 win_T *
3581popup_find_info_window(void)
3582{
3583 win_T *wp;
3584
3585 // info window popup is always local to tab page.
3586 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3587 if (wp->w_popup_flags & POPF_INFO)
3588 return wp;
3589 return NULL;
3590}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003591#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003592
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003593 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003594f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3595{
3596 win_T *wp = popup_find_info_window();
3597
3598 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3599}
3600
3601 void
3602f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003603{
3604 win_T *wp = popup_find_preview_window();
3605
3606 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003607}
3608
Bram Moolenaar79648732019-07-18 21:43:07 +02003609/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003610 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003611 * NOTE: this makes the popup the current window, so that the file can be
3612 * edited. However, it must not remain to be the current window, the caller
3613 * must make sure of that.
3614 */
3615 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003616popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003617{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003618 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003619
3620 if (wp == NULL)
3621 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003622 if (info)
3623 wp->w_popup_flags |= POPF_INFO;
3624 else
3625 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003626
3627 // Set the width to a reasonable value, so that w_topline can be computed.
3628 if (wp->w_minwidth > 0)
3629 wp->w_width = wp->w_minwidth;
3630 else if (wp->w_maxwidth > 0)
3631 wp->w_width = wp->w_maxwidth;
3632 else
3633 wp->w_width = curwin->w_width;
3634
3635 // Will switch to another buffer soon, dummy one can be wiped.
3636 wp->w_buffer->b_locked = FALSE;
3637
3638 win_enter(wp, FALSE);
3639 return OK;
3640}
3641
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003642#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003643/*
3644 * Close any preview popup.
3645 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003646 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003647popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003648{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003649 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003650
3651 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003652 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003653}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003654
3655/*
3656 * Hide the info popup.
3657 */
3658 void
3659popup_hide_info(void)
3660{
3661 win_T *wp = popup_find_info_window();
3662
3663 if (wp != NULL)
3664 popup_hide(wp);
3665}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003666#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003667
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003668/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003669 * Close any popup for a text property associated with "win".
3670 * Return TRUE if a popup was closed.
3671 */
3672 int
3673popup_win_closed(win_T *win)
3674{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003675 int round;
3676 win_T *wp;
3677 win_T *next;
3678 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003679
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003680 for (round = 1; round <= 2; ++round)
3681 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3682 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003683 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003684 next = wp->w_next;
3685 if (wp->w_popup_prop_win == win)
3686 {
3687 popup_close_with_retval(wp, -1);
3688 ret = TRUE;
3689 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003690 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003691 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003692}
3693
3694/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003695 * Set the title of the popup window to the file name.
3696 */
3697 void
3698popup_set_title(win_T *wp)
3699{
3700 if (wp->w_buffer->b_fname != NULL)
3701 {
3702 char_u dirname[MAXPATHL];
3703 size_t len;
3704
3705 mch_dirname(dirname, MAXPATHL);
3706 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3707
3708 vim_free(wp->w_popup_title);
3709 len = STRLEN(wp->w_buffer->b_fname) + 3;
3710 wp->w_popup_title = alloc((int)len);
3711 if (wp->w_popup_title != NULL)
3712 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3713 wp->w_buffer->b_fname);
3714 redraw_win_later(wp, VALID);
3715 }
3716}
3717
3718/*
3719 * If there is a preview window, update the title.
3720 * Used after changing directory.
3721 */
3722 void
3723popup_update_preview_title(void)
3724{
3725 win_T *wp = popup_find_preview_window();
3726
3727 if (wp != NULL)
3728 popup_set_title(wp);
3729}
3730
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003731#endif // FEAT_TEXT_PROP