blob: dc66e75944ad25cb15288d424061daeb46ada2c6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar4b779472005-10-04 09:12:31 +00002 *
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 Moolenaar51b0f372017-11-18 18:52:04 +010026static int pum_extra_width; /* width of extra stuff */
Bram Moolenaar4b779472005-10-04 09:12:31 +000027static int pum_scrollbar; /* TRUE when scrollbar present */
28
29static int pum_row; /* top row of pum */
30static int pum_col; /* left column of pum */
31
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000032static int pum_do_redraw = FALSE; /* do redraw anyway */
33
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010034static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000035
Bram Moolenaar4b779472005-10-04 09:12:31 +000036#define PUM_DEF_HEIGHT 10
37#define PUM_DEF_WIDTH 15
38
Bram Moolenaar51b0f372017-11-18 18:52:04 +010039 static void
40pum_compute_size(void)
41{
42 int i;
43 int w;
44
45 /* Compute the width of the widest match and the widest extra. */
46 pum_base_width = 0;
47 pum_kind_width = 0;
48 pum_extra_width = 0;
49 for (i = 0; i < pum_size; ++i)
50 {
51 w = vim_strsize(pum_array[i].pum_text);
52 if (pum_base_width < w)
53 pum_base_width = w;
54 if (pum_array[i].pum_kind != NULL)
55 {
56 w = vim_strsize(pum_array[i].pum_kind) + 1;
57 if (pum_kind_width < w)
58 pum_kind_width = w;
59 }
60 if (pum_array[i].pum_extra != NULL)
61 {
62 w = vim_strsize(pum_array[i].pum_extra) + 1;
63 if (pum_extra_width < w)
64 pum_extra_width = w;
65 }
66 }
67}
68
Bram Moolenaar4b779472005-10-04 09:12:31 +000069/*
70 * Show the popup menu with items "array[size]".
71 * "array" must remain valid until pum_undisplay() is called!
72 * When possible the leftmost character is aligned with screen column "col".
73 * The menu appears above the screen line "row" or at "row" + "height" - 1.
74 */
75 void
Bram Moolenaar05540972016-01-30 20:31:25 +010076pum_display(
77 pumitem_T *array,
78 int size,
79 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000080 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000081{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000082 int def_width;
83 int max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +000084 int row;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000085 int context_lines;
Bram Moolenaar4b779472005-10-04 09:12:31 +000086 int col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010087 int above_row;
88 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000089 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020090#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010091 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +010092#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093
Bram Moolenaara5e66212017-09-29 22:42:33 +020094 do
95 {
96 def_width = PUM_DEF_WIDTH;
Bram Moolenaara5e66212017-09-29 22:42:33 +020097 above_row = 0;
98 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000099
Bram Moolenaara5e66212017-09-29 22:42:33 +0200100 /* Pretend the pum is already there to avoid that must_redraw is set
101 * when 'cuc' is on. */
102 pum_array = (pumitem_T *)1;
103 validate_cursor_col();
104 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000105
Bram Moolenaara5e66212017-09-29 22:42:33 +0200106 row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000107
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200109 FOR_ALL_WINDOWS(pvwin)
110 if (pvwin->w_p_pvw)
111 break;
112 if (pvwin != NULL)
113 {
114 if (W_WINROW(pvwin) < W_WINROW(curwin))
115 above_row = W_WINROW(pvwin) + pvwin->w_height;
116 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
117 below_row = W_WINROW(pvwin);
118 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100119#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000120
Bram Moolenaara5e66212017-09-29 22:42:33 +0200121 /*
122 * Figure out the size and position of the pum.
123 */
124 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000125 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000126 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200127 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000128 if (p_ph > 0 && pum_height > p_ph)
129 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000130
Bram Moolenaara5e66212017-09-29 22:42:33 +0200131 /* Put the pum below "row" if possible. If there are few lines decide
132 * on where there is more room. */
133 if (row + 2 >= below_row - pum_height
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100134 && row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200135 {
136 /* pum above "row" */
137
138 /* Leave two lines of context if possible */
139 if (curwin->w_wrow - curwin->w_cline_row >= 2)
140 context_lines = 2;
141 else
142 context_lines = curwin->w_wrow - curwin->w_cline_row;
143
144 if (row >= size + context_lines)
145 {
146 pum_row = row - size - context_lines;
147 pum_height = size;
148 }
149 else
150 {
151 pum_row = 0;
152 pum_height = row - context_lines;
153 }
154 if (p_ph > 0 && pum_height > p_ph)
155 {
156 pum_row += pum_height - p_ph;
157 pum_height = p_ph;
158 }
159 }
160 else
161 {
162 /* pum below "row" */
163
164 /* Leave two lines of context if possible */
165 if (curwin->w_cline_row
166 + curwin->w_cline_height - curwin->w_wrow >= 3)
167 context_lines = 3;
168 else
169 context_lines = curwin->w_cline_row
170 + curwin->w_cline_height - curwin->w_wrow;
171
172 pum_row = row + context_lines;
173 if (size > below_row - pum_row)
174 pum_height = below_row - pum_row;
175 else
176 pum_height = size;
177 if (p_ph > 0 && pum_height > p_ph)
178 pum_height = p_ph;
179 }
180
181 /* don't display when we only have room for one line */
182 if (pum_height < 1 || (pum_height == 1 && size > 1))
183 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000184
Bram Moolenaar4033c552017-09-16 20:54:51 +0200185#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200186 /* If there is a preview window at the above avoid drawing over it. */
187 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000188 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200189 pum_row += above_row;
190 pum_height -= above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000191 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200192#endif
193
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100194 pum_array = array;
195 pum_size = size;
196 pum_compute_size();
197 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000198
Bram Moolenaara5e66212017-09-29 22:42:33 +0200199 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000200#ifdef FEAT_RIGHTLEFT
201 if (curwin->w_p_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200202 col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000203 else
204#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200205 col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000206
Bram Moolenaara5e66212017-09-29 22:42:33 +0200207 /* if there are more items than room we need a scrollbar */
208 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000209 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210 pum_scrollbar = 1;
211 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000212 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000213 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214 pum_scrollbar = 0;
215
216 if (def_width < max_width)
217 def_width = max_width;
218
219 if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000220#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200221 && !curwin->w_p_rl)
222 || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000223#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200224 ))
225 {
226 /* align pum column with "col" */
227 pum_col = col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000228
Bram Moolenaara5e66212017-09-29 22:42:33 +0200229#ifdef FEAT_RIGHTLEFT
230 if (curwin->w_p_rl)
231 pum_width = pum_col - pum_scrollbar + 1;
232 else
233#endif
234 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000235
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100236 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
237 && pum_width > PUM_DEF_WIDTH)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200238 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100239 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200240 if (pum_width < PUM_DEF_WIDTH)
241 pum_width = PUM_DEF_WIDTH;
242 }
243 }
244 else if (Columns < def_width)
245 {
246 /* not enough room, will use what we have */
247#ifdef FEAT_RIGHTLEFT
248 if (curwin->w_p_rl)
249 pum_col = Columns - 1;
250 else
251#endif
252 pum_col = 0;
253 pum_width = Columns - 1;
254 }
255 else
256 {
257 if (max_width > PUM_DEF_WIDTH)
258 max_width = PUM_DEF_WIDTH; /* truncate */
259#ifdef FEAT_RIGHTLEFT
260 if (curwin->w_p_rl)
261 pum_col = max_width - 1;
262 else
263#endif
264 pum_col = Columns - max_width;
265 pum_width = max_width - pum_scrollbar;
266 }
267
Bram Moolenaara5e66212017-09-29 22:42:33 +0200268 /* Set selected item and redraw. If the window size changed need to
269 * redo the positioning. Limit this to two times, when there is not
270 * much room the window size will keep changing. */
271 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000272}
273
274/*
275 * Redraw the popup menu, using "pum_first" and "pum_selected".
276 */
277 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100278pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000279{
280 int row = pum_row;
281 int col;
282 int attr_norm = highlight_attr[HLF_PNI];
283 int attr_select = highlight_attr[HLF_PSI];
284 int attr_scroll = highlight_attr[HLF_PSB];
285 int attr_thumb = highlight_attr[HLF_PST];
286 int attr;
287 int i;
288 int idx;
289 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000290 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000291 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000292 int thumb_pos = 0;
293 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000294 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000295 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000296
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100297 /* Never display more than we have */
298 if (pum_first > pum_size - pum_height)
299 pum_first = pum_size - pum_height;
300
Bram Moolenaar4b779472005-10-04 09:12:31 +0000301 if (pum_scrollbar)
302 {
303 thumb_heigth = pum_height * pum_height / pum_size;
304 if (thumb_heigth == 0)
305 thumb_heigth = 1;
306 thumb_pos = (pum_first * (pum_height - thumb_heigth)
307 + (pum_size - pum_height) / 2)
308 / (pum_size - pum_height);
309 }
310
311 for (i = 0; i < pum_height; ++i)
312 {
313 idx = i + pum_first;
314 attr = (idx == pum_selected) ? attr_select : attr_norm;
315
316 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000317#ifdef FEAT_RIGHTLEFT
318 if (curwin->w_p_rl)
319 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200320 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000321 screen_putchar(' ', row, pum_col + 1, attr);
322 }
323 else
324#endif
325 if (pum_col > 0)
326 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000327
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000328 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000329 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000330 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000331 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000332 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000333 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000334 width = 0;
335 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000336 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000337 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000338 case 1: p = pum_array[idx].pum_text; break;
339 case 2: p = pum_array[idx].pum_kind; break;
340 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000341 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000342 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100343 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000344 {
345 if (s == NULL)
346 s = p;
347 w = ptr2cells(p);
348 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
349 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000350 /* Display the text that fits or comes before a Tab.
351 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000352 char_u *st;
353 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000354
355 *p = NUL;
356 st = transstr(s);
357 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000358#ifdef FEAT_RIGHTLEFT
359 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000360 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000361 if (st != NULL)
362 {
363 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000364
365 if (rt != NULL)
366 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100367 char_u *rt_start = rt;
368 int size;
369
370 size = vim_strsize(rt);
371 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000372 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100373 do
374 {
375 size -= has_mbyte
376 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100377 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100378 } while (size > pum_width);
379
380 if (size < pum_width)
381 {
382 /* Most left character requires
383 * 2-cells but only 1 cell is
384 * available on screen. Put a
385 * '<' on the left of the pum
386 * item */
387 *(--rt) = '<';
388 size++;
389 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000390 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100391 screen_puts_len(rt, (int)STRLEN(rt),
392 row, col - size + 1, attr);
393 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000394 }
395 vim_free(st);
396 }
397 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000398 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000399 else
400#endif
401 {
402 if (st != NULL)
403 {
404 screen_puts_len(st, (int)STRLEN(st), row, col,
405 attr);
406 vim_free(st);
407 }
408 col += width;
409 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000410
411 if (*p != TAB)
412 break;
413
414 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000415#ifdef FEAT_RIGHTLEFT
416 if (curwin->w_p_rl)
417 {
418 screen_puts_len((char_u *)" ", 2, row, col - 1,
419 attr);
420 col -= 2;
421 }
422 else
423#endif
424 {
425 screen_puts_len((char_u *)" ", 2, row, col, attr);
426 col += 2;
427 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000428 totwidth += 2;
429 s = NULL; /* start text at next char */
430 width = 0;
431 }
432 else
433 width += w;
434 }
435
436 if (round > 1)
437 n = pum_kind_width + 1;
438 else
439 n = 1;
440
441 /* Stop when there is nothing more to display. */
442 if (round == 3
443 || (round == 2 && pum_array[idx].pum_extra == NULL)
444 || (round == 1 && pum_array[idx].pum_kind == NULL
445 && pum_array[idx].pum_extra == NULL)
446 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000447 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000448#ifdef FEAT_RIGHTLEFT
449 if (curwin->w_p_rl)
450 {
451 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
452 col + 1, ' ', ' ', attr);
453 col = pum_col - pum_base_width - n + 1;
454 }
455 else
456#endif
457 {
458 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000459 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000460 col = pum_col + pum_base_width + n;
461 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000462 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000463 }
464
Bram Moolenaarabc97732007-08-08 20:49:37 +0000465#ifdef FEAT_RIGHTLEFT
466 if (curwin->w_p_rl)
467 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
468 ' ', attr);
469 else
470#endif
471 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
472 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000473 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000474 {
475#ifdef FEAT_RIGHTLEFT
476 if (curwin->w_p_rl)
477 screen_putchar(' ', row, pum_col - pum_width,
478 i >= thumb_pos && i < thumb_pos + thumb_heigth
Bram Moolenaar4b779472005-10-04 09:12:31 +0000479 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000480 else
481#endif
482 screen_putchar(' ', row, pum_col + pum_width,
483 i >= thumb_pos && i < thumb_pos + thumb_heigth
484 ? attr_thumb : attr_scroll);
485 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000486
487 ++row;
488 }
489}
490
Bram Moolenaar4b779472005-10-04 09:12:31 +0000491/*
492 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000493 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000494 * This may be repeated when the preview window is used:
495 * "repeat" == 0: open preview window normally
496 * "repeat" == 1: open preview window but don't set the size
497 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000498 * Returns TRUE when the window was resized and the location of the popup menu
499 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000500 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000501 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100502pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000503{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000504 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000505 int context = pum_height / 2;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000506
Bram Moolenaar4b779472005-10-04 09:12:31 +0000507 pum_selected = n;
508
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000509 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000510 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000511 if (pum_first > pum_selected - 4)
512 {
513 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000514 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000515 if (pum_first > pum_selected - 2)
516 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000517 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000518 if (pum_first < 0)
519 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000520 else if (pum_first > pum_selected)
521 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000522 }
523 else
524 pum_first = pum_selected;
525 }
526 else if (pum_first < pum_selected - pum_height + 5)
527 {
528 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000529 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000530 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000531 {
532 pum_first += pum_height - 2;
533 if (pum_first < pum_selected - pum_height + 1)
534 pum_first = pum_selected - pum_height + 1;
535 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000536 else
537 pum_first = pum_selected - pum_height + 1;
538 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000539
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000540 /* Give a few lines of context when possible. */
541 if (context > 3)
542 context = 3;
543 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000544 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000545 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000546 {
547 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000548 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000549 if (pum_first < 0)
550 pum_first = 0;
551 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000552 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000553 {
554 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000555 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000556 }
557 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000558
Bram Moolenaar4033c552017-09-16 20:54:51 +0200559#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000560 /*
561 * Show extra info in the preview window if there is something and
562 * 'completeopt' contains "preview".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000563 * Skip this when tried twice already.
564 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000565 * NOTE: Be very careful not to sync undo!
566 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000568 && Rows > 10
569 && repeat <= 1
570 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000571 {
572 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200573 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000574 int res = OK;
575
Bram Moolenaaree236d02010-10-27 17:11:15 +0200576 /* Open a preview window. 3 lines by default. Prefer
577 * 'previewheight' if set and smaller. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000578 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200579 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
580 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200581 ++RedrawingDisabled;
Bram Moolenaare7d13762015-10-30 14:23:33 +0100582 /* Prevent undo sync here, if an autocommand syncs undo weird
583 * things can happen to the undo tree. */
584 ++no_u_sync;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000585 resized = prepare_tagpreview(FALSE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100586 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200587 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000588 g_do_tagpreview = 0;
589
590 if (curwin->w_p_pvw)
591 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200592 if (!resized
593 && curbuf->b_nwindows == 1
594 && curbuf->b_fname == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000595 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
596 && curbuf->b_p_bh[0] == 'w')
597 {
598 /* Already a "wipeout" buffer, make it empty. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100599 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000600 ml_delete((linenr_T)1, FALSE);
601 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000602 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000603 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000604 /* Don't want to sync undo in the current buffer. */
605 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000606 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000607 --no_u_sync;
608 if (res == OK)
609 {
610 /* Edit a new, empty buffer. Set options for a "wipeout"
611 * buffer. */
612 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
613 set_option_value((char_u *)"bt", 0L,
614 (char_u *)"nofile", OPT_LOCAL);
615 set_option_value((char_u *)"bh", 0L,
616 (char_u *)"wipe", OPT_LOCAL);
617 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000618 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000619 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000620 }
621 if (res == OK)
622 {
623 char_u *p, *e;
624 linenr_T lnum = 0;
625
626 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
627 {
628 e = vim_strchr(p, '\n');
629 if (e == NULL)
630 {
631 ml_append(lnum++, p, 0, FALSE);
632 break;
633 }
634 else
635 {
636 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000637 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000638 *e = '\n';
639 p = e + 1;
640 }
641 }
642
643 /* Increase the height of the preview window to show the
644 * text, but no more than 'previewheight' lines. */
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000645 if (repeat == 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000646 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000647 if (lnum > p_pvh)
648 lnum = p_pvh;
649 if (curwin->w_height < lnum)
650 {
651 win_setheight((int)lnum);
652 resized = TRUE;
653 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000654 }
655
656 curbuf->b_changed = 0;
657 curbuf->b_p_ma = FALSE;
Bram Moolenaar9c27fc32010-03-17 14:48:24 +0100658 curwin->w_cursor.lnum = 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000659 curwin->w_cursor.col = 0;
660
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200661 if ((curwin != curwin_save && win_valid(curwin_save))
662 || (curtab != curtab_save
663 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000664 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200665 if (curtab != curtab_save && valid_tabpage(curtab_save))
666 goto_tabpage_tp(curtab_save, FALSE, FALSE);
667
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200668 /* When the first completion is done and the preview
669 * window is not resized, skip the preview window's
670 * status line redrawing. */
671 if (ins_compl_active() && !resized)
672 curwin->w_redr_status = FALSE;
673
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000674 /* Return cursor to where we were */
675 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000676 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000677
678 /* When the preview window was resized we need to
679 * update the view on the buffer. Only go back to
680 * the window when needed, otherwise it will always be
681 * redraw. */
682 if (resized)
683 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100684 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000685 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100686 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000687 update_topline();
688 }
689
690 /* Update the screen before drawing the popup menu.
691 * Enable updating the status lines. */
692 pum_do_redraw = TRUE;
693 update_screen(0);
694 pum_do_redraw = FALSE;
695
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000696 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100697 {
698 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000699 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100700 --no_u_sync;
701 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000702
703 /* May need to update the screen again when there are
704 * autocommands involved. */
705 pum_do_redraw = TRUE;
706 update_screen(0);
707 pum_do_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000708 }
709 }
710 }
711 }
712#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000713 }
714
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000715 if (!resized)
716 pum_redraw();
717
718 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000719}
720
721/*
722 * Undisplay the popup menu (later).
723 */
724 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100725pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000726{
727 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000728 redraw_all_later(SOME_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000729 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000730 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000731}
732
733/*
734 * Clear the popup menu. Currently only resets the offset to the first
735 * displayed item.
736 */
737 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100738pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000739{
740 pum_first = 0;
741}
742
743/*
744 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000745 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000746 */
747 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100748pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000749{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000750 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000751}
752
Bram Moolenaare3226be2005-12-18 22:10:00 +0000753/*
754 * Return the height of the popup menu, the number of entries visible.
755 * Only valid when pum_visible() returns TRUE!
756 */
757 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100758pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000759{
760 return pum_height;
761}
762
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100763# if defined(FEAT_BEVALTERM) || defined(PROTO)
764static pumitem_T *balloon_array = NULL;
765static int balloon_arraysize;
766static int balloon_mouse_row = 0;
767static int balloon_mouse_col = 0;
768
769#define BALLOON_MIN_WIDTH 40
770#define BALLOON_MIN_HEIGHT 10
771
772 void
773ui_remove_balloon(void)
774{
775 if (balloon_array != NULL)
776 {
777 pum_undisplay();
778 while (balloon_arraysize > 0)
779 vim_free(balloon_array[--balloon_arraysize].pum_text);
780 vim_free(balloon_array);
781 balloon_array = NULL;
782 }
783}
784
785/*
786 * Terminal version of a balloon, uses the popup menu code.
787 */
788 void
789ui_post_balloon(char_u *mesg)
790{
791 ui_remove_balloon();
792
793 /* TODO: split the text in multiple lines. */
794 balloon_arraysize = 3;
795 balloon_array = (pumitem_T *)alloc_clear(
796 (unsigned)sizeof(pumitem_T) * balloon_arraysize);
797 if (balloon_array != NULL)
798 {
799 /* Add an empty line above and below, looks better. */
800 balloon_array[0].pum_text = vim_strsave((char_u *)"");
801 balloon_array[1].pum_text = vim_strsave(mesg);
802 balloon_array[2].pum_text = vim_strsave((char_u *)"");
803
804 pum_array = balloon_array;
805 pum_size = balloon_arraysize;
806 pum_compute_size();
807 pum_scrollbar = 0;
808 pum_height = balloon_arraysize;
809
810 if (Rows - mouse_row > BALLOON_MIN_HEIGHT)
811 {
812 /* Enough space below the mouse row. */
813 pum_row = mouse_row + 1;
814 if (pum_height > Rows - pum_row)
815 pum_height = Rows - pum_row;
816 }
817 else
818 {
819 /* Show above the mouse row, reduce height if it does not fit. */
820 pum_row = mouse_row - 1 - pum_size;
821 if (pum_row < 0)
822 {
823 pum_height += pum_row;
824 pum_row = 0;
825 }
826 }
827 if (Columns - mouse_col >= pum_base_width
828 || Columns - mouse_col > BALLOON_MIN_WIDTH)
829 /* Enough space to show at mouse column. */
830 pum_col = mouse_col;
831 else
832 /* Not enough space, right align with window. */
833 pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH
834 ? BALLOON_MIN_WIDTH : pum_base_width);
835
836 pum_width = Columns - pum_col;
837 if (pum_width > pum_base_width + 1)
838 pum_width = pum_base_width + 1;
839
840 pum_selected = -1;
841 pum_first = 0;
842 pum_redraw();
843 }
844}
845
846/*
847 * Called when the mouse moved, may remove any displayed balloon.
848 */
849 void
850ui_may_remove_balloon(void)
851{
852 if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col)
853 ui_remove_balloon();
854}
855# endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000856#endif