blob: 1f9840804027d63ec520e260ae5fb3e25e198f01 [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 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020063 if (pum_array[i].pum_text != NULL)
64 {
65 w = vim_strsize(pum_array[i].pum_text);
66 if (pum_base_width < w)
67 pum_base_width = w;
68 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010069 if (pum_array[i].pum_kind != NULL)
70 {
71 w = vim_strsize(pum_array[i].pum_kind) + 1;
72 if (pum_kind_width < w)
73 pum_kind_width = w;
74 }
75 if (pum_array[i].pum_extra != NULL)
76 {
77 w = vim_strsize(pum_array[i].pum_extra) + 1;
78 if (pum_extra_width < w)
79 pum_extra_width = w;
80 }
81 }
82}
83
Bram Moolenaar4b779472005-10-04 09:12:31 +000084/*
85 * Show the popup menu with items "array[size]".
86 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010087 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000088 * The menu appears above the screen line "row" or at "row" + "height" - 1.
89 */
90 void
Bram Moolenaar05540972016-01-30 20:31:25 +010091pum_display(
92 pumitem_T *array,
93 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094 int selected) // index of initially selected item, none if
95 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000096{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000097 int def_width;
98 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000099 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100100 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100101 int above_row;
102 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000103 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200104#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100105 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100106#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000107
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 do
109 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100110 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200111 above_row = 0;
112 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000113
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100114 // Pretend the pum is already there to avoid that must_redraw is set
115 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200116 pum_array = (pumitem_T *)1;
117 validate_cursor_col();
118 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000119
Bram Moolenaar491ac282018-06-17 14:47:55 +0200120 // Remember the essential parts of the window position and size, so we
121 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200122 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200123 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
124 pum_win_height = curwin->w_height;
125 pum_win_col = curwin->w_wincol;
126 pum_win_wcol = curwin->w_wcol;
127 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000128
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200130 FOR_ALL_WINDOWS(pvwin)
131 if (pvwin->w_p_pvw)
132 break;
133 if (pvwin != NULL)
134 {
135 if (W_WINROW(pvwin) < W_WINROW(curwin))
136 above_row = W_WINROW(pvwin) + pvwin->w_height;
137 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
138 below_row = W_WINROW(pvwin);
139 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100140#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141
Bram Moolenaara5e66212017-09-29 22:42:33 +0200142 /*
143 * Figure out the size and position of the pum.
144 */
145 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000146 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000147 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200148 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000149 if (p_ph > 0 && pum_height > p_ph)
150 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000151
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100152 // Put the pum below "pum_win_row" if possible. If there are few lines
153 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200154 if (pum_win_row + 2 >= below_row - pum_height
155 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200156 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100157 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200158
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100159 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200160 if (curwin->w_wrow - curwin->w_cline_row >= 2)
161 context_lines = 2;
162 else
163 context_lines = curwin->w_wrow - curwin->w_cline_row;
164
Bram Moolenaar491ac282018-06-17 14:47:55 +0200165 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200168 pum_height = size;
169 }
170 else
171 {
172 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200173 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200174 }
175 if (p_ph > 0 && pum_height > p_ph)
176 {
177 pum_row += pum_height - p_ph;
178 pum_height = p_ph;
179 }
180 }
181 else
182 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100183 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100185 // Leave two lines of context if possible
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100186 validate_cheight();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200187 if (curwin->w_cline_row
188 + curwin->w_cline_height - curwin->w_wrow >= 3)
189 context_lines = 3;
190 else
191 context_lines = curwin->w_cline_row
192 + curwin->w_cline_height - curwin->w_wrow;
193
Bram Moolenaar491ac282018-06-17 14:47:55 +0200194 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195 if (size > below_row - pum_row)
196 pum_height = below_row - pum_row;
197 else
198 pum_height = size;
199 if (p_ph > 0 && pum_height > p_ph)
200 pum_height = p_ph;
201 }
202
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100203 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200204 if (pum_height < 1 || (pum_height == 1 && size > 1))
205 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000206
Bram Moolenaar4033c552017-09-16 20:54:51 +0200207#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100208 // If there is a preview window above avoid drawing over it.
209 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000210 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100211 pum_row = above_row;
212 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000213 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214#endif
215
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100216 pum_array = array;
217 pum_size = size;
218 pum_compute_size();
219 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000220
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100221 // Calculate column
Bram Moolenaarabc97732007-08-08 20:49:37 +0000222#ifdef FEAT_RIGHTLEFT
223 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100224 cursor_col = curwin->w_wincol + curwin->w_width
225 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000226 else
227#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100228 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000229
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100230 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200231 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000232 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200233 pum_scrollbar = 1;
234 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000235 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000236 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200237 pum_scrollbar = 0;
238
239 if (def_width < max_width)
240 def_width = max_width;
241
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100242 if (((cursor_col < Columns - p_pw
243 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000244#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200245 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100246 || (curwin->w_p_rl
247 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000248#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200249 ))
250 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100251 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100252 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000253
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100254 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200255#ifdef FEAT_RIGHTLEFT
256 if (curwin->w_p_rl)
257 pum_width = pum_col - pum_scrollbar + 1;
258 else
259#endif
260 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000261
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100262 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100263 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200264 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100265 // the width is more than needed for the items, make it
266 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100267 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100268 if (pum_width < p_pw)
269 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200270 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100271 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100272#ifdef FEAT_RIGHTLEFT
273 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100274 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
275 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100276#endif
277 ))
278 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100279 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100280#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100281 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100282 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100283 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100284 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100285 if (pum_col >= Columns)
286 pum_col = Columns - 1;
287 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100288 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100289#endif
290 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100291 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
292 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100293 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100294 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100295 pum_col = Columns - max_width - pum_scrollbar;
296 if (pum_col < 0)
297 pum_col = 0;
298 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100299 }
300
301#ifdef FEAT_RIGHTLEFT
302 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100303 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304 else
305#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100306 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100307
Bram Moolenaar42443c72018-02-10 18:28:52 +0100308 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100309 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100310 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100311#ifdef FEAT_RIGHTLEFT
312 if (curwin->w_p_rl)
313 {
314 if (pum_width > pum_col)
315 pum_width = pum_col;
316 }
317 else
318#endif
319 {
320 if (pum_width >= Columns - pum_col)
321 pum_width = Columns - pum_col - 1;
322 }
323 }
324 else if (pum_width > max_width + pum_kind_width
325 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100326 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100327 {
328 pum_width = max_width + pum_kind_width
329 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100330 if (pum_width < p_pw)
331 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100332 }
333 }
334
Bram Moolenaara5e66212017-09-29 22:42:33 +0200335 }
336 else if (Columns < def_width)
337 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100338 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200339#ifdef FEAT_RIGHTLEFT
340 if (curwin->w_p_rl)
341 pum_col = Columns - 1;
342 else
343#endif
344 pum_col = 0;
345 pum_width = Columns - 1;
346 }
347 else
348 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100349 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100350 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200351#ifdef FEAT_RIGHTLEFT
352 if (curwin->w_p_rl)
353 pum_col = max_width - 1;
354 else
355#endif
356 pum_col = Columns - max_width;
357 pum_width = max_width - pum_scrollbar;
358 }
359
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100360 // Set selected item and redraw. If the window size changed need to
361 // redo the positioning. Limit this to two times, when there is not
362 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200363 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100364
365 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000366}
367
368/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100369 * Set a flag that when pum_redraw() is called it first calls update_screen().
370 * This will avoid clearing and redrawing the popup menu, prevent flicker.
371 */
372 void
373pum_call_update_screen()
374{
375 call_update_screen = TRUE;
376
377 // Update the cursor position to be able to compute the popup menu
378 // position. The cursor line length may have changed because of the
379 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100380 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100381 validate_cursor();
382}
383
384/*
385 * Return TRUE if we are going to redraw the popup menu and the screen position
386 * "row"/"col" is under the popup menu.
387 */
388 int
389pum_under_menu(int row, int col)
390{
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100391 return pum_will_redraw
Bram Moolenaarae654382019-01-17 21:09:05 +0100392 && row >= pum_row
393 && row < pum_row + pum_height
394 && col >= pum_col - 1
395 && col < pum_col + pum_width;
396}
397
398/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000399 * Redraw the popup menu, using "pum_first" and "pum_selected".
400 */
401 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100402pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000403{
404 int row = pum_row;
405 int col;
406 int attr_norm = highlight_attr[HLF_PNI];
407 int attr_select = highlight_attr[HLF_PSI];
408 int attr_scroll = highlight_attr[HLF_PSB];
409 int attr_thumb = highlight_attr[HLF_PST];
410 int attr;
411 int i;
412 int idx;
413 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000414 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000415 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000416 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100417 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000418 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000419 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000420
Bram Moolenaarae654382019-01-17 21:09:05 +0100421 if (call_update_screen)
422 {
423 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100424 // Do not redraw in pum_may_redraw() and don't draw in the area where
425 // the popup menu will be.
426 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100427 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100428 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100429 }
430
431 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100432 if (pum_first > pum_size - pum_height)
433 pum_first = pum_size - pum_height;
434
Bram Moolenaar4b779472005-10-04 09:12:31 +0000435 if (pum_scrollbar)
436 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100437 thumb_height = pum_height * pum_height / pum_size;
438 if (thumb_height == 0)
439 thumb_height = 1;
440 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000441 + (pum_size - pum_height) / 2)
442 / (pum_size - pum_height);
443 }
444
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100445#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200446 // The popup menu is drawn over popup menus with zindex under
447 // POPUPMENU_ZINDEX.
448 screen_zindex = POPUPMENU_ZINDEX;
449#endif
450
Bram Moolenaar4b779472005-10-04 09:12:31 +0000451 for (i = 0; i < pum_height; ++i)
452 {
453 idx = i + pum_first;
454 attr = (idx == pum_selected) ? attr_select : attr_norm;
455
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100456 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000457#ifdef FEAT_RIGHTLEFT
458 if (curwin->w_p_rl)
459 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200460 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000461 screen_putchar(' ', row, pum_col + 1, attr);
462 }
463 else
464#endif
465 if (pum_col > 0)
466 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000467
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100468 // Display each entry, use two spaces for a Tab.
469 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000470 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000471 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000472 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000473 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000474 width = 0;
475 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000476 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000477 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000478 case 1: p = pum_array[idx].pum_text; break;
479 case 2: p = pum_array[idx].pum_kind; break;
480 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000481 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000482 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100483 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000484 {
485 if (s == NULL)
486 s = p;
487 w = ptr2cells(p);
488 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
489 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100490 // Display the text that fits or comes before a Tab.
491 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000492 char_u *st;
493 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000494
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100495 if (saved != NUL)
496 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000497 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100498 if (saved != NUL)
499 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000500#ifdef FEAT_RIGHTLEFT
501 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000502 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000503 if (st != NULL)
504 {
505 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000506
507 if (rt != NULL)
508 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100509 char_u *rt_start = rt;
510 int size;
511
512 size = vim_strsize(rt);
513 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000514 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100515 do
516 {
517 size -= has_mbyte
518 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100519 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100520 } while (size > pum_width);
521
522 if (size < pum_width)
523 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100524 // Most left character requires
525 // 2-cells but only 1 cell is
526 // available on screen. Put a
527 // '<' on the left of the pum
528 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100529 *(--rt) = '<';
530 size++;
531 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000532 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100533 screen_puts_len(rt, (int)STRLEN(rt),
534 row, col - size + 1, attr);
535 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000536 }
537 vim_free(st);
538 }
539 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000540 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000541 else
542#endif
543 {
544 if (st != NULL)
545 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100546 int size = (int)STRLEN(st);
547 int cells = (*mb_string2cells)(st, size);
548
549 // only draw the text that fits
550 while (size > 0
551 && col + cells > pum_width + pum_col)
552 {
553 --size;
554 if (has_mbyte)
555 {
556 size -= (*mb_head_off)(st, st + size);
557 cells -= (*mb_ptr2cells)(st + size);
558 }
559 else
560 --cells;
561 }
562 screen_puts_len(st, size, row, col, attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000563 vim_free(st);
564 }
565 col += width;
566 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000567
568 if (*p != TAB)
569 break;
570
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100571 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000572#ifdef FEAT_RIGHTLEFT
573 if (curwin->w_p_rl)
574 {
575 screen_puts_len((char_u *)" ", 2, row, col - 1,
576 attr);
577 col -= 2;
578 }
579 else
580#endif
581 {
582 screen_puts_len((char_u *)" ", 2, row, col, attr);
583 col += 2;
584 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000585 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100586 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000587 width = 0;
588 }
589 else
590 width += w;
591 }
592
593 if (round > 1)
594 n = pum_kind_width + 1;
595 else
596 n = 1;
597
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100598 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000599 if (round == 3
600 || (round == 2 && pum_array[idx].pum_extra == NULL)
601 || (round == 1 && pum_array[idx].pum_kind == NULL
602 && pum_array[idx].pum_extra == NULL)
603 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000604 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000605#ifdef FEAT_RIGHTLEFT
606 if (curwin->w_p_rl)
607 {
608 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
609 col + 1, ' ', ' ', attr);
610 col = pum_col - pum_base_width - n + 1;
611 }
612 else
613#endif
614 {
615 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000616 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000617 col = pum_col + pum_base_width + n;
618 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000619 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000620 }
621
Bram Moolenaarabc97732007-08-08 20:49:37 +0000622#ifdef FEAT_RIGHTLEFT
623 if (curwin->w_p_rl)
624 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
625 ' ', attr);
626 else
627#endif
628 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
629 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000630 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000631 {
632#ifdef FEAT_RIGHTLEFT
633 if (curwin->w_p_rl)
634 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100635 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000636 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000637 else
638#endif
639 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100640 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000641 ? attr_thumb : attr_scroll);
642 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000643
644 ++row;
645 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200646
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100647#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200648 screen_zindex = 0;
649#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000650}
651
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100652#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200653/*
654 * Position the info popup relative to the popup menu item.
655 */
656 void
657pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200658{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100659 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200660 int row = pum_row;
661 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200662 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200663
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200664 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200665 if (Columns - col < 20 && Columns - col < pum_col)
666 {
667 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200668 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200669 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200670 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200671 }
672 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200673 wp->w_maxwidth = Columns - col + 1;
674 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200675 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
676 {
677 // option value overrules computed value
678 wp->w_maxwidth = wp->w_maxwidth_opt;
679 used_maxwidth_opt = TRUE;
680 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200681
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200682 row -= popup_top_extra(wp);
683 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200684 {
685 if (pum_row < pum_win_row)
686 {
687 // menu above cursor line, align with bottom
688 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200689 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200690 }
691 else
692 // menu below cursor line, align with top
693 row += 1;
694 }
695 else
696 // align with the selected item
697 row += pum_selected - pum_first + 1;
698
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100699 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200700 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100701 // The popup is not going to fit or will overlap with the cursor
702 // position, hide the popup.
703 wp->w_popup_flags |= POPF_HIDDEN;
704 else
705 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200706}
707#endif
708
Bram Moolenaar4b779472005-10-04 09:12:31 +0000709/*
710 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000711 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000712 * This may be repeated when the preview window is used:
713 * "repeat" == 0: open preview window normally
714 * "repeat" == 1: open preview window but don't set the size
715 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000716 * Returns TRUE when the window was resized and the location of the popup menu
717 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000718 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000719 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200720pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000721{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000722 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000723 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200724#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200725 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200726#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100727#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200728 int has_info = FALSE;
729#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000730
Bram Moolenaar4b779472005-10-04 09:12:31 +0000731 pum_selected = n;
732
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000733 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000734 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000735 if (pum_first > pum_selected - 4)
736 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100737 // scroll down; when we did a jump it's probably a PageUp then
738 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000739 if (pum_first > pum_selected - 2)
740 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000741 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000742 if (pum_first < 0)
743 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000744 else if (pum_first > pum_selected)
745 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000746 }
747 else
748 pum_first = pum_selected;
749 }
750 else if (pum_first < pum_selected - pum_height + 5)
751 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100752 // scroll up; when we did a jump it's probably a PageDown then
753 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000754 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000755 {
756 pum_first += pum_height - 2;
757 if (pum_first < pum_selected - pum_height + 1)
758 pum_first = pum_selected - pum_height + 1;
759 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000760 else
761 pum_first = pum_selected - pum_height + 1;
762 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000763
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100764 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000765 if (context > 3)
766 context = 3;
767 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000768 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000769 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100771 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000772 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000773 if (pum_first < 0)
774 pum_first = 0;
775 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000776 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000777 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100778 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000779 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000780 }
781 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200782 // adjust for the number of lines displayed
783 if (pum_first > pum_size - pum_height)
784 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000785
Bram Moolenaar4033c552017-09-16 20:54:51 +0200786#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000787 /*
788 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100789 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000790 * Skip this when tried twice already.
791 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000792 * NOTE: Be very careful not to sync undo!
793 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000794 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000795 && Rows > 10
796 && repeat <= 1
797 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000798 {
799 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200800 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000801 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100802# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200803 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200804# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100805# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200806# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100807# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200808 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200809 if (strstr((char *)p_cot, "popuphidden") != NULL)
810 use_popup = USEPOPUP_HIDDEN;
811 else if (strstr((char *)p_cot, "popup") != NULL)
812 use_popup = USEPOPUP_NORMAL;
813 else
814 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100815 if (use_popup != USEPOPUP_NONE)
816 // don't use WinEnter or WinLeave autocommands for the info
817 // popup
818 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200819# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200820 // Open a preview window and set "curwin" to it.
821 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000822 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200823 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
824 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200825 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200826 // Prevent undo sync here, if an autocommand syncs undo weird
827 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100828 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200829 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100830 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200831 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000832 g_do_tagpreview = 0;
833
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200834 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100835# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200836 || (curwin->w_popup_flags & POPF_INFO)
837# endif
838 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000839 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200840 if (!resized
841 && curbuf->b_nwindows == 1
842 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200843 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000844 && curbuf->b_p_bh[0] == 'w')
845 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200846 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100847 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200848 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000849 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000850 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000851 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200852 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000853 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000854 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000855 --no_u_sync;
856 if (res == OK)
857 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200858 // Edit a new, empty buffer. Set options for a "wipeout"
859 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000860 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
Bram Moolenaard356fc62020-12-09 18:13:44 +0100861 set_option_value((char_u *)"bl", 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000862 set_option_value((char_u *)"bt", 0L,
863 (char_u *)"nofile", OPT_LOCAL);
864 set_option_value((char_u *)"bh", 0L,
865 (char_u *)"wipe", OPT_LOCAL);
866 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000867 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000868 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000869 }
870 if (res == OK)
871 {
872 char_u *p, *e;
873 linenr_T lnum = 0;
874
875 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
876 {
877 e = vim_strchr(p, '\n');
878 if (e == NULL)
879 {
880 ml_append(lnum++, p, 0, FALSE);
881 break;
882 }
883 else
884 {
885 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000886 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000887 *e = '\n';
888 p = e + 1;
889 }
890 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200891 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +0200892 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000893
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100894 // Increase the height of the preview window to show the
895 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200896 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000897 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (lnum > p_pvh)
899 lnum = p_pvh;
900 if (curwin->w_height < lnum)
901 {
902 win_setheight((int)lnum);
903 resized = TRUE;
904 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000905 }
906
907 curbuf->b_changed = 0;
908 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200909 if (pum_selected != prev_selected)
910 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100911# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200912 curwin->w_firstline = 1;
913# endif
914 curwin->w_topline = 1;
915 }
916 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
917 curwin->w_topline = curbuf->b_ml.ml_line_count;
918 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000919 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100920# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200921 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200922 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200923 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200924 if (win_valid(curwin_save))
925 redraw_win_later(curwin_save, SOME_VALID);
926 }
927# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200928 if ((curwin != curwin_save && win_valid(curwin_save))
929 || (curtab != curtab_save
930 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000931 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200932 if (curtab != curtab_save && valid_tabpage(curtab_save))
933 goto_tabpage_tp(curtab_save, FALSE, FALSE);
934
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100935 // When the first completion is done and the preview
936 // window is not resized, skip the preview window's
937 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200938 if (ins_compl_active() && !resized)
939 curwin->w_redr_status = FALSE;
940
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100941 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000942 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000943 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000944
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100945 // When the preview window was resized we need to
946 // update the view on the buffer. Only go back to
947 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100948 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200949 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000950 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100951 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000952 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100953 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000954 update_topline();
955 }
956
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100957 // Update the screen before drawing the popup menu.
958 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100959 pum_pretend_not_visible = TRUE;
960 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100961 // it causes flicker. When resizing we need to draw
962 // anyway, the position may change later.
963 pum_will_redraw = !resized;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000964 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100965 pum_pretend_not_visible = FALSE;
966 pum_will_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000967
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000968 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100969 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100970# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200971 win_T *wp = curwin;
972# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100973 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000974 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100975 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100976# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200977 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
978 popup_hide(wp);
979# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100980 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000981
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100982 // May need to update the screen again when there are
983 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100984 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100985 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000986 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100987 pum_pretend_not_visible = FALSE;
988 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100989 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000990 }
991 }
992 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100993# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200994 if (WIN_IS_POPUP(curwin))
995 // can't keep focus in a popup window
996 win_enter(firstwin, TRUE);
997# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100998# ifdef FEAT_PROP_POPUP
999 if (use_popup != USEPOPUP_NONE)
1000 unblock_autocmds();
1001# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001002 }
1003#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001004 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001005#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001006 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001007 // hide any popup info window
1008 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001009#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001010
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001011 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001012}
1013
1014/*
1015 * Undisplay the popup menu (later).
1016 */
1017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001018pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001019{
1020 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +02001021 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001022 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001023 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001024#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001025 // hide any popup info window
1026 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001027#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001028}
1029
1030/*
1031 * Clear the popup menu. Currently only resets the offset to the first
1032 * displayed item.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001036{
1037 pum_first = 0;
1038}
1039
1040/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001041 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1042 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1043 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001044 */
1045 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001046pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001047{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001048 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001049}
1050
Bram Moolenaare3226be2005-12-18 22:10:00 +00001051/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001052 * Reposition the popup menu to adjust for window layout changes.
1053 */
1054 void
1055pum_may_redraw(void)
1056{
1057 pumitem_T *array = pum_array;
1058 int len = pum_size;
1059 int selected = pum_selected;
1060
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001061 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001062 return; // nothing to do
1063
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001064 if (pum_window != curwin
1065 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1066 && pum_win_height == curwin->w_height
1067 && pum_win_col == curwin->w_wincol
1068 && pum_win_width == curwin->w_width))
Bram Moolenaar491ac282018-06-17 14:47:55 +02001069 {
1070 // window position didn't change, redraw in the same position
1071 pum_redraw();
1072 }
1073 else
1074 {
1075 int wcol = curwin->w_wcol;
1076
1077 // Window layout changed, recompute the position.
1078 // Use the remembered w_wcol value, the cursor may have moved when a
1079 // completion was inserted, but we want the menu in the same position.
1080 pum_undisplay();
1081 curwin->w_wcol = pum_win_wcol;
1082 curwin->w_valid |= VALID_WCOL;
1083 pum_display(array, len, selected);
1084 curwin->w_wcol = wcol;
1085 }
1086}
1087
1088/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001089 * Return the height of the popup menu, the number of entries visible.
1090 * Only valid when pum_visible() returns TRUE!
1091 */
1092 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001093pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001094{
1095 return pum_height;
1096}
1097
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001098#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001099/*
1100 * Add size information about the pum to "dict".
1101 */
1102 void
1103pum_set_event_info(dict_T *dict)
1104{
1105 if (!pum_visible())
1106 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001107 (void)dict_add_number(dict, "height", pum_height);
1108 (void)dict_add_number(dict, "width", pum_width);
1109 (void)dict_add_number(dict, "row", pum_row);
1110 (void)dict_add_number(dict, "col", pum_col);
1111 (void)dict_add_number(dict, "size", pum_size);
1112 (void)dict_add_bool(dict, "scrollbar",
1113 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001114}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001115#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001116
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001117#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001118 static void
1119pum_position_at_mouse(int min_width)
1120{
1121 if (Rows - mouse_row > pum_size)
1122 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001123 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001124 pum_row = mouse_row + 1;
1125 if (pum_height > Rows - pum_row)
1126 pum_height = Rows - pum_row;
1127 }
1128 else
1129 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001130 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001131 pum_row = mouse_row - pum_size;
1132 if (pum_row < 0)
1133 {
1134 pum_height += pum_row;
1135 pum_row = 0;
1136 }
1137 }
1138 if (Columns - mouse_col >= pum_base_width
1139 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001140 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001141 pum_col = mouse_col;
1142 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001143 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001144 pum_col = Columns - (pum_base_width > min_width
1145 ? min_width : pum_base_width);
1146
1147 pum_width = Columns - pum_col;
1148 if (pum_width > pum_base_width + 1)
1149 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001150
1151 // Do not redraw at cursor position.
1152 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001153}
1154
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001155#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001156
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001157#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001158static pumitem_T *balloon_array = NULL;
1159static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001160
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001161# define BALLOON_MIN_WIDTH 50
1162# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001163
Bram Moolenaar246fe032017-11-19 19:56:27 +01001164typedef struct {
1165 char_u *start;
1166 int bytelen;
1167 int cells;
1168 int indent;
1169} balpart_T;
1170
1171/*
1172 * Split a string into parts to display in the balloon.
1173 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1174 * strings and make a struct look good.
1175 * Resulting array is stored in "array" and returns the size of the array.
1176 */
1177 int
1178split_message(char_u *mesg, pumitem_T **array)
1179{
1180 garray_T ga;
1181 char_u *p;
1182 balpart_T *item;
1183 int quoted = FALSE;
1184 int height;
1185 int line;
1186 int item_idx;
1187 int indent = 0;
1188 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001189 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001190 int long_item_count = 0;
1191 int split_long_items = FALSE;
1192
1193 ga_init2(&ga, sizeof(balpart_T), 20);
1194 p = mesg;
1195
1196 while (*p != NUL)
1197 {
1198 if (ga_grow(&ga, 1) == FAIL)
1199 goto failed;
1200 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1201 item->start = p;
1202 item->indent = indent;
1203 item->cells = indent * 2;
1204 ++ga.ga_len;
1205 while (*p != NUL)
1206 {
1207 if (*p == '"')
1208 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001209 else if (*p == '\n')
1210 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001211 else if (*p == '\\' && p[1] != NUL)
1212 ++p;
1213 else if (!quoted)
1214 {
1215 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1216 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001217 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001218 if (*p == '{')
1219 ++indent;
1220 else if (*p == '}' && indent > 0)
1221 --indent;
1222 ++item->cells;
1223 p = skipwhite(p + 1);
1224 break;
1225 }
1226 }
1227 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001228 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001229 }
1230 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001231 if (*p == '\n')
1232 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001233 if (item->cells > max_cells)
1234 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001235 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001236 }
1237
1238 height = 2 + ga.ga_len;
1239
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001240 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001241 if (long_item_count > 0 && height + long_item_count <= max_height)
1242 {
1243 split_long_items = TRUE;
1244 height += long_item_count;
1245 }
1246
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001247 // Limit to half the window height, it has to fit above or below the mouse
1248 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001249 if (height > max_height)
1250 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001251 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001252 if (*array == NULL)
1253 goto failed;
1254
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001255 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001256 (*array)->pum_text = vim_strsave((char_u *)"");
1257 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1258
1259 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1260 {
1261 int skip;
1262 int thislen;
1263 int copylen;
1264 int ind;
1265 int cells;
1266
1267 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001268 if (item->bytelen == 0)
1269 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1270 else
1271 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001272 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001273 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1274 {
1275 cells = item->indent * 2;
1276 for (p = item->start + skip;
1277 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001278 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001279 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1280 break;
1281 thislen = p - (item->start + skip);
1282 }
1283 else
1284 thislen = item->bytelen;
1285
1286 // put indent at the start
1287 p = alloc(thislen + item->indent * 2 + 1);
1288 if (p == NULL)
1289 {
1290 for (line = 0; line <= height - 1; ++line)
1291 vim_free((*array)[line].pum_text);
1292 vim_free(*array);
1293 goto failed;
1294 }
1295 for (ind = 0; ind < item->indent * 2; ++ind)
1296 p[ind] = ' ';
1297
1298 // exclude spaces at the end of the string
1299 for (copylen = thislen; copylen > 0; --copylen)
1300 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001301 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001302
1303 vim_strncpy(p + ind, item->start + skip, copylen);
1304 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001305 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001306 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001307 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001308 }
1309 ga_clear(&ga);
1310 return height;
1311
1312failed:
1313 ga_clear(&ga);
1314 return 0;
1315}
1316
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001317 void
1318ui_remove_balloon(void)
1319{
1320 if (balloon_array != NULL)
1321 {
1322 pum_undisplay();
1323 while (balloon_arraysize > 0)
1324 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001325 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001326 }
1327}
1328
1329/*
1330 * Terminal version of a balloon, uses the popup menu code.
1331 */
1332 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001333ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001334{
1335 ui_remove_balloon();
1336
Bram Moolenaar246fe032017-11-19 19:56:27 +01001337 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001338 {
1339 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001340 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001341 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001342 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001343 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001344 listitem_T *li;
1345 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001346
Bram Moolenaar246fe032017-11-19 19:56:27 +01001347 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001348 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001349 if (balloon_array == NULL)
1350 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001351 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001352 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001354 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001355
1356 balloon_array[idx].pum_text = vim_strsave(
1357 text == NULL ? (char_u *)"" : text);
1358 }
1359 }
1360 else
1361 balloon_arraysize = split_message(mesg, &balloon_array);
1362
1363 if (balloon_arraysize > 0)
1364 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001365 pum_array = balloon_array;
1366 pum_size = balloon_arraysize;
1367 pum_compute_size();
1368 pum_scrollbar = 0;
1369 pum_height = balloon_arraysize;
1370
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001371 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001372 pum_selected = -1;
1373 pum_first = 0;
1374 pum_redraw();
1375 }
1376}
1377
1378/*
1379 * Called when the mouse moved, may remove any displayed balloon.
1380 */
1381 void
1382ui_may_remove_balloon(void)
1383{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001384 // For now: remove the balloon whenever the mouse moves to another screen
1385 // cell.
1386 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001387}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001388#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001389
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001390#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001391/*
1392 * Select the pum entry at the mouse position.
1393 */
1394 static void
1395pum_select_mouse_pos(void)
1396{
1397 int idx = mouse_row - pum_row;
1398
1399 if (idx < 0 || idx >= pum_size)
1400 pum_selected = -1;
1401 else if (*pum_array[idx].pum_text != NUL)
1402 pum_selected = idx;
1403}
1404
1405/*
1406 * Execute the currently selected popup menu item.
1407 */
1408 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001409pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001410{
1411 vimmenu_T *mp;
1412 int idx = 0;
1413 exarg_T ea;
1414
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001415 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001416 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001417 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001418 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001419 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001420 break;
1421 }
1422}
1423
1424/*
1425 * Open the terminal version of the popup menu and don't return until it is
1426 * closed.
1427 */
1428 void
1429pum_show_popupmenu(vimmenu_T *menu)
1430{
1431 vimmenu_T *mp;
1432 int idx = 0;
1433 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001434# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001435 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001436# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001437 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001438
1439 pum_undisplay();
1440 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001441 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001442
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001443 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001444 if (menu_is_separator(mp->dname)
1445 || (mp->modes & mp->enabled & mode))
1446 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001447
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001448 // When there are only Terminal mode menus, using "popup Edit" results in
1449 // pum_size being zero.
1450 if (pum_size <= 0)
1451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001452 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001453 return;
1454 }
1455
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001456 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001457 if (array == NULL)
1458 return;
1459
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001460 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001461 {
1462 char_u *s = NULL;
1463
1464 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001465 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001466 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001467 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001468 s = mp->dname;
1469 if (s != NULL)
1470 {
1471 s = vim_strsave(s);
1472 if (s != NULL)
1473 array[idx++].pum_text = s;
1474 }
1475 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001476
1477 pum_array = array;
1478 pum_compute_size();
1479 pum_scrollbar = 0;
1480 pum_height = pum_size;
1481 pum_position_at_mouse(20);
1482
1483 pum_selected = -1;
1484 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001485# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001486 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001487 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001488# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001489
1490 for (;;)
1491 {
1492 int c;
1493
1494 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001495 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001496 out_flush();
1497
1498 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001499
1500 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1501 // menu.
1502 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001503 break;
1504 else if (c == CAR || c == NL)
1505 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001506 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001507 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001508 break;
1509 }
1510 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1511 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001512 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001513 while (pum_selected > 0)
1514 {
1515 --pum_selected;
1516 if (*array[pum_selected].pum_text != NUL)
1517 break;
1518 }
1519 }
1520 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001522 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001523 while (pum_selected < pum_size - 1)
1524 {
1525 ++pum_selected;
1526 if (*array[pum_selected].pum_text != NUL)
1527 break;
1528 }
1529 }
1530 else if (c == K_RIGHTMOUSE)
1531 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001533 vungetc(c);
1534 break;
1535 }
1536 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1537 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001538 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001539 pum_select_mouse_pos();
1540 }
1541 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1542 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001543 // left mouse click: select clicked item, if any, and close;
1544 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001545 pum_select_mouse_pos();
1546 if (pum_selected >= 0)
1547 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001548 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001549 break;
1550 }
1551 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1552 break;
1553 }
1554 }
1555
Bram Moolenaar38455a92020-12-24 18:39:02 +01001556 for (idx = 0; idx < pum_size; ++idx)
1557 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001558 vim_free(array);
1559 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001560# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001561 p_bevalterm = save_bevalterm;
1562 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001563# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001564}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001565
1566 void
1567pum_make_popup(char_u *path_name, int use_mouse_pos)
1568{
1569 vimmenu_T *menu;
1570
1571 if (!use_mouse_pos)
1572 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001573 // Hack: set mouse position at the cursor so that the menu pops up
1574 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001575 mouse_row = curwin->w_winrow + curwin->w_wrow;
1576 mouse_col = curwin->w_wincol + curwin->w_wcol;
1577 }
1578
1579 menu = gui_find_menu(path_name);
1580 if (menu != NULL)
1581 pum_show_popupmenu(menu);
1582}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001583#endif