blob: 6cfd95a4e97a8e939312782bfe10e8a3c5dbe29d [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
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100425 if (saved != NUL)
426 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000427 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100428 if (saved != NUL)
429 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000430#ifdef FEAT_RIGHTLEFT
431 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000432 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000433 if (st != NULL)
434 {
435 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000436
437 if (rt != NULL)
438 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100439 char_u *rt_start = rt;
440 int size;
441
442 size = vim_strsize(rt);
443 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100445 do
446 {
447 size -= has_mbyte
448 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100449 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100450 } while (size > pum_width);
451
452 if (size < pum_width)
453 {
454 /* Most left character requires
455 * 2-cells but only 1 cell is
456 * available on screen. Put a
457 * '<' on the left of the pum
458 * item */
459 *(--rt) = '<';
460 size++;
461 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000462 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100463 screen_puts_len(rt, (int)STRLEN(rt),
464 row, col - size + 1, attr);
465 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000466 }
467 vim_free(st);
468 }
469 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000470 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000471 else
472#endif
473 {
474 if (st != NULL)
475 {
476 screen_puts_len(st, (int)STRLEN(st), row, col,
477 attr);
478 vim_free(st);
479 }
480 col += width;
481 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000482
483 if (*p != TAB)
484 break;
485
486 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000487#ifdef FEAT_RIGHTLEFT
488 if (curwin->w_p_rl)
489 {
490 screen_puts_len((char_u *)" ", 2, row, col - 1,
491 attr);
492 col -= 2;
493 }
494 else
495#endif
496 {
497 screen_puts_len((char_u *)" ", 2, row, col, attr);
498 col += 2;
499 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000500 totwidth += 2;
501 s = NULL; /* start text at next char */
502 width = 0;
503 }
504 else
505 width += w;
506 }
507
508 if (round > 1)
509 n = pum_kind_width + 1;
510 else
511 n = 1;
512
513 /* Stop when there is nothing more to display. */
514 if (round == 3
515 || (round == 2 && pum_array[idx].pum_extra == NULL)
516 || (round == 1 && pum_array[idx].pum_kind == NULL
517 && pum_array[idx].pum_extra == NULL)
518 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000519 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000520#ifdef FEAT_RIGHTLEFT
521 if (curwin->w_p_rl)
522 {
523 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
524 col + 1, ' ', ' ', attr);
525 col = pum_col - pum_base_width - n + 1;
526 }
527 else
528#endif
529 {
530 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000531 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000532 col = pum_col + pum_base_width + n;
533 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000534 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000535 }
536
Bram Moolenaarabc97732007-08-08 20:49:37 +0000537#ifdef FEAT_RIGHTLEFT
538 if (curwin->w_p_rl)
539 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
540 ' ', attr);
541 else
542#endif
543 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
544 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000545 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000546 {
547#ifdef FEAT_RIGHTLEFT
548 if (curwin->w_p_rl)
549 screen_putchar(' ', row, pum_col - pum_width,
550 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000551 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000552 else
553#endif
554 screen_putchar(' ', row, pum_col + pum_width,
555 i >= thumb_pos && i < thumb_pos + thumb_heigth
556 ? attr_thumb : attr_scroll);
557 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000558
559 ++row;
560 }
561}
562
Bram Moolenaar4b779472005-10-04 09:12:31 +0000563/*
564 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000565 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000566 * This may be repeated when the preview window is used:
567 * "repeat" == 0: open preview window normally
568 * "repeat" == 1: open preview window but don't set the size
569 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000570 * Returns TRUE when the window was resized and the location of the popup menu
571 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000572 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000573 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100574pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000575{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000576 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000577 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000578
Bram Moolenaar4b779472005-10-04 09:12:31 +0000579 pum_selected = n;
580
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000581 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000582 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000583 if (pum_first > pum_selected - 4)
584 {
585 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000586 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000587 if (pum_first > pum_selected - 2)
588 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000589 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000590 if (pum_first < 0)
591 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000592 else if (pum_first > pum_selected)
593 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000594 }
595 else
596 pum_first = pum_selected;
597 }
598 else if (pum_first < pum_selected - pum_height + 5)
599 {
600 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000601 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000602 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000603 {
604 pum_first += pum_height - 2;
605 if (pum_first < pum_selected - pum_height + 1)
606 pum_first = pum_selected - pum_height + 1;
607 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000608 else
609 pum_first = pum_selected - pum_height + 1;
610 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000611
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000612 /* Give a few lines of context when possible. */
613 if (context > 3)
614 context = 3;
615 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000617 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000618 {
619 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000620 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000621 if (pum_first < 0)
622 pum_first = 0;
623 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000624 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000625 {
626 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000627 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000628 }
629 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000630
Bram Moolenaar4033c552017-09-16 20:54:51 +0200631#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000632 /*
633 * Show extra info in the preview window if there is something and
634 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000635 * Skip this when tried twice already.
636 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000637 * NOTE: Be very careful not to sync undo!
638 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000639 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000640 && Rows > 10
641 && repeat <= 1
642 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000643 {
644 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200645 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000646 int res = OK;
647
Bram Moolenaaree236d02010-10-27 17:11:15 +0200648 /* Open a preview window. 3 lines by default. Prefer
649 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000650 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200651 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
652 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200653 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100654 /* Prevent undo sync here, if an autocommand syncs undo weird
655 * things can happen to the undo tree. */
656 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000657 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100658 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200659 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000660 g_do_tagpreview = 0;
661
662 if (curwin->w_p_pvw)
663 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200664 if (!resized
665 && curbuf->b_nwindows == 1
666 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000667 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
668 && curbuf->b_p_bh[0] == 'w')
669 {
670 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100671 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000672 ml_delete((linenr_T)1, FALSE);
673 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000674 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000675 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000676 /* Don't want to sync undo in the current buffer. */
677 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000678 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000679 --no_u_sync;
680 if (res == OK)
681 {
682 /* Edit a new, empty buffer. Set options for a "wipeout"
683 * buffer. */
684 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
685 set_option_value((char_u *)"bt", 0L,
686 (char_u *)"nofile", OPT_LOCAL);
687 set_option_value((char_u *)"bh", 0L,
688 (char_u *)"wipe", OPT_LOCAL);
689 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000690 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000691 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000692 }
693 if (res == OK)
694 {
695 char_u *p, *e;
696 linenr_T lnum = 0;
697
698 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
699 {
700 e = vim_strchr(p, '\n');
701 if (e == NULL)
702 {
703 ml_append(lnum++, p, 0, FALSE);
704 break;
705 }
706 else
707 {
708 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000709 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000710 *e = '\n';
711 p = e + 1;
712 }
713 }
714
715 /* Increase the height of the preview window to show the
716 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000717 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000718 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000719 if (lnum > p_pvh)
720 lnum = p_pvh;
721 if (curwin->w_height < lnum)
722 {
723 win_setheight((int)lnum);
724 resized = TRUE;
725 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000726 }
727
728 curbuf->b_changed = 0;
729 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100730 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000731 curwin->w_cursor.col = 0;
732
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200733 if ((curwin != curwin_save && win_valid(curwin_save))
734 || (curtab != curtab_save
735 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000736 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200737 if (curtab != curtab_save && valid_tabpage(curtab_save))
738 goto_tabpage_tp(curtab_save, FALSE, FALSE);
739
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200740 /* When the first completion is done and the preview
741 * window is not resized, skip the preview window's
742 * status line redrawing. */
743 if (ins_compl_active() && !resized)
744 curwin->w_redr_status = FALSE;
745
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000746 /* Return cursor to where we were */
747 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000748 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000749
750 /* When the preview window was resized we need to
751 * update the view on the buffer. Only go back to
752 * the window when needed, otherwise it will always be
753 * redraw. */
754 if (resized)
755 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100756 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000757 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100758 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000759 update_topline();
760 }
761
762 /* Update the screen before drawing the popup menu.
763 * Enable updating the status lines. */
764 pum_do_redraw = TRUE;
765 update_screen(0);
766 pum_do_redraw = FALSE;
767
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000768 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100769 {
770 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000771 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100772 --no_u_sync;
773 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000774
775 /* May need to update the screen again when there are
776 * autocommands involved. */
777 pum_do_redraw = TRUE;
778 update_screen(0);
779 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000780 }
781 }
782 }
783 }
784#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000785 }
786
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000787 if (!resized)
788 pum_redraw();
789
790 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000791}
792
793/*
794 * Undisplay the popup menu (later).
795 */
796 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100797pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000798{
799 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000800 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000801 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000802 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000803}
804
805/*
806 * Clear the popup menu. Currently only resets the offset to the first
807 * displayed item.
808 */
809 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100810pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000811{
812 pum_first = 0;
813}
814
815/*
816 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000817 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000818 */
819 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100820pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000821{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000822 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000823}
824
Bram Moolenaare3226be2005-12-18 22:10:00 +0000825/*
826 * Return the height of the popup menu, the number of entries visible.
827 * Only valid when pum_visible() returns TRUE!
828 */
829 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100830pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000831{
832 return pum_height;
833}
834
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100835# if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
836 static void
837pum_position_at_mouse(int min_width)
838{
839 if (Rows - mouse_row > pum_size)
840 {
841 /* Enough space below the mouse row. */
842 pum_row = mouse_row + 1;
843 if (pum_height > Rows - pum_row)
844 pum_height = Rows - pum_row;
845 }
846 else
847 {
848 /* Show above the mouse row, reduce height if it does not fit. */
849 pum_row = mouse_row - pum_size;
850 if (pum_row < 0)
851 {
852 pum_height += pum_row;
853 pum_row = 0;
854 }
855 }
856 if (Columns - mouse_col >= pum_base_width
857 || Columns - mouse_col > min_width)
858 /* Enough space to show at mouse column. */
859 pum_col = mouse_col;
860 else
861 /* Not enough space, right align with window. */
862 pum_col = Columns - (pum_base_width > min_width
863 ? min_width : pum_base_width);
864
865 pum_width = Columns - pum_col;
866 if (pum_width > pum_base_width + 1)
867 pum_width = pum_base_width + 1;
868}
869
870# endif
871
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100872# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100873static pumitem_T *balloon_array = NULL;
874static int balloon_arraysize;
875static int balloon_mouse_row = 0;
876static int balloon_mouse_col = 0;
877
Bram Moolenaar246fe032017-11-19 19:56:27 +0100878#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100879#define BALLOON_MIN_HEIGHT 10
880
Bram Moolenaar246fe032017-11-19 19:56:27 +0100881typedef struct {
882 char_u *start;
883 int bytelen;
884 int cells;
885 int indent;
886} balpart_T;
887
888/*
889 * Split a string into parts to display in the balloon.
890 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
891 * strings and make a struct look good.
892 * Resulting array is stored in "array" and returns the size of the array.
893 */
894 int
895split_message(char_u *mesg, pumitem_T **array)
896{
897 garray_T ga;
898 char_u *p;
899 balpart_T *item;
900 int quoted = FALSE;
901 int height;
902 int line;
903 int item_idx;
904 int indent = 0;
905 int max_cells = 0;
906 int max_height = Rows / 2 - 2;
907 int long_item_count = 0;
908 int split_long_items = FALSE;
909
910 ga_init2(&ga, sizeof(balpart_T), 20);
911 p = mesg;
912
913 while (*p != NUL)
914 {
915 if (ga_grow(&ga, 1) == FAIL)
916 goto failed;
917 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
918 item->start = p;
919 item->indent = indent;
920 item->cells = indent * 2;
921 ++ga.ga_len;
922 while (*p != NUL)
923 {
924 if (*p == '"')
925 quoted = !quoted;
926 else if (*p == '\\' && p[1] != NUL)
927 ++p;
928 else if (!quoted)
929 {
930 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
931 {
932 /* Looks like a good point to break. */
933 if (*p == '{')
934 ++indent;
935 else if (*p == '}' && indent > 0)
936 --indent;
937 ++item->cells;
938 p = skipwhite(p + 1);
939 break;
940 }
941 }
942 item->cells += ptr2cells(p);
943 p += MB_PTR2LEN(p);
944 }
945 item->bytelen = p - item->start;
946 if (item->cells > max_cells)
947 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100948 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100949 }
950
951 height = 2 + ga.ga_len;
952
953 /* If there are long items and the height is below the limit: split lines */
954 if (long_item_count > 0 && height + long_item_count <= max_height)
955 {
956 split_long_items = TRUE;
957 height += long_item_count;
958 }
959
960 /* Limit to half the window height, it has to fit above or below the mouse
961 * position. */
962 if (height > max_height)
963 height = max_height;
964 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
965 if (*array == NULL)
966 goto failed;
967
968 /* Add an empty line above and below, looks better. */
969 (*array)->pum_text = vim_strsave((char_u *)"");
970 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
971
972 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
973 {
974 int skip;
975 int thislen;
976 int copylen;
977 int ind;
978 int cells;
979
980 item = ((balpart_T *)ga.ga_data) + item_idx;
981 for (skip = 0; skip < item->bytelen; skip += thislen)
982 {
983 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
984 {
985 cells = item->indent * 2;
986 for (p = item->start + skip; p < item->start + item->bytelen;
987 p += MB_PTR2LEN(p))
988 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
989 break;
990 thislen = p - (item->start + skip);
991 }
992 else
993 thislen = item->bytelen;
994
995 /* put indent at the start */
996 p = alloc(thislen + item->indent * 2 + 1);
997 for (ind = 0; ind < item->indent * 2; ++ind)
998 p[ind] = ' ';
999
1000 /* exclude spaces at the end of the string */
1001 for (copylen = thislen; copylen > 0; --copylen)
1002 if (item->start[skip + copylen - 1] != ' ')
1003 break;
1004
1005 vim_strncpy(p + ind, item->start + skip, copylen);
1006 (*array)[line].pum_text = p;
1007 item->indent = 0; /* wrapped line has no indent */
1008 ++line;
1009 }
1010 }
1011 ga_clear(&ga);
1012 return height;
1013
1014failed:
1015 ga_clear(&ga);
1016 return 0;
1017}
1018
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001019 void
1020ui_remove_balloon(void)
1021{
1022 if (balloon_array != NULL)
1023 {
1024 pum_undisplay();
1025 while (balloon_arraysize > 0)
1026 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001027 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001028 }
1029}
1030
1031/*
1032 * Terminal version of a balloon, uses the popup menu code.
1033 */
1034 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001035ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001036{
1037 ui_remove_balloon();
1038
Bram Moolenaar246fe032017-11-19 19:56:27 +01001039 if (mesg == NULL && list == NULL)
1040 return;
1041 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001042 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001043 listitem_T *li;
1044 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001045
Bram Moolenaar246fe032017-11-19 19:56:27 +01001046 balloon_arraysize = list->lv_len;
1047 balloon_array = (pumitem_T *)alloc_clear(
1048 (unsigned)sizeof(pumitem_T) * list->lv_len);
1049 if (balloon_array == NULL)
1050 return;
1051 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1052 {
1053 char_u *text = get_tv_string_chk(&li->li_tv);
1054
1055 balloon_array[idx].pum_text = vim_strsave(
1056 text == NULL ? (char_u *)"" : text);
1057 }
1058 }
1059 else
1060 balloon_arraysize = split_message(mesg, &balloon_array);
1061
1062 if (balloon_arraysize > 0)
1063 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001064 pum_array = balloon_array;
1065 pum_size = balloon_arraysize;
1066 pum_compute_size();
1067 pum_scrollbar = 0;
1068 pum_height = balloon_arraysize;
1069
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001070 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001071 pum_selected = -1;
1072 pum_first = 0;
1073 pum_redraw();
1074 }
1075}
1076
1077/*
1078 * Called when the mouse moved, may remove any displayed balloon.
1079 */
1080 void
1081ui_may_remove_balloon(void)
1082{
1083 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
1084 ui_remove_balloon();
1085}
1086# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001087
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001088# if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
1089/*
1090 * Select the pum entry at the mouse position.
1091 */
1092 static void
1093pum_select_mouse_pos(void)
1094{
1095 int idx = mouse_row - pum_row;
1096
1097 if (idx < 0 || idx >= pum_size)
1098 pum_selected = -1;
1099 else if (*pum_array[idx].pum_text != NUL)
1100 pum_selected = idx;
1101}
1102
1103/*
1104 * Execute the currently selected popup menu item.
1105 */
1106 static void
1107pum_execute_menu(vimmenu_T *menu)
1108{
1109 vimmenu_T *mp;
1110 int idx = 0;
1111 exarg_T ea;
1112
1113 for (mp = menu->children; mp != NULL; mp = mp->next)
1114 if (idx++ == pum_selected)
1115 {
1116 vim_memset(&ea, 0, sizeof(ea));
1117 execute_menu(&ea, mp);
1118 break;
1119 }
1120}
1121
1122/*
1123 * Open the terminal version of the popup menu and don't return until it is
1124 * closed.
1125 */
1126 void
1127pum_show_popupmenu(vimmenu_T *menu)
1128{
1129 vimmenu_T *mp;
1130 int idx = 0;
1131 pumitem_T *array;
1132#ifdef FEAT_BEVAL_TERM
1133 int save_bevalterm = p_bevalterm;
1134#endif
1135
1136 pum_undisplay();
1137 pum_size = 0;
1138
1139 for (mp = menu->children; mp != NULL; mp = mp->next)
1140 ++pum_size;
1141
1142 array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * pum_size);
1143 if (array == NULL)
1144 return;
1145
1146 for (mp = menu->children; mp != NULL; mp = mp->next)
1147 if (menu_is_separator(mp->dname))
1148 array[idx++].pum_text = (char_u *)"";
1149 else
1150 array[idx++].pum_text = mp->dname;
1151
1152 pum_array = array;
1153 pum_compute_size();
1154 pum_scrollbar = 0;
1155 pum_height = pum_size;
1156 pum_position_at_mouse(20);
1157
1158 pum_selected = -1;
1159 pum_first = 0;
1160# ifdef FEAT_BEVAL_TERM
1161 p_bevalterm = TRUE; /* track mouse movement */
1162 mch_setmouse(TRUE);
1163# endif
1164
1165 for (;;)
1166 {
1167 int c;
1168
1169 pum_redraw();
1170 setcursor();
1171 out_flush();
1172
1173 c = vgetc();
1174 if (c == ESC)
1175 break;
1176 else if (c == CAR || c == NL)
1177 {
1178 /* enter: select current item, if any, and close */
1179 pum_execute_menu(menu);
1180 break;
1181 }
1182 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1183 {
1184 /* cursor up: select previous item */
1185 while (pum_selected > 0)
1186 {
1187 --pum_selected;
1188 if (*array[pum_selected].pum_text != NUL)
1189 break;
1190 }
1191 }
1192 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1193 {
1194 /* cursor down: select next item */
1195 while (pum_selected < pum_size - 1)
1196 {
1197 ++pum_selected;
1198 if (*array[pum_selected].pum_text != NUL)
1199 break;
1200 }
1201 }
1202 else if (c == K_RIGHTMOUSE)
1203 {
1204 /* Right mouse down: reposition the menu. */
1205 vungetc(c);
1206 break;
1207 }
1208 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1209 {
1210 /* mouse moved: selec item in the mouse row */
1211 pum_select_mouse_pos();
1212 }
1213 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1214 {
1215 /* left mouse click: select clicked item, if any, and close;
1216 * right mouse release: select clicked item, close if any */
1217 pum_select_mouse_pos();
1218 if (pum_selected >= 0)
1219 {
1220 pum_execute_menu(menu);
1221 break;
1222 }
1223 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1224 break;
1225 }
1226 }
1227
1228 vim_free(array);
1229 pum_undisplay();
1230# ifdef FEAT_BEVAL_TERM
1231 p_bevalterm = save_bevalterm;
1232 mch_setmouse(TRUE);
1233# endif
1234}
1235# endif
1236
Bram Moolenaar4b779472005-10-04 09:12:31 +00001237#endif