545
|
1 /* 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
|
659
|
17 static pumitem_T *pum_array = NULL; /* items of displayed pum */
|
545
|
18 static int pum_size; /* nr of items in "pum_array" */
|
|
19 static int pum_selected; /* index of selected item or -1 */
|
|
20 static int pum_first = 0; /* index of top item */
|
|
21
|
|
22 static int pum_height; /* nr of displayed pum items */
|
|
23 static int pum_width; /* width of displayed pum items */
|
659
|
24 static int pum_base_width; /* width of pum items base */
|
730
|
25 static int pum_kind_width; /* width of pum items kind column */
|
545
|
26 static int pum_scrollbar; /* TRUE when scrollbar present */
|
|
27
|
|
28 static int pum_row; /* top row of pum */
|
|
29 static int pum_col; /* left column of pum */
|
|
30
|
730
|
31 static int pum_do_redraw = FALSE; /* do redraw anyway */
|
|
32
|
|
33 static int pum_set_selected __ARGS((int n));
|
|
34
|
545
|
35 #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
|
730
|
45 pum_display(array, size, selected)
|
659
|
46 pumitem_T *array;
|
545
|
47 int size;
|
716
|
48 int selected; /* index of initially selected item, none if
|
|
49 out of range */
|
730
|
50 {
|
|
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;
|
545
|
58 int row;
|
|
59 int height;
|
|
60 int col;
|
730
|
61
|
|
62 redo:
|
|
63 def_width = PUM_DEF_WIDTH;
|
|
64 max_width = 0;
|
|
65 kind_width = 0;
|
|
66 extra_width = 0;
|
|
67
|
753
|
68 /* Pretend the pum is already there to avoid that must_redraw is set when
|
|
69 * 'cuc' is on. */
|
|
70 pum_array = (pumitem_T *)1;
|
730
|
71 validate_cursor_col();
|
753
|
72 pum_array = NULL;
|
|
73
|
730
|
74 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;
|
545
|
82
|
|
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. */
|
730
|
93 if (row >= cmdline_row - top_clear - pum_height
|
|
94 && row > (cmdline_row - top_clear - height) / 2)
|
545
|
95 {
|
|
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 */
|
657
|
119 if (pum_height < 1 || (pum_height == 1 && size > 1))
|
545
|
120 return;
|
|
121
|
730
|
122 /* 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
|
659
|
131 /* Compute the width of the widest match and the widest extra. */
|
545
|
132 for (i = 0; i < size; ++i)
|
|
133 {
|
659
|
134 w = vim_strsize(array[i].pum_text);
|
545
|
135 if (max_width < w)
|
|
136 max_width = w;
|
730
|
137 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 }
|
659
|
143 if (array[i].pum_extra != NULL)
|
|
144 {
|
753
|
145 w = vim_strsize(array[i].pum_extra) + 1;
|
659
|
146 if (extra_width < w)
|
|
147 extra_width = w;
|
|
148 }
|
545
|
149 }
|
659
|
150 pum_base_width = max_width;
|
730
|
151 pum_kind_width = kind_width;
|
545
|
152
|
|
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;
|
730
|
170 if (pum_width > max_width + kind_width + extra_width + 1
|
659
|
171 && pum_width > PUM_DEF_WIDTH)
|
|
172 {
|
730
|
173 pum_width = max_width + kind_width + extra_width + 1;
|
659
|
174 if (pum_width < PUM_DEF_WIDTH)
|
|
175 pum_width = PUM_DEF_WIDTH;
|
|
176 }
|
545
|
177 }
|
|
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
|
730
|
195 /* 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;
|
545
|
199 }
|
|
200
|
|
201 /*
|
|
202 * Redraw the popup menu, using "pum_first" and "pum_selected".
|
|
203 */
|
|
204 void
|
|
205 pum_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;
|
659
|
218 int totwidth, width, w;
|
545
|
219 int thumb_pos = 0;
|
|
220 int thumb_heigth = 1;
|
659
|
221 int round;
|
730
|
222 int n;
|
545
|
223
|
|
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
|
659
|
243 /* Display each entry, use two spaces for a Tab.
|
730
|
244 * Do this 3 times: For the main text, kind and extra info */
|
545
|
245 col = pum_col;
|
659
|
246 totwidth = 0;
|
730
|
247 for (round = 1; round <= 3; ++round)
|
545
|
248 {
|
659
|
249 width = 0;
|
|
250 s = NULL;
|
730
|
251 switch (round)
|
545
|
252 {
|
730
|
253 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;
|
|
256 }
|
|
257 if (p != NULL)
|
|
258 for ( ; ; mb_ptr_adv(p))
|
659
|
259 {
|
730
|
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;
|
545
|
268
|
730
|
269 if (*p != TAB)
|
|
270 break;
|
545
|
271
|
730
|
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;
|
659
|
281 }
|
730
|
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)
|
659
|
294 break;
|
730
|
295 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
|
659
|
296 ' ', ' ', attr);
|
730
|
297 col = pum_col + pum_base_width + n;
|
|
298 totwidth = pum_base_width + n;
|
545
|
299 }
|
|
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
|
|
316 pum_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
|
716
|
324 * necessary. When "n" is out of range don't scroll.
|
730
|
325 * Returns TRUE when the window was resized and the location of the popup menu
|
|
326 * must be recomputed.
|
545
|
327 */
|
730
|
328 static int
|
545
|
329 pum_set_selected(n)
|
|
330 int n;
|
|
331 {
|
730
|
332 int resized = FALSE;
|
|
333
|
545
|
334 pum_selected = n;
|
|
335
|
716
|
336 if (pum_selected >= 0 && pum_selected < pum_size)
|
545
|
337 {
|
610
|
338 if (pum_first > pum_selected - 4)
|
|
339 {
|
|
340 /* scroll down; when we did a jump it's probably a PageUp then
|
642
|
341 * scroll a whole page */
|
610
|
342 if (pum_first > pum_selected - 2)
|
|
343 {
|
642
|
344 pum_first -= pum_height - 2;
|
610
|
345 if (pum_first < 0)
|
|
346 pum_first = 0;
|
642
|
347 else if (pum_first > pum_selected)
|
|
348 pum_first = pum_selected;
|
610
|
349 }
|
|
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
|
642
|
356 * scroll a whole page */
|
610
|
357 if (pum_first < pum_selected - pum_height + 1 + 2)
|
642
|
358 {
|
|
359 pum_first += pum_height - 2;
|
|
360 if (pum_first < pum_selected - pum_height + 1)
|
|
361 pum_first = pum_selected - pum_height + 1;
|
|
362 }
|
610
|
363 else
|
|
364 pum_first = pum_selected - pum_height + 1;
|
|
365 }
|
545
|
366
|
|
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 }
|
730
|
383
|
|
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();
|
743
|
462 redraw_later(SOME_VALID);
|
730
|
463
|
|
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
|
545
|
487 }
|
|
488
|
|
489 /* Never display more than we have */
|
|
490 if (pum_first > pum_size - pum_height)
|
|
491 pum_first = pum_size - pum_height;
|
|
492
|
730
|
493 if (!resized)
|
|
494 pum_redraw();
|
|
495
|
|
496 return resized;
|
545
|
497 }
|
|
498
|
|
499 /*
|
|
500 * Undisplay the popup menu (later).
|
|
501 */
|
|
502 void
|
|
503 pum_undisplay()
|
|
504 {
|
|
505 pum_array = NULL;
|
743
|
506 redraw_all_later(SOME_VALID);
|
758
|
507 status_redraw_all();
|
545
|
508 }
|
|
509
|
|
510 /*
|
|
511 * Clear the popup menu. Currently only resets the offset to the first
|
|
512 * displayed item.
|
|
513 */
|
|
514 void
|
|
515 pum_clear()
|
|
516 {
|
|
517 pum_first = 0;
|
|
518 }
|
|
519
|
|
520 /*
|
|
521 * Return TRUE if the popup menu is displayed.
|
730
|
522 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
|
545
|
523 */
|
|
524 int
|
|
525 pum_visible()
|
|
526 {
|
730
|
527 return !pum_do_redraw && pum_array != NULL;
|
545
|
528 }
|
|
529
|
610
|
530 /*
|
|
531 * Return the height of the popup menu, the number of entries visible.
|
|
532 * Only valid when pum_visible() returns TRUE!
|
|
533 */
|
|
534 int
|
|
535 pum_get_height()
|
|
536 {
|
|
537 return pum_height;
|
|
538 }
|
|
539
|
545
|
540 #endif
|