blob: fcc7d851e1d9900fc8666dd78684459475999c00 [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)
186 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol -
187 curwin->w_leftcol - 1;
188 else
189#endif
190 col = W_WINCOL(curwin) + curwin->w_wcol - curwin->w_leftcol;
191
Bram Moolenaar4b779472005-10-04 09:12:31 +0000192 /* if there are more items than room we need a scrollbar */
193 if (pum_height < size)
194 {
195 pum_scrollbar = 1;
196 ++max_width;
197 }
198 else
199 pum_scrollbar = 0;
200
201 if (def_width < max_width)
202 def_width = max_width;
203
Bram Moolenaarabc97732007-08-08 20:49:37 +0000204 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
205#ifdef FEAT_RIGHTLEFT
206 && !curwin->w_p_rl)
207 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
208#endif
209 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000210 {
211 /* align pum column with "col" */
212 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000213
214#ifdef FEAT_RIGHTLEFT
215 if (curwin->w_p_rl)
216 pum_width = pum_col - pum_scrollbar + 1;
217 else
218#endif
219 pum_width = Columns - pum_col - pum_scrollbar;
220
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000221 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000222 && pum_width > PUM_DEF_WIDTH)
223 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000224 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000225 if (pum_width < PUM_DEF_WIDTH)
226 pum_width = PUM_DEF_WIDTH;
227 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000228 }
229 else if (Columns < def_width)
230 {
231 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000232#ifdef FEAT_RIGHTLEFT
233 if (curwin->w_p_rl)
234 pum_col = Columns - 1;
235 else
236#endif
237 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000238 pum_width = Columns - 1;
239 }
240 else
241 {
242 if (max_width > PUM_DEF_WIDTH)
243 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000244#ifdef FEAT_RIGHTLEFT
245 if (curwin->w_p_rl)
246 pum_col = max_width - 1;
247 else
248#endif
249 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000250 pum_width = max_width - pum_scrollbar;
251 }
252
253 pum_array = array;
254 pum_size = size;
255
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000256 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000257 * the positioning. Limit this to two times, when there is not much
258 * room the window size will keep changing. */
259 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000260 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000261}
262
263/*
264 * Redraw the popup menu, using "pum_first" and "pum_selected".
265 */
266 void
267pum_redraw()
268{
269 int row = pum_row;
270 int col;
271 int attr_norm = highlight_attr[HLF_PNI];
272 int attr_select = highlight_attr[HLF_PSI];
273 int attr_scroll = highlight_attr[HLF_PSB];
274 int attr_thumb = highlight_attr[HLF_PST];
275 int attr;
276 int i;
277 int idx;
278 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000279 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000280 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000281 int thumb_pos = 0;
282 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000283 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000284 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000285
286 if (pum_scrollbar)
287 {
288 thumb_heigth = pum_height * pum_height / pum_size;
289 if (thumb_heigth == 0)
290 thumb_heigth = 1;
291 thumb_pos = (pum_first * (pum_height - thumb_heigth)
292 + (pum_size - pum_height) / 2)
293 / (pum_size - pum_height);
294 }
295
296 for (i = 0; i < pum_height; ++i)
297 {
298 idx = i + pum_first;
299 attr = (idx == pum_selected) ? attr_select : attr_norm;
300
301 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000302#ifdef FEAT_RIGHTLEFT
303 if (curwin->w_p_rl)
304 {
305 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
306 screen_putchar(' ', row, pum_col + 1, attr);
307 }
308 else
309#endif
310 if (pum_col > 0)
311 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000312
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000313 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000314 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000315 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000316 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000317 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000318 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000319 width = 0;
320 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000321 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000322 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000323 case 1: p = pum_array[idx].pum_text; break;
324 case 2: p = pum_array[idx].pum_kind; break;
325 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000326 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000327 if (p != NULL)
328 for ( ; ; mb_ptr_adv(p))
329 {
330 if (s == NULL)
331 s = p;
332 w = ptr2cells(p);
333 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
334 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000335 /* Display the text that fits or comes before a Tab.
336 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000337 char_u *st;
338 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000339
340 *p = NUL;
341 st = transstr(s);
342 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000343#ifdef FEAT_RIGHTLEFT
344 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000345 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000346 if (st != NULL)
347 {
348 char_u *rt = reverse_text(st);
349 char_u *rt_saved = rt;
350 int len, j;
351
352 if (rt != NULL)
353 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000354 len = (int)STRLEN(rt);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000355 if (len > pum_width)
356 {
357 for (j = pum_width; j < len; ++j)
358 mb_ptr_adv(rt);
359 len = pum_width;
360 }
361 screen_puts_len(rt, len, row,
362 col - len + 1, attr);
363 vim_free(rt_saved);
364 }
365 vim_free(st);
366 }
367 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000368 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000369 else
370#endif
371 {
372 if (st != NULL)
373 {
374 screen_puts_len(st, (int)STRLEN(st), row, col,
375 attr);
376 vim_free(st);
377 }
378 col += width;
379 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000380
381 if (*p != TAB)
382 break;
383
384 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000385#ifdef FEAT_RIGHTLEFT
386 if (curwin->w_p_rl)
387 {
388 screen_puts_len((char_u *)" ", 2, row, col - 1,
389 attr);
390 col -= 2;
391 }
392 else
393#endif
394 {
395 screen_puts_len((char_u *)" ", 2, row, col, attr);
396 col += 2;
397 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000398 totwidth += 2;
399 s = NULL; /* start text at next char */
400 width = 0;
401 }
402 else
403 width += w;
404 }
405
406 if (round > 1)
407 n = pum_kind_width + 1;
408 else
409 n = 1;
410
411 /* Stop when there is nothing more to display. */
412 if (round == 3
413 || (round == 2 && pum_array[idx].pum_extra == NULL)
414 || (round == 1 && pum_array[idx].pum_kind == NULL
415 && pum_array[idx].pum_extra == NULL)
416 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000417 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000418#ifdef FEAT_RIGHTLEFT
419 if (curwin->w_p_rl)
420 {
421 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
422 col + 1, ' ', ' ', attr);
423 col = pum_col - pum_base_width - n + 1;
424 }
425 else
426#endif
427 {
428 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000429 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000430 col = pum_col + pum_base_width + n;
431 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000432 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000433 }
434
Bram Moolenaarabc97732007-08-08 20:49:37 +0000435#ifdef FEAT_RIGHTLEFT
436 if (curwin->w_p_rl)
437 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
438 ' ', attr);
439 else
440#endif
441 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
442 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000443 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444 {
445#ifdef FEAT_RIGHTLEFT
446 if (curwin->w_p_rl)
447 screen_putchar(' ', row, pum_col - pum_width,
448 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000449 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000450 else
451#endif
452 screen_putchar(' ', row, pum_col + pum_width,
453 i >= thumb_pos && i < thumb_pos + thumb_heigth
454 ? attr_thumb : attr_scroll);
455 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000456
457 ++row;
458 }
459}
460
461#if 0 /* not used yet */
462/*
463 * Return the index of the currently selected item.
464 */
465 int
466pum_get_selected()
467{
468 return pum_selected;
469}
470#endif
471
472/*
473 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000474 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000475 * This may be repeated when the preview window is used:
476 * "repeat" == 0: open preview window normally
477 * "repeat" == 1: open preview window but don't set the size
478 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000479 * Returns TRUE when the window was resized and the location of the popup menu
480 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000481 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000482 static int
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000483pum_set_selected(n, repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000484 int n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000485 int repeat;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000486{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000487 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000488 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000489
Bram Moolenaar4b779472005-10-04 09:12:31 +0000490 pum_selected = n;
491
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000492 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000493 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000494 if (pum_first > pum_selected - 4)
495 {
496 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000497 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000498 if (pum_first > pum_selected - 2)
499 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000500 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000501 if (pum_first < 0)
502 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000503 else if (pum_first > pum_selected)
504 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000505 }
506 else
507 pum_first = pum_selected;
508 }
509 else if (pum_first < pum_selected - pum_height + 5)
510 {
511 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000512 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000513 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000514 {
515 pum_first += pum_height - 2;
516 if (pum_first < pum_selected - pum_height + 1)
517 pum_first = pum_selected - pum_height + 1;
518 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000519 else
520 pum_first = pum_selected - pum_height + 1;
521 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000522
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000523 /* Give a few lines of context when possible. */
524 if (context > 3)
525 context = 3;
526 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000527 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000528 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000529 {
530 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000531 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000532 if (pum_first < 0)
533 pum_first = 0;
534 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000535 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000536 {
537 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000538 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539 }
540 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000541
542#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000543 /*
544 * Show extra info in the preview window if there is something and
545 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000546 * Skip this when tried twice already.
547 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000548 * NOTE: Be very careful not to sync undo!
549 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000550 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000551 && Rows > 10
552 && repeat <= 1
553 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000554 {
555 win_T *curwin_save = curwin;
556 int res = OK;
557
558 /* Open a preview window. 3 lines by default. */
559 g_do_tagpreview = 3;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000560 resized = prepare_tagpreview(FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000561 g_do_tagpreview = 0;
562
563 if (curwin->w_p_pvw)
564 {
565 if (curbuf->b_fname == NULL
566 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
567 && curbuf->b_p_bh[0] == 'w')
568 {
569 /* Already a "wipeout" buffer, make it empty. */
570 while (!bufempty())
571 ml_delete((linenr_T)1, FALSE);
572 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000573 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000574 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000575 /* Don't want to sync undo in the current buffer. */
576 ++no_u_sync;
577 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0);
578 --no_u_sync;
579 if (res == OK)
580 {
581 /* Edit a new, empty buffer. Set options for a "wipeout"
582 * buffer. */
583 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
584 set_option_value((char_u *)"bt", 0L,
585 (char_u *)"nofile", OPT_LOCAL);
586 set_option_value((char_u *)"bh", 0L,
587 (char_u *)"wipe", OPT_LOCAL);
588 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000589 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000590 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000591 }
592 if (res == OK)
593 {
594 char_u *p, *e;
595 linenr_T lnum = 0;
596
597 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
598 {
599 e = vim_strchr(p, '\n');
600 if (e == NULL)
601 {
602 ml_append(lnum++, p, 0, FALSE);
603 break;
604 }
605 else
606 {
607 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000608 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000609 *e = '\n';
610 p = e + 1;
611 }
612 }
613
614 /* Increase the height of the preview window to show the
615 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000616 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000617 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000618 if (lnum > p_pvh)
619 lnum = p_pvh;
620 if (curwin->w_height < lnum)
621 {
622 win_setheight((int)lnum);
623 resized = TRUE;
624 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000625 }
626
627 curbuf->b_changed = 0;
628 curbuf->b_p_ma = FALSE;
629 curwin->w_cursor.lnum = 0;
630 curwin->w_cursor.col = 0;
631
632 if (curwin != curwin_save && win_valid(curwin_save))
633 {
634 /* Return cursor to where we were */
635 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000636 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000637
638 /* When the preview window was resized we need to
639 * update the view on the buffer. Only go back to
640 * the window when needed, otherwise it will always be
641 * redraw. */
642 if (resized)
643 {
644 win_enter(curwin_save, TRUE);
645 update_topline();
646 }
647
648 /* Update the screen before drawing the popup menu.
649 * Enable updating the status lines. */
650 pum_do_redraw = TRUE;
651 update_screen(0);
652 pum_do_redraw = FALSE;
653
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000654 if (!resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000655 win_enter(curwin_save, TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000656
657 /* May need to update the screen again when there are
658 * autocommands involved. */
659 pum_do_redraw = TRUE;
660 update_screen(0);
661 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000662 }
663 }
664 }
665 }
666#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000667 }
668
669 /* Never display more than we have */
670 if (pum_first > pum_size - pum_height)
671 pum_first = pum_size - pum_height;
672
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000673 if (!resized)
674 pum_redraw();
675
676 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000677}
678
679/*
680 * Undisplay the popup menu (later).
681 */
682 void
683pum_undisplay()
684{
685 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000686 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000687#ifdef FEAT_WINDOWS
688 redraw_tabline = TRUE;
689#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000690 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000691}
692
693/*
694 * Clear the popup menu. Currently only resets the offset to the first
695 * displayed item.
696 */
697 void
698pum_clear()
699{
700 pum_first = 0;
701}
702
703/*
704 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000705 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000706 */
707 int
708pum_visible()
709{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000710 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000711}
712
Bram Moolenaare3226be2005-12-18 22:10:00 +0000713/*
714 * Return the height of the popup menu, the number of entries visible.
715 * Only valid when pum_visible() returns TRUE!
716 */
717 int
718pum_get_height()
719{
720 return pum_height;
721}
722
Bram Moolenaar4b779472005-10-04 09:12:31 +0000723#endif