blob: f53649f1232fb0875d118c905ad3222e2a1e56cb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar4b779472005-10-04 09:12:31 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
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/*
Bram Moolenaar76b92b22006-03-24 22:46:53 +000011 * popupmnu.c: Popup menu (PUM)
Bram Moolenaar4b779472005-10-04 09:12:31 +000012 */
13#include "vim.h"
14
15#if defined(FEAT_INS_EXPAND) || defined(PROTO)
16
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000017static pumitem_T *pum_array = NULL; /* items of displayed pum */
Bram Moolenaar4b779472005-10-04 09:12:31 +000018static int pum_size; /* nr of items in "pum_array" */
19static int pum_selected; /* index of selected item or -1 */
20static int pum_first = 0; /* index of top item */
21
22static int pum_height; /* nr of displayed pum items */
23static int pum_width; /* width of displayed pum items */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000024static int pum_base_width; /* width of pum items base */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000025static int pum_kind_width; /* width of pum items kind column */
Bram Moolenaar51b0f372017-11-18 18:52:04 +010026static int pum_extra_width; /* width of extra stuff */
Bram Moolenaar4b779472005-10-04 09:12:31 +000027static int pum_scrollbar; /* TRUE when scrollbar present */
28
29static int pum_row; /* top row of pum */
30static int pum_col; /* left column of pum */
31
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000032static int pum_do_redraw = FALSE; /* do redraw anyway */
33
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010034static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000035
Bram Moolenaar4b779472005-10-04 09:12:31 +000036#define PUM_DEF_HEIGHT 10
37#define PUM_DEF_WIDTH 15
38
Bram Moolenaar51b0f372017-11-18 18:52:04 +010039 static void
40pum_compute_size(void)
41{
42 int i;
43 int w;
44
45 /* Compute the width of the widest match and the widest extra. */
46 pum_base_width = 0;
47 pum_kind_width = 0;
48 pum_extra_width = 0;
49 for (i = 0; i < pum_size; ++i)
50 {
51 w = vim_strsize(pum_array[i].pum_text);
52 if (pum_base_width < w)
53 pum_base_width = w;
54 if (pum_array[i].pum_kind != NULL)
55 {
56 w = vim_strsize(pum_array[i].pum_kind) + 1;
57 if (pum_kind_width < w)
58 pum_kind_width = w;
59 }
60 if (pum_array[i].pum_extra != NULL)
61 {
62 w = vim_strsize(pum_array[i].pum_extra) + 1;
63 if (pum_extra_width < w)
64 pum_extra_width = w;
65 }
66 }
67}
68
Bram Moolenaar4b779472005-10-04 09:12:31 +000069/*
Bram Moolenaara8f04aa2018-02-10 15:36:55 +010070 * Return the minimum width of the popup menu.
71 */
72 static int
73pum_get_width(void)
74{
75 return p_pw == 0 ? PUM_DEF_WIDTH : p_pw;
76}
77
78/*
Bram Moolenaar4b779472005-10-04 09:12:31 +000079 * Show the popup menu with items "array[size]".
80 * "array" must remain valid until pum_undisplay() is called!
81 * When possible the leftmost character is aligned with screen column "col".
82 * The menu appears above the screen line "row" or at "row" + "height" - 1.
83 */
84 void
Bram Moolenaar05540972016-01-30 20:31:25 +010085pum_display(
86 pumitem_T *array,
87 int size,
88 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000089 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000090{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000091 int def_width;
92 int max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +000093 int row;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000094 int context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +000095 int col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010096 int above_row;
97 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000098 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020099#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100100 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100101#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000102
Bram Moolenaara5e66212017-09-29 22:42:33 +0200103 do
104 {
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100105 def_width = pum_get_width();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200106 above_row = 0;
107 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000108
Bram Moolenaara5e66212017-09-29 22:42:33 +0200109 /* Pretend the pum is already there to avoid that must_redraw is set
110 * when 'cuc' is on. */
111 pum_array = (pumitem_T *)1;
112 validate_cursor_col();
113 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000114
Bram Moolenaara5e66212017-09-29 22:42:33 +0200115 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000116
Bram Moolenaar4033c552017-09-16 20:54:51 +0200117#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200118 FOR_ALL_WINDOWS(pvwin)
119 if (pvwin->w_p_pvw)
120 break;
121 if (pvwin != NULL)
122 {
123 if (W_WINROW(pvwin) < W_WINROW(curwin))
124 above_row = W_WINROW(pvwin) + pvwin->w_height;
125 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
126 below_row = W_WINROW(pvwin);
127 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100128#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000129
Bram Moolenaara5e66212017-09-29 22:42:33 +0200130 /*
131 * Figure out the size and position of the pum.
132 */
133 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000134 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000135 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200136 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000137 if (p_ph > 0 && pum_height > p_ph)
138 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000139
Bram Moolenaara5e66212017-09-29 22:42:33 +0200140 /* Put the pum below "row" if possible. If there are few lines decide
141 * on where there is more room. */
142 if (row + 2 >= below_row - pum_height
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100143 && row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200144 {
145 /* pum above "row" */
146
147 /* Leave two lines of context if possible */
148 if (curwin->w_wrow - curwin->w_cline_row >= 2)
149 context_lines = 2;
150 else
151 context_lines = curwin->w_wrow - curwin->w_cline_row;
152
153 if (row >= size + context_lines)
154 {
155 pum_row = row - size - context_lines;
156 pum_height = size;
157 }
158 else
159 {
160 pum_row = 0;
161 pum_height = row - context_lines;
162 }
163 if (p_ph > 0 && pum_height > p_ph)
164 {
165 pum_row += pum_height - p_ph;
166 pum_height = p_ph;
167 }
168 }
169 else
170 {
171 /* pum below "row" */
172
173 /* Leave two lines of context if possible */
174 if (curwin->w_cline_row
175 + curwin->w_cline_height - curwin->w_wrow >= 3)
176 context_lines = 3;
177 else
178 context_lines = curwin->w_cline_row
179 + curwin->w_cline_height - curwin->w_wrow;
180
181 pum_row = row + context_lines;
182 if (size > below_row - pum_row)
183 pum_height = below_row - pum_row;
184 else
185 pum_height = size;
186 if (p_ph > 0 && pum_height > p_ph)
187 pum_height = p_ph;
188 }
189
190 /* don't display when we only have room for one line */
191 if (pum_height < 1 || (pum_height == 1 && size > 1))
192 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000193
Bram Moolenaar4033c552017-09-16 20:54:51 +0200194#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195 /* If there is a preview window at the above avoid drawing over it. */
196 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000197 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200198 pum_row += above_row;
199 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000200 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200201#endif
202
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100203 pum_array = array;
204 pum_size = size;
205 pum_compute_size();
206 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000207
Bram Moolenaara5e66212017-09-29 22:42:33 +0200208 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000209#ifdef FEAT_RIGHTLEFT
210 if (curwin->w_p_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200211 col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000212 else
213#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214 col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000215
Bram Moolenaara5e66212017-09-29 22:42:33 +0200216 /* if there are more items than room we need a scrollbar */
217 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000218 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200219 pum_scrollbar = 1;
220 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000221 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000222 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200223 pum_scrollbar = 0;
224
225 if (def_width < max_width)
226 def_width = max_width;
227
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100228 if (((col < Columns - pum_get_width() || col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000229#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200230 && !curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100231 || (curwin->w_p_rl && (col > pum_get_width() || col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000232#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200233 ))
234 {
235 /* align pum column with "col" */
236 pum_col = col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000237
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100238 /* start with the maximum space available */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200239#ifdef FEAT_RIGHTLEFT
240 if (curwin->w_p_rl)
241 pum_width = pum_col - pum_scrollbar + 1;
242 else
243#endif
244 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000245
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100246 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100247 && pum_width > pum_get_width())
Bram Moolenaara5e66212017-09-29 22:42:33 +0200248 {
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100249 /* the width is too much, make it narrower */
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100250 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100251 if (pum_width < pum_get_width())
252 pum_width = pum_get_width();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200253 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100254 else if (((col > pum_get_width() || col > max_width)
255#ifdef FEAT_RIGHTLEFT
256 && !curwin->w_p_rl)
257 || (curwin->w_p_rl && (col < Columns - pum_get_width()
258 || col < Columns - max_width)
259#endif
260 ))
261 {
262 /* align right pum edge with "col" */
263#ifdef FEAT_RIGHTLEFT
264 if (curwin->w_p_rl)
265 {
266 pum_col = col + max_width + pum_scrollbar + 1;
267 if (pum_col >= Columns)
268 pum_col = Columns - 1;
269 }
270 else
271#endif
272 {
273 pum_col = col - max_width - pum_scrollbar;
274 if (pum_col < 0)
275 pum_col = 0;
276 }
277
278#ifdef FEAT_RIGHTLEFT
279 if (curwin->w_p_rl)
280 pum_width = W_ENDCOL(curwin) - pum_col - pum_scrollbar + 1;
281 else
282#endif
283 pum_width = pum_col - pum_scrollbar;
284
285 if (pum_width < pum_get_width())
286 {
287 pum_width = pum_get_width();
288#ifdef FEAT_RIGHTLEFT
289 if (curwin->w_p_rl)
290 {
291 if (pum_width > pum_col)
292 pum_width = pum_col;
293 }
294 else
295#endif
296 {
297 if (pum_width >= Columns - pum_col)
298 pum_width = Columns - pum_col - 1;
299 }
300 }
301 else if (pum_width > max_width + pum_kind_width
302 + pum_extra_width + 1
303 && pum_width > pum_get_width())
304 {
305 pum_width = max_width + pum_kind_width
306 + pum_extra_width + 1;
307 if (pum_width < pum_get_width())
308 pum_width = pum_get_width();
309 }
310 }
311
Bram Moolenaara5e66212017-09-29 22:42:33 +0200312 }
313 else if (Columns < def_width)
314 {
315 /* not enough room, will use what we have */
316#ifdef FEAT_RIGHTLEFT
317 if (curwin->w_p_rl)
318 pum_col = Columns - 1;
319 else
320#endif
321 pum_col = 0;
322 pum_width = Columns - 1;
323 }
324 else
325 {
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100326 if (max_width > pum_get_width())
327 max_width = pum_get_width(); /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200328#ifdef FEAT_RIGHTLEFT
329 if (curwin->w_p_rl)
330 pum_col = max_width - 1;
331 else
332#endif
333 pum_col = Columns - max_width;
334 pum_width = max_width - pum_scrollbar;
335 }
336
Bram Moolenaara5e66212017-09-29 22:42:33 +0200337 /* Set selected item and redraw. If the window size changed need to
338 * redo the positioning. Limit this to two times, when there is not
339 * much room the window size will keep changing. */
340 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000341}
342
343/*
344 * Redraw the popup menu, using "pum_first" and "pum_selected".
345 */
346 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100347pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000348{
349 int row = pum_row;
350 int col;
351 int attr_norm = highlight_attr[HLF_PNI];
352 int attr_select = highlight_attr[HLF_PSI];
353 int attr_scroll = highlight_attr[HLF_PSB];
354 int attr_thumb = highlight_attr[HLF_PST];
355 int attr;
356 int i;
357 int idx;
358 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000359 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000360 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000361 int thumb_pos = 0;
362 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000363 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000364 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000365
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100366 /* Never display more than we have */
367 if (pum_first > pum_size - pum_height)
368 pum_first = pum_size - pum_height;
369
Bram Moolenaar4b779472005-10-04 09:12:31 +0000370 if (pum_scrollbar)
371 {
372 thumb_heigth = pum_height * pum_height / pum_size;
373 if (thumb_heigth == 0)
374 thumb_heigth = 1;
375 thumb_pos = (pum_first * (pum_height - thumb_heigth)
376 + (pum_size - pum_height) / 2)
377 / (pum_size - pum_height);
378 }
379
380 for (i = 0; i < pum_height; ++i)
381 {
382 idx = i + pum_first;
383 attr = (idx == pum_selected) ? attr_select : attr_norm;
384
385 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000386#ifdef FEAT_RIGHTLEFT
387 if (curwin->w_p_rl)
388 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200389 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000390 screen_putchar(' ', row, pum_col + 1, attr);
391 }
392 else
393#endif
394 if (pum_col > 0)
395 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000396
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000397 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000398 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000399 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000400 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000401 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000402 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000403 width = 0;
404 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000405 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000406 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000407 case 1: p = pum_array[idx].pum_text; break;
408 case 2: p = pum_array[idx].pum_kind; break;
409 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000410 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000411 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100412 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000413 {
414 if (s == NULL)
415 s = p;
416 w = ptr2cells(p);
417 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
418 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000419 /* Display the text that fits or comes before a Tab.
420 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000421 char_u *st;
422 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000423
424 *p = NUL;
425 st = transstr(s);
426 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000427#ifdef FEAT_RIGHTLEFT
428 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000429 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000430 if (st != NULL)
431 {
432 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000433
434 if (rt != NULL)
435 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100436 char_u *rt_start = rt;
437 int size;
438
439 size = vim_strsize(rt);
440 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000441 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100442 do
443 {
444 size -= has_mbyte
445 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100446 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100447 } while (size > pum_width);
448
449 if (size < pum_width)
450 {
451 /* Most left character requires
452 * 2-cells but only 1 cell is
453 * available on screen. Put a
454 * '<' on the left of the pum
455 * item */
456 *(--rt) = '<';
457 size++;
458 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000459 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100460 screen_puts_len(rt, (int)STRLEN(rt),
461 row, col - size + 1, attr);
462 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000463 }
464 vim_free(st);
465 }
466 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000467 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000468 else
469#endif
470 {
471 if (st != NULL)
472 {
473 screen_puts_len(st, (int)STRLEN(st), row, col,
474 attr);
475 vim_free(st);
476 }
477 col += width;
478 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000479
480 if (*p != TAB)
481 break;
482
483 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000484#ifdef FEAT_RIGHTLEFT
485 if (curwin->w_p_rl)
486 {
487 screen_puts_len((char_u *)" ", 2, row, col - 1,
488 attr);
489 col -= 2;
490 }
491 else
492#endif
493 {
494 screen_puts_len((char_u *)" ", 2, row, col, attr);
495 col += 2;
496 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000497 totwidth += 2;
498 s = NULL; /* start text at next char */
499 width = 0;
500 }
501 else
502 width += w;
503 }
504
505 if (round > 1)
506 n = pum_kind_width + 1;
507 else
508 n = 1;
509
510 /* Stop when there is nothing more to display. */
511 if (round == 3
512 || (round == 2 && pum_array[idx].pum_extra == NULL)
513 || (round == 1 && pum_array[idx].pum_kind == NULL
514 && pum_array[idx].pum_extra == NULL)
515 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000516 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000517#ifdef FEAT_RIGHTLEFT
518 if (curwin->w_p_rl)
519 {
520 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
521 col + 1, ' ', ' ', attr);
522 col = pum_col - pum_base_width - n + 1;
523 }
524 else
525#endif
526 {
527 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000528 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000529 col = pum_col + pum_base_width + n;
530 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000531 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000532 }
533
Bram Moolenaarabc97732007-08-08 20:49:37 +0000534#ifdef FEAT_RIGHTLEFT
535 if (curwin->w_p_rl)
536 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
537 ' ', attr);
538 else
539#endif
540 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
541 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000542 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000543 {
544#ifdef FEAT_RIGHTLEFT
545 if (curwin->w_p_rl)
546 screen_putchar(' ', row, pum_col - pum_width,
547 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000548 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000549 else
550#endif
551 screen_putchar(' ', row, pum_col + pum_width,
552 i >= thumb_pos && i < thumb_pos + thumb_heigth
553 ? attr_thumb : attr_scroll);
554 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000555
556 ++row;
557 }
558}
559
Bram Moolenaar4b779472005-10-04 09:12:31 +0000560/*
561 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000562 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000563 * This may be repeated when the preview window is used:
564 * "repeat" == 0: open preview window normally
565 * "repeat" == 1: open preview window but don't set the size
566 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 * Returns TRUE when the window was resized and the location of the popup menu
568 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000569 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000570 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100571pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000572{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000573 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000574 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000575
Bram Moolenaar4b779472005-10-04 09:12:31 +0000576 pum_selected = n;
577
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000578 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000579 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000580 if (pum_first > pum_selected - 4)
581 {
582 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000583 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000584 if (pum_first > pum_selected - 2)
585 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000586 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000587 if (pum_first < 0)
588 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000589 else if (pum_first > pum_selected)
590 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000591 }
592 else
593 pum_first = pum_selected;
594 }
595 else if (pum_first < pum_selected - pum_height + 5)
596 {
597 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000598 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000599 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000600 {
601 pum_first += pum_height - 2;
602 if (pum_first < pum_selected - pum_height + 1)
603 pum_first = pum_selected - pum_height + 1;
604 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000605 else
606 pum_first = pum_selected - pum_height + 1;
607 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000608
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000609 /* Give a few lines of context when possible. */
610 if (context > 3)
611 context = 3;
612 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000613 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000614 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000615 {
616 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000617 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000618 if (pum_first < 0)
619 pum_first = 0;
620 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000621 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000622 {
623 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000624 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000625 }
626 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000627
Bram Moolenaar4033c552017-09-16 20:54:51 +0200628#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000629 /*
630 * Show extra info in the preview window if there is something and
631 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000632 * Skip this when tried twice already.
633 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000634 * NOTE: Be very careful not to sync undo!
635 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000636 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000637 && Rows > 10
638 && repeat <= 1
639 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000640 {
641 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200642 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000643 int res = OK;
644
Bram Moolenaaree236d02010-10-27 17:11:15 +0200645 /* Open a preview window. 3 lines by default. Prefer
646 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000647 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200648 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
649 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200650 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100651 /* Prevent undo sync here, if an autocommand syncs undo weird
652 * things can happen to the undo tree. */
653 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000654 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100655 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200656 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000657 g_do_tagpreview = 0;
658
659 if (curwin->w_p_pvw)
660 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200661 if (!resized
662 && curbuf->b_nwindows == 1
663 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000664 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
665 && curbuf->b_p_bh[0] == 'w')
666 {
667 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100668 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000669 ml_delete((linenr_T)1, FALSE);
670 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000671 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000672 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000673 /* Don't want to sync undo in the current buffer. */
674 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000675 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000676 --no_u_sync;
677 if (res == OK)
678 {
679 /* Edit a new, empty buffer. Set options for a "wipeout"
680 * buffer. */
681 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
682 set_option_value((char_u *)"bt", 0L,
683 (char_u *)"nofile", OPT_LOCAL);
684 set_option_value((char_u *)"bh", 0L,
685 (char_u *)"wipe", OPT_LOCAL);
686 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000687 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000688 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000689 }
690 if (res == OK)
691 {
692 char_u *p, *e;
693 linenr_T lnum = 0;
694
695 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
696 {
697 e = vim_strchr(p, '\n');
698 if (e == NULL)
699 {
700 ml_append(lnum++, p, 0, FALSE);
701 break;
702 }
703 else
704 {
705 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000706 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000707 *e = '\n';
708 p = e + 1;
709 }
710 }
711
712 /* Increase the height of the preview window to show the
713 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000714 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000715 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000716 if (lnum > p_pvh)
717 lnum = p_pvh;
718 if (curwin->w_height < lnum)
719 {
720 win_setheight((int)lnum);
721 resized = TRUE;
722 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000723 }
724
725 curbuf->b_changed = 0;
726 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100727 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000728 curwin->w_cursor.col = 0;
729
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200730 if ((curwin != curwin_save && win_valid(curwin_save))
731 || (curtab != curtab_save
732 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000733 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200734 if (curtab != curtab_save && valid_tabpage(curtab_save))
735 goto_tabpage_tp(curtab_save, FALSE, FALSE);
736
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200737 /* When the first completion is done and the preview
738 * window is not resized, skip the preview window's
739 * status line redrawing. */
740 if (ins_compl_active() && !resized)
741 curwin->w_redr_status = FALSE;
742
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000743 /* Return cursor to where we were */
744 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000745 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000746
747 /* When the preview window was resized we need to
748 * update the view on the buffer. Only go back to
749 * the window when needed, otherwise it will always be
750 * redraw. */
751 if (resized)
752 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100753 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000754 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100755 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000756 update_topline();
757 }
758
759 /* Update the screen before drawing the popup menu.
760 * Enable updating the status lines. */
761 pum_do_redraw = TRUE;
762 update_screen(0);
763 pum_do_redraw = FALSE;
764
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000765 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100766 {
767 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000768 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100769 --no_u_sync;
770 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000771
772 /* May need to update the screen again when there are
773 * autocommands involved. */
774 pum_do_redraw = TRUE;
775 update_screen(0);
776 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000777 }
778 }
779 }
780 }
781#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000782 }
783
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000784 if (!resized)
785 pum_redraw();
786
787 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000788}
789
790/*
791 * Undisplay the popup menu (later).
792 */
793 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100794pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000795{
796 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000797 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000798 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000799 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000800}
801
802/*
803 * Clear the popup menu. Currently only resets the offset to the first
804 * displayed item.
805 */
806 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100807pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000808{
809 pum_first = 0;
810}
811
812/*
813 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000814 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000815 */
816 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100817pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000818{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000819 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000820}
821
Bram Moolenaare3226be2005-12-18 22:10:00 +0000822/*
823 * Return the height of the popup menu, the number of entries visible.
824 * Only valid when pum_visible() returns TRUE!
825 */
826 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100827pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000828{
829 return pum_height;
830}
831
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100832# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100833static pumitem_T *balloon_array = NULL;
834static int balloon_arraysize;
835static int balloon_mouse_row = 0;
836static int balloon_mouse_col = 0;
837
Bram Moolenaar246fe032017-11-19 19:56:27 +0100838#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100839#define BALLOON_MIN_HEIGHT 10
840
Bram Moolenaar246fe032017-11-19 19:56:27 +0100841typedef struct {
842 char_u *start;
843 int bytelen;
844 int cells;
845 int indent;
846} balpart_T;
847
848/*
849 * Split a string into parts to display in the balloon.
850 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
851 * strings and make a struct look good.
852 * Resulting array is stored in "array" and returns the size of the array.
853 */
854 int
855split_message(char_u *mesg, pumitem_T **array)
856{
857 garray_T ga;
858 char_u *p;
859 balpart_T *item;
860 int quoted = FALSE;
861 int height;
862 int line;
863 int item_idx;
864 int indent = 0;
865 int max_cells = 0;
866 int max_height = Rows / 2 - 2;
867 int long_item_count = 0;
868 int split_long_items = FALSE;
869
870 ga_init2(&ga, sizeof(balpart_T), 20);
871 p = mesg;
872
873 while (*p != NUL)
874 {
875 if (ga_grow(&ga, 1) == FAIL)
876 goto failed;
877 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
878 item->start = p;
879 item->indent = indent;
880 item->cells = indent * 2;
881 ++ga.ga_len;
882 while (*p != NUL)
883 {
884 if (*p == '"')
885 quoted = !quoted;
886 else if (*p == '\\' && p[1] != NUL)
887 ++p;
888 else if (!quoted)
889 {
890 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
891 {
892 /* Looks like a good point to break. */
893 if (*p == '{')
894 ++indent;
895 else if (*p == '}' && indent > 0)
896 --indent;
897 ++item->cells;
898 p = skipwhite(p + 1);
899 break;
900 }
901 }
902 item->cells += ptr2cells(p);
903 p += MB_PTR2LEN(p);
904 }
905 item->bytelen = p - item->start;
906 if (item->cells > max_cells)
907 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100908 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100909 }
910
911 height = 2 + ga.ga_len;
912
913 /* If there are long items and the height is below the limit: split lines */
914 if (long_item_count > 0 && height + long_item_count <= max_height)
915 {
916 split_long_items = TRUE;
917 height += long_item_count;
918 }
919
920 /* Limit to half the window height, it has to fit above or below the mouse
921 * position. */
922 if (height > max_height)
923 height = max_height;
924 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
925 if (*array == NULL)
926 goto failed;
927
928 /* Add an empty line above and below, looks better. */
929 (*array)->pum_text = vim_strsave((char_u *)"");
930 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
931
932 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
933 {
934 int skip;
935 int thislen;
936 int copylen;
937 int ind;
938 int cells;
939
940 item = ((balpart_T *)ga.ga_data) + item_idx;
941 for (skip = 0; skip < item->bytelen; skip += thislen)
942 {
943 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
944 {
945 cells = item->indent * 2;
946 for (p = item->start + skip; p < item->start + item->bytelen;
947 p += MB_PTR2LEN(p))
948 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
949 break;
950 thislen = p - (item->start + skip);
951 }
952 else
953 thislen = item->bytelen;
954
955 /* put indent at the start */
956 p = alloc(thislen + item->indent * 2 + 1);
957 for (ind = 0; ind < item->indent * 2; ++ind)
958 p[ind] = ' ';
959
960 /* exclude spaces at the end of the string */
961 for (copylen = thislen; copylen > 0; --copylen)
962 if (item->start[skip + copylen - 1] != ' ')
963 break;
964
965 vim_strncpy(p + ind, item->start + skip, copylen);
966 (*array)[line].pum_text = p;
967 item->indent = 0; /* wrapped line has no indent */
968 ++line;
969 }
970 }
971 ga_clear(&ga);
972 return height;
973
974failed:
975 ga_clear(&ga);
976 return 0;
977}
978
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100979 void
980ui_remove_balloon(void)
981{
982 if (balloon_array != NULL)
983 {
984 pum_undisplay();
985 while (balloon_arraysize > 0)
986 vim_free(balloon_array[--balloon_arraysize].pum_text);
987 vim_free(balloon_array);
988 balloon_array = NULL;
989 }
990}
991
992/*
993 * Terminal version of a balloon, uses the popup menu code.
994 */
995 void
Bram Moolenaar246fe032017-11-19 19:56:27 +0100996ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100997{
998 ui_remove_balloon();
999
Bram Moolenaar246fe032017-11-19 19:56:27 +01001000 if (mesg == NULL && list == NULL)
1001 return;
1002 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001003 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001004 listitem_T *li;
1005 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001006
Bram Moolenaar246fe032017-11-19 19:56:27 +01001007 balloon_arraysize = list->lv_len;
1008 balloon_array = (pumitem_T *)alloc_clear(
1009 (unsigned)sizeof(pumitem_T) * list->lv_len);
1010 if (balloon_array == NULL)
1011 return;
1012 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1013 {
1014 char_u *text = get_tv_string_chk(&li->li_tv);
1015
1016 balloon_array[idx].pum_text = vim_strsave(
1017 text == NULL ? (char_u *)"" : text);
1018 }
1019 }
1020 else
1021 balloon_arraysize = split_message(mesg, &balloon_array);
1022
1023 if (balloon_arraysize > 0)
1024 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001025 pum_array = balloon_array;
1026 pum_size = balloon_arraysize;
1027 pum_compute_size();
1028 pum_scrollbar = 0;
1029 pum_height = balloon_arraysize;
1030
Bram Moolenaar246fe032017-11-19 19:56:27 +01001031 if (Rows - mouse_row > pum_size)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001032 {
1033 /* Enough space below the mouse row. */
1034 pum_row = mouse_row + 1;
1035 if (pum_height > Rows - pum_row)
1036 pum_height = Rows - pum_row;
1037 }
1038 else
1039 {
1040 /* Show above the mouse row, reduce height if it does not fit. */
Bram Moolenaar246fe032017-11-19 19:56:27 +01001041 pum_row = mouse_row - pum_size;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001042 if (pum_row < 0)
1043 {
1044 pum_height += pum_row;
1045 pum_row = 0;
1046 }
1047 }
1048 if (Columns - mouse_col >= pum_base_width
1049 || Columns - mouse_col > BALLOON_MIN_WIDTH)
1050 /* Enough space to show at mouse column. */
1051 pum_col = mouse_col;
1052 else
1053 /* Not enough space, right align with window. */
1054 pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH
1055 ? BALLOON_MIN_WIDTH : pum_base_width);
1056
1057 pum_width = Columns - pum_col;
1058 if (pum_width > pum_base_width + 1)
1059 pum_width = pum_base_width + 1;
1060
1061 pum_selected = -1;
1062 pum_first = 0;
1063 pum_redraw();
1064 }
1065}
1066
1067/*
1068 * Called when the mouse moved, may remove any displayed balloon.
1069 */
1070 void
1071ui_may_remove_balloon(void)
1072{
1073 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
1074 ui_remove_balloon();
1075}
1076# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001077
Bram Moolenaar4b779472005-10-04 09:12:31 +00001078#endif