blob: 26c4ae77f3f16466b81deaac3de7064fc951dc2a [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 {
240 if (pum_first > pum_selected)
241 /* scroll down */
242 pum_first = pum_selected;
243 else if (pum_first < pum_selected - pum_height + 1)
244 /* scroll up */
245 pum_first = pum_selected - pum_height + 1;
246
247 if (pum_height > 6)
248 {
249 /* Give three lines of context when possible. */
250 if (pum_first > pum_selected - 3)
251 {
252 /* scroll down */
253 pum_first = pum_selected - 3;
254 if (pum_first < 0)
255 pum_first = 0;
256 }
257 else if (pum_first < pum_selected + 3 - pum_height + 1)
258 {
259 /* scroll up */
260 pum_first = pum_selected + 3 - pum_height + 1;
261 }
262 }
263 }
264
265 /* Never display more than we have */
266 if (pum_first > pum_size - pum_height)
267 pum_first = pum_size - pum_height;
268
269 pum_redraw();
270}
271
272/*
273 * Undisplay the popup menu (later).
274 */
275 void
276pum_undisplay()
277{
278 pum_array = NULL;
279 redraw_all_later(NOT_VALID);
280}
281
282/*
283 * Clear the popup menu. Currently only resets the offset to the first
284 * displayed item.
285 */
286 void
287pum_clear()
288{
289 pum_first = 0;
290}
291
292/*
293 * Return TRUE if the popup menu is displayed.
294 */
295 int
296pum_visible()
297{
298 return pum_array != NULL;
299}
300
301#endif