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