blob: d9def36647d44d50c28183298cce7b57350d6928 [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!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010072 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000073 * 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 Moolenaar2b10bcb2018-02-24 21:25:44 +010086 int cursor_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 Moolenaar2b10bcb2018-02-24 21:25:44 +0100202 cursor_col = curwin->w_wincol + curwin->w_width
203 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000204 else
205#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100206 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000207
Bram Moolenaara5e66212017-09-29 22:42:33 +0200208 /* if there are more items than room we need a scrollbar */
209 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000210 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200211 pum_scrollbar = 1;
212 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000213 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000214 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200215 pum_scrollbar = 0;
216
217 if (def_width < max_width)
218 def_width = max_width;
219
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100220 if (((cursor_col < Columns - p_pw
221 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000222#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200223 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100224 || (curwin->w_p_rl
225 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000226#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200227 ))
228 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100229 /* align pum with "cursor_col" */
230 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000231
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100232 /* start with the maximum space available */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200233#ifdef FEAT_RIGHTLEFT
234 if (curwin->w_p_rl)
235 pum_width = pum_col - pum_scrollbar + 1;
236 else
237#endif
238 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000239
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100240 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100241 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200242 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100243 /* the width is more than needed for the items, make it
244 * narrower */
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100245 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100246 if (pum_width < p_pw)
247 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200248 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100249 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100250#ifdef FEAT_RIGHTLEFT
251 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100252 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
253 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100254#endif
255 ))
256 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100257 /* align pum edge with "cursor_col" */
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100258#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100259 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100260 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100261 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100262 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100263 if (pum_col >= Columns)
264 pum_col = Columns - 1;
265 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100266 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100267#endif
268 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100269 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
270 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100271 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100272 /* use full width to end of the screen */
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100273 pum_col = Columns - max_width - pum_scrollbar;
274 if (pum_col < 0)
275 pum_col = 0;
276 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100277 }
278
279#ifdef FEAT_RIGHTLEFT
280 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100281 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100282 else
283#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100284 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100285
Bram Moolenaar42443c72018-02-10 18:28:52 +0100286 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100287 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100288 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100289#ifdef FEAT_RIGHTLEFT
290 if (curwin->w_p_rl)
291 {
292 if (pum_width > pum_col)
293 pum_width = pum_col;
294 }
295 else
296#endif
297 {
298 if (pum_width >= Columns - pum_col)
299 pum_width = Columns - pum_col - 1;
300 }
301 }
302 else if (pum_width > max_width + pum_kind_width
303 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100304 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100305 {
306 pum_width = max_width + pum_kind_width
307 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100308 if (pum_width < p_pw)
309 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100310 }
311 }
312
Bram Moolenaara5e66212017-09-29 22:42:33 +0200313 }
314 else if (Columns < def_width)
315 {
316 /* not enough room, will use what we have */
317#ifdef FEAT_RIGHTLEFT
318 if (curwin->w_p_rl)
319 pum_col = Columns - 1;
320 else
321#endif
322 pum_col = 0;
323 pum_width = Columns - 1;
324 }
325 else
326 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100327 if (max_width > p_pw)
328 max_width = p_pw; /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200329#ifdef FEAT_RIGHTLEFT
330 if (curwin->w_p_rl)
331 pum_col = max_width - 1;
332 else
333#endif
334 pum_col = Columns - max_width;
335 pum_width = max_width - pum_scrollbar;
336 }
337
Bram Moolenaara5e66212017-09-29 22:42:33 +0200338 /* Set selected item and redraw. If the window size changed need to
339 * redo the positioning. Limit this to two times, when there is not
340 * much room the window size will keep changing. */
341 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000342}
343
344/*
345 * Redraw the popup menu, using "pum_first" and "pum_selected".
346 */
347 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100348pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000349{
350 int row = pum_row;
351 int col;
352 int attr_norm = highlight_attr[HLF_PNI];
353 int attr_select = highlight_attr[HLF_PSI];
354 int attr_scroll = highlight_attr[HLF_PSB];
355 int attr_thumb = highlight_attr[HLF_PST];
356 int attr;
357 int i;
358 int idx;
359 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000360 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000361 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000362 int thumb_pos = 0;
363 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000364 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000365 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000366
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100367 /* Never display more than we have */
368 if (pum_first > pum_size - pum_height)
369 pum_first = pum_size - pum_height;
370
Bram Moolenaar4b779472005-10-04 09:12:31 +0000371 if (pum_scrollbar)
372 {
373 thumb_heigth = pum_height * pum_height / pum_size;
374 if (thumb_heigth == 0)
375 thumb_heigth = 1;
376 thumb_pos = (pum_first * (pum_height - thumb_heigth)
377 + (pum_size - pum_height) / 2)
378 / (pum_size - pum_height);
379 }
380
381 for (i = 0; i < pum_height; ++i)
382 {
383 idx = i + pum_first;
384 attr = (idx == pum_selected) ? attr_select : attr_norm;
385
386 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000387#ifdef FEAT_RIGHTLEFT
388 if (curwin->w_p_rl)
389 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200390 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000391 screen_putchar(' ', row, pum_col + 1, attr);
392 }
393 else
394#endif
395 if (pum_col > 0)
396 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000397
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000398 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000399 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000400 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000401 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000402 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000403 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000404 width = 0;
405 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000406 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000407 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000408 case 1: p = pum_array[idx].pum_text; break;
409 case 2: p = pum_array[idx].pum_kind; break;
410 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000411 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000412 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100413 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000414 {
415 if (s == NULL)
416 s = p;
417 w = ptr2cells(p);
418 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
419 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000420 /* Display the text that fits or comes before a Tab.
421 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000422 char_u *st;
423 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000424
425 *p = NUL;
426 st = transstr(s);
427 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000428#ifdef FEAT_RIGHTLEFT
429 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000430 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000431 if (st != NULL)
432 {
433 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000434
435 if (rt != NULL)
436 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100437 char_u *rt_start = rt;
438 int size;
439
440 size = vim_strsize(rt);
441 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000442 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100443 do
444 {
445 size -= has_mbyte
446 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100447 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100448 } while (size > pum_width);
449
450 if (size < pum_width)
451 {
452 /* Most left character requires
453 * 2-cells but only 1 cell is
454 * available on screen. Put a
455 * '<' on the left of the pum
456 * item */
457 *(--rt) = '<';
458 size++;
459 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000460 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100461 screen_puts_len(rt, (int)STRLEN(rt),
462 row, col - size + 1, attr);
463 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000464 }
465 vim_free(st);
466 }
467 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000468 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000469 else
470#endif
471 {
472 if (st != NULL)
473 {
474 screen_puts_len(st, (int)STRLEN(st), row, col,
475 attr);
476 vim_free(st);
477 }
478 col += width;
479 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000480
481 if (*p != TAB)
482 break;
483
484 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000485#ifdef FEAT_RIGHTLEFT
486 if (curwin->w_p_rl)
487 {
488 screen_puts_len((char_u *)" ", 2, row, col - 1,
489 attr);
490 col -= 2;
491 }
492 else
493#endif
494 {
495 screen_puts_len((char_u *)" ", 2, row, col, attr);
496 col += 2;
497 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000498 totwidth += 2;
499 s = NULL; /* start text at next char */
500 width = 0;
501 }
502 else
503 width += w;
504 }
505
506 if (round > 1)
507 n = pum_kind_width + 1;
508 else
509 n = 1;
510
511 /* Stop when there is nothing more to display. */
512 if (round == 3
513 || (round == 2 && pum_array[idx].pum_extra == NULL)
514 || (round == 1 && pum_array[idx].pum_kind == NULL
515 && pum_array[idx].pum_extra == NULL)
516 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000517 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000518#ifdef FEAT_RIGHTLEFT
519 if (curwin->w_p_rl)
520 {
521 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
522 col + 1, ' ', ' ', attr);
523 col = pum_col - pum_base_width - n + 1;
524 }
525 else
526#endif
527 {
528 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000529 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000530 col = pum_col + pum_base_width + n;
531 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000532 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000533 }
534
Bram Moolenaarabc97732007-08-08 20:49:37 +0000535#ifdef FEAT_RIGHTLEFT
536 if (curwin->w_p_rl)
537 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
538 ' ', attr);
539 else
540#endif
541 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
542 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000543 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000544 {
545#ifdef FEAT_RIGHTLEFT
546 if (curwin->w_p_rl)
547 screen_putchar(' ', row, pum_col - pum_width,
548 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000549 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000550 else
551#endif
552 screen_putchar(' ', row, pum_col + pum_width,
553 i >= thumb_pos && i < thumb_pos + thumb_heigth
554 ? attr_thumb : attr_scroll);
555 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000556
557 ++row;
558 }
559}
560
Bram Moolenaar4b779472005-10-04 09:12:31 +0000561/*
562 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000563 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000564 * This may be repeated when the preview window is used:
565 * "repeat" == 0: open preview window normally
566 * "repeat" == 1: open preview window but don't set the size
567 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000568 * Returns TRUE when the window was resized and the location of the popup menu
569 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000570 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000571 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100572pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000573{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000574 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000575 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000576
Bram Moolenaar4b779472005-10-04 09:12:31 +0000577 pum_selected = n;
578
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000579 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000580 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000581 if (pum_first > pum_selected - 4)
582 {
583 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000584 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000585 if (pum_first > pum_selected - 2)
586 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000587 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000588 if (pum_first < 0)
589 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000590 else if (pum_first > pum_selected)
591 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000592 }
593 else
594 pum_first = pum_selected;
595 }
596 else if (pum_first < pum_selected - pum_height + 5)
597 {
598 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000599 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000600 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000601 {
602 pum_first += pum_height - 2;
603 if (pum_first < pum_selected - pum_height + 1)
604 pum_first = pum_selected - pum_height + 1;
605 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000606 else
607 pum_first = pum_selected - pum_height + 1;
608 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000609
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000610 /* Give a few lines of context when possible. */
611 if (context > 3)
612 context = 3;
613 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000614 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000615 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616 {
617 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000618 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000619 if (pum_first < 0)
620 pum_first = 0;
621 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000622 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000623 {
624 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000625 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000626 }
627 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000628
Bram Moolenaar4033c552017-09-16 20:54:51 +0200629#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000630 /*
631 * Show extra info in the preview window if there is something and
632 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000633 * Skip this when tried twice already.
634 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000635 * NOTE: Be very careful not to sync undo!
636 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000637 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000638 && Rows > 10
639 && repeat <= 1
640 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000641 {
642 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200643 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000644 int res = OK;
645
Bram Moolenaaree236d02010-10-27 17:11:15 +0200646 /* Open a preview window. 3 lines by default. Prefer
647 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000648 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200649 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
650 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200651 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100652 /* Prevent undo sync here, if an autocommand syncs undo weird
653 * things can happen to the undo tree. */
654 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000655 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100656 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200657 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000658 g_do_tagpreview = 0;
659
660 if (curwin->w_p_pvw)
661 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200662 if (!resized
663 && curbuf->b_nwindows == 1
664 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000665 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
666 && curbuf->b_p_bh[0] == 'w')
667 {
668 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100669 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000670 ml_delete((linenr_T)1, FALSE);
671 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000672 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000673 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000674 /* Don't want to sync undo in the current buffer. */
675 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000676 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000677 --no_u_sync;
678 if (res == OK)
679 {
680 /* Edit a new, empty buffer. Set options for a "wipeout"
681 * buffer. */
682 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
683 set_option_value((char_u *)"bt", 0L,
684 (char_u *)"nofile", OPT_LOCAL);
685 set_option_value((char_u *)"bh", 0L,
686 (char_u *)"wipe", OPT_LOCAL);
687 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000688 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000689 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000690 }
691 if (res == OK)
692 {
693 char_u *p, *e;
694 linenr_T lnum = 0;
695
696 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
697 {
698 e = vim_strchr(p, '\n');
699 if (e == NULL)
700 {
701 ml_append(lnum++, p, 0, FALSE);
702 break;
703 }
704 else
705 {
706 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000707 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000708 *e = '\n';
709 p = e + 1;
710 }
711 }
712
713 /* Increase the height of the preview window to show the
714 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000715 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000716 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000717 if (lnum > p_pvh)
718 lnum = p_pvh;
719 if (curwin->w_height < lnum)
720 {
721 win_setheight((int)lnum);
722 resized = TRUE;
723 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000724 }
725
726 curbuf->b_changed = 0;
727 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100728 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000729 curwin->w_cursor.col = 0;
730
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200731 if ((curwin != curwin_save && win_valid(curwin_save))
732 || (curtab != curtab_save
733 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000734 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200735 if (curtab != curtab_save && valid_tabpage(curtab_save))
736 goto_tabpage_tp(curtab_save, FALSE, FALSE);
737
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200738 /* When the first completion is done and the preview
739 * window is not resized, skip the preview window's
740 * status line redrawing. */
741 if (ins_compl_active() && !resized)
742 curwin->w_redr_status = FALSE;
743
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000744 /* Return cursor to where we were */
745 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000746 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000747
748 /* When the preview window was resized we need to
749 * update the view on the buffer. Only go back to
750 * the window when needed, otherwise it will always be
751 * redraw. */
752 if (resized)
753 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100754 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000755 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100756 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000757 update_topline();
758 }
759
760 /* Update the screen before drawing the popup menu.
761 * Enable updating the status lines. */
762 pum_do_redraw = TRUE;
763 update_screen(0);
764 pum_do_redraw = FALSE;
765
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000766 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100767 {
768 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000769 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100770 --no_u_sync;
771 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000772
773 /* May need to update the screen again when there are
774 * autocommands involved. */
775 pum_do_redraw = TRUE;
776 update_screen(0);
777 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000778 }
779 }
780 }
781 }
782#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000783 }
784
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000785 if (!resized)
786 pum_redraw();
787
788 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000789}
790
791/*
792 * Undisplay the popup menu (later).
793 */
794 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100795pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000796{
797 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000798 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000799 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000800 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000801}
802
803/*
804 * Clear the popup menu. Currently only resets the offset to the first
805 * displayed item.
806 */
807 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100808pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000809{
810 pum_first = 0;
811}
812
813/*
814 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000815 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000816 */
817 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100818pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000819{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000820 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000821}
822
Bram Moolenaare3226be2005-12-18 22:10:00 +0000823/*
824 * Return the height of the popup menu, the number of entries visible.
825 * Only valid when pum_visible() returns TRUE!
826 */
827 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100828pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000829{
830 return pum_height;
831}
832
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100833# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100834static pumitem_T *balloon_array = NULL;
835static int balloon_arraysize;
836static int balloon_mouse_row = 0;
837static int balloon_mouse_col = 0;
838
Bram Moolenaar246fe032017-11-19 19:56:27 +0100839#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100840#define BALLOON_MIN_HEIGHT 10
841
Bram Moolenaar246fe032017-11-19 19:56:27 +0100842typedef struct {
843 char_u *start;
844 int bytelen;
845 int cells;
846 int indent;
847} balpart_T;
848
849/*
850 * Split a string into parts to display in the balloon.
851 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
852 * strings and make a struct look good.
853 * Resulting array is stored in "array" and returns the size of the array.
854 */
855 int
856split_message(char_u *mesg, pumitem_T **array)
857{
858 garray_T ga;
859 char_u *p;
860 balpart_T *item;
861 int quoted = FALSE;
862 int height;
863 int line;
864 int item_idx;
865 int indent = 0;
866 int max_cells = 0;
867 int max_height = Rows / 2 - 2;
868 int long_item_count = 0;
869 int split_long_items = FALSE;
870
871 ga_init2(&ga, sizeof(balpart_T), 20);
872 p = mesg;
873
874 while (*p != NUL)
875 {
876 if (ga_grow(&ga, 1) == FAIL)
877 goto failed;
878 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
879 item->start = p;
880 item->indent = indent;
881 item->cells = indent * 2;
882 ++ga.ga_len;
883 while (*p != NUL)
884 {
885 if (*p == '"')
886 quoted = !quoted;
887 else if (*p == '\\' && p[1] != NUL)
888 ++p;
889 else if (!quoted)
890 {
891 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
892 {
893 /* Looks like a good point to break. */
894 if (*p == '{')
895 ++indent;
896 else if (*p == '}' && indent > 0)
897 --indent;
898 ++item->cells;
899 p = skipwhite(p + 1);
900 break;
901 }
902 }
903 item->cells += ptr2cells(p);
904 p += MB_PTR2LEN(p);
905 }
906 item->bytelen = p - item->start;
907 if (item->cells > max_cells)
908 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100909 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100910 }
911
912 height = 2 + ga.ga_len;
913
914 /* If there are long items and the height is below the limit: split lines */
915 if (long_item_count > 0 && height + long_item_count <= max_height)
916 {
917 split_long_items = TRUE;
918 height += long_item_count;
919 }
920
921 /* Limit to half the window height, it has to fit above or below the mouse
922 * position. */
923 if (height > max_height)
924 height = max_height;
925 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
926 if (*array == NULL)
927 goto failed;
928
929 /* Add an empty line above and below, looks better. */
930 (*array)->pum_text = vim_strsave((char_u *)"");
931 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
932
933 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
934 {
935 int skip;
936 int thislen;
937 int copylen;
938 int ind;
939 int cells;
940
941 item = ((balpart_T *)ga.ga_data) + item_idx;
942 for (skip = 0; skip < item->bytelen; skip += thislen)
943 {
944 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
945 {
946 cells = item->indent * 2;
947 for (p = item->start + skip; p < item->start + item->bytelen;
948 p += MB_PTR2LEN(p))
949 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
950 break;
951 thislen = p - (item->start + skip);
952 }
953 else
954 thislen = item->bytelen;
955
956 /* put indent at the start */
957 p = alloc(thislen + item->indent * 2 + 1);
958 for (ind = 0; ind < item->indent * 2; ++ind)
959 p[ind] = ' ';
960
961 /* exclude spaces at the end of the string */
962 for (copylen = thislen; copylen > 0; --copylen)
963 if (item->start[skip + copylen - 1] != ' ')
964 break;
965
966 vim_strncpy(p + ind, item->start + skip, copylen);
967 (*array)[line].pum_text = p;
968 item->indent = 0; /* wrapped line has no indent */
969 ++line;
970 }
971 }
972 ga_clear(&ga);
973 return height;
974
975failed:
976 ga_clear(&ga);
977 return 0;
978}
979
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100980 void
981ui_remove_balloon(void)
982{
983 if (balloon_array != NULL)
984 {
985 pum_undisplay();
986 while (balloon_arraysize > 0)
987 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +0100988 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100989 }
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