blob: 9d11a45703672d3fb27a4496ccb9b83e08919158 [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 Moolenaar4b779472005-10-04 09:12:31 +000025static int pum_scrollbar; /* TRUE when scrollbar present */
26
27static int pum_row; /* top row of pum */
28static 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
40pum_display(array, size, selected, row, height, col)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000041 pumitem_T *array;
Bram Moolenaar4b779472005-10-04 09:12:31 +000042 int size;
43 int selected; /* index of initially selected item */
44 int row;
45 int height;
46 int col;
47{
48 int w;
49 int def_width = PUM_DEF_WIDTH;
50 int max_width = 0;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000051 int extra_width = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000052 int i;
53
54 /*
55 * Figure out the size and position of the pum.
56 */
57 if (size < PUM_DEF_HEIGHT)
58 pum_height = size;
59 else
60 pum_height = PUM_DEF_HEIGHT;
61
62 /* Put the pum below "row" if possible. If there are few lines decide on
63 * where there is more room. */
64 if (row >= cmdline_row - pum_height && row > (cmdline_row - height) / 2)
65 {
66 /* pum above "row" */
67 if (row >= size)
68 {
69 pum_row = row - size;
70 pum_height = size;
71 }
72 else
73 {
74 pum_row = 0;
75 pum_height = row;
76 }
77 }
78 else
79 {
80 /* pum below "row" */
81 pum_row = row + height;
82 if (size > cmdline_row - pum_row)
83 pum_height = cmdline_row - pum_row;
84 else
85 pum_height = size;
86 }
87
88 /* don't display when we only have room for one line */
Bram Moolenaara6557602006-02-04 22:43:20 +000089 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +000090 return;
91
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000092 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +000093 for (i = 0; i < size; ++i)
94 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000095 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +000096 if (max_width < w)
97 max_width = w;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000098 if (array[i].pum_extra != NULL)
99 {
100 w = vim_strsize(array[i].pum_extra);
101 if (extra_width < w)
102 extra_width = w;
103 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000104 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000105 pum_base_width = max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000106
107 /* if there are more items than room we need a scrollbar */
108 if (pum_height < size)
109 {
110 pum_scrollbar = 1;
111 ++max_width;
112 }
113 else
114 pum_scrollbar = 0;
115
116 if (def_width < max_width)
117 def_width = max_width;
118
119 if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width)
120 {
121 /* align pum column with "col" */
122 pum_col = col;
123 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000124 if (pum_width > max_width + extra_width + 1
125 && pum_width > PUM_DEF_WIDTH)
126 {
127 pum_width = max_width + extra_width + 1;
128 if (pum_width < PUM_DEF_WIDTH)
129 pum_width = PUM_DEF_WIDTH;
130 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000131 }
132 else if (Columns < def_width)
133 {
134 /* not enough room, will use what we have */
135 pum_col = 0;
136 pum_width = Columns - 1;
137 }
138 else
139 {
140 if (max_width > PUM_DEF_WIDTH)
141 max_width = PUM_DEF_WIDTH; /* truncate */
142 pum_col = Columns - max_width;
143 pum_width = max_width - pum_scrollbar;
144 }
145
146 pum_array = array;
147 pum_size = size;
148
149 /* Set selected item and redraw. */
150 pum_set_selected(selected);
151}
152
153/*
154 * Redraw the popup menu, using "pum_first" and "pum_selected".
155 */
156 void
157pum_redraw()
158{
159 int row = pum_row;
160 int col;
161 int attr_norm = highlight_attr[HLF_PNI];
162 int attr_select = highlight_attr[HLF_PSI];
163 int attr_scroll = highlight_attr[HLF_PSB];
164 int attr_thumb = highlight_attr[HLF_PST];
165 int attr;
166 int i;
167 int idx;
168 char_u *s;
169 char_u *p;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000170 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000171 int thumb_pos = 0;
172 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000173 int round;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000174
175 if (pum_scrollbar)
176 {
177 thumb_heigth = pum_height * pum_height / pum_size;
178 if (thumb_heigth == 0)
179 thumb_heigth = 1;
180 thumb_pos = (pum_first * (pum_height - thumb_heigth)
181 + (pum_size - pum_height) / 2)
182 / (pum_size - pum_height);
183 }
184
185 for (i = 0; i < pum_height; ++i)
186 {
187 idx = i + pum_first;
188 attr = (idx == pum_selected) ? attr_select : attr_norm;
189
190 /* prepend a space if there is room */
191 if (pum_col > 0)
192 screen_putchar(' ', row, pum_col - 1, attr);
193
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000194 /* Display each entry, use two spaces for a Tab.
195 * Do this twice: For the main text and for the extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000196 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000197 totwidth = 0;
198 for (round = 1; round <= 2; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000199 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000200 width = 0;
201 s = NULL;
202 for (p = round == 1 ? pum_array[idx].pum_text
203 : pum_array[idx].pum_extra; ; mb_ptr_adv(p))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000204 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000205 if (s == NULL)
206 s = p;
207 w = ptr2cells(p);
208 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
209 {
210 /* Display the text that fits or comes before a Tab. */
211 screen_puts_len(s, p - s, row, col, attr);
212 col += width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000213
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000214 if (*p != TAB)
215 break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000216
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000217 /* Display two spaces for a Tab. */
218 screen_puts_len((char_u *)" ", 2, row, col, attr);
219 col += 2;
220 totwidth += 2;
221 s = NULL; /* start text at next char */
222 width = 0;
223 }
224 else
225 width += w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000226 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000227 if (round == 2 || pum_array[idx].pum_extra == NULL
228 || pum_base_width + 1 >= pum_width)
229 break;
230 screen_fill(row, row + 1, col, pum_col + pum_base_width + 1,
231 ' ', ' ', attr);
232 col = pum_col + pum_base_width + 1;
233 totwidth = pum_base_width + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000234 }
235
236 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr);
237 if (pum_scrollbar > 0)
238 screen_putchar(' ', row, pum_col + pum_width,
239 i >= thumb_pos && i < thumb_pos + thumb_heigth
240 ? attr_thumb : attr_scroll);
241
242 ++row;
243 }
244}
245
246#if 0 /* not used yet */
247/*
248 * Return the index of the currently selected item.
249 */
250 int
251pum_get_selected()
252{
253 return pum_selected;
254}
255#endif
256
257/*
258 * Set the index of the currently selected item. The menu will scroll when
259 * necessary.
260 */
261 void
262pum_set_selected(n)
263 int n;
264{
265 pum_selected = n;
266
267 if (pum_selected >= 0)
268 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000269 if (pum_first > pum_selected - 4)
270 {
271 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000272 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000273 if (pum_first > pum_selected - 2)
274 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000275 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000276 if (pum_first < 0)
277 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000278 else if (pum_first > pum_selected)
279 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000280 }
281 else
282 pum_first = pum_selected;
283 }
284 else if (pum_first < pum_selected - pum_height + 5)
285 {
286 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000287 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000288 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000289 {
290 pum_first += pum_height - 2;
291 if (pum_first < pum_selected - pum_height + 1)
292 pum_first = pum_selected - pum_height + 1;
293 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000294 else
295 pum_first = pum_selected - pum_height + 1;
296 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000297
298 if (pum_height > 6)
299 {
300 /* Give three lines of context when possible. */
301 if (pum_first > pum_selected - 3)
302 {
303 /* scroll down */
304 pum_first = pum_selected - 3;
305 if (pum_first < 0)
306 pum_first = 0;
307 }
308 else if (pum_first < pum_selected + 3 - pum_height + 1)
309 {
310 /* scroll up */
311 pum_first = pum_selected + 3 - pum_height + 1;
312 }
313 }
314 }
315
316 /* Never display more than we have */
317 if (pum_first > pum_size - pum_height)
318 pum_first = pum_size - pum_height;
319
320 pum_redraw();
321}
322
323/*
324 * Undisplay the popup menu (later).
325 */
326 void
327pum_undisplay()
328{
329 pum_array = NULL;
330 redraw_all_later(NOT_VALID);
331}
332
333/*
334 * Clear the popup menu. Currently only resets the offset to the first
335 * displayed item.
336 */
337 void
338pum_clear()
339{
340 pum_first = 0;
341}
342
343/*
344 * Return TRUE if the popup menu is displayed.
345 */
346 int
347pum_visible()
348{
349 return pum_array != NULL;
350}
351
Bram Moolenaare3226be2005-12-18 22:10:00 +0000352/*
353 * Return the height of the popup menu, the number of entries visible.
354 * Only valid when pum_visible() returns TRUE!
355 */
356 int
357pum_get_height()
358{
359 return pum_height;
360}
361
Bram Moolenaar4b779472005-10-04 09:12:31 +0000362#endif