blob: a6e2e8726b2bfdb756a6aec47584a99987cac545 [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 Moolenaar17627312019-06-02 19:53:44 +0200393/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200394 * Shared between popup_create() and f_popup_move().
Bram Moolenaar17627312019-06-02 19:53:44 +0200395 */
396 static void
Bram Moolenaarae943152019-06-16 22:54:14 +0200397apply_move_options(win_T *wp, dict_T *d)
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398{
Bram Moolenaar12034e22019-08-25 22:25:02 +0200399 int nr;
400 char_u *str;
401 dictitem_T *di;
Bram Moolenaarae943152019-06-16 22:54:14 +0200402
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200403 if ((nr = dict_get_number_def(d, (char_u *)"minwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200404 wp->w_minwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200405 if ((nr = dict_get_number_def(d, (char_u *)"minheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200406 wp->w_minheight = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200407 if ((nr = dict_get_number_def(d, (char_u *)"maxwidth", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200408 wp->w_maxwidth = nr;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200409 if ((nr = dict_get_number_def(d, (char_u *)"maxheight", -1)) >= 0)
Bram Moolenaarae943152019-06-16 22:54:14 +0200410 wp->w_maxheight = nr;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200411
412 nr = popup_options_one(d, (char_u *)"line");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200413 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200414 wp->w_wantline = nr;
415 nr = popup_options_one(d, (char_u *)"col");
Bram Moolenaar1fb08312019-08-29 20:02:11 +0200416 if (nr != MAXCOL)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200417 wp->w_wantcol = nr;
418
419 di = dict_find(d, (char_u *)"fixed", -1);
420 if (di != NULL)
421 wp->w_popup_fixed = dict_get_number(d, (char_u *)"fixed") != 0;
422
423 str = dict_get_string(d, (char_u *)"pos", FALSE);
424 if (str != NULL)
425 {
426 for (nr = 0;
427 nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
428 ++nr)
429 if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
430 {
431 wp->w_popup_pos = poppos_entries[nr].pp_val;
432 nr = -1;
433 break;
434 }
435 if (nr != -1)
436 semsg(_(e_invarg2), str);
437 }
438
439 str = dict_get_string(d, (char_u *)"textprop", FALSE);
440 if (str != NULL)
441 {
442 wp->w_popup_prop_type = 0;
443 if (*str != NUL)
444 {
445 nr = find_prop_type_id(str, wp->w_buffer);
446 if (nr <= 0)
447 nr = find_prop_type_id(str, NULL);
448 if (nr <= 0)
449 semsg(_(e_invarg2), str);
450 else
451 {
452 wp->w_popup_prop_type = nr;
453 wp->w_popup_prop_win = curwin;
454
455 di = dict_find(d, (char_u *)"textpropwin", -1);
456 if (di != NULL)
457 {
458 wp->w_popup_prop_win = find_win_by_nr_or_id(&di->di_tv);
459 if (win_valid(wp->w_popup_prop_win))
460 wp->w_popup_prop_win = curwin;
461 }
462 }
463 }
464 }
465
466 di = dict_find(d, (char_u *)"textpropid", -1);
467 if (di != NULL)
468 wp->w_popup_prop_id = dict_get_number(d, (char_u *)"textpropid");
Bram Moolenaarae943152019-06-16 22:54:14 +0200469}
470
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200471 static void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200472handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved)
473{
474 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
475 {
476 char_u *s = di->di_tv.vval.v_string;
477 int flags = 0;
478
479 if (STRCMP(s, "word") == 0)
480 flags = FIND_IDENT | FIND_STRING;
481 else if (STRCMP(s, "WORD") == 0)
482 flags = FIND_STRING;
483 else if (STRCMP(s, "expr") == 0)
484 flags = FIND_IDENT | FIND_STRING | FIND_EVAL;
485 else if (STRCMP(s, "any") != 0)
486 semsg(_(e_invarg2), s);
487 if (flags != 0)
488 {
489 if (mousemoved)
490 set_mousemoved_columns(wp, flags);
491 else
492 set_moved_columns(wp, flags);
493 }
494 }
495 else if (di->di_tv.v_type == VAR_LIST
496 && di->di_tv.vval.v_list != NULL
Bram Moolenaara1396152019-10-20 18:46:05 +0200497 && (di->di_tv.vval.v_list->lv_len == 2
498 || di->di_tv.vval.v_list->lv_len == 3))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200499 {
Bram Moolenaara1396152019-10-20 18:46:05 +0200500 list_T *l = di->di_tv.vval.v_list;
501 listitem_T *li = l->lv_first;
502 int mincol;
503 int maxcol;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200504
Bram Moolenaara1396152019-10-20 18:46:05 +0200505 if (di->di_tv.vval.v_list->lv_len == 3)
506 {
507 varnumber_T nr = tv_get_number(&l->lv_first->li_tv);
508
509 // Three numbers, might be from popup_getoptions().
510 if (mousemoved)
511 wp->w_popup_mouse_row = nr;
512 else
513 wp->w_popup_lnum = nr;
514 li = li->li_next;
515 }
516
517 mincol = tv_get_number(&li->li_tv);
518 maxcol = tv_get_number(&li->li_next->li_tv);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200519 if (mousemoved)
520 {
521 wp->w_popup_mouse_mincol = mincol;
522 wp->w_popup_mouse_maxcol = maxcol;
523 }
524 else
525 {
526 wp->w_popup_mincol = mincol;
527 wp->w_popup_maxcol = maxcol;
528 }
529 }
530 else
531 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
532}
533
534 static void
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200535check_highlight(dict_T *dict, char *name, char_u **pval)
536{
537 dictitem_T *di;
538 char_u *str;
539
540 di = dict_find(dict, (char_u *)name, -1);
541 if (di != NULL)
542 {
543 if (di->di_tv.v_type != VAR_STRING)
544 semsg(_(e_invargval), name);
545 else
546 {
547 str = tv_get_string(&di->di_tv);
548 if (*str != NUL)
549 *pval = vim_strsave(str);
550 }
551 }
552}
553
Bram Moolenaarae943152019-06-16 22:54:14 +0200554/*
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200555 * Scroll to show the line with the cursor.
Bram Moolenaar79648732019-07-18 21:43:07 +0200556 */
557 static void
558popup_show_curline(win_T *wp)
559{
560 if (wp->w_cursor.lnum < wp->w_topline)
561 wp->w_topline = wp->w_cursor.lnum;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200562 else if (wp->w_cursor.lnum >= wp->w_botline
563 && (curwin->w_valid & VALID_BOTLINE))
564 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200565 wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200566 if (wp->w_topline < 1)
567 wp->w_topline = 1;
568 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
569 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200570 while (wp->w_topline < wp->w_cursor.lnum
571 && wp->w_topline < wp->w_buffer->b_ml.ml_line_count
572 && plines_m_win(wp, wp->w_topline, wp->w_cursor.lnum)
573 > wp->w_height)
574 ++wp->w_topline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200575 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200576
577 // Don't use "firstline" now.
578 wp->w_firstline = 0;
579}
580
581/*
582 * Get the sign group name for window "wp".
583 * Returns a pointer to a static buffer, overwritten on the next call.
584 */
585 static char_u *
586popup_get_sign_name(win_T *wp)
587{
588 static char buf[30];
589
590 vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id);
591 return (char_u *)buf;
Bram Moolenaar79648732019-07-18 21:43:07 +0200592}
593
594/*
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200595 * Highlight the line with the cursor.
596 * Also scrolls the text to put the cursor line in view.
597 */
598 static void
599popup_highlight_curline(win_T *wp)
600{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200601 int sign_id = 0;
602 char_u *sign_name = popup_get_sign_name(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200603
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200604 buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu");
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200605
606 if ((wp->w_popup_flags & POPF_CURSORLINE) != 0)
607 {
Bram Moolenaar79648732019-07-18 21:43:07 +0200608 popup_show_curline(wp);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200609
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200610 if (!sign_exists_by_name(sign_name))
611 {
612 char *linehl = "PopupSelected";
613
614 if (syn_name2id((char_u *)linehl) == 0)
615 linehl = "PmenuSel";
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200616 sign_define_by_name(sign_name, NULL, (char_u *)linehl, NULL, NULL);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200617 }
618
619 sign_place(&sign_id, (char_u *)"popupmenu", sign_name,
620 wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO);
621 redraw_win_later(wp, NOT_VALID);
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200622 }
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200623 else
624 sign_undefine_by_name(sign_name, FALSE);
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +0200625 wp->w_popup_last_curline = wp->w_cursor.lnum;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200626}
627
628/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200629 * Shared between popup_create() and f_popup_setoptions().
630 */
631 static void
632apply_general_options(win_T *wp, dict_T *dict)
633{
634 dictitem_T *di;
Bram Moolenaar402502d2019-05-30 22:07:36 +0200635 int nr;
636 char_u *str;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200637
Bram Moolenaarae943152019-06-16 22:54:14 +0200638 // TODO: flip
639
640 di = dict_find(dict, (char_u *)"firstline", -1);
Bram Moolenaardfa97f22019-06-15 14:31:55 +0200641 if (di != NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200642 {
Bram Moolenaarae943152019-06-16 22:54:14 +0200643 wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200644 if (wp->w_firstline < 0)
645 wp->w_firstline = -1;
646 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200647
Bram Moolenaar75fb0852019-06-25 05:15:58 +0200648 di = dict_find(dict, (char_u *)"scrollbar", -1);
649 if (di != NULL)
650 wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar");
651
Bram Moolenaareb2310d2019-06-16 20:09:10 +0200652 str = dict_get_string(dict, (char_u *)"title", FALSE);
653 if (str != NULL)
654 {
655 vim_free(wp->w_popup_title);
656 wp->w_popup_title = vim_strsave(str);
657 }
658
Bram Moolenaar402502d2019-05-30 22:07:36 +0200659 di = dict_find(dict, (char_u *)"wrap", -1);
660 if (di != NULL)
661 {
662 nr = dict_get_number(dict, (char_u *)"wrap");
663 wp->w_p_wrap = nr != 0;
664 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200665
Bram Moolenaara42d9452019-06-15 21:46:30 +0200666 di = dict_find(dict, (char_u *)"drag", -1);
667 if (di != NULL)
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200668 {
669 nr = dict_get_number(dict, (char_u *)"drag");
670 if (nr)
671 wp->w_popup_flags |= POPF_DRAG;
672 else
673 wp->w_popup_flags &= ~POPF_DRAG;
674 }
675
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100676 di = dict_find(dict, (char_u *)"posinvert", -1);
677 if (di != NULL)
678 {
679 nr = dict_get_number(dict, (char_u *)"posinvert");
680 if (nr)
681 wp->w_popup_flags |= POPF_POSINVERT;
682 else
683 wp->w_popup_flags &= ~POPF_POSINVERT;
684 }
685
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200686 di = dict_find(dict, (char_u *)"resize", -1);
687 if (di != NULL)
688 {
689 nr = dict_get_number(dict, (char_u *)"resize");
690 if (nr)
691 wp->w_popup_flags |= POPF_RESIZE;
692 else
693 wp->w_popup_flags &= ~POPF_RESIZE;
694 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +0200695
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200696 di = dict_find(dict, (char_u *)"close", -1);
697 if (di != NULL)
698 {
699 int ok = TRUE;
700
701 if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
702 {
703 char_u *s = di->di_tv.vval.v_string;
704
705 if (STRCMP(s, "none") == 0)
706 wp->w_popup_close = POPCLOSE_NONE;
707 else if (STRCMP(s, "button") == 0)
708 wp->w_popup_close = POPCLOSE_BUTTON;
709 else if (STRCMP(s, "click") == 0)
710 wp->w_popup_close = POPCLOSE_CLICK;
711 else
712 ok = FALSE;
713 }
714 else
715 ok = FALSE;
716 if (!ok)
717 semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv));
718 }
719
Bram Moolenaarae943152019-06-16 22:54:14 +0200720 str = dict_get_string(dict, (char_u *)"highlight", FALSE);
721 if (str != NULL)
722 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
723 str, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200724
Bram Moolenaarcb5ff342019-07-20 16:51:19 +0200725 set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1,
726 (char_u *)"no", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaarae943152019-06-16 22:54:14 +0200727 set_padding_border(dict, wp->w_popup_padding, "padding", 999);
728 set_padding_border(dict, wp->w_popup_border, "border", 1);
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200729
Bram Moolenaar790498b2019-06-01 22:15:29 +0200730 di = dict_find(dict, (char_u *)"borderhighlight", -1);
731 if (di != NULL)
732 {
Bram Moolenaar403d0902019-07-17 21:37:32 +0200733 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200734 emsg(_(e_listreq));
735 else
736 {
737 list_T *list = di->di_tv.vval.v_list;
738 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200739 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200740
Bram Moolenaar403d0902019-07-17 21:37:32 +0200741 for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
742 ++i, li = li->li_next)
743 {
744 str = tv_get_string(&li->li_tv);
745 if (*str != NUL)
746 wp->w_border_highlight[i] = vim_strsave(str);
747 }
Bram Moolenaar790498b2019-06-01 22:15:29 +0200748 if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
749 for (i = 1; i < 4; ++i)
Bram Moolenaar403d0902019-07-17 21:37:32 +0200750 wp->w_border_highlight[i] =
Bram Moolenaar790498b2019-06-01 22:15:29 +0200751 vim_strsave(wp->w_border_highlight[0]);
752 }
753 }
754
Bram Moolenaar790498b2019-06-01 22:15:29 +0200755 di = dict_find(dict, (char_u *)"borderchars", -1);
756 if (di != NULL)
757 {
758 if (di->di_tv.v_type != VAR_LIST)
759 emsg(_(e_listreq));
760 else
761 {
762 list_T *list = di->di_tv.vval.v_list;
763 listitem_T *li;
Bram Moolenaarae943152019-06-16 22:54:14 +0200764 int i;
Bram Moolenaar790498b2019-06-01 22:15:29 +0200765
766 if (list != NULL)
767 for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
768 ++i, li = li->li_next)
769 {
770 str = tv_get_string(&li->li_tv);
771 if (*str != NUL)
772 wp->w_border_char[i] = mb_ptr2char(str);
773 }
774 if (list->lv_len == 1)
775 for (i = 1; i < 8; ++i)
776 wp->w_border_char[i] = wp->w_border_char[0];
777 if (list->lv_len == 2)
778 {
779 for (i = 4; i < 8; ++i)
780 wp->w_border_char[i] = wp->w_border_char[1];
781 for (i = 1; i < 4; ++i)
782 wp->w_border_char[i] = wp->w_border_char[0];
783 }
784 }
785 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200786
Bram Moolenaar4cd583c2019-06-26 05:13:57 +0200787 check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight);
788 check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight);
789
Bram Moolenaarae943152019-06-16 22:54:14 +0200790 di = dict_find(dict, (char_u *)"zindex", -1);
791 if (di != NULL)
792 {
793 wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
794 if (wp->w_zindex < 1)
795 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
796 if (wp->w_zindex > 32000)
797 wp->w_zindex = 32000;
798 }
799
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200800 di = dict_find(dict, (char_u *)"mask", -1);
801 if (di != NULL)
802 {
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200803 int ok = FALSE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200804
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200805 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200806 {
807 listitem_T *li;
808
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200809 ok = TRUE;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200810 for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
811 li = li->li_next)
812 {
813 if (li->li_tv.v_type != VAR_LIST
814 || li->li_tv.vval.v_list == NULL
815 || li->li_tv.vval.v_list->lv_len != 4)
816 {
817 ok = FALSE;
818 break;
819 }
820 }
821 }
822 if (ok)
823 {
824 wp->w_popup_mask = di->di_tv.vval.v_list;
825 ++wp->w_popup_mask->lv_refcount;
Bram Moolenaare865dcb2019-07-26 22:15:50 +0200826 VIM_CLEAR(wp->w_popup_mask_cells);
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200827 }
828 else
829 semsg(_(e_invargval), "mask");
830 }
831
Bram Moolenaarae943152019-06-16 22:54:14 +0200832#if defined(FEAT_TIMERS)
833 // Add timer to close the popup after some time.
834 nr = dict_get_number(dict, (char_u *)"time");
835 if (nr > 0)
836 popup_add_timeout(wp, nr);
837#endif
838
Bram Moolenaar3397f742019-06-02 18:40:06 +0200839 di = dict_find(dict, (char_u *)"moved", -1);
840 if (di != NULL)
841 {
Bram Moolenaar17627312019-06-02 19:53:44 +0200842 set_moved_values(wp);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200843 handle_moved_argument(wp, di, FALSE);
844 }
Bram Moolenaar3397f742019-06-02 18:40:06 +0200845
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200846 di = dict_find(dict, (char_u *)"mousemoved", -1);
847 if (di != NULL)
848 {
849 set_mousemoved_values(wp);
850 handle_moved_argument(wp, di, TRUE);
Bram Moolenaar3397f742019-06-02 18:40:06 +0200851 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200852
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200853 di = dict_find(dict, (char_u *)"cursorline", -1);
854 if (di != NULL)
855 {
856 if (di->di_tv.v_type == VAR_NUMBER)
857 {
858 if (di->di_tv.vval.v_number != 0)
859 wp->w_popup_flags |= POPF_CURSORLINE;
860 else
861 wp->w_popup_flags &= ~POPF_CURSORLINE;
862 }
863 else
864 semsg(_(e_invargval), "cursorline");
865 }
866
Bram Moolenaarae943152019-06-16 22:54:14 +0200867 di = dict_find(dict, (char_u *)"filter", -1);
868 if (di != NULL)
869 {
870 callback_T callback = get_callback(&di->di_tv);
871
872 if (callback.cb_name != NULL)
873 {
874 free_callback(&wp->w_filter_cb);
875 set_callback(&wp->w_filter_cb, &callback);
876 }
877 }
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200878 di = dict_find(dict, (char_u *)"mapping", -1);
879 if (di != NULL)
880 {
881 nr = dict_get_number(dict, (char_u *)"mapping");
882 if (nr)
883 wp->w_popup_flags |= POPF_MAPPING;
884 else
885 wp->w_popup_flags &= ~POPF_MAPPING;
886 }
Bram Moolenaarae943152019-06-16 22:54:14 +0200887
Bram Moolenaar581ba392019-09-03 22:08:33 +0200888 str = dict_get_string(dict, (char_u *)"filtermode", FALSE);
889 if (str != NULL)
890 {
891 if (STRCMP(str, "a") == 0)
892 wp->w_filter_mode = MODE_ALL;
893 else
894 wp->w_filter_mode = mode_str2flags(str);
895 }
896
Bram Moolenaarae943152019-06-16 22:54:14 +0200897 di = dict_find(dict, (char_u *)"callback", -1);
898 if (di != NULL)
899 {
900 callback_T callback = get_callback(&di->di_tv);
901
902 if (callback.cb_name != NULL)
903 {
904 free_callback(&wp->w_close_cb);
905 set_callback(&wp->w_close_cb, &callback);
906 }
907 }
908}
909
910/*
911 * Go through the options in "dict" and apply them to popup window "wp".
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200912 * Only used when creating a new popup window.
Bram Moolenaarae943152019-06-16 22:54:14 +0200913 */
914 static void
915apply_options(win_T *wp, dict_T *dict)
916{
917 int nr;
918
919 apply_move_options(wp, dict);
920 apply_general_options(wp, dict);
921
Bram Moolenaar6313c4f2019-06-16 20:39:13 +0200922 nr = dict_get_number(dict, (char_u *)"hidden");
923 if (nr > 0)
924 {
925 wp->w_popup_flags |= POPF_HIDDEN;
926 --wp->w_buffer->b_nwindows;
927 }
928
Bram Moolenaar33796b32019-06-08 16:01:13 +0200929 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +0200930 popup_highlight_curline(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200931}
932
933/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200934 * Add lines to the popup from a list of strings.
935 */
936 static void
937add_popup_strings(buf_T *buf, list_T *l)
938{
939 listitem_T *li;
940 linenr_T lnum = 0;
941 char_u *p;
942
943 for (li = l->lv_first; li != NULL; li = li->li_next)
944 if (li->li_tv.v_type == VAR_STRING)
945 {
946 p = li->li_tv.vval.v_string;
947 ml_append_buf(buf, lnum++,
948 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
949 }
950}
951
952/*
953 * Add lines to the popup from a list of dictionaries.
954 */
955 static void
956add_popup_dicts(buf_T *buf, list_T *l)
957{
958 listitem_T *li;
959 listitem_T *pli;
960 linenr_T lnum = 0;
961 char_u *p;
962 dict_T *dict;
963
964 // first add the text lines
965 for (li = l->lv_first; li != NULL; li = li->li_next)
966 {
967 if (li->li_tv.v_type != VAR_DICT)
968 {
969 emsg(_(e_dictreq));
970 return;
971 }
972 dict = li->li_tv.vval.v_dict;
973 p = dict == NULL ? NULL
974 : dict_get_string(dict, (char_u *)"text", FALSE);
975 ml_append_buf(buf, lnum++,
976 p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
977 }
978
979 // add the text properties
980 lnum = 1;
981 for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
982 {
983 dictitem_T *di;
984 list_T *plist;
985
986 dict = li->li_tv.vval.v_dict;
987 di = dict_find(dict, (char_u *)"props", -1);
988 if (di != NULL)
989 {
990 if (di->di_tv.v_type != VAR_LIST)
991 {
992 emsg(_(e_listreq));
993 return;
994 }
995 plist = di->di_tv.vval.v_list;
996 if (plist != NULL)
997 {
998 for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
999 {
1000 if (pli->li_tv.v_type != VAR_DICT)
1001 {
1002 emsg(_(e_dictreq));
1003 return;
1004 }
1005 dict = pli->li_tv.vval.v_dict;
1006 if (dict != NULL)
1007 {
1008 int col = dict_get_number(dict, (char_u *)"col");
1009
1010 prop_add_common( lnum, col, dict, buf, NULL);
1011 }
1012 }
1013 }
1014 }
1015 }
1016}
1017
1018/*
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001019 * Get the padding plus border at the top, adjusted to 1 if there is a title.
1020 */
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001021 int
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001022popup_top_extra(win_T *wp)
1023{
1024 int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
1025
1026 if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1027 return 1;
1028 return extra;
1029}
1030
1031/*
Bram Moolenaard529ba52019-07-02 23:13:53 +02001032 * Return the height of popup window "wp", including border and padding.
1033 */
1034 int
1035popup_height(win_T *wp)
1036{
1037 return wp->w_height
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001038 + popup_top_extra(wp)
1039 + wp->w_popup_padding[2] + wp->w_popup_border[2];
Bram Moolenaard529ba52019-07-02 23:13:53 +02001040}
1041
1042/*
1043 * Return the width of popup window "wp", including border, padding and
1044 * scrollbar.
1045 */
1046 int
1047popup_width(win_T *wp)
1048{
Bram Moolenaar017c2692019-07-13 14:17:51 +02001049 // w_leftcol is how many columns of the core are left of the screen
1050 // w_popup_rightoff is how many columns of the core are right of the screen
Bram Moolenaard529ba52019-07-02 23:13:53 +02001051 return wp->w_width + wp->w_leftcol
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001052 + popup_extra_width(wp)
1053 + wp->w_popup_rightoff;
1054}
1055
1056/*
1057 * Return the extra width of popup window "wp": border, padding and scrollbar.
1058 */
1059 int
1060popup_extra_width(win_T *wp)
1061{
1062 return wp->w_popup_padding[3] + wp->w_popup_border[3]
1063 + wp->w_popup_padding[1] + wp->w_popup_border[1]
1064 + wp->w_has_scrollbar;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001065}
1066
1067/*
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001068 * Adjust the position and size of the popup to fit on the screen.
1069 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001070 static void
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001071popup_adjust_position(win_T *wp)
1072{
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001073 linenr_T lnum;
1074 int wrapped = 0;
1075 int maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001076 int used_maxwidth = FALSE;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001077 int maxspace;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001078 int center_vert = FALSE;
1079 int center_hor = FALSE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001080 int allow_adjust_left = !wp->w_popup_fixed;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001081 int top_extra = popup_top_extra(wp);
Bram Moolenaar399d8982019-06-02 15:34:29 +02001082 int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
1083 int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
1084 int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
1085 int extra_height = top_extra + bot_extra;
1086 int extra_width = left_extra + right_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001087 int org_winrow = wp->w_winrow;
1088 int org_wincol = wp->w_wincol;
1089 int org_width = wp->w_width;
1090 int org_height = wp->w_height;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001091 int org_leftcol = wp->w_leftcol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001092 int org_leftoff = wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001093 int minwidth;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001094 int wantline = wp->w_wantline; // adjusted for textprop
1095 int wantcol = wp->w_wantcol; // adjusted for textprop
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001096 int use_wantcol = wantcol != 0;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001097
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001098 wp->w_winrow = 0;
1099 wp->w_wincol = 0;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001100 wp->w_leftcol = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001101 wp->w_popup_leftoff = 0;
1102 wp->w_popup_rightoff = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001103
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001104 // May need to update the "cursorline" highlighting, which may also change
1105 // "topline"
1106 if (wp->w_popup_last_curline != wp->w_cursor.lnum)
1107 popup_highlight_curline(wp);
1108
Bram Moolenaar12034e22019-08-25 22:25:02 +02001109 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
1110 {
1111 win_T *prop_win = wp->w_popup_prop_win;
1112 textprop_T prop;
1113 linenr_T prop_lnum;
1114 pos_T pos;
1115 int screen_row;
1116 int screen_scol;
1117 int screen_ccol;
1118 int screen_ecol;
1119
1120 // Popup window is positioned relative to a text property.
1121 if (find_visible_prop(prop_win,
1122 wp->w_popup_prop_type, wp->w_popup_prop_id,
1123 &prop, &prop_lnum) == FAIL)
1124 {
1125 // Text property is no longer visible, hide the popup.
1126 // Unhiding the popup is done in check_popup_unhidden().
1127 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
1128 {
1129 wp->w_popup_flags |= POPF_HIDDEN;
1130 --wp->w_buffer->b_nwindows;
1131 if (win_valid(wp->w_popup_prop_win))
1132 redraw_win_later(wp->w_popup_prop_win, SOME_VALID);
1133 }
1134 return;
1135 }
1136
1137 // Compute the desired position from the position of the text
1138 // property. Use "wantline" and "wantcol" as offsets.
1139 pos.lnum = prop_lnum;
1140 pos.col = prop.tp_col;
1141 if (wp->w_popup_pos == POPPOS_TOPLEFT
1142 || wp->w_popup_pos == POPPOS_BOTLEFT)
1143 pos.col += prop.tp_len - 1;
1144 textpos2screenpos(prop_win, &pos, &screen_row,
1145 &screen_scol, &screen_ccol, &screen_ecol);
1146
1147 if (wp->w_popup_pos == POPPOS_TOPLEFT
1148 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1149 // below the text
1150 wantline = screen_row + wantline + 1;
1151 else
1152 // above the text
1153 wantline = screen_row + wantline - 1;
1154 center_vert = FALSE;
1155 if (wp->w_popup_pos == POPPOS_TOPLEFT
1156 || wp->w_popup_pos == POPPOS_BOTLEFT)
1157 // right of the text
1158 wantcol = screen_ecol + wantcol;
1159 else
1160 // left of the text
Bram Moolenaarbc2d4c12019-08-28 22:18:30 +02001161 wantcol = screen_scol + wantcol - 2;
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001162 use_wantcol = TRUE;
1163 }
1164 else
1165 {
1166 // If no line was specified default to vertical centering.
1167 if (wantline == 0)
1168 center_vert = TRUE;
1169 else if (wantline < 0)
1170 // If "wantline" is negative it actually means zero.
1171 wantline = 0;
1172 if (wantcol < 0)
1173 // If "wantcol" is negative it actually means zero.
1174 wantcol = 0;
Bram Moolenaar12034e22019-08-25 22:25:02 +02001175 }
1176
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001177 if (wp->w_popup_pos == POPPOS_CENTER)
1178 {
1179 // center after computing the size
1180 center_vert = TRUE;
1181 center_hor = TRUE;
1182 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001183 else
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001184 {
Bram Moolenaaraa1f04d2019-10-24 22:12:54 +02001185 if (wantline > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1186 || wp->w_popup_pos == POPPOS_TOPRIGHT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001187 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001188 wp->w_winrow = wantline - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001189 if (wp->w_winrow >= Rows)
1190 wp->w_winrow = Rows - 1;
1191 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001192
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001193 if (!use_wantcol)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001194 center_hor = TRUE;
Bram Moolenaar1fb08312019-08-29 20:02:11 +02001195 else if (wantcol > 0 && (wp->w_popup_pos == POPPOS_TOPLEFT
1196 || wp->w_popup_pos == POPPOS_BOTLEFT))
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001197 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001198 wp->w_wincol = wantcol - 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001199 if (wp->w_wincol >= Columns - 3)
1200 wp->w_wincol = Columns - 3;
1201 }
1202 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001203
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001204 // When centering or right aligned, use maximum width.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001205 // When left aligned use the space available, but shift to the left when we
1206 // hit the right of the screen.
Bram Moolenaard529ba52019-07-02 23:13:53 +02001207 maxspace = Columns - wp->w_wincol - left_extra;
1208 maxwidth = maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001209 if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001210 {
1211 allow_adjust_left = FALSE;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001212 maxwidth = wp->w_maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001213 }
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001214 minwidth = wp->w_minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001215
Bram Moolenaar8d241042019-06-12 23:40:01 +02001216 // start at the desired first line
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001217 if (wp->w_firstline > 0)
Bram Moolenaar79648732019-07-18 21:43:07 +02001218 wp->w_topline = wp->w_firstline;
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02001219 if (wp->w_topline < 1)
1220 wp->w_topline = 1;
1221 else if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar8d241042019-06-12 23:40:01 +02001222 wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
1223
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001224 // Compute width based on longest text line and the 'wrap' option.
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001225 // Use a minimum width of one, so that something shows when there is no
1226 // text.
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001227 // When "firstline" is -1 then start with the last buffer line and go
1228 // backwards.
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001229 // TODO: more accurate wrapping
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001230 wp->w_width = 1;
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001231 if (wp->w_firstline < 0)
1232 lnum = wp->w_buffer->b_ml.ml_line_count;
1233 else
1234 lnum = wp->w_topline;
1235 while (lnum >= 1 && lnum <= wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001236 {
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001237 int len;
1238 int w_width = wp->w_width;
1239
1240 // Count Tabs for what they are worth and compute the length based on
1241 // the maximum width (matters when 'showbreak' is set).
1242 if (wp->w_width < maxwidth)
1243 wp->w_width = maxwidth;
1244 len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE),
Bram Moolenaare089c3f2019-07-09 20:25:25 +02001245 (colnr_T)MAXCOL);
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001246 wp->w_width = w_width;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001247
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001248 if (wp->w_p_wrap)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001249 {
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001250 while (len > maxwidth)
1251 {
1252 ++wrapped;
1253 len -= maxwidth;
1254 wp->w_width = maxwidth;
Bram Moolenaar7b3d9392019-10-16 22:17:07 +02001255 used_maxwidth = TRUE;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001256 }
1257 }
1258 else if (len > maxwidth
1259 && allow_adjust_left
1260 && (wp->w_popup_pos == POPPOS_TOPLEFT
1261 || wp->w_popup_pos == POPPOS_BOTLEFT))
1262 {
1263 // adjust leftwise to fit text on screen
Bram Moolenaar51c31312019-06-15 22:27:23 +02001264 int shift_by = len - maxwidth;
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001265
Bram Moolenaar51c31312019-06-15 22:27:23 +02001266 if (shift_by > wp->w_wincol)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001267 {
1268 int truncate_shift = shift_by - wp->w_wincol;
Bram Moolenaar51c31312019-06-15 22:27:23 +02001269
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001270 len -= truncate_shift;
1271 shift_by -= truncate_shift;
1272 }
1273
1274 wp->w_wincol -= shift_by;
1275 maxwidth += shift_by;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001276 wp->w_width = maxwidth;
1277 }
1278 if (wp->w_width < len)
Bram Moolenaar017c2692019-07-13 14:17:51 +02001279 {
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001280 wp->w_width = len;
Bram Moolenaar017c2692019-07-13 14:17:51 +02001281 if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth)
1282 wp->w_width = wp->w_maxwidth;
1283 }
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001284
1285 if (wp->w_firstline < 0)
1286 --lnum;
1287 else
1288 ++lnum;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001289
1290 // do not use the width of lines we're not going to show
1291 if (wp->w_maxheight > 0
1292 && (wp->w_firstline >= 0
1293 ? lnum - wp->w_topline
1294 : wp->w_buffer->b_ml.ml_line_count - lnum)
1295 + wrapped >= wp->w_maxheight)
1296 break;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297 }
1298
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001299 if (wp->w_firstline < 0)
1300 wp->w_topline = lnum > 0 ? lnum + 1 : lnum;
1301
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001302 wp->w_has_scrollbar = wp->w_want_scrollbar
1303 && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001304 if (wp->w_has_scrollbar)
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001305 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001306 ++right_extra;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001307 ++extra_width;
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001308 // make space for the scrollbar if needed, when lines wrap and when
1309 // applying minwidth
1310 if (maxwidth + right_extra >= maxspace
1311 && (used_maxwidth || (minwidth > 0 && wp->w_width < minwidth)))
1312 maxwidth -= wp->w_popup_padding[1] + 1;
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02001313 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001314
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001315 if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
1316 {
1317 int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
1318
1319 if (minwidth < title_len)
1320 minwidth = title_len;
1321 }
1322
1323 if (minwidth > 0 && wp->w_width < minwidth)
1324 wp->w_width = minwidth;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001325 if (wp->w_width > maxwidth)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001326 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001327 if (wp->w_width > maxspace && !wp->w_p_wrap)
Bram Moolenaard529ba52019-07-02 23:13:53 +02001328 // some columns cut off on the right
1329 wp->w_popup_rightoff = wp->w_width - maxspace;
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001330 wp->w_width = maxwidth;
Bram Moolenaard529ba52019-07-02 23:13:53 +02001331 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001332 if (center_hor)
Bram Moolenaara730e552019-06-16 19:05:31 +02001333 {
1334 wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
1335 if (wp->w_wincol < 0)
1336 wp->w_wincol = 0;
1337 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001338 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
1339 || wp->w_popup_pos == POPPOS_TOPRIGHT)
1340 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001341 int leftoff = wantcol - (wp->w_width + extra_width);
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001342
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001343 // Right aligned: move to the right if needed.
1344 // No truncation, because that would change the height.
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001345 if (leftoff >= 0)
1346 wp->w_wincol = leftoff;
1347 else if (wp->w_popup_fixed)
1348 {
1349 // "col" specifies the right edge, but popup doesn't fit, skip some
Bram Moolenaard529ba52019-07-02 23:13:53 +02001350 // columns when displaying the window, minus left border and
1351 // padding.
1352 if (-leftoff > left_extra)
1353 wp->w_leftcol = -leftoff - left_extra;
1354 wp->w_width -= wp->w_leftcol;
1355 wp->w_popup_leftoff = -leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001356 if (wp->w_width < 0)
1357 wp->w_width = 0;
1358 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001359 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001360
Bram Moolenaar8edf0e32019-07-30 21:19:26 +02001361 if (wp->w_p_wrap || (!wp->w_popup_fixed
1362 && (wp->w_popup_pos == POPPOS_TOPLEFT
1363 || wp->w_popup_pos == POPPOS_BOTLEFT)))
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001364 {
1365 int want_col = 0;
1366
Bram Moolenaar8c8b88d2019-07-30 20:32:41 +02001367 // try to show the right border and any scrollbar
1368 want_col = left_extra + wp->w_width + right_extra;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001369 if (want_col > 0 && wp->w_wincol > 0
1370 && wp->w_wincol + want_col >= Columns)
1371 {
1372 wp->w_wincol = Columns - want_col;
1373 if (wp->w_wincol < 0)
1374 wp->w_wincol = 0;
1375 }
1376 }
1377
Bram Moolenaar8d241042019-06-12 23:40:01 +02001378 wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
1379 + 1 + wrapped;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001380 if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
1381 wp->w_height = wp->w_minheight;
1382 if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
1383 wp->w_height = wp->w_maxheight;
1384 if (wp->w_height > Rows - wp->w_winrow)
1385 wp->w_height = Rows - wp->w_winrow;
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001386 if (wp->w_height != org_height)
1387 win_comp_scroll(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001388
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001389 if (center_vert)
Bram Moolenaara730e552019-06-16 19:05:31 +02001390 {
1391 wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
1392 if (wp->w_winrow < 0)
1393 wp->w_winrow = 0;
1394 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001395 else if (wp->w_popup_pos == POPPOS_BOTRIGHT
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001396 || wp->w_popup_pos == POPPOS_BOTLEFT)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001397 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001398 if ((wp->w_height + extra_height) <= wantline)
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001399 // bottom aligned: may move down
Bram Moolenaar12034e22019-08-25 22:25:02 +02001400 wp->w_winrow = wantline - (wp->w_height + extra_height);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001401 else if (wantline * 2 >= Rows || !(wp->w_popup_flags & POPF_POSINVERT))
1402 {
1403 // Bottom aligned but does not fit, and less space on the other
1404 // side or "posinvert" is off: reduce height.
1405 wp->w_winrow = 0;
1406 wp->w_height = wantline - extra_height;
1407 }
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001408 else
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001409 // Not enough space and more space on the other side: make top
1410 // aligned.
Bram Moolenaarb754b5b2019-10-24 19:25:00 +02001411 wp->w_winrow = (wantline < 0 ? 0 : wantline) + 1;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001412 }
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001413 else if (wp->w_popup_pos == POPPOS_TOPRIGHT
1414 || wp->w_popup_pos == POPPOS_TOPLEFT)
1415 {
1416 if (wantline + (wp->w_height + extra_height) - 1 > Rows
1417 && wantline * 2 > Rows
1418 && (wp->w_popup_flags & POPF_POSINVERT))
1419 // top aligned and not enough space below but there is space above:
1420 // make bottom aligned
1421 wp->w_winrow = wantline - 2 - wp->w_height - extra_height;
1422 else
1423 wp->w_winrow = wantline - 1;
1424 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02001425 if (wp->w_winrow >= Rows)
1426 wp->w_winrow = Rows - 1;
1427 else if (wp->w_winrow < 0)
1428 wp->w_winrow = 0;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001429
Bram Moolenaar17146962019-05-30 00:12:11 +02001430 wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar12034e22019-08-25 22:25:02 +02001431 if (win_valid(wp->w_popup_prop_win))
1432 {
1433 wp->w_popup_prop_changedtick =
1434 CHANGEDTICK(wp->w_popup_prop_win->w_buffer);
1435 wp->w_popup_prop_topline = wp->w_popup_prop_win->w_topline;
1436 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001437
1438 // Need to update popup_mask if the position or size changed.
Bram Moolenaar356375f2019-08-24 14:46:29 +02001439 // And redraw windows and statuslines that were behind the popup.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001440 if (org_winrow != wp->w_winrow
1441 || org_wincol != wp->w_wincol
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001442 || org_leftcol != wp->w_leftcol
Bram Moolenaard529ba52019-07-02 23:13:53 +02001443 || org_leftoff != wp->w_popup_leftoff
Bram Moolenaar33796b32019-06-08 16:01:13 +02001444 || org_width != wp->w_width
1445 || org_height != wp->w_height)
1446 {
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001447 redraw_win_later(wp, NOT_VALID);
1448 if (wp->w_popup_flags & POPF_ON_CMDLINE)
1449 clear_cmdline = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001450 popup_mask_refresh = TRUE;
1451 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001452}
1453
Bram Moolenaar17627312019-06-02 19:53:44 +02001454typedef enum
1455{
1456 TYPE_NORMAL,
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001457 TYPE_ATCURSOR,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001458 TYPE_BEVAL,
Bram Moolenaara42d9452019-06-15 21:46:30 +02001459 TYPE_NOTIFICATION,
Bram Moolenaara730e552019-06-16 19:05:31 +02001460 TYPE_DIALOG,
Bram Moolenaar79648732019-07-18 21:43:07 +02001461 TYPE_MENU,
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001462 TYPE_PREVIEW, // preview window
1463 TYPE_INFO // popup menu info
Bram Moolenaar17627312019-06-02 19:53:44 +02001464} create_type_T;
1465
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001466/*
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001467 * Make "buf" empty and set the contents to "text".
1468 * Used by popup_create() and popup_settext().
1469 */
1470 static void
1471popup_set_buffer_text(buf_T *buf, typval_T text)
1472{
1473 int lnum;
1474
1475 // Clear the buffer, then replace the lines.
1476 curbuf = buf;
1477 for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
1478 ml_delete(lnum, FALSE);
1479 curbuf = curwin->w_buffer;
1480
1481 // Add text to the buffer.
1482 if (text.v_type == VAR_STRING)
1483 {
1484 // just a string
1485 ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
1486 }
1487 else
1488 {
1489 list_T *l = text.vval.v_list;
1490
1491 if (l->lv_len > 0)
1492 {
1493 if (l->lv_first->li_tv.v_type == VAR_STRING)
1494 // list of strings
1495 add_popup_strings(buf, l);
1496 else
1497 // list of dictionaries
1498 add_popup_dicts(buf, l);
1499 }
1500 }
1501
1502 // delete the line that was in the empty buffer
1503 curbuf = buf;
1504 ml_delete(buf->b_ml.ml_line_count, FALSE);
1505 curbuf = curwin->w_buffer;
1506}
1507
1508/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001509 * Parse the 'previewpopup' or 'completepopup' option and apply the values to
1510 * window "wp" if it is not NULL.
Bram Moolenaar79648732019-07-18 21:43:07 +02001511 * Return FAIL if the parsing fails.
1512 */
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001513 static int
1514parse_popup_option(win_T *wp, int is_preview)
Bram Moolenaar79648732019-07-18 21:43:07 +02001515{
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001516 char_u *p =
1517#ifdef FEAT_QUICKFIX
1518 !is_preview ? p_cpp :
1519#endif
1520 p_pvp;
Bram Moolenaar79648732019-07-18 21:43:07 +02001521
Bram Moolenaar258cef52019-08-21 17:29:29 +02001522 if (wp != NULL)
1523 wp->w_popup_flags &= ~POPF_INFO_MENU;
1524
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001525 for ( ; *p != NUL; p += (*p == ',' ? 1 : 0))
Bram Moolenaar79648732019-07-18 21:43:07 +02001526 {
1527 char_u *e, *dig;
1528 char_u *s = p;
1529 int x;
1530
1531 e = vim_strchr(p, ':');
1532 if (e == NULL || e[1] == NUL)
1533 return FAIL;
1534
1535 p = vim_strchr(e, ',');
1536 if (p == NULL)
1537 p = e + STRLEN(e);
1538 dig = e + 1;
1539 x = getdigits(&dig);
Bram Moolenaar79648732019-07-18 21:43:07 +02001540
1541 if (STRNCMP(s, "height:", 7) == 0)
1542 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001543 if (dig != p)
1544 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001545 if (wp != NULL)
1546 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001547 if (is_preview)
1548 wp->w_minheight = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001549 wp->w_maxheight = x;
1550 }
1551 }
1552 else if (STRNCMP(s, "width:", 6) == 0)
1553 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001554 if (dig != p)
1555 return FAIL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001556 if (wp != NULL)
1557 {
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001558 if (is_preview)
1559 wp->w_minwidth = x;
Bram Moolenaar79648732019-07-18 21:43:07 +02001560 wp->w_maxwidth = x;
1561 }
1562 }
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001563 else if (STRNCMP(s, "highlight:", 10) == 0)
1564 {
1565 if (wp != NULL)
1566 {
1567 int c = *p;
1568
1569 *p = NUL;
1570 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
1571 s + 10, OPT_FREE|OPT_LOCAL, 0);
1572 *p = c;
1573 }
1574 }
Bram Moolenaarbd483b32019-08-21 15:13:41 +02001575 else if (STRNCMP(s, "border:", 7) == 0)
1576 {
1577 char_u *arg = s + 7;
1578 int on = STRNCMP(arg, "on", 2) == 0 && arg + 2 == p;
1579 int off = STRNCMP(arg, "off", 3) == 0 && arg + 3 == p;
1580 int i;
1581
1582 if (!on && !off)
1583 return FAIL;
1584 if (wp != NULL)
1585 {
1586 for (i = 0; i < 4; ++i)
1587 wp->w_popup_border[i] = on ? 1 : 0;
1588 if (off)
1589 // only show the X for close when there is a border
1590 wp->w_popup_close = POPCLOSE_NONE;
1591 }
1592 }
Bram Moolenaar258cef52019-08-21 17:29:29 +02001593 else if (STRNCMP(s, "align:", 6) == 0)
1594 {
1595 char_u *arg = s + 6;
1596 int item = STRNCMP(arg, "item", 4) == 0 && arg + 4 == p;
1597 int menu = STRNCMP(arg, "menu", 4) == 0 && arg + 4 == p;
1598
1599 if (!menu && !item)
1600 return FAIL;
1601 if (wp != NULL && menu)
1602 wp->w_popup_flags |= POPF_INFO_MENU;
1603 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001604 else
1605 return FAIL;
1606 }
1607 return OK;
1608}
1609
1610/*
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001611 * Parse the 'previewpopup' option and apply the values to window "wp" if it
1612 * is not NULL.
1613 * Return FAIL if the parsing fails.
1614 */
1615 int
1616parse_previewpopup(win_T *wp)
1617{
1618 return parse_popup_option(wp, TRUE);
1619}
1620
1621/*
1622 * Parse the 'completepopup' option and apply the values to window "wp" if it
1623 * is not NULL.
1624 * Return FAIL if the parsing fails.
1625 */
1626 int
1627parse_completepopup(win_T *wp)
1628{
1629 return parse_popup_option(wp, FALSE);
1630}
1631
1632/*
Bram Moolenaar79648732019-07-18 21:43:07 +02001633 * Set w_wantline and w_wantcol for the cursor position in the current window.
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001634 * Keep at least "width" columns from the right of the screen.
Bram Moolenaar79648732019-07-18 21:43:07 +02001635 */
1636 void
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001637popup_set_wantpos_cursor(win_T *wp, int width)
Bram Moolenaar79648732019-07-18 21:43:07 +02001638{
1639 setcursor_mayforce(TRUE);
1640 wp->w_wantline = curwin->w_winrow + curwin->w_wrow;
1641 if (wp->w_wantline == 0) // cursor in first line
1642 {
1643 wp->w_wantline = 2;
1644 wp->w_popup_pos = POPPOS_TOPLEFT;
1645 }
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001646
Bram Moolenaar79648732019-07-18 21:43:07 +02001647 wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001648 if (wp->w_wantcol > Columns - width)
1649 {
1650 wp->w_wantcol = Columns - width;
1651 if (wp->w_wantcol < 1)
1652 wp->w_wantcol = 1;
1653 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001654 popup_adjust_position(wp);
1655}
1656
1657/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001658 * Set w_wantline and w_wantcol for the a given screen position.
1659 * Caller must take care of running into the window border.
1660 */
1661 void
1662popup_set_wantpos_rowcol(win_T *wp, int row, int col)
1663{
1664 wp->w_wantline = row;
1665 wp->w_wantcol = col;
1666 popup_adjust_position(wp);
1667}
1668
1669/*
1670 * Add a border and lef&right padding.
1671 */
1672 static void
1673add_border_left_right_padding(win_T *wp)
1674{
1675 int i;
1676
1677 for (i = 0; i < 4; ++i)
1678 {
1679 wp->w_popup_border[i] = 1;
1680 wp->w_popup_padding[i] = (i & 1) ? 1 : 0;
1681 }
1682}
1683
1684/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001685 * popup_create({text}, {options})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001686 * popup_atcursor({text}, {options})
Bram Moolenaar79648732019-07-18 21:43:07 +02001687 * etc.
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001688 * When creating a preview or info popup "argvars" and "rettv" are NULL.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001689 */
Bram Moolenaara730e552019-06-16 19:05:31 +02001690 static win_T *
Bram Moolenaar17627312019-06-02 19:53:44 +02001691popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001692{
Bram Moolenaara3fce622019-06-20 02:31:49 +02001693 win_T *wp;
1694 tabpage_T *tp = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001695 int tabnr = 0;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001696 int new_buffer;
1697 buf_T *buf = NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001698 dict_T *d = NULL;
Bram Moolenaara3fce622019-06-20 02:31:49 +02001699 int nr;
1700 int i;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001701
Bram Moolenaar79648732019-07-18 21:43:07 +02001702 if (argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001703 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02001704 // Check that arguments look OK.
Bram Moolenaar79648732019-07-18 21:43:07 +02001705 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001706 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001707 buf = buflist_findnr( argvars[0].vval.v_number);
1708 if (buf == NULL)
1709 {
1710 semsg(_(e_nobufnr), argvars[0].vval.v_number);
1711 return NULL;
1712 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001713#ifdef FEAT_TERMINAL
Bram Moolenaare0d749a2019-09-25 22:14:48 +02001714 if (buf->b_term != NULL)
1715 {
1716 emsg(_("E278: Cannot put a terminal buffer in a popup window"));
1717 return NULL;
1718 }
Bram Moolenaarf21118e2019-09-25 22:45:45 +02001719#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001720 }
1721 else if (!(argvars[0].v_type == VAR_STRING
1722 && argvars[0].vval.v_string != NULL)
1723 && !(argvars[0].v_type == VAR_LIST
1724 && argvars[0].vval.v_list != NULL))
1725 {
1726 emsg(_(e_listreq));
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001727 return NULL;
1728 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001729 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02001730 {
Bram Moolenaar79648732019-07-18 21:43:07 +02001731 emsg(_(e_dictreq));
Bram Moolenaara3fce622019-06-20 02:31:49 +02001732 return NULL;
1733 }
Bram Moolenaar79648732019-07-18 21:43:07 +02001734 d = argvars[1].vval.v_dict;
1735 }
1736
1737 if (d != NULL)
1738 {
1739 if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
1740 tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
1741 else if (type == TYPE_NOTIFICATION)
1742 tabnr = -1; // notifications are global by default
1743 else
1744 tabnr = 0;
1745 if (tabnr > 0)
1746 {
1747 tp = find_tabpage(tabnr);
1748 if (tp == NULL)
1749 {
1750 semsg(_("E997: Tabpage not found: %d"), tabnr);
1751 return NULL;
1752 }
1753 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001754 }
1755
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001756 // Create the window and buffer.
1757 wp = win_alloc_popup_win();
1758 if (wp == NULL)
Bram Moolenaara730e552019-06-16 19:05:31 +02001759 return NULL;
Bram Moolenaar79648732019-07-18 21:43:07 +02001760 if (rettv != NULL)
1761 rettv->vval.v_number = wp->w_id;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001762 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaar638a4a72019-11-06 19:25:22 +01001763 wp->w_popup_flags = POPF_IS_POPUP | POPF_MAPPING | POPF_POSINVERT;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001764
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001765 if (buf != NULL)
1766 {
1767 // use existing buffer
1768 new_buffer = FALSE;
Bram Moolenaar7866b872019-07-01 22:21:01 +02001769 win_init_popup_win(wp, buf);
Bram Moolenaar46451042019-08-24 15:50:46 +02001770 set_local_options_default(wp, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001771 buffer_ensure_loaded(buf);
1772 }
1773 else
1774 {
1775 // create a new buffer associated with the popup
1776 new_buffer = TRUE;
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001777 buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_DUMMY|BLN_REUSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001778 if (buf == NULL)
1779 return NULL;
1780 ml_open(buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001781
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001782 win_init_popup_win(wp, buf);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001783
Bram Moolenaar46451042019-08-24 15:50:46 +02001784 set_local_options_default(wp, TRUE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001785 set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001786 (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001787 set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
Bram Moolenaar79648732019-07-18 21:43:07 +02001788 (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001789 buf->b_p_ul = -1; // no undo
1790 buf->b_p_swf = FALSE; // no swap file
1791 buf->b_p_bl = FALSE; // unlisted buffer
1792 buf->b_locked = TRUE;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02001793
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001794 // Avoid that 'buftype' is reset when this buffer is entered.
1795 buf->b_p_initialized = TRUE;
1796 }
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001797 wp->w_p_wrap = TRUE; // 'wrap' is default on
1798 wp->w_p_so = 0; // 'scrolloff' zero
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001799
Bram Moolenaara3fce622019-06-20 02:31:49 +02001800 if (tp != NULL)
1801 {
1802 // popup on specified tab page
1803 wp->w_next = tp->tp_first_popupwin;
1804 tp->tp_first_popupwin = wp;
1805 }
1806 else if (tabnr == 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001807 {
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02001808 // popup on current tab page
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001809 wp->w_next = curtab->tp_first_popupwin;
1810 curtab->tp_first_popupwin = wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001811 }
Bram Moolenaara3fce622019-06-20 02:31:49 +02001812 else // (tabnr < 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001813 {
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001814 win_T *prev = first_popupwin;
1815
1816 // Global popup: add at the end, so that it gets displayed on top of
1817 // older ones with the same zindex. Matters for notifications.
1818 if (first_popupwin == NULL)
1819 first_popupwin = wp;
1820 else
1821 {
1822 while (prev->w_next != NULL)
1823 prev = prev->w_next;
1824 prev->w_next = wp;
1825 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001826 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001827
Bram Moolenaar79648732019-07-18 21:43:07 +02001828 if (new_buffer && argvars != NULL)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001829 popup_set_buffer_text(buf, argvars[0]);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001830
Bram Moolenaar79648732019-07-18 21:43:07 +02001831 if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW)
Bram Moolenaar17627312019-06-02 19:53:44 +02001832 {
1833 wp->w_popup_pos = POPPOS_BOTLEFT;
Bram Moolenaar79648732019-07-18 21:43:07 +02001834 }
1835 if (type == TYPE_ATCURSOR)
1836 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001837 popup_set_wantpos_cursor(wp, 0);
Bram Moolenaar17627312019-06-02 19:53:44 +02001838 set_moved_values(wp);
1839 set_moved_columns(wp, FIND_STRING);
1840 }
1841
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001842 if (type == TYPE_BEVAL)
1843 {
1844 wp->w_popup_pos = POPPOS_BOTLEFT;
1845
1846 // by default use the mouse position
1847 wp->w_wantline = mouse_row;
1848 if (wp->w_wantline <= 0) // mouse on first line
1849 {
1850 wp->w_wantline = 2;
1851 wp->w_popup_pos = POPPOS_TOPLEFT;
1852 }
1853 wp->w_wantcol = mouse_col + 1;
1854 set_mousemoved_values(wp);
1855 set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL);
1856 }
1857
Bram Moolenaar33796b32019-06-08 16:01:13 +02001858 // set default values
1859 wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001860 wp->w_popup_close = POPCLOSE_NONE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001861
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001862 if (type == TYPE_NOTIFICATION)
1863 {
1864 win_T *twp, *nextwin;
1865 int height = buf->b_ml.ml_line_count + 3;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001866
1867 // Try to not overlap with another global popup. Guess we need 3
1868 // more screen lines than buffer lines.
1869 wp->w_wantline = 1;
1870 for (twp = first_popupwin; twp != NULL; twp = nextwin)
1871 {
1872 nextwin = twp->w_next;
1873 if (twp != wp
1874 && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
1875 && twp->w_winrow <= wp->w_wantline - 1 + height
1876 && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
1877 {
1878 // move to below this popup and restart the loop to check for
1879 // overlap with other popups
1880 wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
1881 nextwin = first_popupwin;
1882 }
1883 }
1884 if (wp->w_wantline + height > Rows)
1885 {
1886 // can't avoid overlap, put on top in the hope that message goes
1887 // away soon.
1888 wp->w_wantline = 1;
1889 }
1890
1891 wp->w_wantcol = 10;
1892 wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001893 wp->w_minwidth = 20;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001894 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02001895 wp->w_popup_close = POPCLOSE_CLICK;
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001896 for (i = 0; i < 4; ++i)
1897 wp->w_popup_border[i] = 1;
1898 wp->w_popup_padding[1] = 1;
1899 wp->w_popup_padding[3] = 1;
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001900
1901 nr = syn_name2id((char_u *)"PopupNotification");
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001902 set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001903 (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
1904 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001905 }
1906
Bram Moolenaara730e552019-06-16 19:05:31 +02001907 if (type == TYPE_DIALOG || type == TYPE_MENU)
Bram Moolenaara42d9452019-06-15 21:46:30 +02001908 {
Bram Moolenaara42d9452019-06-15 21:46:30 +02001909 wp->w_popup_pos = POPPOS_CENTER;
1910 wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001911 wp->w_popup_flags |= POPF_DRAG;
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001912 wp->w_popup_flags &= ~POPF_MAPPING;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001913 add_border_left_right_padding(wp);
Bram Moolenaara42d9452019-06-15 21:46:30 +02001914 }
1915
Bram Moolenaara730e552019-06-16 19:05:31 +02001916 if (type == TYPE_MENU)
1917 {
1918 typval_T tv;
1919 callback_T callback;
1920
1921 tv.v_type = VAR_STRING;
1922 tv.vval.v_string = (char_u *)"popup_filter_menu";
1923 callback = get_callback(&tv);
1924 if (callback.cb_name != NULL)
1925 set_callback(&wp->w_filter_cb, &callback);
1926
1927 wp->w_p_wrap = 0;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001928 wp->w_popup_flags |= POPF_CURSORLINE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001929 }
1930
Bram Moolenaar79648732019-07-18 21:43:07 +02001931 if (type == TYPE_PREVIEW)
1932 {
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02001933 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
Bram Moolenaar79648732019-07-18 21:43:07 +02001934 wp->w_popup_close = POPCLOSE_BUTTON;
1935 for (i = 0; i < 4; ++i)
1936 wp->w_popup_border[i] = 1;
1937 parse_previewpopup(wp);
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001938 popup_set_wantpos_cursor(wp, wp->w_minwidth);
1939 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001940# ifdef FEAT_QUICKFIX
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001941 if (type == TYPE_INFO)
1942 {
1943 wp->w_popup_pos = POPPOS_TOPLEFT;
1944 wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE;
1945 wp->w_popup_close = POPCLOSE_BUTTON;
1946 add_border_left_right_padding(wp);
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02001947 parse_completepopup(wp);
Bram Moolenaar79648732019-07-18 21:43:07 +02001948 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02001949# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02001950
Bram Moolenaarae943152019-06-16 22:54:14 +02001951 for (i = 0; i < 4; ++i)
1952 VIM_CLEAR(wp->w_border_highlight[i]);
1953 for (i = 0; i < 8; ++i)
1954 wp->w_border_char[i] = 0;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001955 wp->w_want_scrollbar = 1;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02001956 wp->w_popup_fixed = 0;
Bram Moolenaar581ba392019-09-03 22:08:33 +02001957 wp->w_filter_mode = MODE_ALL;
Bram Moolenaarae943152019-06-16 22:54:14 +02001958
Bram Moolenaar79648732019-07-18 21:43:07 +02001959 if (d != NULL)
1960 // Deal with options.
1961 apply_options(wp, d);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001962
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001963#ifdef FEAT_TIMERS
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001964 if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
1965 popup_add_timeout(wp, 3000);
Bram Moolenaar0fcf26b2019-06-23 01:03:51 +02001966#endif
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001967
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001968 popup_adjust_position(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001969
1970 wp->w_vsep_width = 0;
1971
1972 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02001973 popup_mask_refresh = TRUE;
Bram Moolenaara730e552019-06-16 19:05:31 +02001974
1975 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001976}
1977
1978/*
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001979 * popup_clear()
1980 */
1981 void
1982f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1983{
1984 close_all_popups();
1985}
1986
1987/*
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001988 * popup_create({text}, {options})
1989 */
1990 void
1991f_popup_create(typval_T *argvars, typval_T *rettv)
1992{
Bram Moolenaar17627312019-06-02 19:53:44 +02001993 popup_create(argvars, rettv, TYPE_NORMAL);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001994}
1995
1996/*
1997 * popup_atcursor({text}, {options})
1998 */
1999 void
2000f_popup_atcursor(typval_T *argvars, typval_T *rettv)
2001{
Bram Moolenaar17627312019-06-02 19:53:44 +02002002 popup_create(argvars, rettv, TYPE_ATCURSOR);
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02002003}
2004
2005/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002006 * popup_beval({text}, {options})
2007 */
2008 void
2009f_popup_beval(typval_T *argvars, typval_T *rettv)
2010{
2011 popup_create(argvars, rettv, TYPE_BEVAL);
2012}
2013
2014/*
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002015 * Invoke the close callback for window "wp" with value "result".
2016 * Careful: The callback may make "wp" invalid!
2017 */
2018 static void
2019invoke_popup_callback(win_T *wp, typval_T *result)
2020{
2021 typval_T rettv;
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002022 typval_T argv[3];
2023
2024 argv[0].v_type = VAR_NUMBER;
2025 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2026
2027 if (result != NULL && result->v_type != VAR_UNKNOWN)
2028 copy_tv(result, &argv[1]);
2029 else
2030 {
2031 argv[1].v_type = VAR_NUMBER;
2032 argv[1].vval.v_number = 0;
2033 }
2034
2035 argv[2].v_type = VAR_UNKNOWN;
2036
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002037 call_callback(&wp->w_close_cb, -1, &rettv, 2, argv);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002038 if (result != NULL)
2039 clear_tv(&argv[1]);
2040 clear_tv(&rettv);
2041}
2042
2043/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002044 * Close popup "wp" and invoke any close callback for it.
2045 */
2046 static void
2047popup_close_and_callback(win_T *wp, typval_T *arg)
2048{
2049 int id = wp->w_id;
2050
2051 if (wp->w_close_cb.cb_name != NULL)
2052 // Careful: This may make "wp" invalid.
2053 invoke_popup_callback(wp, arg);
2054
2055 popup_close(id);
2056}
2057
Bram Moolenaar12034e22019-08-25 22:25:02 +02002058 static void
2059popup_close_with_retval(win_T *wp, int retval)
2060{
2061 typval_T res;
2062
2063 res.v_type = VAR_NUMBER;
2064 res.vval.v_number = retval;
2065 popup_close_and_callback(wp, &res);
2066}
2067
Bram Moolenaar3397f742019-06-02 18:40:06 +02002068/*
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002069 * Close popup "wp" because of a mouse click.
2070 */
2071 void
2072popup_close_for_mouse_click(win_T *wp)
2073{
Bram Moolenaar12034e22019-08-25 22:25:02 +02002074 popup_close_with_retval(wp, -2);
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002075}
2076
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002077 static void
2078check_mouse_moved(win_T *wp, win_T *mouse_wp)
2079{
2080 // Close the popup when all if these are true:
2081 // - the mouse is not on this popup
2082 // - "mousemoved" was used
2083 // - the mouse is no longer on the same screen row or the mouse column is
2084 // outside of the relevant text
2085 if (wp != mouse_wp
2086 && wp->w_popup_mouse_row != 0
2087 && (wp->w_popup_mouse_row != mouse_row
2088 || mouse_col < wp->w_popup_mouse_mincol
2089 || mouse_col > wp->w_popup_mouse_maxcol))
2090 {
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002091 // Careful: this makes "wp" invalid.
Bram Moolenaar12034e22019-08-25 22:25:02 +02002092 popup_close_with_retval(wp, -2);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002093 }
2094}
2095
2096/*
2097 * Called when the mouse moved: may close a popup with "mousemoved".
2098 */
2099 void
2100popup_handle_mouse_moved(void)
2101{
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002102 win_T *wp, *nextwp;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002103 win_T *mouse_wp;
2104 int row = mouse_row;
2105 int col = mouse_col;
2106
2107 // find the window where the mouse is in
2108 mouse_wp = mouse_find_win(&row, &col, FIND_POPUP);
2109
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002110 for (wp = first_popupwin; wp != NULL; wp = nextwp)
2111 {
2112 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002113 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002114 }
2115 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp)
2116 {
2117 nextwp = wp->w_next;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002118 check_mouse_moved(wp, mouse_wp);
Bram Moolenaar3e35d052019-07-07 20:43:34 +02002119 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002120}
2121
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002122/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002123 * In a filter: check if the typed key is a mouse event that is used for
2124 * dragging the popup.
2125 */
2126 static void
2127filter_handle_drag(win_T *wp, int c, typval_T *rettv)
2128{
2129 int row = mouse_row;
2130 int col = mouse_col;
2131
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002132 if ((wp->w_popup_flags & POPF_DRAG)
Bram Moolenaara730e552019-06-16 19:05:31 +02002133 && is_mouse_key(c)
2134 && (wp == popup_dragwin
2135 || wp == mouse_find_win(&row, &col, FIND_POPUP)))
2136 // do not consume the key, allow for dragging the popup
2137 rettv->vval.v_number = 0;
2138}
2139
Bram Moolenaara730e552019-06-16 19:05:31 +02002140/*
2141 * popup_filter_menu({text}, {options})
2142 */
2143 void
2144f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
2145{
2146 int id = tv_get_number(&argvars[0]);
2147 win_T *wp = win_id2wp(id);
2148 char_u *key = tv_get_string(&argvars[1]);
2149 typval_T res;
2150 int c;
2151 linenr_T old_lnum;
2152
2153 // If the popup has been closed do not consume the key.
2154 if (wp == NULL)
2155 return;
2156
2157 c = *key;
2158 if (c == K_SPECIAL && key[1] != NUL)
2159 c = TO_SPECIAL(key[1], key[2]);
2160
2161 // consume all keys until done
2162 rettv->vval.v_number = 1;
2163 res.v_type = VAR_NUMBER;
2164
2165 old_lnum = wp->w_cursor.lnum;
2166 if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
2167 --wp->w_cursor.lnum;
2168 if ((c == 'j' || c == 'J' || c == K_DOWN)
2169 && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
2170 ++wp->w_cursor.lnum;
2171 if (old_lnum != wp->w_cursor.lnum)
2172 {
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002173 // caller will call popup_highlight_curline()
Bram Moolenaara730e552019-06-16 19:05:31 +02002174 return;
2175 }
2176
2177 if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
2178 {
2179 // Cancelled, invoke callback with -1
2180 res.vval.v_number = -1;
2181 popup_close_and_callback(wp, &res);
2182 return;
2183 }
2184 if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
2185 {
2186 // Invoke callback with current index.
2187 res.vval.v_number = wp->w_cursor.lnum;
2188 popup_close_and_callback(wp, &res);
2189 return;
2190 }
2191
2192 filter_handle_drag(wp, c, rettv);
2193}
2194
2195/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002196 * popup_filter_yesno({text}, {options})
2197 */
2198 void
2199f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
2200{
2201 int id = tv_get_number(&argvars[0]);
2202 win_T *wp = win_id2wp(id);
2203 char_u *key = tv_get_string(&argvars[1]);
2204 typval_T res;
Bram Moolenaara730e552019-06-16 19:05:31 +02002205 int c;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002206
2207 // If the popup has been closed don't consume the key.
2208 if (wp == NULL)
2209 return;
2210
Bram Moolenaara730e552019-06-16 19:05:31 +02002211 c = *key;
2212 if (c == K_SPECIAL && key[1] != NUL)
2213 c = TO_SPECIAL(key[1], key[2]);
2214
Bram Moolenaara42d9452019-06-15 21:46:30 +02002215 // consume all keys until done
2216 rettv->vval.v_number = 1;
2217
Bram Moolenaara730e552019-06-16 19:05:31 +02002218 if (c == 'y' || c == 'Y')
Bram Moolenaara42d9452019-06-15 21:46:30 +02002219 res.vval.v_number = 1;
Bram Moolenaara730e552019-06-16 19:05:31 +02002220 else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
Bram Moolenaara42d9452019-06-15 21:46:30 +02002221 res.vval.v_number = 0;
2222 else
2223 {
Bram Moolenaara730e552019-06-16 19:05:31 +02002224 filter_handle_drag(wp, c, rettv);
Bram Moolenaara42d9452019-06-15 21:46:30 +02002225 return;
2226 }
2227
2228 // Invoke callback
2229 res.v_type = VAR_NUMBER;
2230 popup_close_and_callback(wp, &res);
2231}
2232
2233/*
2234 * popup_dialog({text}, {options})
2235 */
2236 void
2237f_popup_dialog(typval_T *argvars, typval_T *rettv)
2238{
2239 popup_create(argvars, rettv, TYPE_DIALOG);
2240}
2241
2242/*
Bram Moolenaara730e552019-06-16 19:05:31 +02002243 * popup_menu({text}, {options})
2244 */
2245 void
2246f_popup_menu(typval_T *argvars, typval_T *rettv)
2247{
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002248 popup_create(argvars, rettv, TYPE_MENU);
Bram Moolenaara730e552019-06-16 19:05:31 +02002249}
2250
2251/*
Bram Moolenaara42d9452019-06-15 21:46:30 +02002252 * popup_notification({text}, {options})
2253 */
2254 void
2255f_popup_notification(typval_T *argvars, typval_T *rettv)
2256{
2257 popup_create(argvars, rettv, TYPE_NOTIFICATION);
2258}
2259
2260/*
2261 * Find the popup window with window-ID "id".
2262 * If the popup window does not exist NULL is returned.
2263 * If the window is not a popup window, and error message is given.
2264 */
2265 static win_T *
2266find_popup_win(int id)
2267{
2268 win_T *wp = win_id2wp(id);
2269
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002270 if (wp != NULL && !WIN_IS_POPUP(wp))
Bram Moolenaara42d9452019-06-15 21:46:30 +02002271 {
2272 semsg(_("E993: window %d is not a popup window"), id);
2273 return NULL;
2274 }
2275 return wp;
2276}
2277
2278/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002279 * popup_close({id})
2280 */
2281 void
2282f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
2283{
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002284 int id = (int)tv_get_number(argvars);
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002285 win_T *wp = find_popup_win(id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002286
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002287 if (wp != NULL)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002288 popup_close_and_callback(wp, &argvars[1]);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002289}
2290
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002291 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002292popup_hide(win_T *wp)
2293{
2294 if ((wp->w_popup_flags & POPF_HIDDEN) == 0)
2295 {
2296 wp->w_popup_flags |= POPF_HIDDEN;
2297 --wp->w_buffer->b_nwindows;
2298 redraw_all_later(NOT_VALID);
2299 popup_mask_refresh = TRUE;
2300 }
2301}
2302
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002303/*
2304 * popup_hide({id})
2305 */
2306 void
2307f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
2308{
2309 int id = (int)tv_get_number(argvars);
2310 win_T *wp = find_popup_win(id);
2311
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002312 if (wp != NULL)
2313 popup_hide(wp);
2314}
2315
2316 void
2317popup_show(win_T *wp)
2318{
2319 if ((wp->w_popup_flags & POPF_HIDDEN) != 0)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002320 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002321 wp->w_popup_flags &= ~POPF_HIDDEN;
2322 ++wp->w_buffer->b_nwindows;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002323 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002324 popup_mask_refresh = TRUE;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002325 }
2326}
2327
2328/*
2329 * popup_show({id})
2330 */
2331 void
2332f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
2333{
2334 int id = (int)tv_get_number(argvars);
2335 win_T *wp = find_popup_win(id);
2336
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002337 if (wp != NULL)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002338 {
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002339 popup_show(wp);
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002340 if (wp->w_popup_flags & POPF_INFO)
2341 pum_position_info_popup(wp);
2342 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002343}
2344
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002345/*
2346 * popup_settext({id}, {text})
2347 */
2348 void
2349f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
2350{
2351 int id = (int)tv_get_number(&argvars[0]);
2352 win_T *wp = find_popup_win(id);
2353
2354 if (wp != NULL)
2355 {
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002356 if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST)
2357 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2358 else
2359 {
2360 popup_set_buffer_text(wp->w_buffer, argvars[1]);
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002361 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002362 popup_adjust_position(wp);
2363 }
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002364 }
2365}
2366
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002367 static void
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002368popup_free(win_T *wp)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002369{
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002370 sign_undefine_by_name(popup_get_sign_name(wp), FALSE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002371 wp->w_buffer->b_locked = FALSE;
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002372 if (wp->w_winrow + popup_height(wp) >= cmdline_row)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002373 clear_cmdline = TRUE;
2374 win_free_popup(wp);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002375
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002376 redraw_all_later(NOT_VALID);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002377 popup_mask_refresh = TRUE;
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02002378}
2379
Bram Moolenaarec583842019-05-26 14:11:23 +02002380/*
2381 * Close a popup window by Window-id.
Bram Moolenaar9eaac892019-06-01 22:49:29 +02002382 * Does not invoke the callback.
Bram Moolenaarec583842019-05-26 14:11:23 +02002383 */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002384 void
Bram Moolenaarec583842019-05-26 14:11:23 +02002385popup_close(int id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002386{
2387 win_T *wp;
Bram Moolenaarec583842019-05-26 14:11:23 +02002388 tabpage_T *tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002389 win_T *prev = NULL;
2390
Bram Moolenaarec583842019-05-26 14:11:23 +02002391 // go through global popups
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002392 for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
Bram Moolenaarec583842019-05-26 14:11:23 +02002393 if (wp->w_id == id)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002394 {
2395 if (prev == NULL)
2396 first_popupwin = wp->w_next;
2397 else
2398 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002399 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002400 return;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002401 }
2402
Bram Moolenaarec583842019-05-26 14:11:23 +02002403 // go through tab-local popups
2404 FOR_ALL_TABPAGES(tp)
2405 popup_close_tabpage(tp, id);
2406}
2407
2408/*
2409 * Close a popup window with Window-id "id" in tabpage "tp".
2410 */
2411 void
2412popup_close_tabpage(tabpage_T *tp, int id)
2413{
2414 win_T *wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002415 win_T **root = &tp->tp_first_popupwin;
Bram Moolenaarec583842019-05-26 14:11:23 +02002416 win_T *prev = NULL;
2417
Bram Moolenaarec583842019-05-26 14:11:23 +02002418 for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
2419 if (wp->w_id == id)
2420 {
2421 if (prev == NULL)
2422 *root = wp->w_next;
2423 else
2424 prev->w_next = wp->w_next;
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02002425 popup_free(wp);
Bram Moolenaarec583842019-05-26 14:11:23 +02002426 return;
2427 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002428}
2429
2430 void
2431close_all_popups(void)
2432{
2433 while (first_popupwin != NULL)
2434 popup_close(first_popupwin->w_id);
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02002435 while (curtab->tp_first_popupwin != NULL)
2436 popup_close(curtab->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002437}
2438
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002439/*
2440 * popup_move({id}, {options})
2441 */
2442 void
2443f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
2444{
Bram Moolenaarae943152019-06-16 22:54:14 +02002445 dict_T *dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002446 int id = (int)tv_get_number(argvars);
2447 win_T *wp = find_popup_win(id);
2448
2449 if (wp == NULL)
2450 return; // invalid {id}
2451
2452 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2453 {
2454 emsg(_(e_dictreq));
2455 return;
2456 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002457 dict = argvars[1].vval.v_dict;
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002458
Bram Moolenaarae943152019-06-16 22:54:14 +02002459 apply_move_options(wp, dict);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002460
2461 if (wp->w_winrow + wp->w_height >= cmdline_row)
2462 clear_cmdline = TRUE;
2463 popup_adjust_position(wp);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002464}
2465
Bram Moolenaarbc133542019-05-29 20:26:46 +02002466/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002467 * popup_setoptions({id}, {options})
2468 */
2469 void
2470f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2471{
2472 dict_T *dict;
2473 int id = (int)tv_get_number(argvars);
2474 win_T *wp = find_popup_win(id);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002475 linenr_T old_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002476
2477 if (wp == NULL)
2478 return; // invalid {id}
2479
2480 if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
2481 {
2482 emsg(_(e_dictreq));
2483 return;
2484 }
2485 dict = argvars[1].vval.v_dict;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002486 old_firstline = wp->w_firstline;
Bram Moolenaarae943152019-06-16 22:54:14 +02002487
2488 apply_move_options(wp, dict);
2489 apply_general_options(wp, dict);
2490
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002491 if (old_firstline != wp->w_firstline)
2492 redraw_win_later(wp, NOT_VALID);
Bram Moolenaarad24a712019-06-17 20:05:45 +02002493 popup_mask_refresh = TRUE;
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002494 popup_highlight_curline(wp);
Bram Moolenaarae943152019-06-16 22:54:14 +02002495 popup_adjust_position(wp);
2496}
2497
2498/*
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002499 * popup_getpos({id})
Bram Moolenaarbc133542019-05-29 20:26:46 +02002500 */
2501 void
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002502f_popup_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaarbc133542019-05-29 20:26:46 +02002503{
2504 dict_T *dict;
2505 int id = (int)tv_get_number(argvars);
2506 win_T *wp = find_popup_win(id);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002507 int top_extra;
2508 int left_extra;
Bram Moolenaarbc133542019-05-29 20:26:46 +02002509
2510 if (rettv_dict_alloc(rettv) == OK)
2511 {
2512 if (wp == NULL)
2513 return; // invalid {id}
Bram Moolenaareb2310d2019-06-16 20:09:10 +02002514 top_extra = popup_top_extra(wp);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002515 left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
2516
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002517 // we know how much space we need, avoid resizing halfway
Bram Moolenaarbc133542019-05-29 20:26:46 +02002518 dict = rettv->vval.v_dict;
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002519 hash_lock_size(&dict->dv_hashtab, 11);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002520
Bram Moolenaarbc133542019-05-29 20:26:46 +02002521 dict_add_number(dict, "line", wp->w_winrow + 1);
2522 dict_add_number(dict, "col", wp->w_wincol + 1);
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002523 dict_add_number(dict, "width", wp->w_width + left_extra
2524 + wp->w_popup_border[1] + wp->w_popup_padding[1]);
2525 dict_add_number(dict, "height", wp->w_height + top_extra
2526 + wp->w_popup_border[2] + wp->w_popup_padding[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02002527
2528 dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
2529 dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
2530 dict_add_number(dict, "core_width", wp->w_width);
2531 dict_add_number(dict, "core_height", wp->w_height);
2532
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002533 dict_add_number(dict, "scrollbar", wp->w_has_scrollbar);
Bram Moolenaar68acb412019-06-26 03:40:36 +02002534 dict_add_number(dict, "firstline", wp->w_topline);
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002535 dict_add_number(dict, "lastline", wp->w_botline - 1);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002536 dict_add_number(dict, "visible",
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02002537 win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
Bram Moolenaar7b73d7e2019-07-26 21:26:34 +02002538
2539 hash_unlock(&dict->dv_hashtab);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002540 }
2541}
Bram Moolenaarb4f06282019-07-12 21:07:54 +02002542/*
2543 * popup_locate({row}, {col})
2544 */
2545 void
2546f_popup_locate(typval_T *argvars, typval_T *rettv)
2547{
2548 int row = tv_get_number(&argvars[0]) - 1;
2549 int col = tv_get_number(&argvars[1]) - 1;
2550 win_T *wp;
2551
2552 wp = mouse_find_win(&row, &col, FIND_POPUP);
2553 if (WIN_IS_POPUP(wp))
2554 rettv->vval.v_number = wp->w_id;
2555}
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002556
2557/*
Bram Moolenaarae943152019-06-16 22:54:14 +02002558 * For popup_getoptions(): add a "border" or "padding" entry to "dict".
2559 */
2560 static void
2561get_padding_border(dict_T *dict, int *array, char *name)
2562{
2563 list_T *list;
2564 int i;
2565
2566 if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
2567 return;
2568
2569 list = list_alloc();
2570 if (list != NULL)
2571 {
2572 dict_add_list(dict, name, list);
2573 if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
2574 for (i = 0; i < 4; ++i)
2575 list_append_number(list, array[i]);
2576 }
2577}
2578
2579/*
2580 * For popup_getoptions(): add a "borderhighlight" entry to "dict".
2581 */
2582 static void
2583get_borderhighlight(dict_T *dict, win_T *wp)
2584{
2585 list_T *list;
2586 int i;
2587
2588 for (i = 0; i < 4; ++i)
2589 if (wp->w_border_highlight[i] != NULL)
2590 break;
2591 if (i == 4)
2592 return;
2593
2594 list = list_alloc();
2595 if (list != NULL)
2596 {
2597 dict_add_list(dict, "borderhighlight", list);
2598 for (i = 0; i < 4; ++i)
2599 list_append_string(list, wp->w_border_highlight[i], -1);
2600 }
2601}
2602
2603/*
2604 * For popup_getoptions(): add a "borderchars" entry to "dict".
2605 */
2606 static void
2607get_borderchars(dict_T *dict, win_T *wp)
2608{
2609 list_T *list;
2610 int i;
2611 char_u buf[NUMBUFLEN];
2612 int len;
2613
2614 for (i = 0; i < 8; ++i)
2615 if (wp->w_border_char[i] != 0)
2616 break;
2617 if (i == 8)
2618 return;
2619
2620 list = list_alloc();
2621 if (list != NULL)
2622 {
2623 dict_add_list(dict, "borderchars", list);
2624 for (i = 0; i < 8; ++i)
2625 {
2626 len = mb_char2bytes(wp->w_border_char[i], buf);
2627 list_append_string(list, buf, len);
2628 }
2629 }
2630}
2631
2632/*
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002633 * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict".
Bram Moolenaarae943152019-06-16 22:54:14 +02002634 */
2635 static void
2636get_moved_list(dict_T *dict, win_T *wp)
2637{
2638 list_T *list;
2639
2640 list = list_alloc();
2641 if (list != NULL)
2642 {
2643 dict_add_list(dict, "moved", list);
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002644 list_append_number(list, wp->w_popup_lnum);
Bram Moolenaarae943152019-06-16 22:54:14 +02002645 list_append_number(list, wp->w_popup_mincol);
2646 list_append_number(list, wp->w_popup_maxcol);
2647 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002648 list = list_alloc();
2649 if (list != NULL)
2650 {
2651 dict_add_list(dict, "mousemoved", list);
2652 list_append_number(list, wp->w_popup_mouse_row);
2653 list_append_number(list, wp->w_popup_mouse_mincol);
2654 list_append_number(list, wp->w_popup_mouse_maxcol);
2655 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002656}
2657
2658/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02002659 * popup_getoptions({id})
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002660 */
2661 void
2662f_popup_getoptions(typval_T *argvars, typval_T *rettv)
2663{
2664 dict_T *dict;
2665 int id = (int)tv_get_number(argvars);
2666 win_T *wp = find_popup_win(id);
Bram Moolenaara3fce622019-06-20 02:31:49 +02002667 tabpage_T *tp;
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002668 int i;
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002669
2670 if (rettv_dict_alloc(rettv) == OK)
2671 {
2672 if (wp == NULL)
2673 return;
2674
2675 dict = rettv->vval.v_dict;
2676 dict_add_number(dict, "line", wp->w_wantline);
2677 dict_add_number(dict, "col", wp->w_wantcol);
2678 dict_add_number(dict, "minwidth", wp->w_minwidth);
2679 dict_add_number(dict, "minheight", wp->w_minheight);
2680 dict_add_number(dict, "maxheight", wp->w_maxheight);
2681 dict_add_number(dict, "maxwidth", wp->w_maxwidth);
Bram Moolenaar8d241042019-06-12 23:40:01 +02002682 dict_add_number(dict, "firstline", wp->w_firstline);
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002683 dict_add_number(dict, "scrollbar", wp->w_want_scrollbar);
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002684 dict_add_number(dict, "zindex", wp->w_zindex);
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002685 dict_add_number(dict, "fixed", wp->w_popup_fixed);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002686 if (wp->w_popup_prop_type && win_valid(wp->w_popup_prop_win))
2687 {
2688 proptype_T *pt = text_prop_type_by_id(
2689 wp->w_popup_prop_win->w_buffer,
2690 wp->w_popup_prop_type);
2691
2692 if (pt != NULL)
2693 dict_add_string(dict, "textprop", pt->pt_name);
2694 dict_add_number(dict, "textpropwin", wp->w_popup_prop_win->w_id);
2695 dict_add_number(dict, "textpropid", wp->w_popup_prop_id);
2696 }
Bram Moolenaarae943152019-06-16 22:54:14 +02002697 dict_add_string(dict, "title", wp->w_popup_title);
2698 dict_add_number(dict, "wrap", wp->w_p_wrap);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002699 dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0);
Bram Moolenaar12034e22019-08-25 22:25:02 +02002700 dict_add_number(dict, "mapping",
2701 (wp->w_popup_flags & POPF_MAPPING) != 0);
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02002702 dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0);
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002703 dict_add_number(dict, "posinvert",
2704 (wp->w_popup_flags & POPF_POSINVERT) != 0);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002705 dict_add_number(dict, "cursorline",
2706 (wp->w_popup_flags & POPF_CURSORLINE) != 0);
Bram Moolenaarae943152019-06-16 22:54:14 +02002707 dict_add_string(dict, "highlight", wp->w_p_wcr);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02002708 if (wp->w_scrollbar_highlight != NULL)
2709 dict_add_string(dict, "scrollbarhighlight",
2710 wp->w_scrollbar_highlight);
2711 if (wp->w_thumb_highlight != NULL)
2712 dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight);
Bram Moolenaarae943152019-06-16 22:54:14 +02002713
Bram Moolenaara3fce622019-06-20 02:31:49 +02002714 // find the tabpage that holds this popup
2715 i = 1;
2716 FOR_ALL_TABPAGES(tp)
2717 {
Bram Moolenaar1824f452019-10-02 23:06:46 +02002718 win_T *twp;
Bram Moolenaara3fce622019-06-20 02:31:49 +02002719
Bram Moolenaar1824f452019-10-02 23:06:46 +02002720 for (twp = tp->tp_first_popupwin; twp != NULL; twp = twp->w_next)
2721 if (twp->w_id == id)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002722 break;
Bram Moolenaar1824f452019-10-02 23:06:46 +02002723 if (twp != NULL)
Bram Moolenaara3fce622019-06-20 02:31:49 +02002724 break;
2725 ++i;
2726 }
2727 if (tp == NULL)
2728 i = -1; // must be global
2729 else if (tp == curtab)
2730 i = 0;
2731 dict_add_number(dict, "tabpage", i);
2732
Bram Moolenaarae943152019-06-16 22:54:14 +02002733 get_padding_border(dict, wp->w_popup_padding, "padding");
2734 get_padding_border(dict, wp->w_popup_border, "border");
2735 get_borderhighlight(dict, wp);
2736 get_borderchars(dict, wp);
2737 get_moved_list(dict, wp);
2738
2739 if (wp->w_filter_cb.cb_name != NULL)
2740 dict_add_callback(dict, "filter", &wp->w_filter_cb);
2741 if (wp->w_close_cb.cb_name != NULL)
2742 dict_add_callback(dict, "callback", &wp->w_close_cb);
Bram Moolenaarac1f1bc2019-05-30 21:24:26 +02002743
2744 for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
2745 ++i)
2746 if (wp->w_popup_pos == poppos_entries[i].pp_val)
2747 {
2748 dict_add_string(dict, "pos",
2749 (char_u *)poppos_entries[i].pp_name);
2750 break;
2751 }
2752
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002753 dict_add_string(dict, "close", (char_u *)(
2754 wp->w_popup_close == POPCLOSE_BUTTON ? "button"
2755 : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none"));
2756
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02002757# if defined(FEAT_TIMERS)
2758 dict_add_number(dict, "time", wp->w_popup_timer != NULL
2759 ? (long)wp->w_popup_timer->tr_interval : 0L);
2760# endif
Bram Moolenaarbc133542019-05-29 20:26:46 +02002761 }
2762}
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002763
2764 int
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002765error_if_popup_window()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002766{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002767 if (WIN_IS_POPUP(curwin))
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002768 {
2769 emsg(_("E994: Not allowed in a popup window"));
2770 return TRUE;
2771 }
2772 return FALSE;
2773}
2774
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002775/*
2776 * Reset all the POPF_HANDLED flags in global popup windows and popup windows
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +02002777 * in the current tab page.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002778 */
2779 void
2780popup_reset_handled()
2781{
2782 win_T *wp;
2783
2784 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2785 wp->w_popup_flags &= ~POPF_HANDLED;
2786 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2787 wp->w_popup_flags &= ~POPF_HANDLED;
2788}
2789
2790/*
2791 * Find the next visible popup where POPF_HANDLED is not set.
2792 * Must have called popup_reset_handled() first.
2793 * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
2794 * popup with the highest zindex.
2795 */
2796 win_T *
2797find_next_popup(int lowest)
2798{
2799 win_T *wp;
2800 win_T *found_wp;
2801 int found_zindex;
2802
2803 found_zindex = lowest ? INT_MAX : 0;
2804 found_wp = NULL;
2805 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
2806 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2807 && (lowest ? wp->w_zindex < found_zindex
2808 : wp->w_zindex > found_zindex))
2809 {
2810 found_zindex = wp->w_zindex;
2811 found_wp = wp;
2812 }
2813 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
2814 if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
2815 && (lowest ? wp->w_zindex < found_zindex
2816 : wp->w_zindex > found_zindex))
2817 {
2818 found_zindex = wp->w_zindex;
2819 found_wp = wp;
2820 }
2821
2822 if (found_wp != NULL)
2823 found_wp->w_popup_flags |= POPF_HANDLED;
2824 return found_wp;
2825}
2826
2827/*
2828 * Invoke the filter callback for window "wp" with typed character "c".
2829 * Uses the global "mod_mask" for modifiers.
2830 * Returns the return value of the filter.
2831 * Careful: The filter may make "wp" invalid!
2832 */
2833 static int
2834invoke_popup_filter(win_T *wp, int c)
2835{
2836 int res;
2837 typval_T rettv;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002838 typval_T argv[3];
2839 char_u buf[NUMBUFLEN];
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002840 linenr_T old_lnum = wp->w_cursor.lnum;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002841
Bram Moolenaara42d9452019-06-15 21:46:30 +02002842 // Emergency exit: CTRL-C closes the popup.
2843 if (c == Ctrl_C)
2844 {
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002845 int save_got_int = got_int;
2846
2847 // Reset got_int to avoid the callback isn't called.
2848 got_int = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02002849 popup_close_with_retval(wp, -1);
Bram Moolenaarfd00c042019-10-05 11:56:54 +02002850 got_int |= save_got_int;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002851 return 1;
2852 }
2853
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002854 argv[0].v_type = VAR_NUMBER;
2855 argv[0].vval.v_number = (varnumber_T)wp->w_id;
2856
2857 // Convert the number to a string, so that the function can use:
2858 // if a:c == "\<F2>"
2859 buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
2860 argv[1].v_type = VAR_STRING;
2861 argv[1].vval.v_string = vim_strsave(buf);
2862
2863 argv[2].v_type = VAR_UNKNOWN;
2864
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002865 if (is_mouse_key(c))
2866 {
2867 int row = mouse_row - wp->w_winrow;
2868 int col = mouse_col - wp->w_wincol;
2869 linenr_T lnum;
2870
2871 if (row >= 0 && col >= 0)
2872 {
2873 (void)mouse_comp_pos(wp, &row, &col, &lnum, NULL);
2874 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2875 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2876 }
2877 }
2878
Bram Moolenaar638a4a72019-11-06 19:25:22 +01002879 // NOTE: The callback might close the popup and make "wp" invalid.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002880 call_callback(&wp->w_filter_cb, -1, &rettv, 2, argv);
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02002881 if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002882 popup_highlight_curline(wp);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002883 res = tv_get_number(&rettv);
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002884
2885 if (is_mouse_key(c))
2886 {
2887 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2888 set_vim_var_nr(VV_MOUSE_COL, 0);
2889 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002890 vim_free(argv[1].vval.v_string);
2891 clear_tv(&rettv);
2892 return res;
2893}
2894
2895/*
2896 * Called when "c" was typed: invoke popup filter callbacks.
2897 * Returns TRUE when the character was consumed,
2898 */
2899 int
2900popup_do_filter(int c)
2901{
Bram Moolenaar934470e2019-09-01 23:27:05 +02002902 static int recursive = FALSE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002903 int res = FALSE;
Bram Moolenaara42d9452019-06-15 21:46:30 +02002904 win_T *wp;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002905 int save_KeyTyped = KeyTyped;
Bram Moolenaar581ba392019-09-03 22:08:33 +02002906 int state;
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002907 int was_must_redraw = must_redraw;
Bram Moolenaar934470e2019-09-01 23:27:05 +02002908
2909 if (recursive)
2910 return FALSE;
2911 recursive = TRUE;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002912
Bram Moolenaarf6396232019-08-24 19:36:00 +02002913 if (c == K_LEFTMOUSE)
2914 {
2915 int row = mouse_row;
2916 int col = mouse_col;
2917
2918 wp = mouse_find_win(&row, &col, FIND_POPUP);
2919 if (wp != NULL && popup_close_if_on_X(wp, row, col))
Bram Moolenaar934470e2019-09-01 23:27:05 +02002920 res = TRUE;
Bram Moolenaarf6396232019-08-24 19:36:00 +02002921 }
2922
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002923 popup_reset_handled();
Bram Moolenaar581ba392019-09-03 22:08:33 +02002924 state = get_real_state();
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002925 while (!res && (wp = find_next_popup(FALSE)) != NULL)
Bram Moolenaar581ba392019-09-03 22:08:33 +02002926 if (wp->w_filter_cb.cb_name != NULL
2927 && (wp->w_filter_mode & state) != 0)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002928 res = invoke_popup_filter(wp, c);
2929
Bram Moolenaarfbb3bc82019-09-07 14:33:36 +02002930 if (must_redraw > was_must_redraw)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002931 redraw_after_callback(FALSE);
Bram Moolenaar934470e2019-09-01 23:27:05 +02002932 recursive = FALSE;
2933 KeyTyped = save_KeyTyped;
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002934 return res;
2935}
2936
Bram Moolenaar3397f742019-06-02 18:40:06 +02002937/*
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002938 * Return TRUE if there is a popup visible with a filter callback and the
2939 * "mapping" property off.
2940 */
2941 int
2942popup_no_mapping(void)
2943{
2944 int round;
2945 win_T *wp;
2946
2947 for (round = 1; round <= 2; ++round)
2948 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
2949 wp != NULL; wp = wp->w_next)
2950 if (wp->w_filter_cb.cb_name != NULL
2951 && (wp->w_popup_flags & (POPF_HIDDEN | POPF_MAPPING)) == 0)
2952 return TRUE;
2953 return FALSE;
2954}
2955
2956/*
Bram Moolenaar3397f742019-06-02 18:40:06 +02002957 * Called when the cursor moved: check if any popup needs to be closed if the
2958 * cursor moved far enough.
2959 */
2960 void
2961popup_check_cursor_pos()
2962{
2963 win_T *wp;
Bram Moolenaar3397f742019-06-02 18:40:06 +02002964
2965 popup_reset_handled();
2966 while ((wp = find_next_popup(TRUE)) != NULL)
2967 if (wp->w_popup_curwin != NULL
2968 && (curwin != wp->w_popup_curwin
2969 || curwin->w_cursor.lnum != wp->w_popup_lnum
2970 || curwin->w_cursor.col < wp->w_popup_mincol
2971 || curwin->w_cursor.col > wp->w_popup_maxcol))
Bram Moolenaar12034e22019-08-25 22:25:02 +02002972 popup_close_with_retval(wp, -1);
Bram Moolenaar3397f742019-06-02 18:40:06 +02002973}
2974
Bram Moolenaara540f8a2019-06-14 19:23:57 +02002975/*
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002976 * Update "w_popup_mask_cells".
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002977 */
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002978 static void
2979popup_update_mask(win_T *wp, int width, int height)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002980{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002981 listitem_T *lio, *li;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002982 char_u *cells;
2983 int row, col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002984
2985 if (wp->w_popup_mask == NULL)
Bram Moolenaare865dcb2019-07-26 22:15:50 +02002986 return;
2987 if (wp->w_popup_mask_cells != NULL
2988 && wp->w_popup_mask_height == height
2989 && wp->w_popup_mask_width == width)
2990 return; // cache is still valid
2991
2992 vim_free(wp->w_popup_mask_cells);
2993 wp->w_popup_mask_cells = alloc_clear(width * height);
2994 if (wp->w_popup_mask_cells == NULL)
2995 return;
2996 cells = wp->w_popup_mask_cells;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002997
2998 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
2999 {
3000 int cols, cole;
3001 int lines, linee;
3002
3003 li = lio->li_tv.vval.v_list->lv_first;
3004 cols = tv_get_number(&li->li_tv);
3005 if (cols < 0)
3006 cols = width + cols + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003007 li = li->li_next;
3008 cole = tv_get_number(&li->li_tv);
3009 if (cole < 0)
3010 cole = width + cole + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003011 li = li->li_next;
3012 lines = tv_get_number(&li->li_tv);
3013 if (lines < 0)
3014 lines = height + lines + 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003015 li = li->li_next;
3016 linee = tv_get_number(&li->li_tv);
3017 if (linee < 0)
3018 linee = height + linee + 1;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003019
3020 for (row = lines - 1; row < linee && row < height; ++row)
3021 for (col = cols - 1; col < cole && col < width; ++col)
3022 cells[row * width + col] = 1;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003023 }
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003024}
3025
3026/*
3027 * Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
3028 * "col" and "line" are screen coordinates.
3029 */
3030 static int
3031popup_masked(win_T *wp, int width, int height, int screencol, int screenline)
3032{
3033 int col = screencol - wp->w_wincol + wp->w_popup_leftoff;
3034 int line = screenline - wp->w_winrow;
3035
3036 return col >= 0 && col < width
3037 && line >= 0 && line < height
3038 && wp->w_popup_mask_cells[line * width + col];
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003039}
3040
3041/*
3042 * Set flags in popup_transparent[] for window "wp" to "val".
3043 */
3044 static void
3045update_popup_transparent(win_T *wp, int val)
3046{
3047 if (wp->w_popup_mask != NULL)
3048 {
3049 int width = popup_width(wp);
3050 int height = popup_height(wp);
3051 listitem_T *lio, *li;
3052 int cols, cole;
3053 int lines, linee;
3054 int col, line;
3055
3056 for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
3057 {
3058 li = lio->li_tv.vval.v_list->lv_first;
3059 cols = tv_get_number(&li->li_tv);
3060 if (cols < 0)
3061 cols = width + cols + 1;
3062 li = li->li_next;
3063 cole = tv_get_number(&li->li_tv);
3064 if (cole < 0)
3065 cole = width + cole + 1;
3066 li = li->li_next;
3067 lines = tv_get_number(&li->li_tv);
3068 if (lines < 0)
3069 lines = height + lines + 1;
3070 li = li->li_next;
3071 linee = tv_get_number(&li->li_tv);
3072 if (linee < 0)
3073 linee = height + linee + 1;
3074
3075 --cols;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003076 cols -= wp->w_popup_leftoff;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003077 if (cols < 0)
3078 cols = 0;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003079 cole -= wp->w_popup_leftoff;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003080 --lines;
Bram Moolenaar711d02c2019-06-28 04:06:50 +02003081 if (lines < 0)
3082 lines = 0;
Bram Moolenaarb4207472019-07-12 16:05:45 +02003083 for (line = lines; line < linee
3084 && line + wp->w_winrow < screen_Rows; ++line)
3085 for (col = cols; col < cole
3086 && col + wp->w_wincol < screen_Columns; ++col)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003087 popup_transparent[(line + wp->w_winrow) * screen_Columns
3088 + col + wp->w_wincol] = val;
3089 }
3090 }
3091}
3092
3093/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003094 * Only called when popup window "wp" is hidden: If the window is positioned
3095 * next to a text property, and it is now visible, then unhide the popup.
3096 * We don't check if visible popups become hidden, that is done in
3097 * popup_adjust_position().
3098 * Return TRUE if the popup became unhidden.
3099 */
3100 static int
3101check_popup_unhidden(win_T *wp)
3102{
3103 if (wp->w_popup_prop_type > 0 && win_valid(wp->w_popup_prop_win))
3104 {
3105 textprop_T prop;
3106 linenr_T lnum;
3107
3108 if (find_visible_prop(wp->w_popup_prop_win,
3109 wp->w_popup_prop_type, wp->w_popup_prop_id,
3110 &prop, &lnum) == OK)
3111 {
3112 wp->w_popup_flags &= ~POPF_HIDDEN;
3113 ++wp->w_buffer->b_nwindows;
3114 wp->w_popup_prop_topline = 0; // force repositioning
3115 return TRUE;
3116 }
3117 }
3118 return FALSE;
3119}
3120
3121/*
3122 * Return TRUE if popup_adjust_position() needs to be called for "wp".
3123 * That is when the buffer in the popup was changed, or the popup is following
3124 * a textprop and the referenced buffer was changed.
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003125 * Or when the cursor line changed and "cursorline" is set.
Bram Moolenaar12034e22019-08-25 22:25:02 +02003126 */
3127 static int
3128popup_need_position_adjust(win_T *wp)
3129{
3130 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
3131 return TRUE;
3132 if (win_valid(wp->w_popup_prop_win))
3133 return wp->w_popup_prop_changedtick
3134 != CHANGEDTICK(wp->w_popup_prop_win->w_buffer)
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003135 || wp->w_popup_prop_topline != wp->w_popup_prop_win->w_topline
3136 || ((wp->w_popup_flags & POPF_CURSORLINE)
3137 && wp->w_cursor.lnum != wp->w_popup_last_curline);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003138 return FALSE;
3139}
3140
3141/*
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003142 * Update "popup_mask" if needed.
3143 * Also recomputes the popup size and positions.
3144 * Also updates "popup_visible".
3145 * Also marks window lines for redrawing.
3146 */
3147 void
3148may_update_popup_mask(int type)
3149{
3150 win_T *wp;
3151 short *mask;
3152 int line, col;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003153 int redraw_all_popups = FALSE;
3154 int redrawing_all_win;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003155
3156 // Need to recompute when switching tabs.
3157 // Also recompute when the type is CLEAR or NOT_VALID, something basic
3158 // (such as the screen size) must have changed.
3159 if (popup_mask_tab != curtab || type >= NOT_VALID)
3160 {
3161 popup_mask_refresh = TRUE;
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003162 redraw_all_popups = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003163 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003164
3165 // Check if any popup window buffer has changed and if any popup connected
3166 // to a text property has become visible.
3167 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3168 if (wp->w_popup_flags & POPF_HIDDEN)
3169 popup_mask_refresh |= check_popup_unhidden(wp);
3170 else if (popup_need_position_adjust(wp))
3171 popup_mask_refresh = TRUE;
3172 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3173 if (wp->w_popup_flags & POPF_HIDDEN)
3174 popup_mask_refresh |= check_popup_unhidden(wp);
3175 else if (popup_need_position_adjust(wp))
3176 popup_mask_refresh = TRUE;
3177
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003178 if (!popup_mask_refresh)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003179 return;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003180
3181 // Need to update the mask, something has changed.
3182 popup_mask_refresh = FALSE;
3183 popup_mask_tab = curtab;
3184 popup_visible = FALSE;
3185
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003186 // If redrawing all windows, just update "popup_mask".
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003187 // If redrawing only what is needed, update "popup_mask_next" and then
3188 // compare with "popup_mask" to see what changed.
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003189 redrawing_all_win = TRUE;
3190 FOR_ALL_WINDOWS(wp)
3191 if (wp->w_redr_type < SOME_VALID)
3192 redrawing_all_win = FALSE;
3193 if (redrawing_all_win)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003194 mask = popup_mask;
3195 else
3196 mask = popup_mask_next;
3197 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
3198
3199 // Find the window with the lowest zindex that hasn't been handled yet,
3200 // so that the window with a higher zindex overwrites the value in
3201 // popup_mask.
3202 popup_reset_handled();
3203 while ((wp = find_next_popup(TRUE)) != NULL)
3204 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003205 int width;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003206 int height;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003207
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003208 popup_visible = TRUE;
3209
Bram Moolenaar12034e22019-08-25 22:25:02 +02003210 // Recompute the position if the text changed. It may make the popup
3211 // hidden if it's attach to a text property that is no longer visible.
3212 if (redraw_all_popups || popup_need_position_adjust(wp))
3213 {
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003214 popup_adjust_position(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +02003215 if (wp->w_popup_flags & POPF_HIDDEN)
3216 continue;
3217 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003218
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003219 width = popup_width(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003220 height = popup_height(wp);
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003221 popup_update_mask(wp, width, height);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003222 for (line = wp->w_winrow;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003223 line < wp->w_winrow + height && line < screen_Rows; ++line)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003224 for (col = wp->w_wincol;
Bram Moolenaare865dcb2019-07-26 22:15:50 +02003225 col < wp->w_wincol + width - wp->w_popup_leftoff
3226 && col < screen_Columns; ++col)
3227 if (wp->w_popup_mask_cells == NULL
3228 || !popup_masked(wp, width, height, col, line))
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003229 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003230 }
3231
3232 // Only check which lines are to be updated if not already
3233 // updating all lines.
3234 if (mask == popup_mask_next)
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003235 {
3236 int *plines_cache = ALLOC_CLEAR_MULT(int, Rows);
3237 win_T *prev_wp = NULL;
3238
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003239 for (line = 0; line < screen_Rows; ++line)
3240 {
3241 int col_done = 0;
3242
3243 for (col = 0; col < screen_Columns; ++col)
3244 {
3245 int off = line * screen_Columns + col;
3246
3247 if (popup_mask[off] != popup_mask_next[off])
3248 {
3249 popup_mask[off] = popup_mask_next[off];
3250
3251 if (line >= cmdline_row)
3252 {
3253 // the command line needs to be cleared if text below
3254 // the popup is now visible.
3255 if (!msg_scrolled && popup_mask_next[off] == 0)
3256 clear_cmdline = TRUE;
3257 }
3258 else if (col >= col_done)
3259 {
3260 linenr_T lnum;
3261 int line_cp = line;
3262 int col_cp = col;
3263
3264 // The screen position "line" / "col" needs to be
3265 // redrawn. Figure out what window that is and update
3266 // w_redraw_top and w_redr_bot. Only needs to be done
3267 // once for each window line.
3268 wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
3269 if (wp != NULL)
3270 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003271 if (wp != prev_wp)
3272 {
3273 vim_memset(plines_cache, 0, sizeof(int) * Rows);
3274 prev_wp = wp;
3275 }
3276
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003277 if (line_cp >= wp->w_height)
3278 // In (or below) status line
3279 wp->w_redr_status = TRUE;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003280 else
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003281 {
3282 // compute the position in the buffer line from
3283 // the position in the window
3284 mouse_comp_pos(wp, &line_cp, &col_cp,
3285 &lnum, plines_cache);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003286 redrawWinline(wp, lnum);
Bram Moolenaare9a891f2019-08-24 15:26:24 +02003287 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003288
3289 // This line is going to be redrawn, no need to
3290 // check until the right side of the window.
3291 col_done = wp->w_wincol + wp->w_width - 1;
3292 }
3293 }
3294 }
3295 }
3296 }
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003297
3298 vim_free(plines_cache);
3299 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003300}
3301
3302/*
3303 * Return a string of "len" spaces in IObuff.
3304 */
3305 static char_u *
3306get_spaces(int len)
3307{
3308 vim_memset(IObuff, ' ', (size_t)len);
3309 IObuff[len] = NUL;
3310 return IObuff;
3311}
3312
3313/*
3314 * Update popup windows. They are drawn on top of normal windows.
3315 * "win_update" is called for each popup window, lowest zindex first.
3316 */
3317 void
3318update_popups(void (*win_update)(win_T *wp))
3319{
3320 win_T *wp;
3321 int top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003322 int left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003323 int total_width;
3324 int total_height;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003325 int top_padding;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003326 int popup_attr;
3327 int border_attr[4];
3328 int border_char[8];
3329 char_u buf[MB_MAXBYTES];
3330 int row;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003331 int wincol;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003332 int padcol = 0;
3333 int padwidth = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003334 int i;
Bram Moolenaar6efd76a2019-06-26 04:06:57 +02003335 int sb_thumb_top = 0;
3336 int sb_thumb_height = 0;
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003337 int attr_scroll = 0;
3338 int attr_thumb = 0;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003339
3340 // Find the window with the lowest zindex that hasn't been updated yet,
3341 // so that the window with a higher zindex is drawn later, thus goes on
3342 // top.
3343 popup_reset_handled();
3344 while ((wp = find_next_popup(TRUE)) != NULL)
3345 {
3346 // This drawing uses the zindex of the popup window, so that it's on
3347 // top of the text but doesn't draw when another popup with higher
3348 // zindex is on top of the character.
3349 screen_zindex = wp->w_zindex;
3350
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003351 // Set flags in popup_transparent[] for masked cells.
3352 update_popup_transparent(wp, 1);
3353
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003354 // adjust w_winrow and w_wincol for border and padding, since
3355 // win_update() doesn't handle them.
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003356 top_off = popup_top_extra(wp);
Bram Moolenaard529ba52019-07-02 23:13:53 +02003357 left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
3358 - wp->w_popup_leftoff;
3359 if (wp->w_wincol + left_extra < 0)
3360 left_extra = -wp->w_wincol;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003361 wp->w_winrow += top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003362 wp->w_wincol += left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003363
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003364 // Draw the popup text, unless it's off screen.
3365 if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003366 {
Bram Moolenaarc2a43162019-06-26 01:03:53 +02003367 win_update(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003368
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02003369 // move the cursor into the visible lines, otherwise executing
3370 // commands with win_execute() may cause the text to jump.
3371 if (wp->w_cursor.lnum < wp->w_topline)
3372 wp->w_cursor.lnum = wp->w_topline;
3373 else if (wp->w_cursor.lnum >= wp->w_botline)
3374 wp->w_cursor.lnum = wp->w_botline - 1;
3375 }
3376
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003377 wp->w_winrow -= top_off;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003378 wp->w_wincol -= left_extra;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003379
Bram Moolenaard529ba52019-07-02 23:13:53 +02003380 total_width = popup_width(wp);
3381 total_height = popup_height(wp);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003382 popup_attr = get_wcr_attr(wp);
3383
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003384 if (wp->w_winrow + total_height > cmdline_row)
3385 wp->w_popup_flags |= POPF_ON_CMDLINE;
3386 else
3387 wp->w_popup_flags &= ~POPF_ON_CMDLINE;
3388
3389
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003390 // We can only use these line drawing characters when 'encoding' is
3391 // "utf-8" and 'ambiwidth' is "single".
3392 if (enc_utf8 && *p_ambw == 's')
3393 {
3394 border_char[0] = border_char[2] = 0x2550;
3395 border_char[1] = border_char[3] = 0x2551;
3396 border_char[4] = 0x2554;
3397 border_char[5] = 0x2557;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003398 border_char[6] = (wp->w_popup_flags & POPF_RESIZE)
3399 ? 0x21f2 : 0x255d;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003400 border_char[7] = 0x255a;
3401 }
3402 else
3403 {
3404 border_char[0] = border_char[2] = '-';
3405 border_char[1] = border_char[3] = '|';
3406 for (i = 4; i < 8; ++i)
3407 border_char[i] = '+';
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003408 if (wp->w_popup_flags & POPF_RESIZE)
3409 border_char[6] = '@';
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003410 }
3411 for (i = 0; i < 8; ++i)
3412 if (wp->w_border_char[i] != 0)
3413 border_char[i] = wp->w_border_char[i];
3414
3415 for (i = 0; i < 4; ++i)
3416 {
3417 border_attr[i] = popup_attr;
3418 if (wp->w_border_highlight[i] != NULL)
3419 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
3420 }
3421
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003422 wincol = wp->w_wincol - wp->w_popup_leftoff;
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003423 top_padding = wp->w_popup_padding[0];
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003424 if (wp->w_popup_border[0] > 0)
3425 {
3426 // top border
3427 screen_fill(wp->w_winrow, wp->w_winrow + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003428 wincol < 0 ? 0 : wincol, wincol + total_width,
3429 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003430 ? border_char[4] : border_char[0],
3431 border_char[0], border_attr[0]);
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003432 if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003433 {
3434 buf[mb_char2bytes(border_char[5], buf)] = NUL;
3435 screen_puts(buf, wp->w_winrow,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003436 wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003437 }
3438 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003439 else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
3440 top_padding = 1;
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003441
Bram Moolenaard529ba52019-07-02 23:13:53 +02003442 if (top_padding > 0 || wp->w_popup_padding[2] > 0)
3443 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003444 padcol = wincol + wp->w_popup_border[3];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003445 padwidth = wp->w_wincol + total_width - wp->w_popup_border[1]
3446 - wp->w_has_scrollbar;
3447 if (padcol < 0)
3448 {
3449 padwidth += padcol;
3450 padcol = 0;
3451 }
3452 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003453 if (top_padding > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003454 {
3455 // top padding
3456 row = wp->w_winrow + wp->w_popup_border[0];
Bram Moolenaard529ba52019-07-02 23:13:53 +02003457 screen_fill(row, row + top_padding, padcol, padwidth,
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003458 ' ', ' ', popup_attr);
3459 }
3460
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003461 // Title goes on top of border or padding.
3462 if (wp->w_popup_title != NULL)
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003463 {
3464 int len = (int)STRLEN(wp->w_popup_title) + 1;
3465 char_u *title = alloc(len);
3466
3467 trunc_string(wp->w_popup_title, title, total_width - 2, len);
3468 screen_puts(title, wp->w_winrow, wp->w_wincol + 1,
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003469 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
Bram Moolenaar86173482019-10-01 17:02:16 +02003470 vim_free(title);
Bram Moolenaar5d458a72019-08-04 21:12:15 +02003471 }
Bram Moolenaareb2310d2019-06-16 20:09:10 +02003472
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003473 // Compute scrollbar thumb position and size.
3474 if (wp->w_has_scrollbar)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003475 {
Bram Moolenaar076d9882019-09-14 22:23:29 +02003476 linenr_T linecount = wp->w_buffer->b_ml.ml_line_count;
3477 int height = wp->w_height;
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003478
Bram Moolenaar076d9882019-09-14 22:23:29 +02003479 sb_thumb_height = (height * height + linecount / 2) / linecount;
3480 if (wp->w_topline > 1 && sb_thumb_height == height)
3481 --sb_thumb_height; // scrolled, no full thumb
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003482 if (sb_thumb_height == 0)
3483 sb_thumb_height = 1;
Bram Moolenaar437a7462019-07-05 20:17:22 +02003484 if (linecount <= wp->w_height)
3485 // it just fits, avoid divide by zero
3486 sb_thumb_top = 0;
3487 else
3488 sb_thumb_top = (wp->w_topline - 1
3489 + (linecount / wp->w_height) / 2)
Bram Moolenaar68acb412019-06-26 03:40:36 +02003490 * (wp->w_height - sb_thumb_height)
3491 / (linecount - wp->w_height);
Bram Moolenaar076d9882019-09-14 22:23:29 +02003492 if (wp->w_topline > 1 && sb_thumb_top == 0 && height > 1)
3493 sb_thumb_top = 1; // show it's scrolled
3494
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02003495 if (wp->w_scrollbar_highlight != NULL)
3496 attr_scroll = syn_name2attr(wp->w_scrollbar_highlight);
3497 else
3498 attr_scroll = highlight_attr[HLF_PSB];
3499 if (wp->w_thumb_highlight != NULL)
3500 attr_thumb = syn_name2attr(wp->w_thumb_highlight);
3501 else
3502 attr_thumb = highlight_attr[HLF_PST];
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003503 }
3504
3505 for (i = wp->w_popup_border[0];
3506 i < total_height - wp->w_popup_border[2]; ++i)
3507 {
Bram Moolenaard529ba52019-07-02 23:13:53 +02003508 int pad_left;
Bram Moolenaard529ba52019-07-02 23:13:53 +02003509 // left and right padding only needed next to the body
3510 int do_padding =
3511 i >= wp->w_popup_border[0] + wp->w_popup_padding[0]
3512 && i < total_height - wp->w_popup_border[2]
3513 - wp->w_popup_padding[2];
3514
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003515 row = wp->w_winrow + i;
3516
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003517 // left border
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003518 if (wp->w_popup_border[3] > 0 && wincol >= 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003519 {
3520 buf[mb_char2bytes(border_char[3], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003521 screen_puts(buf, row, wincol, border_attr[3]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003522 }
Bram Moolenaard529ba52019-07-02 23:13:53 +02003523 if (do_padding && wp->w_popup_padding[3] > 0)
3524 {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003525 int col = wincol + wp->w_popup_border[3];
3526
Bram Moolenaard529ba52019-07-02 23:13:53 +02003527 // left padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003528 pad_left = wp->w_popup_padding[3];
3529 if (col < 0)
3530 {
3531 pad_left += col;
3532 col = 0;
3533 }
3534 if (pad_left > 0)
3535 screen_puts(get_spaces(pad_left), row, col, popup_attr);
3536 }
Bram Moolenaar75fb0852019-06-25 05:15:58 +02003537 // scrollbar
3538 if (wp->w_has_scrollbar)
3539 {
3540 int line = i - top_off;
3541 int scroll_col = wp->w_wincol + total_width - 1
3542 - wp->w_popup_border[1];
3543
3544 if (line >= 0 && line < wp->w_height)
3545 screen_putchar(' ', row, scroll_col,
3546 line >= sb_thumb_top
3547 && line < sb_thumb_top + sb_thumb_height
3548 ? attr_thumb : attr_scroll);
3549 else
3550 screen_putchar(' ', row, scroll_col, popup_attr);
3551 }
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003552 // right border
3553 if (wp->w_popup_border[1] > 0)
3554 {
3555 buf[mb_char2bytes(border_char[1], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003556 screen_puts(buf, row, wincol + total_width - 1, border_attr[1]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003557 }
3558 // right padding
Bram Moolenaard529ba52019-07-02 23:13:53 +02003559 if (do_padding && wp->w_popup_padding[1] > 0)
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003560 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003561 wincol + wp->w_popup_border[3]
Bram Moolenaard529ba52019-07-02 23:13:53 +02003562 + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol,
3563 popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003564 }
3565
3566 if (wp->w_popup_padding[2] > 0)
3567 {
3568 // bottom padding
3569 row = wp->w_winrow + wp->w_popup_border[0]
3570 + wp->w_popup_padding[0] + wp->w_height;
3571 screen_fill(row, row + wp->w_popup_padding[2],
Bram Moolenaard529ba52019-07-02 23:13:53 +02003572 padcol, padwidth, ' ', ' ', popup_attr);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003573 }
3574
3575 if (wp->w_popup_border[2] > 0)
3576 {
3577 // bottom border
3578 row = wp->w_winrow + total_height - 1;
3579 screen_fill(row , row + 1,
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003580 wincol < 0 ? 0 : wincol,
3581 wincol + total_width,
3582 wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003583 ? border_char[7] : border_char[2],
3584 border_char[2], border_attr[2]);
3585 if (wp->w_popup_border[1] > 0)
3586 {
3587 buf[mb_char2bytes(border_char[6], buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003588 screen_puts(buf, row, wincol + total_width - 1, border_attr[2]);
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003589 }
3590 }
3591
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003592 if (wp->w_popup_close == POPCLOSE_BUTTON)
3593 {
3594 // close button goes on top of anything at the top-right corner
3595 buf[mb_char2bytes('X', buf)] = NUL;
Bram Moolenaarba45f1f2019-07-03 22:50:41 +02003596 screen_puts(buf, wp->w_winrow, wincol + total_width - 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003597 wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
3598 }
3599
Bram Moolenaarc662ec92019-06-23 00:15:57 +02003600 update_popup_transparent(wp, 0);
3601
Bram Moolenaara540f8a2019-06-14 19:23:57 +02003602 // Back to the normal zindex.
3603 screen_zindex = 0;
3604 }
3605}
3606
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003607/*
3608 * Mark references in callbacks of one popup window.
3609 */
3610 static int
3611set_ref_in_one_popup(win_T *wp, int copyID)
3612{
3613 int abort = FALSE;
3614 typval_T tv;
3615
3616 if (wp->w_close_cb.cb_partial != NULL)
3617 {
3618 tv.v_type = VAR_PARTIAL;
3619 tv.vval.v_partial = wp->w_close_cb.cb_partial;
3620 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3621 }
3622 if (wp->w_filter_cb.cb_partial != NULL)
3623 {
3624 tv.v_type = VAR_PARTIAL;
3625 tv.vval.v_partial = wp->w_filter_cb.cb_partial;
3626 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3627 }
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003628 abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003629 return abort;
3630}
3631
3632/*
3633 * Set reference in callbacks of popup windows.
3634 */
3635 int
3636set_ref_in_popups(int copyID)
3637{
3638 int abort = FALSE;
3639 win_T *wp;
3640 tabpage_T *tp;
3641
3642 for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3643 abort = abort || set_ref_in_one_popup(wp, copyID);
3644
3645 FOR_ALL_TABPAGES(tp)
3646 {
3647 for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
3648 abort = abort || set_ref_in_one_popup(wp, copyID);
3649 if (abort)
3650 break;
3651 }
3652 return abort;
3653}
Bram Moolenaar79648732019-07-18 21:43:07 +02003654
3655/*
3656 * Find an existing popup used as the preview window, in the current tab page.
3657 * Return NULL if not found.
3658 */
3659 win_T *
3660popup_find_preview_window(void)
3661{
3662 win_T *wp;
3663
3664 // Preview window popup is always local to tab page.
3665 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3666 if (wp->w_p_pvw)
3667 return wp;
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003668 return NULL;
3669}
3670
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003671 int
3672popup_is_popup(win_T *wp)
3673{
3674 return wp->w_popup_flags != 0;
3675}
3676
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003677#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003678/*
3679 * Find an existing popup used as the info window, in the current tab page.
3680 * Return NULL if not found.
3681 */
3682 win_T *
3683popup_find_info_window(void)
3684{
3685 win_T *wp;
3686
3687 // info window popup is always local to tab page.
3688 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
3689 if (wp->w_popup_flags & POPF_INFO)
3690 return wp;
3691 return NULL;
3692}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003693#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003694
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003695 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003696f_popup_findinfo(typval_T *argvars UNUSED, typval_T *rettv)
3697{
3698 win_T *wp = popup_find_info_window();
3699
3700 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
3701}
3702
3703 void
3704f_popup_findpreview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar56c0c472019-07-28 17:57:43 +02003705{
3706 win_T *wp = popup_find_preview_window();
3707
3708 rettv->vval.v_number = wp == NULL ? 0 : wp->w_id;
Bram Moolenaar79648732019-07-18 21:43:07 +02003709}
3710
Bram Moolenaar79648732019-07-18 21:43:07 +02003711/*
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003712 * Create a popup to be used as the preview or info window.
Bram Moolenaar79648732019-07-18 21:43:07 +02003713 * NOTE: this makes the popup the current window, so that the file can be
3714 * edited. However, it must not remain to be the current window, the caller
3715 * must make sure of that.
3716 */
3717 int
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003718popup_create_preview_window(int info)
Bram Moolenaar79648732019-07-18 21:43:07 +02003719{
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003720 win_T *wp = popup_create(NULL, NULL, info ? TYPE_INFO : TYPE_PREVIEW);
Bram Moolenaar79648732019-07-18 21:43:07 +02003721
3722 if (wp == NULL)
3723 return FAIL;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003724 if (info)
3725 wp->w_popup_flags |= POPF_INFO;
3726 else
3727 wp->w_p_pvw = TRUE;
Bram Moolenaar79648732019-07-18 21:43:07 +02003728
3729 // Set the width to a reasonable value, so that w_topline can be computed.
3730 if (wp->w_minwidth > 0)
3731 wp->w_width = wp->w_minwidth;
3732 else if (wp->w_maxwidth > 0)
3733 wp->w_width = wp->w_maxwidth;
3734 else
3735 wp->w_width = curwin->w_width;
3736
3737 // Will switch to another buffer soon, dummy one can be wiped.
3738 wp->w_buffer->b_locked = FALSE;
3739
3740 win_enter(wp, FALSE);
3741 return OK;
3742}
3743
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003744#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003745/*
3746 * Close any preview popup.
3747 */
Bram Moolenaar79648732019-07-18 21:43:07 +02003748 void
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003749popup_close_preview(void)
Bram Moolenaar79648732019-07-18 21:43:07 +02003750{
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003751 win_T *wp = popup_find_preview_window();
Bram Moolenaar79648732019-07-18 21:43:07 +02003752
3753 if (wp != NULL)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003754 popup_close_with_retval(wp, -1);
Bram Moolenaar79648732019-07-18 21:43:07 +02003755}
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003756
3757/*
3758 * Hide the info popup.
3759 */
3760 void
3761popup_hide_info(void)
3762{
3763 win_T *wp = popup_find_info_window();
3764
3765 if (wp != NULL)
3766 popup_hide(wp);
3767}
Bram Moolenaar36e4d982019-08-20 21:12:16 +02003768#endif
Bram Moolenaar79648732019-07-18 21:43:07 +02003769
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003770/*
Bram Moolenaar12034e22019-08-25 22:25:02 +02003771 * Close any popup for a text property associated with "win".
3772 * Return TRUE if a popup was closed.
3773 */
3774 int
3775popup_win_closed(win_T *win)
3776{
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003777 int round;
3778 win_T *wp;
3779 win_T *next;
3780 int ret = FALSE;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003781
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003782 for (round = 1; round <= 2; ++round)
3783 for (wp = round == 1 ? first_popupwin : curtab->tp_first_popupwin;
3784 wp != NULL; wp = next)
Bram Moolenaar12034e22019-08-25 22:25:02 +02003785 {
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003786 next = wp->w_next;
3787 if (wp->w_popup_prop_win == win)
3788 {
3789 popup_close_with_retval(wp, -1);
3790 ret = TRUE;
3791 }
Bram Moolenaar12034e22019-08-25 22:25:02 +02003792 }
Bram Moolenaar1fb08312019-08-29 20:02:11 +02003793 return ret;
Bram Moolenaar12034e22019-08-25 22:25:02 +02003794}
3795
3796/*
Bram Moolenaar90f3e7a2019-08-01 22:40:44 +02003797 * Set the title of the popup window to the file name.
3798 */
3799 void
3800popup_set_title(win_T *wp)
3801{
3802 if (wp->w_buffer->b_fname != NULL)
3803 {
3804 char_u dirname[MAXPATHL];
3805 size_t len;
3806
3807 mch_dirname(dirname, MAXPATHL);
3808 shorten_buf_fname(wp->w_buffer, dirname, FALSE);
3809
3810 vim_free(wp->w_popup_title);
3811 len = STRLEN(wp->w_buffer->b_fname) + 3;
3812 wp->w_popup_title = alloc((int)len);
3813 if (wp->w_popup_title != NULL)
3814 vim_snprintf((char *)wp->w_popup_title, len, " %s ",
3815 wp->w_buffer->b_fname);
3816 redraw_win_later(wp, VALID);
3817 }
3818}
3819
3820/*
3821 * If there is a preview window, update the title.
3822 * Used after changing directory.
3823 */
3824 void
3825popup_update_preview_title(void)
3826{
3827 win_T *wp = popup_find_preview_window();
3828
3829 if (wp != NULL)
3830 popup_set_title(wp);
3831}
3832
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003833#endif // FEAT_TEXT_PROP