blob: dfdcca072e5de801b84fb764ae5123a3b4f9062d [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
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100255 if (curwin->w_p_rl
256 && col < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100257 {
258 pum_col = col + max_width + pum_scrollbar + 1;
259 if (pum_col >= Columns)
260 pum_col = Columns - 1;
261 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100262 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100263#endif
264 {
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100265 if (col > Columns - max_width - pum_scrollbar)
266 {
267 pum_col = Columns - max_width - pum_scrollbar;
268 if (pum_col < 0)
269 pum_col = 0;
270 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100271 }
272
273#ifdef FEAT_RIGHTLEFT
274 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100275 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100276 else
277#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100278 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100279
Bram Moolenaar42443c72018-02-10 18:28:52 +0100280 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100281 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100282 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100283#ifdef FEAT_RIGHTLEFT
284 if (curwin->w_p_rl)
285 {
286 if (pum_width > pum_col)
287 pum_width = pum_col;
288 }
289 else
290#endif
291 {
292 if (pum_width >= Columns - pum_col)
293 pum_width = Columns - pum_col - 1;
294 }
295 }
296 else if (pum_width > max_width + pum_kind_width
297 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100298 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100299 {
300 pum_width = max_width + pum_kind_width
301 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100302 if (pum_width < p_pw)
303 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304 }
305 }
306
Bram Moolenaara5e66212017-09-29 22:42:33 +0200307 }
308 else if (Columns < def_width)
309 {
310 /* not enough room, will use what we have */
311#ifdef FEAT_RIGHTLEFT
312 if (curwin->w_p_rl)
313 pum_col = Columns - 1;
314 else
315#endif
316 pum_col = 0;
317 pum_width = Columns - 1;
318 }
319 else
320 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100321 if (max_width > p_pw)
322 max_width = p_pw; /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200323#ifdef FEAT_RIGHTLEFT
324 if (curwin->w_p_rl)
325 pum_col = max_width - 1;
326 else
327#endif
328 pum_col = Columns - max_width;
329 pum_width = max_width - pum_scrollbar;
330 }
331
Bram Moolenaara5e66212017-09-29 22:42:33 +0200332 /* Set selected item and redraw. If the window size changed need to
333 * redo the positioning. Limit this to two times, when there is not
334 * much room the window size will keep changing. */
335 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000336}
337
338/*
339 * Redraw the popup menu, using "pum_first" and "pum_selected".
340 */
341 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100342pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000343{
344 int row = pum_row;
345 int col;
346 int attr_norm = highlight_attr[HLF_PNI];
347 int attr_select = highlight_attr[HLF_PSI];
348 int attr_scroll = highlight_attr[HLF_PSB];
349 int attr_thumb = highlight_attr[HLF_PST];
350 int attr;
351 int i;
352 int idx;
353 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000354 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000355 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000356 int thumb_pos = 0;
357 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000358 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000359 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000360
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100361 /* Never display more than we have */
362 if (pum_first > pum_size - pum_height)
363 pum_first = pum_size - pum_height;
364
Bram Moolenaar4b779472005-10-04 09:12:31 +0000365 if (pum_scrollbar)
366 {
367 thumb_heigth = pum_height * pum_height / pum_size;
368 if (thumb_heigth == 0)
369 thumb_heigth = 1;
370 thumb_pos = (pum_first * (pum_height - thumb_heigth)
371 + (pum_size - pum_height) / 2)
372 / (pum_size - pum_height);
373 }
374
375 for (i = 0; i < pum_height; ++i)
376 {
377 idx = i + pum_first;
378 attr = (idx == pum_selected) ? attr_select : attr_norm;
379
380 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000381#ifdef FEAT_RIGHTLEFT
382 if (curwin->w_p_rl)
383 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200384 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000385 screen_putchar(' ', row, pum_col + 1, attr);
386 }
387 else
388#endif
389 if (pum_col > 0)
390 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000391
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000392 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000393 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000394 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000395 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000396 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000397 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000398 width = 0;
399 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000400 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000401 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000402 case 1: p = pum_array[idx].pum_text; break;
403 case 2: p = pum_array[idx].pum_kind; break;
404 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000405 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000406 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100407 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000408 {
409 if (s == NULL)
410 s = p;
411 w = ptr2cells(p);
412 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
413 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000414 /* Display the text that fits or comes before a Tab.
415 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000416 char_u *st;
417 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000418
419 *p = NUL;
420 st = transstr(s);
421 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000422#ifdef FEAT_RIGHTLEFT
423 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000424 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000425 if (st != NULL)
426 {
427 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000428
429 if (rt != NULL)
430 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100431 char_u *rt_start = rt;
432 int size;
433
434 size = vim_strsize(rt);
435 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000436 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100437 do
438 {
439 size -= has_mbyte
440 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100441 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100442 } while (size > pum_width);
443
444 if (size < pum_width)
445 {
446 /* Most left character requires
447 * 2-cells but only 1 cell is
448 * available on screen. Put a
449 * '<' on the left of the pum
450 * item */
451 *(--rt) = '<';
452 size++;
453 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000454 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100455 screen_puts_len(rt, (int)STRLEN(rt),
456 row, col - size + 1, attr);
457 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000458 }
459 vim_free(st);
460 }
461 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000462 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000463 else
464#endif
465 {
466 if (st != NULL)
467 {
468 screen_puts_len(st, (int)STRLEN(st), row, col,
469 attr);
470 vim_free(st);
471 }
472 col += width;
473 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000474
475 if (*p != TAB)
476 break;
477
478 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000479#ifdef FEAT_RIGHTLEFT
480 if (curwin->w_p_rl)
481 {
482 screen_puts_len((char_u *)" ", 2, row, col - 1,
483 attr);
484 col -= 2;
485 }
486 else
487#endif
488 {
489 screen_puts_len((char_u *)" ", 2, row, col, attr);
490 col += 2;
491 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000492 totwidth += 2;
493 s = NULL; /* start text at next char */
494 width = 0;
495 }
496 else
497 width += w;
498 }
499
500 if (round > 1)
501 n = pum_kind_width + 1;
502 else
503 n = 1;
504
505 /* Stop when there is nothing more to display. */
506 if (round == 3
507 || (round == 2 && pum_array[idx].pum_extra == NULL)
508 || (round == 1 && pum_array[idx].pum_kind == NULL
509 && pum_array[idx].pum_extra == NULL)
510 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000511 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000512#ifdef FEAT_RIGHTLEFT
513 if (curwin->w_p_rl)
514 {
515 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
516 col + 1, ' ', ' ', attr);
517 col = pum_col - pum_base_width - n + 1;
518 }
519 else
520#endif
521 {
522 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000523 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000524 col = pum_col + pum_base_width + n;
525 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000526 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000527 }
528
Bram Moolenaarabc97732007-08-08 20:49:37 +0000529#ifdef FEAT_RIGHTLEFT
530 if (curwin->w_p_rl)
531 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
532 ' ', attr);
533 else
534#endif
535 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
536 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000537 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000538 {
539#ifdef FEAT_RIGHTLEFT
540 if (curwin->w_p_rl)
541 screen_putchar(' ', row, pum_col - pum_width,
542 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000543 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000544 else
545#endif
546 screen_putchar(' ', row, pum_col + pum_width,
547 i >= thumb_pos && i < thumb_pos + thumb_heigth
548 ? attr_thumb : attr_scroll);
549 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000550
551 ++row;
552 }
553}
554
Bram Moolenaar4b779472005-10-04 09:12:31 +0000555/*
556 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000557 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000558 * This may be repeated when the preview window is used:
559 * "repeat" == 0: open preview window normally
560 * "repeat" == 1: open preview window but don't set the size
561 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000562 * Returns TRUE when the window was resized and the location of the popup menu
563 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000564 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000565 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100566pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000567{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000568 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000569 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000570
Bram Moolenaar4b779472005-10-04 09:12:31 +0000571 pum_selected = n;
572
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000573 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000574 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000575 if (pum_first > pum_selected - 4)
576 {
577 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000578 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000579 if (pum_first > pum_selected - 2)
580 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000581 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000582 if (pum_first < 0)
583 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000584 else if (pum_first > pum_selected)
585 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000586 }
587 else
588 pum_first = pum_selected;
589 }
590 else if (pum_first < pum_selected - pum_height + 5)
591 {
592 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000593 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000594 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000595 {
596 pum_first += pum_height - 2;
597 if (pum_first < pum_selected - pum_height + 1)
598 pum_first = pum_selected - pum_height + 1;
599 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000600 else
601 pum_first = pum_selected - pum_height + 1;
602 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000603
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000604 /* Give a few lines of context when possible. */
605 if (context > 3)
606 context = 3;
607 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000608 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000609 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000610 {
611 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000612 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000613 if (pum_first < 0)
614 pum_first = 0;
615 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000616 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000617 {
618 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000619 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000620 }
621 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000622
Bram Moolenaar4033c552017-09-16 20:54:51 +0200623#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000624 /*
625 * Show extra info in the preview window if there is something and
626 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000627 * Skip this when tried twice already.
628 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000629 * NOTE: Be very careful not to sync undo!
630 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000631 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000632 && Rows > 10
633 && repeat <= 1
634 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000635 {
636 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200637 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000638 int res = OK;
639
Bram Moolenaaree236d02010-10-27 17:11:15 +0200640 /* Open a preview window. 3 lines by default. Prefer
641 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000642 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200643 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
644 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200645 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100646 /* Prevent undo sync here, if an autocommand syncs undo weird
647 * things can happen to the undo tree. */
648 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000649 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100650 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200651 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000652 g_do_tagpreview = 0;
653
654 if (curwin->w_p_pvw)
655 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200656 if (!resized
657 && curbuf->b_nwindows == 1
658 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000659 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
660 && curbuf->b_p_bh[0] == 'w')
661 {
662 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100663 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000664 ml_delete((linenr_T)1, FALSE);
665 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000666 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000667 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000668 /* Don't want to sync undo in the current buffer. */
669 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000670 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000671 --no_u_sync;
672 if (res == OK)
673 {
674 /* Edit a new, empty buffer. Set options for a "wipeout"
675 * buffer. */
676 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
677 set_option_value((char_u *)"bt", 0L,
678 (char_u *)"nofile", OPT_LOCAL);
679 set_option_value((char_u *)"bh", 0L,
680 (char_u *)"wipe", OPT_LOCAL);
681 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000682 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000683 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000684 }
685 if (res == OK)
686 {
687 char_u *p, *e;
688 linenr_T lnum = 0;
689
690 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
691 {
692 e = vim_strchr(p, '\n');
693 if (e == NULL)
694 {
695 ml_append(lnum++, p, 0, FALSE);
696 break;
697 }
698 else
699 {
700 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000701 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000702 *e = '\n';
703 p = e + 1;
704 }
705 }
706
707 /* Increase the height of the preview window to show the
708 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000709 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000710 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000711 if (lnum > p_pvh)
712 lnum = p_pvh;
713 if (curwin->w_height < lnum)
714 {
715 win_setheight((int)lnum);
716 resized = TRUE;
717 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000718 }
719
720 curbuf->b_changed = 0;
721 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100722 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000723 curwin->w_cursor.col = 0;
724
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200725 if ((curwin != curwin_save && win_valid(curwin_save))
726 || (curtab != curtab_save
727 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000728 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200729 if (curtab != curtab_save && valid_tabpage(curtab_save))
730 goto_tabpage_tp(curtab_save, FALSE, FALSE);
731
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200732 /* When the first completion is done and the preview
733 * window is not resized, skip the preview window's
734 * status line redrawing. */
735 if (ins_compl_active() && !resized)
736 curwin->w_redr_status = FALSE;
737
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000738 /* Return cursor to where we were */
739 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000740 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000741
742 /* When the preview window was resized we need to
743 * update the view on the buffer. Only go back to
744 * the window when needed, otherwise it will always be
745 * redraw. */
746 if (resized)
747 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100748 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000749 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100750 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000751 update_topline();
752 }
753
754 /* Update the screen before drawing the popup menu.
755 * Enable updating the status lines. */
756 pum_do_redraw = TRUE;
757 update_screen(0);
758 pum_do_redraw = FALSE;
759
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000760 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100761 {
762 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000763 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100764 --no_u_sync;
765 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000766
767 /* May need to update the screen again when there are
768 * autocommands involved. */
769 pum_do_redraw = TRUE;
770 update_screen(0);
771 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000772 }
773 }
774 }
775 }
776#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000777 }
778
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000779 if (!resized)
780 pum_redraw();
781
782 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000783}
784
785/*
786 * Undisplay the popup menu (later).
787 */
788 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100789pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000790{
791 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000792 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000793 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000794 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000795}
796
797/*
798 * Clear the popup menu. Currently only resets the offset to the first
799 * displayed item.
800 */
801 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100802pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000803{
804 pum_first = 0;
805}
806
807/*
808 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000809 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000810 */
811 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100812pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000813{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000814 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000815}
816
Bram Moolenaare3226be2005-12-18 22:10:00 +0000817/*
818 * Return the height of the popup menu, the number of entries visible.
819 * Only valid when pum_visible() returns TRUE!
820 */
821 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100822pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000823{
824 return pum_height;
825}
826
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100827# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100828static pumitem_T *balloon_array = NULL;
829static int balloon_arraysize;
830static int balloon_mouse_row = 0;
831static int balloon_mouse_col = 0;
832
Bram Moolenaar246fe032017-11-19 19:56:27 +0100833#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100834#define BALLOON_MIN_HEIGHT 10
835
Bram Moolenaar246fe032017-11-19 19:56:27 +0100836typedef struct {
837 char_u *start;
838 int bytelen;
839 int cells;
840 int indent;
841} balpart_T;
842
843/*
844 * Split a string into parts to display in the balloon.
845 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
846 * strings and make a struct look good.
847 * Resulting array is stored in "array" and returns the size of the array.
848 */
849 int
850split_message(char_u *mesg, pumitem_T **array)
851{
852 garray_T ga;
853 char_u *p;
854 balpart_T *item;
855 int quoted = FALSE;
856 int height;
857 int line;
858 int item_idx;
859 int indent = 0;
860 int max_cells = 0;
861 int max_height = Rows / 2 - 2;
862 int long_item_count = 0;
863 int split_long_items = FALSE;
864
865 ga_init2(&ga, sizeof(balpart_T), 20);
866 p = mesg;
867
868 while (*p != NUL)
869 {
870 if (ga_grow(&ga, 1) == FAIL)
871 goto failed;
872 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
873 item->start = p;
874 item->indent = indent;
875 item->cells = indent * 2;
876 ++ga.ga_len;
877 while (*p != NUL)
878 {
879 if (*p == '"')
880 quoted = !quoted;
881 else if (*p == '\\' && p[1] != NUL)
882 ++p;
883 else if (!quoted)
884 {
885 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
886 {
887 /* Looks like a good point to break. */
888 if (*p == '{')
889 ++indent;
890 else if (*p == '}' && indent > 0)
891 --indent;
892 ++item->cells;
893 p = skipwhite(p + 1);
894 break;
895 }
896 }
897 item->cells += ptr2cells(p);
898 p += MB_PTR2LEN(p);
899 }
900 item->bytelen = p - item->start;
901 if (item->cells > max_cells)
902 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100903 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100904 }
905
906 height = 2 + ga.ga_len;
907
908 /* If there are long items and the height is below the limit: split lines */
909 if (long_item_count > 0 && height + long_item_count <= max_height)
910 {
911 split_long_items = TRUE;
912 height += long_item_count;
913 }
914
915 /* Limit to half the window height, it has to fit above or below the mouse
916 * position. */
917 if (height > max_height)
918 height = max_height;
919 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
920 if (*array == NULL)
921 goto failed;
922
923 /* Add an empty line above and below, looks better. */
924 (*array)->pum_text = vim_strsave((char_u *)"");
925 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
926
927 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
928 {
929 int skip;
930 int thislen;
931 int copylen;
932 int ind;
933 int cells;
934
935 item = ((balpart_T *)ga.ga_data) + item_idx;
936 for (skip = 0; skip < item->bytelen; skip += thislen)
937 {
938 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
939 {
940 cells = item->indent * 2;
941 for (p = item->start + skip; p < item->start + item->bytelen;
942 p += MB_PTR2LEN(p))
943 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
944 break;
945 thislen = p - (item->start + skip);
946 }
947 else
948 thislen = item->bytelen;
949
950 /* put indent at the start */
951 p = alloc(thislen + item->indent * 2 + 1);
952 for (ind = 0; ind < item->indent * 2; ++ind)
953 p[ind] = ' ';
954
955 /* exclude spaces at the end of the string */
956 for (copylen = thislen; copylen > 0; --copylen)
957 if (item->start[skip + copylen - 1] != ' ')
958 break;
959
960 vim_strncpy(p + ind, item->start + skip, copylen);
961 (*array)[line].pum_text = p;
962 item->indent = 0; /* wrapped line has no indent */
963 ++line;
964 }
965 }
966 ga_clear(&ga);
967 return height;
968
969failed:
970 ga_clear(&ga);
971 return 0;
972}
973
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100974 void
975ui_remove_balloon(void)
976{
977 if (balloon_array != NULL)
978 {
979 pum_undisplay();
980 while (balloon_arraysize > 0)
981 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +0100982 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100983 }
984}
985
986/*
987 * Terminal version of a balloon, uses the popup menu code.
988 */
989 void
Bram Moolenaar246fe032017-11-19 19:56:27 +0100990ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100991{
992 ui_remove_balloon();
993
Bram Moolenaar246fe032017-11-19 19:56:27 +0100994 if (mesg == NULL && list == NULL)
995 return;
996 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100997 {
Bram Moolenaar246fe032017-11-19 19:56:27 +0100998 listitem_T *li;
999 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001000
Bram Moolenaar246fe032017-11-19 19:56:27 +01001001 balloon_arraysize = list->lv_len;
1002 balloon_array = (pumitem_T *)alloc_clear(
1003 (unsigned)sizeof(pumitem_T) * list->lv_len);
1004 if (balloon_array == NULL)
1005 return;
1006 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1007 {
1008 char_u *text = get_tv_string_chk(&li->li_tv);
1009
1010 balloon_array[idx].pum_text = vim_strsave(
1011 text == NULL ? (char_u *)"" : text);
1012 }
1013 }
1014 else
1015 balloon_arraysize = split_message(mesg, &balloon_array);
1016
1017 if (balloon_arraysize > 0)
1018 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001019 pum_array = balloon_array;
1020 pum_size = balloon_arraysize;
1021 pum_compute_size();
1022 pum_scrollbar = 0;
1023 pum_height = balloon_arraysize;
1024
Bram Moolenaar246fe032017-11-19 19:56:27 +01001025 if (Rows - mouse_row > pum_size)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001026 {
1027 /* Enough space below the mouse row. */
1028 pum_row = mouse_row + 1;
1029 if (pum_height > Rows - pum_row)
1030 pum_height = Rows - pum_row;
1031 }
1032 else
1033 {
1034 /* Show above the mouse row, reduce height if it does not fit. */
Bram Moolenaar246fe032017-11-19 19:56:27 +01001035 pum_row = mouse_row - pum_size;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001036 if (pum_row < 0)
1037 {
1038 pum_height += pum_row;
1039 pum_row = 0;
1040 }
1041 }
1042 if (Columns - mouse_col >= pum_base_width
1043 || Columns - mouse_col > BALLOON_MIN_WIDTH)
1044 /* Enough space to show at mouse column. */
1045 pum_col = mouse_col;
1046 else
1047 /* Not enough space, right align with window. */
1048 pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH
1049 ? BALLOON_MIN_WIDTH : pum_base_width);
1050
1051 pum_width = Columns - pum_col;
1052 if (pum_width > pum_base_width + 1)
1053 pum_width = pum_base_width + 1;
1054
1055 pum_selected = -1;
1056 pum_first = 0;
1057 pum_redraw();
1058 }
1059}
1060
1061/*
1062 * Called when the mouse moved, may remove any displayed balloon.
1063 */
1064 void
1065ui_may_remove_balloon(void)
1066{
1067 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
1068 ui_remove_balloon();
1069}
1070# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001071
Bram Moolenaar4b779472005-10-04 09:12:31 +00001072#endif