blob: 18f24803358bfdff1774f8c6ddf4bf7367cd293b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar4b779472005-10-04 09:12:31 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar30e8e732019-09-27 13:08:36 +020011 * popupmenu.c: Popup menu (PUM)
Bram Moolenaar4b779472005-10-04 09:12:31 +000012 */
13#include "vim.h"
14
Bram Moolenaar63d9e732019-12-05 21:10:38 +010015static pumitem_T *pum_array = NULL; // items of displayed pum
16static int pum_size; // nr of items in "pum_array"
17static int pum_selected; // index of selected item or -1
18static int pum_first = 0; // index of top item
Bram Moolenaar4b779472005-10-04 09:12:31 +000019
Bram Moolenaarae654382019-01-17 21:09:05 +010020static int call_update_screen = FALSE;
21
Bram Moolenaar63d9e732019-12-05 21:10:38 +010022static int pum_height; // nr of displayed pum items
23static int pum_width; // width of displayed pum items
24static int pum_base_width; // width of pum items base
25static int pum_kind_width; // width of pum items kind column
26static int pum_extra_width; // width of extra stuff
27static int pum_scrollbar; // TRUE when scrollbar present
Bram Moolenaar4b779472005-10-04 09:12:31 +000028
Bram Moolenaar63d9e732019-12-05 21:10:38 +010029static int pum_row; // top row of pum
30static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000031
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020032static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020033static int pum_win_row;
34static int pum_win_height;
35static int pum_win_col;
36static int pum_win_wcol;
37static int pum_win_width;
38
Bram Moolenaar04357fb2019-12-10 21:50:56 +010039// Some parts are not updated when a popup menu is visible. Setting this flag
40// makes pum_visible() return FALSE even when there is a popup menu.
41static int pum_pretend_not_visible = FALSE;
42
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010043static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000044
Bram Moolenaar4b779472005-10-04 09:12:31 +000045#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000046
Bram Moolenaar51b0f372017-11-18 18:52:04 +010047 static void
48pum_compute_size(void)
49{
50 int i;
51 int w;
52
Bram Moolenaar63d9e732019-12-05 21:10:38 +010053 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010054 pum_base_width = 0;
55 pum_kind_width = 0;
56 pum_extra_width = 0;
57 for (i = 0; i < pum_size; ++i)
58 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020059 if (pum_array[i].pum_text != NULL)
60 {
61 w = vim_strsize(pum_array[i].pum_text);
62 if (pum_base_width < w)
63 pum_base_width = w;
64 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010065 if (pum_array[i].pum_kind != NULL)
66 {
67 w = vim_strsize(pum_array[i].pum_kind) + 1;
68 if (pum_kind_width < w)
69 pum_kind_width = w;
70 }
71 if (pum_array[i].pum_extra != NULL)
72 {
73 w = vim_strsize(pum_array[i].pum_extra) + 1;
74 if (pum_extra_width < w)
75 pum_extra_width = w;
76 }
77 }
78}
79
Bram Moolenaar4b779472005-10-04 09:12:31 +000080/*
81 * Show the popup menu with items "array[size]".
82 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010083 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000084 * The menu appears above the screen line "row" or at "row" + "height" - 1.
85 */
86 void
Bram Moolenaar05540972016-01-30 20:31:25 +010087pum_display(
88 pumitem_T *array,
89 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010090 int selected) // index of initially selected item, none if
91 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000092{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093 int def_width;
94 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000095 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010096 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010097 int above_row;
98 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000099 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100101 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100102#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000103
Bram Moolenaara5e66212017-09-29 22:42:33 +0200104 do
105 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100106 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200107 above_row = 0;
108 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000109
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100110 // Pretend the pum is already there to avoid that must_redraw is set
111 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200112 pum_array = (pumitem_T *)1;
113 validate_cursor_col();
114 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000115
Bram Moolenaar491ac282018-06-17 14:47:55 +0200116 // Remember the essential parts of the window position and size, so we
117 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200118 pum_window = curwin;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000119 if (State == CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000120 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000121 pum_win_row = cmdline_row;
122 else
123 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200124 pum_win_height = curwin->w_height;
125 pum_win_col = curwin->w_wincol;
126 pum_win_wcol = curwin->w_wcol;
127 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000128
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200130 FOR_ALL_WINDOWS(pvwin)
131 if (pvwin->w_p_pvw)
132 break;
133 if (pvwin != NULL)
134 {
135 if (W_WINROW(pvwin) < W_WINROW(curwin))
136 above_row = W_WINROW(pvwin) + pvwin->w_height;
137 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
138 below_row = W_WINROW(pvwin);
139 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100140#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141
Bram Moolenaara5e66212017-09-29 22:42:33 +0200142 /*
143 * Figure out the size and position of the pum.
144 */
145 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000146 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000147 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200148 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000149 if (p_ph > 0 && pum_height > p_ph)
150 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000151
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100152 // Put the pum below "pum_win_row" if possible. If there are few lines
153 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200154 if (pum_win_row + 2 >= below_row - pum_height
155 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200156 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100157 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200158
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100159 // Leave two lines of context if possible
Bram Moolenaara5e66212017-09-29 22:42:33 +0200160 if (curwin->w_wrow - curwin->w_cline_row >= 2)
161 context_lines = 2;
162 else
163 context_lines = curwin->w_wrow - curwin->w_cline_row;
164
Bram Moolenaar491ac282018-06-17 14:47:55 +0200165 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200168 pum_height = size;
169 }
170 else
171 {
172 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200173 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200174 }
175 if (p_ph > 0 && pum_height > p_ph)
176 {
177 pum_row += pum_height - p_ph;
178 pum_height = p_ph;
179 }
180 }
181 else
182 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100183 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100185 // Leave two lines of context if possible
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100186 validate_cheight();
Bram Moolenaara5e66212017-09-29 22:42:33 +0200187 if (curwin->w_cline_row
188 + curwin->w_cline_height - curwin->w_wrow >= 3)
189 context_lines = 3;
190 else
191 context_lines = curwin->w_cline_row
192 + curwin->w_cline_height - curwin->w_wrow;
193
Bram Moolenaar491ac282018-06-17 14:47:55 +0200194 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195 if (size > below_row - pum_row)
196 pum_height = below_row - pum_row;
197 else
198 pum_height = size;
199 if (p_ph > 0 && pum_height > p_ph)
200 pum_height = p_ph;
201 }
202
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100203 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200204 if (pum_height < 1 || (pum_height == 1 && size > 1))
205 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000206
Bram Moolenaar4033c552017-09-16 20:54:51 +0200207#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100208 // If there is a preview window above avoid drawing over it.
209 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000210 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100211 pum_row = above_row;
212 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000213 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200214#endif
215
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100216 pum_array = array;
217 pum_size = size;
218 pum_compute_size();
219 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000220
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100221 // Calculate column
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000222#ifdef FEAT_WILDMENU
223 if (State == CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000224 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000225 cursor_col = cmdline_compl_startcol();
226 else
227#endif
Bram Moolenaarabc97732007-08-08 20:49:37 +0000228#ifdef FEAT_RIGHTLEFT
229 if (curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100230 cursor_col = curwin->w_wincol + curwin->w_width
231 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000232 else
233#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100234 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100236 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200237 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000238 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200239 pum_scrollbar = 1;
240 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000241 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000242 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200243 pum_scrollbar = 0;
244
245 if (def_width < max_width)
246 def_width = max_width;
247
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100248 if (((cursor_col < Columns - p_pw
249 || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000250#ifdef FEAT_RIGHTLEFT
Bram Moolenaara5e66212017-09-29 22:42:33 +0200251 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100252 || (curwin->w_p_rl
253 && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000254#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200255 ))
256 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100257 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100258 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000259
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100260 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200261#ifdef FEAT_RIGHTLEFT
262 if (curwin->w_p_rl)
263 pum_width = pum_col - pum_scrollbar + 1;
264 else
265#endif
266 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000267
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100268 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100269 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200270 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100271 // the width is more than needed for the items, make it
272 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100273 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100274 if (pum_width < p_pw)
275 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200276 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100277 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100278#ifdef FEAT_RIGHTLEFT
279 && !curwin->w_p_rl)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100280 || (curwin->w_p_rl && (cursor_col < Columns - p_pw
281 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100282#endif
283 ))
284 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100285 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100286#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100287 if (curwin->w_p_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100288 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100289 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100290 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100291 if (pum_col >= Columns)
292 pum_col = Columns - 1;
293 }
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100294 else if (!curwin->w_p_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295#endif
296 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100297 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
298 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100299 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100300 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100301 pum_col = Columns - max_width - pum_scrollbar;
302 if (pum_col < 0)
303 pum_col = 0;
304 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100305 }
306
307#ifdef FEAT_RIGHTLEFT
308 if (curwin->w_p_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100309 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100310 else
311#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100312 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100313
Bram Moolenaar42443c72018-02-10 18:28:52 +0100314 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100315 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100316 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100317#ifdef FEAT_RIGHTLEFT
318 if (curwin->w_p_rl)
319 {
320 if (pum_width > pum_col)
321 pum_width = pum_col;
322 }
323 else
324#endif
325 {
326 if (pum_width >= Columns - pum_col)
327 pum_width = Columns - pum_col - 1;
328 }
329 }
330 else if (pum_width > max_width + pum_kind_width
331 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100332 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100333 {
334 pum_width = max_width + pum_kind_width
335 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100336 if (pum_width < p_pw)
337 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100338 }
339 }
340
Bram Moolenaara5e66212017-09-29 22:42:33 +0200341 }
342 else if (Columns < def_width)
343 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100344 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200345#ifdef FEAT_RIGHTLEFT
346 if (curwin->w_p_rl)
347 pum_col = Columns - 1;
348 else
349#endif
350 pum_col = 0;
351 pum_width = Columns - 1;
352 }
353 else
354 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100355 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100356 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200357#ifdef FEAT_RIGHTLEFT
358 if (curwin->w_p_rl)
359 pum_col = max_width - 1;
360 else
361#endif
362 pum_col = Columns - max_width;
363 pum_width = max_width - pum_scrollbar;
364 }
365
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100366 // Set selected item and redraw. If the window size changed need to
367 // redo the positioning. Limit this to two times, when there is not
368 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200369 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100370
371 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000372}
373
374/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100375 * Set a flag that when pum_redraw() is called it first calls update_screen().
376 * This will avoid clearing and redrawing the popup menu, prevent flicker.
377 */
378 void
379pum_call_update_screen()
380{
381 call_update_screen = TRUE;
382
383 // Update the cursor position to be able to compute the popup menu
384 // position. The cursor line length may have changed because of the
385 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100386 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100387 validate_cursor();
388}
389
390/*
391 * Return TRUE if we are going to redraw the popup menu and the screen position
392 * "row"/"col" is under the popup menu.
393 */
394 int
Bakudankun65555002021-11-17 20:40:16 +0000395pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100396{
Bakudankun65555002021-11-17 20:40:16 +0000397 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100398 && row >= pum_row
399 && row < pum_row + pum_height
400 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200401 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100402}
403
404/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000405 * Redraw the popup menu, using "pum_first" and "pum_selected".
406 */
407 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100408pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000409{
410 int row = pum_row;
411 int col;
412 int attr_norm = highlight_attr[HLF_PNI];
413 int attr_select = highlight_attr[HLF_PSI];
414 int attr_scroll = highlight_attr[HLF_PSB];
415 int attr_thumb = highlight_attr[HLF_PST];
416 int attr;
417 int i;
418 int idx;
419 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000420 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000421 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000422 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100423 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000424 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000425 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000426
Bram Moolenaarae654382019-01-17 21:09:05 +0100427 if (call_update_screen)
428 {
429 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100430 // Do not redraw in pum_may_redraw() and don't draw in the area where
431 // the popup menu will be.
432 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100433 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100434 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100435 }
436
437 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100438 if (pum_first > pum_size - pum_height)
439 pum_first = pum_size - pum_height;
440
Bram Moolenaar4b779472005-10-04 09:12:31 +0000441 if (pum_scrollbar)
442 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100443 thumb_height = pum_height * pum_height / pum_size;
444 if (thumb_height == 0)
445 thumb_height = 1;
446 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000447 + (pum_size - pum_height) / 2)
448 / (pum_size - pum_height);
449 }
450
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100451#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200452 // The popup menu is drawn over popup menus with zindex under
453 // POPUPMENU_ZINDEX.
454 screen_zindex = POPUPMENU_ZINDEX;
455#endif
456
Bram Moolenaar4b779472005-10-04 09:12:31 +0000457 for (i = 0; i < pum_height; ++i)
458 {
459 idx = i + pum_first;
460 attr = (idx == pum_selected) ? attr_select : attr_norm;
461
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100462 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000463#ifdef FEAT_RIGHTLEFT
464 if (curwin->w_p_rl)
465 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200466 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000467 screen_putchar(' ', row, pum_col + 1, attr);
468 }
469 else
470#endif
471 if (pum_col > 0)
472 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000473
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100474 // Display each entry, use two spaces for a Tab.
475 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000476 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000477 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000478 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000479 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000480 width = 0;
481 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000482 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000483 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000484 case 1: p = pum_array[idx].pum_text; break;
485 case 2: p = pum_array[idx].pum_kind; break;
486 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000487 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000488 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100489 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000490 {
491 if (s == NULL)
492 s = p;
493 w = ptr2cells(p);
494 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
495 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100496 // Display the text that fits or comes before a Tab.
497 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000498 char_u *st;
499 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000500
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100501 if (saved != NUL)
502 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000503 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100504 if (saved != NUL)
505 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000506#ifdef FEAT_RIGHTLEFT
507 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000508 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000509 if (st != NULL)
510 {
511 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000512
513 if (rt != NULL)
514 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100515 char_u *rt_start = rt;
516 int size;
517
518 size = vim_strsize(rt);
519 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000520 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100521 do
522 {
523 size -= has_mbyte
524 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100525 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100526 } while (size > pum_width);
527
528 if (size < pum_width)
529 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100530 // Most left character requires
531 // 2-cells but only 1 cell is
532 // available on screen. Put a
533 // '<' on the left of the pum
534 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100535 *(--rt) = '<';
536 size++;
537 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000538 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100539 screen_puts_len(rt, (int)STRLEN(rt),
540 row, col - size + 1, attr);
541 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000542 }
543 vim_free(st);
544 }
545 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000546 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000547 else
548#endif
549 {
550 if (st != NULL)
551 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100552 int size = (int)STRLEN(st);
553 int cells = (*mb_string2cells)(st, size);
554
555 // only draw the text that fits
556 while (size > 0
557 && col + cells > pum_width + pum_col)
558 {
559 --size;
560 if (has_mbyte)
561 {
562 size -= (*mb_head_off)(st, st + size);
563 cells -= (*mb_ptr2cells)(st + size);
564 }
565 else
566 --cells;
567 }
568 screen_puts_len(st, size, row, col, attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000569 vim_free(st);
570 }
571 col += width;
572 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000573
574 if (*p != TAB)
575 break;
576
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100577 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000578#ifdef FEAT_RIGHTLEFT
579 if (curwin->w_p_rl)
580 {
581 screen_puts_len((char_u *)" ", 2, row, col - 1,
582 attr);
583 col -= 2;
584 }
585 else
586#endif
587 {
588 screen_puts_len((char_u *)" ", 2, row, col, attr);
589 col += 2;
590 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000591 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100592 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000593 width = 0;
594 }
595 else
596 width += w;
597 }
598
599 if (round > 1)
600 n = pum_kind_width + 1;
601 else
602 n = 1;
603
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100604 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000605 if (round == 3
606 || (round == 2 && pum_array[idx].pum_extra == NULL)
607 || (round == 1 && pum_array[idx].pum_kind == NULL
608 && pum_array[idx].pum_extra == NULL)
609 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000610 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000611#ifdef FEAT_RIGHTLEFT
612 if (curwin->w_p_rl)
613 {
614 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
615 col + 1, ' ', ' ', attr);
616 col = pum_col - pum_base_width - n + 1;
617 }
618 else
619#endif
620 {
621 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000622 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000623 col = pum_col + pum_base_width + n;
624 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000625 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000626 }
627
Bram Moolenaarabc97732007-08-08 20:49:37 +0000628#ifdef FEAT_RIGHTLEFT
629 if (curwin->w_p_rl)
630 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
631 ' ', attr);
632 else
633#endif
634 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
635 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000636 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000637 {
638#ifdef FEAT_RIGHTLEFT
639 if (curwin->w_p_rl)
640 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100641 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000642 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000643 else
644#endif
645 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100646 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000647 ? attr_thumb : attr_scroll);
648 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000649
650 ++row;
651 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200652
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100653#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200654 screen_zindex = 0;
655#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000656}
657
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100658#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200659/*
660 * Position the info popup relative to the popup menu item.
661 */
662 void
663pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200664{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100665 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200666 int row = pum_row;
667 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200668 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200669
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200670 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200671 if (Columns - col < 20 && Columns - col < pum_col)
672 {
673 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200674 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200675 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200676 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200677 }
678 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200679 wp->w_maxwidth = Columns - col + 1;
680 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200681 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
682 {
683 // option value overrules computed value
684 wp->w_maxwidth = wp->w_maxwidth_opt;
685 used_maxwidth_opt = TRUE;
686 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200687
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200688 row -= popup_top_extra(wp);
689 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200690 {
691 if (pum_row < pum_win_row)
692 {
693 // menu above cursor line, align with bottom
694 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200695 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200696 }
697 else
698 // menu below cursor line, align with top
699 row += 1;
700 }
701 else
702 // align with the selected item
703 row += pum_selected - pum_first + 1;
704
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100705 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200706 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100707 // The popup is not going to fit or will overlap with the cursor
708 // position, hide the popup.
709 wp->w_popup_flags |= POPF_HIDDEN;
710 else
711 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200712}
713#endif
714
Bram Moolenaar4b779472005-10-04 09:12:31 +0000715/*
716 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000717 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000718 * This may be repeated when the preview window is used:
719 * "repeat" == 0: open preview window normally
720 * "repeat" == 1: open preview window but don't set the size
721 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000722 * Returns TRUE when the window was resized and the location of the popup menu
723 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000724 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000725 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200726pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000727{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000728 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000729 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200730#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200731 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200732#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100733#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200734 int has_info = FALSE;
735#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000736
Bram Moolenaar4b779472005-10-04 09:12:31 +0000737 pum_selected = n;
738
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000739 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000740 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000741 if (pum_first > pum_selected - 4)
742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100743 // scroll down; when we did a jump it's probably a PageUp then
744 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000745 if (pum_first > pum_selected - 2)
746 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000747 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000748 if (pum_first < 0)
749 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000750 else if (pum_first > pum_selected)
751 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000752 }
753 else
754 pum_first = pum_selected;
755 }
756 else if (pum_first < pum_selected - pum_height + 5)
757 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100758 // scroll up; when we did a jump it's probably a PageDown then
759 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000760 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000761 {
762 pum_first += pum_height - 2;
763 if (pum_first < pum_selected - pum_height + 1)
764 pum_first = pum_selected - pum_height + 1;
765 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000766 else
767 pum_first = pum_selected - pum_height + 1;
768 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000769
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100770 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000771 if (context > 3)
772 context = 3;
773 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000774 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000775 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000776 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100777 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000778 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000779 if (pum_first < 0)
780 pum_first = 0;
781 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000782 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000783 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100784 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000785 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000786 }
787 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200788 // adjust for the number of lines displayed
789 if (pum_first > pum_size - pum_height)
790 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000791
Bram Moolenaar4033c552017-09-16 20:54:51 +0200792#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000793 /*
794 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100795 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000796 * Skip this when tried twice already.
797 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000798 * NOTE: Be very careful not to sync undo!
799 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000800 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000801 && Rows > 10
802 && repeat <= 1
803 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000804 {
805 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200806 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000807 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100808# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200809 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200810# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100811# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200812# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100813# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200814 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200815 if (strstr((char *)p_cot, "popuphidden") != NULL)
816 use_popup = USEPOPUP_HIDDEN;
817 else if (strstr((char *)p_cot, "popup") != NULL)
818 use_popup = USEPOPUP_NORMAL;
819 else
820 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100821 if (use_popup != USEPOPUP_NONE)
822 // don't use WinEnter or WinLeave autocommands for the info
823 // popup
824 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200825# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200826 // Open a preview window and set "curwin" to it.
827 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000828 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200829 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
830 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200831 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200832 // Prevent undo sync here, if an autocommand syncs undo weird
833 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100834 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200835 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100836 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200837 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000838 g_do_tagpreview = 0;
839
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200840 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100841# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200842 || (curwin->w_popup_flags & POPF_INFO)
843# endif
844 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000845 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200846 if (!resized
847 && curbuf->b_nwindows == 1
848 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200849 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000850 && curbuf->b_p_bh[0] == 'w')
851 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200852 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100853 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200854 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000855 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000856 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000857 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200858 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000859 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000860 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000861 --no_u_sync;
862 if (res == OK)
863 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200864 // Edit a new, empty buffer. Set options for a "wipeout"
865 // buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000866 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
Bram Moolenaard356fc62020-12-09 18:13:44 +0100867 set_option_value((char_u *)"bl", 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000868 set_option_value((char_u *)"bt", 0L,
869 (char_u *)"nofile", OPT_LOCAL);
870 set_option_value((char_u *)"bh", 0L,
871 (char_u *)"wipe", OPT_LOCAL);
872 set_option_value((char_u *)"diff", 0L,
Bram Moolenaar7f514742007-06-28 19:33:43 +0000873 NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000874 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000875 }
876 if (res == OK)
877 {
878 char_u *p, *e;
879 linenr_T lnum = 0;
880
881 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
882 {
883 e = vim_strchr(p, '\n');
884 if (e == NULL)
885 {
886 ml_append(lnum++, p, 0, FALSE);
887 break;
888 }
889 else
890 {
891 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000892 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000893 *e = '\n';
894 p = e + 1;
895 }
896 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200897 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +0200898 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000899
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100900 // Increase the height of the preview window to show the
901 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200902 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000903 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000904 if (lnum > p_pvh)
905 lnum = p_pvh;
906 if (curwin->w_height < lnum)
907 {
908 win_setheight((int)lnum);
909 resized = TRUE;
910 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000911 }
912
913 curbuf->b_changed = 0;
914 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200915 if (pum_selected != prev_selected)
916 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100917# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200918 curwin->w_firstline = 1;
919# endif
920 curwin->w_topline = 1;
921 }
922 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
923 curwin->w_topline = curbuf->b_ml.ml_line_count;
924 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000925 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100926# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200927 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200928 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200929 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200930 if (win_valid(curwin_save))
931 redraw_win_later(curwin_save, SOME_VALID);
932 }
933# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200934 if ((curwin != curwin_save && win_valid(curwin_save))
935 || (curtab != curtab_save
936 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000937 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200938 int save_redr_status;
939
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200940 if (curtab != curtab_save && valid_tabpage(curtab_save))
941 goto_tabpage_tp(curtab_save, FALSE, FALSE);
942
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100943 // When the first completion is done and the preview
944 // window is not resized, skip the preview window's
945 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200946 if (ins_compl_active() && !resized)
947 curwin->w_redr_status = FALSE;
948
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100949 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000950 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000951 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000952
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100953 // When the preview window was resized we need to
954 // update the view on the buffer. Only go back to
955 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100956 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200957 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000958 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100959 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000960 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100961 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000962 update_topline();
963 }
964
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100965 // Update the screen before drawing the popup menu.
966 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100967 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200968
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100969 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100970 // it causes flicker. When resizing we need to draw
971 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200972 // Also do not redraw the status line of the original
973 // current window here, to avoid it gets drawn with
974 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100975 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200976 save_redr_status = curwin_save->w_redr_status;
977 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000978 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100979 pum_pretend_not_visible = FALSE;
980 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200981 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000982
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000983 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100984 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100985# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200986 win_T *wp = curwin;
987# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100988 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000989 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100990 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100991# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200992 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
993 popup_hide(wp);
994# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +0100995 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000996
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100997 // May need to update the screen again when there are
998 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100999 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001000 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001001 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001002 pum_pretend_not_visible = FALSE;
1003 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001004 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001005 }
1006 }
1007 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001008# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001009 if (WIN_IS_POPUP(curwin))
1010 // can't keep focus in a popup window
1011 win_enter(firstwin, TRUE);
1012# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001013# ifdef FEAT_PROP_POPUP
1014 if (use_popup != USEPOPUP_NONE)
1015 unblock_autocmds();
1016# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001017 }
1018#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001019 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001020#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001021 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001022 // hide any popup info window
1023 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001024#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001025
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001026 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001027}
1028
1029/*
1030 * Undisplay the popup menu (later).
1031 */
1032 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001034{
1035 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +02001036 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001037 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001038 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001039#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001040 // hide any popup info window
1041 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001042#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001043}
1044
1045/*
1046 * Clear the popup menu. Currently only resets the offset to the first
1047 * displayed item.
1048 */
1049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001050pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001051{
1052 pum_first = 0;
1053}
1054
1055/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001056 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1057 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1058 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001059 */
1060 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001061pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001062{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001063 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001064}
1065
Bram Moolenaare3226be2005-12-18 22:10:00 +00001066/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001067 * Return TRUE if the popup can be redrawn in the same position.
1068 */
1069 static int
1070pum_in_same_position(void)
1071{
1072 return pum_window != curwin
1073 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1074 && pum_win_height == curwin->w_height
1075 && pum_win_col == curwin->w_wincol
1076 && pum_win_width == curwin->w_width);
1077}
1078
1079/*
1080 * Return TRUE when pum_may_redraw() will call pum_redraw().
1081 * This means that the pum area should not be overwritten to avoid flicker.
1082 */
1083 int
1084pum_redraw_in_same_position(void)
1085{
1086 if (!pum_visible() || pum_will_redraw)
1087 return FALSE; // nothing to do
1088
1089 return pum_in_same_position();
1090}
1091
1092/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001093 * Reposition the popup menu to adjust for window layout changes.
1094 */
1095 void
1096pum_may_redraw(void)
1097{
1098 pumitem_T *array = pum_array;
1099 int len = pum_size;
1100 int selected = pum_selected;
1101
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001102 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001103 return; // nothing to do
1104
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001105 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001106 {
1107 // window position didn't change, redraw in the same position
1108 pum_redraw();
1109 }
1110 else
1111 {
1112 int wcol = curwin->w_wcol;
1113
1114 // Window layout changed, recompute the position.
1115 // Use the remembered w_wcol value, the cursor may have moved when a
1116 // completion was inserted, but we want the menu in the same position.
1117 pum_undisplay();
1118 curwin->w_wcol = pum_win_wcol;
1119 curwin->w_valid |= VALID_WCOL;
1120 pum_display(array, len, selected);
1121 curwin->w_wcol = wcol;
1122 }
1123}
1124
1125/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001126 * Return the height of the popup menu, the number of entries visible.
1127 * Only valid when pum_visible() returns TRUE!
1128 */
1129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001130pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001131{
1132 return pum_height;
1133}
1134
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001135#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001136/*
1137 * Add size information about the pum to "dict".
1138 */
1139 void
1140pum_set_event_info(dict_T *dict)
1141{
1142 if (!pum_visible())
1143 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001144 (void)dict_add_number(dict, "height", pum_height);
1145 (void)dict_add_number(dict, "width", pum_width);
1146 (void)dict_add_number(dict, "row", pum_row);
1147 (void)dict_add_number(dict, "col", pum_col);
1148 (void)dict_add_number(dict, "size", pum_size);
1149 (void)dict_add_bool(dict, "scrollbar",
1150 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001151}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001152#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001153
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001154#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001155 static void
1156pum_position_at_mouse(int min_width)
1157{
1158 if (Rows - mouse_row > pum_size)
1159 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001160 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001161 pum_row = mouse_row + 1;
1162 if (pum_height > Rows - pum_row)
1163 pum_height = Rows - pum_row;
1164 }
1165 else
1166 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001167 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001168 pum_row = mouse_row - pum_size;
1169 if (pum_row < 0)
1170 {
1171 pum_height += pum_row;
1172 pum_row = 0;
1173 }
1174 }
1175 if (Columns - mouse_col >= pum_base_width
1176 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001177 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001178 pum_col = mouse_col;
1179 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001180 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001181 pum_col = Columns - (pum_base_width > min_width
1182 ? min_width : pum_base_width);
1183
1184 pum_width = Columns - pum_col;
1185 if (pum_width > pum_base_width + 1)
1186 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001187
1188 // Do not redraw at cursor position.
1189 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001190}
1191
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001192#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001193
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001194#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001195static pumitem_T *balloon_array = NULL;
1196static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001197
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001198# define BALLOON_MIN_WIDTH 50
1199# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001200
Bram Moolenaar246fe032017-11-19 19:56:27 +01001201typedef struct {
1202 char_u *start;
1203 int bytelen;
1204 int cells;
1205 int indent;
1206} balpart_T;
1207
1208/*
1209 * Split a string into parts to display in the balloon.
1210 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1211 * strings and make a struct look good.
1212 * Resulting array is stored in "array" and returns the size of the array.
1213 */
1214 int
1215split_message(char_u *mesg, pumitem_T **array)
1216{
1217 garray_T ga;
1218 char_u *p;
1219 balpart_T *item;
1220 int quoted = FALSE;
1221 int height;
1222 int line;
1223 int item_idx;
1224 int indent = 0;
1225 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001226 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001227 int long_item_count = 0;
1228 int split_long_items = FALSE;
1229
1230 ga_init2(&ga, sizeof(balpart_T), 20);
1231 p = mesg;
1232
1233 while (*p != NUL)
1234 {
1235 if (ga_grow(&ga, 1) == FAIL)
1236 goto failed;
1237 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1238 item->start = p;
1239 item->indent = indent;
1240 item->cells = indent * 2;
1241 ++ga.ga_len;
1242 while (*p != NUL)
1243 {
1244 if (*p == '"')
1245 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001246 else if (*p == '\n')
1247 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001248 else if (*p == '\\' && p[1] != NUL)
1249 ++p;
1250 else if (!quoted)
1251 {
1252 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1253 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001254 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001255 if (*p == '{')
1256 ++indent;
1257 else if (*p == '}' && indent > 0)
1258 --indent;
1259 ++item->cells;
1260 p = skipwhite(p + 1);
1261 break;
1262 }
1263 }
1264 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001265 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001266 }
1267 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001268 if (*p == '\n')
1269 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001270 if (item->cells > max_cells)
1271 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001272 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001273 }
1274
1275 height = 2 + ga.ga_len;
1276
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001277 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001278 if (long_item_count > 0 && height + long_item_count <= max_height)
1279 {
1280 split_long_items = TRUE;
1281 height += long_item_count;
1282 }
1283
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001284 // Limit to half the window height, it has to fit above or below the mouse
1285 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001286 if (height > max_height)
1287 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001288 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001289 if (*array == NULL)
1290 goto failed;
1291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001292 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001293 (*array)->pum_text = vim_strsave((char_u *)"");
1294 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1295
1296 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1297 {
1298 int skip;
1299 int thislen;
1300 int copylen;
1301 int ind;
1302 int cells;
1303
1304 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001305 if (item->bytelen == 0)
1306 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1307 else
1308 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001309 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001310 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1311 {
1312 cells = item->indent * 2;
1313 for (p = item->start + skip;
1314 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001315 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001316 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1317 break;
1318 thislen = p - (item->start + skip);
1319 }
1320 else
1321 thislen = item->bytelen;
1322
1323 // put indent at the start
1324 p = alloc(thislen + item->indent * 2 + 1);
1325 if (p == NULL)
1326 {
1327 for (line = 0; line <= height - 1; ++line)
1328 vim_free((*array)[line].pum_text);
1329 vim_free(*array);
1330 goto failed;
1331 }
1332 for (ind = 0; ind < item->indent * 2; ++ind)
1333 p[ind] = ' ';
1334
1335 // exclude spaces at the end of the string
1336 for (copylen = thislen; copylen > 0; --copylen)
1337 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001338 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001339
1340 vim_strncpy(p + ind, item->start + skip, copylen);
1341 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001342 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001343 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001344 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001345 }
1346 ga_clear(&ga);
1347 return height;
1348
1349failed:
1350 ga_clear(&ga);
1351 return 0;
1352}
1353
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001354 void
1355ui_remove_balloon(void)
1356{
1357 if (balloon_array != NULL)
1358 {
1359 pum_undisplay();
1360 while (balloon_arraysize > 0)
1361 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001362 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001363 }
1364}
1365
1366/*
1367 * Terminal version of a balloon, uses the popup menu code.
1368 */
1369 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001370ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001371{
1372 ui_remove_balloon();
1373
Bram Moolenaar246fe032017-11-19 19:56:27 +01001374 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001375 {
1376 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001377 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001378 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001379 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001380 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001381 listitem_T *li;
1382 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001383
Bram Moolenaar246fe032017-11-19 19:56:27 +01001384 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001385 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001386 if (balloon_array == NULL)
1387 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001388 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001389 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001392
1393 balloon_array[idx].pum_text = vim_strsave(
1394 text == NULL ? (char_u *)"" : text);
1395 }
1396 }
1397 else
1398 balloon_arraysize = split_message(mesg, &balloon_array);
1399
1400 if (balloon_arraysize > 0)
1401 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001402 pum_array = balloon_array;
1403 pum_size = balloon_arraysize;
1404 pum_compute_size();
1405 pum_scrollbar = 0;
1406 pum_height = balloon_arraysize;
1407
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001408 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001409 pum_selected = -1;
1410 pum_first = 0;
1411 pum_redraw();
1412 }
1413}
1414
1415/*
1416 * Called when the mouse moved, may remove any displayed balloon.
1417 */
1418 void
1419ui_may_remove_balloon(void)
1420{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001421 // For now: remove the balloon whenever the mouse moves to another screen
1422 // cell.
1423 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001424}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001425#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001426
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001427#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001428/*
1429 * Select the pum entry at the mouse position.
1430 */
1431 static void
1432pum_select_mouse_pos(void)
1433{
1434 int idx = mouse_row - pum_row;
1435
1436 if (idx < 0 || idx >= pum_size)
1437 pum_selected = -1;
1438 else if (*pum_array[idx].pum_text != NUL)
1439 pum_selected = idx;
1440}
1441
1442/*
1443 * Execute the currently selected popup menu item.
1444 */
1445 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001446pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001447{
1448 vimmenu_T *mp;
1449 int idx = 0;
1450 exarg_T ea;
1451
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001452 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001453 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001454 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001455 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001456 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001457 break;
1458 }
1459}
1460
1461/*
1462 * Open the terminal version of the popup menu and don't return until it is
1463 * closed.
1464 */
1465 void
1466pum_show_popupmenu(vimmenu_T *menu)
1467{
1468 vimmenu_T *mp;
1469 int idx = 0;
1470 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001471# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001472 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001473# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001474 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001475
1476 pum_undisplay();
1477 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001478 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001479
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001480 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001481 if (menu_is_separator(mp->dname)
1482 || (mp->modes & mp->enabled & mode))
1483 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001484
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001485 // When there are only Terminal mode menus, using "popup Edit" results in
1486 // pum_size being zero.
1487 if (pum_size <= 0)
1488 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001489 emsg(e_menu_only_exists_in_another_mode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001490 return;
1491 }
1492
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001493 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001494 if (array == NULL)
1495 return;
1496
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001497 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001498 {
1499 char_u *s = NULL;
1500
1501 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001502 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001503 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001504 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001505 s = mp->dname;
1506 if (s != NULL)
1507 {
1508 s = vim_strsave(s);
1509 if (s != NULL)
1510 array[idx++].pum_text = s;
1511 }
1512 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001513
1514 pum_array = array;
1515 pum_compute_size();
1516 pum_scrollbar = 0;
1517 pum_height = pum_size;
1518 pum_position_at_mouse(20);
1519
1520 pum_selected = -1;
1521 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001522# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001523 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001524 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001525# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001526
1527 for (;;)
1528 {
1529 int c;
1530
1531 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001532 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001533 out_flush();
1534
1535 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001536
1537 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1538 // menu.
1539 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001540 break;
1541 else if (c == CAR || c == NL)
1542 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001543 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001544 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001545 break;
1546 }
1547 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1548 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001549 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001550 while (pum_selected > 0)
1551 {
1552 --pum_selected;
1553 if (*array[pum_selected].pum_text != NUL)
1554 break;
1555 }
1556 }
1557 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1558 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001559 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001560 while (pum_selected < pum_size - 1)
1561 {
1562 ++pum_selected;
1563 if (*array[pum_selected].pum_text != NUL)
1564 break;
1565 }
1566 }
1567 else if (c == K_RIGHTMOUSE)
1568 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001569 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001570 vungetc(c);
1571 break;
1572 }
1573 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1574 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001575 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001576 pum_select_mouse_pos();
1577 }
1578 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1579 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001580 // left mouse click: select clicked item, if any, and close;
1581 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001582 pum_select_mouse_pos();
1583 if (pum_selected >= 0)
1584 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001585 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001586 break;
1587 }
1588 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1589 break;
1590 }
1591 }
1592
Bram Moolenaar38455a92020-12-24 18:39:02 +01001593 for (idx = 0; idx < pum_size; ++idx)
1594 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001595 vim_free(array);
1596 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001597# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001598 p_bevalterm = save_bevalterm;
1599 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001600# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001601}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001602
1603 void
1604pum_make_popup(char_u *path_name, int use_mouse_pos)
1605{
1606 vimmenu_T *menu;
1607
1608 if (!use_mouse_pos)
1609 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001610 // Hack: set mouse position at the cursor so that the menu pops up
1611 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001612 mouse_row = curwin->w_winrow + curwin->w_wrow;
1613 mouse_col = curwin->w_wincol + curwin->w_wcol;
1614 }
1615
1616 menu = gui_find_menu(path_name);
1617 if (menu != NULL)
1618 pum_show_popupmenu(menu);
1619}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001620#endif