blob: b479b00457a9d5b63cd85e2daa66725596042969 [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;
57 int top_clear;
Bram Moolenaar4b779472005-10-04 09:12:31 +000058 int row;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000059 int context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +000060 int col;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000061 int above_row = cmdline_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000062 int redo_count = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000063
64redo:
65 def_width = PUM_DEF_WIDTH;
66 max_width = 0;
67 kind_width = 0;
68 extra_width = 0;
69
Bram Moolenaar1e607892006-03-13 22:15:53 +000070 /* Pretend the pum is already there to avoid that must_redraw is set when
71 * 'cuc' is on. */
72 pum_array = (pumitem_T *)1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000073 validate_cursor_col();
Bram Moolenaar1e607892006-03-13 22:15:53 +000074 pum_array = NULL;
75
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000076 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000077
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010078#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000079 if (firstwin->w_p_pvw)
80 top_clear = firstwin->w_height;
81 else
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010082#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000083 top_clear = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000084
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010085#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000086 /* When the preview window is at the bottom stop just above it. Also
87 * avoid drawing over the status line so that it's clear there is a window
88 * boundary. */
89 if (lastwin->w_p_pvw)
90 above_row -= lastwin->w_height + lastwin->w_status_height + 1;
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010091#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000092
Bram Moolenaar4b779472005-10-04 09:12:31 +000093 /*
94 * Figure out the size and position of the pum.
95 */
96 if (size < PUM_DEF_HEIGHT)
97 pum_height = size;
98 else
99 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000100 if (p_ph > 0 && pum_height > p_ph)
101 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000102
103 /* Put the pum below "row" if possible. If there are few lines decide on
104 * where there is more room. */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000105 if (row + 2 >= above_row - pum_height
106 && row > (above_row - top_clear) / 2)
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 Moolenaarefd2bf12006-03-16 21:41:35 +0000144 if (size > above_row - pum_row)
145 pum_height = above_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 Moolenaar96d2c5b2006-03-11 21:27:59 +0000157 /* If there is a preview window at the top avoid drawing over it. */
158 if (firstwin->w_p_pvw
159 && pum_row < firstwin->w_height
160 && pum_height > firstwin->w_height + 4)
161 {
162 pum_row += firstwin->w_height;
163 pum_height -= firstwin->w_height;
164 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100165#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000166
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000167 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000168 for (i = 0; i < size; ++i)
169 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000170 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000171 if (max_width < w)
172 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000173 if (array[i].pum_kind != NULL)
174 {
175 w = vim_strsize(array[i].pum_kind) + 1;
176 if (kind_width < w)
177 kind_width = w;
178 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000179 if (array[i].pum_extra != NULL)
180 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000181 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000182 if (extra_width < w)
183 extra_width = w;
184 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000185 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000186 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000187 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000188
Bram Moolenaarabc97732007-08-08 20:49:37 +0000189 /* Calculate column */
190#ifdef FEAT_RIGHTLEFT
191 if (curwin->w_p_rl)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000192 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000193 else
194#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000195 col = W_WINCOL(curwin) + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000196
Bram Moolenaar4b779472005-10-04 09:12:31 +0000197 /* if there are more items than room we need a scrollbar */
198 if (pum_height < size)
199 {
200 pum_scrollbar = 1;
201 ++max_width;
202 }
203 else
204 pum_scrollbar = 0;
205
206 if (def_width < max_width)
207 def_width = max_width;
208
Bram Moolenaarabc97732007-08-08 20:49:37 +0000209 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
210#ifdef FEAT_RIGHTLEFT
211 && !curwin->w_p_rl)
212 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
213#endif
214 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000215 {
216 /* align pum column with "col" */
217 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000218
219#ifdef FEAT_RIGHTLEFT
220 if (curwin->w_p_rl)
221 pum_width = pum_col - pum_scrollbar + 1;
222 else
223#endif
224 pum_width = Columns - pum_col - pum_scrollbar;
225
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000226 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000227 && pum_width > PUM_DEF_WIDTH)
228 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000229 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000230 if (pum_width < PUM_DEF_WIDTH)
231 pum_width = PUM_DEF_WIDTH;
232 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000233 }
234 else if (Columns < def_width)
235 {
236 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000237#ifdef FEAT_RIGHTLEFT
238 if (curwin->w_p_rl)
239 pum_col = Columns - 1;
240 else
241#endif
242 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000243 pum_width = Columns - 1;
244 }
245 else
246 {
247 if (max_width > PUM_DEF_WIDTH)
248 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000249#ifdef FEAT_RIGHTLEFT
250 if (curwin->w_p_rl)
251 pum_col = max_width - 1;
252 else
253#endif
254 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000255 pum_width = max_width - pum_scrollbar;
256 }
257
258 pum_array = array;
259 pum_size = size;
260
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000261 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000262 * the positioning. Limit this to two times, when there is not much
263 * room the window size will keep changing. */
264 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000265 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000266}
267
268/*
269 * Redraw the popup menu, using "pum_first" and "pum_selected".
270 */
271 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100272pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000273{
274 int row = pum_row;
275 int col;
276 int attr_norm = highlight_attr[HLF_PNI];
277 int attr_select = highlight_attr[HLF_PSI];
278 int attr_scroll = highlight_attr[HLF_PSB];
279 int attr_thumb = highlight_attr[HLF_PST];
280 int attr;
281 int i;
282 int idx;
283 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000284 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000285 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000286 int thumb_pos = 0;
287 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000288 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000289 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000290
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100291 /* Never display more than we have */
292 if (pum_first > pum_size - pum_height)
293 pum_first = pum_size - pum_height;
294
Bram Moolenaar4b779472005-10-04 09:12:31 +0000295 if (pum_scrollbar)
296 {
297 thumb_heigth = pum_height * pum_height / pum_size;
298 if (thumb_heigth == 0)
299 thumb_heigth = 1;
300 thumb_pos = (pum_first * (pum_height - thumb_heigth)
301 + (pum_size - pum_height) / 2)
302 / (pum_size - pum_height);
303 }
304
305 for (i = 0; i < pum_height; ++i)
306 {
307 idx = i + pum_first;
308 attr = (idx == pum_selected) ? attr_select : attr_norm;
309
310 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000311#ifdef FEAT_RIGHTLEFT
312 if (curwin->w_p_rl)
313 {
314 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
315 screen_putchar(' ', row, pum_col + 1, attr);
316 }
317 else
318#endif
319 if (pum_col > 0)
320 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000321
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000322 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000323 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000324 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000325 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000326 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000327 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000328 width = 0;
329 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000330 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000331 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000332 case 1: p = pum_array[idx].pum_text; break;
333 case 2: p = pum_array[idx].pum_kind; break;
334 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000335 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000336 if (p != NULL)
337 for ( ; ; mb_ptr_adv(p))
338 {
339 if (s == NULL)
340 s = p;
341 w = ptr2cells(p);
342 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
343 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000344 /* Display the text that fits or comes before a Tab.
345 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000346 char_u *st;
347 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000348
349 *p = NUL;
350 st = transstr(s);
351 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000352#ifdef FEAT_RIGHTLEFT
353 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000354 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000355 if (st != NULL)
356 {
357 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000358
359 if (rt != NULL)
360 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100361 char_u *rt_start = rt;
362 int size;
363
364 size = vim_strsize(rt);
365 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000366 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100367 do
368 {
369 size -= has_mbyte
370 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000371 mb_ptr_adv(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100372 } while (size > pum_width);
373
374 if (size < pum_width)
375 {
376 /* Most left character requires
377 * 2-cells but only 1 cell is
378 * available on screen. Put a
379 * '<' on the left of the pum
380 * item */
381 *(--rt) = '<';
382 size++;
383 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000384 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100385 screen_puts_len(rt, (int)STRLEN(rt),
386 row, col - size + 1, attr);
387 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000388 }
389 vim_free(st);
390 }
391 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000392 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000393 else
394#endif
395 {
396 if (st != NULL)
397 {
398 screen_puts_len(st, (int)STRLEN(st), row, col,
399 attr);
400 vim_free(st);
401 }
402 col += width;
403 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000404
405 if (*p != TAB)
406 break;
407
408 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000409#ifdef FEAT_RIGHTLEFT
410 if (curwin->w_p_rl)
411 {
412 screen_puts_len((char_u *)" ", 2, row, col - 1,
413 attr);
414 col -= 2;
415 }
416 else
417#endif
418 {
419 screen_puts_len((char_u *)" ", 2, row, col, attr);
420 col += 2;
421 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000422 totwidth += 2;
423 s = NULL; /* start text at next char */
424 width = 0;
425 }
426 else
427 width += w;
428 }
429
430 if (round > 1)
431 n = pum_kind_width + 1;
432 else
433 n = 1;
434
435 /* Stop when there is nothing more to display. */
436 if (round == 3
437 || (round == 2 && pum_array[idx].pum_extra == NULL)
438 || (round == 1 && pum_array[idx].pum_kind == NULL
439 && pum_array[idx].pum_extra == NULL)
440 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000441 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000442#ifdef FEAT_RIGHTLEFT
443 if (curwin->w_p_rl)
444 {
445 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
446 col + 1, ' ', ' ', attr);
447 col = pum_col - pum_base_width - n + 1;
448 }
449 else
450#endif
451 {
452 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000453 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000454 col = pum_col + pum_base_width + n;
455 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000456 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000457 }
458
Bram Moolenaarabc97732007-08-08 20:49:37 +0000459#ifdef FEAT_RIGHTLEFT
460 if (curwin->w_p_rl)
461 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
462 ' ', attr);
463 else
464#endif
465 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
466 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000467 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000468 {
469#ifdef FEAT_RIGHTLEFT
470 if (curwin->w_p_rl)
471 screen_putchar(' ', row, pum_col - pum_width,
472 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000473 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000474 else
475#endif
476 screen_putchar(' ', row, pum_col + pum_width,
477 i >= thumb_pos && i < thumb_pos + thumb_heigth
478 ? attr_thumb : attr_scroll);
479 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000480
481 ++row;
482 }
483}
484
Bram Moolenaar4b779472005-10-04 09:12:31 +0000485/*
486 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000487 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000488 * This may be repeated when the preview window is used:
489 * "repeat" == 0: open preview window normally
490 * "repeat" == 1: open preview window but don't set the size
491 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000492 * Returns TRUE when the window was resized and the location of the popup menu
493 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000494 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000495 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100496pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000497{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000498 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000499 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000500
Bram Moolenaar4b779472005-10-04 09:12:31 +0000501 pum_selected = n;
502
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000503 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000504 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000505 if (pum_first > pum_selected - 4)
506 {
507 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000508 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000509 if (pum_first > pum_selected - 2)
510 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000511 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000512 if (pum_first < 0)
513 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000514 else if (pum_first > pum_selected)
515 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000516 }
517 else
518 pum_first = pum_selected;
519 }
520 else if (pum_first < pum_selected - pum_height + 5)
521 {
522 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000523 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000524 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000525 {
526 pum_first += pum_height - 2;
527 if (pum_first < pum_selected - pum_height + 1)
528 pum_first = pum_selected - pum_height + 1;
529 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000530 else
531 pum_first = pum_selected - pum_height + 1;
532 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000533
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000534 /* Give a few lines of context when possible. */
535 if (context > 3)
536 context = 3;
537 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000538 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000539 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000540 {
541 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000542 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000543 if (pum_first < 0)
544 pum_first = 0;
545 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000546 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000547 {
548 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000549 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000550 }
551 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000552
553#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000554 /*
555 * Show extra info in the preview window if there is something and
556 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000557 * Skip this when tried twice already.
558 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000559 * NOTE: Be very careful not to sync undo!
560 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000561 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000562 && Rows > 10
563 && repeat <= 1
564 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000565 {
566 win_T *curwin_save = curwin;
567 int res = OK;
568
Bram Moolenaaree236d02010-10-27 17:11:15 +0200569 /* Open a preview window. 3 lines by default. Prefer
570 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000571 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200572 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
573 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200574 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100575 /* Prevent undo sync here, if an autocommand syncs undo weird
576 * things can happen to the undo tree. */
577 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000578 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100579 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200580 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000581 g_do_tagpreview = 0;
582
583 if (curwin->w_p_pvw)
584 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200585 if (!resized
586 && curbuf->b_nwindows == 1
587 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000588 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
589 && curbuf->b_p_bh[0] == 'w')
590 {
591 /* Already a "wipeout" buffer, make it empty. */
592 while (!bufempty())
593 ml_delete((linenr_T)1, FALSE);
594 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000595 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000596 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000597 /* Don't want to sync undo in the current buffer. */
598 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000599 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000600 --no_u_sync;
601 if (res == OK)
602 {
603 /* Edit a new, empty buffer. Set options for a "wipeout"
604 * buffer. */
605 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
606 set_option_value((char_u *)"bt", 0L,
607 (char_u *)"nofile", OPT_LOCAL);
608 set_option_value((char_u *)"bh", 0L,
609 (char_u *)"wipe", OPT_LOCAL);
610 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000611 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000612 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000613 }
614 if (res == OK)
615 {
616 char_u *p, *e;
617 linenr_T lnum = 0;
618
619 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
620 {
621 e = vim_strchr(p, '\n');
622 if (e == NULL)
623 {
624 ml_append(lnum++, p, 0, FALSE);
625 break;
626 }
627 else
628 {
629 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000630 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000631 *e = '\n';
632 p = e + 1;
633 }
634 }
635
636 /* Increase the height of the preview window to show the
637 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000638 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000639 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000640 if (lnum > p_pvh)
641 lnum = p_pvh;
642 if (curwin->w_height < lnum)
643 {
644 win_setheight((int)lnum);
645 resized = TRUE;
646 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000647 }
648
649 curbuf->b_changed = 0;
650 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100651 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000652 curwin->w_cursor.col = 0;
653
654 if (curwin != curwin_save && win_valid(curwin_save))
655 {
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200656 /* When the first completion is done and the preview
657 * window is not resized, skip the preview window's
658 * status line redrawing. */
659 if (ins_compl_active() && !resized)
660 curwin->w_redr_status = FALSE;
661
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000662 /* Return cursor to where we were */
663 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000664 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000665
666 /* When the preview window was resized we need to
667 * update the view on the buffer. Only go back to
668 * the window when needed, otherwise it will always be
669 * redraw. */
670 if (resized)
671 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100672 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000673 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100674 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000675 update_topline();
676 }
677
678 /* Update the screen before drawing the popup menu.
679 * Enable updating the status lines. */
680 pum_do_redraw = TRUE;
681 update_screen(0);
682 pum_do_redraw = FALSE;
683
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000684 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100685 {
686 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000687 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100688 --no_u_sync;
689 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000690
691 /* May need to update the screen again when there are
692 * autocommands involved. */
693 pum_do_redraw = TRUE;
694 update_screen(0);
695 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000696 }
697 }
698 }
699 }
700#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000701 }
702
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000703 if (!resized)
704 pum_redraw();
705
706 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000707}
708
709/*
710 * Undisplay the popup menu (later).
711 */
712 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100713pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000714{
715 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000716 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000717#ifdef FEAT_WINDOWS
718 redraw_tabline = TRUE;
719#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000720 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000721}
722
723/*
724 * Clear the popup menu. Currently only resets the offset to the first
725 * displayed item.
726 */
727 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100728pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000729{
730 pum_first = 0;
731}
732
733/*
734 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000735 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000736 */
737 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100738pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000739{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000740 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000741}
742
Bram Moolenaare3226be2005-12-18 22:10:00 +0000743/*
744 * Return the height of the popup menu, the number of entries visible.
745 * Only valid when pum_visible() returns TRUE!
746 */
747 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100748pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000749{
750 return pum_height;
751}
752
Bram Moolenaar4b779472005-10-04 09:12:31 +0000753#endif