blob: ceb72b4f27884ca55a8898f6b624faa8cfd3317a [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 Moolenaarae654382019-01-17 21:09:05 +010039static int pum_do_redraw = FALSE; // do redraw anyway
40static int pum_skip_redraw = FALSE; // skip redraw
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000043
Bram Moolenaar4b779472005-10-04 09:12:31 +000044#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000045
Bram Moolenaar51b0f372017-11-18 18:52:04 +010046 static void
47pum_compute_size(void)
48{
49 int i;
50 int w;
51
Bram Moolenaar63d9e732019-12-05 21:10:38 +010052 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010053 pum_base_width = 0;
54 pum_kind_width = 0;
55 pum_extra_width = 0;
56 for (i = 0; i < pum_size; ++i)
57 {
58 w = vim_strsize(pum_array[i].pum_text);
59 if (pum_base_width < w)
60 pum_base_width = w;
61 if (pum_array[i].pum_kind != NULL)
62 {
63 w = vim_strsize(pum_array[i].pum_kind) + 1;
64 if (pum_kind_width < w)
65 pum_kind_width = w;
66 }
67 if (pum_array[i].pum_extra != NULL)
68 {
69 w = vim_strsize(pum_array[i].pum_extra) + 1;
70 if (pum_extra_width < w)
71 pum_extra_width = w;
72 }
73 }
74}
75
Bram Moolenaar4b779472005-10-04 09:12:31 +000076/*
77 * Show the popup menu with items "array[size]".
78 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010079 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000080 * The menu appears above the screen line "row" or at "row" + "height" - 1.
81 */
82 void
Bram Moolenaar05540972016-01-30 20:31:25 +010083pum_display(
84 pumitem_T *array,
85 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010086 int selected) // index of initially selected item, none if
87 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000088{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000089 int def_width;
90 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000091 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010092 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010093 int above_row;
94 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000095 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +020096#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +010097 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +010098#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000099
Bram Moolenaara5e66212017-09-29 22:42:33 +0200100 do
101 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100102 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200103 above_row = 0;
104 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000105
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100106 // Pretend the pum is already there to avoid that must_redraw is set
107 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 pum_array = (pumitem_T *)1;
109 validate_cursor_col();
110 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000111
Bram Moolenaar491ac282018-06-17 14:47:55 +0200112 // Remember the essential parts of the window position and size, so we
113 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200114 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200115 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
116 pum_win_height = curwin->w_height;
117 pum_win_col = curwin->w_wincol;
118 pum_win_wcol = curwin->w_wcol;
119 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000120
Bram Moolenaar4033c552017-09-16 20:54:51 +0200121#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200122 FOR_ALL_WINDOWS(pvwin)
123 if (pvwin->w_p_pvw)
124 break;
125 if (pvwin != NULL)
126 {
127 if (W_WINROW(pvwin) < W_WINROW(curwin))
128 above_row = W_WINROW(pvwin) + pvwin->w_height;
129 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
130 below_row = W_WINROW(pvwin);
131 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100132#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000133
Bram Moolenaara5e66212017-09-29 22:42:33 +0200134 /*
135 * Figure out the size and position of the pum.
136 */
137 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000138 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000139 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200140 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000141 if (p_ph > 0 && pum_height > p_ph)
142 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000143
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100144 // Put the pum below "pum_win_row" if possible. If there are few lines
145 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200146 if (pum_win_row + 2 >= below_row - pum_height
147 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200148 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100149 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200150
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100151 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200152 if (curwin->w_wrow - curwin->w_cline_row >= 2)
153 context_lines = 2;
154 else
155 context_lines = curwin->w_wrow - curwin->w_cline_row;
156
Bram Moolenaar491ac282018-06-17 14:47:55 +0200157 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200158 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200159 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200160 pum_height = size;
161 }
162 else
163 {
164 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200165 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166 }
167 if (p_ph > 0 && pum_height > p_ph)
168 {
169 pum_row += pum_height - p_ph;
170 pum_height = p_ph;
171 }
172 }
173 else
174 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100175 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200176
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100177 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200178 if (curwin->w_cline_row
179 + curwin->w_cline_height - curwin->w_wrow >= 3)
180 context_lines = 3;
181 else
182 context_lines = curwin->w_cline_row
183 + curwin->w_cline_height - curwin->w_wrow;
184
Bram Moolenaar491ac282018-06-17 14:47:55 +0200185 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200186 if (size > below_row - pum_row)
187 pum_height = below_row - pum_row;
188 else
189 pum_height = size;
190 if (p_ph > 0 && pum_height > p_ph)
191 pum_height = p_ph;
192 }
193
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100194 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195 if (pum_height < 1 || (pum_height == 1 && size > 1))
196 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000197
Bram Moolenaar4033c552017-09-16 20:54:51 +0200198#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100199 // If there is a preview window above avoid drawing over it.
200 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000201 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100202 pum_row = above_row;
203 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000204 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200205#endif
206
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100207 pum_array = array;
208 pum_size = size;
209 pum_compute_size();
210 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000211
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100212 // Calculate column
Bram Moolenaarabc97732007-08-08 20:49:37 +0000213#ifdef FEAT_RIGHTLEFT
214 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100215 cursor_col = curwin->w_wincol + curwin->w_width
216 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000217 else
218#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100219 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000220
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100221 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200222 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000223 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200224 pum_scrollbar = 1;
225 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000226 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000227 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200228 pum_scrollbar = 0;
229
230 if (def_width < max_width)
231 def_width = max_width;
232
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100233 if (((cursor_col < Columns - p_pw
234 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200236 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100237 || (curwin->w_p_rl
238 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000239#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200240 ))
241 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100242 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100243 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000244
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100245 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200246#ifdef FEAT_RIGHTLEFT
247 if (curwin->w_p_rl)
248 pum_width = pum_col - pum_scrollbar + 1;
249 else
250#endif
251 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000252
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100253 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100254 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200255 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100256 // the width is more than needed for the items, make it
257 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100258 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100259 if (pum_width < p_pw)
260 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200261 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100262 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100263#ifdef FEAT_RIGHTLEFT
264 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100265 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
266 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100267#endif
268 ))
269 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100270 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100271#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100272 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100273 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100274 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100275 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100276 if (pum_col >= Columns)
277 pum_col = Columns - 1;
278 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100279 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100280#endif
281 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100282 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
283 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100284 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100285 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100286 pum_col = Columns - max_width - pum_scrollbar;
287 if (pum_col < 0)
288 pum_col = 0;
289 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100290 }
291
292#ifdef FEAT_RIGHTLEFT
293 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100294 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295 else
296#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100297 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100298
Bram Moolenaar42443c72018-02-10 18:28:52 +0100299 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100301 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100302#ifdef FEAT_RIGHTLEFT
303 if (curwin->w_p_rl)
304 {
305 if (pum_width > pum_col)
306 pum_width = pum_col;
307 }
308 else
309#endif
310 {
311 if (pum_width >= Columns - pum_col)
312 pum_width = Columns - pum_col - 1;
313 }
314 }
315 else if (pum_width > max_width + pum_kind_width
316 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100317 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100318 {
319 pum_width = max_width + pum_kind_width
320 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100321 if (pum_width < p_pw)
322 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100323 }
324 }
325
Bram Moolenaara5e66212017-09-29 22:42:33 +0200326 }
327 else if (Columns < def_width)
328 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100329 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200330#ifdef FEAT_RIGHTLEFT
331 if (curwin->w_p_rl)
332 pum_col = Columns - 1;
333 else
334#endif
335 pum_col = 0;
336 pum_width = Columns - 1;
337 }
338 else
339 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100340 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100341 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200342#ifdef FEAT_RIGHTLEFT
343 if (curwin->w_p_rl)
344 pum_col = max_width - 1;
345 else
346#endif
347 pum_col = Columns - max_width;
348 pum_width = max_width - pum_scrollbar;
349 }
350
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100351 // Set selected item and redraw. If the window size changed need to
352 // redo the positioning. Limit this to two times, when there is not
353 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200354 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000355}
356
357/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100358 * Set a flag that when pum_redraw() is called it first calls update_screen().
359 * This will avoid clearing and redrawing the popup menu, prevent flicker.
360 */
361 void
362pum_call_update_screen()
363{
364 call_update_screen = TRUE;
365
366 // Update the cursor position to be able to compute the popup menu
367 // position. The cursor line length may have changed because of the
368 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100369 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100370 validate_cursor();
371}
372
373/*
374 * Return TRUE if we are going to redraw the popup menu and the screen position
375 * "row"/"col" is under the popup menu.
376 */
377 int
378pum_under_menu(int row, int col)
379{
380 return pum_skip_redraw
381 && row >= pum_row
382 && row < pum_row + pum_height
383 && col >= pum_col - 1
384 && col < pum_col + pum_width;
385}
386
387/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000388 * Redraw the popup menu, using "pum_first" and "pum_selected".
389 */
390 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100391pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000392{
393 int row = pum_row;
394 int col;
395 int attr_norm = highlight_attr[HLF_PNI];
396 int attr_select = highlight_attr[HLF_PSI];
397 int attr_scroll = highlight_attr[HLF_PSB];
398 int attr_thumb = highlight_attr[HLF_PST];
399 int attr;
400 int i;
401 int idx;
402 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000403 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000404 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000405 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100406 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000407 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000408 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000409
Bram Moolenaarae654382019-01-17 21:09:05 +0100410 if (call_update_screen)
411 {
412 call_update_screen = FALSE;
413 pum_skip_redraw = TRUE; // do not redraw in pum_may_redraw().
414 update_screen(0);
415 pum_skip_redraw = FALSE;
416 }
417
418 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100419 if (pum_first > pum_size - pum_height)
420 pum_first = pum_size - pum_height;
421
Bram Moolenaar4b779472005-10-04 09:12:31 +0000422 if (pum_scrollbar)
423 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100424 thumb_height = pum_height * pum_height / pum_size;
425 if (thumb_height == 0)
426 thumb_height = 1;
427 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000428 + (pum_size - pum_height) / 2)
429 / (pum_size - pum_height);
430 }
431
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100432#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200433 // The popup menu is drawn over popup menus with zindex under
434 // POPUPMENU_ZINDEX.
435 screen_zindex = POPUPMENU_ZINDEX;
436#endif
437
Bram Moolenaar4b779472005-10-04 09:12:31 +0000438 for (i = 0; i < pum_height; ++i)
439 {
440 idx = i + pum_first;
441 attr = (idx == pum_selected) ? attr_select : attr_norm;
442
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100443 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000444#ifdef FEAT_RIGHTLEFT
445 if (curwin->w_p_rl)
446 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200447 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000448 screen_putchar(' ', row, pum_col + 1, attr);
449 }
450 else
451#endif
452 if (pum_col > 0)
453 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000454
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100455 // Display each entry, use two spaces for a Tab.
456 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000457 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000458 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000459 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000460 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000461 width = 0;
462 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000463 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000464 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000465 case 1: p = pum_array[idx].pum_text; break;
466 case 2: p = pum_array[idx].pum_kind; break;
467 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000468 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000469 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100470 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000471 {
472 if (s == NULL)
473 s = p;
474 w = ptr2cells(p);
475 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
476 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 // Display the text that fits or comes before a Tab.
478 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000479 char_u *st;
480 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000481
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100482 if (saved != NUL)
483 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000484 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100485 if (saved != NUL)
486 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000487#ifdef FEAT_RIGHTLEFT
488 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000489 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000490 if (st != NULL)
491 {
492 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000493
494 if (rt != NULL)
495 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100496 char_u *rt_start = rt;
497 int size;
498
499 size = vim_strsize(rt);
500 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000501 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100502 do
503 {
504 size -= has_mbyte
505 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100506 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100507 } while (size > pum_width);
508
509 if (size < pum_width)
510 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100511 // Most left character requires
512 // 2-cells but only 1 cell is
513 // available on screen. Put a
514 // '<' on the left of the pum
515 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100516 *(--rt) = '<';
517 size++;
518 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000519 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100520 screen_puts_len(rt, (int)STRLEN(rt),
521 row, col - size + 1, attr);
522 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000523 }
524 vim_free(st);
525 }
526 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000527 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000528 else
529#endif
530 {
531 if (st != NULL)
532 {
533 screen_puts_len(st, (int)STRLEN(st), row, col,
534 attr);
535 vim_free(st);
536 }
537 col += width;
538 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000539
540 if (*p != TAB)
541 break;
542
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100543 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000544#ifdef FEAT_RIGHTLEFT
545 if (curwin->w_p_rl)
546 {
547 screen_puts_len((char_u *)" ", 2, row, col - 1,
548 attr);
549 col -= 2;
550 }
551 else
552#endif
553 {
554 screen_puts_len((char_u *)" ", 2, row, col, attr);
555 col += 2;
556 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000557 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100558 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000559 width = 0;
560 }
561 else
562 width += w;
563 }
564
565 if (round > 1)
566 n = pum_kind_width + 1;
567 else
568 n = 1;
569
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100570 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000571 if (round == 3
572 || (round == 2 && pum_array[idx].pum_extra == NULL)
573 || (round == 1 && pum_array[idx].pum_kind == NULL
574 && pum_array[idx].pum_extra == NULL)
575 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000576 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000577#ifdef FEAT_RIGHTLEFT
578 if (curwin->w_p_rl)
579 {
580 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
581 col + 1, ' ', ' ', attr);
582 col = pum_col - pum_base_width - n + 1;
583 }
584 else
585#endif
586 {
587 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000588 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000589 col = pum_col + pum_base_width + n;
590 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000591 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000592 }
593
Bram Moolenaarabc97732007-08-08 20:49:37 +0000594#ifdef FEAT_RIGHTLEFT
595 if (curwin->w_p_rl)
596 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
597 ' ', attr);
598 else
599#endif
600 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
601 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000602 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000603 {
604#ifdef FEAT_RIGHTLEFT
605 if (curwin->w_p_rl)
606 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100607 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000608 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000609 else
610#endif
611 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100612 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000613 ? attr_thumb : attr_scroll);
614 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000615
616 ++row;
617 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200618
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100619#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200620 screen_zindex = 0;
621#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000622}
623
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100624#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200625/*
626 * Position the info popup relative to the popup menu item.
627 */
628 void
629pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200630{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100631 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200632 int row = pum_row;
633 int botpos = POPPOS_BOTLEFT;
634
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200635 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200636 if (Columns - col < 20 && Columns - col < pum_col)
637 {
638 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200639 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200640 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200641 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200642 }
643 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200644 wp->w_maxwidth = Columns - col + 1;
645 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200646
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200647 row -= popup_top_extra(wp);
648 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200649 {
650 if (pum_row < pum_win_row)
651 {
652 // menu above cursor line, align with bottom
653 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200654 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200655 }
656 else
657 // menu below cursor line, align with top
658 row += 1;
659 }
660 else
661 // align with the selected item
662 row += pum_selected - pum_first + 1;
663
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100664 wp->w_popup_flags &= ~POPF_HIDDEN;
665 if (wp->w_maxwidth < 10)
666 // The popup is not going to fit or will overlap with the cursor
667 // position, hide the popup.
668 wp->w_popup_flags |= POPF_HIDDEN;
669 else
670 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200671}
672#endif
673
Bram Moolenaar4b779472005-10-04 09:12:31 +0000674/*
675 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000676 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000677 * This may be repeated when the preview window is used:
678 * "repeat" == 0: open preview window normally
679 * "repeat" == 1: open preview window but don't set the size
680 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000681 * Returns TRUE when the window was resized and the location of the popup menu
682 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000683 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000684 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200685pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000686{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000687 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000688 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200689#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200690 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200691#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100692#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200693 int has_info = FALSE;
694#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000695
Bram Moolenaar4b779472005-10-04 09:12:31 +0000696 pum_selected = n;
697
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000698 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000699 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000700 if (pum_first > pum_selected - 4)
701 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100702 // scroll down; when we did a jump it's probably a PageUp then
703 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000704 if (pum_first > pum_selected - 2)
705 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000706 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000707 if (pum_first < 0)
708 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000709 else if (pum_first > pum_selected)
710 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000711 }
712 else
713 pum_first = pum_selected;
714 }
715 else if (pum_first < pum_selected - pum_height + 5)
716 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100717 // scroll up; when we did a jump it's probably a PageDown then
718 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000719 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000720 {
721 pum_first += pum_height - 2;
722 if (pum_first < pum_selected - pum_height + 1)
723 pum_first = pum_selected - pum_height + 1;
724 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000725 else
726 pum_first = pum_selected - pum_height + 1;
727 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000728
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100729 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000730 if (context > 3)
731 context = 3;
732 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000733 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000734 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000735 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100736 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000737 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000738 if (pum_first < 0)
739 pum_first = 0;
740 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000741 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100743 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000744 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000745 }
746 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200747 // adjust for the number of lines displayed
748 if (pum_first > pum_size - pum_height)
749 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000750
Bram Moolenaar4033c552017-09-16 20:54:51 +0200751#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000752 /*
753 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100754 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000755 * Skip this when tried twice already.
756 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000757 * NOTE: Be very careful not to sync undo!
758 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000759 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000760 && Rows > 10
761 && repeat <= 1
762 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000763 {
764 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200765 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000766 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100767# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200768 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200769# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100770# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200771# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100772# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200773 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200774 if (strstr((char *)p_cot, "popuphidden") != NULL)
775 use_popup = USEPOPUP_HIDDEN;
776 else if (strstr((char *)p_cot, "popup") != NULL)
777 use_popup = USEPOPUP_NORMAL;
778 else
779 use_popup = USEPOPUP_NONE;
Bram Moolenaard570ab92019-09-03 23:20:05 +0200780# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200781 // Open a preview window and set "curwin" to it.
782 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000783 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200784 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
785 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200786 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200787 // Prevent undo sync here, if an autocommand syncs undo weird
788 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100789 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200790 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100791 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200792 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000793 g_do_tagpreview = 0;
794
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200795 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100796# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200797 || (curwin->w_popup_flags & POPF_INFO)
798# endif
799 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000800 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200801 if (!resized
802 && curbuf->b_nwindows == 1
803 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200804 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000805 && curbuf->b_p_bh[0] == 'w')
806 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200807 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100808 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000809 ml_delete((linenr_T)1, FALSE);
810 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000811 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000812 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200813 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000814 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000815 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000816 --no_u_sync;
817 if (res == OK)
818 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200819 // Edit a new, empty buffer. Set options for a "wipeout"
820 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000821 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
822 set_option_value((char_u *)"bt", 0L,
823 (char_u *)"nofile", OPT_LOCAL);
824 set_option_value((char_u *)"bh", 0L,
825 (char_u *)"wipe", OPT_LOCAL);
826 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000827 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000828 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000829 }
830 if (res == OK)
831 {
832 char_u *p, *e;
833 linenr_T lnum = 0;
834
835 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
836 {
837 e = vim_strchr(p, '\n');
838 if (e == NULL)
839 {
840 ml_append(lnum++, p, 0, FALSE);
841 break;
842 }
843 else
844 {
845 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000846 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000847 *e = '\n';
848 p = e + 1;
849 }
850 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200851 // delete the empty last line
852 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000853
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100854 // Increase the height of the preview window to show the
855 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200856 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000857 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000858 if (lnum > p_pvh)
859 lnum = p_pvh;
860 if (curwin->w_height < lnum)
861 {
862 win_setheight((int)lnum);
863 resized = TRUE;
864 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000865 }
866
867 curbuf->b_changed = 0;
868 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200869 if (pum_selected != prev_selected)
870 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100871# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200872 curwin->w_firstline = 1;
873# endif
874 curwin->w_topline = 1;
875 }
876 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
877 curwin->w_topline = curbuf->b_ml.ml_line_count;
878 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000879 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100880# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200881 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200882 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200883 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200884 if (win_valid(curwin_save))
885 redraw_win_later(curwin_save, SOME_VALID);
886 }
887# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200888 if ((curwin != curwin_save && win_valid(curwin_save))
889 || (curtab != curtab_save
890 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000891 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200892 if (curtab != curtab_save && valid_tabpage(curtab_save))
893 goto_tabpage_tp(curtab_save, FALSE, FALSE);
894
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100895 // When the first completion is done and the preview
896 // window is not resized, skip the preview window's
897 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200898 if (ins_compl_active() && !resized)
899 curwin->w_redr_status = FALSE;
900
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100901 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000902 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000903 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000904
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100905 // When the preview window was resized we need to
906 // update the view on the buffer. Only go back to
907 // the window when needed, otherwise it will always be
908 // redraw.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200909 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000910 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100911 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000912 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100913 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000914 update_topline();
915 }
916
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100917 // Update the screen before drawing the popup menu.
918 // Enable updating the status lines.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000919 pum_do_redraw = TRUE;
920 update_screen(0);
921 pum_do_redraw = FALSE;
922
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000923 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100924 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100925# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200926 win_T *wp = curwin;
927# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100928 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000929 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100930 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100931# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200932 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
933 popup_hide(wp);
934# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100935 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000936
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100937 // May need to update the screen again when there are
938 // autocommands involved.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000939 pum_do_redraw = TRUE;
940 update_screen(0);
941 pum_do_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100942 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000943 }
944 }
945 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100946# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200947 if (WIN_IS_POPUP(curwin))
948 // can't keep focus in a popup window
949 win_enter(firstwin, TRUE);
950# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000951 }
952#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000953 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100954#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200955 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200956 // hide any popup info window
957 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200958#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000959
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000960 if (!resized)
961 pum_redraw();
962
963 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000964}
965
966/*
967 * Undisplay the popup menu (later).
968 */
969 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100970pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000971{
972 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +0200973 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000974 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000975 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100976#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200977 // hide any popup info window
978 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200979#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000980}
981
982/*
983 * Clear the popup menu. Currently only resets the offset to the first
984 * displayed item.
985 */
986 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100987pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000988{
989 pum_first = 0;
990}
991
992/*
993 * Return TRUE if the popup menu is displayed.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000994 * Overruled when "pum_do_redraw" is set, used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000995 */
996 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100997pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000998{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000999 return !pum_do_redraw && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001000}
1001
Bram Moolenaare3226be2005-12-18 22:10:00 +00001002/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001003 * Reposition the popup menu to adjust for window layout changes.
1004 */
1005 void
1006pum_may_redraw(void)
1007{
1008 pumitem_T *array = pum_array;
1009 int len = pum_size;
1010 int selected = pum_selected;
1011
Bram Moolenaarae654382019-01-17 21:09:05 +01001012 if (!pum_visible() || pum_skip_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001013 return; // nothing to do
1014
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001015 if (pum_window != curwin
1016 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1017 && pum_win_height == curwin->w_height
1018 && pum_win_col == curwin->w_wincol
1019 && pum_win_width == curwin->w_width))
Bram Moolenaar491ac282018-06-17 14:47:55 +02001020 {
1021 // window position didn't change, redraw in the same position
1022 pum_redraw();
1023 }
1024 else
1025 {
1026 int wcol = curwin->w_wcol;
1027
1028 // Window layout changed, recompute the position.
1029 // Use the remembered w_wcol value, the cursor may have moved when a
1030 // completion was inserted, but we want the menu in the same position.
1031 pum_undisplay();
1032 curwin->w_wcol = pum_win_wcol;
1033 curwin->w_valid |= VALID_WCOL;
1034 pum_display(array, len, selected);
1035 curwin->w_wcol = wcol;
1036 }
1037}
1038
1039/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001040 * Return the height of the popup menu, the number of entries visible.
1041 * Only valid when pum_visible() returns TRUE!
1042 */
1043 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001044pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001045{
1046 return pum_height;
1047}
1048
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001049#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001050/*
1051 * Add size information about the pum to "dict".
1052 */
1053 void
1054pum_set_event_info(dict_T *dict)
1055{
1056 if (!pum_visible())
1057 return;
1058 dict_add_number(dict, "height", pum_height);
1059 dict_add_number(dict, "width", pum_width);
1060 dict_add_number(dict, "row", pum_row);
1061 dict_add_number(dict, "col", pum_col);
1062 dict_add_number(dict, "size", pum_size);
1063 dict_add_special(dict, "scrollbar", pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
1064}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001065#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001066
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001067#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001068 static void
1069pum_position_at_mouse(int min_width)
1070{
1071 if (Rows - mouse_row > pum_size)
1072 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001073 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001074 pum_row = mouse_row + 1;
1075 if (pum_height > Rows - pum_row)
1076 pum_height = Rows - pum_row;
1077 }
1078 else
1079 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001080 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001081 pum_row = mouse_row - pum_size;
1082 if (pum_row < 0)
1083 {
1084 pum_height += pum_row;
1085 pum_row = 0;
1086 }
1087 }
1088 if (Columns - mouse_col >= pum_base_width
1089 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001090 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001091 pum_col = mouse_col;
1092 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001093 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001094 pum_col = Columns - (pum_base_width > min_width
1095 ? min_width : pum_base_width);
1096
1097 pum_width = Columns - pum_col;
1098 if (pum_width > pum_base_width + 1)
1099 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001100
1101 // Do not redraw at cursor position.
1102 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001103}
1104
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001105#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001106
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001107#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001108static pumitem_T *balloon_array = NULL;
1109static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001110
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001111# define BALLOON_MIN_WIDTH 50
1112# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001113
Bram Moolenaar246fe032017-11-19 19:56:27 +01001114typedef struct {
1115 char_u *start;
1116 int bytelen;
1117 int cells;
1118 int indent;
1119} balpart_T;
1120
1121/*
1122 * Split a string into parts to display in the balloon.
1123 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1124 * strings and make a struct look good.
1125 * Resulting array is stored in "array" and returns the size of the array.
1126 */
1127 int
1128split_message(char_u *mesg, pumitem_T **array)
1129{
1130 garray_T ga;
1131 char_u *p;
1132 balpart_T *item;
1133 int quoted = FALSE;
1134 int height;
1135 int line;
1136 int item_idx;
1137 int indent = 0;
1138 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001139 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001140 int long_item_count = 0;
1141 int split_long_items = FALSE;
1142
1143 ga_init2(&ga, sizeof(balpart_T), 20);
1144 p = mesg;
1145
1146 while (*p != NUL)
1147 {
1148 if (ga_grow(&ga, 1) == FAIL)
1149 goto failed;
1150 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1151 item->start = p;
1152 item->indent = indent;
1153 item->cells = indent * 2;
1154 ++ga.ga_len;
1155 while (*p != NUL)
1156 {
1157 if (*p == '"')
1158 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001159 else if (*p == '\n')
1160 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001161 else if (*p == '\\' && p[1] != NUL)
1162 ++p;
1163 else if (!quoted)
1164 {
1165 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1166 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001167 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001168 if (*p == '{')
1169 ++indent;
1170 else if (*p == '}' && indent > 0)
1171 --indent;
1172 ++item->cells;
1173 p = skipwhite(p + 1);
1174 break;
1175 }
1176 }
1177 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001178 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001179 }
1180 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001181 if (*p == '\n')
1182 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001183 if (item->cells > max_cells)
1184 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001185 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001186 }
1187
1188 height = 2 + ga.ga_len;
1189
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001190 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001191 if (long_item_count > 0 && height + long_item_count <= max_height)
1192 {
1193 split_long_items = TRUE;
1194 height += long_item_count;
1195 }
1196
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001197 // Limit to half the window height, it has to fit above or below the mouse
1198 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001199 if (height > max_height)
1200 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001201 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001202 if (*array == NULL)
1203 goto failed;
1204
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001205 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001206 (*array)->pum_text = vim_strsave((char_u *)"");
1207 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1208
1209 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1210 {
1211 int skip;
1212 int thislen;
1213 int copylen;
1214 int ind;
1215 int cells;
1216
1217 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001218 if (item->bytelen == 0)
1219 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1220 else
1221 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001222 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001223 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1224 {
1225 cells = item->indent * 2;
1226 for (p = item->start + skip;
1227 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001228 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001229 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1230 break;
1231 thislen = p - (item->start + skip);
1232 }
1233 else
1234 thislen = item->bytelen;
1235
1236 // put indent at the start
1237 p = alloc(thislen + item->indent * 2 + 1);
1238 if (p == NULL)
1239 {
1240 for (line = 0; line <= height - 1; ++line)
1241 vim_free((*array)[line].pum_text);
1242 vim_free(*array);
1243 goto failed;
1244 }
1245 for (ind = 0; ind < item->indent * 2; ++ind)
1246 p[ind] = ' ';
1247
1248 // exclude spaces at the end of the string
1249 for (copylen = thislen; copylen > 0; --copylen)
1250 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001251 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001252
1253 vim_strncpy(p + ind, item->start + skip, copylen);
1254 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001255 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001256 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001257 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001258 }
1259 ga_clear(&ga);
1260 return height;
1261
1262failed:
1263 ga_clear(&ga);
1264 return 0;
1265}
1266
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001267 void
1268ui_remove_balloon(void)
1269{
1270 if (balloon_array != NULL)
1271 {
1272 pum_undisplay();
1273 while (balloon_arraysize > 0)
1274 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001275 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001276 }
1277}
1278
1279/*
1280 * Terminal version of a balloon, uses the popup menu code.
1281 */
1282 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001283ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001284{
1285 ui_remove_balloon();
1286
Bram Moolenaar246fe032017-11-19 19:56:27 +01001287 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001288 {
1289 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001290 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001291 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001292 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001293 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001294 listitem_T *li;
1295 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001296
Bram Moolenaar246fe032017-11-19 19:56:27 +01001297 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001298 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001299 if (balloon_array == NULL)
1300 return;
1301 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001303 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001304
1305 balloon_array[idx].pum_text = vim_strsave(
1306 text == NULL ? (char_u *)"" : text);
1307 }
1308 }
1309 else
1310 balloon_arraysize = split_message(mesg, &balloon_array);
1311
1312 if (balloon_arraysize > 0)
1313 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001314 pum_array = balloon_array;
1315 pum_size = balloon_arraysize;
1316 pum_compute_size();
1317 pum_scrollbar = 0;
1318 pum_height = balloon_arraysize;
1319
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001320 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001321 pum_selected = -1;
1322 pum_first = 0;
1323 pum_redraw();
1324 }
1325}
1326
1327/*
1328 * Called when the mouse moved, may remove any displayed balloon.
1329 */
1330 void
1331ui_may_remove_balloon(void)
1332{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001333 // For now: remove the balloon whenever the mouse moves to another screen
1334 // cell.
1335 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001336}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001337#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001338
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001339#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001340/*
1341 * Select the pum entry at the mouse position.
1342 */
1343 static void
1344pum_select_mouse_pos(void)
1345{
1346 int idx = mouse_row - pum_row;
1347
1348 if (idx < 0 || idx >= pum_size)
1349 pum_selected = -1;
1350 else if (*pum_array[idx].pum_text != NUL)
1351 pum_selected = idx;
1352}
1353
1354/*
1355 * Execute the currently selected popup menu item.
1356 */
1357 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001358pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001359{
1360 vimmenu_T *mp;
1361 int idx = 0;
1362 exarg_T ea;
1363
1364 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001365 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001366 {
1367 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001368 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001369 break;
1370 }
1371}
1372
1373/*
1374 * Open the terminal version of the popup menu and don't return until it is
1375 * closed.
1376 */
1377 void
1378pum_show_popupmenu(vimmenu_T *menu)
1379{
1380 vimmenu_T *mp;
1381 int idx = 0;
1382 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001383# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001384 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001385# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001386 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001387
1388 pum_undisplay();
1389 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001390 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001391
1392 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001393 if (menu_is_separator(mp->dname)
1394 || (mp->modes & mp->enabled & mode))
1395 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001396
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001397 // When there are only Terminal mode menus, using "popup Edit" results in
1398 // pum_size being zero.
1399 if (pum_size <= 0)
1400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001401 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001402 return;
1403 }
1404
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001405 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001406 if (array == NULL)
1407 return;
1408
1409 for (mp = menu->children; mp != NULL; mp = mp->next)
1410 if (menu_is_separator(mp->dname))
1411 array[idx++].pum_text = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001412 else if (mp->modes & mp->enabled & mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001413 array[idx++].pum_text = mp->dname;
1414
1415 pum_array = array;
1416 pum_compute_size();
1417 pum_scrollbar = 0;
1418 pum_height = pum_size;
1419 pum_position_at_mouse(20);
1420
1421 pum_selected = -1;
1422 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001423# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001424 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001425 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001426# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001427
1428 for (;;)
1429 {
1430 int c;
1431
1432 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001433 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001434 out_flush();
1435
1436 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001437
1438 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1439 // menu.
1440 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001441 break;
1442 else if (c == CAR || c == NL)
1443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001444 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001445 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001446 break;
1447 }
1448 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1449 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001450 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001451 while (pum_selected > 0)
1452 {
1453 --pum_selected;
1454 if (*array[pum_selected].pum_text != NUL)
1455 break;
1456 }
1457 }
1458 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1459 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001460 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001461 while (pum_selected < pum_size - 1)
1462 {
1463 ++pum_selected;
1464 if (*array[pum_selected].pum_text != NUL)
1465 break;
1466 }
1467 }
1468 else if (c == K_RIGHTMOUSE)
1469 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001470 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001471 vungetc(c);
1472 break;
1473 }
1474 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1475 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001476 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001477 pum_select_mouse_pos();
1478 }
1479 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1480 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001481 // left mouse click: select clicked item, if any, and close;
1482 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001483 pum_select_mouse_pos();
1484 if (pum_selected >= 0)
1485 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001486 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001487 break;
1488 }
1489 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1490 break;
1491 }
1492 }
1493
1494 vim_free(array);
1495 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001496# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001497 p_bevalterm = save_bevalterm;
1498 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001499# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001500}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001501
1502 void
1503pum_make_popup(char_u *path_name, int use_mouse_pos)
1504{
1505 vimmenu_T *menu;
1506
1507 if (!use_mouse_pos)
1508 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001509 // Hack: set mouse position at the cursor so that the menu pops up
1510 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001511 mouse_row = curwin->w_winrow + curwin->w_wrow;
1512 mouse_col = curwin->w_wincol + curwin->w_wcol;
1513 }
1514
1515 menu = gui_find_menu(path_name);
1516 if (menu != NULL)
1517 pum_show_popupmenu(menu);
1518}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001519#endif