blob: 7fb29d63580d1cac56d5685c2db6e5a4dd5ee3f2 [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;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +010021static int pum_in_cmdline = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +010022
Bram Moolenaar63d9e732019-12-05 21:10:38 +010023static int pum_height; // nr of displayed pum items
24static int pum_width; // width of displayed pum items
25static int pum_base_width; // width of pum items base
26static int pum_kind_width; // width of pum items kind column
27static int pum_extra_width; // width of extra stuff
28static int pum_scrollbar; // TRUE when scrollbar present
Bram Moolenaar4b779472005-10-04 09:12:31 +000029
Bram Moolenaar63d9e732019-12-05 21:10:38 +010030static int pum_row; // top row of pum
31static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000032
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020033static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020034static int pum_win_row;
35static int pum_win_height;
36static int pum_win_col;
37static int pum_win_wcol;
38static int pum_win_width;
39
Bram Moolenaar04357fb2019-12-10 21:50:56 +010040// Some parts are not updated when a popup menu is visible. Setting this flag
41// makes pum_visible() return FALSE even when there is a popup menu.
42static int pum_pretend_not_visible = FALSE;
43
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000045
Bram Moolenaar4b779472005-10-04 09:12:31 +000046#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000047
Bram Moolenaar51b0f372017-11-18 18:52:04 +010048 static void
49pum_compute_size(void)
50{
51 int i;
52 int w;
53
Bram Moolenaar63d9e732019-12-05 21:10:38 +010054 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010055 pum_base_width = 0;
56 pum_kind_width = 0;
57 pum_extra_width = 0;
58 for (i = 0; i < pum_size; ++i)
59 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020060 if (pum_array[i].pum_text != NULL)
61 {
62 w = vim_strsize(pum_array[i].pum_text);
63 if (pum_base_width < w)
64 pum_base_width = w;
65 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010066 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
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100104#ifdef FEAT_RIGHTLEFT
Bram Moolenaar24959102022-05-07 20:01:16 +0100105 int right_left = State == MODE_CMDLINE ? FALSE : curwin->w_p_rl;
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100106#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000107
Bram Moolenaara5e66212017-09-29 22:42:33 +0200108 do
109 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100110 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200111 above_row = 0;
112 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000113
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100114 // Pretend the pum is already there to avoid that must_redraw is set
115 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200116 pum_array = (pumitem_T *)1;
117 validate_cursor_col();
118 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000119
Bram Moolenaar491ac282018-06-17 14:47:55 +0200120 // Remember the essential parts of the window position and size, so we
121 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200122 pum_window = curwin;
Bram Moolenaar24959102022-05-07 20:01:16 +0100123 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000124 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000125 pum_win_row = cmdline_row;
126 else
127 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200128 pum_win_height = curwin->w_height;
129 pum_win_col = curwin->w_wincol;
130 pum_win_wcol = curwin->w_wcol;
131 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000132
Bram Moolenaar4033c552017-09-16 20:54:51 +0200133#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200134 FOR_ALL_WINDOWS(pvwin)
135 if (pvwin->w_p_pvw)
136 break;
137 if (pvwin != NULL)
138 {
139 if (W_WINROW(pvwin) < W_WINROW(curwin))
140 above_row = W_WINROW(pvwin) + pvwin->w_height;
141 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
142 below_row = W_WINROW(pvwin);
143 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100144#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000145
Bram Moolenaara5e66212017-09-29 22:42:33 +0200146 /*
147 * Figure out the size and position of the pum.
148 */
149 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000150 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000151 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200152 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000153 if (p_ph > 0 && pum_height > p_ph)
154 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000155
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100156 // Put the pum below "pum_win_row" if possible. If there are few lines
157 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200158 if (pum_win_row + 2 >= below_row - pum_height
159 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200160 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100161 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200162
Bram Moolenaar24959102022-05-07 20:01:16 +0100163 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100164 // for cmdline pum, no need for context lines
165 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100167 {
168 // Leave two lines of context if possible
169 if (curwin->w_wrow - curwin->w_cline_row >= 2)
170 context_lines = 2;
171 else
172 context_lines = curwin->w_wrow - curwin->w_cline_row;
173 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200174
Bram Moolenaar491ac282018-06-17 14:47:55 +0200175 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200176 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200177 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200178 pum_height = size;
179 }
180 else
181 {
182 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200183 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184 }
185 if (p_ph > 0 && pum_height > p_ph)
186 {
187 pum_row += pum_height - p_ph;
188 pum_height = p_ph;
189 }
190 }
191 else
192 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100193 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200194
Bram Moolenaar24959102022-05-07 20:01:16 +0100195 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100196 // for cmdline pum, no need for context lines
197 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200198 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100199 {
200 // Leave two lines of context if possible
201 validate_cheight();
202 if (curwin->w_cline_row
203 + curwin->w_cline_height - curwin->w_wrow >= 3)
204 context_lines = 3;
205 else
206 context_lines = curwin->w_cline_row
207 + curwin->w_cline_height - curwin->w_wrow;
208 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200209
Bram Moolenaar491ac282018-06-17 14:47:55 +0200210 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200211 if (size > below_row - pum_row)
212 pum_height = below_row - pum_row;
213 else
214 pum_height = size;
215 if (p_ph > 0 && pum_height > p_ph)
216 pum_height = p_ph;
217 }
218
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100219 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200220 if (pum_height < 1 || (pum_height == 1 && size > 1))
221 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000222
Bram Moolenaar4033c552017-09-16 20:54:51 +0200223#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100224 // If there is a preview window above avoid drawing over it.
225 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000226 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100227 pum_row = above_row;
228 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000229 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200230#endif
231
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100232 pum_array = array;
233 pum_size = size;
234 pum_compute_size();
235 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000236
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100237 // Calculate column
Bram Moolenaar24959102022-05-07 20:01:16 +0100238 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000239 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000240 cursor_col = cmdline_compl_startcol();
241 else
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100242 {
243 // w_wcol includes virtual text "above"
244 int wcol = curwin->w_wcol % curwin->w_width;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000245#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100246 if (right_left)
247 cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
248 else
Bram Moolenaarabc97732007-08-08 20:49:37 +0000249#endif
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100250 cursor_col = curwin->w_wincol + wcol;
251 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000252
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100253 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200254 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000255 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200256 pum_scrollbar = 1;
257 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000258 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000259 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200260 pum_scrollbar = 0;
261
262 if (def_width < max_width)
263 def_width = max_width;
264
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100265 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000266#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100267 && !right_left)
268 || (right_left && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000269#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200270 ))
271 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100272 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100273 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000274
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100275 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200276#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100277 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200278 pum_width = pum_col - pum_scrollbar + 1;
279 else
280#endif
281 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000282
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100283 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100284 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200285 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100286 // the width is more than needed for the items, make it
287 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100288 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100289 if (pum_width < p_pw)
290 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200291 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100292 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100293#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100294 && !right_left)
295 || (right_left && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100296 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100297#endif
298 ))
299 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100300 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100301#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100302 if (right_left
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100303 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100305 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100306 if (pum_col >= Columns)
307 pum_col = Columns - 1;
308 }
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100309 else if (!right_left)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100310#endif
311 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100312 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
313 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100314 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100315 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100316 pum_col = Columns - max_width - pum_scrollbar;
317 if (pum_col < 0)
318 pum_col = 0;
319 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100320 }
321
322#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100323 if (right_left)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100324 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100325 else
326#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100327 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100328
Bram Moolenaar42443c72018-02-10 18:28:52 +0100329 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100330 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100331 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100332#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100333 if (right_left)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100334 {
335 if (pum_width > pum_col)
336 pum_width = pum_col;
337 }
338 else
339#endif
340 {
341 if (pum_width >= Columns - pum_col)
342 pum_width = Columns - pum_col - 1;
343 }
344 }
345 else if (pum_width > max_width + pum_kind_width
346 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100347 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100348 {
349 pum_width = max_width + pum_kind_width
350 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100351 if (pum_width < p_pw)
352 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100353 }
354 }
355
Bram Moolenaara5e66212017-09-29 22:42:33 +0200356 }
357 else if (Columns < def_width)
358 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100359 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200360#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100361 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200362 pum_col = Columns - 1;
363 else
364#endif
365 pum_col = 0;
366 pum_width = Columns - 1;
367 }
368 else
369 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100370 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100371 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200372#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100373 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200374 pum_col = max_width - 1;
375 else
376#endif
377 pum_col = Columns - max_width;
378 pum_width = max_width - pum_scrollbar;
379 }
380
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100381 // Set selected item and redraw. If the window size changed need to
382 // redo the positioning. Limit this to two times, when there is not
383 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200384 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100385
386 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000387}
388
389/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100390 * Set a flag that when pum_redraw() is called it first calls update_screen().
391 * This will avoid clearing and redrawing the popup menu, prevent flicker.
392 */
393 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000394pum_call_update_screen(void)
Bram Moolenaarae654382019-01-17 21:09:05 +0100395{
396 call_update_screen = TRUE;
397
398 // Update the cursor position to be able to compute the popup menu
399 // position. The cursor line length may have changed because of the
400 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100401 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100402 validate_cursor();
403}
404
405/*
406 * Return TRUE if we are going to redraw the popup menu and the screen position
407 * "row"/"col" is under the popup menu.
408 */
409 int
Bakudankun65555002021-11-17 20:40:16 +0000410pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100411{
Bakudankun65555002021-11-17 20:40:16 +0000412 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100413 && row >= pum_row
414 && row < pum_row + pum_height
415 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200416 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100417}
418
419/*
glepnir40c1c332024-06-11 19:37:04 +0200420 * displays text on the popup menu with specific attributes.
421 */
422 static void
zeertzjqafbe5352024-06-14 20:24:22 +0200423pum_screen_put_with_attr(int row, int col, char_u *text, int textlen, hlf_T hlf)
glepnir40c1c332024-06-11 19:37:04 +0200424{
425 int i;
426 int leader_len;
427 int char_len;
428 int cells;
429 int new_attr;
430 char_u *rt_leader = NULL;
431 char_u *match_leader = NULL;
432 char_u *ptr = text;
433 garray_T *ga = NULL;
434 char_u *leader = ins_compl_leader();
435 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
436
zeertzjqafbe5352024-06-14 20:24:22 +0200437 if (leader == NULL || *leader == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
438 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
439 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
glepnir40c1c332024-06-11 19:37:04 +0200440 {
zeertzjqafbe5352024-06-14 20:24:22 +0200441 screen_puts_len(text, textlen, row, col, highlight_attr[hlf]);
442 return;
glepnir40c1c332024-06-11 19:37:04 +0200443 }
444
445#ifdef FEAT_RIGHTLEFT
glepnir1c296022024-06-13 17:21:24 +0200446 if (curwin->w_p_rl)
zeertzjqafbe5352024-06-14 20:24:22 +0200447 rt_leader = reverse_text(leader);
glepnir40c1c332024-06-11 19:37:04 +0200448#endif
449 match_leader = rt_leader != NULL ? rt_leader : leader;
glepnir1c296022024-06-13 17:21:24 +0200450 leader_len = (int)STRLEN(match_leader);
glepnir40c1c332024-06-11 19:37:04 +0200451
glepnir1c296022024-06-13 17:21:24 +0200452 if (in_fuzzy)
zeertzjqafbe5352024-06-14 20:24:22 +0200453 ga = fuzzy_match_str_with_pos(text, match_leader);
glepnir40c1c332024-06-11 19:37:04 +0200454
455 // Render text with proper attributes
456 while (*ptr != NUL && ptr < text + textlen)
457 {
zeertzjqafbe5352024-06-14 20:24:22 +0200458 char_len = mb_ptr2len(ptr);
459 cells = mb_ptr2cells(ptr);
460 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200461
zeertzjqafbe5352024-06-14 20:24:22 +0200462 if (ga != NULL)
463 {
464 // Handle fuzzy matching
465 for (i = 0; i < ga->ga_len; i++)
466 {
467 int_u *match_pos = ((int_u *)ga->ga_data) + i;
468 int_u actual_char_pos = 0;
469 char_u *temp_ptr = text;
470 while (temp_ptr < ptr)
471 {
472 temp_ptr += mb_ptr2len(temp_ptr);
473 actual_char_pos++;
474 }
475 if (actual_char_pos == match_pos[0])
476 {
477 new_attr = highlight_attr[hlf == HLF_PSI
478 ? HLF_PMSI : HLF_PMNI];
479 break;
480 }
481 }
482 }
483 else if (!in_fuzzy && (ptr - text < leader_len)
484 && (STRNCMP(text, match_leader, leader_len) == 0))
485 new_attr = highlight_attr[hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI];
glepnir40c1c332024-06-11 19:37:04 +0200486
zeertzjqafbe5352024-06-14 20:24:22 +0200487 screen_puts_len(ptr, char_len, row, col, new_attr);
488 col += cells;
489 ptr += char_len;
glepnir40c1c332024-06-11 19:37:04 +0200490 }
491
492 if (ga != NULL)
493 {
zeertzjqafbe5352024-06-14 20:24:22 +0200494 ga_clear(ga);
495 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200496 }
497 if (rt_leader)
zeertzjqafbe5352024-06-14 20:24:22 +0200498 vim_free(rt_leader);
glepnir40c1c332024-06-11 19:37:04 +0200499}
500
501/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000502 * Redraw the popup menu, using "pum_first" and "pum_selected".
503 */
504 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100505pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000506{
507 int row = pum_row;
508 int col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000509 int attr_scroll = highlight_attr[HLF_PSB];
510 int attr_thumb = highlight_attr[HLF_PST];
zeertzjqafbe5352024-06-14 20:24:22 +0200511 hlf_T *hlfs; // array used for highlights
512 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000513 int attr;
514 int i;
515 int idx;
516 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000517 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000518 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000519 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100520 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000521 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000522 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000523
zeertzjqafbe5352024-06-14 20:24:22 +0200524 hlf_T hlfsNorm[3];
525 hlf_T hlfsSel[3];
Bram Moolenaare638acc2023-03-15 17:08:51 +0000526 // "word"
zeertzjqafbe5352024-06-14 20:24:22 +0200527 hlfsNorm[0] = HLF_PNI;
528 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000529 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200530 hlfsNorm[1] = HLF_PNK;
531 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000532 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200533 hlfsNorm[2] = HLF_PNX;
534 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000535
Bram Moolenaarae654382019-01-17 21:09:05 +0100536 if (call_update_screen)
537 {
538 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100539 // Do not redraw in pum_may_redraw() and don't draw in the area where
540 // the popup menu will be.
541 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100542 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100543 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100544 }
545
546 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100547 if (pum_first > pum_size - pum_height)
548 pum_first = pum_size - pum_height;
549
Bram Moolenaar4b779472005-10-04 09:12:31 +0000550 if (pum_scrollbar)
551 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100552 thumb_height = pum_height * pum_height / pum_size;
553 if (thumb_height == 0)
554 thumb_height = 1;
555 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000556 + (pum_size - pum_height) / 2)
557 / (pum_size - pum_height);
558 }
559
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100560#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200561 // The popup menu is drawn over popup menus with zindex under
562 // POPUPMENU_ZINDEX.
563 screen_zindex = POPUPMENU_ZINDEX;
564#endif
565
Bram Moolenaar4b779472005-10-04 09:12:31 +0000566 for (i = 0; i < pum_height; ++i)
567 {
568 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200569 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
570 hlf = hlfs[0]; // start with "word" highlight
571 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000572
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100573 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000574#ifdef FEAT_RIGHTLEFT
575 if (curwin->w_p_rl)
576 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200577 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000578 screen_putchar(' ', row, pum_col + 1, attr);
579 }
580 else
581#endif
582 if (pum_col > 0)
583 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000584
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100585 // Display each entry, use two spaces for a Tab.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000586 // Do this 3 times:
587 // 0 - main text
588 // 1 - kind
589 // 2 - extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000590 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000591 totwidth = 0;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000592 for (round = 0; round < 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000593 {
zeertzjqafbe5352024-06-14 20:24:22 +0200594 hlf = hlfs[round];
595 attr = highlight_attr[hlf];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000596 width = 0;
597 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000598 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000599 {
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000600 case 0: p = pum_array[idx].pum_text; break;
601 case 1: p = pum_array[idx].pum_kind; break;
602 case 2: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000603 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000604 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100605 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000606 {
607 if (s == NULL)
608 s = p;
609 w = ptr2cells(p);
610 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
611 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100612 // Display the text that fits or comes before a Tab.
613 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000614 char_u *st;
615 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000616
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100617 if (saved != NUL)
618 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000619 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100620 if (saved != NUL)
621 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000622#ifdef FEAT_RIGHTLEFT
623 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000624 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000625 if (st != NULL)
626 {
627 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000628
629 if (rt != NULL)
630 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100631 char_u *rt_start = rt;
632 int size;
633
634 size = vim_strsize(rt);
635 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000636 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100637 do
638 {
639 size -= has_mbyte
640 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100641 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100642 } while (size > pum_width);
643
644 if (size < pum_width)
645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100646 // Most left character requires
647 // 2-cells but only 1 cell is
648 // available on screen. Put a
649 // '<' on the left of the pum
650 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100651 *(--rt) = '<';
652 size++;
653 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000654 }
zeertzjqafbe5352024-06-14 20:24:22 +0200655 pum_screen_put_with_attr(row, col - size + 1, rt, (int)STRLEN(rt), hlf);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100656 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000657 }
658 vim_free(st);
659 }
660 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000661 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000662 else
663#endif
664 {
665 if (st != NULL)
666 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100667 int size = (int)STRLEN(st);
668 int cells = (*mb_string2cells)(st, size);
669
670 // only draw the text that fits
671 while (size > 0
672 && col + cells > pum_width + pum_col)
673 {
674 --size;
675 if (has_mbyte)
676 {
677 size -= (*mb_head_off)(st, st + size);
678 cells -= (*mb_ptr2cells)(st + size);
679 }
680 else
681 --cells;
682 }
zeertzjqafbe5352024-06-14 20:24:22 +0200683 pum_screen_put_with_attr(row, col, st, size, hlf);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000684 vim_free(st);
685 }
686 col += width;
687 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000688
689 if (*p != TAB)
690 break;
691
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100692 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000693#ifdef FEAT_RIGHTLEFT
694 if (curwin->w_p_rl)
695 {
696 screen_puts_len((char_u *)" ", 2, row, col - 1,
697 attr);
698 col -= 2;
699 }
700 else
701#endif
702 {
703 screen_puts_len((char_u *)" ", 2, row, col, attr);
704 col += 2;
705 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000706 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100707 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000708 width = 0;
709 }
710 else
711 width += w;
712 }
713
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000714 if (round > 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000715 n = pum_kind_width + 1;
716 else
717 n = 1;
718
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100719 // Stop when there is nothing more to display.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000720 if (round == 2
721 || (round == 1 && pum_array[idx].pum_extra == NULL)
722 || (round == 0 && pum_array[idx].pum_kind == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000723 && pum_array[idx].pum_extra == NULL)
724 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000725 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000726#ifdef FEAT_RIGHTLEFT
727 if (curwin->w_p_rl)
728 {
729 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
730 col + 1, ' ', ' ', attr);
731 col = pum_col - pum_base_width - n + 1;
732 }
733 else
734#endif
735 {
736 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000738 col = pum_col + pum_base_width + n;
739 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000740 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000741 }
742
Bram Moolenaarabc97732007-08-08 20:49:37 +0000743#ifdef FEAT_RIGHTLEFT
744 if (curwin->w_p_rl)
745 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
746 ' ', attr);
747 else
748#endif
749 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
750 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000751 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000752 {
753#ifdef FEAT_RIGHTLEFT
754 if (curwin->w_p_rl)
755 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100756 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000757 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000758 else
759#endif
760 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100761 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000762 ? attr_thumb : attr_scroll);
763 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000764
765 ++row;
766 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200767
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100768#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200769 screen_zindex = 0;
770#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000771}
772
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100773#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200774/*
775 * Position the info popup relative to the popup menu item.
776 */
777 void
778pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200779{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100780 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200781 int row = pum_row;
782 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200783 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200784
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200785 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200786 if (Columns - col < 20 && Columns - col < pum_col)
787 {
788 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200789 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200790 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200791 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200792 }
793 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200794 wp->w_maxwidth = Columns - col + 1;
795 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200796 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
797 {
798 // option value overrules computed value
799 wp->w_maxwidth = wp->w_maxwidth_opt;
800 used_maxwidth_opt = TRUE;
801 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200802
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200803 row -= popup_top_extra(wp);
804 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200805 {
806 if (pum_row < pum_win_row)
807 {
808 // menu above cursor line, align with bottom
809 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200810 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200811 }
812 else
813 // menu below cursor line, align with top
814 row += 1;
815 }
816 else
817 // align with the selected item
818 row += pum_selected - pum_first + 1;
819
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100820 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200821 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100822 // The popup is not going to fit or will overlap with the cursor
823 // position, hide the popup.
824 wp->w_popup_flags |= POPF_HIDDEN;
825 else
826 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200827}
828#endif
829
Bram Moolenaar4b779472005-10-04 09:12:31 +0000830/*
831 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000832 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000833 * This may be repeated when the preview window is used:
834 * "repeat" == 0: open preview window normally
835 * "repeat" == 1: open preview window but don't set the size
836 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000837 * Returns TRUE when the window was resized and the location of the popup menu
838 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000839 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000840 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200841pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000842{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000843 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000844 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200845#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200846 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +0200847 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +0200848#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100849#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200850 int has_info = FALSE;
851#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000852
Bram Moolenaar4b779472005-10-04 09:12:31 +0000853 pum_selected = n;
854
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000855 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000856 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000857 if (pum_first > pum_selected - 4)
858 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100859 // scroll down; when we did a jump it's probably a PageUp then
860 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000861 if (pum_first > pum_selected - 2)
862 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000863 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000864 if (pum_first < 0)
865 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000866 else if (pum_first > pum_selected)
867 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000868 }
869 else
870 pum_first = pum_selected;
871 }
872 else if (pum_first < pum_selected - pum_height + 5)
873 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100874 // scroll up; when we did a jump it's probably a PageDown then
875 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000876 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000877 {
878 pum_first += pum_height - 2;
879 if (pum_first < pum_selected - pum_height + 1)
880 pum_first = pum_selected - pum_height + 1;
881 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000882 else
883 pum_first = pum_selected - pum_height + 1;
884 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000885
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100886 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000887 if (context > 3)
888 context = 3;
889 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000890 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000891 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000892 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100893 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000894 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000895 if (pum_first < 0)
896 pum_first = 0;
897 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000898 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000899 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100900 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000901 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000902 }
903 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200904 // adjust for the number of lines displayed
905 if (pum_first > pum_size - pum_height)
906 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000907
Bram Moolenaar4033c552017-09-16 20:54:51 +0200908#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000909 /*
910 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100911 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000912 * Skip this when tried twice already.
913 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000914 * NOTE: Be very careful not to sync undo!
915 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000916 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000917 && Rows > 10
918 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +0200919 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000920 {
921 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200922 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100923# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200924 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200925# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100926# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200927# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100928# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200929 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +0200930 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200931 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +0200932 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200933 use_popup = USEPOPUP_NORMAL;
934 else
935 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100936 if (use_popup != USEPOPUP_NONE)
937 // don't use WinEnter or WinLeave autocommands for the info
938 // popup
939 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200940# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200941 // Open a preview window and set "curwin" to it.
942 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000943 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200944 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
945 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200946 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200947 // Prevent undo sync here, if an autocommand syncs undo weird
948 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100949 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200950 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100951 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100952 if (RedrawingDisabled > 0)
953 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000954 g_do_tagpreview = 0;
955
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200956 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100957# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200958 || (curwin->w_popup_flags & POPF_INFO)
959# endif
960 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000961 {
mathew20f61d92023-08-26 18:11:02 +0200962 int res = OK;
963
Bram Moolenaar50e53762016-10-27 14:49:15 +0200964 if (!resized
965 && curbuf->b_nwindows == 1
966 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200967 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000968 && curbuf->b_p_bh[0] == 'w')
969 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200970 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100971 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200972 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000973 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000974 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000975 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200976 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000977 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000978 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000979 --no_u_sync;
980 if (res == OK)
981 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200982 // Edit a new, empty buffer. Set options for a "wipeout"
983 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100984 set_option_value_give_err((char_u *)"swf",
985 0L, NULL, OPT_LOCAL);
986 set_option_value_give_err((char_u *)"bl",
987 0L, NULL, OPT_LOCAL);
988 set_option_value_give_err((char_u *)"bt",
989 0L, (char_u *)"nofile", OPT_LOCAL);
990 set_option_value_give_err((char_u *)"bh",
991 0L, (char_u *)"wipe", OPT_LOCAL);
992 set_option_value_give_err((char_u *)"diff",
993 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000994 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000995 }
996 if (res == OK)
997 {
998 char_u *p, *e;
999 linenr_T lnum = 0;
1000
1001 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1002 {
1003 e = vim_strchr(p, '\n');
1004 if (e == NULL)
1005 {
1006 ml_append(lnum++, p, 0, FALSE);
1007 break;
1008 }
mathewc51fa7b2023-08-23 20:55:17 +02001009 *e = NUL;
1010 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1011 *e = '\n';
1012 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001013 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001014 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001015 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001016
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001017 // Increase the height of the preview window to show the
1018 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001019 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001020 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001021 if (lnum > p_pvh)
1022 lnum = p_pvh;
1023 if (curwin->w_height < lnum)
1024 {
1025 win_setheight((int)lnum);
1026 resized = TRUE;
1027 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001028 }
1029
1030 curbuf->b_changed = 0;
1031 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001032 if (pum_selected != prev_selected)
1033 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001034# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001035 curwin->w_firstline = 1;
1036# endif
1037 curwin->w_topline = 1;
1038 }
1039 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1040 curwin->w_topline = curbuf->b_ml.ml_line_count;
1041 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001042 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001043# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001044 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001045 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001046 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001047 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001048 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001049 }
1050# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001051 if ((curwin != curwin_save && win_valid(curwin_save))
1052 || (curtab != curtab_save
1053 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001054 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001055 int save_redr_status;
1056
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001057 if (curtab != curtab_save && valid_tabpage(curtab_save))
1058 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1059
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001060 // When the first completion is done and the preview
1061 // window is not resized, skip the preview window's
1062 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001063 if (ins_compl_active() && !resized)
1064 curwin->w_redr_status = FALSE;
1065
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001066 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001067 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001068 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001069
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001070 // When the preview window was resized we need to
1071 // update the view on the buffer. Only go back to
1072 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001073 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001074 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001075 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001076 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001077 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001078 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001079 update_topline();
1080 }
1081
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001082 // Update the screen before drawing the popup menu.
1083 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001084 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001085
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001086 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001087 // it causes flicker. When resizing we need to draw
1088 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001089 // Also do not redraw the status line of the original
1090 // current window here, to avoid it gets drawn with
1091 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001092 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001093 save_redr_status = curwin_save->w_redr_status;
1094 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001095 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001096 pum_pretend_not_visible = FALSE;
1097 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001098 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001099
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001100 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001101 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001102# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001103 win_T *wp = curwin;
1104# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001105 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001106 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001107 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001108# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001109 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1110 popup_hide(wp);
1111# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001112 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001113
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001114 // May need to update the screen again when there are
1115 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001116 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001117 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001118 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001119 pum_pretend_not_visible = FALSE;
1120 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001121 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001122 }
1123 }
1124 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001125# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001126 if (WIN_IS_POPUP(curwin))
1127 // can't keep focus in a popup window
1128 win_enter(firstwin, TRUE);
1129# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001130# ifdef FEAT_PROP_POPUP
1131 if (use_popup != USEPOPUP_NONE)
1132 unblock_autocmds();
1133# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001134 }
1135#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001136 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001137#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001138 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001139 // hide any popup info window
1140 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001141#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001142
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001143 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001144}
1145
1146/*
1147 * Undisplay the popup menu (later).
1148 */
1149 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001150pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001151{
1152 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001153 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001154 redraw_tabline = TRUE;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001155 if (pum_in_cmdline)
1156 {
1157 clear_cmdline = TRUE;
1158 pum_in_cmdline = FALSE;
1159 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001160 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001161#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001162 // hide any popup info window
1163 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001164#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001165}
1166
1167/*
1168 * Clear the popup menu. Currently only resets the offset to the first
1169 * displayed item.
1170 */
1171 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001172pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001173{
1174 pum_first = 0;
1175}
1176
1177/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001178 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1179 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1180 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001181 */
1182 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001183pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001184{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001185 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001186}
1187
Bram Moolenaare3226be2005-12-18 22:10:00 +00001188/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001189 * Return TRUE if the popup can be redrawn in the same position.
1190 */
1191 static int
1192pum_in_same_position(void)
1193{
1194 return pum_window != curwin
1195 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1196 && pum_win_height == curwin->w_height
1197 && pum_win_col == curwin->w_wincol
1198 && pum_win_width == curwin->w_width);
1199}
1200
1201/*
1202 * Return TRUE when pum_may_redraw() will call pum_redraw().
1203 * This means that the pum area should not be overwritten to avoid flicker.
1204 */
1205 int
1206pum_redraw_in_same_position(void)
1207{
1208 if (!pum_visible() || pum_will_redraw)
1209 return FALSE; // nothing to do
1210
1211 return pum_in_same_position();
1212}
1213
1214/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001215 * Reposition the popup menu to adjust for window layout changes.
1216 */
1217 void
1218pum_may_redraw(void)
1219{
1220 pumitem_T *array = pum_array;
1221 int len = pum_size;
1222 int selected = pum_selected;
1223
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001224 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001225 return; // nothing to do
1226
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001227 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001228 {
1229 // window position didn't change, redraw in the same position
1230 pum_redraw();
1231 }
1232 else
1233 {
1234 int wcol = curwin->w_wcol;
1235
1236 // Window layout changed, recompute the position.
1237 // Use the remembered w_wcol value, the cursor may have moved when a
1238 // completion was inserted, but we want the menu in the same position.
1239 pum_undisplay();
1240 curwin->w_wcol = pum_win_wcol;
1241 curwin->w_valid |= VALID_WCOL;
1242 pum_display(array, len, selected);
1243 curwin->w_wcol = wcol;
1244 }
1245}
1246
1247/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001248 * Return the height of the popup menu, the number of entries visible.
1249 * Only valid when pum_visible() returns TRUE!
1250 */
1251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001252pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001253{
1254 return pum_height;
1255}
1256
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001257#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001258/*
1259 * Add size information about the pum to "dict".
1260 */
1261 void
1262pum_set_event_info(dict_T *dict)
1263{
1264 if (!pum_visible())
1265 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001266 (void)dict_add_number(dict, "height", pum_height);
1267 (void)dict_add_number(dict, "width", pum_width);
1268 (void)dict_add_number(dict, "row", pum_row);
1269 (void)dict_add_number(dict, "col", pum_col);
1270 (void)dict_add_number(dict, "size", pum_size);
1271 (void)dict_add_bool(dict, "scrollbar",
1272 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001273}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001274#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001275
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001276#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001277 static void
1278pum_position_at_mouse(int min_width)
1279{
1280 if (Rows - mouse_row > pum_size)
1281 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001282 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001283 pum_row = mouse_row + 1;
1284 if (pum_height > Rows - pum_row)
1285 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001286 if (pum_row + pum_height > cmdline_row)
1287 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001288 }
1289 else
1290 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001291 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001292 pum_row = mouse_row - pum_size;
1293 if (pum_row < 0)
1294 {
1295 pum_height += pum_row;
1296 pum_row = 0;
1297 }
1298 }
1299 if (Columns - mouse_col >= pum_base_width
1300 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001301 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001302 pum_col = mouse_col;
1303 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001304 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001305 pum_col = Columns - (pum_base_width > min_width
1306 ? min_width : pum_base_width);
1307
1308 pum_width = Columns - pum_col;
1309 if (pum_width > pum_base_width + 1)
1310 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001311
1312 // Do not redraw at cursor position.
1313 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001314}
1315
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001316#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001317
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001318#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001319static pumitem_T *balloon_array = NULL;
1320static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001321
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001322# define BALLOON_MIN_WIDTH 50
1323# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001324
Bram Moolenaar246fe032017-11-19 19:56:27 +01001325typedef struct {
1326 char_u *start;
1327 int bytelen;
1328 int cells;
1329 int indent;
1330} balpart_T;
1331
1332/*
1333 * Split a string into parts to display in the balloon.
1334 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1335 * strings and make a struct look good.
1336 * Resulting array is stored in "array" and returns the size of the array.
1337 */
1338 int
1339split_message(char_u *mesg, pumitem_T **array)
1340{
1341 garray_T ga;
1342 char_u *p;
1343 balpart_T *item;
1344 int quoted = FALSE;
1345 int height;
1346 int line;
1347 int item_idx;
1348 int indent = 0;
1349 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001350 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001351 int long_item_count = 0;
1352 int split_long_items = FALSE;
1353
1354 ga_init2(&ga, sizeof(balpart_T), 20);
1355 p = mesg;
1356
1357 while (*p != NUL)
1358 {
1359 if (ga_grow(&ga, 1) == FAIL)
1360 goto failed;
1361 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1362 item->start = p;
1363 item->indent = indent;
1364 item->cells = indent * 2;
1365 ++ga.ga_len;
1366 while (*p != NUL)
1367 {
1368 if (*p == '"')
1369 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001370 else if (*p == '\n')
1371 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001372 else if (*p == '\\' && p[1] != NUL)
1373 ++p;
1374 else if (!quoted)
1375 {
1376 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1377 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001378 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001379 if (*p == '{')
1380 ++indent;
1381 else if (*p == '}' && indent > 0)
1382 --indent;
1383 ++item->cells;
1384 p = skipwhite(p + 1);
1385 break;
1386 }
1387 }
1388 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001389 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001390 }
1391 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001392 if (*p == '\n')
1393 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001394 if (item->cells > max_cells)
1395 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001396 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001397 }
1398
1399 height = 2 + ga.ga_len;
1400
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001401 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001402 if (long_item_count > 0 && height + long_item_count <= max_height)
1403 {
1404 split_long_items = TRUE;
1405 height += long_item_count;
1406 }
1407
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001408 // Limit to half the window height, it has to fit above or below the mouse
1409 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001410 if (height > max_height)
1411 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001412 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001413 if (*array == NULL)
1414 goto failed;
1415
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001416 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001417 (*array)->pum_text = vim_strsave((char_u *)"");
1418 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1419
1420 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1421 {
1422 int skip;
1423 int thislen;
1424 int copylen;
1425 int ind;
1426 int cells;
1427
1428 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001429 if (item->bytelen == 0)
1430 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1431 else
1432 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001433 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001434 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1435 {
1436 cells = item->indent * 2;
1437 for (p = item->start + skip;
1438 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001439 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001440 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1441 break;
1442 thislen = p - (item->start + skip);
1443 }
1444 else
1445 thislen = item->bytelen;
1446
1447 // put indent at the start
1448 p = alloc(thislen + item->indent * 2 + 1);
1449 if (p == NULL)
1450 {
1451 for (line = 0; line <= height - 1; ++line)
1452 vim_free((*array)[line].pum_text);
1453 vim_free(*array);
1454 goto failed;
1455 }
1456 for (ind = 0; ind < item->indent * 2; ++ind)
1457 p[ind] = ' ';
1458
1459 // exclude spaces at the end of the string
1460 for (copylen = thislen; copylen > 0; --copylen)
1461 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001462 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001463
1464 vim_strncpy(p + ind, item->start + skip, copylen);
1465 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001466 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001467 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001468 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001469 }
1470 ga_clear(&ga);
1471 return height;
1472
1473failed:
1474 ga_clear(&ga);
1475 return 0;
1476}
1477
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001478 void
1479ui_remove_balloon(void)
1480{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001481 if (balloon_array == NULL)
1482 return;
1483
1484 pum_undisplay();
1485 while (balloon_arraysize > 0)
1486 vim_free(balloon_array[--balloon_arraysize].pum_text);
1487 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001488}
1489
1490/*
1491 * Terminal version of a balloon, uses the popup menu code.
1492 */
1493 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001494ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001495{
1496 ui_remove_balloon();
1497
Bram Moolenaar246fe032017-11-19 19:56:27 +01001498 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001499 {
1500 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001501 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001502 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001503 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001504 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001505 listitem_T *li;
1506 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001507
Bram Moolenaar246fe032017-11-19 19:56:27 +01001508 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001509 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001510 if (balloon_array == NULL)
1511 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001512 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001513 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001515 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001516
1517 balloon_array[idx].pum_text = vim_strsave(
1518 text == NULL ? (char_u *)"" : text);
1519 }
1520 }
1521 else
1522 balloon_arraysize = split_message(mesg, &balloon_array);
1523
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001524 if (balloon_arraysize <= 0)
1525 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001526
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001527 pum_array = balloon_array;
1528 pum_size = balloon_arraysize;
1529 pum_compute_size();
1530 pum_scrollbar = 0;
1531 pum_height = balloon_arraysize;
1532
1533 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1534 pum_selected = -1;
1535 pum_first = 0;
1536 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001537}
1538
1539/*
1540 * Called when the mouse moved, may remove any displayed balloon.
1541 */
1542 void
1543ui_may_remove_balloon(void)
1544{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001545 // For now: remove the balloon whenever the mouse moves to another screen
1546 // cell.
1547 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001548}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001549#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001550
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001551#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001552/*
1553 * Select the pum entry at the mouse position.
1554 */
1555 static void
1556pum_select_mouse_pos(void)
1557{
1558 int idx = mouse_row - pum_row;
1559
1560 if (idx < 0 || idx >= pum_size)
1561 pum_selected = -1;
1562 else if (*pum_array[idx].pum_text != NUL)
1563 pum_selected = idx;
1564}
1565
1566/*
1567 * Execute the currently selected popup menu item.
1568 */
1569 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001570pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001571{
1572 vimmenu_T *mp;
1573 int idx = 0;
1574 exarg_T ea;
1575
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001576 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001577 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001578 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001579 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001580 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001581 break;
1582 }
1583}
1584
1585/*
1586 * Open the terminal version of the popup menu and don't return until it is
1587 * closed.
1588 */
1589 void
1590pum_show_popupmenu(vimmenu_T *menu)
1591{
1592 vimmenu_T *mp;
1593 int idx = 0;
1594 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001595# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001596 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001597# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001598 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001599
1600 pum_undisplay();
1601 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001602 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001603
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001604 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001605 if (menu_is_separator(mp->dname)
1606 || (mp->modes & mp->enabled & mode))
1607 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001608
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001609 // When there are only Terminal mode menus, using "popup Edit" results in
1610 // pum_size being zero.
1611 if (pum_size <= 0)
1612 {
zeertzjq276410e2023-05-07 21:59:33 +01001613 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001614 return;
1615 }
1616
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001617 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001618 if (array == NULL)
1619 return;
1620
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001621 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001622 {
1623 char_u *s = NULL;
1624
1625 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001626 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001627 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001628 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001629 s = mp->dname;
1630 if (s != NULL)
1631 {
1632 s = vim_strsave(s);
1633 if (s != NULL)
1634 array[idx++].pum_text = s;
1635 }
1636 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001637
1638 pum_array = array;
1639 pum_compute_size();
1640 pum_scrollbar = 0;
1641 pum_height = pum_size;
1642 pum_position_at_mouse(20);
1643
1644 pum_selected = -1;
1645 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001646# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001647 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001648 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001649# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001650
1651 for (;;)
1652 {
1653 int c;
1654
1655 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001656 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001657 out_flush();
1658
1659 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001660
zeertzjq92a16782022-07-26 12:24:41 +01001661 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1662 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001663 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001664 break;
1665 else if (c == CAR || c == NL)
1666 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001667 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001668 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001669 break;
1670 }
1671 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1672 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001673 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001674 while (pum_selected > 0)
1675 {
1676 --pum_selected;
1677 if (*array[pum_selected].pum_text != NUL)
1678 break;
1679 }
1680 }
1681 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1682 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001683 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001684 while (pum_selected < pum_size - 1)
1685 {
1686 ++pum_selected;
1687 if (*array[pum_selected].pum_text != NUL)
1688 break;
1689 }
1690 }
1691 else if (c == K_RIGHTMOUSE)
1692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001693 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001694 vungetc(c);
1695 break;
1696 }
1697 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1698 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001699 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001700 pum_select_mouse_pos();
1701 }
1702 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1703 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001704 // left mouse click: select clicked item, if any, and close;
1705 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001706 pum_select_mouse_pos();
1707 if (pum_selected >= 0)
1708 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001709 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001710 break;
1711 }
1712 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1713 break;
1714 }
1715 }
1716
Bram Moolenaar38455a92020-12-24 18:39:02 +01001717 for (idx = 0; idx < pum_size; ++idx)
1718 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001719 vim_free(array);
1720 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001721# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001722 p_bevalterm = save_bevalterm;
1723 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001724# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001725}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001726
1727 void
1728pum_make_popup(char_u *path_name, int use_mouse_pos)
1729{
1730 vimmenu_T *menu;
1731
1732 if (!use_mouse_pos)
1733 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001734 // Hack: set mouse position at the cursor so that the menu pops up
1735 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01001736 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001737 mouse_col = curwin->w_wincol + curwin->w_wcol;
1738 }
1739
1740 menu = gui_find_menu(path_name);
1741 if (menu != NULL)
1742 pum_show_popupmenu(menu);
1743}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001744#endif