800
|
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 * popupmnu.c: Popup menu (PUM)
|
|
12 */
|
|
13 #include "vim.h"
|
|
14
|
|
15 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
|
|
16
|
|
17 static pumitem_T *pum_array = NULL; /* items of displayed pum */
|
|
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 */
|
|
24 static int pum_base_width; /* width of pum items base */
|
|
25 static int pum_kind_width; /* width of pum items kind column */
|
|
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
|
|
31 static int pum_do_redraw = FALSE; /* do redraw anyway */
|
|
32
|
|
33 static int pum_set_selected __ARGS((int n));
|
|
34
|
|
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
|
|
45 pum_display(array, size, selected)
|
|
46 pumitem_T *array;
|
|
47 int size;
|
|
48 int selected; /* index of initially selected item, none if
|
|
49 out of range */
|
|
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;
|
|
58 int row;
|
|
59 int height;
|
|
60 int col;
|
|
61 int above_row = cmdline_row;
|
|
62
|
|
63 redo:
|
|
64 def_width = PUM_DEF_WIDTH;
|
|
65 max_width = 0;
|
|
66 kind_width = 0;
|
|
67 extra_width = 0;
|
|
68
|
|
69 /* Pretend the pum is already there to avoid that must_redraw is set when
|
|
70 * 'cuc' is on. */
|
|
71 pum_array = (pumitem_T *)1;
|
|
72 validate_cursor_col();
|
|
73 pum_array = NULL;
|
|
74
|
|
75 row = curwin->w_cline_row + W_WINROW(curwin);
|
|
76 height = curwin->w_cline_height;
|
|
77 col = curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol;
|
|
78
|
|
79 if (firstwin->w_p_pvw)
|
|
80 top_clear = firstwin->w_height;
|
|
81 else
|
|
82 top_clear = 0;
|
|
83
|
|
84 /* When the preview window is at the bottom stop just above it. Also
|
|
85 * avoid drawing over the status line so that it's clear there is a window
|
|
86 * boundary. */
|
|
87 if (lastwin->w_p_pvw)
|
|
88 above_row -= lastwin->w_height + lastwin->w_status_height + 1;
|
|
89
|
|
90 /*
|
|
91 * Figure out the size and position of the pum.
|
|
92 */
|
|
93 if (size < PUM_DEF_HEIGHT)
|
|
94 pum_height = size;
|
|
95 else
|
|
96 pum_height = PUM_DEF_HEIGHT;
|
|
97 if (p_ph > 0 && pum_height > p_ph)
|
|
98 pum_height = p_ph;
|
|
99
|
|
100 /* Put the pum below "row" if possible. If there are few lines decide on
|
|
101 * where there is more room. */
|
|
102 if (row >= above_row - pum_height
|
|
103 && row > (above_row - top_clear - height) / 2)
|
|
104 {
|
|
105 /* pum above "row" */
|
|
106 if (row >= size)
|
|
107 {
|
|
108 pum_row = row - size;
|
|
109 pum_height = size;
|
|
110 }
|
|
111 else
|
|
112 {
|
|
113 pum_row = 0;
|
|
114 pum_height = row;
|
|
115 }
|
|
116 if (p_ph > 0 && pum_height > p_ph)
|
|
117 {
|
|
118 pum_row += pum_height - p_ph;
|
|
119 pum_height = p_ph;
|
|
120 }
|
|
121 }
|
|
122 else
|
|
123 {
|
|
124 /* pum below "row" */
|
|
125 pum_row = row + height;
|
|
126 if (size > above_row - pum_row)
|
|
127 pum_height = above_row - pum_row;
|
|
128 else
|
|
129 pum_height = size;
|
|
130 if (p_ph > 0 && pum_height > p_ph)
|
|
131 pum_height = p_ph;
|
|
132 }
|
|
133
|
|
134 /* don't display when we only have room for one line */
|
|
135 if (pum_height < 1 || (pum_height == 1 && size > 1))
|
|
136 return;
|
|
137
|
|
138 /* If there is a preview window at the top avoid drawing over it. */
|
|
139 if (firstwin->w_p_pvw
|
|
140 && pum_row < firstwin->w_height
|
|
141 && pum_height > firstwin->w_height + 4)
|
|
142 {
|
|
143 pum_row += firstwin->w_height;
|
|
144 pum_height -= firstwin->w_height;
|
|
145 }
|
|
146
|
|
147 /* Compute the width of the widest match and the widest extra. */
|
|
148 for (i = 0; i < size; ++i)
|
|
149 {
|
|
150 w = vim_strsize(array[i].pum_text);
|
|
151 if (max_width < w)
|
|
152 max_width = w;
|
|
153 if (array[i].pum_kind != NULL)
|
|
154 {
|
|
155 w = vim_strsize(array[i].pum_kind) + 1;
|
|
156 if (kind_width < w)
|
|
157 kind_width = w;
|
|
158 }
|
|
159 if (array[i].pum_extra != NULL)
|
|
160 {
|
|
161 w = vim_strsize(array[i].pum_extra) + 1;
|
|
162 if (extra_width < w)
|
|
163 extra_width = w;
|
|
164 }
|
|
165 }
|
|
166 pum_base_width = max_width;
|
|
167 pum_kind_width = kind_width;
|
|
168
|
|
169 /* if there are more items than room we need a scrollbar */
|
|
170 if (pum_height < size)
|
|
171 {
|
|
172 pum_scrollbar = 1;
|
|
173 ++max_width;
|
|
174 }
|
|
175 else
|
|
176 pum_scrollbar = 0;
|
|
177
|
|
178 if (def_width < max_width)
|
|
179 def_width = max_width;
|
|
180
|
|
181 if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
|
|
182 {
|
|
183 /* align pum column with "col" */
|
|
184 pum_col = col;
|
|
185 pum_width = Columns - pum_col - pum_scrollbar;
|
|
186 if (pum_width > max_width + kind_width + extra_width + 1
|
|
187 && pum_width > PUM_DEF_WIDTH)
|
|
188 {
|
|
189 pum_width = max_width + kind_width + extra_width + 1;
|
|
190 if (pum_width < PUM_DEF_WIDTH)
|
|
191 pum_width = PUM_DEF_WIDTH;
|
|
192 }
|
|
193 }
|
|
194 else if (Columns < def_width)
|
|
195 {
|
|
196 /* not enough room, will use what we have */
|
|
197 pum_col = 0;
|
|
198 pum_width = Columns - 1;
|
|
199 }
|
|
200 else
|
|
201 {
|
|
202 if (max_width > PUM_DEF_WIDTH)
|
|
203 max_width = PUM_DEF_WIDTH; /* truncate */
|
|
204 pum_col = Columns - max_width;
|
|
205 pum_width = max_width - pum_scrollbar;
|
|
206 }
|
|
207
|
|
208 pum_array = array;
|
|
209 pum_size = size;
|
|
210
|
|
211 /* Set selected item and redraw. If the window size changed need to redo
|
|
212 * the positioning. */
|
|
213 if (pum_set_selected(selected))
|
|
214 goto redo;
|
|
215 }
|
|
216
|
|
217 /*
|
|
218 * Redraw the popup menu, using "pum_first" and "pum_selected".
|
|
219 */
|
|
220 void
|
|
221 pum_redraw()
|
|
222 {
|
|
223 int row = pum_row;
|
|
224 int col;
|
|
225 int attr_norm = highlight_attr[HLF_PNI];
|
|
226 int attr_select = highlight_attr[HLF_PSI];
|
|
227 int attr_scroll = highlight_attr[HLF_PSB];
|
|
228 int attr_thumb = highlight_attr[HLF_PST];
|
|
229 int attr;
|
|
230 int i;
|
|
231 int idx;
|
|
232 char_u *s;
|
|
233 char_u *p = NULL;
|
|
234 int totwidth, width, w;
|
|
235 int thumb_pos = 0;
|
|
236 int thumb_heigth = 1;
|
|
237 int round;
|
|
238 int n;
|
|
239
|
|
240 if (pum_scrollbar)
|
|
241 {
|
|
242 thumb_heigth = pum_height * pum_height / pum_size;
|
|
243 if (thumb_heigth == 0)
|
|
244 thumb_heigth = 1;
|
|
245 thumb_pos = (pum_first * (pum_height - thumb_heigth)
|
|
246 + (pum_size - pum_height) / 2)
|
|
247 / (pum_size - pum_height);
|
|
248 }
|
|
249
|
|
250 for (i = 0; i < pum_height; ++i)
|
|
251 {
|
|
252 idx = i + pum_first;
|
|
253 attr = (idx == pum_selected) ? attr_select : attr_norm;
|
|
254
|
|
255 /* prepend a space if there is room */
|
|
256 if (pum_col > 0)
|
|
257 screen_putchar(' ', row, pum_col - 1, attr);
|
|
258
|
|
259 /* Display each entry, use two spaces for a Tab.
|
|
260 * Do this 3 times: For the main text, kind and extra info */
|
|
261 col = pum_col;
|
|
262 totwidth = 0;
|
|
263 for (round = 1; round <= 3; ++round)
|
|
264 {
|
|
265 width = 0;
|
|
266 s = NULL;
|
|
267 switch (round)
|
|
268 {
|
|
269 case 1: p = pum_array[idx].pum_text; break;
|
|
270 case 2: p = pum_array[idx].pum_kind; break;
|
|
271 case 3: p = pum_array[idx].pum_extra; break;
|
|
272 }
|
|
273 if (p != NULL)
|
|
274 for ( ; ; mb_ptr_adv(p))
|
|
275 {
|
|
276 if (s == NULL)
|
|
277 s = p;
|
|
278 w = ptr2cells(p);
|
|
279 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
|
|
280 {
|
|
281 /* Display the text that fits or comes before a Tab. */
|
|
282 screen_puts_len(s, p - s, row, col, attr);
|
|
283 col += width;
|
|
284
|
|
285 if (*p != TAB)
|
|
286 break;
|
|
287
|
|
288 /* Display two spaces for a Tab. */
|
|
289 screen_puts_len((char_u *)" ", 2, row, col, attr);
|
|
290 col += 2;
|
|
291 totwidth += 2;
|
|
292 s = NULL; /* start text at next char */
|
|
293 width = 0;
|
|
294 }
|
|
295 else
|
|
296 width += w;
|
|
297 }
|
|
298
|
|
299 if (round > 1)
|
|
300 n = pum_kind_width + 1;
|
|
301 else
|
|
302 n = 1;
|
|
303
|
|
304 /* Stop when there is nothing more to display. */
|
|
305 if (round == 3
|
|
306 || (round == 2 && pum_array[idx].pum_extra == NULL)
|
|
307 || (round == 1 && pum_array[idx].pum_kind == NULL
|
|
308 && pum_array[idx].pum_extra == NULL)
|
|
309 || pum_base_width + n >= pum_width)
|
|
310 break;
|
|
311 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
|
|
312 ' ', ' ', attr);
|
|
313 col = pum_col + pum_base_width + n;
|
|
314 totwidth = pum_base_width + n;
|
|
315 }
|
|
316
|
|
317 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
|
|
318 if (pum_scrollbar > 0)
|
|
319 screen_putchar(' ', row, pum_col + pum_width,
|
|
320 i >= thumb_pos && i < thumb_pos + thumb_heigth
|
|
321 ? attr_thumb : attr_scroll);
|
|
322
|
|
323 ++row;
|
|
324 }
|
|
325 }
|
|
326
|
|
327 #if 0 /* not used yet */
|
|
328 /*
|
|
329 * Return the index of the currently selected item.
|
|
330 */
|
|
331 int
|
|
332 pum_get_selected()
|
|
333 {
|
|
334 return pum_selected;
|
|
335 }
|
|
336 #endif
|
|
337
|
|
338 /*
|
|
339 * Set the index of the currently selected item. The menu will scroll when
|
|
340 * necessary. When "n" is out of range don't scroll.
|
|
341 * Returns TRUE when the window was resized and the location of the popup menu
|
|
342 * must be recomputed.
|
|
343 */
|
|
344 static int
|
|
345 pum_set_selected(n)
|
|
346 int n;
|
|
347 {
|
|
348 int resized = FALSE;
|
|
349 int context = pum_height / 2;
|
|
350
|
|
351 pum_selected = n;
|
|
352
|
|
353 if (pum_selected >= 0 && pum_selected < pum_size)
|
|
354 {
|
|
355 if (pum_first > pum_selected - 4)
|
|
356 {
|
|
357 /* scroll down; when we did a jump it's probably a PageUp then
|
|
358 * scroll a whole page */
|
|
359 if (pum_first > pum_selected - 2)
|
|
360 {
|
|
361 pum_first -= pum_height - 2;
|
|
362 if (pum_first < 0)
|
|
363 pum_first = 0;
|
|
364 else if (pum_first > pum_selected)
|
|
365 pum_first = pum_selected;
|
|
366 }
|
|
367 else
|
|
368 pum_first = pum_selected;
|
|
369 }
|
|
370 else if (pum_first < pum_selected - pum_height + 5)
|
|
371 {
|
|
372 /* scroll up; when we did a jump it's probably a PageDown then
|
|
373 * scroll a whole page */
|
|
374 if (pum_first < pum_selected - pum_height + 1 + 2)
|
|
375 {
|
|
376 pum_first += pum_height - 2;
|
|
377 if (pum_first < pum_selected - pum_height + 1)
|
|
378 pum_first = pum_selected - pum_height + 1;
|
|
379 }
|
|
380 else
|
|
381 pum_first = pum_selected - pum_height + 1;
|
|
382 }
|
|
383
|
|
384 /* Give a few lines of context when possible. */
|
|
385 if (context > 3)
|
|
386 context = 3;
|
|
387 if (pum_height > 2)
|
|
388 {
|
|
389 if (pum_first > pum_selected - context)
|
|
390 {
|
|
391 /* scroll down */
|
|
392 pum_first = pum_selected - context;
|
|
393 if (pum_first < 0)
|
|
394 pum_first = 0;
|
|
395 }
|
|
396 else if (pum_first < pum_selected + context - pum_height + 1)
|
|
397 {
|
|
398 /* scroll up */
|
|
399 pum_first = pum_selected + context - pum_height + 1;
|
|
400 }
|
|
401 }
|
|
402
|
|
403 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
|
816
|
404 /*
|
|
405 * Show extra info in the preview window if there is something and
|
|
406 * 'completeopt' contains "preview".
|
|
407 * NOTE: Be very careful not to sync undo!
|
|
408 */
|
800
|
409 if (pum_array[pum_selected].pum_info != NULL
|
|
410 && vim_strchr(p_cot, 'p') != NULL)
|
|
411 {
|
|
412 win_T *curwin_save = curwin;
|
|
413 int res = OK;
|
|
414
|
|
415 /* Open a preview window. 3 lines by default. */
|
|
416 g_do_tagpreview = 3;
|
816
|
417 resized = prepare_tagpreview(FALSE);
|
800
|
418 g_do_tagpreview = 0;
|
|
419
|
|
420 if (curwin->w_p_pvw)
|
|
421 {
|
|
422 if (curbuf->b_fname == NULL
|
|
423 && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f'
|
|
424 && curbuf->b_p_bh[0] == 'w')
|
|
425 {
|
|
426 /* Already a "wipeout" buffer, make it empty. */
|
|
427 while (!bufempty())
|
|
428 ml_delete((linenr_T)1, FALSE);
|
|
429 }
|
825
|
430 else
|
800
|
431 {
|
825
|
432 /* Don't want to sync undo in the current buffer. */
|
|
433 ++no_u_sync;
|
|
434 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0);
|
|
435 --no_u_sync;
|
|
436 if (res == OK)
|
|
437 {
|
|
438 /* Edit a new, empty buffer. Set options for a "wipeout"
|
|
439 * buffer. */
|
|
440 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
|
|
441 set_option_value((char_u *)"bt", 0L,
|
|
442 (char_u *)"nofile", OPT_LOCAL);
|
|
443 set_option_value((char_u *)"bh", 0L,
|
|
444 (char_u *)"wipe", OPT_LOCAL);
|
|
445 set_option_value((char_u *)"diff", 0L,
|
|
446 (char_u *)"", OPT_LOCAL);
|
|
447 }
|
800
|
448 }
|
|
449 if (res == OK)
|
|
450 {
|
|
451 char_u *p, *e;
|
|
452 linenr_T lnum = 0;
|
|
453
|
|
454 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
|
|
455 {
|
|
456 e = vim_strchr(p, '\n');
|
|
457 if (e == NULL)
|
|
458 {
|
|
459 ml_append(lnum++, p, 0, FALSE);
|
|
460 break;
|
|
461 }
|
|
462 else
|
|
463 {
|
|
464 *e = NUL;
|
|
465 ml_append(lnum++, p, e - p + 1, FALSE);
|
|
466 *e = '\n';
|
|
467 p = e + 1;
|
|
468 }
|
|
469 }
|
|
470
|
|
471 /* Increase the height of the preview window to show the
|
|
472 * text, but no more than 'previewheight' lines. */
|
|
473 if (lnum > p_pvh)
|
|
474 lnum = p_pvh;
|
|
475 if (curwin->w_height < lnum)
|
|
476 {
|
|
477 win_setheight((int)lnum);
|
|
478 resized = TRUE;
|
|
479 }
|
|
480
|
|
481 curbuf->b_changed = 0;
|
|
482 curbuf->b_p_ma = FALSE;
|
|
483 curwin->w_cursor.lnum = 0;
|
|
484 curwin->w_cursor.col = 0;
|
|
485
|
|
486 if (curwin != curwin_save && win_valid(curwin_save))
|
|
487 {
|
|
488 /* Return cursor to where we were */
|
|
489 validate_cursor();
|
|
490 redraw_later(SOME_VALID);
|
|
491
|
|
492 /* When the preview window was resized we need to
|
|
493 * update the view on the buffer. Only go back to
|
|
494 * the window when needed, otherwise it will always be
|
|
495 * redraw. */
|
|
496 if (resized)
|
|
497 {
|
|
498 win_enter(curwin_save, TRUE);
|
|
499 update_topline();
|
|
500 }
|
|
501
|
|
502 /* Update the screen before drawing the popup menu.
|
|
503 * Enable updating the status lines. */
|
|
504 pum_do_redraw = TRUE;
|
|
505 update_screen(0);
|
|
506 pum_do_redraw = FALSE;
|
|
507
|
816
|
508 if (!resized && win_valid(curwin_save))
|
800
|
509 win_enter(curwin_save, TRUE);
|
|
510
|
|
511 /* May need to update the screen again when there are
|
|
512 * autocommands involved. */
|
|
513 pum_do_redraw = TRUE;
|
|
514 update_screen(0);
|
|
515 pum_do_redraw = FALSE;
|
|
516 }
|
|
517 }
|
|
518 }
|
|
519 }
|
|
520 #endif
|
|
521 }
|
|
522
|
|
523 /* Never display more than we have */
|
|
524 if (pum_first > pum_size - pum_height)
|
|
525 pum_first = pum_size - pum_height;
|
|
526
|
|
527 if (!resized)
|
|
528 pum_redraw();
|
|
529
|
|
530 return resized;
|
|
531 }
|
|
532
|
|
533 /*
|
|
534 * Undisplay the popup menu (later).
|
|
535 */
|
|
536 void
|
|
537 pum_undisplay()
|
|
538 {
|
|
539 pum_array = NULL;
|
|
540 redraw_all_later(SOME_VALID);
|
|
541 status_redraw_all();
|
|
542 }
|
|
543
|
|
544 /*
|
|
545 * Clear the popup menu. Currently only resets the offset to the first
|
|
546 * displayed item.
|
|
547 */
|
|
548 void
|
|
549 pum_clear()
|
|
550 {
|
|
551 pum_first = 0;
|
|
552 }
|
|
553
|
|
554 /*
|
|
555 * Return TRUE if the popup menu is displayed.
|
|
556 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
|
|
557 */
|
|
558 int
|
|
559 pum_visible()
|
|
560 {
|
|
561 return !pum_do_redraw && pum_array != NULL;
|
|
562 }
|
|
563
|
|
564 /*
|
|
565 * Return the height of the popup menu, the number of entries visible.
|
|
566 * Only valid when pum_visible() returns TRUE!
|
|
567 */
|
|
568 int
|
|
569 pum_get_height()
|
|
570 {
|
|
571 return pum_height;
|
|
572 }
|
|
573
|
|
574 #endif
|