blob: 4dd3a94882dbf8da0b60caa1fad776e44669d243 [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;
59 int height;
60 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 Moolenaar96d2c5b2006-03-11 21:27:59 +000076 row = curwin->w_cline_row + W_WINROW(curwin);
77 height = curwin->w_cline_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000078
79 if (firstwin->w_p_pvw)
80 top_clear = firstwin->w_height;
81 else
82 top_clear = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000083
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000084 /* When the preview window is at the bottom stop just above it. Also
85 * avoid drawing over the status line so that it's clear there is a window
86 * boundary. */
87 if (lastwin->w_p_pvw)
88 above_row -= lastwin->w_height + lastwin->w_status_height + 1;
89
Bram Moolenaar4b779472005-10-04 09:12:31 +000090 /*
91 * Figure out the size and position of the pum.
92 */
93 if (size < PUM_DEF_HEIGHT)
94 pum_height = size;
95 else
96 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +000097 if (p_ph > 0 && pum_height > p_ph)
98 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +000099
100 /* Put the pum below "row" if possible. If there are few lines decide on
101 * where there is more room. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000102 if (row >= above_row - pum_height
103 && row > (above_row - top_clear - height) / 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000104 {
105 /* pum above "row" */
106 if (row >= size)
107 {
108 pum_row = row - size;
109 pum_height = size;
110 }
111 else
112 {
113 pum_row = 0;
114 pum_height = row;
115 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000116 if (p_ph > 0 && pum_height > p_ph)
117 {
118 pum_row += pum_height - p_ph;
119 pum_height = p_ph;
120 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000121 }
122 else
123 {
124 /* pum below "row" */
125 pum_row = row + height;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000126 if (size > above_row - pum_row)
127 pum_height = above_row - pum_row;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000128 else
129 pum_height = size;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000130 if (p_ph > 0 && pum_height > p_ph)
131 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000132 }
133
134 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000135 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000136 return;
137
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000138 /* If there is a preview window at the top avoid drawing over it. */
139 if (firstwin->w_p_pvw
140 && pum_row < firstwin->w_height
141 && pum_height > firstwin->w_height + 4)
142 {
143 pum_row += firstwin->w_height;
144 pum_height -= firstwin->w_height;
145 }
146
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000147 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000148 for (i = 0; i < size; ++i)
149 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000150 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000151 if (max_width < w)
152 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000153 if (array[i].pum_kind != NULL)
154 {
155 w = vim_strsize(array[i].pum_kind) + 1;
156 if (kind_width < w)
157 kind_width = w;
158 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000159 if (array[i].pum_extra != NULL)
160 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000161 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000162 if (extra_width < w)
163 extra_width = w;
164 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000165 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000166 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000167 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000168
Bram Moolenaarabc97732007-08-08 20:49:37 +0000169 /* Calculate column */
170#ifdef FEAT_RIGHTLEFT
171 if (curwin->w_p_rl)
172 col = W_WINCOL(curwin) + W_WIDTH(curwin) - curwin->w_wcol -
173 curwin->w_leftcol - 1;
174 else
175#endif
176 col = W_WINCOL(curwin) + curwin->w_wcol - curwin->w_leftcol;
177
Bram Moolenaar4b779472005-10-04 09:12:31 +0000178 /* if there are more items than room we need a scrollbar */
179 if (pum_height < size)
180 {
181 pum_scrollbar = 1;
182 ++max_width;
183 }
184 else
185 pum_scrollbar = 0;
186
187 if (def_width < max_width)
188 def_width = max_width;
189
Bram Moolenaarabc97732007-08-08 20:49:37 +0000190 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
191#ifdef FEAT_RIGHTLEFT
192 && !curwin->w_p_rl)
193 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
194#endif
195 ))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000196 {
197 /* align pum column with "col" */
198 pum_col = col;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000199
200#ifdef FEAT_RIGHTLEFT
201 if (curwin->w_p_rl)
202 pum_width = pum_col - pum_scrollbar + 1;
203 else
204#endif
205 pum_width = Columns - pum_col - pum_scrollbar;
206
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000207 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000208 && pum_width > PUM_DEF_WIDTH)
209 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000210 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000211 if (pum_width < PUM_DEF_WIDTH)
212 pum_width = PUM_DEF_WIDTH;
213 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000214 }
215 else if (Columns < def_width)
216 {
217 /* not enough room, will use what we have */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000218#ifdef FEAT_RIGHTLEFT
219 if (curwin->w_p_rl)
220 pum_col = Columns - 1;
221 else
222#endif
223 pum_col = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000224 pum_width = Columns - 1;
225 }
226 else
227 {
228 if (max_width > PUM_DEF_WIDTH)
229 max_width = PUM_DEF_WIDTH; /* truncate */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000230#ifdef FEAT_RIGHTLEFT
231 if (curwin->w_p_rl)
232 pum_col = max_width - 1;
233 else
234#endif
235 pum_col = Columns - max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000236 pum_width = max_width - pum_scrollbar;
237 }
238
239 pum_array = array;
240 pum_size = size;
241
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000242 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000243 * the positioning. Limit this to two times, when there is not much
244 * room the window size will keep changing. */
245 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000246 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000247}
248
249/*
250 * Redraw the popup menu, using "pum_first" and "pum_selected".
251 */
252 void
253pum_redraw()
254{
255 int row = pum_row;
256 int col;
257 int attr_norm = highlight_attr[HLF_PNI];
258 int attr_select = highlight_attr[HLF_PSI];
259 int attr_scroll = highlight_attr[HLF_PSB];
260 int attr_thumb = highlight_attr[HLF_PST];
261 int attr;
262 int i;
263 int idx;
264 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000265 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000266 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000267 int thumb_pos = 0;
268 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000269 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000270 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000271
272 if (pum_scrollbar)
273 {
274 thumb_heigth = pum_height * pum_height / pum_size;
275 if (thumb_heigth == 0)
276 thumb_heigth = 1;
277 thumb_pos = (pum_first * (pum_height - thumb_heigth)
278 + (pum_size - pum_height) / 2)
279 / (pum_size - pum_height);
280 }
281
282 for (i = 0; i < pum_height; ++i)
283 {
284 idx = i + pum_first;
285 attr = (idx == pum_selected) ? attr_select : attr_norm;
286
287 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000288#ifdef FEAT_RIGHTLEFT
289 if (curwin->w_p_rl)
290 {
291 if (pum_col < W_WINCOL(curwin) + W_WIDTH(curwin) - 1)
292 screen_putchar(' ', row, pum_col + 1, attr);
293 }
294 else
295#endif
296 if (pum_col > 0)
297 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000298
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000299 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000300 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000301 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000302 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000303 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000304 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000305 width = 0;
306 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000307 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000308 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000309 case 1: p = pum_array[idx].pum_text; break;
310 case 2: p = pum_array[idx].pum_kind; break;
311 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000312 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000313 if (p != NULL)
314 for ( ; ; mb_ptr_adv(p))
315 {
316 if (s == NULL)
317 s = p;
318 w = ptr2cells(p);
319 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
320 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000321 /* Display the text that fits or comes before a Tab.
322 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000323 char_u *st;
324 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000325
326 *p = NUL;
327 st = transstr(s);
328 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000329#ifdef FEAT_RIGHTLEFT
330 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000331 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000332 if (st != NULL)
333 {
334 char_u *rt = reverse_text(st);
335 char_u *rt_saved = rt;
336 int len, j;
337
338 if (rt != NULL)
339 {
340 len = STRLEN(rt);
341 if (len > pum_width)
342 {
343 for (j = pum_width; j < len; ++j)
344 mb_ptr_adv(rt);
345 len = pum_width;
346 }
347 screen_puts_len(rt, len, row,
348 col - len + 1, attr);
349 vim_free(rt_saved);
350 }
351 vim_free(st);
352 }
353 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000354 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000355 else
356#endif
357 {
358 if (st != NULL)
359 {
360 screen_puts_len(st, (int)STRLEN(st), row, col,
361 attr);
362 vim_free(st);
363 }
364 col += width;
365 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000366
367 if (*p != TAB)
368 break;
369
370 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000371#ifdef FEAT_RIGHTLEFT
372 if (curwin->w_p_rl)
373 {
374 screen_puts_len((char_u *)" ", 2, row, col - 1,
375 attr);
376 col -= 2;
377 }
378 else
379#endif
380 {
381 screen_puts_len((char_u *)" ", 2, row, col, attr);
382 col += 2;
383 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000384 totwidth += 2;
385 s = NULL; /* start text at next char */
386 width = 0;
387 }
388 else
389 width += w;
390 }
391
392 if (round > 1)
393 n = pum_kind_width + 1;
394 else
395 n = 1;
396
397 /* Stop when there is nothing more to display. */
398 if (round == 3
399 || (round == 2 && pum_array[idx].pum_extra == NULL)
400 || (round == 1 && pum_array[idx].pum_kind == NULL
401 && pum_array[idx].pum_extra == NULL)
402 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000403 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000404#ifdef FEAT_RIGHTLEFT
405 if (curwin->w_p_rl)
406 {
407 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
408 col + 1, ' ', ' ', attr);
409 col = pum_col - pum_base_width - n + 1;
410 }
411 else
412#endif
413 {
414 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000415 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000416 col = pum_col + pum_base_width + n;
417 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000418 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000419 }
420
Bram Moolenaarabc97732007-08-08 20:49:37 +0000421#ifdef FEAT_RIGHTLEFT
422 if (curwin->w_p_rl)
423 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
424 ' ', attr);
425 else
426#endif
427 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
428 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000429 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000430 {
431#ifdef FEAT_RIGHTLEFT
432 if (curwin->w_p_rl)
433 screen_putchar(' ', row, pum_col - pum_width,
434 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000435 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000436 else
437#endif
438 screen_putchar(' ', row, pum_col + pum_width,
439 i >= thumb_pos && i < thumb_pos + thumb_heigth
440 ? attr_thumb : attr_scroll);
441 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000442
443 ++row;
444 }
445}
446
447#if 0 /* not used yet */
448/*
449 * Return the index of the currently selected item.
450 */
451 int
452pum_get_selected()
453{
454 return pum_selected;
455}
456#endif
457
458/*
459 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000460 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000461 * This may be repeated when the preview window is used:
462 * "repeat" == 0: open preview window normally
463 * "repeat" == 1: open preview window but don't set the size
464 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000465 * Returns TRUE when the window was resized and the location of the popup menu
466 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000467 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000468 static int
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000469pum_set_selected(n, repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000470 int n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000471 int repeat;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000472{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000473 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000474 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000475
Bram Moolenaar4b779472005-10-04 09:12:31 +0000476 pum_selected = n;
477
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000478 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000479 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000480 if (pum_first > pum_selected - 4)
481 {
482 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000483 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000484 if (pum_first > pum_selected - 2)
485 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000486 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000487 if (pum_first < 0)
488 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000489 else if (pum_first > pum_selected)
490 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000491 }
492 else
493 pum_first = pum_selected;
494 }
495 else if (pum_first < pum_selected - pum_height + 5)
496 {
497 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000498 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000499 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000500 {
501 pum_first += pum_height - 2;
502 if (pum_first < pum_selected - pum_height + 1)
503 pum_first = pum_selected - pum_height + 1;
504 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000505 else
506 pum_first = pum_selected - pum_height + 1;
507 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000508
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000509 /* Give a few lines of context when possible. */
510 if (context > 3)
511 context = 3;
512 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000513 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000514 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000515 {
516 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000517 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000518 if (pum_first < 0)
519 pum_first = 0;
520 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000521 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000522 {
523 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000524 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000525 }
526 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000527
528#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000529 /*
530 * Show extra info in the preview window if there is something and
531 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000532 * Skip this when tried twice already.
533 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000534 * NOTE: Be very careful not to sync undo!
535 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000536 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000537 && Rows > 10
538 && repeat <= 1
539 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000540 {
541 win_T *curwin_save = curwin;
542 int res = OK;
543
544 /* Open a preview window. 3 lines by default. */
545 g_do_tagpreview = 3;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000546 resized = prepare_tagpreview(FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000547 g_do_tagpreview = 0;
548
549 if (curwin->w_p_pvw)
550 {
551 if (curbuf->b_fname == NULL
552 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
553 && curbuf->b_p_bh[0] == 'w')
554 {
555 /* Already a "wipeout" buffer, make it empty. */
556 while (!bufempty())
557 ml_delete((linenr_T)1, FALSE);
558 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000559 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000560 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000561 /* Don't want to sync undo in the current buffer. */
562 ++no_u_sync;
563 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0);
564 --no_u_sync;
565 if (res == OK)
566 {
567 /* Edit a new, empty buffer. Set options for a "wipeout"
568 * buffer. */
569 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
570 set_option_value((char_u *)"bt", 0L,
571 (char_u *)"nofile", OPT_LOCAL);
572 set_option_value((char_u *)"bh", 0L,
573 (char_u *)"wipe", OPT_LOCAL);
574 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000575 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000576 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000577 }
578 if (res == OK)
579 {
580 char_u *p, *e;
581 linenr_T lnum = 0;
582
583 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
584 {
585 e = vim_strchr(p, '\n');
586 if (e == NULL)
587 {
588 ml_append(lnum++, p, 0, FALSE);
589 break;
590 }
591 else
592 {
593 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000594 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000595 *e = '\n';
596 p = e + 1;
597 }
598 }
599
600 /* Increase the height of the preview window to show the
601 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000602 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000603 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000604 if (lnum > p_pvh)
605 lnum = p_pvh;
606 if (curwin->w_height < lnum)
607 {
608 win_setheight((int)lnum);
609 resized = TRUE;
610 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000611 }
612
613 curbuf->b_changed = 0;
614 curbuf->b_p_ma = FALSE;
615 curwin->w_cursor.lnum = 0;
616 curwin->w_cursor.col = 0;
617
618 if (curwin != curwin_save && win_valid(curwin_save))
619 {
620 /* Return cursor to where we were */
621 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000622 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000623
624 /* When the preview window was resized we need to
625 * update the view on the buffer. Only go back to
626 * the window when needed, otherwise it will always be
627 * redraw. */
628 if (resized)
629 {
630 win_enter(curwin_save, TRUE);
631 update_topline();
632 }
633
634 /* Update the screen before drawing the popup menu.
635 * Enable updating the status lines. */
636 pum_do_redraw = TRUE;
637 update_screen(0);
638 pum_do_redraw = FALSE;
639
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000640 if (!resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000641 win_enter(curwin_save, TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000642
643 /* May need to update the screen again when there are
644 * autocommands involved. */
645 pum_do_redraw = TRUE;
646 update_screen(0);
647 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000648 }
649 }
650 }
651 }
652#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000653 }
654
655 /* Never display more than we have */
656 if (pum_first > pum_size - pum_height)
657 pum_first = pum_size - pum_height;
658
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000659 if (!resized)
660 pum_redraw();
661
662 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000663}
664
665/*
666 * Undisplay the popup menu (later).
667 */
668 void
669pum_undisplay()
670{
671 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000672 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000673#ifdef FEAT_WINDOWS
674 redraw_tabline = TRUE;
675#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000676 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000677}
678
679/*
680 * Clear the popup menu. Currently only resets the offset to the first
681 * displayed item.
682 */
683 void
684pum_clear()
685{
686 pum_first = 0;
687}
688
689/*
690 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000691 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000692 */
693 int
694pum_visible()
695{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000696 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000697}
698
Bram Moolenaare3226be2005-12-18 22:10:00 +0000699/*
700 * Return the height of the popup menu, the number of entries visible.
701 * Only valid when pum_visible() returns TRUE!
702 */
703 int
704pum_get_height()
705{
706 return pum_height;
707}
708
Bram Moolenaar4b779472005-10-04 09:12:31 +0000709#endif