blob: 5126e8647ba358fd182031f8f63994a717d55f28 [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/*
11 * popupmenu.c: Popup menu (PUM)
12 */
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
33static int pum_set_selected __ARGS((int n));
34
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 Moolenaar96d2c5b2006-03-11 21:27:59 +000061
62redo:
63 def_width = PUM_DEF_WIDTH;
64 max_width = 0;
65 kind_width = 0;
66 extra_width = 0;
67
Bram Moolenaar1e607892006-03-13 22:15:53 +000068 /* Pretend the pum is already there to avoid that must_redraw is set when
69 * 'cuc' is on. */
70 pum_array = (pumitem_T *)1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000071 validate_cursor_col();
Bram Moolenaar1e607892006-03-13 22:15:53 +000072 pum_array = NULL;
73
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000074 row = curwin->w_cline_row + W_WINROW(curwin);
75 height = curwin->w_cline_height;
76 col = curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol;
77
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
83 /*
84 * Figure out the size and position of the pum.
85 */
86 if (size < PUM_DEF_HEIGHT)
87 pum_height = size;
88 else
89 pum_height = PUM_DEF_HEIGHT;
90
91 /* Put the pum below "row" if possible. If there are few lines decide on
92 * where there is more room. */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093 if (row >= cmdline_row - top_clear - pum_height
94 && row > (cmdline_row - top_clear - height) / 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +000095 {
96 /* pum above "row" */
97 if (row >= size)
98 {
99 pum_row = row - size;
100 pum_height = size;
101 }
102 else
103 {
104 pum_row = 0;
105 pum_height = row;
106 }
107 }
108 else
109 {
110 /* pum below "row" */
111 pum_row = row + height;
112 if (size > cmdline_row - pum_row)
113 pum_height = cmdline_row - pum_row;
114 else
115 pum_height = size;
116 }
117
118 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +0000119 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000120 return;
121
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000122 /* If there is a preview window at the top avoid drawing over it. */
123 if (firstwin->w_p_pvw
124 && pum_row < firstwin->w_height
125 && pum_height > firstwin->w_height + 4)
126 {
127 pum_row += firstwin->w_height;
128 pum_height -= firstwin->w_height;
129 }
130
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000131 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000132 for (i = 0; i < size; ++i)
133 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000134 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000135 if (max_width < w)
136 max_width = w;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000137 if (array[i].pum_kind != NULL)
138 {
139 w = vim_strsize(array[i].pum_kind) + 1;
140 if (kind_width < w)
141 kind_width = w;
142 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000143 if (array[i].pum_extra != NULL)
144 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000145 w = vim_strsize(array[i].pum_extra) + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146 if (extra_width < w)
147 extra_width = w;
148 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000149 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000150 pum_base_width = max_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000151 pum_kind_width = kind_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000152
153 /* if there are more items than room we need a scrollbar */
154 if (pum_height < size)
155 {
156 pum_scrollbar = 1;
157 ++max_width;
158 }
159 else
160 pum_scrollbar = 0;
161
162 if (def_width < max_width)
163 def_width = max_width;
164
165 if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
166 {
167 /* align pum column with "col" */
168 pum_col = col;
169 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000170 if (pum_width > max_width + kind_width + extra_width + 1
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000171 && pum_width > PUM_DEF_WIDTH)
172 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000173 pum_width = max_width + kind_width + extra_width + 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000174 if (pum_width < PUM_DEF_WIDTH)
175 pum_width = PUM_DEF_WIDTH;
176 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000177 }
178 else if (Columns < def_width)
179 {
180 /* not enough room, will use what we have */
181 pum_col = 0;
182 pum_width = Columns - 1;
183 }
184 else
185 {
186 if (max_width > PUM_DEF_WIDTH)
187 max_width = PUM_DEF_WIDTH; /* truncate */
188 pum_col = Columns - max_width;
189 pum_width = max_width - pum_scrollbar;
190 }
191
192 pum_array = array;
193 pum_size = size;
194
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000195 /* Set selected item and redraw. If the window size changed need to redo
196 * the positioning. */
197 if (pum_set_selected(selected))
198 goto redo;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000199}
200
201/*
202 * Redraw the popup menu, using "pum_first" and "pum_selected".
203 */
204 void
205pum_redraw()
206{
207 int row = pum_row;
208 int col;
209 int attr_norm = highlight_attr[HLF_PNI];
210 int attr_select = highlight_attr[HLF_PSI];
211 int attr_scroll = highlight_attr[HLF_PSB];
212 int attr_thumb = highlight_attr[HLF_PST];
213 int attr;
214 int i;
215 int idx;
216 char_u *s;
217 char_u *p;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000218 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000219 int thumb_pos = 0;
220 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000221 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000222 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000223
224 if (pum_scrollbar)
225 {
226 thumb_heigth = pum_height * pum_height / pum_size;
227 if (thumb_heigth == 0)
228 thumb_heigth = 1;
229 thumb_pos = (pum_first * (pum_height - thumb_heigth)
230 + (pum_size - pum_height) / 2)
231 / (pum_size - pum_height);
232 }
233
234 for (i = 0; i < pum_height; ++i)
235 {
236 idx = i + pum_first;
237 attr = (idx == pum_selected) ? attr_select : attr_norm;
238
239 /* prepend a space if there is room */
240 if (pum_col > 0)
241 screen_putchar(' ', row, pum_col - 1, attr);
242
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000243 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000244 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000245 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000246 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000247 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000248 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000249 width = 0;
250 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000251 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000252 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000253 case 1: p = pum_array[idx].pum_text; break;
254 case 2: p = pum_array[idx].pum_kind; break;
255 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000256 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000257 if (p != NULL)
258 for ( ; ; mb_ptr_adv(p))
259 {
260 if (s == NULL)
261 s = p;
262 w = ptr2cells(p);
263 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
264 {
265 /* Display the text that fits or comes before a Tab. */
266 screen_puts_len(s, p - s, row, col, attr);
267 col += width;
268
269 if (*p != TAB)
270 break;
271
272 /* Display two spaces for a Tab. */
273 screen_puts_len((char_u *)" ", 2, row, col, attr);
274 col += 2;
275 totwidth += 2;
276 s = NULL; /* start text at next char */
277 width = 0;
278 }
279 else
280 width += w;
281 }
282
283 if (round > 1)
284 n = pum_kind_width + 1;
285 else
286 n = 1;
287
288 /* Stop when there is nothing more to display. */
289 if (round == 3
290 || (round == 2 && pum_array[idx].pum_extra == NULL)
291 || (round == 1 && pum_array[idx].pum_kind == NULL
292 && pum_array[idx].pum_extra == NULL)
293 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000294 break;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000295 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000296 ' ', ' ', attr);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000297 col = pum_col + pum_base_width + n;
298 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000299 }
300
301 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
302 if (pum_scrollbar > 0)
303 screen_putchar(' ', row, pum_col + pum_width,
304 i >= thumb_pos && i < thumb_pos + thumb_heigth
305 ? attr_thumb : attr_scroll);
306
307 ++row;
308 }
309}
310
311#if 0 /* not used yet */
312/*
313 * Return the index of the currently selected item.
314 */
315 int
316pum_get_selected()
317{
318 return pum_selected;
319}
320#endif
321
322/*
323 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000324 * necessary. When "n" is out of range don't scroll.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000325 * Returns TRUE when the window was resized and the location of the popup menu
326 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000327 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000328 static int
Bram Moolenaar4b779472005-10-04 09:12:31 +0000329pum_set_selected(n)
330 int n;
331{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000332 int resized = FALSE;
333
Bram Moolenaar4b779472005-10-04 09:12:31 +0000334 pum_selected = n;
335
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000336 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000337 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000338 if (pum_first > pum_selected - 4)
339 {
340 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000341 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000342 if (pum_first > pum_selected - 2)
343 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000344 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000345 if (pum_first < 0)
346 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000347 else if (pum_first > pum_selected)
348 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000349 }
350 else
351 pum_first = pum_selected;
352 }
353 else if (pum_first < pum_selected - pum_height + 5)
354 {
355 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000356 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000357 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000358 {
359 pum_first += pum_height - 2;
360 if (pum_first < pum_selected - pum_height + 1)
361 pum_first = pum_selected - pum_height + 1;
362 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000363 else
364 pum_first = pum_selected - pum_height + 1;
365 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000366
367 if (pum_height > 6)
368 {
369 /* Give three lines of context when possible. */
370 if (pum_first > pum_selected - 3)
371 {
372 /* scroll down */
373 pum_first = pum_selected - 3;
374 if (pum_first < 0)
375 pum_first = 0;
376 }
377 else if (pum_first < pum_selected + 3 - pum_height + 1)
378 {
379 /* scroll up */
380 pum_first = pum_selected + 3 - pum_height + 1;
381 }
382 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000383
384#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
385 /* Show extra info in the preview window if there is something and
386 * 'completeopt' contains "preview". */
387 if (pum_array[pum_selected].pum_info != NULL
388 && vim_strchr(p_cot, 'p') != NULL)
389 {
390 win_T *curwin_save = curwin;
391 int res = OK;
392
393 /* Open a preview window. 3 lines by default. */
394 g_do_tagpreview = 3;
395 resized = prepare_tagpreview();
396 g_do_tagpreview = 0;
397
398 if (curwin->w_p_pvw)
399 {
400 if (curbuf->b_fname == NULL
401 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
402 && curbuf->b_p_bh[0] == 'w')
403 {
404 /* Already a "wipeout" buffer, make it empty. */
405 while (!bufempty())
406 ml_delete((linenr_T)1, FALSE);
407 }
408 else if ((res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0))
409 == OK)
410 {
411 /* Edit a new, empty buffer. Set options for a "wipeout"
412 * buffer. */
413 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
414 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile",
415 OPT_LOCAL);
416 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe",
417 OPT_LOCAL);
418 set_option_value((char_u *)"diff", 0L, (char_u *)"",
419 OPT_LOCAL);
420 }
421 if (res == OK)
422 {
423 char_u *p, *e;
424 linenr_T lnum = 0;
425
426 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
427 {
428 e = vim_strchr(p, '\n');
429 if (e == NULL)
430 {
431 ml_append(lnum++, p, 0, FALSE);
432 break;
433 }
434 else
435 {
436 *e = NUL;
437 ml_append(lnum++, p, e - p + 1, FALSE);
438 *e = '\n';
439 p = e + 1;
440 }
441 }
442
443 /* Increase the height of the preview window to show the
444 * text, but no more than 'previewheight' lines. */
445 if (lnum > p_pvh)
446 lnum = p_pvh;
447 if (curwin->w_height < lnum)
448 {
449 win_setheight((int)lnum);
450 resized = TRUE;
451 }
452
453 curbuf->b_changed = 0;
454 curbuf->b_p_ma = FALSE;
455 curwin->w_cursor.lnum = 0;
456 curwin->w_cursor.col = 0;
457
458 if (curwin != curwin_save && win_valid(curwin_save))
459 {
460 /* Return cursor to where we were */
461 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000462 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000463
464 /* When the preview window was resized we need to
465 * update the view on the buffer. Only go back to
466 * the window when needed, otherwise it will always be
467 * redraw. */
468 if (resized)
469 {
470 win_enter(curwin_save, TRUE);
471 update_topline();
472 }
473
474 /* Update the screen before drawing the popup menu.
475 * Enable updating the status lines. */
476 pum_do_redraw = TRUE;
477 update_screen(0);
478 pum_do_redraw = FALSE;
479
480 if (win_valid(curwin_save))
481 win_enter(curwin_save, TRUE);
482 }
483 }
484 }
485 }
486#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000487 }
488
489 /* Never display more than we have */
490 if (pum_first > pum_size - pum_height)
491 pum_first = pum_size - pum_height;
492
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000493 if (!resized)
494 pum_redraw();
495
496 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000497}
498
499/*
500 * Undisplay the popup menu (later).
501 */
502 void
503pum_undisplay()
504{
505 pum_array = NULL;
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000506 redraw_all_later(SOME_VALID);
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000507 status_redraw_all();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000508}
509
510/*
511 * Clear the popup menu. Currently only resets the offset to the first
512 * displayed item.
513 */
514 void
515pum_clear()
516{
517 pum_first = 0;
518}
519
520/*
521 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000522 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000523 */
524 int
525pum_visible()
526{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000527 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000528}
529
Bram Moolenaare3226be2005-12-18 22:10:00 +0000530/*
531 * Return the height of the popup menu, the number of entries visible.
532 * Only valid when pum_visible() returns TRUE!
533 */
534 int
535pum_get_height()
536{
537 return pum_height;
538}
539
Bram Moolenaar4b779472005-10-04 09:12:31 +0000540#endif