blob: 8ff0711cc52f26360707ce04f300ab64acf76da2 [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
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 Moolenaarfc1421e2006-04-20 22:17:20 +0000490pum_set_selected(n, repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000491 int n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000492 int repeat;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000493{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000494 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000495 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000496
Bram Moolenaar4b779472005-10-04 09:12:31 +0000497 pum_selected = n;
498
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000499 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000500 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000501 if (pum_first > pum_selected - 4)
502 {
503 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000504 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000505 if (pum_first > pum_selected - 2)
506 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000507 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000508 if (pum_first < 0)
509 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000510 else if (pum_first > pum_selected)
511 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000512 }
513 else
514 pum_first = pum_selected;
515 }
516 else if (pum_first < pum_selected - pum_height + 5)
517 {
518 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000519 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000520 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000521 {
522 pum_first += pum_height - 2;
523 if (pum_first < pum_selected - pum_height + 1)
524 pum_first = pum_selected - pum_height + 1;
525 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000526 else
527 pum_first = pum_selected - pum_height + 1;
528 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000529
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000530 /* Give a few lines of context when possible. */
531 if (context > 3)
532 context = 3;
533 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000534 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000535 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000536 {
537 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000538 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539 if (pum_first < 0)
540 pum_first = 0;
541 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000542 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000543 {
544 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000545 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000546 }
547 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000548
549#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000550 /*
551 * Show extra info in the preview window if there is something and
552 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000553 * Skip this when tried twice already.
554 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000555 * NOTE: Be very careful not to sync undo!
556 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000557 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000558 && Rows > 10
559 && repeat <= 1
560 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000561 {
562 win_T *curwin_save = curwin;
563 int res = OK;
564
Bram Moolenaaree236d02010-10-27 17:11:15 +0200565 /* Open a preview window. 3 lines by default. Prefer
566 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200568 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
569 g_do_tagpreview = p_pvh;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000570 resized = prepare_tagpreview(FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000571 g_do_tagpreview = 0;
572
573 if (curwin->w_p_pvw)
574 {
575 if (curbuf->b_fname == NULL
576 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
577 && curbuf->b_p_bh[0] == 'w')
578 {
579 /* Already a "wipeout" buffer, make it empty. */
580 while (!bufempty())
581 ml_delete((linenr_T)1, FALSE);
582 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000583 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000584 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000585 /* Don't want to sync undo in the current buffer. */
586 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000587 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000588 --no_u_sync;
589 if (res == OK)
590 {
591 /* Edit a new, empty buffer. Set options for a "wipeout"
592 * buffer. */
593 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
594 set_option_value((char_u *)"bt", 0L,
595 (char_u *)"nofile", OPT_LOCAL);
596 set_option_value((char_u *)"bh", 0L,
597 (char_u *)"wipe", OPT_LOCAL);
598 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000599 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000600 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000601 }
602 if (res == OK)
603 {
604 char_u *p, *e;
605 linenr_T lnum = 0;
606
607 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
608 {
609 e = vim_strchr(p, '\n');
610 if (e == NULL)
611 {
612 ml_append(lnum++, p, 0, FALSE);
613 break;
614 }
615 else
616 {
617 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000618 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000619 *e = '\n';
620 p = e + 1;
621 }
622 }
623
624 /* Increase the height of the preview window to show the
625 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000626 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000627 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000628 if (lnum > p_pvh)
629 lnum = p_pvh;
630 if (curwin->w_height < lnum)
631 {
632 win_setheight((int)lnum);
633 resized = TRUE;
634 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000635 }
636
637 curbuf->b_changed = 0;
638 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100639 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000640 curwin->w_cursor.col = 0;
641
642 if (curwin != curwin_save && win_valid(curwin_save))
643 {
644 /* Return cursor to where we were */
645 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000646 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000647
648 /* When the preview window was resized we need to
649 * update the view on the buffer. Only go back to
650 * the window when needed, otherwise it will always be
651 * redraw. */
652 if (resized)
653 {
654 win_enter(curwin_save, TRUE);
655 update_topline();
656 }
657
658 /* Update the screen before drawing the popup menu.
659 * Enable updating the status lines. */
660 pum_do_redraw = TRUE;
661 update_screen(0);
662 pum_do_redraw = FALSE;
663
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000664 if (!resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000665 win_enter(curwin_save, TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000666
667 /* May need to update the screen again when there are
668 * autocommands involved. */
669 pum_do_redraw = TRUE;
670 update_screen(0);
671 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000672 }
673 }
674 }
675 }
676#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000677 }
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