blob: f908bfca4b092e9c110a45fc391754c044f412a6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar4b779472005-10-04 09:12:31 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar30e8e732019-09-27 13:08:36 +020011 * popupmenu.c: Popup menu (PUM)
Bram Moolenaar4b779472005-10-04 09:12:31 +000012 */
13#include "vim.h"
14
Bram Moolenaar63d9e732019-12-05 21:10:38 +010015static pumitem_T *pum_array = NULL; // items of displayed pum
16static int pum_size; // nr of items in "pum_array"
17static int pum_selected; // index of selected item or -1
18static int pum_first = 0; // index of top item
Bram Moolenaar4b779472005-10-04 09:12:31 +000019
Bram Moolenaarae654382019-01-17 21:09:05 +010020static int call_update_screen = FALSE;
21
Bram Moolenaar63d9e732019-12-05 21:10:38 +010022static int pum_height; // nr of displayed pum items
23static int pum_width; // width of displayed pum items
24static int pum_base_width; // width of pum items base
25static int pum_kind_width; // width of pum items kind column
26static int pum_extra_width; // width of extra stuff
27static int pum_scrollbar; // TRUE when scrollbar present
Bram Moolenaar4b779472005-10-04 09:12:31 +000028
Bram Moolenaar63d9e732019-12-05 21:10:38 +010029static int pum_row; // top row of pum
30static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000031
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020032static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020033static int pum_win_row;
34static int pum_win_height;
35static int pum_win_col;
36static int pum_win_wcol;
37static int pum_win_width;
38
Bram Moolenaar04357fb2019-12-10 21:50:56 +010039// Some parts are not updated when a popup menu is visible. Setting this flag
40// makes pum_visible() return FALSE even when there is a popup menu.
41static int pum_pretend_not_visible = FALSE;
42
43// When set the popup menu will redraw soon using the pum_win_ values. Do not
44// draw over the poup menu area to avoid flicker.
45static int pum_will_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000046
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000048
Bram Moolenaar4b779472005-10-04 09:12:31 +000049#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000050
Bram Moolenaar51b0f372017-11-18 18:52:04 +010051 static void
52pum_compute_size(void)
53{
54 int i;
55 int w;
56
Bram Moolenaar63d9e732019-12-05 21:10:38 +010057 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010058 pum_base_width = 0;
59 pum_kind_width = 0;
60 pum_extra_width = 0;
61 for (i = 0; i < pum_size; ++i)
62 {
63 w = vim_strsize(pum_array[i].pum_text);
64 if (pum_base_width < w)
65 pum_base_width = w;
66 if (pum_array[i].pum_kind != NULL)
67 {
68 w = vim_strsize(pum_array[i].pum_kind) + 1;
69 if (pum_kind_width < w)
70 pum_kind_width = w;
71 }
72 if (pum_array[i].pum_extra != NULL)
73 {
74 w = vim_strsize(pum_array[i].pum_extra) + 1;
75 if (pum_extra_width < w)
76 pum_extra_width = w;
77 }
78 }
79}
80
Bram Moolenaar4b779472005-10-04 09:12:31 +000081/*
82 * Show the popup menu with items "array[size]".
83 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010084 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000085 * The menu appears above the screen line "row" or at "row" + "height" - 1.
86 */
87 void
Bram Moolenaar05540972016-01-30 20:31:25 +010088pum_display(
89 pumitem_T *array,
90 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010091 int selected) // index of initially selected item, none if
92 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000094 int def_width;
95 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000096 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010097 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010098 int above_row;
99 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000100 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100102 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100103#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000104
Bram Moolenaara5e66212017-09-29 22:42:33 +0200105 do
106 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100107 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 above_row = 0;
109 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000110
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100111 // Pretend the pum is already there to avoid that must_redraw is set
112 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200113 pum_array = (pumitem_T *)1;
114 validate_cursor_col();
115 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000116
Bram Moolenaar491ac282018-06-17 14:47:55 +0200117 // Remember the essential parts of the window position and size, so we
118 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200119 pum_window = curwin;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200120 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
121 pum_win_height = curwin->w_height;
122 pum_win_col = curwin->w_wincol;
123 pum_win_wcol = curwin->w_wcol;
124 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000125
Bram Moolenaar4033c552017-09-16 20:54:51 +0200126#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200127 FOR_ALL_WINDOWS(pvwin)
128 if (pvwin->w_p_pvw)
129 break;
130 if (pvwin != NULL)
131 {
132 if (W_WINROW(pvwin) < W_WINROW(curwin))
133 above_row = W_WINROW(pvwin) + pvwin->w_height;
134 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
135 below_row = W_WINROW(pvwin);
136 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100137#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000138
Bram Moolenaara5e66212017-09-29 22:42:33 +0200139 /*
140 * Figure out the size and position of the pum.
141 */
142 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000143 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000144 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200145 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000146 if (p_ph > 0 && pum_height > p_ph)
147 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000148
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100149 // Put the pum below "pum_win_row" if possible. If there are few lines
150 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200151 if (pum_win_row + 2 >= below_row - pum_height
152 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200153 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100154 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200155
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100156 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200157 if (curwin->w_wrow - curwin->w_cline_row >= 2)
158 context_lines = 2;
159 else
160 context_lines = curwin->w_wrow - curwin->w_cline_row;
161
Bram Moolenaar491ac282018-06-17 14:47:55 +0200162 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200163 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200165 pum_height = size;
166 }
167 else
168 {
169 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200170 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200171 }
172 if (p_ph > 0 && pum_height > p_ph)
173 {
174 pum_row += pum_height - p_ph;
175 pum_height = p_ph;
176 }
177 }
178 else
179 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100180 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200181
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100182 // Leave two lines of context if possible
Bram 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 Moolenaar4b779472005-10-04 09:12:31 +0000360}
361
362/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100363 * Set a flag that when pum_redraw() is called it first calls update_screen().
364 * This will avoid clearing and redrawing the popup menu, prevent flicker.
365 */
366 void
367pum_call_update_screen()
368{
369 call_update_screen = TRUE;
370
371 // Update the cursor position to be able to compute the popup menu
372 // position. The cursor line length may have changed because of the
373 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100374 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100375 validate_cursor();
376}
377
378/*
379 * Return TRUE if we are going to redraw the popup menu and the screen position
380 * "row"/"col" is under the popup menu.
381 */
382 int
383pum_under_menu(int row, int col)
384{
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100385 return pum_will_redraw
Bram Moolenaarae654382019-01-17 21:09:05 +0100386 && row >= pum_row
387 && row < pum_row + pum_height
388 && col >= pum_col - 1
389 && col < pum_col + pum_width;
390}
391
392/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000393 * Redraw the popup menu, using "pum_first" and "pum_selected".
394 */
395 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100396pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000397{
398 int row = pum_row;
399 int col;
400 int attr_norm = highlight_attr[HLF_PNI];
401 int attr_select = highlight_attr[HLF_PSI];
402 int attr_scroll = highlight_attr[HLF_PSB];
403 int attr_thumb = highlight_attr[HLF_PST];
404 int attr;
405 int i;
406 int idx;
407 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000408 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000409 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000410 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100411 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000412 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000413 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000414
Bram Moolenaarae654382019-01-17 21:09:05 +0100415 if (call_update_screen)
416 {
417 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100418 // Do not redraw in pum_may_redraw() and don't draw in the area where
419 // the popup menu will be.
420 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100421 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100422 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100423 }
424
425 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100426 if (pum_first > pum_size - pum_height)
427 pum_first = pum_size - pum_height;
428
Bram Moolenaar4b779472005-10-04 09:12:31 +0000429 if (pum_scrollbar)
430 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100431 thumb_height = pum_height * pum_height / pum_size;
432 if (thumb_height == 0)
433 thumb_height = 1;
434 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000435 + (pum_size - pum_height) / 2)
436 / (pum_size - pum_height);
437 }
438
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100439#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200440 // The popup menu is drawn over popup menus with zindex under
441 // POPUPMENU_ZINDEX.
442 screen_zindex = POPUPMENU_ZINDEX;
443#endif
444
Bram Moolenaar4b779472005-10-04 09:12:31 +0000445 for (i = 0; i < pum_height; ++i)
446 {
447 idx = i + pum_first;
448 attr = (idx == pum_selected) ? attr_select : attr_norm;
449
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100450 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000451#ifdef FEAT_RIGHTLEFT
452 if (curwin->w_p_rl)
453 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200454 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000455 screen_putchar(' ', row, pum_col + 1, attr);
456 }
457 else
458#endif
459 if (pum_col > 0)
460 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000461
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100462 // Display each entry, use two spaces for a Tab.
463 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000464 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000465 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000466 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000467 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000468 width = 0;
469 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000470 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000471 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000472 case 1: p = pum_array[idx].pum_text; break;
473 case 2: p = pum_array[idx].pum_kind; break;
474 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000475 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000476 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100477 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000478 {
479 if (s == NULL)
480 s = p;
481 w = ptr2cells(p);
482 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100484 // Display the text that fits or comes before a Tab.
485 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000486 char_u *st;
487 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000488
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100489 if (saved != NUL)
490 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000491 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100492 if (saved != NUL)
493 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000494#ifdef FEAT_RIGHTLEFT
495 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000496 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000497 if (st != NULL)
498 {
499 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000500
501 if (rt != NULL)
502 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100503 char_u *rt_start = rt;
504 int size;
505
506 size = vim_strsize(rt);
507 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000508 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100509 do
510 {
511 size -= has_mbyte
512 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100513 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100514 } while (size > pum_width);
515
516 if (size < pum_width)
517 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100518 // Most left character requires
519 // 2-cells but only 1 cell is
520 // available on screen. Put a
521 // '<' on the left of the pum
522 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100523 *(--rt) = '<';
524 size++;
525 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000526 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100527 screen_puts_len(rt, (int)STRLEN(rt),
528 row, col - size + 1, attr);
529 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000530 }
531 vim_free(st);
532 }
533 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000534 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000535 else
536#endif
537 {
538 if (st != NULL)
539 {
540 screen_puts_len(st, (int)STRLEN(st), row, col,
541 attr);
542 vim_free(st);
543 }
544 col += width;
545 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000546
547 if (*p != TAB)
548 break;
549
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100550 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000551#ifdef FEAT_RIGHTLEFT
552 if (curwin->w_p_rl)
553 {
554 screen_puts_len((char_u *)" ", 2, row, col - 1,
555 attr);
556 col -= 2;
557 }
558 else
559#endif
560 {
561 screen_puts_len((char_u *)" ", 2, row, col, attr);
562 col += 2;
563 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000564 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100565 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000566 width = 0;
567 }
568 else
569 width += w;
570 }
571
572 if (round > 1)
573 n = pum_kind_width + 1;
574 else
575 n = 1;
576
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100577 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000578 if (round == 3
579 || (round == 2 && pum_array[idx].pum_extra == NULL)
580 || (round == 1 && pum_array[idx].pum_kind == NULL
581 && pum_array[idx].pum_extra == NULL)
582 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000583 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000584#ifdef FEAT_RIGHTLEFT
585 if (curwin->w_p_rl)
586 {
587 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
588 col + 1, ' ', ' ', attr);
589 col = pum_col - pum_base_width - n + 1;
590 }
591 else
592#endif
593 {
594 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000595 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000596 col = pum_col + pum_base_width + n;
597 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000598 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000599 }
600
Bram Moolenaarabc97732007-08-08 20:49:37 +0000601#ifdef FEAT_RIGHTLEFT
602 if (curwin->w_p_rl)
603 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
604 ' ', attr);
605 else
606#endif
607 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
608 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000609 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000610 {
611#ifdef FEAT_RIGHTLEFT
612 if (curwin->w_p_rl)
613 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100614 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000615 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000616 else
617#endif
618 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100619 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000620 ? attr_thumb : attr_scroll);
621 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000622
623 ++row;
624 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200625
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100626#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200627 screen_zindex = 0;
628#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000629}
630
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100631#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200632/*
633 * Position the info popup relative to the popup menu item.
634 */
635 void
636pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200637{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100638 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200639 int row = pum_row;
640 int botpos = POPPOS_BOTLEFT;
641
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200642 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200643 if (Columns - col < 20 && Columns - col < pum_col)
644 {
645 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200646 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200647 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200648 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200649 }
650 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200651 wp->w_maxwidth = Columns - col + 1;
652 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200653
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200654 row -= popup_top_extra(wp);
655 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200656 {
657 if (pum_row < pum_win_row)
658 {
659 // menu above cursor line, align with bottom
660 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200661 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200662 }
663 else
664 // menu below cursor line, align with top
665 row += 1;
666 }
667 else
668 // align with the selected item
669 row += pum_selected - pum_first + 1;
670
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100671 wp->w_popup_flags &= ~POPF_HIDDEN;
672 if (wp->w_maxwidth < 10)
673 // The popup is not going to fit or will overlap with the cursor
674 // position, hide the popup.
675 wp->w_popup_flags |= POPF_HIDDEN;
676 else
677 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200678}
679#endif
680
Bram Moolenaar4b779472005-10-04 09:12:31 +0000681/*
682 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000683 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000684 * This may be repeated when the preview window is used:
685 * "repeat" == 0: open preview window normally
686 * "repeat" == 1: open preview window but don't set the size
687 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000688 * Returns TRUE when the window was resized and the location of the popup menu
689 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000690 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000691 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200692pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000693{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000694 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000695 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200696#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200697 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200698#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100699#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200700 int has_info = FALSE;
701#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000702
Bram Moolenaar4b779472005-10-04 09:12:31 +0000703 pum_selected = n;
704
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000705 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000706 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000707 if (pum_first > pum_selected - 4)
708 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100709 // scroll down; when we did a jump it's probably a PageUp then
710 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000711 if (pum_first > pum_selected - 2)
712 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000713 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000714 if (pum_first < 0)
715 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000716 else if (pum_first > pum_selected)
717 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000718 }
719 else
720 pum_first = pum_selected;
721 }
722 else if (pum_first < pum_selected - pum_height + 5)
723 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100724 // scroll up; when we did a jump it's probably a PageDown then
725 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000726 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000727 {
728 pum_first += pum_height - 2;
729 if (pum_first < pum_selected - pum_height + 1)
730 pum_first = pum_selected - pum_height + 1;
731 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000732 else
733 pum_first = pum_selected - pum_height + 1;
734 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000735
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100736 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000737 if (context > 3)
738 context = 3;
739 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000740 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000741 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100743 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000744 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000745 if (pum_first < 0)
746 pum_first = 0;
747 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000748 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000749 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100750 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000751 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000752 }
753 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200754 // adjust for the number of lines displayed
755 if (pum_first > pum_size - pum_height)
756 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000757
Bram Moolenaar4033c552017-09-16 20:54:51 +0200758#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000759 /*
760 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100761 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000762 * Skip this when tried twice already.
763 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000764 * NOTE: Be very careful not to sync undo!
765 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000766 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000767 && Rows > 10
768 && repeat <= 1
769 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000770 {
771 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200772 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000773 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100774# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200775 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200776# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100777# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200778# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100779# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200780 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200781 if (strstr((char *)p_cot, "popuphidden") != NULL)
782 use_popup = USEPOPUP_HIDDEN;
783 else if (strstr((char *)p_cot, "popup") != NULL)
784 use_popup = USEPOPUP_NORMAL;
785 else
786 use_popup = USEPOPUP_NONE;
Bram Moolenaard570ab92019-09-03 23:20:05 +0200787# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200788 // Open a preview window and set "curwin" to it.
789 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000790 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200791 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
792 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200793 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200794 // Prevent undo sync here, if an autocommand syncs undo weird
795 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100796 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200797 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100798 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200799 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000800 g_do_tagpreview = 0;
801
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200802 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100803# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200804 || (curwin->w_popup_flags & POPF_INFO)
805# endif
806 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000807 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200808 if (!resized
809 && curbuf->b_nwindows == 1
810 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200811 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000812 && curbuf->b_p_bh[0] == 'w')
813 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200814 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100815 while (!BUFEMPTY())
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000816 ml_delete((linenr_T)1, FALSE);
817 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000818 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000819 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200820 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000821 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000822 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000823 --no_u_sync;
824 if (res == OK)
825 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200826 // Edit a new, empty buffer. Set options for a "wipeout"
827 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000828 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
829 set_option_value((char_u *)"bt", 0L,
830 (char_u *)"nofile", OPT_LOCAL);
831 set_option_value((char_u *)"bh", 0L,
832 (char_u *)"wipe", OPT_LOCAL);
833 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000834 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000835 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000836 }
837 if (res == OK)
838 {
839 char_u *p, *e;
840 linenr_T lnum = 0;
841
842 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
843 {
844 e = vim_strchr(p, '\n');
845 if (e == NULL)
846 {
847 ml_append(lnum++, p, 0, FALSE);
848 break;
849 }
850 else
851 {
852 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000853 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000854 *e = '\n';
855 p = e + 1;
856 }
857 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200858 // delete the empty last line
859 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000860
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100861 // Increase the height of the preview window to show the
862 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200863 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000864 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000865 if (lnum > p_pvh)
866 lnum = p_pvh;
867 if (curwin->w_height < lnum)
868 {
869 win_setheight((int)lnum);
870 resized = TRUE;
871 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000872 }
873
874 curbuf->b_changed = 0;
875 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200876 if (pum_selected != prev_selected)
877 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100878# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200879 curwin->w_firstline = 1;
880# endif
881 curwin->w_topline = 1;
882 }
883 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
884 curwin->w_topline = curbuf->b_ml.ml_line_count;
885 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000886 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100887# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200888 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200889 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200890 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200891 if (win_valid(curwin_save))
892 redraw_win_later(curwin_save, SOME_VALID);
893 }
894# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200895 if ((curwin != curwin_save && win_valid(curwin_save))
896 || (curtab != curtab_save
897 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000898 {
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200899 if (curtab != curtab_save && valid_tabpage(curtab_save))
900 goto_tabpage_tp(curtab_save, FALSE, FALSE);
901
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100902 // When the first completion is done and the preview
903 // window is not resized, skip the preview window's
904 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200905 if (ins_compl_active() && !resized)
906 curwin->w_redr_status = FALSE;
907
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100908 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000909 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000910 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000911
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100912 // When the preview window was resized we need to
913 // update the view on the buffer. Only go back to
914 // the window when needed, otherwise it will always be
915 // redraw.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200916 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000917 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100918 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000919 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100920 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000921 update_topline();
922 }
923
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100924 // Update the screen before drawing the popup menu.
925 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100926 pum_pretend_not_visible = TRUE;
927 // But don't draw text at the new popup menu position,
928 // it causes flicker.
929 pum_will_redraw = TRUE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000930 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100931 pum_pretend_not_visible = FALSE;
932 pum_will_redraw = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000933
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000934 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100935 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100936# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200937 win_T *wp = curwin;
938# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100939 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000940 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100941 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100942# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200943 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
944 popup_hide(wp);
945# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100946 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000947
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100948 // May need to update the screen again when there are
949 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100950 pum_pretend_not_visible = TRUE;
951 pum_will_redraw = TRUE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000952 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100953 pum_pretend_not_visible = FALSE;
954 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100955 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000956 }
957 }
958 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100959# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200960 if (WIN_IS_POPUP(curwin))
961 // can't keep focus in a popup window
962 win_enter(firstwin, TRUE);
963# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000964 }
965#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000966 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100967#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200968 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200969 // hide any popup info window
970 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200971#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000972
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000973 if (!resized)
974 pum_redraw();
975
976 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000977}
978
979/*
980 * Undisplay the popup menu (later).
981 */
982 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100983pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000984{
985 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +0200986 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +0000987 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000988 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100989#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +0200990 // hide any popup info window
991 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200992#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000993}
994
995/*
996 * Clear the popup menu. Currently only resets the offset to the first
997 * displayed item.
998 */
999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001000pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001001{
1002 pum_first = 0;
1003}
1004
1005/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001006 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1007 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1008 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001009 */
1010 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001011pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001012{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001013 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001014}
1015
Bram Moolenaare3226be2005-12-18 22:10:00 +00001016/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001017 * Reposition the popup menu to adjust for window layout changes.
1018 */
1019 void
1020pum_may_redraw(void)
1021{
1022 pumitem_T *array = pum_array;
1023 int len = pum_size;
1024 int selected = pum_selected;
1025
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001026 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001027 return; // nothing to do
1028
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001029 if (pum_window != curwin
1030 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1031 && pum_win_height == curwin->w_height
1032 && pum_win_col == curwin->w_wincol
1033 && pum_win_width == curwin->w_width))
Bram Moolenaar491ac282018-06-17 14:47:55 +02001034 {
1035 // window position didn't change, redraw in the same position
1036 pum_redraw();
1037 }
1038 else
1039 {
1040 int wcol = curwin->w_wcol;
1041
1042 // Window layout changed, recompute the position.
1043 // Use the remembered w_wcol value, the cursor may have moved when a
1044 // completion was inserted, but we want the menu in the same position.
1045 pum_undisplay();
1046 curwin->w_wcol = pum_win_wcol;
1047 curwin->w_valid |= VALID_WCOL;
1048 pum_display(array, len, selected);
1049 curwin->w_wcol = wcol;
1050 }
1051}
1052
1053/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001054 * Return the height of the popup menu, the number of entries visible.
1055 * Only valid when pum_visible() returns TRUE!
1056 */
1057 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001058pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001059{
1060 return pum_height;
1061}
1062
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001063#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001064/*
1065 * Add size information about the pum to "dict".
1066 */
1067 void
1068pum_set_event_info(dict_T *dict)
1069{
1070 if (!pum_visible())
1071 return;
1072 dict_add_number(dict, "height", pum_height);
1073 dict_add_number(dict, "width", pum_width);
1074 dict_add_number(dict, "row", pum_row);
1075 dict_add_number(dict, "col", pum_col);
1076 dict_add_number(dict, "size", pum_size);
1077 dict_add_special(dict, "scrollbar", pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
1078}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001079#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001080
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001081#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001082 static void
1083pum_position_at_mouse(int min_width)
1084{
1085 if (Rows - mouse_row > pum_size)
1086 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001087 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001088 pum_row = mouse_row + 1;
1089 if (pum_height > Rows - pum_row)
1090 pum_height = Rows - pum_row;
1091 }
1092 else
1093 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001094 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001095 pum_row = mouse_row - pum_size;
1096 if (pum_row < 0)
1097 {
1098 pum_height += pum_row;
1099 pum_row = 0;
1100 }
1101 }
1102 if (Columns - mouse_col >= pum_base_width
1103 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001104 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001105 pum_col = mouse_col;
1106 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001107 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001108 pum_col = Columns - (pum_base_width > min_width
1109 ? min_width : pum_base_width);
1110
1111 pum_width = Columns - pum_col;
1112 if (pum_width > pum_base_width + 1)
1113 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001114
1115 // Do not redraw at cursor position.
1116 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001117}
1118
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001119#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001120
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001121#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001122static pumitem_T *balloon_array = NULL;
1123static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001124
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001125# define BALLOON_MIN_WIDTH 50
1126# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001127
Bram Moolenaar246fe032017-11-19 19:56:27 +01001128typedef struct {
1129 char_u *start;
1130 int bytelen;
1131 int cells;
1132 int indent;
1133} balpart_T;
1134
1135/*
1136 * Split a string into parts to display in the balloon.
1137 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1138 * strings and make a struct look good.
1139 * Resulting array is stored in "array" and returns the size of the array.
1140 */
1141 int
1142split_message(char_u *mesg, pumitem_T **array)
1143{
1144 garray_T ga;
1145 char_u *p;
1146 balpart_T *item;
1147 int quoted = FALSE;
1148 int height;
1149 int line;
1150 int item_idx;
1151 int indent = 0;
1152 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001153 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001154 int long_item_count = 0;
1155 int split_long_items = FALSE;
1156
1157 ga_init2(&ga, sizeof(balpart_T), 20);
1158 p = mesg;
1159
1160 while (*p != NUL)
1161 {
1162 if (ga_grow(&ga, 1) == FAIL)
1163 goto failed;
1164 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1165 item->start = p;
1166 item->indent = indent;
1167 item->cells = indent * 2;
1168 ++ga.ga_len;
1169 while (*p != NUL)
1170 {
1171 if (*p == '"')
1172 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001173 else if (*p == '\n')
1174 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001175 else if (*p == '\\' && p[1] != NUL)
1176 ++p;
1177 else if (!quoted)
1178 {
1179 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1180 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001181 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001182 if (*p == '{')
1183 ++indent;
1184 else if (*p == '}' && indent > 0)
1185 --indent;
1186 ++item->cells;
1187 p = skipwhite(p + 1);
1188 break;
1189 }
1190 }
1191 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001192 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001193 }
1194 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001195 if (*p == '\n')
1196 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001197 if (item->cells > max_cells)
1198 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001199 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001200 }
1201
1202 height = 2 + ga.ga_len;
1203
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001204 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001205 if (long_item_count > 0 && height + long_item_count <= max_height)
1206 {
1207 split_long_items = TRUE;
1208 height += long_item_count;
1209 }
1210
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001211 // Limit to half the window height, it has to fit above or below the mouse
1212 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001213 if (height > max_height)
1214 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001215 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001216 if (*array == NULL)
1217 goto failed;
1218
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001219 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001220 (*array)->pum_text = vim_strsave((char_u *)"");
1221 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1222
1223 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1224 {
1225 int skip;
1226 int thislen;
1227 int copylen;
1228 int ind;
1229 int cells;
1230
1231 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001232 if (item->bytelen == 0)
1233 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1234 else
1235 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001236 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001237 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1238 {
1239 cells = item->indent * 2;
1240 for (p = item->start + skip;
1241 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001242 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001243 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1244 break;
1245 thislen = p - (item->start + skip);
1246 }
1247 else
1248 thislen = item->bytelen;
1249
1250 // put indent at the start
1251 p = alloc(thislen + item->indent * 2 + 1);
1252 if (p == NULL)
1253 {
1254 for (line = 0; line <= height - 1; ++line)
1255 vim_free((*array)[line].pum_text);
1256 vim_free(*array);
1257 goto failed;
1258 }
1259 for (ind = 0; ind < item->indent * 2; ++ind)
1260 p[ind] = ' ';
1261
1262 // exclude spaces at the end of the string
1263 for (copylen = thislen; copylen > 0; --copylen)
1264 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001265 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001266
1267 vim_strncpy(p + ind, item->start + skip, copylen);
1268 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001269 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001270 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001271 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001272 }
1273 ga_clear(&ga);
1274 return height;
1275
1276failed:
1277 ga_clear(&ga);
1278 return 0;
1279}
1280
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001281 void
1282ui_remove_balloon(void)
1283{
1284 if (balloon_array != NULL)
1285 {
1286 pum_undisplay();
1287 while (balloon_arraysize > 0)
1288 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001289 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001290 }
1291}
1292
1293/*
1294 * Terminal version of a balloon, uses the popup menu code.
1295 */
1296 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001297ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001298{
1299 ui_remove_balloon();
1300
Bram Moolenaar246fe032017-11-19 19:56:27 +01001301 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001302 {
1303 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001304 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001305 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001306 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001307 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001308 listitem_T *li;
1309 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001310
Bram Moolenaar246fe032017-11-19 19:56:27 +01001311 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001312 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001313 if (balloon_array == NULL)
1314 return;
1315 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001317 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001318
1319 balloon_array[idx].pum_text = vim_strsave(
1320 text == NULL ? (char_u *)"" : text);
1321 }
1322 }
1323 else
1324 balloon_arraysize = split_message(mesg, &balloon_array);
1325
1326 if (balloon_arraysize > 0)
1327 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001328 pum_array = balloon_array;
1329 pum_size = balloon_arraysize;
1330 pum_compute_size();
1331 pum_scrollbar = 0;
1332 pum_height = balloon_arraysize;
1333
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001334 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001335 pum_selected = -1;
1336 pum_first = 0;
1337 pum_redraw();
1338 }
1339}
1340
1341/*
1342 * Called when the mouse moved, may remove any displayed balloon.
1343 */
1344 void
1345ui_may_remove_balloon(void)
1346{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001347 // For now: remove the balloon whenever the mouse moves to another screen
1348 // cell.
1349 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001350}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001351#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001352
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001353#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001354/*
1355 * Select the pum entry at the mouse position.
1356 */
1357 static void
1358pum_select_mouse_pos(void)
1359{
1360 int idx = mouse_row - pum_row;
1361
1362 if (idx < 0 || idx >= pum_size)
1363 pum_selected = -1;
1364 else if (*pum_array[idx].pum_text != NUL)
1365 pum_selected = idx;
1366}
1367
1368/*
1369 * Execute the currently selected popup menu item.
1370 */
1371 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001372pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001373{
1374 vimmenu_T *mp;
1375 int idx = 0;
1376 exarg_T ea;
1377
1378 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001379 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001380 {
1381 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001382 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001383 break;
1384 }
1385}
1386
1387/*
1388 * Open the terminal version of the popup menu and don't return until it is
1389 * closed.
1390 */
1391 void
1392pum_show_popupmenu(vimmenu_T *menu)
1393{
1394 vimmenu_T *mp;
1395 int idx = 0;
1396 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001397# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001398 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001399# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001400 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001401
1402 pum_undisplay();
1403 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001404 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001405
1406 for (mp = menu->children; mp != NULL; mp = mp->next)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001407 if (menu_is_separator(mp->dname)
1408 || (mp->modes & mp->enabled & mode))
1409 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001410
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001411 // When there are only Terminal mode menus, using "popup Edit" results in
1412 // pum_size being zero.
1413 if (pum_size <= 0)
1414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001415 emsg(e_menuothermode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001416 return;
1417 }
1418
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001419 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001420 if (array == NULL)
1421 return;
1422
1423 for (mp = menu->children; mp != NULL; mp = mp->next)
1424 if (menu_is_separator(mp->dname))
1425 array[idx++].pum_text = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001426 else if (mp->modes & mp->enabled & mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001427 array[idx++].pum_text = mp->dname;
1428
1429 pum_array = array;
1430 pum_compute_size();
1431 pum_scrollbar = 0;
1432 pum_height = pum_size;
1433 pum_position_at_mouse(20);
1434
1435 pum_selected = -1;
1436 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001437# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001438 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001439 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001440# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001441
1442 for (;;)
1443 {
1444 int c;
1445
1446 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001447 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001448 out_flush();
1449
1450 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001451
1452 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1453 // menu.
1454 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001455 break;
1456 else if (c == CAR || c == NL)
1457 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001458 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001459 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001460 break;
1461 }
1462 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1463 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001464 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001465 while (pum_selected > 0)
1466 {
1467 --pum_selected;
1468 if (*array[pum_selected].pum_text != NUL)
1469 break;
1470 }
1471 }
1472 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1473 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001474 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001475 while (pum_selected < pum_size - 1)
1476 {
1477 ++pum_selected;
1478 if (*array[pum_selected].pum_text != NUL)
1479 break;
1480 }
1481 }
1482 else if (c == K_RIGHTMOUSE)
1483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001484 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001485 vungetc(c);
1486 break;
1487 }
1488 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1489 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001490 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001491 pum_select_mouse_pos();
1492 }
1493 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1494 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001495 // left mouse click: select clicked item, if any, and close;
1496 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001497 pum_select_mouse_pos();
1498 if (pum_selected >= 0)
1499 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001500 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001501 break;
1502 }
1503 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1504 break;
1505 }
1506 }
1507
1508 vim_free(array);
1509 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001510# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001511 p_bevalterm = save_bevalterm;
1512 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001513# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001514}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001515
1516 void
1517pum_make_popup(char_u *path_name, int use_mouse_pos)
1518{
1519 vimmenu_T *menu;
1520
1521 if (!use_mouse_pos)
1522 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001523 // Hack: set mouse position at the cursor so that the menu pops up
1524 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001525 mouse_row = curwin->w_winrow + curwin->w_wrow;
1526 mouse_col = curwin->w_wincol + curwin->w_wcol;
1527 }
1528
1529 menu = gui_find_menu(path_name);
1530 if (menu != NULL)
1531 pum_show_popupmenu(menu);
1532}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001533#endif