blob: ed04645ea10ba4a7ab17f087359362bc4810e76b [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
Bram Moolenaarb754b5b2019-10-24 19:25:00 +020074 // Zero means "not set", use -1 instead.
75 if (n == 0)
76 n = -1;
Bram Moolenaarcc31ad92019-05-30 19:25:06 +020077 return n;
78}
79
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +020080 static void
Bram Moolenaarae943152019-06-16 22:54:14 +020081set_padding_border(dict_T *dict, int *array, char *name, int max_val)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082{
83 dictitem_T *di;
84
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020085 di = dict_find(dict, (char_u *)name, -1);
86 if (di != NULL)
87 {
88 if (di->di_tv.v_type != VAR_LIST)
89 emsg(_(e_listreq));
90 else
91 {
92 list_T *list = di->di_tv.vval.v_list;
93 listitem_T *li;
94 int i;
95 int nr;
96
97 for (i = 0; i < 4; ++i)
98 array[i] = 1;
99 if (list != NULL)
100 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
101 ++i, li = li->li_next)
102 {
103 nr = (int)tv_get_number(&li->li_tv);
104 if (nr >= 0)
105 array[i] = nr > max_val ? max_val : nr;
106 }
107 }
108 }
109}
110
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200111/*
Bram Moolenaar17627312019-06-02 19:53:44 +0200112 * Used when popup options contain "moved": set default moved values.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200113 */
114 static void
Bram Moolenaar17627312019-06-02 19:53:44 +0200115set_moved_values(win_T *wp)
116{
117 wp->w_popup_curwin = curwin;
118 wp->w_popup_lnum = curwin->w_cursor.lnum;
119 wp->w_popup_mincol = curwin->w_cursor.col;
120 wp->w_popup_maxcol = curwin->w_cursor.col;
121}
122
123/*
124 * Used when popup options contain "moved" with "word" or "WORD".
125 */
126 static void
127set_moved_columns(win_T *wp, int flags)
128{
129 char_u *ptr;
130 int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
131
132 if (len > 0)
133 {
134 wp->w_popup_mincol = (int)(ptr - ml_get_curline());
135 wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
136 }
137}
138
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200139/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200140 * Used when popup options contain "mousemoved": set default moved values.
141 */
142 static void
143set_mousemoved_values(win_T *wp)
144{
145 wp->w_popup_mouse_row = mouse_row;
146 wp->w_popup_mouse_mincol = mouse_col;
147 wp->w_popup_mouse_maxcol = mouse_col;
148}
149
150/*
151 * Used when popup options contain "moved" with "word" or "WORD".
152 */
153 static void
154set_mousemoved_columns(win_T *wp, int flags)
155{
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200156 win_T *textwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200157 char_u *text;
158 int col;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200159 pos_T pos;
160 colnr_T mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200161
162 if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags,
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200163 &textwp, &pos.lnum, &text, NULL, &col) == OK)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200164 {
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200165 // convert text column to mouse column
166 pos.col = col;
167 pos.coladd = 0;
168 getvcol(textwp, &pos, &mcol, NULL, NULL);
169 wp->w_popup_mouse_mincol = mcol;
170
Bram Moolenaar10727682019-07-12 13:59:20 +0200171 pos.col = col + (colnr_T)STRLEN(text) - 1;
Bram Moolenaarb05caa72019-07-10 21:55:54 +0200172 getvcol(textwp, &pos, NULL, NULL, &mcol);
173 wp->w_popup_mouse_maxcol = mcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200174 vim_free(text);
175 }
176}
177
178/*
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200179 * Return TRUE if "row"/"col" is on the border of the popup.
180 * The values are relative to the top-left corner.
181 */
182 int
183popup_on_border(win_T *wp, int row, int col)
184{
185 return (row == 0 && wp->w_popup_border[0] > 0)
186 || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
187 || (col == 0 && wp->w_popup_border[3] > 0)
188 || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
189}
190
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200191/*
Bram Moolenaarf6396232019-08-24 19:36:00 +0200192 * Return TRUE and close the popup if "row"/"col" is on the "X" button of the
193 * popup and w_popup_close is POPCLOSE_BUTTON.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200194 * The values are relative to the top-left corner.
Bram Moolenaarf6396232019-08-24 19:36:00 +0200195 * Caller should check the left mouse button was clicked.
196 * Return TRUE if the popup was closed.
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200197 */
198 int
Bram Moolenaarf6396232019-08-24 19:36:00 +0200199popup_close_if_on_X(win_T *wp, int row, int col)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200200{
Bram Moolenaarf6396232019-08-24 19:36:00 +0200201 if (wp->w_popup_close == POPCLOSE_BUTTON
202 && row == 0 && col == popup_width(wp) - 1)
203 {
204 popup_close_for_mouse_click(wp);
205 return TRUE;
206 }
207 return FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200208}
209
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200210// Values set when dragging a popup window starts.
211static int drag_start_row;
212static int drag_start_col;
213static int drag_start_wantline;
214static int drag_start_wantcol;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200215static int drag_on_resize_handle;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200216
217/*
218 * Mouse down on border of popup window: start dragging it.
219 * Uses mouse_col and mouse_row.
220 */
221 void
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200222popup_start_drag(win_T *wp, int row, int col)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200223{
224 drag_start_row = mouse_row;
225 drag_start_col = mouse_col;
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200226 if (wp->w_wantline <= 0)
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200227 drag_start_wantline = wp->w_winrow + 1;
228 else
229 drag_start_wantline = wp->w_wantline;
230 if (wp->w_wantcol == 0)
231 drag_start_wantcol = wp->w_wincol + 1;
232 else
233 drag_start_wantcol = wp->w_wantcol;
Bram Moolenaara42d9452019-06-15 21:46:30 +0200234
235 // Stop centering the popup
236 if (wp->w_popup_pos == POPPOS_CENTER)
237 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200238
239 drag_on_resize_handle = wp->w_popup_border[1] > 0
240 && wp->w_popup_border[2] > 0
241 && row == popup_height(wp) - 1
242 && col == popup_width(wp) - 1;
243
244 if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle)
245 {
246 if (wp->w_popup_pos == POPPOS_TOPRIGHT
247 || wp->w_popup_pos == POPPOS_BOTRIGHT)
248 wp->w_wantcol = wp->w_wincol + 1;
249 if (wp->w_popup_pos == POPPOS_BOTLEFT)
250 wp->w_wantline = wp->w_winrow + 1;
251 wp->w_popup_pos = POPPOS_TOPLEFT;
252 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200253}
254
255/*
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200256 * Mouse moved while dragging a popup window: adjust the window popup position
257 * or resize.
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200258 */
259 void
260popup_drag(win_T *wp)
261{
262 // The popup may be closed before dragging stops.
263 if (!win_valid_popup(wp))
264 return;
265
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200266 if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle)
267 {
268 int width_inc = mouse_col - drag_start_col;
269 int height_inc = mouse_row - drag_start_row;
270
271 if (width_inc != 0)
272 {
273 int width = wp->w_width + width_inc;
274
275 if (width < 1)
276 width = 1;
277 wp->w_minwidth = width;
278 wp->w_maxwidth = width;
279 drag_start_col = mouse_col;
280 }
281
282 if (height_inc != 0)
283 {
284 int height = wp->w_height + height_inc;
285
286 if (height < 1)
287 height = 1;
288 wp->w_minheight = height;
289 wp->w_maxheight = height;
290 drag_start_row = mouse_row;
291 }
292
293 popup_adjust_position(wp);
294 return;
295 }
296
297 if (!(wp->w_popup_flags & POPF_DRAG))
298 return;
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200299 wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
300 if (wp->w_wantline < 1)
301 wp->w_wantline = 1;
302 if (wp->w_wantline > Rows)
303 wp->w_wantline = Rows;
304 wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
305 if (wp->w_wantcol < 1)
306 wp->w_wantcol = 1;
307 if (wp->w_wantcol > Columns)
308 wp->w_wantcol = Columns;
309
310 popup_adjust_position(wp);
311}
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200312
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200313/*
314 * Set w_firstline to match the current "wp->w_topline".
315 */
316 void
317popup_set_firstline(win_T *wp)
318{
319 int height = wp->w_height;
320
321 wp->w_firstline = wp->w_topline;
322 popup_adjust_position(wp);
323
324 // we don't want the popup to get smaller, decrement the first line
325 // until it doesn't
326 while (wp->w_firstline > 1 && wp->w_height < height)
327 {
328 --wp->w_firstline;
329 popup_adjust_position(wp);
330 }
331}
332
333/*
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200334 * Return TRUE if the position is in the popup window scrollbar.
335 */
336 int
337popup_is_in_scrollbar(win_T *wp, int row, int col)
338{
339 return wp->w_has_scrollbar
340 && row >= wp->w_popup_border[0]
341 && row < popup_height(wp) - wp->w_popup_border[2]
342 && col == popup_width(wp) - wp->w_popup_border[1] - 1;
343}
344
345
346/*
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200347 * Handle a click in a popup window, if it is in the scrollbar.
348 */
349 void
350popup_handle_scrollbar_click(win_T *wp, int row, int col)
351{
352 int height = popup_height(wp);
353 int old_topline = wp->w_topline;
354
Bram Moolenaar13b11ed2019-08-01 15:52:45 +0200355 if (popup_is_in_scrollbar(wp, row, col))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +0200356 {
357 if (row >= height / 2)
358 {
359 // Click in lower half, scroll down.
360 if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count)
361 ++wp->w_topline;
362 }
363 else if (wp->w_topline > 1)
364 // click on upper half, scroll up.
365 --wp->w_topline;
366 if (wp->w_topline != old_topline)
367 {
368 popup_set_firstline(wp);
369 redraw_win_later(wp, NOT_VALID);
370 }
371 }
372}
373
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200374#if defined(FEAT_TIMERS)
375 static void
376popup_add_timeout(win_T *wp, int time)
377{
378 char_u cbbuf[50];
379 char_u *ptr = cbbuf;
380 typval_T tv;
381
382 vim_snprintf((char *)cbbuf, sizeof(cbbuf),
383 "{_ -> popup_close(%d)}", wp->w_id);
384 if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
385 {
386 wp->w_popup_timer = create_timer(time, 0);
387 wp->w_popup_timer->tr_callback = get_callback(&tv);
388 clear_tv(&tv);
389 }
390}
391#endif
392
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100393 static poppos_T
394get_pos_entry(dict_T *d, int give_error)
395{
396 char_u *str = dict_get_string(d, (char_u *)"pos", FALSE);
397 int nr;
398
399 if (str == NULL)
400 return POPPOS_NONE;
401
402 for (nr = 0; nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
403 ++nr)
404 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
405 return poppos_entries[nr].pp_val;
406
407 if (give_error)
408 semsg(_(e_invarg2), str);
409 return POPPOS_NONE;
410}
411
Bram Moolenaar17627312019-06-02 19:53:44 +0200412/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200413 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200414 */
415 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200416apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200417{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200418 int nr;
419 char_u *str;
420 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200421
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200422 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200423 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200425 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200426 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200427 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200429 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200430
431 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200432 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200433 wp->w_wantline = nr;
434 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200435 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200436 wp->w_wantcol = nr;
437
438 di = dict_find(d, (char_u *)"fixed", -1);
439 if (di != NULL)
440 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
441
Bram Moolenaar12034e22019-08-25 22:25:02 +0200442 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100443 poppos_T ppt = get_pos_entry(d, TRUE);
444
445 if (ppt != POPPOS_NONE)
446 wp->w_popup_pos = ppt;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200447 }
448
449 str = dict_get_string(d, (char_u *)"textprop", FALSE);
450 if (str != NULL)
451 {
452 wp->w_popup_prop_type = 0;
453 if (*str != NUL)
454 {
455 nr = find_prop_type_id(str, wp->w_buffer);
456 if (nr <= 0)
457 nr = find_prop_type_id(str, NULL);
458 if (nr <= 0)
459 semsg(_(e_invarg2), str);
460 else
461 {
462 wp->w_popup_prop_type = nr;
463 wp->w_popup_prop_win = curwin;
464
465 di = dict_find(d, (char_u *)"textpropwin", -1);
466 if (di != NULL)
467 {
468 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
469 if (win_valid(wp->w_popup_prop_win))
470 wp->w_popup_prop_win = curwin;
471 }
472 }
473 }
474 }
475
476 di = dict_find(d, (char_u *)"textpropid", -1);
477 if (di != NULL)
478 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200479}
480
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200481 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200482handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
483{
484 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
485 {
486 char_u *s = di->di_tv.vval.v_string;
487 int flags = 0;
488
489 if (STRCMP(s, "word") == 0)
490 flags = FIND_IDENT | FIND_STRING;
491 else if (STRCMP(s, "WORD") == 0)
492 flags = FIND_STRING;
493 else if (STRCMP(s, "expr") == 0)
494 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
495 else if (STRCMP(s, "any") != 0)
496 semsg(_(e_invarg2), s);
497 if (flags != 0)
498 {
499 if (mousemoved)
500 set_mousemoved_columns(wp, flags);
501 else
502 set_moved_columns(wp, flags);
503 }
504 }
505 else if (di->di_tv.v_type == VAR_LIST
506 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200507 && (di->di_tv.vval.v_list->lv_len == 2
508 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200509 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200510 list_T *l = di->di_tv.vval.v_list;
511 listitem_T *li = l->lv_first;
512 int mincol;
513 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200514
Bram Moolenaara1396152019-10-20 18:46:05 +0200515 if (di->di_tv.vval.v_list->lv_len == 3)
516 {
517 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
518
519 // Three numbers, might be from popup_getoptions().
520 if (mousemoved)
521 wp->w_popup_mouse_row = nr;
522 else
523 wp->w_popup_lnum = nr;
524 li = li->li_next;
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +0100525 if (nr == 0)
526 wp->w_popup_curwin = NULL;
Bram Moolenaara1396152019-10-20 18:46:05 +0200527 }
528
529 mincol = tv_get_number(&li->li_tv);
530 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200531 if (mousemoved)
532 {
533 wp->w_popup_mouse_mincol = mincol;
534 wp->w_popup_mouse_maxcol = maxcol;
535 }
536 else
537 {
538 wp->w_popup_mincol = mincol;
539 wp->w_popup_maxcol = maxcol;
540 }
541 }
542 else
543 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
544}
545
546 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200547check_highlight(dict_T *dict, char *name, char_u **pval)
548{
549 dictitem_T *di;
550 char_u *str;
551
552 di = dict_find(dict, (char_u *)name, -1);
553 if (di != NULL)
554 {
555 if (di->di_tv.v_type != VAR_STRING)
556 semsg(_(e_invargval), name);
557 else
558 {
559 str = tv_get_string(&di->di_tv);
560 if (*str != NUL)
561 *pval = vim_strsave(str);
562 }
563 }
564}
565
Bram Moolenaarae943152019-06-16 22:54:14 +0200566/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200567 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200568 */
569 static void
570popup_show_curline(win_T *wp)
571{
572 if (wp->w_cursor.lnum < wp->w_topline)
573 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200574 else if (wp->w_cursor.lnum >= wp->w_botline
575 && (curwin->w_valid & VALID_BOTLINE))
576 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200577 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200578 if (wp->w_topline < 1)
579 wp->w_topline = 1;
580 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
581 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200582 while (wp->w_topline < wp->w_cursor.lnum
583 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
584 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
585 > wp->w_height)
586 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200587 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200588
589 // Don't use "firstline" now.
590 wp->w_firstline = 0;
591}
592
593/*
594 * Get the sign group name for window "wp".
595 * Returns a pointer to a static buffer, overwritten on the next call.
596 */
597 static char_u *
598popup_get_sign_name(win_T *wp)
599{
600 static char buf[30];
601
602 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
603 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200604}
605
606/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200607 * Highlight the line with the cursor.
608 * Also scrolls the text to put the cursor line in view.
609 */
610 static void
611popup_highlight_curline(win_T *wp)
612{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200613 int sign_id = 0;
614 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200615
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200616 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200617
618 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
619 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200620 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200621
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200622 if (!sign_exists_by_name(sign_name))
623 {
624 char *linehl = "PopupSelected";
625
626 if (syn_name2id((char_u *)linehl) == 0)
627 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200628 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200629 }
630
631 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
632 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
633 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200634 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200635 else
636 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200637 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200638}
639
640/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200641 * Shared between popup_create() and f_popup_setoptions().
642 */
643 static void
644apply_general_options(win_T *wp, dict_T *dict)
645{
646 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200647 int nr;
648 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200649
Bram Moolenaarae943152019-06-16 22:54:14 +0200650 // TODO: flip
651
652 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200653 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200654 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200655 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200656 if (wp->w_firstline < 0)
657 wp->w_firstline = -1;
658 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200659
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200660 di = dict_find(dict, (char_u *)"scrollbar", -1);
661 if (di != NULL)
662 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
663
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200664 str = dict_get_string(dict, (char_u *)"title", FALSE);
665 if (str != NULL)
666 {
667 vim_free(wp->w_popup_title);
668 wp->w_popup_title = vim_strsave(str);
669 }
670
Bram Moolenaar402502d2019-05-30 22:07:36 +0200671 di = dict_find(dict, (char_u *)"wrap", -1);
672 if (di != NULL)
673 {
674 nr = dict_get_number(dict, (char_u *)"wrap");
675 wp->w_p_wrap = nr != 0;
676 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200677
Bram Moolenaara42d9452019-06-15 21:46:30 +0200678 di = dict_find(dict, (char_u *)"drag", -1);
679 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200680 {
681 nr = dict_get_number(dict, (char_u *)"drag");
682 if (nr)
683 wp->w_popup_flags |= POPF_DRAG;
684 else
685 wp->w_popup_flags &= ~POPF_DRAG;
686 }
687
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100688 di = dict_find(dict, (char_u *)"posinvert", -1);
689 if (di != NULL)
690 {
691 nr = dict_get_number(dict, (char_u *)"posinvert");
692 if (nr)
693 wp->w_popup_flags |= POPF_POSINVERT;
694 else
695 wp->w_popup_flags &= ~POPF_POSINVERT;
696 }
697
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200698 di = dict_find(dict, (char_u *)"resize", -1);
699 if (di != NULL)
700 {
701 nr = dict_get_number(dict, (char_u *)"resize");
702 if (nr)
703 wp->w_popup_flags |= POPF_RESIZE;
704 else
705 wp->w_popup_flags &= ~POPF_RESIZE;
706 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200707
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200708 di = dict_find(dict, (char_u *)"close", -1);
709 if (di != NULL)
710 {
711 int ok = TRUE;
712
713 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
714 {
715 char_u *s = di->di_tv.vval.v_string;
716
717 if (STRCMP(s, "none") == 0)
718 wp->w_popup_close = POPCLOSE_NONE;
719 else if (STRCMP(s, "button") == 0)
720 wp->w_popup_close = POPCLOSE_BUTTON;
721 else if (STRCMP(s, "click") == 0)
722 wp->w_popup_close = POPCLOSE_CLICK;
723 else
724 ok = FALSE;
725 }
726 else
727 ok = FALSE;
728 if (!ok)
729 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
730 }
731
Bram Moolenaarae943152019-06-16 22:54:14 +0200732 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
733 if (str != NULL)
734 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
735 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200736
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200737 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
738 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200739 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
740 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200741
Bram Moolenaar790498b2019-06-01 22:15:29 +0200742 di = dict_find(dict, (char_u *)"borderhighlight", -1);
743 if (di != NULL)
744 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200745 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200746 emsg(_(e_listreq));
747 else
748 {
749 list_T *list = di->di_tv.vval.v_list;
750 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200751 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200752
Bram Moolenaar403d0902019-07-17 21:37:32 +0200753 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
754 ++i, li = li->li_next)
755 {
756 str = tv_get_string(&li->li_tv);
757 if (*str != NUL)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100758 {
759 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200760 wp->w_border_highlight[i] = vim_strsave(str);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100761 }
Bram Moolenaar403d0902019-07-17 21:37:32 +0200762 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200763 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
764 for (i = 1; i < 4; ++i)
Bram Moolenaar622b6462019-11-10 15:16:54 +0100765 {
766 vim_free(wp->w_border_highlight[i]);
Bram Moolenaar403d0902019-07-17 21:37:32 +0200767 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200768 vim_strsave(wp->w_border_highlight[0]);
Bram Moolenaar622b6462019-11-10 15:16:54 +0100769 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200770 }
771 }
772
Bram Moolenaar790498b2019-06-01 22:15:29 +0200773 di = dict_find(dict, (char_u *)"borderchars", -1);
774 if (di != NULL)
775 {
776 if (di->di_tv.v_type != VAR_LIST)
777 emsg(_(e_listreq));
778 else
779 {
780 list_T *list = di->di_tv.vval.v_list;
781 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200782 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200783
784 if (list != NULL)
785 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
786 ++i, li = li->li_next)
787 {
788 str = tv_get_string(&li->li_tv);
789 if (*str != NUL)
790 wp->w_border_char[i] = mb_ptr2char(str);
791 }
792 if (list->lv_len == 1)
793 for (i = 1; i < 8; ++i)
794 wp->w_border_char[i] = wp->w_border_char[0];
795 if (list->lv_len == 2)
796 {
797 for (i = 4; i < 8; ++i)
798 wp->w_border_char[i] = wp->w_border_char[1];
799 for (i = 1; i < 4; ++i)
800 wp->w_border_char[i] = wp->w_border_char[0];
801 }
802 }
803 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200804
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200805 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
806 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
807
Bram Moolenaarae943152019-06-16 22:54:14 +0200808 di = dict_find(dict, (char_u *)"zindex", -1);
809 if (di != NULL)
810 {
811 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
812 if (wp->w_zindex < 1)
813 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
814 if (wp->w_zindex > 32000)
815 wp->w_zindex = 32000;
816 }
817
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200818 di = dict_find(dict, (char_u *)"mask", -1);
819 if (di != NULL)
820 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200821 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200822
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200823 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200824 {
825 listitem_T *li;
826
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200827 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200828 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
829 li = li->li_next)
830 {
831 if (li->li_tv.v_type != VAR_LIST
832 || li->li_tv.vval.v_list == NULL
833 || li->li_tv.vval.v_list->lv_len != 4)
834 {
835 ok = FALSE;
836 break;
837 }
838 }
839 }
840 if (ok)
841 {
842 wp->w_popup_mask = di->di_tv.vval.v_list;
843 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200844 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200845 }
846 else
847 semsg(_(e_invargval), "mask");
848 }
849
Bram Moolenaarae943152019-06-16 22:54:14 +0200850#if defined(FEAT_TIMERS)
851 // Add timer to close the popup after some time.
852 nr = dict_get_number(dict, (char_u *)"time");
853 if (nr > 0)
854 popup_add_timeout(wp, nr);
855#endif
856
Bram Moolenaar3397f742019-06-02 18:40:06 +0200857 di = dict_find(dict, (char_u *)"moved", -1);
858 if (di != NULL)
859 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200860 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200861 handle_moved_argument(wp, di, FALSE);
862 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200863
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200864 di = dict_find(dict, (char_u *)"mousemoved", -1);
865 if (di != NULL)
866 {
867 set_mousemoved_values(wp);
868 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200869 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200870
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200871 di = dict_find(dict, (char_u *)"cursorline", -1);
872 if (di != NULL)
873 {
874 if (di->di_tv.v_type == VAR_NUMBER)
875 {
876 if (di->di_tv.vval.v_number != 0)
877 wp->w_popup_flags |= POPF_CURSORLINE;
878 else
879 wp->w_popup_flags &= ~POPF_CURSORLINE;
880 }
881 else
882 semsg(_(e_invargval), "cursorline");
883 }
884
Bram Moolenaarae943152019-06-16 22:54:14 +0200885 di = dict_find(dict, (char_u *)"filter", -1);
886 if (di != NULL)
887 {
888 callback_T callback = get_callback(&di->di_tv);
889
890 if (callback.cb_name != NULL)
891 {
892 free_callback(&wp->w_filter_cb);
893 set_callback(&wp->w_filter_cb, &callback);
894 }
895 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200896 di = dict_find(dict, (char_u *)"mapping", -1);
897 if (di != NULL)
898 {
899 nr = dict_get_number(dict, (char_u *)"mapping");
900 if (nr)
901 wp->w_popup_flags |= POPF_MAPPING;
902 else
903 wp->w_popup_flags &= ~POPF_MAPPING;
904 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200905
Bram Moolenaar581ba392019-09-03 22:08:33 +0200906 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
907 if (str != NULL)
908 {
909 if (STRCMP(str, "a") == 0)
910 wp->w_filter_mode = MODE_ALL;
911 else
912 wp->w_filter_mode = mode_str2flags(str);
913 }
914
Bram Moolenaarae943152019-06-16 22:54:14 +0200915 di = dict_find(dict, (char_u *)"callback", -1);
916 if (di != NULL)
917 {
918 callback_T callback = get_callback(&di->di_tv);
919
920 if (callback.cb_name != NULL)
921 {
922 free_callback(&wp->w_close_cb);
923 set_callback(&wp->w_close_cb, &callback);
924 }
925 }
926}
927
928/*
929 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200930 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200931 */
932 static void
933apply_options(win_T *wp, dict_T *dict)
934{
935 int nr;
936
937 apply_move_options(wp, dict);
938 apply_general_options(wp, dict);
939
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200940 nr = dict_get_number(dict, (char_u *)"hidden");
941 if (nr > 0)
942 {
943 wp->w_popup_flags |= POPF_HIDDEN;
944 --wp->w_buffer->b_nwindows;
945 }
946
Bram Moolenaar33796b32019-06-08 16:01:13 +0200947 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200948 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200949}
950
951/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200952 * Add lines to the popup from a list of strings.
953 */
954 static void
955add_popup_strings(buf_T *buf, list_T *l)
956{
957 listitem_T *li;
958 linenr_T lnum = 0;
959 char_u *p;
960
961 for (li = l->lv_first; li != NULL; li = li->li_next)
962 if (li->li_tv.v_type == VAR_STRING)
963 {
964 p = li->li_tv.vval.v_string;
965 ml_append_buf(buf, lnum++,
966 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
967 }
968}
969
970/*
971 * Add lines to the popup from a list of dictionaries.
972 */
973 static void
974add_popup_dicts(buf_T *buf, list_T *l)
975{
976 listitem_T *li;
977 listitem_T *pli;
978 linenr_T lnum = 0;
979 char_u *p;
980 dict_T *dict;
981
982 // first add the text lines
983 for (li = l->lv_first; li != NULL; li = li->li_next)
984 {
985 if (li->li_tv.v_type != VAR_DICT)
986 {
987 emsg(_(e_dictreq));
988 return;
989 }
990 dict = li->li_tv.vval.v_dict;
991 p = dict == NULL ? NULL
992 : dict_get_string(dict, (char_u *)"text", FALSE);
993 ml_append_buf(buf, lnum++,
994 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
995 }
996
997 // add the text properties
998 lnum = 1;
999 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
1000 {
1001 dictitem_T *di;
1002 list_T *plist;
1003
1004 dict = li->li_tv.vval.v_dict;
1005 di = dict_find(dict, (char_u *)"props", -1);
1006 if (di != NULL)
1007 {
1008 if (di->di_tv.v_type != VAR_LIST)
1009 {
1010 emsg(_(e_listreq));
1011 return;
1012 }
1013 plist = di->di_tv.vval.v_list;
1014 if (plist != NULL)
1015 {
1016 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
1017 {
1018 if (pli->li_tv.v_type != VAR_DICT)
1019 {
1020 emsg(_(e_dictreq));
1021 return;
1022 }
1023 dict = pli->li_tv.vval.v_dict;
1024 if (dict != NULL)
1025 {
1026 int col = dict_get_number(dict, (char_u *)"col");
1027
1028 prop_add_common( lnum, col, dict, buf, NULL);
1029 }
1030 }
1031 }
1032 }
1033 }
1034}
1035
1036/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001037 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1038 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001039 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001040popup_top_extra(win_T *wp)
1041{
1042 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1043
1044 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1045 return 1;
1046 return extra;
1047}
1048
1049/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001050 * Return the height of popup window "wp", including border and padding.
1051 */
1052 int
1053popup_height(win_T *wp)
1054{
1055 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001056 + popup_top_extra(wp)
1057 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001058}
1059
1060/*
1061 * Return the width of popup window "wp", including border, padding and
1062 * scrollbar.
1063 */
1064 int
1065popup_width(win_T *wp)
1066{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001067 // w_leftcol is how many columns of the core are left of the screen
1068 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001069 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001070 + popup_extra_width(wp)
1071 + wp->w_popup_rightoff;
1072}
1073
1074/*
1075 * Return the extra width of popup window "wp": border, padding and scrollbar.
1076 */
1077 int
1078popup_extra_width(win_T *wp)
1079{
1080 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1081 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1082 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001083}
1084
1085/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001086 * Adjust the position and size of the popup to fit on the screen.
1087 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001088 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001089popup_adjust_position(win_T *wp)
1090{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001091 linenr_T lnum;
1092 int wrapped = 0;
1093 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001094 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001095 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001096 int center_vert = FALSE;
1097 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001098 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001099 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001100 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1101 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1102 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1103 int extra_height = top_extra + bot_extra;
1104 int extra_width = left_extra + right_extra;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001105 int w_height_before_limit;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001106 int org_winrow = wp->w_winrow;
1107 int org_wincol = wp->w_wincol;
1108 int org_width = wp->w_width;
1109 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001110 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001111 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001112 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001113 int wantline = wp->w_wantline; // adjusted for textprop
1114 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001115 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001116
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001117 wp->w_winrow = 0;
1118 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001119 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001120 wp->w_popup_leftoff = 0;
1121 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001122
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001123 // May need to update the "cursorline" highlighting, which may also change
1124 // "topline"
1125 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1126 popup_highlight_curline(wp);
1127
Bram Moolenaar12034e22019-08-25 22:25:02 +02001128 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1129 {
1130 win_T *prop_win = wp->w_popup_prop_win;
1131 textprop_T prop;
1132 linenr_T prop_lnum;
1133 pos_T pos;
1134 int screen_row;
1135 int screen_scol;
1136 int screen_ccol;
1137 int screen_ecol;
1138
1139 // Popup window is positioned relative to a text property.
1140 if (find_visible_prop(prop_win,
1141 wp->w_popup_prop_type, wp->w_popup_prop_id,
1142 &prop, &prop_lnum) == FAIL)
1143 {
1144 // Text property is no longer visible, hide the popup.
1145 // Unhiding the popup is done in check_popup_unhidden().
1146 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1147 {
1148 wp->w_popup_flags |= POPF_HIDDEN;
1149 --wp->w_buffer->b_nwindows;
1150 if (win_valid(wp->w_popup_prop_win))
1151 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1152 }
1153 return;
1154 }
1155
1156 // Compute the desired position from the position of the text
1157 // property. Use "wantline" and "wantcol" as offsets.
1158 pos.lnum = prop_lnum;
1159 pos.col = prop.tp_col;
1160 if (wp->w_popup_pos == POPPOS_TOPLEFT
1161 || wp->w_popup_pos == POPPOS_BOTLEFT)
1162 pos.col += prop.tp_len - 1;
1163 textpos2screenpos(prop_win, &pos, &screen_row,
1164 &screen_scol, &screen_ccol, &screen_ecol);
1165
1166 if (wp->w_popup_pos == POPPOS_TOPLEFT
1167 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1168 // below the text
1169 wantline = screen_row + wantline + 1;
1170 else
1171 // above the text
1172 wantline = screen_row + wantline - 1;
1173 center_vert = FALSE;
1174 if (wp->w_popup_pos == POPPOS_TOPLEFT
1175 || wp->w_popup_pos == POPPOS_BOTLEFT)
1176 // right of the text
1177 wantcol = screen_ecol + wantcol;
1178 else
1179 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001180 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001181 use_wantcol = TRUE;
1182 }
1183 else
1184 {
1185 // If no line was specified default to vertical centering.
1186 if (wantline == 0)
1187 center_vert = TRUE;
1188 else if (wantline < 0)
1189 // If "wantline" is negative it actually means zero.
1190 wantline = 0;
1191 if (wantcol < 0)
1192 // If "wantcol" is negative it actually means zero.
1193 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001194 }
1195
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001196 if (wp->w_popup_pos == POPPOS_CENTER)
1197 {
1198 // center after computing the size
1199 center_vert = TRUE;
1200 center_hor = TRUE;
1201 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001202 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001203 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001204 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1205 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001206 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001207 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001208 if (wp->w_winrow >= Rows)
1209 wp->w_winrow = Rows - 1;
1210 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001211
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001212 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001213 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001214 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1215 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001216 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001217 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001218 if (wp->w_wincol >= Columns - 3)
1219 wp->w_wincol = Columns - 3;
1220 }
1221 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001222
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001223 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001224 // When left aligned use the space available, but shift to the left when we
1225 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001226 maxspace = Columns - wp->w_wincol - left_extra;
1227 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001228 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001229 {
1230 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001231 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001232 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001233 minwidth = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001234
Bram Moolenaar8d241042019-06-12 23:40:01 +02001235 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001236 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001237 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001238 if (wp->w_topline < 1)
1239 wp->w_topline = 1;
1240 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001241 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1242
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001243 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001244 // Use a minimum width of one, so that something shows when there is no
1245 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001246 // When "firstline" is -1 then start with the last buffer line and go
1247 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001248 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001249 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001250 if (wp->w_firstline < 0)
1251 lnum = wp->w_buffer->b_ml.ml_line_count;
1252 else
1253 lnum = wp->w_topline;
1254 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001255 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001256 int len;
1257 int w_width = wp->w_width;
1258
1259 // Count Tabs for what they are worth and compute the length based on
1260 // the maximum width (matters when 'showbreak' is set).
1261 if (wp->w_width < maxwidth)
1262 wp->w_width = maxwidth;
1263 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001264 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001265 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001266
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001267 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001268 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001269 while (len > maxwidth)
1270 {
1271 ++wrapped;
1272 len -= maxwidth;
1273 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001274 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001275 }
1276 }
1277 else if (len > maxwidth
1278 && allow_adjust_left
1279 && (wp->w_popup_pos == POPPOS_TOPLEFT
1280 || wp->w_popup_pos == POPPOS_BOTLEFT))
1281 {
1282 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001283 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001284
Bram Moolenaar51c31312019-06-15 22:27:23 +02001285 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001286 {
1287 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001288
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001289 len -= truncate_shift;
1290 shift_by -= truncate_shift;
1291 }
1292
1293 wp->w_wincol -= shift_by;
1294 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001295 wp->w_width = maxwidth;
1296 }
1297 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001298 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001299 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001300 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1301 wp->w_width = wp->w_maxwidth;
1302 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001303
1304 if (wp->w_firstline < 0)
1305 --lnum;
1306 else
1307 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001308
1309 // do not use the width of lines we're not going to show
1310 if (wp->w_maxheight > 0
1311 && (wp->w_firstline >= 0
1312 ? lnum - wp->w_topline
1313 : wp->w_buffer->b_ml.ml_line_count - lnum)
1314 + wrapped >= wp->w_maxheight)
1315 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001316 }
1317
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001318 if (wp->w_firstline < 0)
1319 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1320
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001321 wp->w_has_scrollbar = wp->w_want_scrollbar
1322 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001323 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001324 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001325 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001326 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001327 // make space for the scrollbar if needed, when lines wrap and when
1328 // applying minwidth
1329 if (maxwidth + right_extra >= maxspace
1330 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1331 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001332 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001333
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001334 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1335 {
1336 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1337
1338 if (minwidth < title_len)
1339 minwidth = title_len;
1340 }
1341
1342 if (minwidth > 0 && wp->w_width < minwidth)
1343 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001344 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001345 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001346 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001347 // some columns cut off on the right
1348 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001349 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001350 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001351 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001352 {
1353 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1354 if (wp->w_wincol < 0)
1355 wp->w_wincol = 0;
1356 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001357 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1358 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1359 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001360 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001361
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001362 // Right aligned: move to the right if needed.
1363 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001364 if (leftoff >= 0)
1365 wp->w_wincol = leftoff;
1366 else if (wp->w_popup_fixed)
1367 {
1368 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001369 // columns when displaying the window, minus left border and
1370 // padding.
1371 if (-leftoff > left_extra)
1372 wp->w_leftcol = -leftoff - left_extra;
1373 wp->w_width -= wp->w_leftcol;
1374 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001375 if (wp->w_width < 0)
1376 wp->w_width = 0;
1377 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001378 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001379
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001380 if (wp->w_p_wrap || (!wp->w_popup_fixed
1381 && (wp->w_popup_pos == POPPOS_TOPLEFT
1382 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001383 {
1384 int want_col = 0;
1385
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001386 // try to show the right border and any scrollbar
1387 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001388 if (want_col > 0 && wp->w_wincol > 0
1389 && wp->w_wincol + want_col >= Columns)
1390 {
1391 wp->w_wincol = Columns - want_col;
1392 if (wp->w_wincol < 0)
1393 wp->w_wincol = 0;
1394 }
1395 }
1396
Bram Moolenaar8d241042019-06-12 23:40:01 +02001397 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1398 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001399 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1400 wp->w_height = wp->w_minheight;
1401 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1402 wp->w_height = wp->w_maxheight;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001403 w_height_before_limit = wp->w_height;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001404 if (wp->w_height > Rows - wp->w_winrow)
1405 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001406 if (wp->w_height != org_height)
1407 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001408
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001409 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001410 {
1411 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1412 if (wp->w_winrow < 0)
1413 wp->w_winrow = 0;
1414 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001415 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001416 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001417 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001418 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001419 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001420 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001421 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1422 {
1423 // Bottom aligned but does not fit, and less space on the other
1424 // side or "posinvert" is off: reduce height.
1425 wp->w_winrow = 0;
1426 wp->w_height = wantline - extra_height;
1427 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001428 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001429 // Not enough space and more space on the other side: make top
1430 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001431 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001432 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001433 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1434 || wp->w_popup_pos == POPPOS_TOPLEFT)
1435 {
1436 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1437 && wantline * 2 > Rows
1438 && (wp->w_popup_flags & POPF_POSINVERT))
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001439 {
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001440 // top aligned and not enough space below but there is space above:
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001441 // make bottom aligned and recompute the height
1442 wp->w_height = w_height_before_limit;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001443 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
Bram Moolenaar5c6b6182019-11-10 17:51:38 +01001444 if (wp->w_winrow < 0)
1445 {
1446 wp->w_height += wp->w_winrow;
1447 wp->w_winrow = 0;
1448 }
1449 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001450 else
1451 wp->w_winrow = wantline - 1;
1452 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001453 if (wp->w_winrow >= Rows)
1454 wp->w_winrow = Rows - 1;
1455 else if (wp->w_winrow < 0)
1456 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001457
Bram Moolenaar17146962019-05-30 00:12:11 +02001458 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001459 if (win_valid(wp->w_popup_prop_win))
1460 {
1461 wp->w_popup_prop_changedtick =
1462 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1463 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1464 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001465
1466 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001467 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001468 if (org_winrow != wp->w_winrow
1469 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001470 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001471 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001472 || org_width != wp->w_width
1473 || org_height != wp->w_height)
1474 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001475 redraw_win_later(wp, NOT_VALID);
1476 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1477 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001478 popup_mask_refresh = TRUE;
1479 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001480}
1481
Bram Moolenaar17627312019-06-02 19:53:44 +02001482typedef enum
1483{
1484 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001485 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001486 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001487 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001488 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001489 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001490 TYPE_PREVIEW, // preview window
1491 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001492} create_type_T;
1493
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001494/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001495 * Make "buf" empty and set the contents to "text".
1496 * Used by popup_create() and popup_settext().
1497 */
1498 static void
1499popup_set_buffer_text(buf_T *buf, typval_T text)
1500{
1501 int lnum;
1502
1503 // Clear the buffer, then replace the lines.
1504 curbuf = buf;
1505 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1506 ml_delete(lnum, FALSE);
1507 curbuf = curwin->w_buffer;
1508
1509 // Add text to the buffer.
1510 if (text.v_type == VAR_STRING)
1511 {
1512 // just a string
1513 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1514 }
1515 else
1516 {
1517 list_T *l = text.vval.v_list;
1518
1519 if (l->lv_len > 0)
1520 {
1521 if (l->lv_first->li_tv.v_type == VAR_STRING)
1522 // list of strings
1523 add_popup_strings(buf, l);
1524 else
1525 // list of dictionaries
1526 add_popup_dicts(buf, l);
1527 }
1528 }
1529
1530 // delete the line that was in the empty buffer
1531 curbuf = buf;
1532 ml_delete(buf->b_ml.ml_line_count, FALSE);
1533 curbuf = curwin->w_buffer;
1534}
1535
1536/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001537 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1538 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001539 * Return FAIL if the parsing fails.
1540 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001541 static int
1542parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001543{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001544 char_u *p =
1545#ifdef FEAT_QUICKFIX
1546 !is_preview ? p_cpp :
1547#endif
1548 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001549
Bram Moolenaar258cef52019-08-21 17:29:29 +02001550 if (wp != NULL)
1551 wp->w_popup_flags &= ~POPF_INFO_MENU;
1552
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001553 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001554 {
1555 char_u *e, *dig;
1556 char_u *s = p;
1557 int x;
1558
1559 e = vim_strchr(p, ':');
1560 if (e == NULL || e[1] == NUL)
1561 return FAIL;
1562
1563 p = vim_strchr(e, ',');
1564 if (p == NULL)
1565 p = e + STRLEN(e);
1566 dig = e + 1;
1567 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001568
1569 if (STRNCMP(s, "height:", 7) == 0)
1570 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001571 if (dig != p)
1572 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001573 if (wp != NULL)
1574 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001575 if (is_preview)
1576 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001577 wp->w_maxheight = x;
1578 }
1579 }
1580 else if (STRNCMP(s, "width:", 6) == 0)
1581 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001582 if (dig != p)
1583 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001584 if (wp != NULL)
1585 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001586 if (is_preview)
1587 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001588 wp->w_maxwidth = x;
1589 }
1590 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001591 else if (STRNCMP(s, "highlight:", 10) == 0)
1592 {
1593 if (wp != NULL)
1594 {
1595 int c = *p;
1596
1597 *p = NUL;
1598 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1599 s + 10, OPT_FREE|OPT_LOCAL, 0);
1600 *p = c;
1601 }
1602 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001603 else if (STRNCMP(s, "border:", 7) == 0)
1604 {
1605 char_u *arg = s + 7;
1606 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1607 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1608 int i;
1609
1610 if (!on && !off)
1611 return FAIL;
1612 if (wp != NULL)
1613 {
1614 for (i = 0; i < 4; ++i)
1615 wp->w_popup_border[i] = on ? 1 : 0;
1616 if (off)
1617 // only show the X for close when there is a border
1618 wp->w_popup_close = POPCLOSE_NONE;
1619 }
1620 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001621 else if (STRNCMP(s, "align:", 6) == 0)
1622 {
1623 char_u *arg = s + 6;
1624 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1625 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1626
1627 if (!menu && !item)
1628 return FAIL;
1629 if (wp != NULL && menu)
1630 wp->w_popup_flags |= POPF_INFO_MENU;
1631 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001632 else
1633 return FAIL;
1634 }
1635 return OK;
1636}
1637
1638/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001639 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1640 * is not NULL.
1641 * Return FAIL if the parsing fails.
1642 */
1643 int
1644parse_previewpopup(win_T *wp)
1645{
1646 return parse_popup_option(wp, TRUE);
1647}
1648
1649/*
1650 * Parse the 'completepopup' option and apply the values to window "wp" if it
1651 * is not NULL.
1652 * Return FAIL if the parsing fails.
1653 */
1654 int
1655parse_completepopup(win_T *wp)
1656{
1657 return parse_popup_option(wp, FALSE);
1658}
1659
1660/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001661 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001662 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001663 */
1664 void
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001665popup_set_wantpos_cursor(win_T *wp, int width, dict_T *d)
Bram Moolenaar79648732019-07-18 21:43:07 +02001666{
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001667 poppos_T ppt = POPPOS_NONE;
1668
1669 if (d != NULL)
1670 ppt = get_pos_entry(d, FALSE);
1671
Bram Moolenaar79648732019-07-18 21:43:07 +02001672 setcursor_mayforce(TRUE);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001673 if (ppt == POPPOS_TOPRIGHT || ppt == POPPOS_TOPLEFT)
Bram Moolenaar79648732019-07-18 21:43:07 +02001674 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001675 wp->w_wantline = curwin->w_winrow + curwin->w_wrow + 2;
1676 }
1677 else
1678 {
1679 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1680 if (wp->w_wantline == 0) // cursor in first line
1681 {
1682 wp->w_wantline = 2;
1683 wp->w_popup_pos = ppt == POPPOS_BOTRIGHT
1684 ? POPPOS_TOPRIGHT : POPPOS_TOPLEFT;
1685 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001686 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001687
Bram Moolenaar79648732019-07-18 21:43:07 +02001688 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001689 if (wp->w_wantcol > Columns - width)
1690 {
1691 wp->w_wantcol = Columns - width;
1692 if (wp->w_wantcol < 1)
1693 wp->w_wantcol = 1;
1694 }
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001695
Bram Moolenaar79648732019-07-18 21:43:07 +02001696 popup_adjust_position(wp);
1697}
1698
1699/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001700 * Set w_wantline and w_wantcol for the a given screen position.
1701 * Caller must take care of running into the window border.
1702 */
1703 void
1704popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1705{
1706 wp->w_wantline = row;
1707 wp->w_wantcol = col;
1708 popup_adjust_position(wp);
1709}
1710
1711/*
1712 * Add a border and lef&right padding.
1713 */
1714 static void
1715add_border_left_right_padding(win_T *wp)
1716{
1717 int i;
1718
1719 for (i = 0; i < 4; ++i)
1720 {
1721 wp->w_popup_border[i] = 1;
1722 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1723 }
1724}
1725
1726/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001727 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001728 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001729 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001730 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001731 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001732 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001733popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001734{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001735 win_T *wp;
1736 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001737 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001738 int new_buffer;
1739 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001740 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001741 int nr;
1742 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001743
Bram Moolenaar79648732019-07-18 21:43:07 +02001744 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001745 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001746 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001747 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001748 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001749 buf = buflist_findnr( argvars[0].vval.v_number);
1750 if (buf == NULL)
1751 {
1752 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1753 return NULL;
1754 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001755#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001756 if (buf->b_term != NULL)
1757 {
1758 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1759 return NULL;
1760 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001761#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001762 }
1763 else if (!(argvars[0].v_type == VAR_STRING
1764 && argvars[0].vval.v_string != NULL)
1765 && !(argvars[0].v_type == VAR_LIST
1766 && argvars[0].vval.v_list != NULL))
1767 {
1768 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001769 return NULL;
1770 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001771 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001772 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001773 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001774 return NULL;
1775 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001776 d = argvars[1].vval.v_dict;
1777 }
1778
1779 if (d != NULL)
1780 {
1781 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1782 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1783 else if (type == TYPE_NOTIFICATION)
1784 tabnr = -1; // notifications are global by default
1785 else
1786 tabnr = 0;
1787 if (tabnr > 0)
1788 {
1789 tp = find_tabpage(tabnr);
1790 if (tp == NULL)
1791 {
1792 semsg(_("E997: Tabpage not found: %d"), tabnr);
1793 return NULL;
1794 }
1795 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001796 }
1797
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001798 // Create the window and buffer.
1799 wp = win_alloc_popup_win();
1800 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001801 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001802 if (rettv != NULL)
1803 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001804 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001805 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001806
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001807 if (buf != NULL)
1808 {
1809 // use existing buffer
1810 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001811 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001812 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001813 buffer_ensure_loaded(buf);
1814 }
1815 else
1816 {
1817 // create a new buffer associated with the popup
1818 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001819 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001820 if (buf == NULL)
1821 return NULL;
1822 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001823
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001824 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001825
Bram Moolenaar46451042019-08-24 15:50:46 +02001826 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001827 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001828 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001829 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001830 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001831 buf->b_p_ul = -1; // no undo
1832 buf->b_p_swf = FALSE; // no swap file
1833 buf->b_p_bl = FALSE; // unlisted buffer
1834 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001835
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001836 // Avoid that 'buftype' is reset when this buffer is entered.
1837 buf->b_p_initialized = TRUE;
1838 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001839 wp->w_p_wrap = TRUE; // 'wrap' is default on
1840 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001841
Bram Moolenaara3fce622019-06-20 02:31:49 +02001842 if (tp != NULL)
1843 {
1844 // popup on specified tab page
1845 wp->w_next = tp->tp_first_popupwin;
1846 tp->tp_first_popupwin = wp;
1847 }
1848 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001849 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001850 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001851 wp->w_next = curtab->tp_first_popupwin;
1852 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001853 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001854 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001855 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001856 win_T *prev = first_popupwin;
1857
1858 // Global popup: add at the end, so that it gets displayed on top of
1859 // older ones with the same zindex. Matters for notifications.
1860 if (first_popupwin == NULL)
1861 first_popupwin = wp;
1862 else
1863 {
1864 while (prev->w_next != NULL)
1865 prev = prev->w_next;
1866 prev->w_next = wp;
1867 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001868 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001869
Bram Moolenaar79648732019-07-18 21:43:07 +02001870 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001871 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001872
Bram Moolenaar79648732019-07-18 21:43:07 +02001873 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001874 {
1875 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001876 }
1877 if (type == TYPE_ATCURSOR)
1878 {
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001879 popup_set_wantpos_cursor(wp, 0, d);
Bram Moolenaar17627312019-06-02 19:53:44 +02001880 set_moved_values(wp);
1881 set_moved_columns(wp, FIND_STRING);
1882 }
1883
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001884 if (type == TYPE_BEVAL)
1885 {
1886 wp->w_popup_pos = POPPOS_BOTLEFT;
1887
1888 // by default use the mouse position
1889 wp->w_wantline = mouse_row;
1890 if (wp->w_wantline <= 0) // mouse on first line
1891 {
1892 wp->w_wantline = 2;
1893 wp->w_popup_pos = POPPOS_TOPLEFT;
1894 }
1895 wp->w_wantcol = mouse_col + 1;
1896 set_mousemoved_values(wp);
1897 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1898 }
1899
Bram Moolenaar33796b32019-06-08 16:01:13 +02001900 // set default values
1901 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001902 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001903
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001904 if (type == TYPE_NOTIFICATION)
1905 {
1906 win_T *twp, *nextwin;
1907 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001908
1909 // Try to not overlap with another global popup. Guess we need 3
1910 // more screen lines than buffer lines.
1911 wp->w_wantline = 1;
1912 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1913 {
1914 nextwin = twp->w_next;
1915 if (twp != wp
1916 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1917 && twp->w_winrow <= wp->w_wantline - 1 + height
1918 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1919 {
1920 // move to below this popup and restart the loop to check for
1921 // overlap with other popups
1922 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1923 nextwin = first_popupwin;
1924 }
1925 }
1926 if (wp->w_wantline + height > Rows)
1927 {
1928 // can't avoid overlap, put on top in the hope that message goes
1929 // away soon.
1930 wp->w_wantline = 1;
1931 }
1932
1933 wp->w_wantcol = 10;
1934 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001935 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001936 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001937 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001938 for (i = 0; i < 4; ++i)
1939 wp->w_popup_border[i] = 1;
1940 wp->w_popup_padding[1] = 1;
1941 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001942
1943 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001944 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001945 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1946 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001947 }
1948
Bram Moolenaara730e552019-06-16 19:05:31 +02001949 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001950 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001951 wp->w_popup_pos = POPPOS_CENTER;
1952 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001953 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001954 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001955 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001956 }
1957
Bram Moolenaara730e552019-06-16 19:05:31 +02001958 if (type == TYPE_MENU)
1959 {
1960 typval_T tv;
1961 callback_T callback;
1962
1963 tv.v_type = VAR_STRING;
1964 tv.vval.v_string = (char_u *)"popup_filter_menu";
1965 callback = get_callback(&tv);
1966 if (callback.cb_name != NULL)
1967 set_callback(&wp->w_filter_cb, &callback);
1968
1969 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001970 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001971 }
1972
Bram Moolenaar79648732019-07-18 21:43:07 +02001973 if (type == TYPE_PREVIEW)
1974 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001975 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001976 wp->w_popup_close = POPCLOSE_BUTTON;
1977 for (i = 0; i < 4; ++i)
1978 wp->w_popup_border[i] = 1;
1979 parse_previewpopup(wp);
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001980 popup_set_wantpos_cursor(wp, wp->w_minwidth, d);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001981 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001982# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001983 if (type == TYPE_INFO)
1984 {
1985 wp->w_popup_pos = POPPOS_TOPLEFT;
1986 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1987 wp->w_popup_close = POPCLOSE_BUTTON;
1988 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001989 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001990 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001991# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001992
Bram Moolenaarae943152019-06-16 22:54:14 +02001993 for (i = 0; i < 4; ++i)
1994 VIM_CLEAR(wp->w_border_highlight[i]);
1995 for (i = 0; i < 8; ++i)
1996 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001997 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001998 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001999 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02002000
Bram Moolenaar79648732019-07-18 21:43:07 +02002001 if (d != NULL)
2002 // Deal with options.
2003 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002004
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002005#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002006 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
2007 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02002008#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002009
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002010 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002011
2012 wp->w_vsep_width = 0;
2013
2014 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002015 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02002016
2017 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002018}
2019
2020/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002021 * popup_clear()
2022 */
2023 void
2024f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2025{
2026 close_all_popups();
2027}
2028
2029/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002030 * popup_create({text}, {options})
2031 */
2032 void
2033f_popup_create(typval_T *argvars, typval_T *rettv)
2034{
Bram Moolenaar17627312019-06-02 19:53:44 +02002035 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002036}
2037
2038/*
2039 * popup_atcursor({text}, {options})
2040 */
2041 void
2042f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2043{
Bram Moolenaar17627312019-06-02 19:53:44 +02002044 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002045}
2046
2047/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002048 * popup_beval({text}, {options})
2049 */
2050 void
2051f_popup_beval(typval_T *argvars, typval_T *rettv)
2052{
2053 popup_create(argvars, rettv, TYPE_BEVAL);
2054}
2055
2056/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002057 * Invoke the close callback for window "wp" with value "result".
2058 * Careful: The callback may make "wp" invalid!
2059 */
2060 static void
2061invoke_popup_callback(win_T *wp, typval_T *result)
2062{
2063 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002064 typval_T argv[3];
2065
2066 argv[0].v_type = VAR_NUMBER;
2067 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2068
2069 if (result != NULL && result->v_type != VAR_UNKNOWN)
2070 copy_tv(result, &argv[1]);
2071 else
2072 {
2073 argv[1].v_type = VAR_NUMBER;
2074 argv[1].vval.v_number = 0;
2075 }
2076
2077 argv[2].v_type = VAR_UNKNOWN;
2078
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002079 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002080 if (result != NULL)
2081 clear_tv(&argv[1]);
2082 clear_tv(&rettv);
2083}
2084
2085/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002086 * Close popup "wp" and invoke any close callback for it.
2087 */
2088 static void
2089popup_close_and_callback(win_T *wp, typval_T *arg)
2090{
2091 int id = wp->w_id;
2092
2093 if (wp->w_close_cb.cb_name != NULL)
2094 // Careful: This may make "wp" invalid.
2095 invoke_popup_callback(wp, arg);
2096
2097 popup_close(id);
2098}
2099
Bram Moolenaar12034e22019-08-25 22:25:02 +02002100 static void
2101popup_close_with_retval(win_T *wp, int retval)
2102{
2103 typval_T res;
2104
2105 res.v_type = VAR_NUMBER;
2106 res.vval.v_number = retval;
2107 popup_close_and_callback(wp, &res);
2108}
2109
Bram Moolenaar3397f742019-06-02 18:40:06 +02002110/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002111 * Close popup "wp" because of a mouse click.
2112 */
2113 void
2114popup_close_for_mouse_click(win_T *wp)
2115{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002116 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002117}
2118
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002119 static void
2120check_mouse_moved(win_T *wp, win_T *mouse_wp)
2121{
2122 // Close the popup when all if these are true:
2123 // - the mouse is not on this popup
2124 // - "mousemoved" was used
2125 // - the mouse is no longer on the same screen row or the mouse column is
2126 // outside of the relevant text
2127 if (wp != mouse_wp
2128 && wp->w_popup_mouse_row != 0
2129 && (wp->w_popup_mouse_row != mouse_row
2130 || mouse_col < wp->w_popup_mouse_mincol
2131 || mouse_col > wp->w_popup_mouse_maxcol))
2132 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002133 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002134 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002135 }
2136}
2137
2138/*
2139 * Called when the mouse moved: may close a popup with "mousemoved".
2140 */
2141 void
2142popup_handle_mouse_moved(void)
2143{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002144 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002145 win_T *mouse_wp;
2146 int row = mouse_row;
2147 int col = mouse_col;
2148
2149 // find the window where the mouse is in
2150 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2151
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002152 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2153 {
2154 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002155 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002156 }
2157 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2158 {
2159 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002160 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002161 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002162}
2163
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002164/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002165 * In a filter: check if the typed key is a mouse event that is used for
2166 * dragging the popup.
2167 */
2168 static void
2169filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2170{
2171 int row = mouse_row;
2172 int col = mouse_col;
2173
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002174 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002175 && is_mouse_key(c)
2176 && (wp == popup_dragwin
2177 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2178 // do not consume the key, allow for dragging the popup
2179 rettv->vval.v_number = 0;
2180}
2181
Bram Moolenaara730e552019-06-16 19:05:31 +02002182/*
2183 * popup_filter_menu({text}, {options})
2184 */
2185 void
2186f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2187{
2188 int id = tv_get_number(&argvars[0]);
2189 win_T *wp = win_id2wp(id);
2190 char_u *key = tv_get_string(&argvars[1]);
2191 typval_T res;
2192 int c;
2193 linenr_T old_lnum;
2194
2195 // If the popup has been closed do not consume the key.
2196 if (wp == NULL)
2197 return;
2198
2199 c = *key;
2200 if (c == K_SPECIAL && key[1] != NUL)
2201 c = TO_SPECIAL(key[1], key[2]);
2202
2203 // consume all keys until done
2204 rettv->vval.v_number = 1;
2205 res.v_type = VAR_NUMBER;
2206
2207 old_lnum = wp->w_cursor.lnum;
2208 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2209 --wp->w_cursor.lnum;
2210 if ((c == 'j' || c == 'J' || c == K_DOWN)
2211 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2212 ++wp->w_cursor.lnum;
2213 if (old_lnum != wp->w_cursor.lnum)
2214 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002215 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002216 return;
2217 }
2218
2219 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2220 {
2221 // Cancelled, invoke callback with -1
2222 res.vval.v_number = -1;
2223 popup_close_and_callback(wp, &res);
2224 return;
2225 }
2226 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2227 {
2228 // Invoke callback with current index.
2229 res.vval.v_number = wp->w_cursor.lnum;
2230 popup_close_and_callback(wp, &res);
2231 return;
2232 }
2233
2234 filter_handle_drag(wp, c, rettv);
2235}
2236
2237/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002238 * popup_filter_yesno({text}, {options})
2239 */
2240 void
2241f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2242{
2243 int id = tv_get_number(&argvars[0]);
2244 win_T *wp = win_id2wp(id);
2245 char_u *key = tv_get_string(&argvars[1]);
2246 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002247 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002248
2249 // If the popup has been closed don't consume the key.
2250 if (wp == NULL)
2251 return;
2252
Bram Moolenaara730e552019-06-16 19:05:31 +02002253 c = *key;
2254 if (c == K_SPECIAL && key[1] != NUL)
2255 c = TO_SPECIAL(key[1], key[2]);
2256
Bram Moolenaara42d9452019-06-15 21:46:30 +02002257 // consume all keys until done
2258 rettv->vval.v_number = 1;
2259
Bram Moolenaara730e552019-06-16 19:05:31 +02002260 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002261 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002262 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002263 res.vval.v_number = 0;
2264 else
2265 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002266 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002267 return;
2268 }
2269
2270 // Invoke callback
2271 res.v_type = VAR_NUMBER;
2272 popup_close_and_callback(wp, &res);
2273}
2274
2275/*
2276 * popup_dialog({text}, {options})
2277 */
2278 void
2279f_popup_dialog(typval_T *argvars, typval_T *rettv)
2280{
2281 popup_create(argvars, rettv, TYPE_DIALOG);
2282}
2283
2284/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002285 * popup_menu({text}, {options})
2286 */
2287 void
2288f_popup_menu(typval_T *argvars, typval_T *rettv)
2289{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002290 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002291}
2292
2293/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002294 * popup_notification({text}, {options})
2295 */
2296 void
2297f_popup_notification(typval_T *argvars, typval_T *rettv)
2298{
2299 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2300}
2301
2302/*
2303 * Find the popup window with window-ID "id".
2304 * If the popup window does not exist NULL is returned.
2305 * If the window is not a popup window, and error message is given.
2306 */
2307 static win_T *
2308find_popup_win(int id)
2309{
2310 win_T *wp = win_id2wp(id);
2311
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002312 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002313 {
2314 semsg(_("E993: window %d is not a popup window"), id);
2315 return NULL;
2316 }
2317 return wp;
2318}
2319
2320/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002321 * popup_close({id})
2322 */
2323 void
2324f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2325{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002326 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002327 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002328
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002329 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002330 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002331}
2332
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002333 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002334popup_hide(win_T *wp)
2335{
2336 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2337 {
2338 wp->w_popup_flags |= POPF_HIDDEN;
2339 --wp->w_buffer->b_nwindows;
2340 redraw_all_later(NOT_VALID);
2341 popup_mask_refresh = TRUE;
2342 }
2343}
2344
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002345/*
2346 * popup_hide({id})
2347 */
2348 void
2349f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2350{
2351 int id = (int)tv_get_number(argvars);
2352 win_T *wp = find_popup_win(id);
2353
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002354 if (wp != NULL)
2355 popup_hide(wp);
2356}
2357
2358 void
2359popup_show(win_T *wp)
2360{
2361 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002362 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002363 wp->w_popup_flags &= ~POPF_HIDDEN;
2364 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002365 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002366 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002367 }
2368}
2369
2370/*
2371 * popup_show({id})
2372 */
2373 void
2374f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2375{
2376 int id = (int)tv_get_number(argvars);
2377 win_T *wp = find_popup_win(id);
2378
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002379 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002380 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002381 popup_show(wp);
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002382 if (wp->w_popup_flags & POPF_INFO)
2383 pum_position_info_popup(wp);
2384 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002385}
2386
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002387/*
2388 * popup_settext({id}, {text})
2389 */
2390 void
2391f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2392{
2393 int id = (int)tv_get_number(&argvars[0]);
2394 win_T *wp = find_popup_win(id);
2395
2396 if (wp != NULL)
2397 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002398 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2399 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2400 else
2401 {
2402 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002403 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002404 popup_adjust_position(wp);
2405 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002406 }
2407}
2408
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002409 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002410popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002411{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002412 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002413 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002414 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002415 clear_cmdline = TRUE;
2416 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002417
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002418 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002419 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002420}
2421
Bram Moolenaarec583842019-05-26 14:11:23 +02002422/*
2423 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002424 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002425 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002426 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002427popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002428{
2429 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002430 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002431 win_T *prev = NULL;
2432
Bram Moolenaarec583842019-05-26 14:11:23 +02002433 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002434 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002435 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002436 {
2437 if (prev == NULL)
2438 first_popupwin = wp->w_next;
2439 else
2440 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002441 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002442 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002443 }
2444
Bram Moolenaarec583842019-05-26 14:11:23 +02002445 // go through tab-local popups
2446 FOR_ALL_TABPAGES(tp)
2447 popup_close_tabpage(tp, id);
2448}
2449
2450/*
2451 * Close a popup window with Window-id "id" in tabpage "tp".
2452 */
2453 void
2454popup_close_tabpage(tabpage_T *tp, int id)
2455{
2456 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002457 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002458 win_T *prev = NULL;
2459
Bram Moolenaarec583842019-05-26 14:11:23 +02002460 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2461 if (wp->w_id == id)
2462 {
2463 if (prev == NULL)
2464 *root = wp->w_next;
2465 else
2466 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002467 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002468 return;
2469 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002470}
2471
2472 void
2473close_all_popups(void)
2474{
2475 while (first_popupwin != NULL)
2476 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002477 while (curtab->tp_first_popupwin != NULL)
2478 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002479}
2480
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002481/*
2482 * popup_move({id}, {options})
2483 */
2484 void
2485f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2486{
Bram Moolenaarae943152019-06-16 22:54:14 +02002487 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002488 int id = (int)tv_get_number(argvars);
2489 win_T *wp = find_popup_win(id);
2490
2491 if (wp == NULL)
2492 return; // invalid {id}
2493
2494 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2495 {
2496 emsg(_(e_dictreq));
2497 return;
2498 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002499 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002500
Bram Moolenaarae943152019-06-16 22:54:14 +02002501 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002502
2503 if (wp->w_winrow + wp->w_height >= cmdline_row)
2504 clear_cmdline = TRUE;
2505 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002506}
2507
Bram Moolenaarbc133542019-05-29 20:26:46 +02002508/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002509 * popup_setoptions({id}, {options})
2510 */
2511 void
2512f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2513{
2514 dict_T *dict;
2515 int id = (int)tv_get_number(argvars);
2516 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002517 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002518
2519 if (wp == NULL)
2520 return; // invalid {id}
2521
2522 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2523 {
2524 emsg(_(e_dictreq));
2525 return;
2526 }
2527 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002528 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002529
2530 apply_move_options(wp, dict);
2531 apply_general_options(wp, dict);
2532
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002533 if (old_firstline != wp->w_firstline)
2534 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002535 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002536 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002537 popup_adjust_position(wp);
2538}
2539
2540/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002541 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002542 */
2543 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002544f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002545{
2546 dict_T *dict;
2547 int id = (int)tv_get_number(argvars);
2548 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002549 int top_extra;
2550 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002551
2552 if (rettv_dict_alloc(rettv) == OK)
2553 {
2554 if (wp == NULL)
2555 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002556 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002557 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2558
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002559 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002560 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002561 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002562
Bram Moolenaarbc133542019-05-29 20:26:46 +02002563 dict_add_number(dict, "line", wp->w_winrow + 1);
2564 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002565 dict_add_number(dict, "width", wp->w_width + left_extra
2566 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2567 dict_add_number(dict, "height", wp->w_height + top_extra
2568 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002569
2570 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2571 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2572 dict_add_number(dict, "core_width", wp->w_width);
2573 dict_add_number(dict, "core_height", wp->w_height);
2574
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002575 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002576 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002577 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002578 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002579 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002580
2581 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002582 }
2583}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002584/*
2585 * popup_locate({row}, {col})
2586 */
2587 void
2588f_popup_locate(typval_T *argvars, typval_T *rettv)
2589{
2590 int row = tv_get_number(&argvars[0]) - 1;
2591 int col = tv_get_number(&argvars[1]) - 1;
2592 win_T *wp;
2593
2594 wp = mouse_find_win(&row, &col, FIND_POPUP);
2595 if (WIN_IS_POPUP(wp))
2596 rettv->vval.v_number = wp->w_id;
2597}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002598
2599/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002600 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2601 */
2602 static void
2603get_padding_border(dict_T *dict, int *array, char *name)
2604{
2605 list_T *list;
2606 int i;
2607
2608 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2609 return;
2610
2611 list = list_alloc();
2612 if (list != NULL)
2613 {
2614 dict_add_list(dict, name, list);
2615 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2616 for (i = 0; i < 4; ++i)
2617 list_append_number(list, array[i]);
2618 }
2619}
2620
2621/*
2622 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2623 */
2624 static void
2625get_borderhighlight(dict_T *dict, win_T *wp)
2626{
2627 list_T *list;
2628 int i;
2629
2630 for (i = 0; i < 4; ++i)
2631 if (wp->w_border_highlight[i] != NULL)
2632 break;
2633 if (i == 4)
2634 return;
2635
2636 list = list_alloc();
2637 if (list != NULL)
2638 {
2639 dict_add_list(dict, "borderhighlight", list);
2640 for (i = 0; i < 4; ++i)
2641 list_append_string(list, wp->w_border_highlight[i], -1);
2642 }
2643}
2644
2645/*
2646 * For popup_getoptions(): add a "borderchars" entry to "dict".
2647 */
2648 static void
2649get_borderchars(dict_T *dict, win_T *wp)
2650{
2651 list_T *list;
2652 int i;
2653 char_u buf[NUMBUFLEN];
2654 int len;
2655
2656 for (i = 0; i < 8; ++i)
2657 if (wp->w_border_char[i] != 0)
2658 break;
2659 if (i == 8)
2660 return;
2661
2662 list = list_alloc();
2663 if (list != NULL)
2664 {
2665 dict_add_list(dict, "borderchars", list);
2666 for (i = 0; i < 8; ++i)
2667 {
2668 len = mb_char2bytes(wp->w_border_char[i], buf);
2669 list_append_string(list, buf, len);
2670 }
2671 }
2672}
2673
2674/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002675 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002676 */
2677 static void
2678get_moved_list(dict_T *dict, win_T *wp)
2679{
2680 list_T *list;
2681
2682 list = list_alloc();
2683 if (list != NULL)
2684 {
2685 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002686 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002687 list_append_number(list, wp->w_popup_mincol);
2688 list_append_number(list, wp->w_popup_maxcol);
2689 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002690 list = list_alloc();
2691 if (list != NULL)
2692 {
2693 dict_add_list(dict, "mousemoved", list);
2694 list_append_number(list, wp->w_popup_mouse_row);
2695 list_append_number(list, wp->w_popup_mouse_mincol);
2696 list_append_number(list, wp->w_popup_mouse_maxcol);
2697 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002698}
2699
2700/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002701 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002702 */
2703 void
2704f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2705{
2706 dict_T *dict;
2707 int id = (int)tv_get_number(argvars);
2708 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002709 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002710 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002711
2712 if (rettv_dict_alloc(rettv) == OK)
2713 {
2714 if (wp == NULL)
2715 return;
2716
2717 dict = rettv->vval.v_dict;
2718 dict_add_number(dict, "line", wp->w_wantline);
2719 dict_add_number(dict, "col", wp->w_wantcol);
2720 dict_add_number(dict, "minwidth", wp->w_minwidth);
2721 dict_add_number(dict, "minheight", wp->w_minheight);
2722 dict_add_number(dict, "maxheight", wp->w_maxheight);
2723 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002724 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002725 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002726 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002727 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002728 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2729 {
2730 proptype_T *pt = text_prop_type_by_id(
2731 wp->w_popup_prop_win->w_buffer,
2732 wp->w_popup_prop_type);
2733
2734 if (pt != NULL)
2735 dict_add_string(dict, "textprop", pt->pt_name);
2736 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2737 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2738 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002739 dict_add_string(dict, "title", wp->w_popup_title);
2740 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002741 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002742 dict_add_number(dict, "mapping",
2743 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002744 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002745 dict_add_number(dict, "posinvert",
2746 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002747 dict_add_number(dict, "cursorline",
2748 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002749 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002750 if (wp->w_scrollbar_highlight != NULL)
2751 dict_add_string(dict, "scrollbarhighlight",
2752 wp->w_scrollbar_highlight);
2753 if (wp->w_thumb_highlight != NULL)
2754 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002755
Bram Moolenaara3fce622019-06-20 02:31:49 +02002756 // find the tabpage that holds this popup
2757 i = 1;
2758 FOR_ALL_TABPAGES(tp)
2759 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002760 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002761
Bram Moolenaar1824f452019-10-02 23:06:46 +02002762 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2763 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002764 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002765 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002766 break;
2767 ++i;
2768 }
2769 if (tp == NULL)
2770 i = -1; // must be global
2771 else if (tp == curtab)
2772 i = 0;
2773 dict_add_number(dict, "tabpage", i);
2774
Bram Moolenaarae943152019-06-16 22:54:14 +02002775 get_padding_border(dict, wp->w_popup_padding, "padding");
2776 get_padding_border(dict, wp->w_popup_border, "border");
2777 get_borderhighlight(dict, wp);
2778 get_borderchars(dict, wp);
2779 get_moved_list(dict, wp);
2780
2781 if (wp->w_filter_cb.cb_name != NULL)
2782 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2783 if (wp->w_close_cb.cb_name != NULL)
2784 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002785
2786 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2787 ++i)
2788 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2789 {
2790 dict_add_string(dict, "pos",
2791 (char_u *)poppos_entries[i].pp_name);
2792 break;
2793 }
2794
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002795 dict_add_string(dict, "close", (char_u *)(
2796 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2797 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2798
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002799# if defined(FEAT_TIMERS)
2800 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2801 ? (long)wp->w_popup_timer->tr_interval : 0L);
2802# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002803 }
2804}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002805
2806 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002807error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002808{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002809 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002810 {
2811 emsg(_("E994: Not allowed in a popup window"));
2812 return TRUE;
2813 }
2814 return FALSE;
2815}
2816
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002817/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002818 * Reset all the "handled_flag" flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002819 * in the current tab page.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002820 * Each calling function should use a different flag, see the list at
2821 * POPUP_HANDLED_1. This won't work with recursive calls though.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002822 */
2823 void
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002824popup_reset_handled(int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002825{
2826 win_T *wp;
2827
2828 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002829 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002830 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002831 wp->w_popup_handled &= ~handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002832}
2833
2834/*
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002835 * Find the next visible popup where "handled_flag" is not set.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002836 * Must have called popup_reset_handled() first.
2837 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2838 * popup with the highest zindex.
2839 */
2840 win_T *
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002841find_next_popup(int lowest, int handled_flag)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002842{
2843 win_T *wp;
2844 win_T *found_wp;
2845 int found_zindex;
2846
2847 found_zindex = lowest ? INT_MAX : 0;
2848 found_wp = NULL;
2849 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002850 if ((wp->w_popup_handled & handled_flag) == 0
2851 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002852 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002853 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002854 {
2855 found_zindex = wp->w_zindex;
2856 found_wp = wp;
2857 }
2858 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002859 if ((wp->w_popup_handled & handled_flag) == 0
2860 && (wp->w_popup_flags & POPF_HIDDEN) == 0
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002861 && (lowest ? wp->w_zindex < found_zindex
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002862 : wp->w_zindex > found_zindex))
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002863 {
2864 found_zindex = wp->w_zindex;
2865 found_wp = wp;
2866 }
2867
2868 if (found_wp != NULL)
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002869 found_wp->w_popup_handled |= handled_flag;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002870 return found_wp;
2871}
2872
2873/*
2874 * Invoke the filter callback for window "wp" with typed character "c".
2875 * Uses the global "mod_mask" for modifiers.
2876 * Returns the return value of the filter.
2877 * Careful: The filter may make "wp" invalid!
2878 */
2879 static int
2880invoke_popup_filter(win_T *wp, int c)
2881{
2882 int res;
2883 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002884 typval_T argv[3];
2885 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002886 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002887
Bram Moolenaara42d9452019-06-15 21:46:30 +02002888 // Emergency exit: CTRL-C closes the popup.
2889 if (c == Ctrl_C)
2890 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002891 int save_got_int = got_int;
2892
2893 // Reset got_int to avoid the callback isn't called.
2894 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002895 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002896 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002897 return 1;
2898 }
2899
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002900 argv[0].v_type = VAR_NUMBER;
2901 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2902
2903 // Convert the number to a string, so that the function can use:
2904 // if a:c == "\<F2>"
2905 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2906 argv[1].v_type = VAR_STRING;
2907 argv[1].vval.v_string = vim_strsave(buf);
2908
2909 argv[2].v_type = VAR_UNKNOWN;
2910
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002911 if (is_mouse_key(c))
2912 {
2913 int row = mouse_row - wp->w_winrow;
2914 int col = mouse_col - wp->w_wincol;
2915 linenr_T lnum;
2916
2917 if (row >= 0 && col >= 0)
2918 {
2919 (void)mouse_comp_pos(wp, &row, &col, &lnum, NULL);
2920 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2921 set_vim_var_nr(VV_MOUSE_COL, col + 1);
Bram Moolenaard002e412019-11-11 21:45:05 +01002922 set_vim_var_nr(VV_MOUSE_WINID, wp->w_id);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002923 }
2924 }
2925
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002926 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002927 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002928 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002929 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002930 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002931
2932 if (is_mouse_key(c))
2933 {
2934 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2935 set_vim_var_nr(VV_MOUSE_COL, 0);
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002936 set_vim_var_nr(VV_MOUSE_WINID, wp->w_id);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002937 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002938 vim_free(argv[1].vval.v_string);
2939 clear_tv(&rettv);
2940 return res;
2941}
2942
2943/*
2944 * Called when "c" was typed: invoke popup filter callbacks.
2945 * Returns TRUE when the character was consumed,
2946 */
2947 int
2948popup_do_filter(int c)
2949{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002950 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002951 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002952 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002953 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002954 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002955 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002956
2957 if (recursive)
2958 return FALSE;
2959 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002960
Bram Moolenaarf6396232019-08-24 19:36:00 +02002961 if (c == K_LEFTMOUSE)
2962 {
2963 int row = mouse_row;
2964 int col = mouse_col;
2965
2966 wp = mouse_find_win(&row, &col, FIND_POPUP);
2967 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002968 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002969 }
2970
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002971 popup_reset_handled(POPUP_HANDLED_2);
Bram Moolenaar581ba392019-09-03 22:08:33 +02002972 state = get_real_state();
Bram Moolenaarafe45b62019-11-13 22:35:19 +01002973 while (!res && (wp = find_next_popup(FALSE, POPUP_HANDLED_2)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002974 if (wp->w_filter_cb.cb_name != NULL
2975 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002976 res = invoke_popup_filter(wp, c);
2977
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002978 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002979 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002980 recursive = FALSE;
2981 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002982 return res;
2983}
2984
Bram Moolenaar3397f742019-06-02 18:40:06 +02002985/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002986 * Return TRUE if there is a popup visible with a filter callback and the
2987 * "mapping" property off.
2988 */
2989 int
2990popup_no_mapping(void)
2991{
2992 int round;
2993 win_T *wp;
2994
2995 for (round = 1; round <= 2; ++round)
2996 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2997 wp != NULL; wp = wp->w_next)
2998 if (wp->w_filter_cb.cb_name != NULL
2999 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
3000 return TRUE;
3001 return FALSE;
3002}
3003
3004/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02003005 * Called when the cursor moved: check if any popup needs to be closed if the
3006 * cursor moved far enough.
3007 */
3008 void
3009popup_check_cursor_pos()
3010{
3011 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02003012
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003013 popup_reset_handled(POPUP_HANDLED_3);
3014 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_3)) != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02003015 if (wp->w_popup_curwin != NULL
3016 && (curwin != wp->w_popup_curwin
3017 || curwin->w_cursor.lnum != wp->w_popup_lnum
3018 || curwin->w_cursor.col < wp->w_popup_mincol
3019 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02003020 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02003021}
3022
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003023/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003024 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003025 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003026 static void
3027popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003028{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003029 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003030 char_u *cells;
3031 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003032
3033 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003034 return;
3035 if (wp->w_popup_mask_cells != NULL
3036 && wp->w_popup_mask_height == height
3037 && wp->w_popup_mask_width == width)
3038 return; // cache is still valid
3039
3040 vim_free(wp->w_popup_mask_cells);
3041 wp->w_popup_mask_cells = alloc_clear(width * height);
3042 if (wp->w_popup_mask_cells == NULL)
3043 return;
3044 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003045
3046 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3047 {
3048 int cols, cole;
3049 int lines, linee;
3050
3051 li = lio->li_tv.vval.v_list->lv_first;
3052 cols = tv_get_number(&li->li_tv);
3053 if (cols < 0)
3054 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003055 li = li->li_next;
3056 cole = tv_get_number(&li->li_tv);
3057 if (cole < 0)
3058 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003059 li = li->li_next;
3060 lines = tv_get_number(&li->li_tv);
3061 if (lines < 0)
3062 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003063 li = li->li_next;
3064 linee = tv_get_number(&li->li_tv);
3065 if (linee < 0)
3066 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003067
3068 for (row = lines - 1; row < linee && row < height; ++row)
3069 for (col = cols - 1; col < cole && col < width; ++col)
3070 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003071 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003072}
3073
3074/*
3075 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3076 * "col" and "line" are screen coordinates.
3077 */
3078 static int
3079popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3080{
3081 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3082 int line = screenline - wp->w_winrow;
3083
3084 return col >= 0 && col < width
3085 && line >= 0 && line < height
3086 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003087}
3088
3089/*
3090 * Set flags in popup_transparent[] for window "wp" to "val".
3091 */
3092 static void
3093update_popup_transparent(win_T *wp, int val)
3094{
3095 if (wp->w_popup_mask != NULL)
3096 {
3097 int width = popup_width(wp);
3098 int height = popup_height(wp);
3099 listitem_T *lio, *li;
3100 int cols, cole;
3101 int lines, linee;
3102 int col, line;
3103
3104 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3105 {
3106 li = lio->li_tv.vval.v_list->lv_first;
3107 cols = tv_get_number(&li->li_tv);
3108 if (cols < 0)
3109 cols = width + cols + 1;
3110 li = li->li_next;
3111 cole = tv_get_number(&li->li_tv);
3112 if (cole < 0)
3113 cole = width + cole + 1;
3114 li = li->li_next;
3115 lines = tv_get_number(&li->li_tv);
3116 if (lines < 0)
3117 lines = height + lines + 1;
3118 li = li->li_next;
3119 linee = tv_get_number(&li->li_tv);
3120 if (linee < 0)
3121 linee = height + linee + 1;
3122
3123 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003124 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003125 if (cols < 0)
3126 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003127 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003128 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003129 if (lines < 0)
3130 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003131 for (line = lines; line < linee
3132 && line + wp->w_winrow < screen_Rows; ++line)
3133 for (col = cols; col < cole
3134 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003135 popup_transparent[(line + wp->w_winrow) * screen_Columns
3136 + col + wp->w_wincol] = val;
3137 }
3138 }
3139}
3140
3141/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003142 * Only called when popup window "wp" is hidden: If the window is positioned
3143 * next to a text property, and it is now visible, then unhide the popup.
3144 * We don't check if visible popups become hidden, that is done in
3145 * popup_adjust_position().
3146 * Return TRUE if the popup became unhidden.
3147 */
3148 static int
3149check_popup_unhidden(win_T *wp)
3150{
3151 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3152 {
3153 textprop_T prop;
3154 linenr_T lnum;
3155
3156 if (find_visible_prop(wp->w_popup_prop_win,
3157 wp->w_popup_prop_type, wp->w_popup_prop_id,
3158 &prop, &lnum) == OK)
3159 {
3160 wp->w_popup_flags &= ~POPF_HIDDEN;
3161 ++wp->w_buffer->b_nwindows;
3162 wp->w_popup_prop_topline = 0; // force repositioning
3163 return TRUE;
3164 }
3165 }
3166 return FALSE;
3167}
3168
3169/*
3170 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3171 * That is when the buffer in the popup was changed, or the popup is following
3172 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003173 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003174 */
3175 static int
3176popup_need_position_adjust(win_T *wp)
3177{
3178 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3179 return TRUE;
3180 if (win_valid(wp->w_popup_prop_win))
3181 return wp->w_popup_prop_changedtick
3182 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003183 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3184 || ((wp->w_popup_flags & POPF_CURSORLINE)
3185 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003186 return FALSE;
3187}
3188
3189/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003190 * Update "popup_mask" if needed.
3191 * Also recomputes the popup size and positions.
3192 * Also updates "popup_visible".
3193 * Also marks window lines for redrawing.
3194 */
3195 void
3196may_update_popup_mask(int type)
3197{
3198 win_T *wp;
3199 short *mask;
3200 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003201 int redraw_all_popups = FALSE;
3202 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003203
3204 // Need to recompute when switching tabs.
3205 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3206 // (such as the screen size) must have changed.
3207 if (popup_mask_tab != curtab || type >= NOT_VALID)
3208 {
3209 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003210 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003211 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003212
3213 // Check if any popup window buffer has changed and if any popup connected
3214 // to a text property has become visible.
3215 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3216 if (wp->w_popup_flags & POPF_HIDDEN)
3217 popup_mask_refresh |= check_popup_unhidden(wp);
3218 else if (popup_need_position_adjust(wp))
3219 popup_mask_refresh = TRUE;
3220 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3221 if (wp->w_popup_flags & POPF_HIDDEN)
3222 popup_mask_refresh |= check_popup_unhidden(wp);
3223 else if (popup_need_position_adjust(wp))
3224 popup_mask_refresh = TRUE;
3225
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003226 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003227 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003228
3229 // Need to update the mask, something has changed.
3230 popup_mask_refresh = FALSE;
3231 popup_mask_tab = curtab;
3232 popup_visible = FALSE;
3233
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003234 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003235 // If redrawing only what is needed, update "popup_mask_next" and then
3236 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003237 redrawing_all_win = TRUE;
3238 FOR_ALL_WINDOWS(wp)
3239 if (wp->w_redr_type < SOME_VALID)
3240 redrawing_all_win = FALSE;
3241 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003242 mask = popup_mask;
3243 else
3244 mask = popup_mask_next;
3245 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3246
3247 // Find the window with the lowest zindex that hasn't been handled yet,
3248 // so that the window with a higher zindex overwrites the value in
3249 // popup_mask.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003250 popup_reset_handled(POPUP_HANDLED_4);
3251 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_4)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003252 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003253 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003254 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003255
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003256 popup_visible = TRUE;
3257
Bram Moolenaar12034e22019-08-25 22:25:02 +02003258 // Recompute the position if the text changed. It may make the popup
3259 // hidden if it's attach to a text property that is no longer visible.
3260 if (redraw_all_popups || popup_need_position_adjust(wp))
3261 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003262 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003263 if (wp->w_popup_flags & POPF_HIDDEN)
3264 continue;
3265 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003266
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003267 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003268 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003269 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003270 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003271 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003272 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003273 col < wp->w_wincol + width - wp->w_popup_leftoff
3274 && col < screen_Columns; ++col)
3275 if (wp->w_popup_mask_cells == NULL
3276 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003277 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003278 }
3279
3280 // Only check which lines are to be updated if not already
3281 // updating all lines.
3282 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003283 {
3284 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3285 win_T *prev_wp = NULL;
3286
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003287 for (line = 0; line < screen_Rows; ++line)
3288 {
3289 int col_done = 0;
3290
3291 for (col = 0; col < screen_Columns; ++col)
3292 {
3293 int off = line * screen_Columns + col;
3294
3295 if (popup_mask[off] != popup_mask_next[off])
3296 {
3297 popup_mask[off] = popup_mask_next[off];
3298
3299 if (line >= cmdline_row)
3300 {
3301 // the command line needs to be cleared if text below
3302 // the popup is now visible.
3303 if (!msg_scrolled && popup_mask_next[off] == 0)
3304 clear_cmdline = TRUE;
3305 }
3306 else if (col >= col_done)
3307 {
3308 linenr_T lnum;
3309 int line_cp = line;
3310 int col_cp = col;
3311
3312 // The screen position "line" / "col" needs to be
3313 // redrawn. Figure out what window that is and update
3314 // w_redraw_top and w_redr_bot. Only needs to be done
3315 // once for each window line.
3316 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3317 if (wp != NULL)
3318 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003319 if (wp != prev_wp)
3320 {
3321 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3322 prev_wp = wp;
3323 }
3324
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003325 if (line_cp >= wp->w_height)
3326 // In (or below) status line
3327 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003328 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003329 {
3330 // compute the position in the buffer line from
3331 // the position in the window
3332 mouse_comp_pos(wp, &line_cp, &col_cp,
3333 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003334 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003335 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003336
3337 // This line is going to be redrawn, no need to
3338 // check until the right side of the window.
3339 col_done = wp->w_wincol + wp->w_width - 1;
3340 }
3341 }
3342 }
3343 }
3344 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003345
3346 vim_free(plines_cache);
3347 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003348}
3349
3350/*
3351 * Return a string of "len" spaces in IObuff.
3352 */
3353 static char_u *
3354get_spaces(int len)
3355{
3356 vim_memset(IObuff, ' ', (size_t)len);
3357 IObuff[len] = NUL;
3358 return IObuff;
3359}
3360
3361/*
3362 * Update popup windows. They are drawn on top of normal windows.
3363 * "win_update" is called for each popup window, lowest zindex first.
3364 */
3365 void
3366update_popups(void (*win_update)(win_T *wp))
3367{
3368 win_T *wp;
3369 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003370 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003371 int total_width;
3372 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003373 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003374 int popup_attr;
3375 int border_attr[4];
3376 int border_char[8];
3377 char_u buf[MB_MAXBYTES];
3378 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003379 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003380 int padcol = 0;
3381 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003382 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003383 int sb_thumb_top = 0;
3384 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003385 int attr_scroll = 0;
3386 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003387
3388 // Find the window with the lowest zindex that hasn't been updated yet,
3389 // so that the window with a higher zindex is drawn later, thus goes on
3390 // top.
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003391 popup_reset_handled(POPUP_HANDLED_5);
3392 while ((wp = find_next_popup(TRUE, POPUP_HANDLED_5)) != NULL)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003393 {
3394 // This drawing uses the zindex of the popup window, so that it's on
3395 // top of the text but doesn't draw when another popup with higher
3396 // zindex is on top of the character.
3397 screen_zindex = wp->w_zindex;
3398
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003399 // Set flags in popup_transparent[] for masked cells.
3400 update_popup_transparent(wp, 1);
3401
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003402 // adjust w_winrow and w_wincol for border and padding, since
3403 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003404 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003405 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3406 - wp->w_popup_leftoff;
3407 if (wp->w_wincol + left_extra < 0)
3408 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003409 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003410 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003411
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003412 // Draw the popup text, unless it's off screen.
3413 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003414 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003415 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003416
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003417 // move the cursor into the visible lines, otherwise executing
3418 // commands with win_execute() may cause the text to jump.
3419 if (wp->w_cursor.lnum < wp->w_topline)
3420 wp->w_cursor.lnum = wp->w_topline;
3421 else if (wp->w_cursor.lnum >= wp->w_botline)
3422 wp->w_cursor.lnum = wp->w_botline - 1;
3423 }
3424
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003425 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003426 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003427
Bram Moolenaard529ba52019-07-02 23:13:53 +02003428 total_width = popup_width(wp);
3429 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003430 popup_attr = get_wcr_attr(wp);
3431
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003432 if (wp->w_winrow + total_height > cmdline_row)
3433 wp->w_popup_flags |= POPF_ON_CMDLINE;
3434 else
3435 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3436
3437
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003438 // We can only use these line drawing characters when 'encoding' is
3439 // "utf-8" and 'ambiwidth' is "single".
3440 if (enc_utf8 && *p_ambw == 's')
3441 {
3442 border_char[0] = border_char[2] = 0x2550;
3443 border_char[1] = border_char[3] = 0x2551;
3444 border_char[4] = 0x2554;
3445 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003446 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3447 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003448 border_char[7] = 0x255a;
3449 }
3450 else
3451 {
3452 border_char[0] = border_char[2] = '-';
3453 border_char[1] = border_char[3] = '|';
3454 for (i = 4; i < 8; ++i)
3455 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003456 if (wp->w_popup_flags & POPF_RESIZE)
3457 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003458 }
3459 for (i = 0; i < 8; ++i)
3460 if (wp->w_border_char[i] != 0)
3461 border_char[i] = wp->w_border_char[i];
3462
3463 for (i = 0; i < 4; ++i)
3464 {
3465 border_attr[i] = popup_attr;
3466 if (wp->w_border_highlight[i] != NULL)
3467 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3468 }
3469
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003470 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003471 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003472 if (wp->w_popup_border[0] > 0)
3473 {
3474 // top border
3475 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003476 wincol < 0 ? 0 : wincol, wincol + total_width,
3477 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003478 ? border_char[4] : border_char[0],
3479 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003480 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003481 {
3482 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3483 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003484 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003485 }
3486 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003487 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3488 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003489
Bram Moolenaard529ba52019-07-02 23:13:53 +02003490 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3491 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003492 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003493 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3494 - wp->w_has_scrollbar;
3495 if (padcol < 0)
3496 {
3497 padwidth += padcol;
3498 padcol = 0;
3499 }
3500 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003501 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003502 {
3503 // top padding
3504 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003505 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003506 ' ', ' ', popup_attr);
3507 }
3508
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003509 // Title goes on top of border or padding.
3510 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003511 {
3512 int len = (int)STRLEN(wp->w_popup_title) + 1;
3513 char_u *title = alloc(len);
3514
3515 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3516 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003517 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003518 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003519 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003520
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003521 // Compute scrollbar thumb position and size.
3522 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003523 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003524 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3525 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003526
Bram Moolenaar076d9882019-09-14 22:23:29 +02003527 sb_thumb_height = (height * height + linecount / 2) / linecount;
3528 if (wp->w_topline > 1 && sb_thumb_height == height)
3529 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003530 if (sb_thumb_height == 0)
3531 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003532 if (linecount <= wp->w_height)
3533 // it just fits, avoid divide by zero
3534 sb_thumb_top = 0;
3535 else
3536 sb_thumb_top = (wp->w_topline - 1
3537 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003538 * (wp->w_height - sb_thumb_height)
3539 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003540 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3541 sb_thumb_top = 1; // show it's scrolled
3542
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003543 if (wp->w_scrollbar_highlight != NULL)
3544 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3545 else
3546 attr_scroll = highlight_attr[HLF_PSB];
3547 if (wp->w_thumb_highlight != NULL)
3548 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3549 else
3550 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003551 }
3552
3553 for (i = wp->w_popup_border[0];
3554 i < total_height - wp->w_popup_border[2]; ++i)
3555 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003556 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003557 // left and right padding only needed next to the body
3558 int do_padding =
3559 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3560 && i < total_height - wp->w_popup_border[2]
3561 - wp->w_popup_padding[2];
3562
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003563 row = wp->w_winrow + i;
3564
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003565 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003566 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003567 {
3568 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003569 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003570 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003571 if (do_padding && wp->w_popup_padding[3] > 0)
3572 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003573 int col = wincol + wp->w_popup_border[3];
3574
Bram Moolenaard529ba52019-07-02 23:13:53 +02003575 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003576 pad_left = wp->w_popup_padding[3];
3577 if (col < 0)
3578 {
3579 pad_left += col;
3580 col = 0;
3581 }
3582 if (pad_left > 0)
3583 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3584 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003585 // scrollbar
3586 if (wp->w_has_scrollbar)
3587 {
3588 int line = i - top_off;
3589 int scroll_col = wp->w_wincol + total_width - 1
3590 - wp->w_popup_border[1];
3591
3592 if (line >= 0 && line < wp->w_height)
3593 screen_putchar(' ', row, scroll_col,
3594 line >= sb_thumb_top
3595 && line < sb_thumb_top + sb_thumb_height
3596 ? attr_thumb : attr_scroll);
3597 else
3598 screen_putchar(' ', row, scroll_col, popup_attr);
3599 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003600 // right border
3601 if (wp->w_popup_border[1] > 0)
3602 {
3603 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003604 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003605 }
3606 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003607 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003608 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003609 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003610 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3611 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003612 }
3613
3614 if (wp->w_popup_padding[2] > 0)
3615 {
3616 // bottom padding
3617 row = wp->w_winrow + wp->w_popup_border[0]
3618 + wp->w_popup_padding[0] + wp->w_height;
3619 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003620 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003621 }
3622
3623 if (wp->w_popup_border[2] > 0)
3624 {
3625 // bottom border
3626 row = wp->w_winrow + total_height - 1;
3627 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003628 wincol < 0 ? 0 : wincol,
3629 wincol + total_width,
3630 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003631 ? border_char[7] : border_char[2],
3632 border_char[2], border_attr[2]);
3633 if (wp->w_popup_border[1] > 0)
3634 {
3635 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003636 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003637 }
3638 }
3639
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003640 if (wp->w_popup_close == POPCLOSE_BUTTON)
3641 {
3642 // close button goes on top of anything at the top-right corner
3643 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003644 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003645 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3646 }
3647
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003648 update_popup_transparent(wp, 0);
3649
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003650 // Back to the normal zindex.
3651 screen_zindex = 0;
3652 }
3653}
3654
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003655/*
3656 * Mark references in callbacks of one popup window.
3657 */
3658 static int
3659set_ref_in_one_popup(win_T *wp, int copyID)
3660{
3661 int abort = FALSE;
3662 typval_T tv;
3663
3664 if (wp->w_close_cb.cb_partial != NULL)
3665 {
3666 tv.v_type = VAR_PARTIAL;
3667 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3668 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3669 }
3670 if (wp->w_filter_cb.cb_partial != NULL)
3671 {
3672 tv.v_type = VAR_PARTIAL;
3673 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3674 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3675 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003676 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003677 return abort;
3678}
3679
3680/*
3681 * Set reference in callbacks of popup windows.
3682 */
3683 int
3684set_ref_in_popups(int copyID)
3685{
3686 int abort = FALSE;
3687 win_T *wp;
3688 tabpage_T *tp;
3689
3690 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3691 abort = abort || set_ref_in_one_popup(wp, copyID);
3692
3693 FOR_ALL_TABPAGES(tp)
3694 {
3695 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3696 abort = abort || set_ref_in_one_popup(wp, copyID);
3697 if (abort)
3698 break;
3699 }
3700 return abort;
3701}
Bram Moolenaar79648732019-07-18 21:43:07 +02003702
3703/*
3704 * Find an existing popup used as the preview window, in the current tab page.
3705 * Return NULL if not found.
3706 */
3707 win_T *
3708popup_find_preview_window(void)
3709{
3710 win_T *wp;
3711
3712 // Preview window popup is always local to tab page.
3713 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3714 if (wp->w_p_pvw)
3715 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003716 return NULL;
3717}
3718
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003719 int
3720popup_is_popup(win_T *wp)
3721{
3722 return wp->w_popup_flags != 0;
3723}
3724
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003725#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003726/*
3727 * Find an existing popup used as the info window, in the current tab page.
3728 * Return NULL if not found.
3729 */
3730 win_T *
3731popup_find_info_window(void)
3732{
3733 win_T *wp;
3734
3735 // info window popup is always local to tab page.
3736 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3737 if (wp->w_popup_flags & POPF_INFO)
3738 return wp;
3739 return NULL;
3740}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003741#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003742
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003743 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003744f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3745{
3746 win_T *wp = popup_find_info_window();
3747
3748 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3749}
3750
3751 void
3752f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003753{
3754 win_T *wp = popup_find_preview_window();
3755
3756 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003757}
3758
Bram Moolenaar79648732019-07-18 21:43:07 +02003759/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003760 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003761 * NOTE: this makes the popup the current window, so that the file can be
3762 * edited. However, it must not remain to be the current window, the caller
3763 * must make sure of that.
3764 */
3765 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003766popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003767{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003768 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003769
3770 if (wp == NULL)
3771 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003772 if (info)
3773 wp->w_popup_flags |= POPF_INFO;
3774 else
3775 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003776
3777 // Set the width to a reasonable value, so that w_topline can be computed.
3778 if (wp->w_minwidth > 0)
3779 wp->w_width = wp->w_minwidth;
3780 else if (wp->w_maxwidth > 0)
3781 wp->w_width = wp->w_maxwidth;
3782 else
3783 wp->w_width = curwin->w_width;
3784
3785 // Will switch to another buffer soon, dummy one can be wiped.
3786 wp->w_buffer->b_locked = FALSE;
3787
3788 win_enter(wp, FALSE);
3789 return OK;
3790}
3791
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003792#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003793/*
3794 * Close any preview popup.
3795 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003796 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003797popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003798{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003799 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003800
3801 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003802 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003803}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003804
3805/*
3806 * Hide the info popup.
3807 */
3808 void
3809popup_hide_info(void)
3810{
3811 win_T *wp = popup_find_info_window();
3812
3813 if (wp != NULL)
3814 popup_hide(wp);
3815}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003816#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003817
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003818/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003819 * Close any popup for a text property associated with "win".
3820 * Return TRUE if a popup was closed.
3821 */
3822 int
3823popup_win_closed(win_T *win)
3824{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003825 int round;
3826 win_T *wp;
3827 win_T *next;
3828 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003829
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003830 for (round = 1; round <= 2; ++round)
3831 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3832 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003833 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003834 next = wp->w_next;
3835 if (wp->w_popup_prop_win == win)
3836 {
3837 popup_close_with_retval(wp, -1);
3838 ret = TRUE;
3839 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003840 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003841 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003842}
3843
3844/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003845 * Set the title of the popup window to the file name.
3846 */
3847 void
3848popup_set_title(win_T *wp)
3849{
3850 if (wp->w_buffer->b_fname != NULL)
3851 {
3852 char_u dirname[MAXPATHL];
3853 size_t len;
3854
3855 mch_dirname(dirname, MAXPATHL);
3856 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3857
3858 vim_free(wp->w_popup_title);
3859 len = STRLEN(wp->w_buffer->b_fname) + 3;
3860 wp->w_popup_title = alloc((int)len);
3861 if (wp->w_popup_title != NULL)
3862 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3863 wp->w_buffer->b_fname);
3864 redraw_win_later(wp, VALID);
3865 }
3866}
3867
3868/*
3869 * If there is a preview window, update the title.
3870 * Used after changing directory.
3871 */
3872 void
3873popup_update_preview_title(void)
3874{
3875 win_T *wp = popup_find_preview_window();
3876
3877 if (wp != NULL)
3878 popup_set_title(wp);
3879}
3880
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003881#endif // FEAT_TEXT_PROP