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 */
|
545
|
25 static int pum_scrollbar; /* TRUE when scrollbar present */
|
|
26
|
|
27 static int pum_row; /* top row of pum */
|
|
28 static int pum_col; /* left column of pum */
|
|
29
|
|
30 #define PUM_DEF_HEIGHT 10
|
|
31 #define PUM_DEF_WIDTH 15
|
|
32
|
|
33 /*
|
|
34 * Show the popup menu with items "array[size]".
|
|
35 * "array" must remain valid until pum_undisplay() is called!
|
|
36 * When possible the leftmost character is aligned with screen column "col".
|
|
37 * The menu appears above the screen line "row" or at "row" + "height" - 1.
|
|
38 */
|
|
39 void
|
|
40 pum_display(array, size, selected, row, height, col)
|
659
|
41 pumitem_T *array;
|
545
|
42 int size;
|
716
|
43 int selected; /* index of initially selected item, none if
|
|
44 out of range */
|
545
|
45 int row;
|
|
46 int height;
|
|
47 int col;
|
|
48 {
|
|
49 int w;
|
|
50 int def_width = PUM_DEF_WIDTH;
|
|
51 int max_width = 0;
|
659
|
52 int extra_width = 0;
|
545
|
53 int i;
|
|
54
|
|
55 /*
|
|
56 * Figure out the size and position of the pum.
|
|
57 */
|
|
58 if (size < PUM_DEF_HEIGHT)
|
|
59 pum_height = size;
|
|
60 else
|
|
61 pum_height = PUM_DEF_HEIGHT;
|
|
62
|
|
63 /* Put the pum below "row" if possible. If there are few lines decide on
|
|
64 * where there is more room. */
|
|
65 if (row >= cmdline_row - pum_height && row > (cmdline_row - height) / 2)
|
|
66 {
|
|
67 /* pum above "row" */
|
|
68 if (row >= size)
|
|
69 {
|
|
70 pum_row = row - size;
|
|
71 pum_height = size;
|
|
72 }
|
|
73 else
|
|
74 {
|
|
75 pum_row = 0;
|
|
76 pum_height = row;
|
|
77 }
|
|
78 }
|
|
79 else
|
|
80 {
|
|
81 /* pum below "row" */
|
|
82 pum_row = row + height;
|
|
83 if (size > cmdline_row - pum_row)
|
|
84 pum_height = cmdline_row - pum_row;
|
|
85 else
|
|
86 pum_height = size;
|
|
87 }
|
|
88
|
|
89 /* don't display when we only have room for one line */
|
657
|
90 if (pum_height < 1 || (pum_height == 1 && size > 1))
|
545
|
91 return;
|
|
92
|
659
|
93 /* Compute the width of the widest match and the widest extra. */
|
545
|
94 for (i = 0; i < size; ++i)
|
|
95 {
|
659
|
96 w = vim_strsize(array[i].pum_text);
|
545
|
97 if (max_width < w)
|
|
98 max_width = w;
|
659
|
99 if (array[i].pum_extra != NULL)
|
|
100 {
|
|
101 w = vim_strsize(array[i].pum_extra);
|
|
102 if (extra_width < w)
|
|
103 extra_width = w;
|
|
104 }
|
545
|
105 }
|
659
|
106 pum_base_width = max_width;
|
545
|
107
|
|
108 /* if there are more items than room we need a scrollbar */
|
|
109 if (pum_height < size)
|
|
110 {
|
|
111 pum_scrollbar = 1;
|
|
112 ++max_width;
|
|
113 }
|
|
114 else
|
|
115 pum_scrollbar = 0;
|
|
116
|
|
117 if (def_width < max_width)
|
|
118 def_width = max_width;
|
|
119
|
|
120 if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
|
|
121 {
|
|
122 /* align pum column with "col" */
|
|
123 pum_col = col;
|
|
124 pum_width = Columns - pum_col - pum_scrollbar;
|
659
|
125 if (pum_width > max_width + extra_width + 1
|
|
126 && pum_width > PUM_DEF_WIDTH)
|
|
127 {
|
|
128 pum_width = max_width + extra_width + 1;
|
|
129 if (pum_width < PUM_DEF_WIDTH)
|
|
130 pum_width = PUM_DEF_WIDTH;
|
|
131 }
|
545
|
132 }
|
|
133 else if (Columns < def_width)
|
|
134 {
|
|
135 /* not enough room, will use what we have */
|
|
136 pum_col = 0;
|
|
137 pum_width = Columns - 1;
|
|
138 }
|
|
139 else
|
|
140 {
|
|
141 if (max_width > PUM_DEF_WIDTH)
|
|
142 max_width = PUM_DEF_WIDTH; /* truncate */
|
|
143 pum_col = Columns - max_width;
|
|
144 pum_width = max_width - pum_scrollbar;
|
|
145 }
|
|
146
|
|
147 pum_array = array;
|
|
148 pum_size = size;
|
|
149
|
|
150 /* Set selected item and redraw. */
|
|
151 pum_set_selected(selected);
|
|
152 }
|
|
153
|
|
154 /*
|
|
155 * Redraw the popup menu, using "pum_first" and "pum_selected".
|
|
156 */
|
|
157 void
|
|
158 pum_redraw()
|
|
159 {
|
|
160 int row = pum_row;
|
|
161 int col;
|
|
162 int attr_norm = highlight_attr[HLF_PNI];
|
|
163 int attr_select = highlight_attr[HLF_PSI];
|
|
164 int attr_scroll = highlight_attr[HLF_PSB];
|
|
165 int attr_thumb = highlight_attr[HLF_PST];
|
|
166 int attr;
|
|
167 int i;
|
|
168 int idx;
|
|
169 char_u *s;
|
|
170 char_u *p;
|
659
|
171 int totwidth, width, w;
|
545
|
172 int thumb_pos = 0;
|
|
173 int thumb_heigth = 1;
|
659
|
174 int round;
|
545
|
175
|
|
176 if (pum_scrollbar)
|
|
177 {
|
|
178 thumb_heigth = pum_height * pum_height / pum_size;
|
|
179 if (thumb_heigth == 0)
|
|
180 thumb_heigth = 1;
|
|
181 thumb_pos = (pum_first * (pum_height - thumb_heigth)
|
|
182 + (pum_size - pum_height) / 2)
|
|
183 / (pum_size - pum_height);
|
|
184 }
|
|
185
|
|
186 for (i = 0; i < pum_height; ++i)
|
|
187 {
|
|
188 idx = i + pum_first;
|
|
189 attr = (idx == pum_selected) ? attr_select : attr_norm;
|
|
190
|
|
191 /* prepend a space if there is room */
|
|
192 if (pum_col > 0)
|
|
193 screen_putchar(' ', row, pum_col - 1, attr);
|
|
194
|
659
|
195 /* Display each entry, use two spaces for a Tab.
|
|
196 * Do this twice: For the main text and for the extra info */
|
545
|
197 col = pum_col;
|
659
|
198 totwidth = 0;
|
|
199 for (round = 1; round <= 2; ++round)
|
545
|
200 {
|
659
|
201 width = 0;
|
|
202 s = NULL;
|
|
203 for (p = round == 1 ? pum_array[idx].pum_text
|
|
204 : pum_array[idx].pum_extra; ; mb_ptr_adv(p))
|
545
|
205 {
|
659
|
206 if (s == NULL)
|
|
207 s = p;
|
|
208 w = ptr2cells(p);
|
|
209 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
|
|
210 {
|
|
211 /* Display the text that fits or comes before a Tab. */
|
|
212 screen_puts_len(s, p - s, row, col, attr);
|
|
213 col += width;
|
545
|
214
|
659
|
215 if (*p != TAB)
|
|
216 break;
|
545
|
217
|
659
|
218 /* Display two spaces for a Tab. */
|
|
219 screen_puts_len((char_u *)" ", 2, row, col, attr);
|
|
220 col += 2;
|
|
221 totwidth += 2;
|
|
222 s = NULL; /* start text at next char */
|
|
223 width = 0;
|
|
224 }
|
|
225 else
|
|
226 width += w;
|
545
|
227 }
|
659
|
228 if (round == 2 || pum_array[idx].pum_extra == NULL
|
|
229 || pum_base_width + 1 >= pum_width)
|
|
230 break;
|
|
231 screen_fill(row, row + 1, col, pum_col + pum_base_width + 1,
|
|
232 ' ', ' ', attr);
|
|
233 col = pum_col + pum_base_width + 1;
|
|
234 totwidth = pum_base_width + 1;
|
545
|
235 }
|
|
236
|
|
237 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
|
|
238 if (pum_scrollbar > 0)
|
|
239 screen_putchar(' ', row, pum_col + pum_width,
|
|
240 i >= thumb_pos && i < thumb_pos + thumb_heigth
|
|
241 ? attr_thumb : attr_scroll);
|
|
242
|
|
243 ++row;
|
|
244 }
|
|
245 }
|
|
246
|
|
247 #if 0 /* not used yet */
|
|
248 /*
|
|
249 * Return the index of the currently selected item.
|
|
250 */
|
|
251 int
|
|
252 pum_get_selected()
|
|
253 {
|
|
254 return pum_selected;
|
|
255 }
|
|
256 #endif
|
|
257
|
|
258 /*
|
|
259 * Set the index of the currently selected item. The menu will scroll when
|
716
|
260 * necessary. When "n" is out of range don't scroll.
|
545
|
261 */
|
|
262 void
|
|
263 pum_set_selected(n)
|
|
264 int n;
|
|
265 {
|
|
266 pum_selected = n;
|
|
267
|
716
|
268 if (pum_selected >= 0 && pum_selected < pum_size)
|
545
|
269 {
|
610
|
270 if (pum_first > pum_selected - 4)
|
|
271 {
|
|
272 /* scroll down; when we did a jump it's probably a PageUp then
|
642
|
273 * scroll a whole page */
|
610
|
274 if (pum_first > pum_selected - 2)
|
|
275 {
|
642
|
276 pum_first -= pum_height - 2;
|
610
|
277 if (pum_first < 0)
|
|
278 pum_first = 0;
|
642
|
279 else if (pum_first > pum_selected)
|
|
280 pum_first = pum_selected;
|
610
|
281 }
|
|
282 else
|
|
283 pum_first = pum_selected;
|
|
284 }
|
|
285 else if (pum_first < pum_selected - pum_height + 5)
|
|
286 {
|
|
287 /* scroll up; when we did a jump it's probably a PageDown then
|
642
|
288 * scroll a whole page */
|
610
|
289 if (pum_first < pum_selected - pum_height + 1 + 2)
|
642
|
290 {
|
|
291 pum_first += pum_height - 2;
|
|
292 if (pum_first < pum_selected - pum_height + 1)
|
|
293 pum_first = pum_selected - pum_height + 1;
|
|
294 }
|
610
|
295 else
|
|
296 pum_first = pum_selected - pum_height + 1;
|
|
297 }
|
545
|
298
|
|
299 if (pum_height > 6)
|
|
300 {
|
|
301 /* Give three lines of context when possible. */
|
|
302 if (pum_first > pum_selected - 3)
|
|
303 {
|
|
304 /* scroll down */
|
|
305 pum_first = pum_selected - 3;
|
|
306 if (pum_first < 0)
|
|
307 pum_first = 0;
|
|
308 }
|
|
309 else if (pum_first < pum_selected + 3 - pum_height + 1)
|
|
310 {
|
|
311 /* scroll up */
|
|
312 pum_first = pum_selected + 3 - pum_height + 1;
|
|
313 }
|
|
314 }
|
|
315 }
|
|
316
|
|
317 /* Never display more than we have */
|
|
318 if (pum_first > pum_size - pum_height)
|
|
319 pum_first = pum_size - pum_height;
|
|
320
|
|
321 pum_redraw();
|
|
322 }
|
|
323
|
|
324 /*
|
|
325 * Undisplay the popup menu (later).
|
|
326 */
|
|
327 void
|
|
328 pum_undisplay()
|
|
329 {
|
|
330 pum_array = NULL;
|
|
331 redraw_all_later(NOT_VALID);
|
|
332 }
|
|
333
|
|
334 /*
|
|
335 * Clear the popup menu. Currently only resets the offset to the first
|
|
336 * displayed item.
|
|
337 */
|
|
338 void
|
|
339 pum_clear()
|
|
340 {
|
|
341 pum_first = 0;
|
|
342 }
|
|
343
|
|
344 /*
|
|
345 * Return TRUE if the popup menu is displayed.
|
|
346 */
|
|
347 int
|
|
348 pum_visible()
|
|
349 {
|
|
350 return pum_array != NULL;
|
|
351 }
|
|
352
|
610
|
353 /*
|
|
354 * Return the height of the popup menu, the number of entries visible.
|
|
355 * Only valid when pum_visible() returns TRUE!
|
|
356 */
|
|
357 int
|
|
358 pum_get_height()
|
|
359 {
|
|
360 return pum_height;
|
|
361 }
|
|
362
|
545
|
363 #endif
|