blob: eb4631a4ce312685c3c8bffa15e35acd1a07683e [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;
78 col = curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol;
79
80 if (firstwin->w_p_pvw)
81 top_clear = firstwin->w_height;
82 else
83 top_clear = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000084
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000085 /* When the preview window is at the bottom stop just above it. Also
86 * avoid drawing over the status line so that it's clear there is a window
87 * boundary. */
88 if (lastwin->w_p_pvw)
89 above_row -= lastwin->w_height + lastwin->w_status_height + 1;
90
Bram Moolenaar4b779472005-10-04 09:12:31 +000091 /*
92 * Figure out the size and position of the pum.
93 */
94 if (size < PUM_DEF_HEIGHT)
95 pum_height = size;
96 else
97 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +000098 if (p_ph > 0 && pum_height > p_ph)
99 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000100
101 /* Put the pum below "row" if possible. If there are few lines decide on
102 * where there is more room. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000103 if (row >= above_row - pum_height
104 && row > (above_row - top_clear - height) / 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000105 {
106 /* pum above "row" */
107 if (row >= size)
108 {
109 pum_row = row - size;
110 pum_height = size;
111 }
112 else
113 {
114 pum_row = 0;
115 pum_height = row;
116 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000117 if (p_ph > 0 && pum_height > p_ph)
118 {
119 pum_row += pum_height - p_ph;
120 pum_height = p_ph;
121 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000122 }
123 else
124 {
125 /* pum below "row" */
126 pum_row = row + height;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000127 if (size > above_row - pum_row)
128 pum_height = above_row - pum_row;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000129 else
130 pum_height = size;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000131 if (p_ph > 0 && pum_height > p_ph)
132 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000133 }
134
135 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000136 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000137 return;
138
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000139 /* If there is a preview window at the top avoid drawing over it. */
140 if (firstwin->w_p_pvw
141 && pum_row < firstwin->w_height
142 && pum_height > firstwin->w_height + 4)
143 {
144 pum_row += firstwin->w_height;
145 pum_height -= firstwin->w_height;
146 }
147
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000148 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000149 for (i = 0; i < size; ++i)
150 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000152 if (max_width < w)
153 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000154 if (array[i].pum_kind != NULL)
155 {
156 w = vim_strsize(array[i].pum_kind) + 1;
157 if (kind_width < w)
158 kind_width = w;
159 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000160 if (array[i].pum_extra != NULL)
161 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000162 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000163 if (extra_width < w)
164 extra_width = w;
165 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000166 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000167 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000168 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000169
170 /* if there are more items than room we need a scrollbar */
171 if (pum_height < size)
172 {
173 pum_scrollbar = 1;
174 ++max_width;
175 }
176 else
177 pum_scrollbar = 0;
178
179 if (def_width < max_width)
180 def_width = max_width;
181
182 if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
183 {
184 /* align pum column with "col" */
185 pum_col = col;
186 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000187 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000188 && pum_width > PUM_DEF_WIDTH)
189 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000190 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000191 if (pum_width < PUM_DEF_WIDTH)
192 pum_width = PUM_DEF_WIDTH;
193 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000194 }
195 else if (Columns < def_width)
196 {
197 /* not enough room, will use what we have */
198 pum_col = 0;
199 pum_width = Columns - 1;
200 }
201 else
202 {
203 if (max_width > PUM_DEF_WIDTH)
204 max_width = PUM_DEF_WIDTH; /* truncate */
205 pum_col = Columns - max_width;
206 pum_width = max_width - pum_scrollbar;
207 }
208
209 pum_array = array;
210 pum_size = size;
211
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000212 /* Set selected item and redraw. If the window size changed need to redo
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 * the positioning. Limit this to two times, when there is not much
214 * room the window size will keep changing. */
215 if (pum_set_selected(selected, redo_count) && ++redo_count <= 2)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000216 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000217}
218
219/*
220 * Redraw the popup menu, using "pum_first" and "pum_selected".
221 */
222 void
223pum_redraw()
224{
225 int row = pum_row;
226 int col;
227 int attr_norm = highlight_attr[HLF_PNI];
228 int attr_select = highlight_attr[HLF_PSI];
229 int attr_scroll = highlight_attr[HLF_PSB];
230 int attr_thumb = highlight_attr[HLF_PST];
231 int attr;
232 int i;
233 int idx;
234 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000235 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000236 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000237 int thumb_pos = 0;
238 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000239 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000240 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000241
242 if (pum_scrollbar)
243 {
244 thumb_heigth = pum_height * pum_height / pum_size;
245 if (thumb_heigth == 0)
246 thumb_heigth = 1;
247 thumb_pos = (pum_first * (pum_height - thumb_heigth)
248 + (pum_size - pum_height) / 2)
249 / (pum_size - pum_height);
250 }
251
252 for (i = 0; i < pum_height; ++i)
253 {
254 idx = i + pum_first;
255 attr = (idx == pum_selected) ? attr_select : attr_norm;
256
257 /* prepend a space if there is room */
258 if (pum_col > 0)
259 screen_putchar(' ', row, pum_col - 1, attr);
260
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000261 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000262 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000263 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000264 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000265 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000266 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000267 width = 0;
268 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000269 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000270 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000271 case 1: p = pum_array[idx].pum_text; break;
272 case 2: p = pum_array[idx].pum_kind; break;
273 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000274 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000275 if (p != NULL)
276 for ( ; ; mb_ptr_adv(p))
277 {
278 if (s == NULL)
279 s = p;
280 w = ptr2cells(p);
281 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
282 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000283 /* Display the text that fits or comes before a Tab.
284 * First convert it to printable characters. */
285 char_u *st;
286 int saved = *p;
287
288 *p = NUL;
289 st = transstr(s);
290 *p = saved;
291 if (st != NULL)
292 {
293 screen_puts_len(st, (int)STRLEN(st), row, col,
294 attr);
295 vim_free(st);
296 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000297 col += width;
298
299 if (*p != TAB)
300 break;
301
302 /* Display two spaces for a Tab. */
303 screen_puts_len((char_u *)" ", 2, row, col, attr);
304 col += 2;
305 totwidth += 2;
306 s = NULL; /* start text at next char */
307 width = 0;
308 }
309 else
310 width += w;
311 }
312
313 if (round > 1)
314 n = pum_kind_width + 1;
315 else
316 n = 1;
317
318 /* Stop when there is nothing more to display. */
319 if (round == 3
320 || (round == 2 && pum_array[idx].pum_extra == NULL)
321 || (round == 1 && pum_array[idx].pum_kind == NULL
322 && pum_array[idx].pum_extra == NULL)
323 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000324 break;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000325 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000326 ' ', ' ', attr);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000327 col = pum_col + pum_base_width + n;
328 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000329 }
330
331 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
332 if (pum_scrollbar > 0)
333 screen_putchar(' ', row, pum_col + pum_width,
334 i >= thumb_pos && i < thumb_pos + thumb_heigth
335 ? attr_thumb : attr_scroll);
336
337 ++row;
338 }
339}
340
341#if 0 /* not used yet */
342/*
343 * Return the index of the currently selected item.
344 */
345 int
346pum_get_selected()
347{
348 return pum_selected;
349}
350#endif
351
352/*
353 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000354 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000355 * This may be repeated when the preview window is used:
356 * "repeat" == 0: open preview window normally
357 * "repeat" == 1: open preview window but don't set the size
358 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000359 * Returns TRUE when the window was resized and the location of the popup menu
360 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000361 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000362 static int
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000363pum_set_selected(n, repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000364 int n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000365 int repeat;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000366{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000367 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000368 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000369
Bram Moolenaar4b779472005-10-04 09:12:31 +0000370 pum_selected = n;
371
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000372 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000373 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000374 if (pum_first > pum_selected - 4)
375 {
376 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000377 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000378 if (pum_first > pum_selected - 2)
379 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000380 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000381 if (pum_first < 0)
382 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000383 else if (pum_first > pum_selected)
384 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000385 }
386 else
387 pum_first = pum_selected;
388 }
389 else if (pum_first < pum_selected - pum_height + 5)
390 {
391 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000392 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000393 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000394 {
395 pum_first += pum_height - 2;
396 if (pum_first < pum_selected - pum_height + 1)
397 pum_first = pum_selected - pum_height + 1;
398 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000399 else
400 pum_first = pum_selected - pum_height + 1;
401 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000402
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000403 /* Give a few lines of context when possible. */
404 if (context > 3)
405 context = 3;
406 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000407 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000408 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000409 {
410 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000411 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000412 if (pum_first < 0)
413 pum_first = 0;
414 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000415 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000416 {
417 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000418 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000419 }
420 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000421
422#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000423 /*
424 * Show extra info in the preview window if there is something and
425 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000426 * Skip this when tried twice already.
427 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000428 * NOTE: Be very careful not to sync undo!
429 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000430 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000431 && Rows > 10
432 && repeat <= 1
433 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000434 {
435 win_T *curwin_save = curwin;
436 int res = OK;
437
438 /* Open a preview window. 3 lines by default. */
439 g_do_tagpreview = 3;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000440 resized = prepare_tagpreview(FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000441 g_do_tagpreview = 0;
442
443 if (curwin->w_p_pvw)
444 {
445 if (curbuf->b_fname == NULL
446 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
447 && curbuf->b_p_bh[0] == 'w')
448 {
449 /* Already a "wipeout" buffer, make it empty. */
450 while (!bufempty())
451 ml_delete((linenr_T)1, FALSE);
452 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000453 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000454 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000455 /* Don't want to sync undo in the current buffer. */
456 ++no_u_sync;
457 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0);
458 --no_u_sync;
459 if (res == OK)
460 {
461 /* Edit a new, empty buffer. Set options for a "wipeout"
462 * buffer. */
463 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
464 set_option_value((char_u *)"bt", 0L,
465 (char_u *)"nofile", OPT_LOCAL);
466 set_option_value((char_u *)"bh", 0L,
467 (char_u *)"wipe", OPT_LOCAL);
468 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000469 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000470 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000471 }
472 if (res == OK)
473 {
474 char_u *p, *e;
475 linenr_T lnum = 0;
476
477 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
478 {
479 e = vim_strchr(p, '\n');
480 if (e == NULL)
481 {
482 ml_append(lnum++, p, 0, FALSE);
483 break;
484 }
485 else
486 {
487 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000488 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000489 *e = '\n';
490 p = e + 1;
491 }
492 }
493
494 /* Increase the height of the preview window to show the
495 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000496 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000497 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000498 if (lnum > p_pvh)
499 lnum = p_pvh;
500 if (curwin->w_height < lnum)
501 {
502 win_setheight((int)lnum);
503 resized = TRUE;
504 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000505 }
506
507 curbuf->b_changed = 0;
508 curbuf->b_p_ma = FALSE;
509 curwin->w_cursor.lnum = 0;
510 curwin->w_cursor.col = 0;
511
512 if (curwin != curwin_save && win_valid(curwin_save))
513 {
514 /* Return cursor to where we were */
515 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000516 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000517
518 /* When the preview window was resized we need to
519 * update the view on the buffer. Only go back to
520 * the window when needed, otherwise it will always be
521 * redraw. */
522 if (resized)
523 {
524 win_enter(curwin_save, TRUE);
525 update_topline();
526 }
527
528 /* Update the screen before drawing the popup menu.
529 * Enable updating the status lines. */
530 pum_do_redraw = TRUE;
531 update_screen(0);
532 pum_do_redraw = FALSE;
533
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000534 if (!resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000535 win_enter(curwin_save, TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000536
537 /* May need to update the screen again when there are
538 * autocommands involved. */
539 pum_do_redraw = TRUE;
540 update_screen(0);
541 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000542 }
543 }
544 }
545 }
546#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000547 }
548
549 /* Never display more than we have */
550 if (pum_first > pum_size - pum_height)
551 pum_first = pum_size - pum_height;
552
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000553 if (!resized)
554 pum_redraw();
555
556 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000557}
558
559/*
560 * Undisplay the popup menu (later).
561 */
562 void
563pum_undisplay()
564{
565 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000566 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000567#ifdef FEAT_WINDOWS
568 redraw_tabline = TRUE;
569#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000570 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000571}
572
573/*
574 * Clear the popup menu. Currently only resets the offset to the first
575 * displayed item.
576 */
577 void
578pum_clear()
579{
580 pum_first = 0;
581}
582
583/*
584 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000585 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000586 */
587 int
588pum_visible()
589{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000590 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000591}
592
Bram Moolenaare3226be2005-12-18 22:10:00 +0000593/*
594 * Return the height of the popup menu, the number of entries visible.
595 * Only valid when pum_visible() returns TRUE!
596 */
597 int
598pum_get_height()
599{
600 return pum_height;
601}
602
Bram Moolenaar4b779472005-10-04 09:12:31 +0000603#endif