blob: 86a9cbb0704a6b9d034b4d5aa71588136e6d5e6e [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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010043static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000044
Bram Moolenaar4b779472005-10-04 09:12:31 +000045#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000046
Bram Moolenaar51b0f372017-11-18 18:52:04 +010047 static void
48pum_compute_size(void)
49{
50 int i;
51 int w;
52
Bram Moolenaar63d9e732019-12-05 21:10:38 +010053 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010054 pum_base_width = 0;
55 pum_kind_width = 0;
56 pum_extra_width = 0;
57 for (i = 0; i < pum_size; ++i)
58 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020059 if (pum_array[i].pum_text != NULL)
60 {
61 w = vim_strsize(pum_array[i].pum_text);
62 if (pum_base_width < w)
63 pum_base_width = w;
64 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010065 if (pum_array[i].pum_kind != NULL)
66 {
67 w = vim_strsize(pum_array[i].pum_kind) + 1;
68 if (pum_kind_width < w)
69 pum_kind_width = w;
70 }
71 if (pum_array[i].pum_extra != NULL)
72 {
73 w = vim_strsize(pum_array[i].pum_extra) + 1;
74 if (pum_extra_width < w)
75 pum_extra_width = w;
76 }
77 }
78}
79
Bram Moolenaar4b779472005-10-04 09:12:31 +000080/*
81 * Show the popup menu with items "array[size]".
82 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010083 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000084 * The menu appears above the screen line "row" or at "row" + "height" - 1.
85 */
86 void
Bram Moolenaar05540972016-01-30 20:31:25 +010087pum_display(
88 pumitem_T *array,
89 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010090 int selected) // index of initially selected item, none if
91 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000092{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093 int def_width;
94 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000095 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010096 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010097 int above_row;
98 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000099 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100101 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100102#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000103
Bram Moolenaara5e66212017-09-29 22:42:33 +0200104 do
105 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100106 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200107 above_row = 0;
108 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000109
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100110 // Pretend the pum is already there to avoid that must_redraw is set
111 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200112 pum_array = (pumitem_T *)1;
113 validate_cursor_col();
114 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000115
Bram Moolenaar491ac282018-06-17 14:47:55 +0200116 // Remember the essential parts of the window position and size, so we
117 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200118 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200119 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
120 pum_win_height = curwin->w_height;
121 pum_win_col = curwin->w_wincol;
122 pum_win_wcol = curwin->w_wcol;
123 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000124
Bram Moolenaar4033c552017-09-16 20:54:51 +0200125#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200126 FOR_ALL_WINDOWS(pvwin)
127 if (pvwin->w_p_pvw)
128 break;
129 if (pvwin != NULL)
130 {
131 if (W_WINROW(pvwin) < W_WINROW(curwin))
132 above_row = W_WINROW(pvwin) + pvwin->w_height;
133 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
134 below_row = W_WINROW(pvwin);
135 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100136#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000137
Bram Moolenaara5e66212017-09-29 22:42:33 +0200138 /*
139 * Figure out the size and position of the pum.
140 */
141 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000142 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000143 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200144 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000145 if (p_ph > 0 && pum_height > p_ph)
146 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000147
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100148 // Put the pum below "pum_win_row" if possible. If there are few lines
149 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200150 if (pum_win_row + 2 >= below_row - pum_height
151 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200152 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100153 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200154
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100155 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200156 if (curwin->w_wrow - curwin->w_cline_row >= 2)
157 context_lines = 2;
158 else
159 context_lines = curwin->w_wrow - curwin->w_cline_row;
160
Bram Moolenaar491ac282018-06-17 14:47:55 +0200161 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200162 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200163 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200164 pum_height = size;
165 }
166 else
167 {
168 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200169 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200170 }
171 if (p_ph > 0 && pum_height > p_ph)
172 {
173 pum_row += pum_height - p_ph;
174 pum_height = p_ph;
175 }
176 }
177 else
178 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100179 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200180
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100181 // Leave two lines of context if possible
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100182 validate_cheight();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200183 if (curwin->w_cline_row
184 + curwin->w_cline_height - curwin->w_wrow >= 3)
185 context_lines = 3;
186 else
187 context_lines = curwin->w_cline_row
188 + curwin->w_cline_height - curwin->w_wrow;
189
Bram Moolenaar491ac282018-06-17 14:47:55 +0200190 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200191 if (size > below_row - pum_row)
192 pum_height = below_row - pum_row;
193 else
194 pum_height = size;
195 if (p_ph > 0 && pum_height > p_ph)
196 pum_height = p_ph;
197 }
198
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100199 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200200 if (pum_height < 1 || (pum_height == 1 && size > 1))
201 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000202
Bram Moolenaar4033c552017-09-16 20:54:51 +0200203#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100204 // If there is a preview window above avoid drawing over it.
205 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000206 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100207 pum_row = above_row;
208 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000209 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210#endif
211
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100212 pum_array = array;
213 pum_size = size;
214 pum_compute_size();
215 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000216
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100217 // Calculate column
Bram Moolenaarabc97732007-08-08 20:49:37 +0000218#ifdef FEAT_RIGHTLEFT
219 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100220 cursor_col = curwin->w_wincol + curwin->w_width
221 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000222 else
223#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100224 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000225
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100226 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200227 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000228 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200229 pum_scrollbar = 1;
230 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000231 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000232 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200233 pum_scrollbar = 0;
234
235 if (def_width < max_width)
236 def_width = max_width;
237
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100238 if (((cursor_col < Columns - p_pw
239 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000240#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200241 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100242 || (curwin->w_p_rl
243 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000244#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200245 ))
246 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100247 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100248 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000249
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100250 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200251#ifdef FEAT_RIGHTLEFT
252 if (curwin->w_p_rl)
253 pum_width = pum_col - pum_scrollbar + 1;
254 else
255#endif
256 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000257
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100258 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100259 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200260 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100261 // the width is more than needed for the items, make it
262 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100263 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100264 if (pum_width < p_pw)
265 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200266 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100267 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100268#ifdef FEAT_RIGHTLEFT
269 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100270 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
271 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100272#endif
273 ))
274 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100275 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100276#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100277 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100278 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100279 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100280 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100281 if (pum_col >= Columns)
282 pum_col = Columns - 1;
283 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100284 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100285#endif
286 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100287 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
288 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100289 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100290 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100291 pum_col = Columns - max_width - pum_scrollbar;
292 if (pum_col < 0)
293 pum_col = 0;
294 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295 }
296
297#ifdef FEAT_RIGHTLEFT
298 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100299 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300 else
301#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100302 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100303
Bram Moolenaar42443c72018-02-10 18:28:52 +0100304 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100305 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100306 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100307#ifdef FEAT_RIGHTLEFT
308 if (curwin->w_p_rl)
309 {
310 if (pum_width > pum_col)
311 pum_width = pum_col;
312 }
313 else
314#endif
315 {
316 if (pum_width >= Columns - pum_col)
317 pum_width = Columns - pum_col - 1;
318 }
319 }
320 else if (pum_width > max_width + pum_kind_width
321 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100322 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100323 {
324 pum_width = max_width + pum_kind_width
325 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100326 if (pum_width < p_pw)
327 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100328 }
329 }
330
Bram Moolenaara5e66212017-09-29 22:42:33 +0200331 }
332 else if (Columns < def_width)
333 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100334 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200335#ifdef FEAT_RIGHTLEFT
336 if (curwin->w_p_rl)
337 pum_col = Columns - 1;
338 else
339#endif
340 pum_col = 0;
341 pum_width = Columns - 1;
342 }
343 else
344 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100345 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100346 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200347#ifdef FEAT_RIGHTLEFT
348 if (curwin->w_p_rl)
349 pum_col = max_width - 1;
350 else
351#endif
352 pum_col = Columns - max_width;
353 pum_width = max_width - pum_scrollbar;
354 }
355
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100356 // Set selected item and redraw. If the window size changed need to
357 // redo the positioning. Limit this to two times, when there is not
358 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200359 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100360
361 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000362}
363
364/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100365 * Set a flag that when pum_redraw() is called it first calls update_screen().
366 * This will avoid clearing and redrawing the popup menu, prevent flicker.
367 */
368 void
369pum_call_update_screen()
370{
371 call_update_screen = TRUE;
372
373 // Update the cursor position to be able to compute the popup menu
374 // position. The cursor line length may have changed because of the
375 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100376 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100377 validate_cursor();
378}
379
380/*
381 * Return TRUE if we are going to redraw the popup menu and the screen position
382 * "row"/"col" is under the popup menu.
383 */
384 int
Bakudankun65555002021-11-17 20:40:16 +0000385pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100386{
Bakudankun65555002021-11-17 20:40:16 +0000387 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100388 && row >= pum_row
389 && row < pum_row + pum_height
390 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200391 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100392}
393
394/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000395 * Redraw the popup menu, using "pum_first" and "pum_selected".
396 */
397 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100398pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000399{
400 int row = pum_row;
401 int col;
402 int attr_norm = highlight_attr[HLF_PNI];
403 int attr_select = highlight_attr[HLF_PSI];
404 int attr_scroll = highlight_attr[HLF_PSB];
405 int attr_thumb = highlight_attr[HLF_PST];
406 int attr;
407 int i;
408 int idx;
409 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000410 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000411 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000412 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100413 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000414 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000415 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000416
Bram Moolenaarae654382019-01-17 21:09:05 +0100417 if (call_update_screen)
418 {
419 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100420 // Do not redraw in pum_may_redraw() and don't draw in the area where
421 // the popup menu will be.
422 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100423 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100424 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100425 }
426
427 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100428 if (pum_first > pum_size - pum_height)
429 pum_first = pum_size - pum_height;
430
Bram Moolenaar4b779472005-10-04 09:12:31 +0000431 if (pum_scrollbar)
432 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100433 thumb_height = pum_height * pum_height / pum_size;
434 if (thumb_height == 0)
435 thumb_height = 1;
436 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000437 + (pum_size - pum_height) / 2)
438 / (pum_size - pum_height);
439 }
440
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100441#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200442 // The popup menu is drawn over popup menus with zindex under
443 // POPUPMENU_ZINDEX.
444 screen_zindex = POPUPMENU_ZINDEX;
445#endif
446
Bram Moolenaar4b779472005-10-04 09:12:31 +0000447 for (i = 0; i < pum_height; ++i)
448 {
449 idx = i + pum_first;
450 attr = (idx == pum_selected) ? attr_select : attr_norm;
451
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100452 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000453#ifdef FEAT_RIGHTLEFT
454 if (curwin->w_p_rl)
455 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200456 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000457 screen_putchar(' ', row, pum_col + 1, attr);
458 }
459 else
460#endif
461 if (pum_col > 0)
462 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000463
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100464 // Display each entry, use two spaces for a Tab.
465 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000466 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000467 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000468 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000469 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000470 width = 0;
471 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000472 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000473 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000474 case 1: p = pum_array[idx].pum_text; break;
475 case 2: p = pum_array[idx].pum_kind; break;
476 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000477 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000478 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100479 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000480 {
481 if (s == NULL)
482 s = p;
483 w = ptr2cells(p);
484 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
485 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100486 // Display the text that fits or comes before a Tab.
487 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000488 char_u *st;
489 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000490
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100491 if (saved != NUL)
492 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000493 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100494 if (saved != NUL)
495 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000496#ifdef FEAT_RIGHTLEFT
497 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000498 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000499 if (st != NULL)
500 {
501 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000502
503 if (rt != NULL)
504 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100505 char_u *rt_start = rt;
506 int size;
507
508 size = vim_strsize(rt);
509 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000510 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100511 do
512 {
513 size -= has_mbyte
514 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100515 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100516 } while (size > pum_width);
517
518 if (size < pum_width)
519 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100520 // Most left character requires
521 // 2-cells but only 1 cell is
522 // available on screen. Put a
523 // '<' on the left of the pum
524 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100525 *(--rt) = '<';
526 size++;
527 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000528 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100529 screen_puts_len(rt, (int)STRLEN(rt),
530 row, col - size + 1, attr);
531 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000532 }
533 vim_free(st);
534 }
535 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000536 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000537 else
538#endif
539 {
540 if (st != NULL)
541 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100542 int size = (int)STRLEN(st);
543 int cells = (*mb_string2cells)(st, size);
544
545 // only draw the text that fits
546 while (size > 0
547 && col + cells > pum_width + pum_col)
548 {
549 --size;
550 if (has_mbyte)
551 {
552 size -= (*mb_head_off)(st, st + size);
553 cells -= (*mb_ptr2cells)(st + size);
554 }
555 else
556 --cells;
557 }
558 screen_puts_len(st, size, row, col, attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000559 vim_free(st);
560 }
561 col += width;
562 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000563
564 if (*p != TAB)
565 break;
566
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100567 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000568#ifdef FEAT_RIGHTLEFT
569 if (curwin->w_p_rl)
570 {
571 screen_puts_len((char_u *)" ", 2, row, col - 1,
572 attr);
573 col -= 2;
574 }
575 else
576#endif
577 {
578 screen_puts_len((char_u *)" ", 2, row, col, attr);
579 col += 2;
580 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000581 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100582 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000583 width = 0;
584 }
585 else
586 width += w;
587 }
588
589 if (round > 1)
590 n = pum_kind_width + 1;
591 else
592 n = 1;
593
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100594 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000595 if (round == 3
596 || (round == 2 && pum_array[idx].pum_extra == NULL)
597 || (round == 1 && pum_array[idx].pum_kind == NULL
598 && pum_array[idx].pum_extra == NULL)
599 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000600 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000601#ifdef FEAT_RIGHTLEFT
602 if (curwin->w_p_rl)
603 {
604 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
605 col + 1, ' ', ' ', attr);
606 col = pum_col - pum_base_width - n + 1;
607 }
608 else
609#endif
610 {
611 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000612 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000613 col = pum_col + pum_base_width + n;
614 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000615 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616 }
617
Bram Moolenaarabc97732007-08-08 20:49:37 +0000618#ifdef FEAT_RIGHTLEFT
619 if (curwin->w_p_rl)
620 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
621 ' ', attr);
622 else
623#endif
624 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
625 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000626 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000627 {
628#ifdef FEAT_RIGHTLEFT
629 if (curwin->w_p_rl)
630 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100631 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000632 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000633 else
634#endif
635 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100636 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000637 ? attr_thumb : attr_scroll);
638 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000639
640 ++row;
641 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200642
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100643#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200644 screen_zindex = 0;
645#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000646}
647
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100648#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200649/*
650 * Position the info popup relative to the popup menu item.
651 */
652 void
653pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200654{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100655 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200656 int row = pum_row;
657 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200658 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200659
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200660 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200661 if (Columns - col < 20 && Columns - col < pum_col)
662 {
663 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200664 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200665 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200666 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200667 }
668 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200669 wp->w_maxwidth = Columns - col + 1;
670 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200671 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
672 {
673 // option value overrules computed value
674 wp->w_maxwidth = wp->w_maxwidth_opt;
675 used_maxwidth_opt = TRUE;
676 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200677
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200678 row -= popup_top_extra(wp);
679 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200680 {
681 if (pum_row < pum_win_row)
682 {
683 // menu above cursor line, align with bottom
684 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200685 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200686 }
687 else
688 // menu below cursor line, align with top
689 row += 1;
690 }
691 else
692 // align with the selected item
693 row += pum_selected - pum_first + 1;
694
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100695 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200696 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100697 // The popup is not going to fit or will overlap with the cursor
698 // position, hide the popup.
699 wp->w_popup_flags |= POPF_HIDDEN;
700 else
701 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200702}
703#endif
704
Bram Moolenaar4b779472005-10-04 09:12:31 +0000705/*
706 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000707 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000708 * This may be repeated when the preview window is used:
709 * "repeat" == 0: open preview window normally
710 * "repeat" == 1: open preview window but don't set the size
711 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000712 * Returns TRUE when the window was resized and the location of the popup menu
713 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000714 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000715 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200716pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000717{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000718 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000719 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200720#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200721 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200722#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100723#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200724 int has_info = FALSE;
725#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000726
Bram Moolenaar4b779472005-10-04 09:12:31 +0000727 pum_selected = n;
728
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000729 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000730 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000731 if (pum_first > pum_selected - 4)
732 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 // scroll down; when we did a jump it's probably a PageUp then
734 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000735 if (pum_first > pum_selected - 2)
736 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000737 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000738 if (pum_first < 0)
739 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000740 else if (pum_first > pum_selected)
741 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000742 }
743 else
744 pum_first = pum_selected;
745 }
746 else if (pum_first < pum_selected - pum_height + 5)
747 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100748 // scroll up; when we did a jump it's probably a PageDown then
749 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000750 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000751 {
752 pum_first += pum_height - 2;
753 if (pum_first < pum_selected - pum_height + 1)
754 pum_first = pum_selected - pum_height + 1;
755 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000756 else
757 pum_first = pum_selected - pum_height + 1;
758 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000759
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100760 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000761 if (context > 3)
762 context = 3;
763 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000764 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000765 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000766 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100767 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000768 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000769 if (pum_first < 0)
770 pum_first = 0;
771 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000772 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000773 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100774 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000775 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000776 }
777 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200778 // adjust for the number of lines displayed
779 if (pum_first > pum_size - pum_height)
780 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000781
Bram Moolenaar4033c552017-09-16 20:54:51 +0200782#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000783 /*
784 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100785 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000786 * Skip this when tried twice already.
787 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000788 * NOTE: Be very careful not to sync undo!
789 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000790 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000791 && Rows > 10
792 && repeat <= 1
793 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000794 {
795 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200796 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000797 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100798# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200799 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200800# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100801# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200802# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100803# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200804 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200805 if (strstr((char *)p_cot, "popuphidden") != NULL)
806 use_popup = USEPOPUP_HIDDEN;
807 else if (strstr((char *)p_cot, "popup") != NULL)
808 use_popup = USEPOPUP_NORMAL;
809 else
810 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100811 if (use_popup != USEPOPUP_NONE)
812 // don't use WinEnter or WinLeave autocommands for the info
813 // popup
814 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200815# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200816 // Open a preview window and set "curwin" to it.
817 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000818 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200819 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
820 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200821 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200822 // Prevent undo sync here, if an autocommand syncs undo weird
823 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100824 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200825 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100826 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200827 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000828 g_do_tagpreview = 0;
829
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200830 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100831# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200832 || (curwin->w_popup_flags & POPF_INFO)
833# endif
834 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000835 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200836 if (!resized
837 && curbuf->b_nwindows == 1
838 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200839 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000840 && curbuf->b_p_bh[0] == 'w')
841 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200842 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100843 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200844 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000845 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000846 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000847 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200848 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000849 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000850 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000851 --no_u_sync;
852 if (res == OK)
853 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200854 // Edit a new, empty buffer. Set options for a "wipeout"
855 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000856 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
Bram Moolenaard356fc62020-12-09 18:13:44 +0100857 set_option_value((char_u *)"bl", 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000858 set_option_value((char_u *)"bt", 0L,
859 (char_u *)"nofile", OPT_LOCAL);
860 set_option_value((char_u *)"bh", 0L,
861 (char_u *)"wipe", OPT_LOCAL);
862 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000863 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000864 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000865 }
866 if (res == OK)
867 {
868 char_u *p, *e;
869 linenr_T lnum = 0;
870
871 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
872 {
873 e = vim_strchr(p, '\n');
874 if (e == NULL)
875 {
876 ml_append(lnum++, p, 0, FALSE);
877 break;
878 }
879 else
880 {
881 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000882 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000883 *e = '\n';
884 p = e + 1;
885 }
886 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200887 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +0200888 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000889
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100890 // Increase the height of the preview window to show the
891 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200892 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000893 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000894 if (lnum > p_pvh)
895 lnum = p_pvh;
896 if (curwin->w_height < lnum)
897 {
898 win_setheight((int)lnum);
899 resized = TRUE;
900 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000901 }
902
903 curbuf->b_changed = 0;
904 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200905 if (pum_selected != prev_selected)
906 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100907# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200908 curwin->w_firstline = 1;
909# endif
910 curwin->w_topline = 1;
911 }
912 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
913 curwin->w_topline = curbuf->b_ml.ml_line_count;
914 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000915 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100916# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200917 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200918 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200919 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200920 if (win_valid(curwin_save))
921 redraw_win_later(curwin_save, SOME_VALID);
922 }
923# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200924 if ((curwin != curwin_save && win_valid(curwin_save))
925 || (curtab != curtab_save
926 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000927 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200928 int save_redr_status;
929
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200930 if (curtab != curtab_save && valid_tabpage(curtab_save))
931 goto_tabpage_tp(curtab_save, FALSE, FALSE);
932
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100933 // When the first completion is done and the preview
934 // window is not resized, skip the preview window's
935 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200936 if (ins_compl_active() && !resized)
937 curwin->w_redr_status = FALSE;
938
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100939 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000940 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000941 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000942
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100943 // When the preview window was resized we need to
944 // update the view on the buffer. Only go back to
945 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100946 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200947 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000948 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100949 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000950 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100951 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000952 update_topline();
953 }
954
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100955 // Update the screen before drawing the popup menu.
956 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100957 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200958
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100959 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100960 // it causes flicker. When resizing we need to draw
961 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200962 // Also do not redraw the status line of the original
963 // current window here, to avoid it gets drawn with
964 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100965 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200966 save_redr_status = curwin_save->w_redr_status;
967 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000968 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100969 pum_pretend_not_visible = FALSE;
970 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200971 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000972
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000973 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100974 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100975# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200976 win_T *wp = curwin;
977# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100978 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000979 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100980 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100981# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200982 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
983 popup_hide(wp);
984# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100985 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000986
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100987 // May need to update the screen again when there are
988 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100989 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100990 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000991 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100992 pum_pretend_not_visible = FALSE;
993 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100994 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000995 }
996 }
997 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100998# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200999 if (WIN_IS_POPUP(curwin))
1000 // can't keep focus in a popup window
1001 win_enter(firstwin, TRUE);
1002# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001003# ifdef FEAT_PROP_POPUP
1004 if (use_popup != USEPOPUP_NONE)
1005 unblock_autocmds();
1006# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001007 }
1008#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001009 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001010#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001011 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001012 // hide any popup info window
1013 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001014#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001015
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001016 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001017}
1018
1019/*
1020 * Undisplay the popup menu (later).
1021 */
1022 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001023pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001024{
1025 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +02001026 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001027 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001028 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001029#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001030 // hide any popup info window
1031 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001032#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001033}
1034
1035/*
1036 * Clear the popup menu. Currently only resets the offset to the first
1037 * displayed item.
1038 */
1039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001040pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001041{
1042 pum_first = 0;
1043}
1044
1045/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001046 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1047 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1048 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001049 */
1050 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001051pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001052{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001053 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001054}
1055
Bram Moolenaare3226be2005-12-18 22:10:00 +00001056/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001057 * Return TRUE if the popup can be redrawn in the same position.
1058 */
1059 static int
1060pum_in_same_position(void)
1061{
1062 return pum_window != curwin
1063 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1064 && pum_win_height == curwin->w_height
1065 && pum_win_col == curwin->w_wincol
1066 && pum_win_width == curwin->w_width);
1067}
1068
1069/*
1070 * Return TRUE when pum_may_redraw() will call pum_redraw().
1071 * This means that the pum area should not be overwritten to avoid flicker.
1072 */
1073 int
1074pum_redraw_in_same_position(void)
1075{
1076 if (!pum_visible() || pum_will_redraw)
1077 return FALSE; // nothing to do
1078
1079 return pum_in_same_position();
1080}
1081
1082/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001083 * Reposition the popup menu to adjust for window layout changes.
1084 */
1085 void
1086pum_may_redraw(void)
1087{
1088 pumitem_T *array = pum_array;
1089 int len = pum_size;
1090 int selected = pum_selected;
1091
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001092 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001093 return; // nothing to do
1094
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001095 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001096 {
1097 // window position didn't change, redraw in the same position
1098 pum_redraw();
1099 }
1100 else
1101 {
1102 int wcol = curwin->w_wcol;
1103
1104 // Window layout changed, recompute the position.
1105 // Use the remembered w_wcol value, the cursor may have moved when a
1106 // completion was inserted, but we want the menu in the same position.
1107 pum_undisplay();
1108 curwin->w_wcol = pum_win_wcol;
1109 curwin->w_valid |= VALID_WCOL;
1110 pum_display(array, len, selected);
1111 curwin->w_wcol = wcol;
1112 }
1113}
1114
1115/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001116 * Return the height of the popup menu, the number of entries visible.
1117 * Only valid when pum_visible() returns TRUE!
1118 */
1119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001120pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001121{
1122 return pum_height;
1123}
1124
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001125#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001126/*
1127 * Add size information about the pum to "dict".
1128 */
1129 void
1130pum_set_event_info(dict_T *dict)
1131{
1132 if (!pum_visible())
1133 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001134 (void)dict_add_number(dict, "height", pum_height);
1135 (void)dict_add_number(dict, "width", pum_width);
1136 (void)dict_add_number(dict, "row", pum_row);
1137 (void)dict_add_number(dict, "col", pum_col);
1138 (void)dict_add_number(dict, "size", pum_size);
1139 (void)dict_add_bool(dict, "scrollbar",
1140 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001141}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001142#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001143
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001144#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001145 static void
1146pum_position_at_mouse(int min_width)
1147{
1148 if (Rows - mouse_row > pum_size)
1149 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001150 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001151 pum_row = mouse_row + 1;
1152 if (pum_height > Rows - pum_row)
1153 pum_height = Rows - pum_row;
1154 }
1155 else
1156 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001157 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001158 pum_row = mouse_row - pum_size;
1159 if (pum_row < 0)
1160 {
1161 pum_height += pum_row;
1162 pum_row = 0;
1163 }
1164 }
1165 if (Columns - mouse_col >= pum_base_width
1166 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001167 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001168 pum_col = mouse_col;
1169 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001170 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001171 pum_col = Columns - (pum_base_width > min_width
1172 ? min_width : pum_base_width);
1173
1174 pum_width = Columns - pum_col;
1175 if (pum_width > pum_base_width + 1)
1176 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001177
1178 // Do not redraw at cursor position.
1179 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001180}
1181
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001182#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001183
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001184#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001185static pumitem_T *balloon_array = NULL;
1186static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001187
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001188# define BALLOON_MIN_WIDTH 50
1189# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001190
Bram Moolenaar246fe032017-11-19 19:56:27 +01001191typedef struct {
1192 char_u *start;
1193 int bytelen;
1194 int cells;
1195 int indent;
1196} balpart_T;
1197
1198/*
1199 * Split a string into parts to display in the balloon.
1200 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1201 * strings and make a struct look good.
1202 * Resulting array is stored in "array" and returns the size of the array.
1203 */
1204 int
1205split_message(char_u *mesg, pumitem_T **array)
1206{
1207 garray_T ga;
1208 char_u *p;
1209 balpart_T *item;
1210 int quoted = FALSE;
1211 int height;
1212 int line;
1213 int item_idx;
1214 int indent = 0;
1215 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001216 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001217 int long_item_count = 0;
1218 int split_long_items = FALSE;
1219
1220 ga_init2(&ga, sizeof(balpart_T), 20);
1221 p = mesg;
1222
1223 while (*p != NUL)
1224 {
1225 if (ga_grow(&ga, 1) == FAIL)
1226 goto failed;
1227 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1228 item->start = p;
1229 item->indent = indent;
1230 item->cells = indent * 2;
1231 ++ga.ga_len;
1232 while (*p != NUL)
1233 {
1234 if (*p == '"')
1235 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001236 else if (*p == '\n')
1237 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001238 else if (*p == '\\' && p[1] != NUL)
1239 ++p;
1240 else if (!quoted)
1241 {
1242 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1243 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001244 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001245 if (*p == '{')
1246 ++indent;
1247 else if (*p == '}' && indent > 0)
1248 --indent;
1249 ++item->cells;
1250 p = skipwhite(p + 1);
1251 break;
1252 }
1253 }
1254 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001255 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001256 }
1257 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001258 if (*p == '\n')
1259 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001260 if (item->cells > max_cells)
1261 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001262 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001263 }
1264
1265 height = 2 + ga.ga_len;
1266
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001267 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001268 if (long_item_count > 0 && height + long_item_count <= max_height)
1269 {
1270 split_long_items = TRUE;
1271 height += long_item_count;
1272 }
1273
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001274 // Limit to half the window height, it has to fit above or below the mouse
1275 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001276 if (height > max_height)
1277 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001278 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001279 if (*array == NULL)
1280 goto failed;
1281
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001282 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001283 (*array)->pum_text = vim_strsave((char_u *)"");
1284 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1285
1286 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1287 {
1288 int skip;
1289 int thislen;
1290 int copylen;
1291 int ind;
1292 int cells;
1293
1294 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001295 if (item->bytelen == 0)
1296 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1297 else
1298 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001299 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001300 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1301 {
1302 cells = item->indent * 2;
1303 for (p = item->start + skip;
1304 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001305 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001306 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1307 break;
1308 thislen = p - (item->start + skip);
1309 }
1310 else
1311 thislen = item->bytelen;
1312
1313 // put indent at the start
1314 p = alloc(thislen + item->indent * 2 + 1);
1315 if (p == NULL)
1316 {
1317 for (line = 0; line <= height - 1; ++line)
1318 vim_free((*array)[line].pum_text);
1319 vim_free(*array);
1320 goto failed;
1321 }
1322 for (ind = 0; ind < item->indent * 2; ++ind)
1323 p[ind] = ' ';
1324
1325 // exclude spaces at the end of the string
1326 for (copylen = thislen; copylen > 0; --copylen)
1327 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001328 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001329
1330 vim_strncpy(p + ind, item->start + skip, copylen);
1331 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001332 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001333 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001334 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001335 }
1336 ga_clear(&ga);
1337 return height;
1338
1339failed:
1340 ga_clear(&ga);
1341 return 0;
1342}
1343
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001344 void
1345ui_remove_balloon(void)
1346{
1347 if (balloon_array != NULL)
1348 {
1349 pum_undisplay();
1350 while (balloon_arraysize > 0)
1351 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001352 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001353 }
1354}
1355
1356/*
1357 * Terminal version of a balloon, uses the popup menu code.
1358 */
1359 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001360ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001361{
1362 ui_remove_balloon();
1363
Bram Moolenaar246fe032017-11-19 19:56:27 +01001364 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001365 {
1366 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001367 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001368 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001369 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001370 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001371 listitem_T *li;
1372 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001373
Bram Moolenaar246fe032017-11-19 19:56:27 +01001374 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001375 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001376 if (balloon_array == NULL)
1377 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001378 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001379 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001382
1383 balloon_array[idx].pum_text = vim_strsave(
1384 text == NULL ? (char_u *)"" : text);
1385 }
1386 }
1387 else
1388 balloon_arraysize = split_message(mesg, &balloon_array);
1389
1390 if (balloon_arraysize > 0)
1391 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001392 pum_array = balloon_array;
1393 pum_size = balloon_arraysize;
1394 pum_compute_size();
1395 pum_scrollbar = 0;
1396 pum_height = balloon_arraysize;
1397
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001398 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001399 pum_selected = -1;
1400 pum_first = 0;
1401 pum_redraw();
1402 }
1403}
1404
1405/*
1406 * Called when the mouse moved, may remove any displayed balloon.
1407 */
1408 void
1409ui_may_remove_balloon(void)
1410{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001411 // For now: remove the balloon whenever the mouse moves to another screen
1412 // cell.
1413 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001414}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001415#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001416
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001417#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001418/*
1419 * Select the pum entry at the mouse position.
1420 */
1421 static void
1422pum_select_mouse_pos(void)
1423{
1424 int idx = mouse_row - pum_row;
1425
1426 if (idx < 0 || idx >= pum_size)
1427 pum_selected = -1;
1428 else if (*pum_array[idx].pum_text != NUL)
1429 pum_selected = idx;
1430}
1431
1432/*
1433 * Execute the currently selected popup menu item.
1434 */
1435 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001436pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001437{
1438 vimmenu_T *mp;
1439 int idx = 0;
1440 exarg_T ea;
1441
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001442 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001443 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001444 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001445 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001446 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001447 break;
1448 }
1449}
1450
1451/*
1452 * Open the terminal version of the popup menu and don't return until it is
1453 * closed.
1454 */
1455 void
1456pum_show_popupmenu(vimmenu_T *menu)
1457{
1458 vimmenu_T *mp;
1459 int idx = 0;
1460 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001461# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001462 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001463# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001464 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001465
1466 pum_undisplay();
1467 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001468 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001469
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001470 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001471 if (menu_is_separator(mp->dname)
1472 || (mp->modes & mp->enabled & mode))
1473 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001474
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001475 // When there are only Terminal mode menus, using "popup Edit" results in
1476 // pum_size being zero.
1477 if (pum_size <= 0)
1478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001479 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001480 return;
1481 }
1482
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001483 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001484 if (array == NULL)
1485 return;
1486
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001487 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001488 {
1489 char_u *s = NULL;
1490
1491 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001492 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001493 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001494 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001495 s = mp->dname;
1496 if (s != NULL)
1497 {
1498 s = vim_strsave(s);
1499 if (s != NULL)
1500 array[idx++].pum_text = s;
1501 }
1502 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001503
1504 pum_array = array;
1505 pum_compute_size();
1506 pum_scrollbar = 0;
1507 pum_height = pum_size;
1508 pum_position_at_mouse(20);
1509
1510 pum_selected = -1;
1511 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001512# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001513 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001514 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001515# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001516
1517 for (;;)
1518 {
1519 int c;
1520
1521 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001522 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001523 out_flush();
1524
1525 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001526
1527 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1528 // menu.
1529 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001530 break;
1531 else if (c == CAR || c == NL)
1532 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001533 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001534 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001535 break;
1536 }
1537 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1538 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001539 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001540 while (pum_selected > 0)
1541 {
1542 --pum_selected;
1543 if (*array[pum_selected].pum_text != NUL)
1544 break;
1545 }
1546 }
1547 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1548 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001549 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001550 while (pum_selected < pum_size - 1)
1551 {
1552 ++pum_selected;
1553 if (*array[pum_selected].pum_text != NUL)
1554 break;
1555 }
1556 }
1557 else if (c == K_RIGHTMOUSE)
1558 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001559 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001560 vungetc(c);
1561 break;
1562 }
1563 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1564 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001565 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001566 pum_select_mouse_pos();
1567 }
1568 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1569 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001570 // left mouse click: select clicked item, if any, and close;
1571 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001572 pum_select_mouse_pos();
1573 if (pum_selected >= 0)
1574 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001575 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001576 break;
1577 }
1578 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1579 break;
1580 }
1581 }
1582
Bram Moolenaar38455a92020-12-24 18:39:02 +01001583 for (idx = 0; idx < pum_size; ++idx)
1584 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001585 vim_free(array);
1586 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001587# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001588 p_bevalterm = save_bevalterm;
1589 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001590# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001591}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001592
1593 void
1594pum_make_popup(char_u *path_name, int use_mouse_pos)
1595{
1596 vimmenu_T *menu;
1597
1598 if (!use_mouse_pos)
1599 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001600 // Hack: set mouse position at the cursor so that the menu pops up
1601 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001602 mouse_row = curwin->w_winrow + curwin->w_wrow;
1603 mouse_col = curwin->w_wincol + curwin->w_wcol;
1604 }
1605
1606 menu = gui_find_menu(path_name);
1607 if (menu != NULL)
1608 pum_show_popupmenu(menu);
1609}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001610#endif