blob: 2839ea1997f96015fcba84a1695e35ff318391c2 [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/*
70 * Show the popup menu with items "array[size]".
71 * "array" must remain valid until pum_undisplay() is called!
72 * When possible the leftmost character is aligned with screen column "col".
73 * The menu appears above the screen line "row" or at "row" + "height" - 1.
74 */
75 void
Bram Moolenaar05540972016-01-30 20:31:25 +010076pum_display(
77 pumitem_T *array,
78 int size,
79 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000080 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000081{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000082 int def_width;
83 int max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +000084 int row;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000085 int context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +000086 int col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010087 int above_row;
88 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000089 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020090#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010091 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +010092#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093
Bram Moolenaara5e66212017-09-29 22:42:33 +020094 do
95 {
Bram Moolenaar42443c72018-02-10 18:28:52 +010096 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +020097 above_row = 0;
98 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000099
Bram Moolenaara5e66212017-09-29 22:42:33 +0200100 /* Pretend the pum is already there to avoid that must_redraw is set
101 * when 'cuc' is on. */
102 pum_array = (pumitem_T *)1;
103 validate_cursor_col();
104 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000105
Bram Moolenaara5e66212017-09-29 22:42:33 +0200106 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000107
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200109 FOR_ALL_WINDOWS(pvwin)
110 if (pvwin->w_p_pvw)
111 break;
112 if (pvwin != NULL)
113 {
114 if (W_WINROW(pvwin) < W_WINROW(curwin))
115 above_row = W_WINROW(pvwin) + pvwin->w_height;
116 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
117 below_row = W_WINROW(pvwin);
118 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100119#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000120
Bram Moolenaara5e66212017-09-29 22:42:33 +0200121 /*
122 * Figure out the size and position of the pum.
123 */
124 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000125 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000126 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200127 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000128 if (p_ph > 0 && pum_height > p_ph)
129 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000130
Bram Moolenaara5e66212017-09-29 22:42:33 +0200131 /* Put the pum below "row" if possible. If there are few lines decide
132 * on where there is more room. */
133 if (row + 2 >= below_row - pum_height
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100134 && row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200135 {
136 /* pum above "row" */
137
138 /* Leave two lines of context if possible */
139 if (curwin->w_wrow - curwin->w_cline_row >= 2)
140 context_lines = 2;
141 else
142 context_lines = curwin->w_wrow - curwin->w_cline_row;
143
144 if (row >= size + context_lines)
145 {
146 pum_row = row - size - context_lines;
147 pum_height = size;
148 }
149 else
150 {
151 pum_row = 0;
152 pum_height = row - context_lines;
153 }
154 if (p_ph > 0 && pum_height > p_ph)
155 {
156 pum_row += pum_height - p_ph;
157 pum_height = p_ph;
158 }
159 }
160 else
161 {
162 /* pum below "row" */
163
164 /* Leave two lines of context if possible */
165 if (curwin->w_cline_row
166 + curwin->w_cline_height - curwin->w_wrow >= 3)
167 context_lines = 3;
168 else
169 context_lines = curwin->w_cline_row
170 + curwin->w_cline_height - curwin->w_wrow;
171
172 pum_row = row + context_lines;
173 if (size > below_row - pum_row)
174 pum_height = below_row - pum_row;
175 else
176 pum_height = size;
177 if (p_ph > 0 && pum_height > p_ph)
178 pum_height = p_ph;
179 }
180
181 /* don't display when we only have room for one line */
182 if (pum_height < 1 || (pum_height == 1 && size > 1))
183 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000184
Bram Moolenaar4033c552017-09-16 20:54:51 +0200185#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200186 /* If there is a preview window at the above avoid drawing over it. */
187 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000188 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200189 pum_row += above_row;
190 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000191 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200192#endif
193
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100194 pum_array = array;
195 pum_size = size;
196 pum_compute_size();
197 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000198
Bram Moolenaara5e66212017-09-29 22:42:33 +0200199 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000200#ifdef FEAT_RIGHTLEFT
201 if (curwin->w_p_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200202 col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000203 else
204#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200205 col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000206
Bram Moolenaara5e66212017-09-29 22:42:33 +0200207 /* if there are more items than room we need a scrollbar */
208 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000209 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210 pum_scrollbar = 1;
211 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000212 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000213 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214 pum_scrollbar = 0;
215
216 if (def_width < max_width)
217 def_width = max_width;
218
Bram Moolenaar42443c72018-02-10 18:28:52 +0100219 if (((col < Columns - p_pw || col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000220#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200221 && !curwin->w_p_rl)
Bram Moolenaar42443c72018-02-10 18:28:52 +0100222 || (curwin->w_p_rl && (col > p_pw || col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000223#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200224 ))
225 {
226 /* align pum column with "col" */
227 pum_col = col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000228
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100229 /* start with the maximum space available */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200230#ifdef FEAT_RIGHTLEFT
231 if (curwin->w_p_rl)
232 pum_width = pum_col - pum_scrollbar + 1;
233 else
234#endif
235 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000236
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100237 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100238 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200239 {
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100240 /* the width is too much, make it narrower */
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100241 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100242 if (pum_width < p_pw)
243 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200244 }
Bram Moolenaar42443c72018-02-10 18:28:52 +0100245 else if (((col > p_pw || col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100246#ifdef FEAT_RIGHTLEFT
247 && !curwin->w_p_rl)
Bram Moolenaar42443c72018-02-10 18:28:52 +0100248 || (curwin->w_p_rl && (col < Columns - p_pw
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100249 || col < Columns - max_width)
250#endif
251 ))
252 {
253 /* align right pum edge with "col" */
254#ifdef FEAT_RIGHTLEFT
255 if (curwin->w_p_rl)
256 {
257 pum_col = col + max_width + pum_scrollbar + 1;
258 if (pum_col >= Columns)
259 pum_col = Columns - 1;
260 }
261 else
262#endif
263 {
264 pum_col = col - max_width - pum_scrollbar;
265 if (pum_col < 0)
266 pum_col = 0;
267 }
268
269#ifdef FEAT_RIGHTLEFT
270 if (curwin->w_p_rl)
271 pum_width = W_ENDCOL(curwin) - pum_col - pum_scrollbar + 1;
272 else
273#endif
274 pum_width = pum_col - pum_scrollbar;
275
Bram Moolenaar42443c72018-02-10 18:28:52 +0100276 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100277 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100278 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100279#ifdef FEAT_RIGHTLEFT
280 if (curwin->w_p_rl)
281 {
282 if (pum_width > pum_col)
283 pum_width = pum_col;
284 }
285 else
286#endif
287 {
288 if (pum_width >= Columns - pum_col)
289 pum_width = Columns - pum_col - 1;
290 }
291 }
292 else if (pum_width > max_width + pum_kind_width
293 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100294 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295 {
296 pum_width = max_width + pum_kind_width
297 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100298 if (pum_width < p_pw)
299 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300 }
301 }
302
Bram Moolenaara5e66212017-09-29 22:42:33 +0200303 }
304 else if (Columns < def_width)
305 {
306 /* not enough room, will use what we have */
307#ifdef FEAT_RIGHTLEFT
308 if (curwin->w_p_rl)
309 pum_col = Columns - 1;
310 else
311#endif
312 pum_col = 0;
313 pum_width = Columns - 1;
314 }
315 else
316 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100317 if (max_width > p_pw)
318 max_width = p_pw; /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200319#ifdef FEAT_RIGHTLEFT
320 if (curwin->w_p_rl)
321 pum_col = max_width - 1;
322 else
323#endif
324 pum_col = Columns - max_width;
325 pum_width = max_width - pum_scrollbar;
326 }
327
Bram Moolenaara5e66212017-09-29 22:42:33 +0200328 /* Set selected item and redraw. If the window size changed need to
329 * redo the positioning. Limit this to two times, when there is not
330 * much room the window size will keep changing. */
331 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000332}
333
334/*
335 * Redraw the popup menu, using "pum_first" and "pum_selected".
336 */
337 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100338pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000339{
340 int row = pum_row;
341 int col;
342 int attr_norm = highlight_attr[HLF_PNI];
343 int attr_select = highlight_attr[HLF_PSI];
344 int attr_scroll = highlight_attr[HLF_PSB];
345 int attr_thumb = highlight_attr[HLF_PST];
346 int attr;
347 int i;
348 int idx;
349 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000350 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000351 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000352 int thumb_pos = 0;
353 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000354 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000355 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000356
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100357 /* Never display more than we have */
358 if (pum_first > pum_size - pum_height)
359 pum_first = pum_size - pum_height;
360
Bram Moolenaar4b779472005-10-04 09:12:31 +0000361 if (pum_scrollbar)
362 {
363 thumb_heigth = pum_height * pum_height / pum_size;
364 if (thumb_heigth == 0)
365 thumb_heigth = 1;
366 thumb_pos = (pum_first * (pum_height - thumb_heigth)
367 + (pum_size - pum_height) / 2)
368 / (pum_size - pum_height);
369 }
370
371 for (i = 0; i < pum_height; ++i)
372 {
373 idx = i + pum_first;
374 attr = (idx == pum_selected) ? attr_select : attr_norm;
375
376 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000377#ifdef FEAT_RIGHTLEFT
378 if (curwin->w_p_rl)
379 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200380 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000381 screen_putchar(' ', row, pum_col + 1, attr);
382 }
383 else
384#endif
385 if (pum_col > 0)
386 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000387
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000388 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000389 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000390 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000391 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000392 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000393 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000394 width = 0;
395 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000396 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000397 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000398 case 1: p = pum_array[idx].pum_text; break;
399 case 2: p = pum_array[idx].pum_kind; break;
400 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000401 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000402 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100403 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000404 {
405 if (s == NULL)
406 s = p;
407 w = ptr2cells(p);
408 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
409 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000410 /* Display the text that fits or comes before a Tab.
411 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000412 char_u *st;
413 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000414
415 *p = NUL;
416 st = transstr(s);
417 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000418#ifdef FEAT_RIGHTLEFT
419 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000420 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000421 if (st != NULL)
422 {
423 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000424
425 if (rt != NULL)
426 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100427 char_u *rt_start = rt;
428 int size;
429
430 size = vim_strsize(rt);
431 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000432 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100433 do
434 {
435 size -= has_mbyte
436 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100437 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100438 } while (size > pum_width);
439
440 if (size < pum_width)
441 {
442 /* Most left character requires
443 * 2-cells but only 1 cell is
444 * available on screen. Put a
445 * '<' on the left of the pum
446 * item */
447 *(--rt) = '<';
448 size++;
449 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000450 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100451 screen_puts_len(rt, (int)STRLEN(rt),
452 row, col - size + 1, attr);
453 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000454 }
455 vim_free(st);
456 }
457 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000458 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000459 else
460#endif
461 {
462 if (st != NULL)
463 {
464 screen_puts_len(st, (int)STRLEN(st), row, col,
465 attr);
466 vim_free(st);
467 }
468 col += width;
469 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000470
471 if (*p != TAB)
472 break;
473
474 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000475#ifdef FEAT_RIGHTLEFT
476 if (curwin->w_p_rl)
477 {
478 screen_puts_len((char_u *)" ", 2, row, col - 1,
479 attr);
480 col -= 2;
481 }
482 else
483#endif
484 {
485 screen_puts_len((char_u *)" ", 2, row, col, attr);
486 col += 2;
487 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000488 totwidth += 2;
489 s = NULL; /* start text at next char */
490 width = 0;
491 }
492 else
493 width += w;
494 }
495
496 if (round > 1)
497 n = pum_kind_width + 1;
498 else
499 n = 1;
500
501 /* Stop when there is nothing more to display. */
502 if (round == 3
503 || (round == 2 && pum_array[idx].pum_extra == NULL)
504 || (round == 1 && pum_array[idx].pum_kind == NULL
505 && pum_array[idx].pum_extra == NULL)
506 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000507 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000508#ifdef FEAT_RIGHTLEFT
509 if (curwin->w_p_rl)
510 {
511 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
512 col + 1, ' ', ' ', attr);
513 col = pum_col - pum_base_width - n + 1;
514 }
515 else
516#endif
517 {
518 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000519 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000520 col = pum_col + pum_base_width + n;
521 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000522 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000523 }
524
Bram Moolenaarabc97732007-08-08 20:49:37 +0000525#ifdef FEAT_RIGHTLEFT
526 if (curwin->w_p_rl)
527 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
528 ' ', attr);
529 else
530#endif
531 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
532 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000533 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000534 {
535#ifdef FEAT_RIGHTLEFT
536 if (curwin->w_p_rl)
537 screen_putchar(' ', row, pum_col - pum_width,
538 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000540 else
541#endif
542 screen_putchar(' ', row, pum_col + pum_width,
543 i >= thumb_pos && i < thumb_pos + thumb_heigth
544 ? attr_thumb : attr_scroll);
545 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000546
547 ++row;
548 }
549}
550
Bram Moolenaar4b779472005-10-04 09:12:31 +0000551/*
552 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000553 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000554 * This may be repeated when the preview window is used:
555 * "repeat" == 0: open preview window normally
556 * "repeat" == 1: open preview window but don't set the size
557 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000558 * Returns TRUE when the window was resized and the location of the popup menu
559 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000560 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000563{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000564 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000565 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000566
Bram Moolenaar4b779472005-10-04 09:12:31 +0000567 pum_selected = n;
568
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000569 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000570 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000571 if (pum_first > pum_selected - 4)
572 {
573 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000574 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000575 if (pum_first > pum_selected - 2)
576 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000577 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000578 if (pum_first < 0)
579 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000580 else if (pum_first > pum_selected)
581 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000582 }
583 else
584 pum_first = pum_selected;
585 }
586 else if (pum_first < pum_selected - pum_height + 5)
587 {
588 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000589 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000590 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000591 {
592 pum_first += pum_height - 2;
593 if (pum_first < pum_selected - pum_height + 1)
594 pum_first = pum_selected - pum_height + 1;
595 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000596 else
597 pum_first = pum_selected - pum_height + 1;
598 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000599
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000600 /* Give a few lines of context when possible. */
601 if (context > 3)
602 context = 3;
603 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000604 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000605 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000606 {
607 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000608 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000609 if (pum_first < 0)
610 pum_first = 0;
611 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000612 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000613 {
614 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000615 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616 }
617 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000618
Bram Moolenaar4033c552017-09-16 20:54:51 +0200619#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000620 /*
621 * Show extra info in the preview window if there is something and
622 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000623 * Skip this when tried twice already.
624 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000625 * NOTE: Be very careful not to sync undo!
626 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000627 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000628 && Rows > 10
629 && repeat <= 1
630 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000631 {
632 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200633 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000634 int res = OK;
635
Bram Moolenaaree236d02010-10-27 17:11:15 +0200636 /* Open a preview window. 3 lines by default. Prefer
637 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000638 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200639 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
640 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200641 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100642 /* Prevent undo sync here, if an autocommand syncs undo weird
643 * things can happen to the undo tree. */
644 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000645 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100646 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200647 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000648 g_do_tagpreview = 0;
649
650 if (curwin->w_p_pvw)
651 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200652 if (!resized
653 && curbuf->b_nwindows == 1
654 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000655 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
656 && curbuf->b_p_bh[0] == 'w')
657 {
658 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100659 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000660 ml_delete((linenr_T)1, FALSE);
661 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000662 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000663 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000664 /* Don't want to sync undo in the current buffer. */
665 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000666 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000667 --no_u_sync;
668 if (res == OK)
669 {
670 /* Edit a new, empty buffer. Set options for a "wipeout"
671 * buffer. */
672 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
673 set_option_value((char_u *)"bt", 0L,
674 (char_u *)"nofile", OPT_LOCAL);
675 set_option_value((char_u *)"bh", 0L,
676 (char_u *)"wipe", OPT_LOCAL);
677 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000678 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000679 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000680 }
681 if (res == OK)
682 {
683 char_u *p, *e;
684 linenr_T lnum = 0;
685
686 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
687 {
688 e = vim_strchr(p, '\n');
689 if (e == NULL)
690 {
691 ml_append(lnum++, p, 0, FALSE);
692 break;
693 }
694 else
695 {
696 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000697 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000698 *e = '\n';
699 p = e + 1;
700 }
701 }
702
703 /* Increase the height of the preview window to show the
704 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000705 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000706 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000707 if (lnum > p_pvh)
708 lnum = p_pvh;
709 if (curwin->w_height < lnum)
710 {
711 win_setheight((int)lnum);
712 resized = TRUE;
713 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000714 }
715
716 curbuf->b_changed = 0;
717 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100718 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000719 curwin->w_cursor.col = 0;
720
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200721 if ((curwin != curwin_save && win_valid(curwin_save))
722 || (curtab != curtab_save
723 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000724 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200725 if (curtab != curtab_save && valid_tabpage(curtab_save))
726 goto_tabpage_tp(curtab_save, FALSE, FALSE);
727
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200728 /* When the first completion is done and the preview
729 * window is not resized, skip the preview window's
730 * status line redrawing. */
731 if (ins_compl_active() && !resized)
732 curwin->w_redr_status = FALSE;
733
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000734 /* Return cursor to where we were */
735 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000736 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000737
738 /* When the preview window was resized we need to
739 * update the view on the buffer. Only go back to
740 * the window when needed, otherwise it will always be
741 * redraw. */
742 if (resized)
743 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100744 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000745 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100746 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000747 update_topline();
748 }
749
750 /* Update the screen before drawing the popup menu.
751 * Enable updating the status lines. */
752 pum_do_redraw = TRUE;
753 update_screen(0);
754 pum_do_redraw = FALSE;
755
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000756 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100757 {
758 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000759 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100760 --no_u_sync;
761 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000762
763 /* May need to update the screen again when there are
764 * autocommands involved. */
765 pum_do_redraw = TRUE;
766 update_screen(0);
767 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000768 }
769 }
770 }
771 }
772#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000773 }
774
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000775 if (!resized)
776 pum_redraw();
777
778 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000779}
780
781/*
782 * Undisplay the popup menu (later).
783 */
784 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100785pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000786{
787 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000788 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000789 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000790 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000791}
792
793/*
794 * Clear the popup menu. Currently only resets the offset to the first
795 * displayed item.
796 */
797 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100798pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000799{
800 pum_first = 0;
801}
802
803/*
804 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000805 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000806 */
807 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100808pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000809{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000810 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000811}
812
Bram Moolenaare3226be2005-12-18 22:10:00 +0000813/*
814 * Return the height of the popup menu, the number of entries visible.
815 * Only valid when pum_visible() returns TRUE!
816 */
817 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100818pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000819{
820 return pum_height;
821}
822
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100823# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100824static pumitem_T *balloon_array = NULL;
825static int balloon_arraysize;
826static int balloon_mouse_row = 0;
827static int balloon_mouse_col = 0;
828
Bram Moolenaar246fe032017-11-19 19:56:27 +0100829#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100830#define BALLOON_MIN_HEIGHT 10
831
Bram Moolenaar246fe032017-11-19 19:56:27 +0100832typedef struct {
833 char_u *start;
834 int bytelen;
835 int cells;
836 int indent;
837} balpart_T;
838
839/*
840 * Split a string into parts to display in the balloon.
841 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
842 * strings and make a struct look good.
843 * Resulting array is stored in "array" and returns the size of the array.
844 */
845 int
846split_message(char_u *mesg, pumitem_T **array)
847{
848 garray_T ga;
849 char_u *p;
850 balpart_T *item;
851 int quoted = FALSE;
852 int height;
853 int line;
854 int item_idx;
855 int indent = 0;
856 int max_cells = 0;
857 int max_height = Rows / 2 - 2;
858 int long_item_count = 0;
859 int split_long_items = FALSE;
860
861 ga_init2(&ga, sizeof(balpart_T), 20);
862 p = mesg;
863
864 while (*p != NUL)
865 {
866 if (ga_grow(&ga, 1) == FAIL)
867 goto failed;
868 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
869 item->start = p;
870 item->indent = indent;
871 item->cells = indent * 2;
872 ++ga.ga_len;
873 while (*p != NUL)
874 {
875 if (*p == '"')
876 quoted = !quoted;
877 else if (*p == '\\' && p[1] != NUL)
878 ++p;
879 else if (!quoted)
880 {
881 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
882 {
883 /* Looks like a good point to break. */
884 if (*p == '{')
885 ++indent;
886 else if (*p == '}' && indent > 0)
887 --indent;
888 ++item->cells;
889 p = skipwhite(p + 1);
890 break;
891 }
892 }
893 item->cells += ptr2cells(p);
894 p += MB_PTR2LEN(p);
895 }
896 item->bytelen = p - item->start;
897 if (item->cells > max_cells)
898 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100899 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100900 }
901
902 height = 2 + ga.ga_len;
903
904 /* If there are long items and the height is below the limit: split lines */
905 if (long_item_count > 0 && height + long_item_count <= max_height)
906 {
907 split_long_items = TRUE;
908 height += long_item_count;
909 }
910
911 /* Limit to half the window height, it has to fit above or below the mouse
912 * position. */
913 if (height > max_height)
914 height = max_height;
915 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
916 if (*array == NULL)
917 goto failed;
918
919 /* Add an empty line above and below, looks better. */
920 (*array)->pum_text = vim_strsave((char_u *)"");
921 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
922
923 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
924 {
925 int skip;
926 int thislen;
927 int copylen;
928 int ind;
929 int cells;
930
931 item = ((balpart_T *)ga.ga_data) + item_idx;
932 for (skip = 0; skip < item->bytelen; skip += thislen)
933 {
934 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
935 {
936 cells = item->indent * 2;
937 for (p = item->start + skip; p < item->start + item->bytelen;
938 p += MB_PTR2LEN(p))
939 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
940 break;
941 thislen = p - (item->start + skip);
942 }
943 else
944 thislen = item->bytelen;
945
946 /* put indent at the start */
947 p = alloc(thislen + item->indent * 2 + 1);
948 for (ind = 0; ind < item->indent * 2; ++ind)
949 p[ind] = ' ';
950
951 /* exclude spaces at the end of the string */
952 for (copylen = thislen; copylen > 0; --copylen)
953 if (item->start[skip + copylen - 1] != ' ')
954 break;
955
956 vim_strncpy(p + ind, item->start + skip, copylen);
957 (*array)[line].pum_text = p;
958 item->indent = 0; /* wrapped line has no indent */
959 ++line;
960 }
961 }
962 ga_clear(&ga);
963 return height;
964
965failed:
966 ga_clear(&ga);
967 return 0;
968}
969
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100970 void
971ui_remove_balloon(void)
972{
973 if (balloon_array != NULL)
974 {
975 pum_undisplay();
976 while (balloon_arraysize > 0)
977 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +0100978 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100979 }
980}
981
982/*
983 * Terminal version of a balloon, uses the popup menu code.
984 */
985 void
Bram Moolenaar246fe032017-11-19 19:56:27 +0100986ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100987{
988 ui_remove_balloon();
989
Bram Moolenaar246fe032017-11-19 19:56:27 +0100990 if (mesg == NULL && list == NULL)
991 return;
992 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100993 {
Bram Moolenaar246fe032017-11-19 19:56:27 +0100994 listitem_T *li;
995 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100996
Bram Moolenaar246fe032017-11-19 19:56:27 +0100997 balloon_arraysize = list->lv_len;
998 balloon_array = (pumitem_T *)alloc_clear(
999 (unsigned)sizeof(pumitem_T) * list->lv_len);
1000 if (balloon_array == NULL)
1001 return;
1002 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1003 {
1004 char_u *text = get_tv_string_chk(&li->li_tv);
1005
1006 balloon_array[idx].pum_text = vim_strsave(
1007 text == NULL ? (char_u *)"" : text);
1008 }
1009 }
1010 else
1011 balloon_arraysize = split_message(mesg, &balloon_array);
1012
1013 if (balloon_arraysize > 0)
1014 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001015 pum_array = balloon_array;
1016 pum_size = balloon_arraysize;
1017 pum_compute_size();
1018 pum_scrollbar = 0;
1019 pum_height = balloon_arraysize;
1020
Bram Moolenaar246fe032017-11-19 19:56:27 +01001021 if (Rows - mouse_row > pum_size)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001022 {
1023 /* Enough space below the mouse row. */
1024 pum_row = mouse_row + 1;
1025 if (pum_height > Rows - pum_row)
1026 pum_height = Rows - pum_row;
1027 }
1028 else
1029 {
1030 /* Show above the mouse row, reduce height if it does not fit. */
Bram Moolenaar246fe032017-11-19 19:56:27 +01001031 pum_row = mouse_row - pum_size;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001032 if (pum_row < 0)
1033 {
1034 pum_height += pum_row;
1035 pum_row = 0;
1036 }
1037 }
1038 if (Columns - mouse_col >= pum_base_width
1039 || Columns - mouse_col > BALLOON_MIN_WIDTH)
1040 /* Enough space to show at mouse column. */
1041 pum_col = mouse_col;
1042 else
1043 /* Not enough space, right align with window. */
1044 pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH
1045 ? BALLOON_MIN_WIDTH : pum_base_width);
1046
1047 pum_width = Columns - pum_col;
1048 if (pum_width > pum_base_width + 1)
1049 pum_width = pum_base_width + 1;
1050
1051 pum_selected = -1;
1052 pum_first = 0;
1053 pum_redraw();
1054 }
1055}
1056
1057/*
1058 * Called when the mouse moved, may remove any displayed balloon.
1059 */
1060 void
1061ui_may_remove_balloon(void)
1062{
1063 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
1064 ui_remove_balloon();
1065}
1066# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001067
Bram Moolenaar4b779472005-10-04 09:12:31 +00001068#endif