blob: 23e8711906e0360313e68c1fc7ea1191f50bdb67 [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 Moolenaar4b779472005-10-04 09:12:31 +000026static int pum_scrollbar; /* TRUE when scrollbar present */
27
28static int pum_row; /* top row of pum */
29static int pum_col; /* left column of pum */
30
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000031static int pum_do_redraw = FALSE; /* do redraw anyway */
32
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010033static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000034
Bram Moolenaar4b779472005-10-04 09:12:31 +000035#define PUM_DEF_HEIGHT 10
36#define PUM_DEF_WIDTH 15
37
38/*
39 * Show the popup menu with items "array[size]".
40 * "array" must remain valid until pum_undisplay() is called!
41 * When possible the leftmost character is aligned with screen column "col".
42 * The menu appears above the screen line "row" or at "row" + "height" - 1.
43 */
44 void
Bram Moolenaar05540972016-01-30 20:31:25 +010045pum_display(
46 pumitem_T *array,
47 int size,
48 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000049 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000050{
51 int w;
52 int def_width;
53 int max_width;
54 int kind_width;
55 int extra_width;
56 int i;
Bram Moolenaar4b779472005-10-04 09:12:31 +000057 int row;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000058 int context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +000059 int col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010060 int above_row;
61 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000062 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020063#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010064 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +010065#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000066
67redo:
68 def_width = PUM_DEF_WIDTH;
69 max_width = 0;
70 kind_width = 0;
71 extra_width = 0;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010072 above_row = 0;
73 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000074
Bram Moolenaar1e607892006-03-13 22:15:53 +000075 /* Pretend the pum is already there to avoid that must_redraw is set when
76 * 'cuc' is on. */
77 pum_array = (pumitem_T *)1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000078 validate_cursor_col();
Bram Moolenaar1e607892006-03-13 22:15:53 +000079 pum_array = NULL;
80
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000081 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000082
Bram Moolenaar4033c552017-09-16 20:54:51 +020083#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010084 FOR_ALL_WINDOWS(pvwin)
85 if (pvwin->w_p_pvw)
86 break;
87 if (pvwin != NULL)
88 {
89 if (W_WINROW(pvwin) < W_WINROW(curwin))
90 above_row = W_WINROW(pvwin) + pvwin->w_height;
91 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
92 below_row = W_WINROW(pvwin);
93 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010094#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000095
Bram Moolenaar4b779472005-10-04 09:12:31 +000096 /*
97 * Figure out the size and position of the pum.
98 */
99 if (size < PUM_DEF_HEIGHT)
100 pum_height = size;
101 else
102 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000103 if (p_ph > 0 && pum_height > p_ph)
104 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000105
106 /* Put the pum below "row" if possible. If there are few lines decide on
107 * where there is more room. */
Bram Moolenaar73095282016-11-24 17:47:07 +0100108 if (row + 2 >= below_row - pum_height
109 && row - above_row > (below_row - above_row) / 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000110 {
111 /* pum above "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000112
113 /* Leave two lines of context if possible */
114 if (curwin->w_wrow - curwin->w_cline_row >= 2)
115 context_lines = 2;
116 else
117 context_lines = curwin->w_wrow - curwin->w_cline_row;
118
119 if (row >= size + context_lines)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000120 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000121 pum_row = row - size - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000122 pum_height = size;
123 }
124 else
125 {
126 pum_row = 0;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000127 pum_height = row - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000128 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000129 if (p_ph > 0 && pum_height > p_ph)
130 {
131 pum_row += pum_height - p_ph;
132 pum_height = p_ph;
133 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000134 }
135 else
136 {
137 /* pum below "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000138
139 /* Leave two lines of context if possible */
140 if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3)
141 context_lines = 3;
142 else
143 context_lines = curwin->w_cline_row
144 + curwin->w_cline_height - curwin->w_wrow;
145
146 pum_row = row + context_lines;
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100147 if (size > below_row - pum_row)
148 pum_height = below_row - pum_row;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000149 else
150 pum_height = size;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000151 if (p_ph > 0 && pum_height > p_ph)
152 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000153 }
154
155 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000156 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000157 return;
158
Bram Moolenaar4033c552017-09-16 20:54:51 +0200159#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100160 /* If there is a preview window at the above avoid drawing over it. */
161 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000162 {
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100163 pum_row += above_row;
164 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000165 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100166#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000167
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000168 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000169 for (i = 0; i < size; ++i)
170 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000171 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000172 if (max_width < w)
173 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000174 if (array[i].pum_kind != NULL)
175 {
176 w = vim_strsize(array[i].pum_kind) + 1;
177 if (kind_width < w)
178 kind_width = w;
179 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000180 if (array[i].pum_extra != NULL)
181 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000182 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000183 if (extra_width < w)
184 extra_width = w;
185 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000186 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000187 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000188 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000189
Bram Moolenaarabc97732007-08-08 20:49:37 +0000190 /* Calculate column */
191#ifdef FEAT_RIGHTLEFT
192 if (curwin->w_p_rl)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000193 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000194 else
195#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000196 col = W_WINCOL(curwin) + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000197
Bram Moolenaar4b779472005-10-04 09:12:31 +0000198 /* if there are more items than room we need a scrollbar */
199 if (pum_height < size)
200 {
201 pum_scrollbar = 1;
202 ++max_width;
203 }
204 else
205 pum_scrollbar = 0;
206
207 if (def_width < max_width)
208 def_width = max_width;
209
Bram Moolenaarabc97732007-08-08 20:49:37 +0000210 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
211#ifdef FEAT_RIGHTLEFT
212 && !curwin->w_p_rl)
213 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
214#endif
215 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000216 {
217 /* align pum column with "col" */
218 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000219
220#ifdef FEAT_RIGHTLEFT
221 if (curwin->w_p_rl)
222 pum_width = pum_col - pum_scrollbar + 1;
223 else
224#endif
225 pum_width = Columns - pum_col - pum_scrollbar;
226
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000227 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000228 && pum_width > PUM_DEF_WIDTH)
229 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000230 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000231 if (pum_width < PUM_DEF_WIDTH)
232 pum_width = PUM_DEF_WIDTH;
233 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000234 }
235 else if (Columns < def_width)
236 {
237 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000238#ifdef FEAT_RIGHTLEFT
239 if (curwin->w_p_rl)
240 pum_col = Columns - 1;
241 else
242#endif
243 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000244 pum_width = Columns - 1;
245 }
246 else
247 {
248 if (max_width > PUM_DEF_WIDTH)
249 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000250#ifdef FEAT_RIGHTLEFT
251 if (curwin->w_p_rl)
252 pum_col = max_width - 1;
253 else
254#endif
255 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000256 pum_width = max_width - pum_scrollbar;
257 }
258
259 pum_array = array;
260 pum_size = size;
261
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000262 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000263 * the positioning. Limit this to two times, when there is not much
264 * room the window size will keep changing. */
265 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000266 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000267}
268
269/*
270 * Redraw the popup menu, using "pum_first" and "pum_selected".
271 */
272 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100273pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000274{
275 int row = pum_row;
276 int col;
277 int attr_norm = highlight_attr[HLF_PNI];
278 int attr_select = highlight_attr[HLF_PSI];
279 int attr_scroll = highlight_attr[HLF_PSB];
280 int attr_thumb = highlight_attr[HLF_PST];
281 int attr;
282 int i;
283 int idx;
284 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000285 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000286 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000287 int thumb_pos = 0;
288 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000289 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000290 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000291
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100292 /* Never display more than we have */
293 if (pum_first > pum_size - pum_height)
294 pum_first = pum_size - pum_height;
295
Bram Moolenaar4b779472005-10-04 09:12:31 +0000296 if (pum_scrollbar)
297 {
298 thumb_heigth = pum_height * pum_height / pum_size;
299 if (thumb_heigth == 0)
300 thumb_heigth = 1;
301 thumb_pos = (pum_first * (pum_height - thumb_heigth)
302 + (pum_size - pum_height) / 2)
303 / (pum_size - pum_height);
304 }
305
306 for (i = 0; i < pum_height; ++i)
307 {
308 idx = i + pum_first;
309 attr = (idx == pum_selected) ? attr_select : attr_norm;
310
311 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000312#ifdef FEAT_RIGHTLEFT
313 if (curwin->w_p_rl)
314 {
315 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
316 screen_putchar(' ', row, pum_col + 1, attr);
317 }
318 else
319#endif
320 if (pum_col > 0)
321 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000322
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000323 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000324 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000325 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000326 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000327 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000328 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000329 width = 0;
330 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000331 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000332 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000333 case 1: p = pum_array[idx].pum_text; break;
334 case 2: p = pum_array[idx].pum_kind; break;
335 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000336 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000337 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100338 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000339 {
340 if (s == NULL)
341 s = p;
342 w = ptr2cells(p);
343 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
344 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000345 /* Display the text that fits or comes before a Tab.
346 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000347 char_u *st;
348 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000349
350 *p = NUL;
351 st = transstr(s);
352 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000353#ifdef FEAT_RIGHTLEFT
354 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000355 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000356 if (st != NULL)
357 {
358 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000359
360 if (rt != NULL)
361 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100362 char_u *rt_start = rt;
363 int size;
364
365 size = vim_strsize(rt);
366 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000367 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100368 do
369 {
370 size -= has_mbyte
371 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100372 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100373 } while (size > pum_width);
374
375 if (size < pum_width)
376 {
377 /* Most left character requires
378 * 2-cells but only 1 cell is
379 * available on screen. Put a
380 * '<' on the left of the pum
381 * item */
382 *(--rt) = '<';
383 size++;
384 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000385 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100386 screen_puts_len(rt, (int)STRLEN(rt),
387 row, col - size + 1, attr);
388 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000389 }
390 vim_free(st);
391 }
392 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000393 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000394 else
395#endif
396 {
397 if (st != NULL)
398 {
399 screen_puts_len(st, (int)STRLEN(st), row, col,
400 attr);
401 vim_free(st);
402 }
403 col += width;
404 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000405
406 if (*p != TAB)
407 break;
408
409 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000410#ifdef FEAT_RIGHTLEFT
411 if (curwin->w_p_rl)
412 {
413 screen_puts_len((char_u *)" ", 2, row, col - 1,
414 attr);
415 col -= 2;
416 }
417 else
418#endif
419 {
420 screen_puts_len((char_u *)" ", 2, row, col, attr);
421 col += 2;
422 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000423 totwidth += 2;
424 s = NULL; /* start text at next char */
425 width = 0;
426 }
427 else
428 width += w;
429 }
430
431 if (round > 1)
432 n = pum_kind_width + 1;
433 else
434 n = 1;
435
436 /* Stop when there is nothing more to display. */
437 if (round == 3
438 || (round == 2 && pum_array[idx].pum_extra == NULL)
439 || (round == 1 && pum_array[idx].pum_kind == NULL
440 && pum_array[idx].pum_extra == NULL)
441 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000442 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000443#ifdef FEAT_RIGHTLEFT
444 if (curwin->w_p_rl)
445 {
446 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
447 col + 1, ' ', ' ', attr);
448 col = pum_col - pum_base_width - n + 1;
449 }
450 else
451#endif
452 {
453 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000454 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000455 col = pum_col + pum_base_width + n;
456 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000457 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000458 }
459
Bram Moolenaarabc97732007-08-08 20:49:37 +0000460#ifdef FEAT_RIGHTLEFT
461 if (curwin->w_p_rl)
462 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
463 ' ', attr);
464 else
465#endif
466 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
467 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000468 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000469 {
470#ifdef FEAT_RIGHTLEFT
471 if (curwin->w_p_rl)
472 screen_putchar(' ', row, pum_col - pum_width,
473 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000474 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000475 else
476#endif
477 screen_putchar(' ', row, pum_col + pum_width,
478 i >= thumb_pos && i < thumb_pos + thumb_heigth
479 ? attr_thumb : attr_scroll);
480 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000481
482 ++row;
483 }
484}
485
Bram Moolenaar4b779472005-10-04 09:12:31 +0000486/*
487 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000488 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000489 * This may be repeated when the preview window is used:
490 * "repeat" == 0: open preview window normally
491 * "repeat" == 1: open preview window but don't set the size
492 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000493 * Returns TRUE when the window was resized and the location of the popup menu
494 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000495 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000496 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100497pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000498{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000499 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000500 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000501
Bram Moolenaar4b779472005-10-04 09:12:31 +0000502 pum_selected = n;
503
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000504 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000505 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000506 if (pum_first > pum_selected - 4)
507 {
508 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000509 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000510 if (pum_first > pum_selected - 2)
511 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000512 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000513 if (pum_first < 0)
514 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000515 else if (pum_first > pum_selected)
516 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000517 }
518 else
519 pum_first = pum_selected;
520 }
521 else if (pum_first < pum_selected - pum_height + 5)
522 {
523 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000524 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000525 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000526 {
527 pum_first += pum_height - 2;
528 if (pum_first < pum_selected - pum_height + 1)
529 pum_first = pum_selected - pum_height + 1;
530 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000531 else
532 pum_first = pum_selected - pum_height + 1;
533 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000534
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000535 /* Give a few lines of context when possible. */
536 if (context > 3)
537 context = 3;
538 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000540 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000541 {
542 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000543 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000544 if (pum_first < 0)
545 pum_first = 0;
546 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000547 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000548 {
549 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000550 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000551 }
552 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000553
Bram Moolenaar4033c552017-09-16 20:54:51 +0200554#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000555 /*
556 * Show extra info in the preview window if there is something and
557 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000558 * Skip this when tried twice already.
559 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000560 * NOTE: Be very careful not to sync undo!
561 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000562 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000563 && Rows > 10
564 && repeat <= 1
565 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000566 {
567 win_T *curwin_save = curwin;
568 int res = OK;
569
Bram Moolenaaree236d02010-10-27 17:11:15 +0200570 /* Open a preview window. 3 lines by default. Prefer
571 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000572 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200573 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
574 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200575 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100576 /* Prevent undo sync here, if an autocommand syncs undo weird
577 * things can happen to the undo tree. */
578 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000579 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100580 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200581 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000582 g_do_tagpreview = 0;
583
584 if (curwin->w_p_pvw)
585 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200586 if (!resized
587 && curbuf->b_nwindows == 1
588 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000589 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
590 && curbuf->b_p_bh[0] == 'w')
591 {
592 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100593 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000594 ml_delete((linenr_T)1, FALSE);
595 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000596 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000597 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000598 /* Don't want to sync undo in the current buffer. */
599 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000600 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000601 --no_u_sync;
602 if (res == OK)
603 {
604 /* Edit a new, empty buffer. Set options for a "wipeout"
605 * buffer. */
606 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
607 set_option_value((char_u *)"bt", 0L,
608 (char_u *)"nofile", OPT_LOCAL);
609 set_option_value((char_u *)"bh", 0L,
610 (char_u *)"wipe", OPT_LOCAL);
611 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000612 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000613 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000614 }
615 if (res == OK)
616 {
617 char_u *p, *e;
618 linenr_T lnum = 0;
619
620 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
621 {
622 e = vim_strchr(p, '\n');
623 if (e == NULL)
624 {
625 ml_append(lnum++, p, 0, FALSE);
626 break;
627 }
628 else
629 {
630 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000631 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000632 *e = '\n';
633 p = e + 1;
634 }
635 }
636
637 /* Increase the height of the preview window to show the
638 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000639 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000640 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000641 if (lnum > p_pvh)
642 lnum = p_pvh;
643 if (curwin->w_height < lnum)
644 {
645 win_setheight((int)lnum);
646 resized = TRUE;
647 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000648 }
649
650 curbuf->b_changed = 0;
651 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100652 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000653 curwin->w_cursor.col = 0;
654
655 if (curwin != curwin_save && win_valid(curwin_save))
656 {
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200657 /* When the first completion is done and the preview
658 * window is not resized, skip the preview window's
659 * status line redrawing. */
660 if (ins_compl_active() && !resized)
661 curwin->w_redr_status = FALSE;
662
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000663 /* Return cursor to where we were */
664 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000665 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000666
667 /* When the preview window was resized we need to
668 * update the view on the buffer. Only go back to
669 * the window when needed, otherwise it will always be
670 * redraw. */
671 if (resized)
672 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100673 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000674 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100675 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000676 update_topline();
677 }
678
679 /* Update the screen before drawing the popup menu.
680 * Enable updating the status lines. */
681 pum_do_redraw = TRUE;
682 update_screen(0);
683 pum_do_redraw = FALSE;
684
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000685 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100686 {
687 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000688 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100689 --no_u_sync;
690 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000691
692 /* May need to update the screen again when there are
693 * autocommands involved. */
694 pum_do_redraw = TRUE;
695 update_screen(0);
696 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000697 }
698 }
699 }
700 }
701#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000702 }
703
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000704 if (!resized)
705 pum_redraw();
706
707 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000708}
709
710/*
711 * Undisplay the popup menu (later).
712 */
713 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100714pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000715{
716 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000717 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000718 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000719 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000720}
721
722/*
723 * Clear the popup menu. Currently only resets the offset to the first
724 * displayed item.
725 */
726 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100727pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000728{
729 pum_first = 0;
730}
731
732/*
733 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000734 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000735 */
736 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100737pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000738{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000739 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000740}
741
Bram Moolenaare3226be2005-12-18 22:10:00 +0000742/*
743 * Return the height of the popup menu, the number of entries visible.
744 * Only valid when pum_visible() returns TRUE!
745 */
746 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100747pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000748{
749 return pum_height;
750}
751
Bram Moolenaar4b779472005-10-04 09:12:31 +0000752#endif