blob: 3a5e1032410bbf7748bebab0cc56ad4f2d273710 [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 Moolenaar30e8e732019-09-27 13:08:36 +020011 * popupmenu.c: Popup menu (PUM)
Bram Moolenaar4b779472005-10-04 09:12:31 +000012 */
13#include "vim.h"
14
Bram Moolenaar63d9e732019-12-05 21:10:38 +010015static pumitem_T *pum_array = NULL; // items of displayed pum
16static int pum_size; // nr of items in "pum_array"
17static int pum_selected; // index of selected item or -1
18static int pum_first = 0; // index of top item
Bram Moolenaar4b779472005-10-04 09:12:31 +000019
Bram Moolenaarae654382019-01-17 21:09:05 +010020static int call_update_screen = FALSE;
21
Bram Moolenaar63d9e732019-12-05 21:10:38 +010022static int pum_height; // nr of displayed pum items
23static int pum_width; // width of displayed pum items
24static int pum_base_width; // width of pum items base
25static int pum_kind_width; // width of pum items kind column
26static int pum_extra_width; // width of extra stuff
27static int pum_scrollbar; // TRUE when scrollbar present
Bram Moolenaar4b779472005-10-04 09:12:31 +000028
Bram Moolenaar63d9e732019-12-05 21:10:38 +010029static int pum_row; // top row of pum
30static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000031
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020032static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020033static int pum_win_row;
34static int pum_win_height;
35static int pum_win_col;
36static int pum_win_wcol;
37static int pum_win_width;
38
Bram Moolenaar04357fb2019-12-10 21:50:56 +010039// Some parts are not updated when a popup menu is visible. Setting this flag
40// makes pum_visible() return FALSE even when there is a popup menu.
41static int pum_pretend_not_visible = FALSE;
42
43// When set the popup menu will redraw soon using the pum_win_ values. Do not
44// draw over the poup menu area to avoid flicker.
45static int pum_will_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000046
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000048
Bram Moolenaar4b779472005-10-04 09:12:31 +000049#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000050
Bram Moolenaar51b0f372017-11-18 18:52:04 +010051 static void
52pum_compute_size(void)
53{
54 int i;
55 int w;
56
Bram Moolenaar63d9e732019-12-05 21:10:38 +010057 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010058 pum_base_width = 0;
59 pum_kind_width = 0;
60 pum_extra_width = 0;
61 for (i = 0; i < pum_size; ++i)
62 {
63 w = vim_strsize(pum_array[i].pum_text);
64 if (pum_base_width < w)
65 pum_base_width = w;
66 if (pum_array[i].pum_kind != NULL)
67 {
68 w = vim_strsize(pum_array[i].pum_kind) + 1;
69 if (pum_kind_width < w)
70 pum_kind_width = w;
71 }
72 if (pum_array[i].pum_extra != NULL)
73 {
74 w = vim_strsize(pum_array[i].pum_extra) + 1;
75 if (pum_extra_width < w)
76 pum_extra_width = w;
77 }
78 }
79}
80
Bram Moolenaar4b779472005-10-04 09:12:31 +000081/*
82 * Show the popup menu with items "array[size]".
83 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010084 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000085 * The menu appears above the screen line "row" or at "row" + "height" - 1.
86 */
87 void
Bram Moolenaar05540972016-01-30 20:31:25 +010088pum_display(
89 pumitem_T *array,
90 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010091 int selected) // index of initially selected item, none if
92 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000094 int def_width;
95 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000096 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010097 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010098 int above_row;
99 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000100 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100102 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100103#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000104
Bram Moolenaara5e66212017-09-29 22:42:33 +0200105 do
106 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100107 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 above_row = 0;
109 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000110
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100111 // Pretend the pum is already there to avoid that must_redraw is set
112 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200113 pum_array = (pumitem_T *)1;
114 validate_cursor_col();
115 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000116
Bram Moolenaar491ac282018-06-17 14:47:55 +0200117 // Remember the essential parts of the window position and size, so we
118 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200119 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200120 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
121 pum_win_height = curwin->w_height;
122 pum_win_col = curwin->w_wincol;
123 pum_win_wcol = curwin->w_wcol;
124 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000125
Bram Moolenaar4033c552017-09-16 20:54:51 +0200126#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200127 FOR_ALL_WINDOWS(pvwin)
128 if (pvwin->w_p_pvw)
129 break;
130 if (pvwin != NULL)
131 {
132 if (W_WINROW(pvwin) < W_WINROW(curwin))
133 above_row = W_WINROW(pvwin) + pvwin->w_height;
134 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
135 below_row = W_WINROW(pvwin);
136 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100137#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000138
Bram Moolenaara5e66212017-09-29 22:42:33 +0200139 /*
140 * Figure out the size and position of the pum.
141 */
142 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000143 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000144 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200145 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000146 if (p_ph > 0 && pum_height > p_ph)
147 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000148
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100149 // Put the pum below "pum_win_row" if possible. If there are few lines
150 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200151 if (pum_win_row + 2 >= below_row - pum_height
152 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200153 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100154 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200155
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100156 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200157 if (curwin->w_wrow - curwin->w_cline_row >= 2)
158 context_lines = 2;
159 else
160 context_lines = curwin->w_wrow - curwin->w_cline_row;
161
Bram Moolenaar491ac282018-06-17 14:47:55 +0200162 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200163 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200165 pum_height = size;
166 }
167 else
168 {
169 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200170 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200171 }
172 if (p_ph > 0 && pum_height > p_ph)
173 {
174 pum_row += pum_height - p_ph;
175 pum_height = p_ph;
176 }
177 }
178 else
179 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100180 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200181
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100182 // Leave two lines of context if possible
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100183 validate_cheight();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184 if (curwin->w_cline_row
185 + curwin->w_cline_height - curwin->w_wrow >= 3)
186 context_lines = 3;
187 else
188 context_lines = curwin->w_cline_row
189 + curwin->w_cline_height - curwin->w_wrow;
190
Bram Moolenaar491ac282018-06-17 14:47:55 +0200191 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200192 if (size > below_row - pum_row)
193 pum_height = below_row - pum_row;
194 else
195 pum_height = size;
196 if (p_ph > 0 && pum_height > p_ph)
197 pum_height = p_ph;
198 }
199
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100200 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200201 if (pum_height < 1 || (pum_height == 1 && size > 1))
202 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000203
Bram Moolenaar4033c552017-09-16 20:54:51 +0200204#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100205 // If there is a preview window above avoid drawing over it.
206 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000207 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100208 pum_row = above_row;
209 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000210 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200211#endif
212
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100213 pum_array = array;
214 pum_size = size;
215 pum_compute_size();
216 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000217
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100218 // Calculate column
Bram Moolenaarabc97732007-08-08 20:49:37 +0000219#ifdef FEAT_RIGHTLEFT
220 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100221 cursor_col = curwin->w_wincol + curwin->w_width
222 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000223 else
224#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100225 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000226
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100227 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200228 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000229 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200230 pum_scrollbar = 1;
231 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000232 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000233 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200234 pum_scrollbar = 0;
235
236 if (def_width < max_width)
237 def_width = max_width;
238
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100239 if (((cursor_col < Columns - p_pw
240 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000241#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200242 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100243 || (curwin->w_p_rl
244 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000245#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200246 ))
247 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100248 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100249 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000250
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100251 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200252#ifdef FEAT_RIGHTLEFT
253 if (curwin->w_p_rl)
254 pum_width = pum_col - pum_scrollbar + 1;
255 else
256#endif
257 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000258
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100259 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100260 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200261 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100262 // the width is more than needed for the items, make it
263 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100264 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100265 if (pum_width < p_pw)
266 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200267 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100268 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100269#ifdef FEAT_RIGHTLEFT
270 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100271 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
272 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100273#endif
274 ))
275 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100276 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100277#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100278 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100279 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100280 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100281 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100282 if (pum_col >= Columns)
283 pum_col = Columns - 1;
284 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100285 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100286#endif
287 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100288 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
289 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100290 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100291 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100292 pum_col = Columns - max_width - pum_scrollbar;
293 if (pum_col < 0)
294 pum_col = 0;
295 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100296 }
297
298#ifdef FEAT_RIGHTLEFT
299 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100300 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100301 else
302#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100303 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304
Bram Moolenaar42443c72018-02-10 18:28:52 +0100305 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100306 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100307 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100308#ifdef FEAT_RIGHTLEFT
309 if (curwin->w_p_rl)
310 {
311 if (pum_width > pum_col)
312 pum_width = pum_col;
313 }
314 else
315#endif
316 {
317 if (pum_width >= Columns - pum_col)
318 pum_width = Columns - pum_col - 1;
319 }
320 }
321 else if (pum_width > max_width + pum_kind_width
322 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100323 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100324 {
325 pum_width = max_width + pum_kind_width
326 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100327 if (pum_width < p_pw)
328 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100329 }
330 }
331
Bram Moolenaara5e66212017-09-29 22:42:33 +0200332 }
333 else if (Columns < def_width)
334 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100335 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200336#ifdef FEAT_RIGHTLEFT
337 if (curwin->w_p_rl)
338 pum_col = Columns - 1;
339 else
340#endif
341 pum_col = 0;
342 pum_width = Columns - 1;
343 }
344 else
345 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100346 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100347 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200348#ifdef FEAT_RIGHTLEFT
349 if (curwin->w_p_rl)
350 pum_col = max_width - 1;
351 else
352#endif
353 pum_col = Columns - max_width;
354 pum_width = max_width - pum_scrollbar;
355 }
356
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100357 // Set selected item and redraw. If the window size changed need to
358 // redo the positioning. Limit this to two times, when there is not
359 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200360 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000361}
362
363/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100364 * Set a flag that when pum_redraw() is called it first calls update_screen().
365 * This will avoid clearing and redrawing the popup menu, prevent flicker.
366 */
367 void
368pum_call_update_screen()
369{
370 call_update_screen = TRUE;
371
372 // Update the cursor position to be able to compute the popup menu
373 // position. The cursor line length may have changed because of the
374 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100375 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100376 validate_cursor();
377}
378
379/*
380 * Return TRUE if we are going to redraw the popup menu and the screen position
381 * "row"/"col" is under the popup menu.
382 */
383 int
384pum_under_menu(int row, int col)
385{
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100386 return pum_will_redraw
Bram Moolenaarae654382019-01-17 21:09:05 +0100387 && row >= pum_row
388 && row < pum_row + pum_height
389 && col >= pum_col - 1
390 && col < pum_col + pum_width;
391}
392
393/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000394 * Redraw the popup menu, using "pum_first" and "pum_selected".
395 */
396 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100397pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000398{
399 int row = pum_row;
400 int col;
401 int attr_norm = highlight_attr[HLF_PNI];
402 int attr_select = highlight_attr[HLF_PSI];
403 int attr_scroll = highlight_attr[HLF_PSB];
404 int attr_thumb = highlight_attr[HLF_PST];
405 int attr;
406 int i;
407 int idx;
408 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000409 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000410 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000411 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100412 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000413 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000414 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000415
Bram Moolenaarae654382019-01-17 21:09:05 +0100416 if (call_update_screen)
417 {
418 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100419 // Do not redraw in pum_may_redraw() and don't draw in the area where
420 // the popup menu will be.
421 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100422 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100423 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100424 }
425
426 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100427 if (pum_first > pum_size - pum_height)
428 pum_first = pum_size - pum_height;
429
Bram Moolenaar4b779472005-10-04 09:12:31 +0000430 if (pum_scrollbar)
431 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100432 thumb_height = pum_height * pum_height / pum_size;
433 if (thumb_height == 0)
434 thumb_height = 1;
435 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000436 + (pum_size - pum_height) / 2)
437 / (pum_size - pum_height);
438 }
439
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100440#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200441 // The popup menu is drawn over popup menus with zindex under
442 // POPUPMENU_ZINDEX.
443 screen_zindex = POPUPMENU_ZINDEX;
444#endif
445
Bram Moolenaar4b779472005-10-04 09:12:31 +0000446 for (i = 0; i < pum_height; ++i)
447 {
448 idx = i + pum_first;
449 attr = (idx == pum_selected) ? attr_select : attr_norm;
450
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100451 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000452#ifdef FEAT_RIGHTLEFT
453 if (curwin->w_p_rl)
454 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200455 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000456 screen_putchar(' ', row, pum_col + 1, attr);
457 }
458 else
459#endif
460 if (pum_col > 0)
461 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000462
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100463 // Display each entry, use two spaces for a Tab.
464 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000465 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000466 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000467 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000468 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000469 width = 0;
470 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000471 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000472 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000473 case 1: p = pum_array[idx].pum_text; break;
474 case 2: p = pum_array[idx].pum_kind; break;
475 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000476 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000477 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100478 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000479 {
480 if (s == NULL)
481 s = p;
482 w = ptr2cells(p);
483 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
484 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100485 // Display the text that fits or comes before a Tab.
486 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000487 char_u *st;
488 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000489
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100490 if (saved != NUL)
491 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000492 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100493 if (saved != NUL)
494 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000495#ifdef FEAT_RIGHTLEFT
496 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000497 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000498 if (st != NULL)
499 {
500 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000501
502 if (rt != NULL)
503 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100504 char_u *rt_start = rt;
505 int size;
506
507 size = vim_strsize(rt);
508 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000509 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100510 do
511 {
512 size -= has_mbyte
513 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100514 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100515 } while (size > pum_width);
516
517 if (size < pum_width)
518 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100519 // Most left character requires
520 // 2-cells but only 1 cell is
521 // available on screen. Put a
522 // '<' on the left of the pum
523 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100524 *(--rt) = '<';
525 size++;
526 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000527 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100528 screen_puts_len(rt, (int)STRLEN(rt),
529 row, col - size + 1, attr);
530 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000531 }
532 vim_free(st);
533 }
534 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000535 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000536 else
537#endif
538 {
539 if (st != NULL)
540 {
541 screen_puts_len(st, (int)STRLEN(st), row, col,
542 attr);
543 vim_free(st);
544 }
545 col += width;
546 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000547
548 if (*p != TAB)
549 break;
550
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100551 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000552#ifdef FEAT_RIGHTLEFT
553 if (curwin->w_p_rl)
554 {
555 screen_puts_len((char_u *)" ", 2, row, col - 1,
556 attr);
557 col -= 2;
558 }
559 else
560#endif
561 {
562 screen_puts_len((char_u *)" ", 2, row, col, attr);
563 col += 2;
564 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000565 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100566 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567 width = 0;
568 }
569 else
570 width += w;
571 }
572
573 if (round > 1)
574 n = pum_kind_width + 1;
575 else
576 n = 1;
577
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100578 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000579 if (round == 3
580 || (round == 2 && pum_array[idx].pum_extra == NULL)
581 || (round == 1 && pum_array[idx].pum_kind == NULL
582 && pum_array[idx].pum_extra == NULL)
583 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000584 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000585#ifdef FEAT_RIGHTLEFT
586 if (curwin->w_p_rl)
587 {
588 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
589 col + 1, ' ', ' ', attr);
590 col = pum_col - pum_base_width - n + 1;
591 }
592 else
593#endif
594 {
595 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000596 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000597 col = pum_col + pum_base_width + n;
598 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000599 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000600 }
601
Bram Moolenaarabc97732007-08-08 20:49:37 +0000602#ifdef FEAT_RIGHTLEFT
603 if (curwin->w_p_rl)
604 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
605 ' ', attr);
606 else
607#endif
608 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
609 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000610 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000611 {
612#ifdef FEAT_RIGHTLEFT
613 if (curwin->w_p_rl)
614 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100615 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000617 else
618#endif
619 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100620 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000621 ? attr_thumb : attr_scroll);
622 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000623
624 ++row;
625 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200626
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100627#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200628 screen_zindex = 0;
629#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000630}
631
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100632#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200633/*
634 * Position the info popup relative to the popup menu item.
635 */
636 void
637pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200638{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100639 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200640 int row = pum_row;
641 int botpos = POPPOS_BOTLEFT;
642
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200643 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200644 if (Columns - col < 20 && Columns - col < pum_col)
645 {
646 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200647 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200648 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200649 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200650 }
651 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200652 wp->w_maxwidth = Columns - col + 1;
653 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200654
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200655 row -= popup_top_extra(wp);
656 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200657 {
658 if (pum_row < pum_win_row)
659 {
660 // menu above cursor line, align with bottom
661 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200662 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200663 }
664 else
665 // menu below cursor line, align with top
666 row += 1;
667 }
668 else
669 // align with the selected item
670 row += pum_selected - pum_first + 1;
671
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100672 wp->w_popup_flags &= ~POPF_HIDDEN;
673 if (wp->w_maxwidth < 10)
674 // The popup is not going to fit or will overlap with the cursor
675 // position, hide the popup.
676 wp->w_popup_flags |= POPF_HIDDEN;
677 else
678 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200679}
680#endif
681
Bram Moolenaar4b779472005-10-04 09:12:31 +0000682/*
683 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000684 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000685 * This may be repeated when the preview window is used:
686 * "repeat" == 0: open preview window normally
687 * "repeat" == 1: open preview window but don't set the size
688 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000689 * Returns TRUE when the window was resized and the location of the popup menu
690 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000691 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000692 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200693pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000694{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000695 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000696 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200697#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200698 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200699#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100700#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200701 int has_info = FALSE;
702#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000703
Bram Moolenaar4b779472005-10-04 09:12:31 +0000704 pum_selected = n;
705
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000706 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000707 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000708 if (pum_first > pum_selected - 4)
709 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100710 // scroll down; when we did a jump it's probably a PageUp then
711 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000712 if (pum_first > pum_selected - 2)
713 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000714 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000715 if (pum_first < 0)
716 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000717 else if (pum_first > pum_selected)
718 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000719 }
720 else
721 pum_first = pum_selected;
722 }
723 else if (pum_first < pum_selected - pum_height + 5)
724 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100725 // scroll up; when we did a jump it's probably a PageDown then
726 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000727 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000728 {
729 pum_first += pum_height - 2;
730 if (pum_first < pum_selected - pum_height + 1)
731 pum_first = pum_selected - pum_height + 1;
732 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000733 else
734 pum_first = pum_selected - pum_height + 1;
735 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000736
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100737 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000738 if (context > 3)
739 context = 3;
740 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000741 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000742 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000743 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100744 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000745 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000746 if (pum_first < 0)
747 pum_first = 0;
748 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000749 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000750 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100751 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000752 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000753 }
754 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200755 // adjust for the number of lines displayed
756 if (pum_first > pum_size - pum_height)
757 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000758
Bram Moolenaar4033c552017-09-16 20:54:51 +0200759#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000760 /*
761 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100762 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000763 * Skip this when tried twice already.
764 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000765 * NOTE: Be very careful not to sync undo!
766 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000767 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000768 && Rows > 10
769 && repeat <= 1
770 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000771 {
772 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200773 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000774 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100775# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200776 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200777# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100778# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200779# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100780# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200781 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200782 if (strstr((char *)p_cot, "popuphidden") != NULL)
783 use_popup = USEPOPUP_HIDDEN;
784 else if (strstr((char *)p_cot, "popup") != NULL)
785 use_popup = USEPOPUP_NORMAL;
786 else
787 use_popup = USEPOPUP_NONE;
Bram Moolenaard570ab92019-09-03 23:20:05 +0200788# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200789 // Open a preview window and set "curwin" to it.
790 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000791 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200792 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
793 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200794 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200795 // Prevent undo sync here, if an autocommand syncs undo weird
796 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100797 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200798 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100799 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200800 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000801 g_do_tagpreview = 0;
802
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200803 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100804# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200805 || (curwin->w_popup_flags & POPF_INFO)
806# endif
807 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000808 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200809 if (!resized
810 && curbuf->b_nwindows == 1
811 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200812 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000813 && curbuf->b_p_bh[0] == 'w')
814 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200815 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100816 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000817 ml_delete((linenr_T)1, FALSE);
818 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000819 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000820 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200821 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000822 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000823 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000824 --no_u_sync;
825 if (res == OK)
826 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200827 // Edit a new, empty buffer. Set options for a "wipeout"
828 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000829 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
830 set_option_value((char_u *)"bt", 0L,
831 (char_u *)"nofile", OPT_LOCAL);
832 set_option_value((char_u *)"bh", 0L,
833 (char_u *)"wipe", OPT_LOCAL);
834 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000835 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000836 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000837 }
838 if (res == OK)
839 {
840 char_u *p, *e;
841 linenr_T lnum = 0;
842
843 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
844 {
845 e = vim_strchr(p, '\n');
846 if (e == NULL)
847 {
848 ml_append(lnum++, p, 0, FALSE);
849 break;
850 }
851 else
852 {
853 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000854 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000855 *e = '\n';
856 p = e + 1;
857 }
858 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200859 // delete the empty last line
860 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000861
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100862 // Increase the height of the preview window to show the
863 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200864 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000865 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000866 if (lnum > p_pvh)
867 lnum = p_pvh;
868 if (curwin->w_height < lnum)
869 {
870 win_setheight((int)lnum);
871 resized = TRUE;
872 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000873 }
874
875 curbuf->b_changed = 0;
876 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200877 if (pum_selected != prev_selected)
878 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100879# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200880 curwin->w_firstline = 1;
881# endif
882 curwin->w_topline = 1;
883 }
884 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
885 curwin->w_topline = curbuf->b_ml.ml_line_count;
886 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000887 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100888# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200889 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200890 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200891 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200892 if (win_valid(curwin_save))
893 redraw_win_later(curwin_save, SOME_VALID);
894 }
895# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200896 if ((curwin != curwin_save && win_valid(curwin_save))
897 || (curtab != curtab_save
898 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000899 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200900 if (curtab != curtab_save && valid_tabpage(curtab_save))
901 goto_tabpage_tp(curtab_save, FALSE, FALSE);
902
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100903 // When the first completion is done and the preview
904 // window is not resized, skip the preview window's
905 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200906 if (ins_compl_active() && !resized)
907 curwin->w_redr_status = FALSE;
908
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100909 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000910 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000911 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000912
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100913 // When the preview window was resized we need to
914 // update the view on the buffer. Only go back to
915 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100916 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200917 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000918 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100919 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000920 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100921 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000922 update_topline();
923 }
924
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100925 // Update the screen before drawing the popup menu.
926 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100927 pum_pretend_not_visible = TRUE;
928 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100929 // it causes flicker. When resizing we need to draw
930 // anyway, the position may change later.
931 pum_will_redraw = !resized;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000932 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100933 pum_pretend_not_visible = FALSE;
934 pum_will_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000935
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000936 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100937 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100938# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200939 win_T *wp = curwin;
940# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100941 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000942 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100943 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100944# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200945 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
946 popup_hide(wp);
947# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100948 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000949
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100950 // May need to update the screen again when there are
951 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100952 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100953 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000954 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100955 pum_pretend_not_visible = FALSE;
956 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100957 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000958 }
959 }
960 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100961# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200962 if (WIN_IS_POPUP(curwin))
963 // can't keep focus in a popup window
964 win_enter(firstwin, TRUE);
965# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000966 }
967#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000968 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100969#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200970 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200971 // hide any popup info window
972 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200973#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000974
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000975 if (!resized)
976 pum_redraw();
977
978 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000979}
980
981/*
982 * Undisplay the popup menu (later).
983 */
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000986{
987 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +0200988 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000989 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000990 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100991#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200992 // hide any popup info window
993 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200994#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000995}
996
997/*
998 * Clear the popup menu. Currently only resets the offset to the first
999 * displayed item.
1000 */
1001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001002pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001003{
1004 pum_first = 0;
1005}
1006
1007/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001008 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1009 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1010 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001011 */
1012 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001013pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001014{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001015 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001016}
1017
Bram Moolenaare3226be2005-12-18 22:10:00 +00001018/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001019 * Reposition the popup menu to adjust for window layout changes.
1020 */
1021 void
1022pum_may_redraw(void)
1023{
1024 pumitem_T *array = pum_array;
1025 int len = pum_size;
1026 int selected = pum_selected;
1027
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001028 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001029 return; // nothing to do
1030
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001031 if (pum_window != curwin
1032 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1033 && pum_win_height == curwin->w_height
1034 && pum_win_col == curwin->w_wincol
1035 && pum_win_width == curwin->w_width))
Bram Moolenaar491ac282018-06-17 14:47:55 +02001036 {
1037 // window position didn't change, redraw in the same position
1038 pum_redraw();
1039 }
1040 else
1041 {
1042 int wcol = curwin->w_wcol;
1043
1044 // Window layout changed, recompute the position.
1045 // Use the remembered w_wcol value, the cursor may have moved when a
1046 // completion was inserted, but we want the menu in the same position.
1047 pum_undisplay();
1048 curwin->w_wcol = pum_win_wcol;
1049 curwin->w_valid |= VALID_WCOL;
1050 pum_display(array, len, selected);
1051 curwin->w_wcol = wcol;
1052 }
1053}
1054
1055/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001056 * Return the height of the popup menu, the number of entries visible.
1057 * Only valid when pum_visible() returns TRUE!
1058 */
1059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001060pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001061{
1062 return pum_height;
1063}
1064
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001065#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001066/*
1067 * Add size information about the pum to "dict".
1068 */
1069 void
1070pum_set_event_info(dict_T *dict)
1071{
1072 if (!pum_visible())
1073 return;
1074 dict_add_number(dict, "height", pum_height);
1075 dict_add_number(dict, "width", pum_width);
1076 dict_add_number(dict, "row", pum_row);
1077 dict_add_number(dict, "col", pum_col);
1078 dict_add_number(dict, "size", pum_size);
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001079 dict_add_bool(dict, "scrollbar", pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001080}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001081#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001082
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001083#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001084 static void
1085pum_position_at_mouse(int min_width)
1086{
1087 if (Rows - mouse_row > pum_size)
1088 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001089 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001090 pum_row = mouse_row + 1;
1091 if (pum_height > Rows - pum_row)
1092 pum_height = Rows - pum_row;
1093 }
1094 else
1095 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001096 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001097 pum_row = mouse_row - pum_size;
1098 if (pum_row < 0)
1099 {
1100 pum_height += pum_row;
1101 pum_row = 0;
1102 }
1103 }
1104 if (Columns - mouse_col >= pum_base_width
1105 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001106 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001107 pum_col = mouse_col;
1108 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001109 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001110 pum_col = Columns - (pum_base_width > min_width
1111 ? min_width : pum_base_width);
1112
1113 pum_width = Columns - pum_col;
1114 if (pum_width > pum_base_width + 1)
1115 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001116
1117 // Do not redraw at cursor position.
1118 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001119}
1120
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001121#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001122
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001123#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001124static pumitem_T *balloon_array = NULL;
1125static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001126
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001127# define BALLOON_MIN_WIDTH 50
1128# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001129
Bram Moolenaar246fe032017-11-19 19:56:27 +01001130typedef struct {
1131 char_u *start;
1132 int bytelen;
1133 int cells;
1134 int indent;
1135} balpart_T;
1136
1137/*
1138 * Split a string into parts to display in the balloon.
1139 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1140 * strings and make a struct look good.
1141 * Resulting array is stored in "array" and returns the size of the array.
1142 */
1143 int
1144split_message(char_u *mesg, pumitem_T **array)
1145{
1146 garray_T ga;
1147 char_u *p;
1148 balpart_T *item;
1149 int quoted = FALSE;
1150 int height;
1151 int line;
1152 int item_idx;
1153 int indent = 0;
1154 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001155 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001156 int long_item_count = 0;
1157 int split_long_items = FALSE;
1158
1159 ga_init2(&ga, sizeof(balpart_T), 20);
1160 p = mesg;
1161
1162 while (*p != NUL)
1163 {
1164 if (ga_grow(&ga, 1) == FAIL)
1165 goto failed;
1166 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1167 item->start = p;
1168 item->indent = indent;
1169 item->cells = indent * 2;
1170 ++ga.ga_len;
1171 while (*p != NUL)
1172 {
1173 if (*p == '"')
1174 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001175 else if (*p == '\n')
1176 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001177 else if (*p == '\\' && p[1] != NUL)
1178 ++p;
1179 else if (!quoted)
1180 {
1181 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1182 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001183 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001184 if (*p == '{')
1185 ++indent;
1186 else if (*p == '}' && indent > 0)
1187 --indent;
1188 ++item->cells;
1189 p = skipwhite(p + 1);
1190 break;
1191 }
1192 }
1193 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001194 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001195 }
1196 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001197 if (*p == '\n')
1198 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001199 if (item->cells > max_cells)
1200 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001201 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001202 }
1203
1204 height = 2 + ga.ga_len;
1205
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001206 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001207 if (long_item_count > 0 && height + long_item_count <= max_height)
1208 {
1209 split_long_items = TRUE;
1210 height += long_item_count;
1211 }
1212
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001213 // Limit to half the window height, it has to fit above or below the mouse
1214 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001215 if (height > max_height)
1216 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001217 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001218 if (*array == NULL)
1219 goto failed;
1220
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001221 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001222 (*array)->pum_text = vim_strsave((char_u *)"");
1223 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1224
1225 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1226 {
1227 int skip;
1228 int thislen;
1229 int copylen;
1230 int ind;
1231 int cells;
1232
1233 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001234 if (item->bytelen == 0)
1235 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1236 else
1237 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001238 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001239 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1240 {
1241 cells = item->indent * 2;
1242 for (p = item->start + skip;
1243 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001244 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001245 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1246 break;
1247 thislen = p - (item->start + skip);
1248 }
1249 else
1250 thislen = item->bytelen;
1251
1252 // put indent at the start
1253 p = alloc(thislen + item->indent * 2 + 1);
1254 if (p == NULL)
1255 {
1256 for (line = 0; line <= height - 1; ++line)
1257 vim_free((*array)[line].pum_text);
1258 vim_free(*array);
1259 goto failed;
1260 }
1261 for (ind = 0; ind < item->indent * 2; ++ind)
1262 p[ind] = ' ';
1263
1264 // exclude spaces at the end of the string
1265 for (copylen = thislen; copylen > 0; --copylen)
1266 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001267 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001268
1269 vim_strncpy(p + ind, item->start + skip, copylen);
1270 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001271 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001272 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001273 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001274 }
1275 ga_clear(&ga);
1276 return height;
1277
1278failed:
1279 ga_clear(&ga);
1280 return 0;
1281}
1282
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001283 void
1284ui_remove_balloon(void)
1285{
1286 if (balloon_array != NULL)
1287 {
1288 pum_undisplay();
1289 while (balloon_arraysize > 0)
1290 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001291 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001292 }
1293}
1294
1295/*
1296 * Terminal version of a balloon, uses the popup menu code.
1297 */
1298 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001299ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001300{
1301 ui_remove_balloon();
1302
Bram Moolenaar246fe032017-11-19 19:56:27 +01001303 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001304 {
1305 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001306 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001307 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001308 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001309 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001310 listitem_T *li;
1311 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001312
Bram Moolenaar246fe032017-11-19 19:56:27 +01001313 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001314 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001315 if (balloon_array == NULL)
1316 return;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001317 range_list_materialize(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001318 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001320 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001321
1322 balloon_array[idx].pum_text = vim_strsave(
1323 text == NULL ? (char_u *)"" : text);
1324 }
1325 }
1326 else
1327 balloon_arraysize = split_message(mesg, &balloon_array);
1328
1329 if (balloon_arraysize > 0)
1330 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001331 pum_array = balloon_array;
1332 pum_size = balloon_arraysize;
1333 pum_compute_size();
1334 pum_scrollbar = 0;
1335 pum_height = balloon_arraysize;
1336
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001337 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001338 pum_selected = -1;
1339 pum_first = 0;
1340 pum_redraw();
1341 }
1342}
1343
1344/*
1345 * Called when the mouse moved, may remove any displayed balloon.
1346 */
1347 void
1348ui_may_remove_balloon(void)
1349{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001350 // For now: remove the balloon whenever the mouse moves to another screen
1351 // cell.
1352 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001353}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001354#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001355
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001356#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001357/*
1358 * Select the pum entry at the mouse position.
1359 */
1360 static void
1361pum_select_mouse_pos(void)
1362{
1363 int idx = mouse_row - pum_row;
1364
1365 if (idx < 0 || idx >= pum_size)
1366 pum_selected = -1;
1367 else if (*pum_array[idx].pum_text != NUL)
1368 pum_selected = idx;
1369}
1370
1371/*
1372 * Execute the currently selected popup menu item.
1373 */
1374 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001375pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001376{
1377 vimmenu_T *mp;
1378 int idx = 0;
1379 exarg_T ea;
1380
1381 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001382 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001383 {
1384 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001385 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001386 break;
1387 }
1388}
1389
1390/*
1391 * Open the terminal version of the popup menu and don't return until it is
1392 * closed.
1393 */
1394 void
1395pum_show_popupmenu(vimmenu_T *menu)
1396{
1397 vimmenu_T *mp;
1398 int idx = 0;
1399 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001400# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001401 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001402# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001403 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001404
1405 pum_undisplay();
1406 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001407 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001408
1409 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001410 if (menu_is_separator(mp->dname)
1411 || (mp->modes & mp->enabled & mode))
1412 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001413
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001414 // When there are only Terminal mode menus, using "popup Edit" results in
1415 // pum_size being zero.
1416 if (pum_size <= 0)
1417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001418 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001419 return;
1420 }
1421
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001422 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001423 if (array == NULL)
1424 return;
1425
1426 for (mp = menu->children; mp != NULL; mp = mp->next)
1427 if (menu_is_separator(mp->dname))
1428 array[idx++].pum_text = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001429 else if (mp->modes & mp->enabled & mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001430 array[idx++].pum_text = mp->dname;
1431
1432 pum_array = array;
1433 pum_compute_size();
1434 pum_scrollbar = 0;
1435 pum_height = pum_size;
1436 pum_position_at_mouse(20);
1437
1438 pum_selected = -1;
1439 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001440# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001441 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001442 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001443# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001444
1445 for (;;)
1446 {
1447 int c;
1448
1449 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001450 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001451 out_flush();
1452
1453 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001454
1455 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1456 // menu.
1457 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001458 break;
1459 else if (c == CAR || c == NL)
1460 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001461 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001462 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001463 break;
1464 }
1465 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1466 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001467 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001468 while (pum_selected > 0)
1469 {
1470 --pum_selected;
1471 if (*array[pum_selected].pum_text != NUL)
1472 break;
1473 }
1474 }
1475 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1476 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001477 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001478 while (pum_selected < pum_size - 1)
1479 {
1480 ++pum_selected;
1481 if (*array[pum_selected].pum_text != NUL)
1482 break;
1483 }
1484 }
1485 else if (c == K_RIGHTMOUSE)
1486 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001487 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001488 vungetc(c);
1489 break;
1490 }
1491 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1492 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001493 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001494 pum_select_mouse_pos();
1495 }
1496 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1497 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001498 // left mouse click: select clicked item, if any, and close;
1499 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001500 pum_select_mouse_pos();
1501 if (pum_selected >= 0)
1502 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001503 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001504 break;
1505 }
1506 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1507 break;
1508 }
1509 }
1510
1511 vim_free(array);
1512 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001513# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001514 p_bevalterm = save_bevalterm;
1515 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001516# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001517}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001518
1519 void
1520pum_make_popup(char_u *path_name, int use_mouse_pos)
1521{
1522 vimmenu_T *menu;
1523
1524 if (!use_mouse_pos)
1525 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001526 // Hack: set mouse position at the cursor so that the menu pops up
1527 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001528 mouse_row = curwin->w_winrow + curwin->w_wrow;
1529 mouse_col = curwin->w_wincol + curwin->w_wcol;
1530 }
1531
1532 menu = gui_find_menu(path_name);
1533 if (menu != NULL)
1534 pum_show_popupmenu(menu);
1535}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001536#endif