blob: 418f0ca61ac0560ca3e46ea0910ef610e4d53b13 [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
Bram Moolenaara5e66212017-09-29 22:42:33 +020067 do
68 {
69 def_width = PUM_DEF_WIDTH;
70 max_width = 0;
71 kind_width = 0;
72 extra_width = 0;
73 above_row = 0;
74 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000075
Bram Moolenaara5e66212017-09-29 22:42:33 +020076 /* Pretend the pum is already there to avoid that must_redraw is set
77 * when 'cuc' is on. */
78 pum_array = (pumitem_T *)1;
79 validate_cursor_col();
80 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +000081
Bram Moolenaara5e66212017-09-29 22:42:33 +020082 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000083
Bram Moolenaar4033c552017-09-16 20:54:51 +020084#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +020085 FOR_ALL_WINDOWS(pvwin)
86 if (pvwin->w_p_pvw)
87 break;
88 if (pvwin != NULL)
89 {
90 if (W_WINROW(pvwin) < W_WINROW(curwin))
91 above_row = W_WINROW(pvwin) + pvwin->w_height;
92 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
93 below_row = W_WINROW(pvwin);
94 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +010095#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000096
Bram Moolenaara5e66212017-09-29 22:42:33 +020097 /*
98 * Figure out the size and position of the pum.
99 */
100 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000101 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000102 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200103 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000104 if (p_ph > 0 && pum_height > p_ph)
105 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000106
Bram Moolenaara5e66212017-09-29 22:42:33 +0200107 /* Put the pum below "row" if possible. If there are few lines decide
108 * on where there is more room. */
109 if (row + 2 >= below_row - pum_height
110 && row - above_row > (below_row - above_row) / 2)
111 {
112 /* pum above "row" */
113
114 /* Leave two lines of context if possible */
115 if (curwin->w_wrow - curwin->w_cline_row >= 2)
116 context_lines = 2;
117 else
118 context_lines = curwin->w_wrow - curwin->w_cline_row;
119
120 if (row >= size + context_lines)
121 {
122 pum_row = row - size - context_lines;
123 pum_height = size;
124 }
125 else
126 {
127 pum_row = 0;
128 pum_height = row - context_lines;
129 }
130 if (p_ph > 0 && pum_height > p_ph)
131 {
132 pum_row += pum_height - p_ph;
133 pum_height = p_ph;
134 }
135 }
136 else
137 {
138 /* pum below "row" */
139
140 /* Leave two lines of context if possible */
141 if (curwin->w_cline_row
142 + curwin->w_cline_height - curwin->w_wrow >= 3)
143 context_lines = 3;
144 else
145 context_lines = curwin->w_cline_row
146 + curwin->w_cline_height - curwin->w_wrow;
147
148 pum_row = row + context_lines;
149 if (size > below_row - pum_row)
150 pum_height = below_row - pum_row;
151 else
152 pum_height = size;
153 if (p_ph > 0 && pum_height > p_ph)
154 pum_height = p_ph;
155 }
156
157 /* don't display when we only have room for one line */
158 if (pum_height < 1 || (pum_height == 1 && size > 1))
159 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000160
Bram Moolenaar4033c552017-09-16 20:54:51 +0200161#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200162 /* If there is a preview window at the above avoid drawing over it. */
163 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000164 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200165 pum_row += above_row;
166 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000167 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200168#endif
169
170 /* Compute the width of the widest match and the widest extra. */
171 for (i = 0; i < size; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200173 w = vim_strsize(array[i].pum_text);
174 if (max_width < w)
175 max_width = w;
176 if (array[i].pum_kind != NULL)
177 {
178 w = vim_strsize(array[i].pum_kind) + 1;
179 if (kind_width < w)
180 kind_width = w;
181 }
182 if (array[i].pum_extra != NULL)
183 {
184 w = vim_strsize(array[i].pum_extra) + 1;
185 if (extra_width < w)
186 extra_width = w;
187 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000188 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200189 pum_base_width = max_width;
190 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000191
Bram Moolenaara5e66212017-09-29 22:42:33 +0200192 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000193#ifdef FEAT_RIGHTLEFT
194 if (curwin->w_p_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195 col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000196 else
197#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200198 col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000199
Bram Moolenaara5e66212017-09-29 22:42:33 +0200200 /* if there are more items than room we need a scrollbar */
201 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000202 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200203 pum_scrollbar = 1;
204 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000205 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000206 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200207 pum_scrollbar = 0;
208
209 if (def_width < max_width)
210 def_width = max_width;
211
212 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000213#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214 && !curwin->w_p_rl)
215 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000216#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200217 ))
218 {
219 /* align pum column with "col" */
220 pum_col = col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000221
Bram Moolenaara5e66212017-09-29 22:42:33 +0200222#ifdef FEAT_RIGHTLEFT
223 if (curwin->w_p_rl)
224 pum_width = pum_col - pum_scrollbar + 1;
225 else
226#endif
227 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000228
Bram Moolenaara5e66212017-09-29 22:42:33 +0200229 if (pum_width > max_width + kind_width + extra_width + 1
230 && pum_width > PUM_DEF_WIDTH)
231 {
232 pum_width = max_width + kind_width + extra_width + 1;
233 if (pum_width < PUM_DEF_WIDTH)
234 pum_width = PUM_DEF_WIDTH;
235 }
236 }
237 else if (Columns < def_width)
238 {
239 /* not enough room, will use what we have */
240#ifdef FEAT_RIGHTLEFT
241 if (curwin->w_p_rl)
242 pum_col = Columns - 1;
243 else
244#endif
245 pum_col = 0;
246 pum_width = Columns - 1;
247 }
248 else
249 {
250 if (max_width > PUM_DEF_WIDTH)
251 max_width = PUM_DEF_WIDTH; /* truncate */
252#ifdef FEAT_RIGHTLEFT
253 if (curwin->w_p_rl)
254 pum_col = max_width - 1;
255 else
256#endif
257 pum_col = Columns - max_width;
258 pum_width = max_width - pum_scrollbar;
259 }
260
261 pum_array = array;
262 pum_size = size;
263
264 /* Set selected item and redraw. If the window size changed need to
265 * redo the positioning. Limit this to two times, when there is not
266 * much room the window size will keep changing. */
267 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000268}
269
270/*
271 * Redraw the popup menu, using "pum_first" and "pum_selected".
272 */
273 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100274pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000275{
276 int row = pum_row;
277 int col;
278 int attr_norm = highlight_attr[HLF_PNI];
279 int attr_select = highlight_attr[HLF_PSI];
280 int attr_scroll = highlight_attr[HLF_PSB];
281 int attr_thumb = highlight_attr[HLF_PST];
282 int attr;
283 int i;
284 int idx;
285 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000286 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000287 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000288 int thumb_pos = 0;
289 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000290 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000291 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000292
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100293 /* Never display more than we have */
294 if (pum_first > pum_size - pum_height)
295 pum_first = pum_size - pum_height;
296
Bram Moolenaar4b779472005-10-04 09:12:31 +0000297 if (pum_scrollbar)
298 {
299 thumb_heigth = pum_height * pum_height / pum_size;
300 if (thumb_heigth == 0)
301 thumb_heigth = 1;
302 thumb_pos = (pum_first * (pum_height - thumb_heigth)
303 + (pum_size - pum_height) / 2)
304 / (pum_size - pum_height);
305 }
306
307 for (i = 0; i < pum_height; ++i)
308 {
309 idx = i + pum_first;
310 attr = (idx == pum_selected) ? attr_select : attr_norm;
311
312 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000313#ifdef FEAT_RIGHTLEFT
314 if (curwin->w_p_rl)
315 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200316 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000317 screen_putchar(' ', row, pum_col + 1, attr);
318 }
319 else
320#endif
321 if (pum_col > 0)
322 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000323
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000324 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000325 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000326 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000327 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000328 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000329 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000330 width = 0;
331 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000332 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000333 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000334 case 1: p = pum_array[idx].pum_text; break;
335 case 2: p = pum_array[idx].pum_kind; break;
336 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000337 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000338 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100339 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000340 {
341 if (s == NULL)
342 s = p;
343 w = ptr2cells(p);
344 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
345 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000346 /* Display the text that fits or comes before a Tab.
347 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000348 char_u *st;
349 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000350
351 *p = NUL;
352 st = transstr(s);
353 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000354#ifdef FEAT_RIGHTLEFT
355 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000356 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000357 if (st != NULL)
358 {
359 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000360
361 if (rt != NULL)
362 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100363 char_u *rt_start = rt;
364 int size;
365
366 size = vim_strsize(rt);
367 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000368 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100369 do
370 {
371 size -= has_mbyte
372 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100373 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100374 } while (size > pum_width);
375
376 if (size < pum_width)
377 {
378 /* Most left character requires
379 * 2-cells but only 1 cell is
380 * available on screen. Put a
381 * '<' on the left of the pum
382 * item */
383 *(--rt) = '<';
384 size++;
385 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000386 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100387 screen_puts_len(rt, (int)STRLEN(rt),
388 row, col - size + 1, attr);
389 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000390 }
391 vim_free(st);
392 }
393 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000394 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000395 else
396#endif
397 {
398 if (st != NULL)
399 {
400 screen_puts_len(st, (int)STRLEN(st), row, col,
401 attr);
402 vim_free(st);
403 }
404 col += width;
405 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000406
407 if (*p != TAB)
408 break;
409
410 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000411#ifdef FEAT_RIGHTLEFT
412 if (curwin->w_p_rl)
413 {
414 screen_puts_len((char_u *)" ", 2, row, col - 1,
415 attr);
416 col -= 2;
417 }
418 else
419#endif
420 {
421 screen_puts_len((char_u *)" ", 2, row, col, attr);
422 col += 2;
423 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000424 totwidth += 2;
425 s = NULL; /* start text at next char */
426 width = 0;
427 }
428 else
429 width += w;
430 }
431
432 if (round > 1)
433 n = pum_kind_width + 1;
434 else
435 n = 1;
436
437 /* Stop when there is nothing more to display. */
438 if (round == 3
439 || (round == 2 && pum_array[idx].pum_extra == NULL)
440 || (round == 1 && pum_array[idx].pum_kind == NULL
441 && pum_array[idx].pum_extra == NULL)
442 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000443 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444#ifdef FEAT_RIGHTLEFT
445 if (curwin->w_p_rl)
446 {
447 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
448 col + 1, ' ', ' ', attr);
449 col = pum_col - pum_base_width - n + 1;
450 }
451 else
452#endif
453 {
454 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000455 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000456 col = pum_col + pum_base_width + n;
457 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000458 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000459 }
460
Bram Moolenaarabc97732007-08-08 20:49:37 +0000461#ifdef FEAT_RIGHTLEFT
462 if (curwin->w_p_rl)
463 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
464 ' ', attr);
465 else
466#endif
467 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
468 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000469 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000470 {
471#ifdef FEAT_RIGHTLEFT
472 if (curwin->w_p_rl)
473 screen_putchar(' ', row, pum_col - pum_width,
474 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000475 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000476 else
477#endif
478 screen_putchar(' ', row, pum_col + pum_width,
479 i >= thumb_pos && i < thumb_pos + thumb_heigth
480 ? attr_thumb : attr_scroll);
481 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000482
483 ++row;
484 }
485}
486
Bram Moolenaar4b779472005-10-04 09:12:31 +0000487/*
488 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000489 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000490 * This may be repeated when the preview window is used:
491 * "repeat" == 0: open preview window normally
492 * "repeat" == 1: open preview window but don't set the size
493 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000494 * Returns TRUE when the window was resized and the location of the popup menu
495 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000496 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000497 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100498pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000499{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000500 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000501 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000502
Bram Moolenaar4b779472005-10-04 09:12:31 +0000503 pum_selected = n;
504
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000505 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000506 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000507 if (pum_first > pum_selected - 4)
508 {
509 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000510 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000511 if (pum_first > pum_selected - 2)
512 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000513 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000514 if (pum_first < 0)
515 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000516 else if (pum_first > pum_selected)
517 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000518 }
519 else
520 pum_first = pum_selected;
521 }
522 else if (pum_first < pum_selected - pum_height + 5)
523 {
524 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000525 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000526 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000527 {
528 pum_first += pum_height - 2;
529 if (pum_first < pum_selected - pum_height + 1)
530 pum_first = pum_selected - pum_height + 1;
531 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000532 else
533 pum_first = pum_selected - pum_height + 1;
534 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000535
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000536 /* Give a few lines of context when possible. */
537 if (context > 3)
538 context = 3;
539 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000540 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000541 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000542 {
543 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000544 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000545 if (pum_first < 0)
546 pum_first = 0;
547 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000548 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000549 {
550 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000551 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000552 }
553 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000554
Bram Moolenaar4033c552017-09-16 20:54:51 +0200555#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000556 /*
557 * Show extra info in the preview window if there is something and
558 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000559 * Skip this when tried twice already.
560 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000561 * NOTE: Be very careful not to sync undo!
562 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000563 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000564 && Rows > 10
565 && repeat <= 1
566 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 {
568 win_T *curwin_save = curwin;
569 int res = OK;
570
Bram Moolenaaree236d02010-10-27 17:11:15 +0200571 /* Open a preview window. 3 lines by default. Prefer
572 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000573 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200574 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
575 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200576 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100577 /* Prevent undo sync here, if an autocommand syncs undo weird
578 * things can happen to the undo tree. */
579 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000580 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100581 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200582 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000583 g_do_tagpreview = 0;
584
585 if (curwin->w_p_pvw)
586 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200587 if (!resized
588 && curbuf->b_nwindows == 1
589 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000590 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
591 && curbuf->b_p_bh[0] == 'w')
592 {
593 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100594 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000595 ml_delete((linenr_T)1, FALSE);
596 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000597 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000598 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000599 /* Don't want to sync undo in the current buffer. */
600 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000601 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000602 --no_u_sync;
603 if (res == OK)
604 {
605 /* Edit a new, empty buffer. Set options for a "wipeout"
606 * buffer. */
607 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
608 set_option_value((char_u *)"bt", 0L,
609 (char_u *)"nofile", OPT_LOCAL);
610 set_option_value((char_u *)"bh", 0L,
611 (char_u *)"wipe", OPT_LOCAL);
612 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000613 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000614 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000615 }
616 if (res == OK)
617 {
618 char_u *p, *e;
619 linenr_T lnum = 0;
620
621 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
622 {
623 e = vim_strchr(p, '\n');
624 if (e == NULL)
625 {
626 ml_append(lnum++, p, 0, FALSE);
627 break;
628 }
629 else
630 {
631 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000632 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000633 *e = '\n';
634 p = e + 1;
635 }
636 }
637
638 /* Increase the height of the preview window to show the
639 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000640 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000641 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000642 if (lnum > p_pvh)
643 lnum = p_pvh;
644 if (curwin->w_height < lnum)
645 {
646 win_setheight((int)lnum);
647 resized = TRUE;
648 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000649 }
650
651 curbuf->b_changed = 0;
652 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100653 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000654 curwin->w_cursor.col = 0;
655
656 if (curwin != curwin_save && win_valid(curwin_save))
657 {
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200658 /* When the first completion is done and the preview
659 * window is not resized, skip the preview window's
660 * status line redrawing. */
661 if (ins_compl_active() && !resized)
662 curwin->w_redr_status = FALSE;
663
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000664 /* Return cursor to where we were */
665 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000666 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000667
668 /* When the preview window was resized we need to
669 * update the view on the buffer. Only go back to
670 * the window when needed, otherwise it will always be
671 * redraw. */
672 if (resized)
673 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100674 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000675 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100676 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000677 update_topline();
678 }
679
680 /* Update the screen before drawing the popup menu.
681 * Enable updating the status lines. */
682 pum_do_redraw = TRUE;
683 update_screen(0);
684 pum_do_redraw = FALSE;
685
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000686 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100687 {
688 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000689 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100690 --no_u_sync;
691 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000692
693 /* May need to update the screen again when there are
694 * autocommands involved. */
695 pum_do_redraw = TRUE;
696 update_screen(0);
697 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000698 }
699 }
700 }
701 }
702#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000703 }
704
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000705 if (!resized)
706 pum_redraw();
707
708 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000709}
710
711/*
712 * Undisplay the popup menu (later).
713 */
714 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100715pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000716{
717 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000718 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000719 redraw_tabline = TRUE;
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