blob: 44ed878045d0808cc2f7a38b191c7391d5e420d0 [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 Moolenaarfc1421e2006-04-20 22:17:20 +000033static int pum_set_selected __ARGS((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 Moolenaar96d2c5b2006-03-11 21:27:59 +000045pum_display(array, size, selected)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000046 pumitem_T *array;
Bram Moolenaar4b779472005-10-04 09:12:31 +000047 int size;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000048 int selected; /* index of initially selected item, none if
49 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
266pum_redraw()
267{
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
285 if (pum_scrollbar)
286 {
287 thumb_heigth = pum_height * pum_height / pum_size;
288 if (thumb_heigth == 0)
289 thumb_heigth = 1;
290 thumb_pos = (pum_first * (pum_height - thumb_heigth)
291 + (pum_size - pum_height) / 2)
292 / (pum_size - pum_height);
293 }
294
295 for (i = 0; i < pum_height; ++i)
296 {
297 idx = i + pum_first;
298 attr = (idx == pum_selected) ? attr_select : attr_norm;
299
300 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000301#ifdef FEAT_RIGHTLEFT
302 if (curwin->w_p_rl)
303 {
304 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
305 screen_putchar(' ', row, pum_col + 1, attr);
306 }
307 else
308#endif
309 if (pum_col > 0)
310 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000311
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000312 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000313 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000314 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000315 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000316 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000317 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000318 width = 0;
319 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000320 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000321 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000322 case 1: p = pum_array[idx].pum_text; break;
323 case 2: p = pum_array[idx].pum_kind; break;
324 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000325 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000326 if (p != NULL)
327 for ( ; ; mb_ptr_adv(p))
328 {
329 if (s == NULL)
330 s = p;
331 w = ptr2cells(p);
332 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
333 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000334 /* Display the text that fits or comes before a Tab.
335 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000336 char_u *st;
337 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000338
339 *p = NUL;
340 st = transstr(s);
341 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000342#ifdef FEAT_RIGHTLEFT
343 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000344 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000345 if (st != NULL)
346 {
347 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000348
349 if (rt != NULL)
350 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100351 char_u *rt_start = rt;
352 int size;
353
354 size = vim_strsize(rt);
355 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000356 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100357 do
358 {
359 size -= has_mbyte
360 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000361 mb_ptr_adv(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100362 } while (size > pum_width);
363
364 if (size < pum_width)
365 {
366 /* Most left character requires
367 * 2-cells but only 1 cell is
368 * available on screen. Put a
369 * '<' on the left of the pum
370 * item */
371 *(--rt) = '<';
372 size++;
373 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000374 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100375 screen_puts_len(rt, (int)STRLEN(rt),
376 row, col - size + 1, attr);
377 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000378 }
379 vim_free(st);
380 }
381 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000382 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000383 else
384#endif
385 {
386 if (st != NULL)
387 {
388 screen_puts_len(st, (int)STRLEN(st), row, col,
389 attr);
390 vim_free(st);
391 }
392 col += width;
393 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000394
395 if (*p != TAB)
396 break;
397
398 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000399#ifdef FEAT_RIGHTLEFT
400 if (curwin->w_p_rl)
401 {
402 screen_puts_len((char_u *)" ", 2, row, col - 1,
403 attr);
404 col -= 2;
405 }
406 else
407#endif
408 {
409 screen_puts_len((char_u *)" ", 2, row, col, attr);
410 col += 2;
411 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000412 totwidth += 2;
413 s = NULL; /* start text at next char */
414 width = 0;
415 }
416 else
417 width += w;
418 }
419
420 if (round > 1)
421 n = pum_kind_width + 1;
422 else
423 n = 1;
424
425 /* Stop when there is nothing more to display. */
426 if (round == 3
427 || (round == 2 && pum_array[idx].pum_extra == NULL)
428 || (round == 1 && pum_array[idx].pum_kind == NULL
429 && pum_array[idx].pum_extra == NULL)
430 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000431 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000432#ifdef FEAT_RIGHTLEFT
433 if (curwin->w_p_rl)
434 {
435 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
436 col + 1, ' ', ' ', attr);
437 col = pum_col - pum_base_width - n + 1;
438 }
439 else
440#endif
441 {
442 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000443 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444 col = pum_col + pum_base_width + n;
445 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000446 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000447 }
448
Bram Moolenaarabc97732007-08-08 20:49:37 +0000449#ifdef FEAT_RIGHTLEFT
450 if (curwin->w_p_rl)
451 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
452 ' ', attr);
453 else
454#endif
455 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
456 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000457 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000458 {
459#ifdef FEAT_RIGHTLEFT
460 if (curwin->w_p_rl)
461 screen_putchar(' ', row, pum_col - pum_width,
462 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000463 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000464 else
465#endif
466 screen_putchar(' ', row, pum_col + pum_width,
467 i >= thumb_pos && i < thumb_pos + thumb_heigth
468 ? attr_thumb : attr_scroll);
469 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000470
471 ++row;
472 }
473}
474
Bram Moolenaar4b779472005-10-04 09:12:31 +0000475/*
476 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000477 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000478 * This may be repeated when the preview window is used:
479 * "repeat" == 0: open preview window normally
480 * "repeat" == 1: open preview window but don't set the size
481 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000482 * Returns TRUE when the window was resized and the location of the popup menu
483 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000484 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000485 static int
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000486pum_set_selected(n, repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000487 int n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000488 int repeat;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000489{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000490 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000491 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000492
Bram Moolenaar4b779472005-10-04 09:12:31 +0000493 pum_selected = n;
494
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000495 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000496 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000497 if (pum_first > pum_selected - 4)
498 {
499 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000500 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000501 if (pum_first > pum_selected - 2)
502 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000503 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000504 if (pum_first < 0)
505 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000506 else if (pum_first > pum_selected)
507 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000508 }
509 else
510 pum_first = pum_selected;
511 }
512 else if (pum_first < pum_selected - pum_height + 5)
513 {
514 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000515 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000516 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000517 {
518 pum_first += pum_height - 2;
519 if (pum_first < pum_selected - pum_height + 1)
520 pum_first = pum_selected - pum_height + 1;
521 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000522 else
523 pum_first = pum_selected - pum_height + 1;
524 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000525
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000526 /* Give a few lines of context when possible. */
527 if (context > 3)
528 context = 3;
529 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000530 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000531 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000532 {
533 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000534 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000535 if (pum_first < 0)
536 pum_first = 0;
537 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000538 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539 {
540 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000541 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000542 }
543 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000544
545#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000546 /*
547 * Show extra info in the preview window if there is something and
548 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000549 * Skip this when tried twice already.
550 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000551 * NOTE: Be very careful not to sync undo!
552 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000553 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000554 && Rows > 10
555 && repeat <= 1
556 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000557 {
558 win_T *curwin_save = curwin;
559 int res = OK;
560
Bram Moolenaaree236d02010-10-27 17:11:15 +0200561 /* Open a preview window. 3 lines by default. Prefer
562 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000563 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200564 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
565 g_do_tagpreview = p_pvh;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000566 resized = prepare_tagpreview(FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 g_do_tagpreview = 0;
568
569 if (curwin->w_p_pvw)
570 {
571 if (curbuf->b_fname == NULL
572 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
573 && curbuf->b_p_bh[0] == 'w')
574 {
575 /* Already a "wipeout" buffer, make it empty. */
576 while (!bufempty())
577 ml_delete((linenr_T)1, FALSE);
578 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000579 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000580 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000581 /* Don't want to sync undo in the current buffer. */
582 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000583 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000584 --no_u_sync;
585 if (res == OK)
586 {
587 /* Edit a new, empty buffer. Set options for a "wipeout"
588 * buffer. */
589 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
590 set_option_value((char_u *)"bt", 0L,
591 (char_u *)"nofile", OPT_LOCAL);
592 set_option_value((char_u *)"bh", 0L,
593 (char_u *)"wipe", OPT_LOCAL);
594 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000595 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000596 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000597 }
598 if (res == OK)
599 {
600 char_u *p, *e;
601 linenr_T lnum = 0;
602
603 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
604 {
605 e = vim_strchr(p, '\n');
606 if (e == NULL)
607 {
608 ml_append(lnum++, p, 0, FALSE);
609 break;
610 }
611 else
612 {
613 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000614 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000615 *e = '\n';
616 p = e + 1;
617 }
618 }
619
620 /* Increase the height of the preview window to show the
621 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000622 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000623 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000624 if (lnum > p_pvh)
625 lnum = p_pvh;
626 if (curwin->w_height < lnum)
627 {
628 win_setheight((int)lnum);
629 resized = TRUE;
630 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000631 }
632
633 curbuf->b_changed = 0;
634 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100635 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000636 curwin->w_cursor.col = 0;
637
638 if (curwin != curwin_save && win_valid(curwin_save))
639 {
640 /* Return cursor to where we were */
641 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000642 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000643
644 /* When the preview window was resized we need to
645 * update the view on the buffer. Only go back to
646 * the window when needed, otherwise it will always be
647 * redraw. */
648 if (resized)
649 {
650 win_enter(curwin_save, TRUE);
651 update_topline();
652 }
653
654 /* Update the screen before drawing the popup menu.
655 * Enable updating the status lines. */
656 pum_do_redraw = TRUE;
657 update_screen(0);
658 pum_do_redraw = FALSE;
659
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000660 if (!resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000661 win_enter(curwin_save, TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000662
663 /* May need to update the screen again when there are
664 * autocommands involved. */
665 pum_do_redraw = TRUE;
666 update_screen(0);
667 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000668 }
669 }
670 }
671 }
672#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000673 }
674
675 /* Never display more than we have */
676 if (pum_first > pum_size - pum_height)
677 pum_first = pum_size - pum_height;
678
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000679 if (!resized)
680 pum_redraw();
681
682 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000683}
684
685/*
686 * Undisplay the popup menu (later).
687 */
688 void
689pum_undisplay()
690{
691 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000692 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000693#ifdef FEAT_WINDOWS
694 redraw_tabline = TRUE;
695#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000696 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000697}
698
699/*
700 * Clear the popup menu. Currently only resets the offset to the first
701 * displayed item.
702 */
703 void
704pum_clear()
705{
706 pum_first = 0;
707}
708
709/*
710 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000711 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000712 */
713 int
714pum_visible()
715{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000716 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000717}
718
Bram Moolenaare3226be2005-12-18 22:10:00 +0000719/*
720 * Return the height of the popup menu, the number of entries visible.
721 * Only valid when pum_visible() returns TRUE!
722 */
723 int
724pum_get_height()
725{
726 return pum_height;
727}
728
Bram Moolenaar4b779472005-10-04 09:12:31 +0000729#endif