blob: 47cda947cf665f7b7821fe7e56551d6a91be8cd3 [file] [log] [blame]
Bram Moolenaar4b779472005-10-04 09:12:31 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
78 if (firstwin->w_p_pvw)
79 top_clear = firstwin->w_height;
80 else
81 top_clear = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000082
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000083 /* When the preview window is at the bottom stop just above it. Also
84 * avoid drawing over the status line so that it's clear there is a window
85 * boundary. */
86 if (lastwin->w_p_pvw)
87 above_row -= lastwin->w_height + lastwin->w_status_height + 1;
88
Bram Moolenaar4b779472005-10-04 09:12:31 +000089 /*
90 * Figure out the size and position of the pum.
91 */
92 if (size < PUM_DEF_HEIGHT)
93 pum_height = size;
94 else
95 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +000096 if (p_ph > 0 && pum_height > p_ph)
97 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +000098
99 /* Put the pum below "row" if possible. If there are few lines decide on
100 * where there is more room. */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000101 if (row + 2 >= above_row - pum_height
102 && row > (above_row - top_clear) / 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000103 {
104 /* pum above "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000105
106 /* Leave two lines of context if possible */
107 if (curwin->w_wrow - curwin->w_cline_row >= 2)
108 context_lines = 2;
109 else
110 context_lines = curwin->w_wrow - curwin->w_cline_row;
111
112 if (row >= size + context_lines)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000113 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000114 pum_row = row - size - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000115 pum_height = size;
116 }
117 else
118 {
119 pum_row = 0;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000120 pum_height = row - context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000121 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000122 if (p_ph > 0 && pum_height > p_ph)
123 {
124 pum_row += pum_height - p_ph;
125 pum_height = p_ph;
126 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000127 }
128 else
129 {
130 /* pum below "row" */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000131
132 /* Leave two lines of context if possible */
133 if (curwin->w_cline_row + curwin->w_cline_height - curwin->w_wrow >= 3)
134 context_lines = 3;
135 else
136 context_lines = curwin->w_cline_row
137 + curwin->w_cline_height - curwin->w_wrow;
138
139 pum_row = row + context_lines;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000140 if (size > above_row - pum_row)
141 pum_height = above_row - pum_row;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000142 else
143 pum_height = size;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000144 if (p_ph > 0 && pum_height > p_ph)
145 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000146 }
147
148 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000149 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000150 return;
151
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000152 /* If there is a preview window at the top avoid drawing over it. */
153 if (firstwin->w_p_pvw
154 && pum_row < firstwin->w_height
155 && pum_height > firstwin->w_height + 4)
156 {
157 pum_row += firstwin->w_height;
158 pum_height -= firstwin->w_height;
159 }
160
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000161 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000162 for (i = 0; i < size; ++i)
163 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000164 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000165 if (max_width < w)
166 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000167 if (array[i].pum_kind != NULL)
168 {
169 w = vim_strsize(array[i].pum_kind) + 1;
170 if (kind_width < w)
171 kind_width = w;
172 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000173 if (array[i].pum_extra != NULL)
174 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000175 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000176 if (extra_width < w)
177 extra_width = w;
178 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000179 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000180 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000181 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000182
Bram Moolenaarabc97732007-08-08 20:49:37 +0000183 /* Calculate column */
184#ifdef FEAT_RIGHTLEFT
185 if (curwin->w_p_rl)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000186 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000187 else
188#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +0000189 col = W_WINCOL(curwin) + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000190
Bram Moolenaar4b779472005-10-04 09:12:31 +0000191 /* if there are more items than room we need a scrollbar */
192 if (pum_height < size)
193 {
194 pum_scrollbar = 1;
195 ++max_width;
196 }
197 else
198 pum_scrollbar = 0;
199
200 if (def_width < max_width)
201 def_width = max_width;
202
Bram Moolenaarabc97732007-08-08 20:49:37 +0000203 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
204#ifdef FEAT_RIGHTLEFT
205 && !curwin->w_p_rl)
206 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
207#endif
208 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000209 {
210 /* align pum column with "col" */
211 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000212
213#ifdef FEAT_RIGHTLEFT
214 if (curwin->w_p_rl)
215 pum_width = pum_col - pum_scrollbar + 1;
216 else
217#endif
218 pum_width = Columns - pum_col - pum_scrollbar;
219
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000220 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000221 && pum_width > PUM_DEF_WIDTH)
222 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000223 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000224 if (pum_width < PUM_DEF_WIDTH)
225 pum_width = PUM_DEF_WIDTH;
226 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000227 }
228 else if (Columns < def_width)
229 {
230 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000231#ifdef FEAT_RIGHTLEFT
232 if (curwin->w_p_rl)
233 pum_col = Columns - 1;
234 else
235#endif
236 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000237 pum_width = Columns - 1;
238 }
239 else
240 {
241 if (max_width > PUM_DEF_WIDTH)
242 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000243#ifdef FEAT_RIGHTLEFT
244 if (curwin->w_p_rl)
245 pum_col = max_width - 1;
246 else
247#endif
248 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000249 pum_width = max_width - pum_scrollbar;
250 }
251
252 pum_array = array;
253 pum_size = size;
254
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000255 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000256 * the positioning. Limit this to two times, when there is not much
257 * room the window size will keep changing. */
258 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000259 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000260}
261
262/*
263 * Redraw the popup menu, using "pum_first" and "pum_selected".
264 */
265 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100266pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000267{
268 int row = pum_row;
269 int col;
270 int attr_norm = highlight_attr[HLF_PNI];
271 int attr_select = highlight_attr[HLF_PSI];
272 int attr_scroll = highlight_attr[HLF_PSB];
273 int attr_thumb = highlight_attr[HLF_PST];
274 int attr;
275 int i;
276 int idx;
277 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000278 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000279 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000280 int thumb_pos = 0;
281 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000282 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000283 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000284
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100285 /* Never display more than we have */
286 if (pum_first > pum_size - pum_height)
287 pum_first = pum_size - pum_height;
288
Bram Moolenaar4b779472005-10-04 09:12:31 +0000289 if (pum_scrollbar)
290 {
291 thumb_heigth = pum_height * pum_height / pum_size;
292 if (thumb_heigth == 0)
293 thumb_heigth = 1;
294 thumb_pos = (pum_first * (pum_height - thumb_heigth)
295 + (pum_size - pum_height) / 2)
296 / (pum_size - pum_height);
297 }
298
299 for (i = 0; i < pum_height; ++i)
300 {
301 idx = i + pum_first;
302 attr = (idx == pum_selected) ? attr_select : attr_norm;
303
304 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000305#ifdef FEAT_RIGHTLEFT
306 if (curwin->w_p_rl)
307 {
308 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
309 screen_putchar(' ', row, pum_col + 1, attr);
310 }
311 else
312#endif
313 if (pum_col > 0)
314 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000315
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000316 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000317 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000318 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000319 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000320 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000321 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000322 width = 0;
323 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000324 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000325 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000326 case 1: p = pum_array[idx].pum_text; break;
327 case 2: p = pum_array[idx].pum_kind; break;
328 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000329 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000330 if (p != NULL)
331 for ( ; ; mb_ptr_adv(p))
332 {
333 if (s == NULL)
334 s = p;
335 w = ptr2cells(p);
336 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
337 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000338 /* Display the text that fits or comes before a Tab.
339 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000340 char_u *st;
341 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000342
343 *p = NUL;
344 st = transstr(s);
345 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000346#ifdef FEAT_RIGHTLEFT
347 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000348 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000349 if (st != NULL)
350 {
351 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000352
353 if (rt != NULL)
354 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100355 char_u *rt_start = rt;
356 int size;
357
358 size = vim_strsize(rt);
359 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000360 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100361 do
362 {
363 size -= has_mbyte
364 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000365 mb_ptr_adv(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100366 } while (size > pum_width);
367
368 if (size < pum_width)
369 {
370 /* Most left character requires
371 * 2-cells but only 1 cell is
372 * available on screen. Put a
373 * '<' on the left of the pum
374 * item */
375 *(--rt) = '<';
376 size++;
377 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000378 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100379 screen_puts_len(rt, (int)STRLEN(rt),
380 row, col - size + 1, attr);
381 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000382 }
383 vim_free(st);
384 }
385 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000386 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000387 else
388#endif
389 {
390 if (st != NULL)
391 {
392 screen_puts_len(st, (int)STRLEN(st), row, col,
393 attr);
394 vim_free(st);
395 }
396 col += width;
397 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000398
399 if (*p != TAB)
400 break;
401
402 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000403#ifdef FEAT_RIGHTLEFT
404 if (curwin->w_p_rl)
405 {
406 screen_puts_len((char_u *)" ", 2, row, col - 1,
407 attr);
408 col -= 2;
409 }
410 else
411#endif
412 {
413 screen_puts_len((char_u *)" ", 2, row, col, attr);
414 col += 2;
415 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000416 totwidth += 2;
417 s = NULL; /* start text at next char */
418 width = 0;
419 }
420 else
421 width += w;
422 }
423
424 if (round > 1)
425 n = pum_kind_width + 1;
426 else
427 n = 1;
428
429 /* Stop when there is nothing more to display. */
430 if (round == 3
431 || (round == 2 && pum_array[idx].pum_extra == NULL)
432 || (round == 1 && pum_array[idx].pum_kind == NULL
433 && pum_array[idx].pum_extra == NULL)
434 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000435 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000436#ifdef FEAT_RIGHTLEFT
437 if (curwin->w_p_rl)
438 {
439 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
440 col + 1, ' ', ' ', attr);
441 col = pum_col - pum_base_width - n + 1;
442 }
443 else
444#endif
445 {
446 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000447 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000448 col = pum_col + pum_base_width + n;
449 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000450 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000451 }
452
Bram Moolenaarabc97732007-08-08 20:49:37 +0000453#ifdef FEAT_RIGHTLEFT
454 if (curwin->w_p_rl)
455 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
456 ' ', attr);
457 else
458#endif
459 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
460 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000461 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000462 {
463#ifdef FEAT_RIGHTLEFT
464 if (curwin->w_p_rl)
465 screen_putchar(' ', row, pum_col - pum_width,
466 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000467 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000468 else
469#endif
470 screen_putchar(' ', row, pum_col + pum_width,
471 i >= thumb_pos && i < thumb_pos + thumb_heigth
472 ? attr_thumb : attr_scroll);
473 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000474
475 ++row;
476 }
477}
478
Bram Moolenaar4b779472005-10-04 09:12:31 +0000479/*
480 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000481 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000482 * This may be repeated when the preview window is used:
483 * "repeat" == 0: open preview window normally
484 * "repeat" == 1: open preview window but don't set the size
485 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000486 * Returns TRUE when the window was resized and the location of the popup menu
487 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000488 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000489 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100490pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000491{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000492 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000493 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000494
Bram Moolenaar4b779472005-10-04 09:12:31 +0000495 pum_selected = n;
496
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000497 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000498 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000499 if (pum_first > pum_selected - 4)
500 {
501 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000502 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000503 if (pum_first > pum_selected - 2)
504 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000505 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000506 if (pum_first < 0)
507 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000508 else if (pum_first > pum_selected)
509 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000510 }
511 else
512 pum_first = pum_selected;
513 }
514 else if (pum_first < pum_selected - pum_height + 5)
515 {
516 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000517 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000518 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000519 {
520 pum_first += pum_height - 2;
521 if (pum_first < pum_selected - pum_height + 1)
522 pum_first = pum_selected - pum_height + 1;
523 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000524 else
525 pum_first = pum_selected - pum_height + 1;
526 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000527
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000528 /* Give a few lines of context when possible. */
529 if (context > 3)
530 context = 3;
531 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000532 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000533 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000534 {
535 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000536 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000537 if (pum_first < 0)
538 pum_first = 0;
539 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000540 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000541 {
542 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000543 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000544 }
545 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000546
547#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000548 /*
549 * Show extra info in the preview window if there is something and
550 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000551 * Skip this when tried twice already.
552 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000553 * NOTE: Be very careful not to sync undo!
554 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000555 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000556 && Rows > 10
557 && repeat <= 1
558 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000559 {
560 win_T *curwin_save = curwin;
561 int res = OK;
562
Bram Moolenaaree236d02010-10-27 17:11:15 +0200563 /* Open a preview window. 3 lines by default. Prefer
564 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000565 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200566 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
567 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200568 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100569 /* Prevent undo sync here, if an autocommand syncs undo weird
570 * things can happen to the undo tree. */
571 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000572 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100573 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200574 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000575 g_do_tagpreview = 0;
576
577 if (curwin->w_p_pvw)
578 {
579 if (curbuf->b_fname == NULL
580 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
581 && curbuf->b_p_bh[0] == 'w')
582 {
583 /* Already a "wipeout" buffer, make it empty. */
584 while (!bufempty())
585 ml_delete((linenr_T)1, FALSE);
586 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000587 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000588 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000589 /* Don't want to sync undo in the current buffer. */
590 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000591 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000592 --no_u_sync;
593 if (res == OK)
594 {
595 /* Edit a new, empty buffer. Set options for a "wipeout"
596 * buffer. */
597 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
598 set_option_value((char_u *)"bt", 0L,
599 (char_u *)"nofile", OPT_LOCAL);
600 set_option_value((char_u *)"bh", 0L,
601 (char_u *)"wipe", OPT_LOCAL);
602 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000603 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000604 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000605 }
606 if (res == OK)
607 {
608 char_u *p, *e;
609 linenr_T lnum = 0;
610
611 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
612 {
613 e = vim_strchr(p, '\n');
614 if (e == NULL)
615 {
616 ml_append(lnum++, p, 0, FALSE);
617 break;
618 }
619 else
620 {
621 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000622 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000623 *e = '\n';
624 p = e + 1;
625 }
626 }
627
628 /* Increase the height of the preview window to show the
629 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000630 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000631 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000632 if (lnum > p_pvh)
633 lnum = p_pvh;
634 if (curwin->w_height < lnum)
635 {
636 win_setheight((int)lnum);
637 resized = TRUE;
638 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000639 }
640
641 curbuf->b_changed = 0;
642 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100643 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000644 curwin->w_cursor.col = 0;
645
646 if (curwin != curwin_save && win_valid(curwin_save))
647 {
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200648 /* When the first completion is done and the preview
649 * window is not resized, skip the preview window's
650 * status line redrawing. */
651 if (ins_compl_active() && !resized)
652 curwin->w_redr_status = FALSE;
653
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000654 /* Return cursor to where we were */
655 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000656 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000657
658 /* When the preview window was resized we need to
659 * update the view on the buffer. Only go back to
660 * the window when needed, otherwise it will always be
661 * redraw. */
662 if (resized)
663 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100664 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000665 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100666 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000667 update_topline();
668 }
669
670 /* Update the screen before drawing the popup menu.
671 * Enable updating the status lines. */
672 pum_do_redraw = TRUE;
673 update_screen(0);
674 pum_do_redraw = FALSE;
675
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000676 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100677 {
678 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000679 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100680 --no_u_sync;
681 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000682
683 /* May need to update the screen again when there are
684 * autocommands involved. */
685 pum_do_redraw = TRUE;
686 update_screen(0);
687 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000688 }
689 }
690 }
691 }
692#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000693 }
694
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000695 if (!resized)
696 pum_redraw();
697
698 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000699}
700
701/*
702 * Undisplay the popup menu (later).
703 */
704 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100705pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000706{
707 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000708 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000709#ifdef FEAT_WINDOWS
710 redraw_tabline = TRUE;
711#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000712 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000713}
714
715/*
716 * Clear the popup menu. Currently only resets the offset to the first
717 * displayed item.
718 */
719 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100720pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000721{
722 pum_first = 0;
723}
724
725/*
726 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000727 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000728 */
729 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100730pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000731{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000732 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000733}
734
Bram Moolenaare3226be2005-12-18 22:10:00 +0000735/*
736 * Return the height of the popup menu, the number of entries visible.
737 * Only valid when pum_visible() returns TRUE!
738 */
739 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100740pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000741{
742 return pum_height;
743}
744
Bram Moolenaar4b779472005-10-04 09:12:31 +0000745#endif