blob: 467d67020b4f61a151f40a4bc47649097322ecff [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar4b779472005-10-04 09:12:31 +00002 *
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/*
Bram Moolenaar76b92b22006-03-24 22:46:53 +000011 * popupmnu.c: Popup menu (PUM)
Bram Moolenaar4b779472005-10-04 09:12:31 +000012 */
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
Bram Moolenaarae654382019-01-17 21:09:05 +010022static int call_update_screen = FALSE;
23
Bram Moolenaar4b779472005-10-04 09:12:31 +000024static int pum_height; /* nr of displayed pum items */
25static int pum_width; /* width of displayed pum items */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000026static int pum_base_width; /* width of pum items base */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000027static int pum_kind_width; /* width of pum items kind column */
Bram Moolenaar51b0f372017-11-18 18:52:04 +010028static int pum_extra_width; /* width of extra stuff */
Bram Moolenaar4b779472005-10-04 09:12:31 +000029static int pum_scrollbar; /* TRUE when scrollbar present */
30
31static int pum_row; /* top row of pum */
32static int pum_col; /* left column of pum */
33
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020034static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020035static int pum_win_row;
36static int pum_win_height;
37static int pum_win_col;
38static int pum_win_wcol;
39static int pum_win_width;
40
Bram Moolenaarae654382019-01-17 21:09:05 +010041static int pum_do_redraw = FALSE; // do redraw anyway
42static int pum_skip_redraw = FALSE; // skip redraw
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000043
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000045
Bram Moolenaar4b779472005-10-04 09:12:31 +000046#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000047
Bram Moolenaar51b0f372017-11-18 18:52:04 +010048 static void
49pum_compute_size(void)
50{
51 int i;
52 int w;
53
54 /* Compute the width of the widest match and the widest extra. */
55 pum_base_width = 0;
56 pum_kind_width = 0;
57 pum_extra_width = 0;
58 for (i = 0; i < pum_size; ++i)
59 {
60 w = vim_strsize(pum_array[i].pum_text);
61 if (pum_base_width < w)
62 pum_base_width = w;
63 if (pum_array[i].pum_kind != NULL)
64 {
65 w = vim_strsize(pum_array[i].pum_kind) + 1;
66 if (pum_kind_width < w)
67 pum_kind_width = w;
68 }
69 if (pum_array[i].pum_extra != NULL)
70 {
71 w = vim_strsize(pum_array[i].pum_extra) + 1;
72 if (pum_extra_width < w)
73 pum_extra_width = w;
74 }
75 }
76}
77
Bram Moolenaar4b779472005-10-04 09:12:31 +000078/*
79 * Show the popup menu with items "array[size]".
80 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010081 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000082 * The menu appears above the screen line "row" or at "row" + "height" - 1.
83 */
84 void
Bram Moolenaar05540972016-01-30 20:31:25 +010085pum_display(
86 pumitem_T *array,
87 int size,
88 int selected) /* index of initially selected item, none if
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000089 out of range */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000090{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000091 int def_width;
92 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000093 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010094 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010095 int above_row;
96 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000097 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020098#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010099 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100100#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000101
Bram Moolenaara5e66212017-09-29 22:42:33 +0200102 do
103 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100104 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200105 above_row = 0;
106 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000107
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 /* Pretend the pum is already there to avoid that must_redraw is set
109 * when 'cuc' is on. */
110 pum_array = (pumitem_T *)1;
111 validate_cursor_col();
112 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000113
Bram Moolenaar491ac282018-06-17 14:47:55 +0200114 // Remember the essential parts of the window position and size, so we
115 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200116 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200117 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
118 pum_win_height = curwin->w_height;
119 pum_win_col = curwin->w_wincol;
120 pum_win_wcol = curwin->w_wcol;
121 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000122
Bram Moolenaar4033c552017-09-16 20:54:51 +0200123#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200124 FOR_ALL_WINDOWS(pvwin)
125 if (pvwin->w_p_pvw)
126 break;
127 if (pvwin != NULL)
128 {
129 if (W_WINROW(pvwin) < W_WINROW(curwin))
130 above_row = W_WINROW(pvwin) + pvwin->w_height;
131 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
132 below_row = W_WINROW(pvwin);
133 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100134#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000135
Bram Moolenaara5e66212017-09-29 22:42:33 +0200136 /*
137 * Figure out the size and position of the pum.
138 */
139 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000140 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000141 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200142 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000143 if (p_ph > 0 && pum_height > p_ph)
144 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000145
Bram Moolenaara750ac22018-09-09 15:27:59 +0200146 /* Put the pum below "pum_win_row" if possible. If there are few lines
147 * decide on where there is more room. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200148 if (pum_win_row + 2 >= below_row - pum_height
149 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200150 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200151 /* pum above "pum_win_row" */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200152
153 /* Leave two lines of context if possible */
154 if (curwin->w_wrow - curwin->w_cline_row >= 2)
155 context_lines = 2;
156 else
157 context_lines = curwin->w_wrow - curwin->w_cline_row;
158
Bram Moolenaar491ac282018-06-17 14:47:55 +0200159 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200160 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200161 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200162 pum_height = size;
163 }
164 else
165 {
166 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200168 }
169 if (p_ph > 0 && pum_height > p_ph)
170 {
171 pum_row += pum_height - p_ph;
172 pum_height = p_ph;
173 }
174 }
175 else
176 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200177 /* pum below "pum_win_row" */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200178
179 /* Leave two lines of context if possible */
180 if (curwin->w_cline_row
181 + curwin->w_cline_height - curwin->w_wrow >= 3)
182 context_lines = 3;
183 else
184 context_lines = curwin->w_cline_row
185 + curwin->w_cline_height - curwin->w_wrow;
186
Bram Moolenaar491ac282018-06-17 14:47:55 +0200187 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200188 if (size > below_row - pum_row)
189 pum_height = below_row - pum_row;
190 else
191 pum_height = size;
192 if (p_ph > 0 && pum_height > p_ph)
193 pum_height = p_ph;
194 }
195
196 /* don't display when we only have room for one line */
197 if (pum_height < 1 || (pum_height == 1 && size > 1))
198 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000199
Bram Moolenaar4033c552017-09-16 20:54:51 +0200200#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100201 // If there is a preview window above avoid drawing over it.
202 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000203 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100204 pum_row = above_row;
205 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000206 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200207#endif
208
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100209 pum_array = array;
210 pum_size = size;
211 pum_compute_size();
212 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000213
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214 /* Calculate column */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000215#ifdef FEAT_RIGHTLEFT
216 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100217 cursor_col = curwin->w_wincol + curwin->w_width
218 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000219 else
220#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100221 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000222
Bram Moolenaara5e66212017-09-29 22:42:33 +0200223 /* if there are more items than room we need a scrollbar */
224 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000225 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200226 pum_scrollbar = 1;
227 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000228 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000229 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200230 pum_scrollbar = 0;
231
232 if (def_width < max_width)
233 def_width = max_width;
234
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100235 if (((cursor_col < Columns - p_pw
236 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000237#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200238 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100239 || (curwin->w_p_rl
240 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000241#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200242 ))
243 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100244 /* align pum with "cursor_col" */
245 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000246
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100247 /* start with the maximum space available */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200248#ifdef FEAT_RIGHTLEFT
249 if (curwin->w_p_rl)
250 pum_width = pum_col - pum_scrollbar + 1;
251 else
252#endif
253 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000254
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100255 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100256 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200257 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100258 /* the width is more than needed for the items, make it
259 * narrower */
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100260 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100261 if (pum_width < p_pw)
262 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200263 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100264 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100265#ifdef FEAT_RIGHTLEFT
266 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100267 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
268 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100269#endif
270 ))
271 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100272 /* align pum edge with "cursor_col" */
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100274 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100275 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100276 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100277 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100278 if (pum_col >= Columns)
279 pum_col = Columns - 1;
280 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100281 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100282#endif
283 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100284 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
285 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100286 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100287 /* use full width to end of the screen */
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100288 pum_col = Columns - max_width - pum_scrollbar;
289 if (pum_col < 0)
290 pum_col = 0;
291 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100292 }
293
294#ifdef FEAT_RIGHTLEFT
295 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100296 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100297 else
298#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100299 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300
Bram Moolenaar42443c72018-02-10 18:28:52 +0100301 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100302 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100303 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304#ifdef FEAT_RIGHTLEFT
305 if (curwin->w_p_rl)
306 {
307 if (pum_width > pum_col)
308 pum_width = pum_col;
309 }
310 else
311#endif
312 {
313 if (pum_width >= Columns - pum_col)
314 pum_width = Columns - pum_col - 1;
315 }
316 }
317 else if (pum_width > max_width + pum_kind_width
318 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100319 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100320 {
321 pum_width = max_width + pum_kind_width
322 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100323 if (pum_width < p_pw)
324 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100325 }
326 }
327
Bram Moolenaara5e66212017-09-29 22:42:33 +0200328 }
329 else if (Columns < def_width)
330 {
331 /* not enough room, will use what we have */
332#ifdef FEAT_RIGHTLEFT
333 if (curwin->w_p_rl)
334 pum_col = Columns - 1;
335 else
336#endif
337 pum_col = 0;
338 pum_width = Columns - 1;
339 }
340 else
341 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100342 if (max_width > p_pw)
343 max_width = p_pw; /* truncate */
Bram Moolenaara5e66212017-09-29 22:42:33 +0200344#ifdef FEAT_RIGHTLEFT
345 if (curwin->w_p_rl)
346 pum_col = max_width - 1;
347 else
348#endif
349 pum_col = Columns - max_width;
350 pum_width = max_width - pum_scrollbar;
351 }
352
Bram Moolenaara5e66212017-09-29 22:42:33 +0200353 /* Set selected item and redraw. If the window size changed need to
354 * redo the positioning. Limit this to two times, when there is not
355 * much room the window size will keep changing. */
356 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000357}
358
359/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100360 * Set a flag that when pum_redraw() is called it first calls update_screen().
361 * This will avoid clearing and redrawing the popup menu, prevent flicker.
362 */
363 void
364pum_call_update_screen()
365{
366 call_update_screen = TRUE;
367
368 // Update the cursor position to be able to compute the popup menu
369 // position. The cursor line length may have changed because of the
370 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100371 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100372 validate_cursor();
373}
374
375/*
376 * Return TRUE if we are going to redraw the popup menu and the screen position
377 * "row"/"col" is under the popup menu.
378 */
379 int
380pum_under_menu(int row, int col)
381{
382 return pum_skip_redraw
383 && row >= pum_row
384 && row < pum_row + pum_height
385 && col >= pum_col - 1
386 && col < pum_col + pum_width;
387}
388
389/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000390 * Redraw the popup menu, using "pum_first" and "pum_selected".
391 */
392 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100393pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000394{
395 int row = pum_row;
396 int col;
397 int attr_norm = highlight_attr[HLF_PNI];
398 int attr_select = highlight_attr[HLF_PSI];
399 int attr_scroll = highlight_attr[HLF_PSB];
400 int attr_thumb = highlight_attr[HLF_PST];
401 int attr;
402 int i;
403 int idx;
404 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000405 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000406 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000407 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100408 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000409 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000410 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000411
Bram Moolenaarae654382019-01-17 21:09:05 +0100412 if (call_update_screen)
413 {
414 call_update_screen = FALSE;
415 pum_skip_redraw = TRUE; // do not redraw in pum_may_redraw().
416 update_screen(0);
417 pum_skip_redraw = FALSE;
418 }
419
420 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100421 if (pum_first > pum_size - pum_height)
422 pum_first = pum_size - pum_height;
423
Bram Moolenaar4b779472005-10-04 09:12:31 +0000424 if (pum_scrollbar)
425 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100426 thumb_height = pum_height * pum_height / pum_size;
427 if (thumb_height == 0)
428 thumb_height = 1;
429 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000430 + (pum_size - pum_height) / 2)
431 / (pum_size - pum_height);
432 }
433
Bram Moolenaar33796b32019-06-08 16:01:13 +0200434#ifdef FEAT_TEXT_PROP
435 // The popup menu is drawn over popup menus with zindex under
436 // POPUPMENU_ZINDEX.
437 screen_zindex = POPUPMENU_ZINDEX;
438#endif
439
Bram Moolenaar4b779472005-10-04 09:12:31 +0000440 for (i = 0; i < pum_height; ++i)
441 {
442 idx = i + pum_first;
443 attr = (idx == pum_selected) ? attr_select : attr_norm;
444
445 /* prepend a space if there is room */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000446#ifdef FEAT_RIGHTLEFT
447 if (curwin->w_p_rl)
448 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200449 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000450 screen_putchar(' ', row, pum_col + 1, attr);
451 }
452 else
453#endif
454 if (pum_col > 0)
455 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000456
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000457 /* Display each entry, use two spaces for a Tab.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000458 * Do this 3 times: For the main text, kind and extra info */
Bram Moolenaar4b779472005-10-04 09:12:31 +0000459 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000460 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000461 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000462 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000463 width = 0;
464 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000465 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000466 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000467 case 1: p = pum_array[idx].pum_text; break;
468 case 2: p = pum_array[idx].pum_kind; break;
469 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000470 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000471 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100472 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000473 {
474 if (s == NULL)
475 s = p;
476 w = ptr2cells(p);
477 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
478 {
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000479 /* Display the text that fits or comes before a Tab.
480 * First convert it to printable characters. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000481 char_u *st;
482 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000483
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100484 if (saved != NUL)
485 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000486 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100487 if (saved != NUL)
488 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000489#ifdef FEAT_RIGHTLEFT
490 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000491 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000492 if (st != NULL)
493 {
494 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000495
496 if (rt != NULL)
497 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100498 char_u *rt_start = rt;
499 int size;
500
501 size = vim_strsize(rt);
502 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000503 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100504 do
505 {
506 size -= has_mbyte
507 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100508 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100509 } while (size > pum_width);
510
511 if (size < pum_width)
512 {
513 /* Most left character requires
514 * 2-cells but only 1 cell is
515 * available on screen. Put a
516 * '<' on the left of the pum
517 * item */
518 *(--rt) = '<';
519 size++;
520 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000521 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100522 screen_puts_len(rt, (int)STRLEN(rt),
523 row, col - size + 1, attr);
524 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000525 }
526 vim_free(st);
527 }
528 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000529 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000530 else
531#endif
532 {
533 if (st != NULL)
534 {
535 screen_puts_len(st, (int)STRLEN(st), row, col,
536 attr);
537 vim_free(st);
538 }
539 col += width;
540 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000541
542 if (*p != TAB)
543 break;
544
545 /* Display two spaces for a Tab. */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000546#ifdef FEAT_RIGHTLEFT
547 if (curwin->w_p_rl)
548 {
549 screen_puts_len((char_u *)" ", 2, row, col - 1,
550 attr);
551 col -= 2;
552 }
553 else
554#endif
555 {
556 screen_puts_len((char_u *)" ", 2, row, col, attr);
557 col += 2;
558 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000559 totwidth += 2;
560 s = NULL; /* start text at next char */
561 width = 0;
562 }
563 else
564 width += w;
565 }
566
567 if (round > 1)
568 n = pum_kind_width + 1;
569 else
570 n = 1;
571
572 /* Stop when there is nothing more to display. */
573 if (round == 3
574 || (round == 2 && pum_array[idx].pum_extra == NULL)
575 || (round == 1 && pum_array[idx].pum_kind == NULL
576 && pum_array[idx].pum_extra == NULL)
577 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000578 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000579#ifdef FEAT_RIGHTLEFT
580 if (curwin->w_p_rl)
581 {
582 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
583 col + 1, ' ', ' ', attr);
584 col = pum_col - pum_base_width - n + 1;
585 }
586 else
587#endif
588 {
589 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000590 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000591 col = pum_col + pum_base_width + n;
592 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000593 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000594 }
595
Bram Moolenaarabc97732007-08-08 20:49:37 +0000596#ifdef FEAT_RIGHTLEFT
597 if (curwin->w_p_rl)
598 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
599 ' ', attr);
600 else
601#endif
602 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
603 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000604 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000605 {
606#ifdef FEAT_RIGHTLEFT
607 if (curwin->w_p_rl)
608 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100609 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000610 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000611 else
612#endif
613 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100614 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000615 ? attr_thumb : attr_scroll);
616 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000617
618 ++row;
619 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200620
621#ifdef FEAT_TEXT_PROP
622 screen_zindex = 0;
623#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000624}
625
Bram Moolenaar4b779472005-10-04 09:12:31 +0000626/*
627 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000628 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000629 * This may be repeated when the preview window is used:
630 * "repeat" == 0: open preview window normally
631 * "repeat" == 1: open preview window but don't set the size
632 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000633 * Returns TRUE when the window was resized and the location of the popup menu
634 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000635 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000636 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100637pum_set_selected(int n, int repeat)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000638{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000639 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000640 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200641#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200642 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200643#endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200644#ifdef FEAT_TEXT_PROP
645 int has_info = FALSE;
646#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000647
Bram Moolenaar4b779472005-10-04 09:12:31 +0000648 pum_selected = n;
649
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000650 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000651 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000652 if (pum_first > pum_selected - 4)
653 {
654 /* scroll down; when we did a jump it's probably a PageUp then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000655 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000656 if (pum_first > pum_selected - 2)
657 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000658 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000659 if (pum_first < 0)
660 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000661 else if (pum_first > pum_selected)
662 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000663 }
664 else
665 pum_first = pum_selected;
666 }
667 else if (pum_first < pum_selected - pum_height + 5)
668 {
669 /* scroll up; when we did a jump it's probably a PageDown then
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000670 * scroll a whole page */
Bram Moolenaare3226be2005-12-18 22:10:00 +0000671 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000672 {
673 pum_first += pum_height - 2;
674 if (pum_first < pum_selected - pum_height + 1)
675 pum_first = pum_selected - pum_height + 1;
676 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000677 else
678 pum_first = pum_selected - pum_height + 1;
679 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000680
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000681 /* Give a few lines of context when possible. */
682 if (context > 3)
683 context = 3;
684 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000685 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000686 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000687 {
688 /* scroll down */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000689 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000690 if (pum_first < 0)
691 pum_first = 0;
692 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000693 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000694 {
695 /* scroll up */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000696 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000697 }
698 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200699 // adjust for the number of lines displayed
700 if (pum_first > pum_size - pum_height)
701 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000702
Bram Moolenaar4033c552017-09-16 20:54:51 +0200703#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000704 /*
705 * Show extra info in the preview window if there is something and
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200706 * 'completeopt' contains "preview" or "popup".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000707 * Skip this when tried twice already.
708 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000709 * NOTE: Be very careful not to sync undo!
710 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000711 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000712 && Rows > 10
713 && repeat <= 1
714 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000715 {
716 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200717 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000718 int res = OK;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200719# ifdef FEAT_TEXT_PROP
720 int use_popup = strstr((char *)p_cot, "popup") != NULL;
721# else
722# define use_popup 0
723# endif
724 has_info = TRUE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000725
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200726 // Open a preview window. 3 lines by default. Prefer
727 // 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000728 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200729 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
730 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200731 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200732 // Prevent undo sync here, if an autocommand syncs undo weird
733 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100734 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200735 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100736 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200737 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000738 g_do_tagpreview = 0;
739
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200740 if (curwin->w_p_pvw
741# ifdef FEAT_TEXT_PROP
742 || (curwin->w_popup_flags & POPF_INFO)
743# endif
744 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000745 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200746# ifdef FEAT_TEXT_PROP
747 if (use_popup)
748 {
749 int col = pum_col + pum_width + 1;
750
751 if (Columns - col < 20 && Columns - col < pum_col)
752 {
753 col = pum_col - 1;
754 curwin->w_popup_pos = POPPOS_TOPRIGHT;
755 curwin->w_maxwidth = pum_col - 1;
756 }
757 else
758 curwin->w_maxwidth = Columns - col + 1;
759 curwin->w_maxwidth -= popup_extra_width(curwin);
760 popup_set_wantpos_rowcol(curwin,
761 pum_row + pum_selected - pum_first, col);
762 }
763# endif
Bram Moolenaar50e53762016-10-27 14:49:15 +0200764 if (!resized
765 && curbuf->b_nwindows == 1
766 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200767 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000768 && curbuf->b_p_bh[0] == 'w')
769 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200770 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100771 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000772 ml_delete((linenr_T)1, FALSE);
773 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000774 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000775 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200776 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000777 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000778 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000779 --no_u_sync;
780 if (res == OK)
781 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200782 // Edit a new, empty buffer. Set options for a "wipeout"
783 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000784 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
785 set_option_value((char_u *)"bt", 0L,
786 (char_u *)"nofile", OPT_LOCAL);
787 set_option_value((char_u *)"bh", 0L,
788 (char_u *)"wipe", OPT_LOCAL);
789 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000790 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000791 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000792 }
793 if (res == OK)
794 {
795 char_u *p, *e;
796 linenr_T lnum = 0;
797
798 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
799 {
800 e = vim_strchr(p, '\n');
801 if (e == NULL)
802 {
803 ml_append(lnum++, p, 0, FALSE);
804 break;
805 }
806 else
807 {
808 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000809 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000810 *e = '\n';
811 p = e + 1;
812 }
813 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200814 // delete the empty last line
815 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000816
817 /* Increase the height of the preview window to show the
818 * text, but no more than 'previewheight' lines. */
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200819 if (repeat == 0 && !use_popup)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000820 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000821 if (lnum > p_pvh)
822 lnum = p_pvh;
823 if (curwin->w_height < lnum)
824 {
825 win_setheight((int)lnum);
826 resized = TRUE;
827 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000828 }
829
830 curbuf->b_changed = 0;
831 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200832 if (pum_selected != prev_selected)
833 {
834# ifdef FEAT_TEXT_PROP
835 curwin->w_firstline = 1;
836# endif
837 curwin->w_topline = 1;
838 }
839 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
840 curwin->w_topline = curbuf->b_ml.ml_line_count;
841 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000842 curwin->w_cursor.col = 0;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200843 if (use_popup && win_valid(curwin_save))
844 redraw_win_later(curwin_save, SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000845
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200846 if ((curwin != curwin_save && win_valid(curwin_save))
847 || (curtab != curtab_save
848 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000849 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200850 if (curtab != curtab_save && valid_tabpage(curtab_save))
851 goto_tabpage_tp(curtab_save, FALSE, FALSE);
852
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200853 /* When the first completion is done and the preview
854 * window is not resized, skip the preview window's
855 * status line redrawing. */
856 if (ins_compl_active() && !resized)
857 curwin->w_redr_status = FALSE;
858
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000859 /* Return cursor to where we were */
860 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000861 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000862
863 /* When the preview window was resized we need to
864 * update the view on the buffer. Only go back to
865 * the window when needed, otherwise it will always be
866 * redraw. */
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200867 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000868 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100869 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000870 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100871 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000872 update_topline();
873 }
874
875 /* Update the screen before drawing the popup menu.
876 * Enable updating the status lines. */
877 pum_do_redraw = TRUE;
878 update_screen(0);
879 pum_do_redraw = FALSE;
880
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000881 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100882 {
883 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000884 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100885 --no_u_sync;
886 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000887
888 /* May need to update the screen again when there are
889 * autocommands involved. */
890 pum_do_redraw = TRUE;
891 update_screen(0);
892 pum_do_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100893 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000894 }
895 }
896 }
Bram Moolenaareaf35242019-08-20 23:14:15 +0200897# if defined(FEAT_TEXT_PROP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200898 if (WIN_IS_POPUP(curwin))
899 // can't keep focus in a popup window
900 win_enter(firstwin, TRUE);
901# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000902 }
903#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000904 }
Bram Moolenaareaf35242019-08-20 23:14:15 +0200905# if defined(FEAT_TEXT_PROP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200906 if (!has_info)
907 // close any popup info window
908 popup_close_preview(TRUE);
909# endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000910
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000911 if (!resized)
912 pum_redraw();
913
914 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000915}
916
917/*
918 * Undisplay the popup menu (later).
919 */
920 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100921pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000922{
923 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +0200924 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000925 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000926 status_redraw_all();
Bram Moolenaareaf35242019-08-20 23:14:15 +0200927# if defined(FEAT_TEXT_PROP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200928 // close any popup info window
929 popup_close_preview(TRUE);
930#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000931}
932
933/*
934 * Clear the popup menu. Currently only resets the offset to the first
935 * displayed item.
936 */
937 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100938pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000939{
940 pum_first = 0;
941}
942
943/*
944 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000945 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000946 */
947 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100948pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000949{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000950 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000951}
952
Bram Moolenaare3226be2005-12-18 22:10:00 +0000953/*
Bram Moolenaar491ac282018-06-17 14:47:55 +0200954 * Reposition the popup menu to adjust for window layout changes.
955 */
956 void
957pum_may_redraw(void)
958{
959 pumitem_T *array = pum_array;
960 int len = pum_size;
961 int selected = pum_selected;
962
Bram Moolenaarae654382019-01-17 21:09:05 +0100963 if (!pum_visible() || pum_skip_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +0200964 return; // nothing to do
965
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200966 if (pum_window != curwin
967 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
968 && pum_win_height == curwin->w_height
969 && pum_win_col == curwin->w_wincol
970 && pum_win_width == curwin->w_width))
Bram Moolenaar491ac282018-06-17 14:47:55 +0200971 {
972 // window position didn't change, redraw in the same position
973 pum_redraw();
974 }
975 else
976 {
977 int wcol = curwin->w_wcol;
978
979 // Window layout changed, recompute the position.
980 // Use the remembered w_wcol value, the cursor may have moved when a
981 // completion was inserted, but we want the menu in the same position.
982 pum_undisplay();
983 curwin->w_wcol = pum_win_wcol;
984 curwin->w_valid |= VALID_WCOL;
985 pum_display(array, len, selected);
986 curwin->w_wcol = wcol;
987 }
988}
989
990/*
Bram Moolenaare3226be2005-12-18 22:10:00 +0000991 * Return the height of the popup menu, the number of entries visible.
992 * Only valid when pum_visible() returns TRUE!
993 */
994 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100995pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000996{
997 return pum_height;
998}
999
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001000/*
1001 * Add size information about the pum to "dict".
1002 */
1003 void
1004pum_set_event_info(dict_T *dict)
1005{
1006 if (!pum_visible())
1007 return;
1008 dict_add_number(dict, "height", pum_height);
1009 dict_add_number(dict, "width", pum_width);
1010 dict_add_number(dict, "row", pum_row);
1011 dict_add_number(dict, "col", pum_col);
1012 dict_add_number(dict, "size", pum_size);
1013 dict_add_special(dict, "scrollbar", pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
1014}
1015
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001016# if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
1017 static void
1018pum_position_at_mouse(int min_width)
1019{
1020 if (Rows - mouse_row > pum_size)
1021 {
1022 /* Enough space below the mouse row. */
1023 pum_row = mouse_row + 1;
1024 if (pum_height > Rows - pum_row)
1025 pum_height = Rows - pum_row;
1026 }
1027 else
1028 {
1029 /* Show above the mouse row, reduce height if it does not fit. */
1030 pum_row = mouse_row - pum_size;
1031 if (pum_row < 0)
1032 {
1033 pum_height += pum_row;
1034 pum_row = 0;
1035 }
1036 }
1037 if (Columns - mouse_col >= pum_base_width
1038 || Columns - mouse_col > min_width)
1039 /* Enough space to show at mouse column. */
1040 pum_col = mouse_col;
1041 else
1042 /* Not enough space, right align with window. */
1043 pum_col = Columns - (pum_base_width > min_width
1044 ? min_width : pum_base_width);
1045
1046 pum_width = Columns - pum_col;
1047 if (pum_width > pum_base_width + 1)
1048 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001049
1050 // Do not redraw at cursor position.
1051 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001052}
1053
1054# endif
1055
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001056# if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001057static pumitem_T *balloon_array = NULL;
1058static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001059
Bram Moolenaar246fe032017-11-19 19:56:27 +01001060#define BALLOON_MIN_WIDTH 50
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001061#define BALLOON_MIN_HEIGHT 10
1062
Bram Moolenaar246fe032017-11-19 19:56:27 +01001063typedef struct {
1064 char_u *start;
1065 int bytelen;
1066 int cells;
1067 int indent;
1068} balpart_T;
1069
1070/*
1071 * Split a string into parts to display in the balloon.
1072 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1073 * strings and make a struct look good.
1074 * Resulting array is stored in "array" and returns the size of the array.
1075 */
1076 int
1077split_message(char_u *mesg, pumitem_T **array)
1078{
1079 garray_T ga;
1080 char_u *p;
1081 balpart_T *item;
1082 int quoted = FALSE;
1083 int height;
1084 int line;
1085 int item_idx;
1086 int indent = 0;
1087 int max_cells = 0;
1088 int max_height = Rows / 2 - 2;
1089 int long_item_count = 0;
1090 int split_long_items = FALSE;
1091
1092 ga_init2(&ga, sizeof(balpart_T), 20);
1093 p = mesg;
1094
1095 while (*p != NUL)
1096 {
1097 if (ga_grow(&ga, 1) == FAIL)
1098 goto failed;
1099 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1100 item->start = p;
1101 item->indent = indent;
1102 item->cells = indent * 2;
1103 ++ga.ga_len;
1104 while (*p != NUL)
1105 {
1106 if (*p == '"')
1107 quoted = !quoted;
1108 else if (*p == '\\' && p[1] != NUL)
1109 ++p;
1110 else if (!quoted)
1111 {
1112 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1113 {
1114 /* Looks like a good point to break. */
1115 if (*p == '{')
1116 ++indent;
1117 else if (*p == '}' && indent > 0)
1118 --indent;
1119 ++item->cells;
1120 p = skipwhite(p + 1);
1121 break;
1122 }
1123 }
1124 item->cells += ptr2cells(p);
1125 p += MB_PTR2LEN(p);
1126 }
1127 item->bytelen = p - item->start;
1128 if (item->cells > max_cells)
1129 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001130 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001131 }
1132
1133 height = 2 + ga.ga_len;
1134
1135 /* If there are long items and the height is below the limit: split lines */
1136 if (long_item_count > 0 && height + long_item_count <= max_height)
1137 {
1138 split_long_items = TRUE;
1139 height += long_item_count;
1140 }
1141
1142 /* Limit to half the window height, it has to fit above or below the mouse
1143 * position. */
1144 if (height > max_height)
1145 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001146 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001147 if (*array == NULL)
1148 goto failed;
1149
1150 /* Add an empty line above and below, looks better. */
1151 (*array)->pum_text = vim_strsave((char_u *)"");
1152 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1153
1154 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1155 {
1156 int skip;
1157 int thislen;
1158 int copylen;
1159 int ind;
1160 int cells;
1161
1162 item = ((balpart_T *)ga.ga_data) + item_idx;
1163 for (skip = 0; skip < item->bytelen; skip += thislen)
1164 {
1165 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1166 {
1167 cells = item->indent * 2;
1168 for (p = item->start + skip; p < item->start + item->bytelen;
1169 p += MB_PTR2LEN(p))
1170 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1171 break;
1172 thislen = p - (item->start + skip);
1173 }
1174 else
1175 thislen = item->bytelen;
1176
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001177 // put indent at the start
Bram Moolenaar246fe032017-11-19 19:56:27 +01001178 p = alloc(thislen + item->indent * 2 + 1);
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001179 if (p == NULL)
1180 {
1181 for (line = 0; line <= height - 1; ++line)
1182 vim_free((*array)[line].pum_text);
1183 vim_free(*array);
1184 goto failed;
1185 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001186 for (ind = 0; ind < item->indent * 2; ++ind)
1187 p[ind] = ' ';
1188
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001189 // exclude spaces at the end of the string
Bram Moolenaar246fe032017-11-19 19:56:27 +01001190 for (copylen = thislen; copylen > 0; --copylen)
1191 if (item->start[skip + copylen - 1] != ' ')
1192 break;
1193
1194 vim_strncpy(p + ind, item->start + skip, copylen);
1195 (*array)[line].pum_text = p;
1196 item->indent = 0; /* wrapped line has no indent */
1197 ++line;
1198 }
1199 }
1200 ga_clear(&ga);
1201 return height;
1202
1203failed:
1204 ga_clear(&ga);
1205 return 0;
1206}
1207
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001208 void
1209ui_remove_balloon(void)
1210{
1211 if (balloon_array != NULL)
1212 {
1213 pum_undisplay();
1214 while (balloon_arraysize > 0)
1215 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001216 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001217 }
1218}
1219
1220/*
1221 * Terminal version of a balloon, uses the popup menu code.
1222 */
1223 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001224ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001225{
1226 ui_remove_balloon();
1227
Bram Moolenaar246fe032017-11-19 19:56:27 +01001228 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001229 {
1230 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001231 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001232 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001233 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001234 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001235 listitem_T *li;
1236 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001237
Bram Moolenaar246fe032017-11-19 19:56:27 +01001238 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001239 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001240 if (balloon_array == NULL)
1241 return;
1242 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001244 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001245
1246 balloon_array[idx].pum_text = vim_strsave(
1247 text == NULL ? (char_u *)"" : text);
1248 }
1249 }
1250 else
1251 balloon_arraysize = split_message(mesg, &balloon_array);
1252
1253 if (balloon_arraysize > 0)
1254 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001255 pum_array = balloon_array;
1256 pum_size = balloon_arraysize;
1257 pum_compute_size();
1258 pum_scrollbar = 0;
1259 pum_height = balloon_arraysize;
1260
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001261 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001262 pum_selected = -1;
1263 pum_first = 0;
1264 pum_redraw();
1265 }
1266}
1267
1268/*
1269 * Called when the mouse moved, may remove any displayed balloon.
1270 */
1271 void
1272ui_may_remove_balloon(void)
1273{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001274 // For now: remove the balloon whenever the mouse moves to another screen
1275 // cell.
1276 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001277}
1278# endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001279
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001280# if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
1281/*
1282 * Select the pum entry at the mouse position.
1283 */
1284 static void
1285pum_select_mouse_pos(void)
1286{
1287 int idx = mouse_row - pum_row;
1288
1289 if (idx < 0 || idx >= pum_size)
1290 pum_selected = -1;
1291 else if (*pum_array[idx].pum_text != NUL)
1292 pum_selected = idx;
1293}
1294
1295/*
1296 * Execute the currently selected popup menu item.
1297 */
1298 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001299pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001300{
1301 vimmenu_T *mp;
1302 int idx = 0;
1303 exarg_T ea;
1304
1305 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001306 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001307 {
1308 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001309 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001310 break;
1311 }
1312}
1313
1314/*
1315 * Open the terminal version of the popup menu and don't return until it is
1316 * closed.
1317 */
1318 void
1319pum_show_popupmenu(vimmenu_T *menu)
1320{
1321 vimmenu_T *mp;
1322 int idx = 0;
1323 pumitem_T *array;
1324#ifdef FEAT_BEVAL_TERM
1325 int save_bevalterm = p_bevalterm;
1326#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001327 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001328
1329 pum_undisplay();
1330 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001331 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001332
1333 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001334 if (menu_is_separator(mp->dname)
1335 || (mp->modes & mp->enabled & mode))
1336 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001337
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001338 // When there are only Terminal mode menus, using "popup Edit" results in
1339 // pum_size being zero.
1340 if (pum_size <= 0)
1341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001342 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001343 return;
1344 }
1345
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001346 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001347 if (array == NULL)
1348 return;
1349
1350 for (mp = menu->children; mp != NULL; mp = mp->next)
1351 if (menu_is_separator(mp->dname))
1352 array[idx++].pum_text = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001353 else if (mp->modes & mp->enabled & mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001354 array[idx++].pum_text = mp->dname;
1355
1356 pum_array = array;
1357 pum_compute_size();
1358 pum_scrollbar = 0;
1359 pum_height = pum_size;
1360 pum_position_at_mouse(20);
1361
1362 pum_selected = -1;
1363 pum_first = 0;
1364# ifdef FEAT_BEVAL_TERM
1365 p_bevalterm = TRUE; /* track mouse movement */
1366 mch_setmouse(TRUE);
1367# endif
1368
1369 for (;;)
1370 {
1371 int c;
1372
1373 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001374 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001375 out_flush();
1376
1377 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001378
1379 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1380 // menu.
1381 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001382 break;
1383 else if (c == CAR || c == NL)
1384 {
1385 /* enter: select current item, if any, and close */
Bram Moolenaar987723e2018-03-06 11:43:04 +01001386 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001387 break;
1388 }
1389 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1390 {
1391 /* cursor up: select previous item */
1392 while (pum_selected > 0)
1393 {
1394 --pum_selected;
1395 if (*array[pum_selected].pum_text != NUL)
1396 break;
1397 }
1398 }
1399 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1400 {
1401 /* cursor down: select next item */
1402 while (pum_selected < pum_size - 1)
1403 {
1404 ++pum_selected;
1405 if (*array[pum_selected].pum_text != NUL)
1406 break;
1407 }
1408 }
1409 else if (c == K_RIGHTMOUSE)
1410 {
1411 /* Right mouse down: reposition the menu. */
1412 vungetc(c);
1413 break;
1414 }
1415 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1416 {
Bram Moolenaar52f18a12018-03-07 22:09:11 +01001417 /* mouse moved: select item in the mouse row */
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001418 pum_select_mouse_pos();
1419 }
1420 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1421 {
1422 /* left mouse click: select clicked item, if any, and close;
1423 * right mouse release: select clicked item, close if any */
1424 pum_select_mouse_pos();
1425 if (pum_selected >= 0)
1426 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001427 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001428 break;
1429 }
1430 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1431 break;
1432 }
1433 }
1434
1435 vim_free(array);
1436 pum_undisplay();
1437# ifdef FEAT_BEVAL_TERM
1438 p_bevalterm = save_bevalterm;
1439 mch_setmouse(TRUE);
1440# endif
1441}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001442
1443 void
1444pum_make_popup(char_u *path_name, int use_mouse_pos)
1445{
1446 vimmenu_T *menu;
1447
1448 if (!use_mouse_pos)
1449 {
1450 /* Hack: set mouse position at the cursor so that the menu pops up
1451 * around there. */
1452 mouse_row = curwin->w_winrow + curwin->w_wrow;
1453 mouse_col = curwin->w_wincol + curwin->w_wcol;
1454 }
1455
1456 menu = gui_find_menu(path_name);
1457 if (menu != NULL)
1458 pum_show_popupmenu(menu);
1459}
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001460# endif
1461
Bram Moolenaar4b779472005-10-04 09:12:31 +00001462#endif