blob: 66fd6b9c5353de9a7c0a79fbf710d7ed718c8edc [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 Moolenaar491ac282018-06-17 14:47:55 +020032static int pum_win_row;
33static int pum_win_height;
34static int pum_win_col;
35static int pum_win_wcol;
36static int pum_win_width;
37
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000038static int pum_do_redraw = FALSE; /* do redraw anyway */
39
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000041
Bram Moolenaar4b779472005-10-04 09:12:31 +000042#define PUM_DEF_HEIGHT 10
43#define PUM_DEF_WIDTH 15
44
Bram Moolenaar51b0f372017-11-18 18:52:04 +010045 static void
46pum_compute_size(void)
47{
48 int i;
49 int w;
50
51 /* Compute the width of the widest match and the widest extra. */
52 pum_base_width = 0;
53 pum_kind_width = 0;
54 pum_extra_width = 0;
55 for (i = 0; i < pum_size; ++i)
56 {
57 w = vim_strsize(pum_array[i].pum_text);
58 if (pum_base_width < w)
59 pum_base_width = w;
60 if (pum_array[i].pum_kind != NULL)
61 {
62 w = vim_strsize(pum_array[i].pum_kind) + 1;
63 if (pum_kind_width < w)
64 pum_kind_width = w;
65 }
66 if (pum_array[i].pum_extra != NULL)
67 {
68 w = vim_strsize(pum_array[i].pum_extra) + 1;
69 if (pum_extra_width < w)
70 pum_extra_width = w;
71 }
72 }
73}
74
Bram Moolenaar4b779472005-10-04 09:12:31 +000075/*
76 * Show the popup menu with items "array[size]".
77 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010078 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000079 * The menu appears above the screen line "row" or at "row" + "height" - 1.
80 */
81 void
Bram Moolenaar05540972016-01-30 20:31:25 +010082pum_display(
83 pumitem_T *array,
84 int size,
85 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000086 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000087{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000088 int def_width;
89 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000090 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010091 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010092 int above_row;
93 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000094 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020095#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010096 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +010097#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000098
Bram Moolenaara5e66212017-09-29 22:42:33 +020099 do
100 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100101 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200102 above_row = 0;
103 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000104
Bram Moolenaara5e66212017-09-29 22:42:33 +0200105 /* Pretend the pum is already there to avoid that must_redraw is set
106 * when 'cuc' is on. */
107 pum_array = (pumitem_T *)1;
108 validate_cursor_col();
109 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000110
Bram Moolenaar491ac282018-06-17 14:47:55 +0200111 // Remember the essential parts of the window position and size, so we
112 // can decide when to reposition the popup menu.
113 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
114 pum_win_height = curwin->w_height;
115 pum_win_col = curwin->w_wincol;
116 pum_win_wcol = curwin->w_wcol;
117 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000118
Bram Moolenaar4033c552017-09-16 20:54:51 +0200119#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200120 FOR_ALL_WINDOWS(pvwin)
121 if (pvwin->w_p_pvw)
122 break;
123 if (pvwin != NULL)
124 {
125 if (W_WINROW(pvwin) < W_WINROW(curwin))
126 above_row = W_WINROW(pvwin) + pvwin->w_height;
127 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
128 below_row = W_WINROW(pvwin);
129 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100130#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000131
Bram Moolenaara5e66212017-09-29 22:42:33 +0200132 /*
133 * Figure out the size and position of the pum.
134 */
135 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000136 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000137 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200138 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000139 if (p_ph > 0 && pum_height > p_ph)
140 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000141
Bram Moolenaar491ac282018-06-17 14:47:55 +0200142 /* Put the pum below "pum_win_row" if possible. If there are few lines decide
Bram Moolenaara5e66212017-09-29 22:42:33 +0200143 * on where there is more room. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200144 if (pum_win_row + 2 >= below_row - pum_height
145 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200146 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200147 /* pum above "pum_win_row" */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200148
149 /* Leave two lines of context if possible */
150 if (curwin->w_wrow - curwin->w_cline_row >= 2)
151 context_lines = 2;
152 else
153 context_lines = curwin->w_wrow - curwin->w_cline_row;
154
Bram Moolenaar491ac282018-06-17 14:47:55 +0200155 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200156 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200157 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200158 pum_height = size;
159 }
160 else
161 {
162 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200163 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200164 }
165 if (p_ph > 0 && pum_height > p_ph)
166 {
167 pum_row += pum_height - p_ph;
168 pum_height = p_ph;
169 }
170 }
171 else
172 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200173 /* pum below "pum_win_row" */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200174
175 /* Leave two lines of context if possible */
176 if (curwin->w_cline_row
177 + curwin->w_cline_height - curwin->w_wrow >= 3)
178 context_lines = 3;
179 else
180 context_lines = curwin->w_cline_row
181 + curwin->w_cline_height - curwin->w_wrow;
182
Bram Moolenaar491ac282018-06-17 14:47:55 +0200183 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184 if (size > below_row - pum_row)
185 pum_height = below_row - pum_row;
186 else
187 pum_height = size;
188 if (p_ph > 0 && pum_height > p_ph)
189 pum_height = p_ph;
190 }
191
192 /* don't display when we only have room for one line */
193 if (pum_height < 1 || (pum_height == 1 && size > 1))
194 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000195
Bram Moolenaar4033c552017-09-16 20:54:51 +0200196#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200197 /* If there is a preview window at the above avoid drawing over it. */
198 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000199 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200200 pum_row += above_row;
201 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000202 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200203#endif
204
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100205 pum_array = array;
206 pum_size = size;
207 pum_compute_size();
208 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000209
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000211#ifdef FEAT_RIGHTLEFT
212 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100213 cursor_col = curwin->w_wincol + curwin->w_width
214 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000215 else
216#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100217 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000218
Bram Moolenaara5e66212017-09-29 22:42:33 +0200219 /* if there are more items than room we need a scrollbar */
220 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000221 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200222 pum_scrollbar = 1;
223 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000224 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000225 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200226 pum_scrollbar = 0;
227
228 if (def_width < max_width)
229 def_width = max_width;
230
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100231 if (((cursor_col < Columns - p_pw
232 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000233#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200234 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100235 || (curwin->w_p_rl
236 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000237#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200238 ))
239 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100240 /* align pum with "cursor_col" */
241 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000242
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100243 /* start with the maximum space available */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200244#ifdef FEAT_RIGHTLEFT
245 if (curwin->w_p_rl)
246 pum_width = pum_col - pum_scrollbar + 1;
247 else
248#endif
249 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000250
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100251 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100252 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200253 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100254 /* the width is more than needed for the items, make it
255 * narrower */
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100256 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100257 if (pum_width < p_pw)
258 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200259 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100260 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100261#ifdef FEAT_RIGHTLEFT
262 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100263 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
264 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100265#endif
266 ))
267 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100268 /* align pum edge with "cursor_col" */
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100269#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100270 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100271 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100272 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100273 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100274 if (pum_col >= Columns)
275 pum_col = Columns - 1;
276 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100277 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100278#endif
279 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100280 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
281 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100282 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100283 /* use full width to end of the screen */
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100284 pum_col = Columns - max_width - pum_scrollbar;
285 if (pum_col < 0)
286 pum_col = 0;
287 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100288 }
289
290#ifdef FEAT_RIGHTLEFT
291 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100292 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100293 else
294#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100295 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100296
Bram Moolenaar42443c72018-02-10 18:28:52 +0100297 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100298 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100299 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300#ifdef FEAT_RIGHTLEFT
301 if (curwin->w_p_rl)
302 {
303 if (pum_width > pum_col)
304 pum_width = pum_col;
305 }
306 else
307#endif
308 {
309 if (pum_width >= Columns - pum_col)
310 pum_width = Columns - pum_col - 1;
311 }
312 }
313 else if (pum_width > max_width + pum_kind_width
314 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100315 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100316 {
317 pum_width = max_width + pum_kind_width
318 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100319 if (pum_width < p_pw)
320 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100321 }
322 }
323
Bram Moolenaara5e66212017-09-29 22:42:33 +0200324 }
325 else if (Columns < def_width)
326 {
327 /* not enough room, will use what we have */
328#ifdef FEAT_RIGHTLEFT
329 if (curwin->w_p_rl)
330 pum_col = Columns - 1;
331 else
332#endif
333 pum_col = 0;
334 pum_width = Columns - 1;
335 }
336 else
337 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100338 if (max_width > p_pw)
339 max_width = p_pw; /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200340#ifdef FEAT_RIGHTLEFT
341 if (curwin->w_p_rl)
342 pum_col = max_width - 1;
343 else
344#endif
345 pum_col = Columns - max_width;
346 pum_width = max_width - pum_scrollbar;
347 }
348
Bram Moolenaara5e66212017-09-29 22:42:33 +0200349 /* Set selected item and redraw. If the window size changed need to
350 * redo the positioning. Limit this to two times, when there is not
351 * much room the window size will keep changing. */
352 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000353}
354
355/*
356 * Redraw the popup menu, using "pum_first" and "pum_selected".
357 */
358 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100359pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000360{
361 int row = pum_row;
362 int col;
363 int attr_norm = highlight_attr[HLF_PNI];
364 int attr_select = highlight_attr[HLF_PSI];
365 int attr_scroll = highlight_attr[HLF_PSB];
366 int attr_thumb = highlight_attr[HLF_PST];
367 int attr;
368 int i;
369 int idx;
370 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000371 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000372 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000373 int thumb_pos = 0;
374 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000375 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000376 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000377
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100378 /* Never display more than we have */
379 if (pum_first > pum_size - pum_height)
380 pum_first = pum_size - pum_height;
381
Bram Moolenaar4b779472005-10-04 09:12:31 +0000382 if (pum_scrollbar)
383 {
384 thumb_heigth = pum_height * pum_height / pum_size;
385 if (thumb_heigth == 0)
386 thumb_heigth = 1;
387 thumb_pos = (pum_first * (pum_height - thumb_heigth)
388 + (pum_size - pum_height) / 2)
389 / (pum_size - pum_height);
390 }
391
392 for (i = 0; i < pum_height; ++i)
393 {
394 idx = i + pum_first;
395 attr = (idx == pum_selected) ? attr_select : attr_norm;
396
397 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000398#ifdef FEAT_RIGHTLEFT
399 if (curwin->w_p_rl)
400 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200401 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000402 screen_putchar(' ', row, pum_col + 1, attr);
403 }
404 else
405#endif
406 if (pum_col > 0)
407 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000408
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000409 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000410 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000411 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000412 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000413 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000414 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000415 width = 0;
416 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000417 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000418 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000419 case 1: p = pum_array[idx].pum_text; break;
420 case 2: p = pum_array[idx].pum_kind; break;
421 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000422 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000423 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100424 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000425 {
426 if (s == NULL)
427 s = p;
428 w = ptr2cells(p);
429 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
430 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000431 /* Display the text that fits or comes before a Tab.
432 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000433 char_u *st;
434 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000435
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100436 if (saved != NUL)
437 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000438 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100439 if (saved != NUL)
440 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000441#ifdef FEAT_RIGHTLEFT
442 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000443 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444 if (st != NULL)
445 {
446 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000447
448 if (rt != NULL)
449 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100450 char_u *rt_start = rt;
451 int size;
452
453 size = vim_strsize(rt);
454 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000455 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100456 do
457 {
458 size -= has_mbyte
459 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100460 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100461 } while (size > pum_width);
462
463 if (size < pum_width)
464 {
465 /* Most left character requires
466 * 2-cells but only 1 cell is
467 * available on screen. Put a
468 * '<' on the left of the pum
469 * item */
470 *(--rt) = '<';
471 size++;
472 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000473 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100474 screen_puts_len(rt, (int)STRLEN(rt),
475 row, col - size + 1, attr);
476 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000477 }
478 vim_free(st);
479 }
480 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000481 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000482 else
483#endif
484 {
485 if (st != NULL)
486 {
487 screen_puts_len(st, (int)STRLEN(st), row, col,
488 attr);
489 vim_free(st);
490 }
491 col += width;
492 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000493
494 if (*p != TAB)
495 break;
496
497 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000498#ifdef FEAT_RIGHTLEFT
499 if (curwin->w_p_rl)
500 {
501 screen_puts_len((char_u *)" ", 2, row, col - 1,
502 attr);
503 col -= 2;
504 }
505 else
506#endif
507 {
508 screen_puts_len((char_u *)" ", 2, row, col, attr);
509 col += 2;
510 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000511 totwidth += 2;
512 s = NULL; /* start text at next char */
513 width = 0;
514 }
515 else
516 width += w;
517 }
518
519 if (round > 1)
520 n = pum_kind_width + 1;
521 else
522 n = 1;
523
524 /* Stop when there is nothing more to display. */
525 if (round == 3
526 || (round == 2 && pum_array[idx].pum_extra == NULL)
527 || (round == 1 && pum_array[idx].pum_kind == NULL
528 && pum_array[idx].pum_extra == NULL)
529 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000530 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000531#ifdef FEAT_RIGHTLEFT
532 if (curwin->w_p_rl)
533 {
534 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
535 col + 1, ' ', ' ', attr);
536 col = pum_col - pum_base_width - n + 1;
537 }
538 else
539#endif
540 {
541 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000542 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000543 col = pum_col + pum_base_width + n;
544 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000545 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000546 }
547
Bram Moolenaarabc97732007-08-08 20:49:37 +0000548#ifdef FEAT_RIGHTLEFT
549 if (curwin->w_p_rl)
550 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
551 ' ', attr);
552 else
553#endif
554 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
555 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000556 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000557 {
558#ifdef FEAT_RIGHTLEFT
559 if (curwin->w_p_rl)
560 screen_putchar(' ', row, pum_col - pum_width,
561 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000562 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000563 else
564#endif
565 screen_putchar(' ', row, pum_col + pum_width,
566 i >= thumb_pos && i < thumb_pos + thumb_heigth
567 ? attr_thumb : attr_scroll);
568 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000569
570 ++row;
571 }
572}
573
Bram Moolenaar4b779472005-10-04 09:12:31 +0000574/*
575 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000576 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000577 * This may be repeated when the preview window is used:
578 * "repeat" == 0: open preview window normally
579 * "repeat" == 1: open preview window but don't set the size
580 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000581 * Returns TRUE when the window was resized and the location of the popup menu
582 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000583 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000584 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100585pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000586{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000587 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000588 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000589
Bram Moolenaar4b779472005-10-04 09:12:31 +0000590 pum_selected = n;
591
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000592 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000593 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000594 if (pum_first > pum_selected - 4)
595 {
596 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000597 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000598 if (pum_first > pum_selected - 2)
599 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000600 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000601 if (pum_first < 0)
602 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000603 else if (pum_first > pum_selected)
604 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000605 }
606 else
607 pum_first = pum_selected;
608 }
609 else if (pum_first < pum_selected - pum_height + 5)
610 {
611 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000612 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000613 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000614 {
615 pum_first += pum_height - 2;
616 if (pum_first < pum_selected - pum_height + 1)
617 pum_first = pum_selected - pum_height + 1;
618 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000619 else
620 pum_first = pum_selected - pum_height + 1;
621 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000622
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000623 /* Give a few lines of context when possible. */
624 if (context > 3)
625 context = 3;
626 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000627 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000628 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000629 {
630 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000631 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000632 if (pum_first < 0)
633 pum_first = 0;
634 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000635 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000636 {
637 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000638 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000639 }
640 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000641
Bram Moolenaar4033c552017-09-16 20:54:51 +0200642#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000643 /*
644 * Show extra info in the preview window if there is something and
645 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000646 * Skip this when tried twice already.
647 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000648 * NOTE: Be very careful not to sync undo!
649 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000650 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000651 && Rows > 10
652 && repeat <= 1
653 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000654 {
655 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200656 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000657 int res = OK;
658
Bram Moolenaaree236d02010-10-27 17:11:15 +0200659 /* Open a preview window. 3 lines by default. Prefer
660 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000661 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200662 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
663 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200664 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100665 /* Prevent undo sync here, if an autocommand syncs undo weird
666 * things can happen to the undo tree. */
667 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000668 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100669 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200670 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000671 g_do_tagpreview = 0;
672
673 if (curwin->w_p_pvw)
674 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200675 if (!resized
676 && curbuf->b_nwindows == 1
677 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000678 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
679 && curbuf->b_p_bh[0] == 'w')
680 {
681 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100682 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000683 ml_delete((linenr_T)1, FALSE);
684 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000685 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000686 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000687 /* Don't want to sync undo in the current buffer. */
688 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000689 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000690 --no_u_sync;
691 if (res == OK)
692 {
693 /* Edit a new, empty buffer. Set options for a "wipeout"
694 * buffer. */
695 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
696 set_option_value((char_u *)"bt", 0L,
697 (char_u *)"nofile", OPT_LOCAL);
698 set_option_value((char_u *)"bh", 0L,
699 (char_u *)"wipe", OPT_LOCAL);
700 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000701 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000702 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000703 }
704 if (res == OK)
705 {
706 char_u *p, *e;
707 linenr_T lnum = 0;
708
709 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
710 {
711 e = vim_strchr(p, '\n');
712 if (e == NULL)
713 {
714 ml_append(lnum++, p, 0, FALSE);
715 break;
716 }
717 else
718 {
719 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000720 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000721 *e = '\n';
722 p = e + 1;
723 }
724 }
725
726 /* Increase the height of the preview window to show the
727 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000728 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000729 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000730 if (lnum > p_pvh)
731 lnum = p_pvh;
732 if (curwin->w_height < lnum)
733 {
734 win_setheight((int)lnum);
735 resized = TRUE;
736 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000737 }
738
739 curbuf->b_changed = 0;
740 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100741 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000742 curwin->w_cursor.col = 0;
743
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200744 if ((curwin != curwin_save && win_valid(curwin_save))
745 || (curtab != curtab_save
746 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000747 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200748 if (curtab != curtab_save && valid_tabpage(curtab_save))
749 goto_tabpage_tp(curtab_save, FALSE, FALSE);
750
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200751 /* When the first completion is done and the preview
752 * window is not resized, skip the preview window's
753 * status line redrawing. */
754 if (ins_compl_active() && !resized)
755 curwin->w_redr_status = FALSE;
756
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000757 /* Return cursor to where we were */
758 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000759 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000760
761 /* When the preview window was resized we need to
762 * update the view on the buffer. Only go back to
763 * the window when needed, otherwise it will always be
764 * redraw. */
765 if (resized)
766 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100767 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000768 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100769 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000770 update_topline();
771 }
772
773 /* Update the screen before drawing the popup menu.
774 * Enable updating the status lines. */
775 pum_do_redraw = TRUE;
776 update_screen(0);
777 pum_do_redraw = FALSE;
778
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000779 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100780 {
781 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000782 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100783 --no_u_sync;
784 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000785
786 /* May need to update the screen again when there are
787 * autocommands involved. */
788 pum_do_redraw = TRUE;
789 update_screen(0);
790 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000791 }
792 }
793 }
794 }
795#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000796 }
797
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000798 if (!resized)
799 pum_redraw();
800
801 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000802}
803
804/*
805 * Undisplay the popup menu (later).
806 */
807 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100808pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000809{
810 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +0200811 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000812 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000813 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000814}
815
816/*
817 * Clear the popup menu. Currently only resets the offset to the first
818 * displayed item.
819 */
820 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100821pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000822{
823 pum_first = 0;
824}
825
826/*
827 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000828 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000829 */
830 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100831pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000832{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000833 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000834}
835
Bram Moolenaare3226be2005-12-18 22:10:00 +0000836/*
Bram Moolenaar491ac282018-06-17 14:47:55 +0200837 * Reposition the popup menu to adjust for window layout changes.
838 */
839 void
840pum_may_redraw(void)
841{
842 pumitem_T *array = pum_array;
843 int len = pum_size;
844 int selected = pum_selected;
845
846 if (!pum_visible())
847 return; // nothing to do
848
849 if (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
850 && pum_win_height == curwin->w_height
851 && pum_win_col == curwin->w_wincol
852 && pum_win_width == curwin->w_width)
853 {
854 // window position didn't change, redraw in the same position
855 pum_redraw();
856 }
857 else
858 {
859 int wcol = curwin->w_wcol;
860
861 // Window layout changed, recompute the position.
862 // Use the remembered w_wcol value, the cursor may have moved when a
863 // completion was inserted, but we want the menu in the same position.
864 pum_undisplay();
865 curwin->w_wcol = pum_win_wcol;
866 curwin->w_valid |= VALID_WCOL;
867 pum_display(array, len, selected);
868 curwin->w_wcol = wcol;
869 }
870}
871
872/*
Bram Moolenaare3226be2005-12-18 22:10:00 +0000873 * Return the height of the popup menu, the number of entries visible.
874 * Only valid when pum_visible() returns TRUE!
875 */
876 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100877pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000878{
879 return pum_height;
880}
881
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100882# if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
883 static void
884pum_position_at_mouse(int min_width)
885{
886 if (Rows - mouse_row > pum_size)
887 {
888 /* Enough space below the mouse row. */
889 pum_row = mouse_row + 1;
890 if (pum_height > Rows - pum_row)
891 pum_height = Rows - pum_row;
892 }
893 else
894 {
895 /* Show above the mouse row, reduce height if it does not fit. */
896 pum_row = mouse_row - pum_size;
897 if (pum_row < 0)
898 {
899 pum_height += pum_row;
900 pum_row = 0;
901 }
902 }
903 if (Columns - mouse_col >= pum_base_width
904 || Columns - mouse_col > min_width)
905 /* Enough space to show at mouse column. */
906 pum_col = mouse_col;
907 else
908 /* Not enough space, right align with window. */
909 pum_col = Columns - (pum_base_width > min_width
910 ? min_width : pum_base_width);
911
912 pum_width = Columns - pum_col;
913 if (pum_width > pum_base_width + 1)
914 pum_width = pum_base_width + 1;
915}
916
917# endif
918
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100919# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100920static pumitem_T *balloon_array = NULL;
921static int balloon_arraysize;
922static int balloon_mouse_row = 0;
923static int balloon_mouse_col = 0;
924
Bram Moolenaar246fe032017-11-19 19:56:27 +0100925#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100926#define BALLOON_MIN_HEIGHT 10
927
Bram Moolenaar246fe032017-11-19 19:56:27 +0100928typedef struct {
929 char_u *start;
930 int bytelen;
931 int cells;
932 int indent;
933} balpart_T;
934
935/*
936 * Split a string into parts to display in the balloon.
937 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
938 * strings and make a struct look good.
939 * Resulting array is stored in "array" and returns the size of the array.
940 */
941 int
942split_message(char_u *mesg, pumitem_T **array)
943{
944 garray_T ga;
945 char_u *p;
946 balpart_T *item;
947 int quoted = FALSE;
948 int height;
949 int line;
950 int item_idx;
951 int indent = 0;
952 int max_cells = 0;
953 int max_height = Rows / 2 - 2;
954 int long_item_count = 0;
955 int split_long_items = FALSE;
956
957 ga_init2(&ga, sizeof(balpart_T), 20);
958 p = mesg;
959
960 while (*p != NUL)
961 {
962 if (ga_grow(&ga, 1) == FAIL)
963 goto failed;
964 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
965 item->start = p;
966 item->indent = indent;
967 item->cells = indent * 2;
968 ++ga.ga_len;
969 while (*p != NUL)
970 {
971 if (*p == '"')
972 quoted = !quoted;
973 else if (*p == '\\' && p[1] != NUL)
974 ++p;
975 else if (!quoted)
976 {
977 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
978 {
979 /* Looks like a good point to break. */
980 if (*p == '{')
981 ++indent;
982 else if (*p == '}' && indent > 0)
983 --indent;
984 ++item->cells;
985 p = skipwhite(p + 1);
986 break;
987 }
988 }
989 item->cells += ptr2cells(p);
990 p += MB_PTR2LEN(p);
991 }
992 item->bytelen = p - item->start;
993 if (item->cells > max_cells)
994 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +0100995 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +0100996 }
997
998 height = 2 + ga.ga_len;
999
1000 /* If there are long items and the height is below the limit: split lines */
1001 if (long_item_count > 0 && height + long_item_count <= max_height)
1002 {
1003 split_long_items = TRUE;
1004 height += long_item_count;
1005 }
1006
1007 /* Limit to half the window height, it has to fit above or below the mouse
1008 * position. */
1009 if (height > max_height)
1010 height = max_height;
1011 *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height);
1012 if (*array == NULL)
1013 goto failed;
1014
1015 /* Add an empty line above and below, looks better. */
1016 (*array)->pum_text = vim_strsave((char_u *)"");
1017 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1018
1019 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1020 {
1021 int skip;
1022 int thislen;
1023 int copylen;
1024 int ind;
1025 int cells;
1026
1027 item = ((balpart_T *)ga.ga_data) + item_idx;
1028 for (skip = 0; skip < item->bytelen; skip += thislen)
1029 {
1030 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1031 {
1032 cells = item->indent * 2;
1033 for (p = item->start + skip; p < item->start + item->bytelen;
1034 p += MB_PTR2LEN(p))
1035 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1036 break;
1037 thislen = p - (item->start + skip);
1038 }
1039 else
1040 thislen = item->bytelen;
1041
1042 /* put indent at the start */
1043 p = alloc(thislen + item->indent * 2 + 1);
1044 for (ind = 0; ind < item->indent * 2; ++ind)
1045 p[ind] = ' ';
1046
1047 /* exclude spaces at the end of the string */
1048 for (copylen = thislen; copylen > 0; --copylen)
1049 if (item->start[skip + copylen - 1] != ' ')
1050 break;
1051
1052 vim_strncpy(p + ind, item->start + skip, copylen);
1053 (*array)[line].pum_text = p;
1054 item->indent = 0; /* wrapped line has no indent */
1055 ++line;
1056 }
1057 }
1058 ga_clear(&ga);
1059 return height;
1060
1061failed:
1062 ga_clear(&ga);
1063 return 0;
1064}
1065
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001066 void
1067ui_remove_balloon(void)
1068{
1069 if (balloon_array != NULL)
1070 {
1071 pum_undisplay();
1072 while (balloon_arraysize > 0)
1073 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001074 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001075 }
1076}
1077
1078/*
1079 * Terminal version of a balloon, uses the popup menu code.
1080 */
1081 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001082ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001083{
1084 ui_remove_balloon();
1085
Bram Moolenaar246fe032017-11-19 19:56:27 +01001086 if (mesg == NULL && list == NULL)
1087 return;
1088 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001089 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001090 listitem_T *li;
1091 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001092
Bram Moolenaar246fe032017-11-19 19:56:27 +01001093 balloon_arraysize = list->lv_len;
1094 balloon_array = (pumitem_T *)alloc_clear(
1095 (unsigned)sizeof(pumitem_T) * list->lv_len);
1096 if (balloon_array == NULL)
1097 return;
1098 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1099 {
1100 char_u *text = get_tv_string_chk(&li->li_tv);
1101
1102 balloon_array[idx].pum_text = vim_strsave(
1103 text == NULL ? (char_u *)"" : text);
1104 }
1105 }
1106 else
1107 balloon_arraysize = split_message(mesg, &balloon_array);
1108
1109 if (balloon_arraysize > 0)
1110 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001111 pum_array = balloon_array;
1112 pum_size = balloon_arraysize;
1113 pum_compute_size();
1114 pum_scrollbar = 0;
1115 pum_height = balloon_arraysize;
1116
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001117 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001118 pum_selected = -1;
1119 pum_first = 0;
1120 pum_redraw();
1121 }
1122}
1123
1124/*
1125 * Called when the mouse moved, may remove any displayed balloon.
1126 */
1127 void
1128ui_may_remove_balloon(void)
1129{
1130 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
1131 ui_remove_balloon();
1132}
1133# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001134
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001135# if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
1136/*
1137 * Select the pum entry at the mouse position.
1138 */
1139 static void
1140pum_select_mouse_pos(void)
1141{
1142 int idx = mouse_row - pum_row;
1143
1144 if (idx < 0 || idx >= pum_size)
1145 pum_selected = -1;
1146 else if (*pum_array[idx].pum_text != NUL)
1147 pum_selected = idx;
1148}
1149
1150/*
1151 * Execute the currently selected popup menu item.
1152 */
1153 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001154pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001155{
1156 vimmenu_T *mp;
1157 int idx = 0;
1158 exarg_T ea;
1159
1160 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001161 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001162 {
1163 vim_memset(&ea, 0, sizeof(ea));
1164 execute_menu(&ea, mp);
1165 break;
1166 }
1167}
1168
1169/*
1170 * Open the terminal version of the popup menu and don't return until it is
1171 * closed.
1172 */
1173 void
1174pum_show_popupmenu(vimmenu_T *menu)
1175{
1176 vimmenu_T *mp;
1177 int idx = 0;
1178 pumitem_T *array;
1179#ifdef FEAT_BEVAL_TERM
1180 int save_bevalterm = p_bevalterm;
1181#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001182 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001183
1184 pum_undisplay();
1185 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001186 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001187
1188 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001189 if (menu_is_separator(mp->dname)
1190 || (mp->modes & mp->enabled & mode))
1191 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001192
1193 array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * pum_size);
1194 if (array == NULL)
1195 return;
1196
1197 for (mp = menu->children; mp != NULL; mp = mp->next)
1198 if (menu_is_separator(mp->dname))
1199 array[idx++].pum_text = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001200 else if (mp->modes & mp->enabled & mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001201 array[idx++].pum_text = mp->dname;
1202
1203 pum_array = array;
1204 pum_compute_size();
1205 pum_scrollbar = 0;
1206 pum_height = pum_size;
1207 pum_position_at_mouse(20);
1208
1209 pum_selected = -1;
1210 pum_first = 0;
1211# ifdef FEAT_BEVAL_TERM
1212 p_bevalterm = TRUE; /* track mouse movement */
1213 mch_setmouse(TRUE);
1214# endif
1215
1216 for (;;)
1217 {
1218 int c;
1219
1220 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001221 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001222 out_flush();
1223
1224 c = vgetc();
Bram Moolenaar52f18a12018-03-07 22:09:11 +01001225 if (c == ESC || c == Ctrl_C)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001226 break;
1227 else if (c == CAR || c == NL)
1228 {
1229 /* enter: select current item, if any, and close */
Bram Moolenaar987723e2018-03-06 11:43:04 +01001230 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001231 break;
1232 }
1233 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1234 {
1235 /* cursor up: select previous item */
1236 while (pum_selected > 0)
1237 {
1238 --pum_selected;
1239 if (*array[pum_selected].pum_text != NUL)
1240 break;
1241 }
1242 }
1243 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1244 {
1245 /* cursor down: select next item */
1246 while (pum_selected < pum_size - 1)
1247 {
1248 ++pum_selected;
1249 if (*array[pum_selected].pum_text != NUL)
1250 break;
1251 }
1252 }
1253 else if (c == K_RIGHTMOUSE)
1254 {
1255 /* Right mouse down: reposition the menu. */
1256 vungetc(c);
1257 break;
1258 }
1259 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1260 {
Bram Moolenaar52f18a12018-03-07 22:09:11 +01001261 /* mouse moved: select item in the mouse row */
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001262 pum_select_mouse_pos();
1263 }
1264 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1265 {
1266 /* left mouse click: select clicked item, if any, and close;
1267 * right mouse release: select clicked item, close if any */
1268 pum_select_mouse_pos();
1269 if (pum_selected >= 0)
1270 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001271 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001272 break;
1273 }
1274 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1275 break;
1276 }
1277 }
1278
1279 vim_free(array);
1280 pum_undisplay();
1281# ifdef FEAT_BEVAL_TERM
1282 p_bevalterm = save_bevalterm;
1283 mch_setmouse(TRUE);
1284# endif
1285}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001286
1287 void
1288pum_make_popup(char_u *path_name, int use_mouse_pos)
1289{
1290 vimmenu_T *menu;
1291
1292 if (!use_mouse_pos)
1293 {
1294 /* Hack: set mouse position at the cursor so that the menu pops up
1295 * around there. */
1296 mouse_row = curwin->w_winrow + curwin->w_wrow;
1297 mouse_col = curwin->w_wincol + curwin->w_wcol;
1298 }
1299
1300 menu = gui_find_menu(path_name);
1301 if (menu != NULL)
1302 pum_show_popupmenu(menu);
1303}
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001304# endif
1305
Bram Moolenaar4b779472005-10-04 09:12:31 +00001306#endif