blob: ffc9dd0ea8a65401cc91413c76ce466f59983b04 [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;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000043 int selected; /* index of initially selected item, none if
44 out of range */
Bram Moolenaar4b779472005-10-04 09:12:31 +000045 int row;
46 int height;
47 int col;
48{
49 int w;
50 int def_width = PUM_DEF_WIDTH;
51 int max_width = 0;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000052 int extra_width = 0;
Bram Moolenaar4b779472005-10-04 09:12:31 +000053 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 */
Bram Moolenaara6557602006-02-04 22:43:20 +000090 if (pum_height < 1 || (pum_height == 1 && size > 1))
Bram Moolenaar4b779472005-10-04 09:12:31 +000091 return;
92
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000093 /* Compute the width of the widest match and the widest extra. */
Bram Moolenaar4b779472005-10-04 09:12:31 +000094 for (i = 0; i < size; ++i)
95 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000096 w = vim_strsize(array[i].pum_text);
Bram Moolenaar4b779472005-10-04 09:12:31 +000097 if (max_width < w)
98 max_width = w;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000099 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 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000105 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000106 pum_base_width = max_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000107
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;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000125 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 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000132 }
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
158pum_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;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000171 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000172 int thumb_pos = 0;
173 int thumb_heigth = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000174 int round;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000175
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
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000195 /* Display each entry, use two spaces for a Tab.
196 * Do this twice: For the main text and for the extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000197 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000198 totwidth = 0;
199 for (round = 1; round <= 2; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000200 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000201 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))
Bram Moolenaar4b779472005-10-04 09:12:31 +0000205 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000206 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;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000214
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000215 if (*p != TAB)
216 break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000217
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000218 /* 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;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000227 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000228 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;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000235 }
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
252pum_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
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000260 * necessary. When "n" is out of range don't scroll.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000261 */
262 void
263pum_set_selected(n)
264 int n;
265{
266 pum_selected = n;
267
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000268 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000269 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000270 if (pum_first > pum_selected - 4)
271 {
272 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000273 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000274 if (pum_first > pum_selected - 2)
275 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000276 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000277 if (pum_first < 0)
278 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000279 else if (pum_first > pum_selected)
280 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000281 }
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
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000288 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000289 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000290 {
291 pum_first += pum_height - 2;
292 if (pum_first < pum_selected - pum_height + 1)
293 pum_first = pum_selected - pum_height + 1;
294 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000295 else
296 pum_first = pum_selected - pum_height + 1;
297 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000298
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
328pum_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
339pum_clear()
340{
341 pum_first = 0;
342}
343
344/*
345 * Return TRUE if the popup menu is displayed.
346 */
347 int
348pum_visible()
349{
350 return pum_array != NULL;
351}
352
Bram Moolenaare3226be2005-12-18 22:10:00 +0000353/*
354 * Return the height of the popup menu, the number of entries visible.
355 * Only valid when pum_visible() returns TRUE!
356 */
357 int
358pum_get_height()
359{
360 return pum_height;
361}
362
Bram Moolenaar4b779472005-10-04 09:12:31 +0000363#endif