blob: 6a75686ff8d7169f9d5c670528cd10dd88174219 [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 Moolenaar91e44a32016-11-04 20:08:52 +010063 win_T *pvwin;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000064
65redo:
66 def_width = PUM_DEF_WIDTH;
67 max_width = 0;
68 kind_width = 0;
69 extra_width = 0;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010070 above_row = 0;
71 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000072
Bram Moolenaar1e607892006-03-13 22:15:53 +000073 /* Pretend the pum is already there to avoid that must_redraw is set when
74 * 'cuc' is on. */
75 pum_array = (pumitem_T *)1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000076 validate_cursor_col();
Bram Moolenaar1e607892006-03-13 22:15:53 +000077 pum_array = NULL;
78
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000079 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000080
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010081#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010082 FOR_ALL_WINDOWS(pvwin)
83 if (pvwin->w_p_pvw)
84 break;
85 if (pvwin != NULL)
86 {
87 if (W_WINROW(pvwin) < W_WINROW(curwin))
88 above_row = W_WINROW(pvwin) + pvwin->w_height;
89 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
90 below_row = W_WINROW(pvwin);
91 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010092#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000093
Bram Moolenaar4b779472005-10-04 09:12:31 +000094 /*
95 * Figure out the size and position of the pum.
96 */
97 if (size < PUM_DEF_HEIGHT)
98 pum_height = size;
99 else
100 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000101 if (p_ph > 0 && pum_height > p_ph)
102 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000103
104 /* Put the pum below "row" if possible. If there are few lines decide on
105 * where there is more room. */
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100106 if (row - above_row >= below_row - row)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000107 {
108 /* pum above "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000109
110 /* Leave two lines of context if possible */
111 if (curwin->w_wrow - curwin->w_cline_row >= 2)
112 context_lines = 2;
113 else
114 context_lines = curwin->w_wrow - curwin->w_cline_row;
115
116 if (row >= size + context_lines)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000117 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000118 pum_row = row - size - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000119 pum_height = size;
120 }
121 else
122 {
123 pum_row = 0;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000124 pum_height = row - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000125 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000126 if (p_ph > 0 && pum_height > p_ph)
127 {
128 pum_row += pum_height - p_ph;
129 pum_height = p_ph;
130 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000131 }
132 else
133 {
134 /* pum below "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000135
136 /* Leave two lines of context if possible */
137 if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3)
138 context_lines = 3;
139 else
140 context_lines = curwin->w_cline_row
141 + curwin->w_cline_height - curwin->w_wrow;
142
143 pum_row = row + context_lines;
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100144 if (size > below_row - pum_row)
145 pum_height = below_row - pum_row;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000146 else
147 pum_height = size;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000148 if (p_ph > 0 && pum_height > p_ph)
149 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000150 }
151
152 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000153 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000154 return;
155
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100156#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100157 /* If there is a preview window at the above avoid drawing over it. */
158 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000159 {
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100160 pum_row += above_row;
161 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000162 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100163#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000164
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000165 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000166 for (i = 0; i < size; ++i)
167 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000168 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000169 if (max_width < w)
170 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000171 if (array[i].pum_kind != NULL)
172 {
173 w = vim_strsize(array[i].pum_kind) + 1;
174 if (kind_width < w)
175 kind_width = w;
176 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000177 if (array[i].pum_extra != NULL)
178 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000179 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000180 if (extra_width < w)
181 extra_width = w;
182 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000183 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000184 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000185 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000186
Bram Moolenaarabc97732007-08-08 20:49:37 +0000187 /* Calculate column */
188#ifdef FEAT_RIGHTLEFT
189 if (curwin->w_p_rl)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000190 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000191 else
192#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000193 col = W_WINCOL(curwin) + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000194
Bram Moolenaar4b779472005-10-04 09:12:31 +0000195 /* if there are more items than room we need a scrollbar */
196 if (pum_height < size)
197 {
198 pum_scrollbar = 1;
199 ++max_width;
200 }
201 else
202 pum_scrollbar = 0;
203
204 if (def_width < max_width)
205 def_width = max_width;
206
Bram Moolenaarabc97732007-08-08 20:49:37 +0000207 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
208#ifdef FEAT_RIGHTLEFT
209 && !curwin->w_p_rl)
210 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
211#endif
212 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000213 {
214 /* align pum column with "col" */
215 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000216
217#ifdef FEAT_RIGHTLEFT
218 if (curwin->w_p_rl)
219 pum_width = pum_col - pum_scrollbar + 1;
220 else
221#endif
222 pum_width = Columns - pum_col - pum_scrollbar;
223
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000224 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000225 && pum_width > PUM_DEF_WIDTH)
226 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000227 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000228 if (pum_width < PUM_DEF_WIDTH)
229 pum_width = PUM_DEF_WIDTH;
230 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000231 }
232 else if (Columns < def_width)
233 {
234 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235#ifdef FEAT_RIGHTLEFT
236 if (curwin->w_p_rl)
237 pum_col = Columns - 1;
238 else
239#endif
240 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000241 pum_width = Columns - 1;
242 }
243 else
244 {
245 if (max_width > PUM_DEF_WIDTH)
246 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000247#ifdef FEAT_RIGHTLEFT
248 if (curwin->w_p_rl)
249 pum_col = max_width - 1;
250 else
251#endif
252 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000253 pum_width = max_width - pum_scrollbar;
254 }
255
256 pum_array = array;
257 pum_size = size;
258
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000259 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000260 * the positioning. Limit this to two times, when there is not much
261 * room the window size will keep changing. */
262 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000263 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000264}
265
266/*
267 * Redraw the popup menu, using "pum_first" and "pum_selected".
268 */
269 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100270pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000271{
272 int row = pum_row;
273 int col;
274 int attr_norm = highlight_attr[HLF_PNI];
275 int attr_select = highlight_attr[HLF_PSI];
276 int attr_scroll = highlight_attr[HLF_PSB];
277 int attr_thumb = highlight_attr[HLF_PST];
278 int attr;
279 int i;
280 int idx;
281 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000282 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000283 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000284 int thumb_pos = 0;
285 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000286 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000287 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000288
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100289 /* Never display more than we have */
290 if (pum_first > pum_size - pum_height)
291 pum_first = pum_size - pum_height;
292
Bram Moolenaar4b779472005-10-04 09:12:31 +0000293 if (pum_scrollbar)
294 {
295 thumb_heigth = pum_height * pum_height / pum_size;
296 if (thumb_heigth == 0)
297 thumb_heigth = 1;
298 thumb_pos = (pum_first * (pum_height - thumb_heigth)
299 + (pum_size - pum_height) / 2)
300 / (pum_size - pum_height);
301 }
302
303 for (i = 0; i < pum_height; ++i)
304 {
305 idx = i + pum_first;
306 attr = (idx == pum_selected) ? attr_select : attr_norm;
307
308 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000309#ifdef FEAT_RIGHTLEFT
310 if (curwin->w_p_rl)
311 {
312 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
313 screen_putchar(' ', row, pum_col + 1, attr);
314 }
315 else
316#endif
317 if (pum_col > 0)
318 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000319
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000320 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000321 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000322 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000323 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000324 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000325 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000326 width = 0;
327 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000328 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000329 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000330 case 1: p = pum_array[idx].pum_text; break;
331 case 2: p = pum_array[idx].pum_kind; break;
332 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000333 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000334 if (p != NULL)
335 for ( ; ; mb_ptr_adv(p))
336 {
337 if (s == NULL)
338 s = p;
339 w = ptr2cells(p);
340 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
341 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000342 /* Display the text that fits or comes before a Tab.
343 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000344 char_u *st;
345 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000346
347 *p = NUL;
348 st = transstr(s);
349 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000350#ifdef FEAT_RIGHTLEFT
351 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000352 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000353 if (st != NULL)
354 {
355 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000356
357 if (rt != NULL)
358 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100359 char_u *rt_start = rt;
360 int size;
361
362 size = vim_strsize(rt);
363 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000364 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100365 do
366 {
367 size -= has_mbyte
368 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000369 mb_ptr_adv(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100370 } while (size > pum_width);
371
372 if (size < pum_width)
373 {
374 /* Most left character requires
375 * 2-cells but only 1 cell is
376 * available on screen. Put a
377 * '<' on the left of the pum
378 * item */
379 *(--rt) = '<';
380 size++;
381 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000382 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100383 screen_puts_len(rt, (int)STRLEN(rt),
384 row, col - size + 1, attr);
385 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000386 }
387 vim_free(st);
388 }
389 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000390 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000391 else
392#endif
393 {
394 if (st != NULL)
395 {
396 screen_puts_len(st, (int)STRLEN(st), row, col,
397 attr);
398 vim_free(st);
399 }
400 col += width;
401 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000402
403 if (*p != TAB)
404 break;
405
406 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000407#ifdef FEAT_RIGHTLEFT
408 if (curwin->w_p_rl)
409 {
410 screen_puts_len((char_u *)" ", 2, row, col - 1,
411 attr);
412 col -= 2;
413 }
414 else
415#endif
416 {
417 screen_puts_len((char_u *)" ", 2, row, col, attr);
418 col += 2;
419 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000420 totwidth += 2;
421 s = NULL; /* start text at next char */
422 width = 0;
423 }
424 else
425 width += w;
426 }
427
428 if (round > 1)
429 n = pum_kind_width + 1;
430 else
431 n = 1;
432
433 /* Stop when there is nothing more to display. */
434 if (round == 3
435 || (round == 2 && pum_array[idx].pum_extra == NULL)
436 || (round == 1 && pum_array[idx].pum_kind == NULL
437 && pum_array[idx].pum_extra == NULL)
438 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000439 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000440#ifdef FEAT_RIGHTLEFT
441 if (curwin->w_p_rl)
442 {
443 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
444 col + 1, ' ', ' ', attr);
445 col = pum_col - pum_base_width - n + 1;
446 }
447 else
448#endif
449 {
450 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000451 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000452 col = pum_col + pum_base_width + n;
453 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000454 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000455 }
456
Bram Moolenaarabc97732007-08-08 20:49:37 +0000457#ifdef FEAT_RIGHTLEFT
458 if (curwin->w_p_rl)
459 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
460 ' ', attr);
461 else
462#endif
463 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
464 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000465 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000466 {
467#ifdef FEAT_RIGHTLEFT
468 if (curwin->w_p_rl)
469 screen_putchar(' ', row, pum_col - pum_width,
470 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000471 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000472 else
473#endif
474 screen_putchar(' ', row, pum_col + pum_width,
475 i >= thumb_pos && i < thumb_pos + thumb_heigth
476 ? attr_thumb : attr_scroll);
477 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000478
479 ++row;
480 }
481}
482
Bram Moolenaar4b779472005-10-04 09:12:31 +0000483/*
484 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000485 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000486 * This may be repeated when the preview window is used:
487 * "repeat" == 0: open preview window normally
488 * "repeat" == 1: open preview window but don't set the size
489 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000490 * Returns TRUE when the window was resized and the location of the popup menu
491 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000492 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000493 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100494pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000495{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000496 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000497 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000498
Bram Moolenaar4b779472005-10-04 09:12:31 +0000499 pum_selected = n;
500
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000501 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000502 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000503 if (pum_first > pum_selected - 4)
504 {
505 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000506 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000507 if (pum_first > pum_selected - 2)
508 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000509 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000510 if (pum_first < 0)
511 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000512 else if (pum_first > pum_selected)
513 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000514 }
515 else
516 pum_first = pum_selected;
517 }
518 else if (pum_first < pum_selected - pum_height + 5)
519 {
520 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000521 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000522 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000523 {
524 pum_first += pum_height - 2;
525 if (pum_first < pum_selected - pum_height + 1)
526 pum_first = pum_selected - pum_height + 1;
527 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000528 else
529 pum_first = pum_selected - pum_height + 1;
530 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000531
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000532 /* Give a few lines of context when possible. */
533 if (context > 3)
534 context = 3;
535 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000536 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000537 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000538 {
539 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000540 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000541 if (pum_first < 0)
542 pum_first = 0;
543 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000544 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000545 {
546 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000547 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000548 }
549 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000550
551#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000552 /*
553 * Show extra info in the preview window if there is something and
554 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000555 * Skip this when tried twice already.
556 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000557 * NOTE: Be very careful not to sync undo!
558 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000559 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000560 && Rows > 10
561 && repeat <= 1
562 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000563 {
564 win_T *curwin_save = curwin;
565 int res = OK;
566
Bram Moolenaaree236d02010-10-27 17:11:15 +0200567 /* Open a preview window. 3 lines by default. Prefer
568 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000569 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200570 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
571 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200572 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100573 /* Prevent undo sync here, if an autocommand syncs undo weird
574 * things can happen to the undo tree. */
575 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000576 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100577 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200578 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000579 g_do_tagpreview = 0;
580
581 if (curwin->w_p_pvw)
582 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200583 if (!resized
584 && curbuf->b_nwindows == 1
585 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000586 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
587 && curbuf->b_p_bh[0] == 'w')
588 {
589 /* Already a "wipeout" buffer, make it empty. */
590 while (!bufempty())
591 ml_delete((linenr_T)1, FALSE);
592 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000593 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000594 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000595 /* Don't want to sync undo in the current buffer. */
596 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000597 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000598 --no_u_sync;
599 if (res == OK)
600 {
601 /* Edit a new, empty buffer. Set options for a "wipeout"
602 * buffer. */
603 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
604 set_option_value((char_u *)"bt", 0L,
605 (char_u *)"nofile", OPT_LOCAL);
606 set_option_value((char_u *)"bh", 0L,
607 (char_u *)"wipe", OPT_LOCAL);
608 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000609 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000610 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000611 }
612 if (res == OK)
613 {
614 char_u *p, *e;
615 linenr_T lnum = 0;
616
617 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
618 {
619 e = vim_strchr(p, '\n');
620 if (e == NULL)
621 {
622 ml_append(lnum++, p, 0, FALSE);
623 break;
624 }
625 else
626 {
627 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000628 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000629 *e = '\n';
630 p = e + 1;
631 }
632 }
633
634 /* Increase the height of the preview window to show the
635 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000636 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000637 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000638 if (lnum > p_pvh)
639 lnum = p_pvh;
640 if (curwin->w_height < lnum)
641 {
642 win_setheight((int)lnum);
643 resized = TRUE;
644 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000645 }
646
647 curbuf->b_changed = 0;
648 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100649 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000650 curwin->w_cursor.col = 0;
651
652 if (curwin != curwin_save && win_valid(curwin_save))
653 {
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200654 /* When the first completion is done and the preview
655 * window is not resized, skip the preview window's
656 * status line redrawing. */
657 if (ins_compl_active() && !resized)
658 curwin->w_redr_status = FALSE;
659
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000660 /* Return cursor to where we were */
661 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000662 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000663
664 /* When the preview window was resized we need to
665 * update the view on the buffer. Only go back to
666 * the window when needed, otherwise it will always be
667 * redraw. */
668 if (resized)
669 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100670 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000671 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100672 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000673 update_topline();
674 }
675
676 /* Update the screen before drawing the popup menu.
677 * Enable updating the status lines. */
678 pum_do_redraw = TRUE;
679 update_screen(0);
680 pum_do_redraw = FALSE;
681
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000682 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100683 {
684 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000685 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100686 --no_u_sync;
687 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000688
689 /* May need to update the screen again when there are
690 * autocommands involved. */
691 pum_do_redraw = TRUE;
692 update_screen(0);
693 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000694 }
695 }
696 }
697 }
698#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000699 }
700
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000701 if (!resized)
702 pum_redraw();
703
704 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000705}
706
707/*
708 * Undisplay the popup menu (later).
709 */
710 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100711pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000712{
713 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000714 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000715#ifdef FEAT_WINDOWS
716 redraw_tabline = TRUE;
717#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000718 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000719}
720
721/*
722 * Clear the popup menu. Currently only resets the offset to the first
723 * displayed item.
724 */
725 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100726pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000727{
728 pum_first = 0;
729}
730
731/*
732 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000733 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000734 */
735 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100736pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000737{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000738 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000739}
740
Bram Moolenaare3226be2005-12-18 22:10:00 +0000741/*
742 * Return the height of the popup menu, the number of entries visible.
743 * Only valid when pum_visible() returns TRUE!
744 */
745 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100746pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000747{
748 return pum_height;
749}
750
Bram Moolenaar4b779472005-10-04 09:12:31 +0000751#endif