blob: 79686c65ea1041ee3edeae758596483a857cb4ce [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
zeertzjq883018f2024-06-15 15:37:11 +020029#ifdef FEAT_RIGHTLEFT
30static int pum_rl; // TRUE when pum is drawn 'rightleft'
31#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +000032
Bram Moolenaar63d9e732019-12-05 21:10:38 +010033static int pum_row; // top row of pum
34static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000035
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020036static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020037static int pum_win_row;
38static int pum_win_height;
39static int pum_win_col;
40static int pum_win_wcol;
41static int pum_win_width;
42
Bram Moolenaar04357fb2019-12-10 21:50:56 +010043// Some parts are not updated when a popup menu is visible. Setting this flag
44// makes pum_visible() return FALSE even when there is a popup menu.
45static int pum_pretend_not_visible = FALSE;
46
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000048
Bram Moolenaar4b779472005-10-04 09:12:31 +000049#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000050
Bram Moolenaar51b0f372017-11-18 18:52:04 +010051 static void
52pum_compute_size(void)
53{
54 int i;
55 int w;
56
Bram Moolenaar63d9e732019-12-05 21:10:38 +010057 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010058 pum_base_width = 0;
59 pum_kind_width = 0;
60 pum_extra_width = 0;
61 for (i = 0; i < pum_size; ++i)
62 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020063 if (pum_array[i].pum_text != NULL)
64 {
65 w = vim_strsize(pum_array[i].pum_text);
66 if (pum_base_width < w)
67 pum_base_width = w;
68 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010069 if (pum_array[i].pum_kind != NULL)
70 {
71 w = vim_strsize(pum_array[i].pum_kind) + 1;
72 if (pum_kind_width < w)
73 pum_kind_width = w;
74 }
75 if (pum_array[i].pum_extra != NULL)
76 {
77 w = vim_strsize(pum_array[i].pum_extra) + 1;
78 if (pum_extra_width < w)
79 pum_extra_width = w;
80 }
81 }
82}
83
Bram Moolenaar4b779472005-10-04 09:12:31 +000084/*
85 * Show the popup menu with items "array[size]".
86 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010087 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000088 * The menu appears above the screen line "row" or at "row" + "height" - 1.
89 */
90 void
Bram Moolenaar05540972016-01-30 20:31:25 +010091pum_display(
92 pumitem_T *array,
93 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094 int selected) // index of initially selected item, none if
95 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000096{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000097 int def_width;
98 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000099 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100100 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100101 int above_row;
102 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000103 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200104#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100105 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100106#endif
zeertzjq883018f2024-06-15 15:37:11 +0200107
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100108#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200109 pum_rl = State != MODE_CMDLINE && curwin->w_p_rl;
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100110#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000111
Bram Moolenaara5e66212017-09-29 22:42:33 +0200112 do
113 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100114 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200115 above_row = 0;
116 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000117
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100118 // Pretend the pum is already there to avoid that must_redraw is set
119 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200120 pum_array = (pumitem_T *)1;
121 validate_cursor_col();
122 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000123
Bram Moolenaar491ac282018-06-17 14:47:55 +0200124 // Remember the essential parts of the window position and size, so we
125 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200126 pum_window = curwin;
Bram Moolenaar24959102022-05-07 20:01:16 +0100127 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000128 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000129 pum_win_row = cmdline_row;
130 else
131 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200132 pum_win_height = curwin->w_height;
133 pum_win_col = curwin->w_wincol;
134 pum_win_wcol = curwin->w_wcol;
135 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000136
Bram Moolenaar4033c552017-09-16 20:54:51 +0200137#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200138 FOR_ALL_WINDOWS(pvwin)
139 if (pvwin->w_p_pvw)
140 break;
141 if (pvwin != NULL)
142 {
143 if (W_WINROW(pvwin) < W_WINROW(curwin))
144 above_row = W_WINROW(pvwin) + pvwin->w_height;
145 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
146 below_row = W_WINROW(pvwin);
147 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100148#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000149
Bram Moolenaara5e66212017-09-29 22:42:33 +0200150 /*
151 * Figure out the size and position of the pum.
152 */
153 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000154 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000155 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200156 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000157 if (p_ph > 0 && pum_height > p_ph)
158 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000159
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100160 // Put the pum below "pum_win_row" if possible. If there are few lines
161 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200162 if (pum_win_row + 2 >= below_row - pum_height
163 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200164 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100165 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166
Bram Moolenaar24959102022-05-07 20:01:16 +0100167 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100168 // for cmdline pum, no need for context lines
169 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200170 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100171 {
172 // Leave two lines of context if possible
173 if (curwin->w_wrow - curwin->w_cline_row >= 2)
174 context_lines = 2;
175 else
176 context_lines = curwin->w_wrow - curwin->w_cline_row;
177 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200178
Bram Moolenaar491ac282018-06-17 14:47:55 +0200179 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200180 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200181 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200182 pum_height = size;
183 }
184 else
185 {
186 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200187 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200188 }
189 if (p_ph > 0 && pum_height > p_ph)
190 {
191 pum_row += pum_height - p_ph;
192 pum_height = p_ph;
193 }
194 }
195 else
196 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100197 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200198
Bram Moolenaar24959102022-05-07 20:01:16 +0100199 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100200 // for cmdline pum, no need for context lines
201 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200202 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100203 {
204 // Leave two lines of context if possible
205 validate_cheight();
206 if (curwin->w_cline_row
207 + curwin->w_cline_height - curwin->w_wrow >= 3)
208 context_lines = 3;
209 else
210 context_lines = curwin->w_cline_row
211 + curwin->w_cline_height - curwin->w_wrow;
212 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200213
Bram Moolenaar491ac282018-06-17 14:47:55 +0200214 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200215 if (size > below_row - pum_row)
216 pum_height = below_row - pum_row;
217 else
218 pum_height = size;
219 if (p_ph > 0 && pum_height > p_ph)
220 pum_height = p_ph;
221 }
222
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100223 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200224 if (pum_height < 1 || (pum_height == 1 && size > 1))
225 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000226
Bram Moolenaar4033c552017-09-16 20:54:51 +0200227#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100228 // If there is a preview window above avoid drawing over it.
229 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000230 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100231 pum_row = above_row;
232 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000233 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200234#endif
235
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100236 pum_array = array;
237 pum_size = size;
238 pum_compute_size();
239 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000240
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100241 // Calculate column
Bram Moolenaar24959102022-05-07 20:01:16 +0100242 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000243 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000244 cursor_col = cmdline_compl_startcol();
245 else
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100246 {
247 // w_wcol includes virtual text "above"
248 int wcol = curwin->w_wcol % curwin->w_width;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000249#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200250 if (pum_rl)
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100251 cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
252 else
Bram Moolenaarabc97732007-08-08 20:49:37 +0000253#endif
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100254 cursor_col = curwin->w_wincol + wcol;
255 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000256
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100257 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200258 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000259 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200260 pum_scrollbar = 1;
261 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000262 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000263 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200264 pum_scrollbar = 0;
265
266 if (def_width < max_width)
267 def_width = max_width;
268
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100269 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000270#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200271 && !pum_rl)
272 || (pum_rl && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000273#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200274 ))
275 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100276 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100277 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000278
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100279 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200280#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200281 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200282 pum_width = pum_col - pum_scrollbar + 1;
283 else
284#endif
285 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000286
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100287 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100288 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200289 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100290 // the width is more than needed for the items, make it
291 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100292 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100293 if (pum_width < p_pw)
294 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200295 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100296 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100297#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200298 && !pum_rl)
299 || (pum_rl && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100300 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100301#endif
302 ))
303 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100304 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100305#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200306 if (pum_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100307 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100308 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100309 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100310 if (pum_col >= Columns)
311 pum_col = Columns - 1;
312 }
zeertzjq883018f2024-06-15 15:37:11 +0200313 else if (!pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100314#endif
315 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100316 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
317 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100318 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100319 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100320 pum_col = Columns - max_width - pum_scrollbar;
321 if (pum_col < 0)
322 pum_col = 0;
323 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100324 }
325
326#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200327 if (pum_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100328 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100329 else
330#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100331 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100332
Bram Moolenaar42443c72018-02-10 18:28:52 +0100333 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100334 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100335 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100336#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200337 if (pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100338 {
339 if (pum_width > pum_col)
340 pum_width = pum_col;
341 }
342 else
343#endif
344 {
345 if (pum_width >= Columns - pum_col)
346 pum_width = Columns - pum_col - 1;
347 }
348 }
349 else if (pum_width > max_width + pum_kind_width
350 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100351 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100352 {
353 pum_width = max_width + pum_kind_width
354 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100355 if (pum_width < p_pw)
356 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100357 }
358 }
359
Bram Moolenaara5e66212017-09-29 22:42:33 +0200360 }
361 else if (Columns < def_width)
362 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100363 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200364#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200365 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200366 pum_col = Columns - 1;
367 else
368#endif
369 pum_col = 0;
370 pum_width = Columns - 1;
371 }
372 else
373 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100374 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100375 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200376#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200377 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200378 pum_col = max_width - 1;
379 else
380#endif
381 pum_col = Columns - max_width;
382 pum_width = max_width - pum_scrollbar;
383 }
384
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100385 // Set selected item and redraw. If the window size changed need to
386 // redo the positioning. Limit this to two times, when there is not
387 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200388 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100389
390 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000391}
392
393/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100394 * Set a flag that when pum_redraw() is called it first calls update_screen().
395 * This will avoid clearing and redrawing the popup menu, prevent flicker.
396 */
397 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000398pum_call_update_screen(void)
Bram Moolenaarae654382019-01-17 21:09:05 +0100399{
400 call_update_screen = TRUE;
401
402 // Update the cursor position to be able to compute the popup menu
403 // position. The cursor line length may have changed because of the
404 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100405 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100406 validate_cursor();
407}
408
409/*
410 * Return TRUE if we are going to redraw the popup menu and the screen position
411 * "row"/"col" is under the popup menu.
412 */
413 int
Bakudankun65555002021-11-17 20:40:16 +0000414pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100415{
Bakudankun65555002021-11-17 20:40:16 +0000416 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100417 && row >= pum_row
418 && row < pum_row + pum_height
419 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200420 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100421}
422
423/*
glepnir40c1c332024-06-11 19:37:04 +0200424 * displays text on the popup menu with specific attributes.
425 */
426 static void
zeertzjqafbe5352024-06-14 20:24:22 +0200427pum_screen_put_with_attr(int row, int col, char_u *text, int textlen, hlf_T hlf)
glepnir40c1c332024-06-11 19:37:04 +0200428{
429 int i;
430 int leader_len;
431 int char_len;
432 int cells;
433 int new_attr;
434 char_u *rt_leader = NULL;
435 char_u *match_leader = NULL;
436 char_u *ptr = text;
437 garray_T *ga = NULL;
438 char_u *leader = ins_compl_leader();
439 int in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
440
zeertzjqafbe5352024-06-14 20:24:22 +0200441 if (leader == NULL || *leader == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
442 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
443 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
glepnir40c1c332024-06-11 19:37:04 +0200444 {
zeertzjqafbe5352024-06-14 20:24:22 +0200445 screen_puts_len(text, textlen, row, col, highlight_attr[hlf]);
446 return;
glepnir40c1c332024-06-11 19:37:04 +0200447 }
448
449#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200450 if (pum_rl)
zeertzjqafbe5352024-06-14 20:24:22 +0200451 rt_leader = reverse_text(leader);
glepnir40c1c332024-06-11 19:37:04 +0200452#endif
453 match_leader = rt_leader != NULL ? rt_leader : leader;
glepnir1c296022024-06-13 17:21:24 +0200454 leader_len = (int)STRLEN(match_leader);
glepnir40c1c332024-06-11 19:37:04 +0200455
glepnir1c296022024-06-13 17:21:24 +0200456 if (in_fuzzy)
zeertzjqafbe5352024-06-14 20:24:22 +0200457 ga = fuzzy_match_str_with_pos(text, match_leader);
glepnir40c1c332024-06-11 19:37:04 +0200458
459 // Render text with proper attributes
460 while (*ptr != NUL && ptr < text + textlen)
461 {
zeertzjqafbe5352024-06-14 20:24:22 +0200462 char_len = mb_ptr2len(ptr);
463 cells = mb_ptr2cells(ptr);
464 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200465
zeertzjqafbe5352024-06-14 20:24:22 +0200466 if (ga != NULL)
467 {
468 // Handle fuzzy matching
469 for (i = 0; i < ga->ga_len; i++)
470 {
471 int_u *match_pos = ((int_u *)ga->ga_data) + i;
472 int_u actual_char_pos = 0;
473 char_u *temp_ptr = text;
474 while (temp_ptr < ptr)
475 {
476 temp_ptr += mb_ptr2len(temp_ptr);
477 actual_char_pos++;
478 }
479 if (actual_char_pos == match_pos[0])
480 {
481 new_attr = highlight_attr[hlf == HLF_PSI
482 ? HLF_PMSI : HLF_PMNI];
483 break;
484 }
485 }
486 }
487 else if (!in_fuzzy && (ptr - text < leader_len)
488 && (STRNCMP(text, match_leader, leader_len) == 0))
489 new_attr = highlight_attr[hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI];
glepnir40c1c332024-06-11 19:37:04 +0200490
zeertzjqafbe5352024-06-14 20:24:22 +0200491 screen_puts_len(ptr, char_len, row, col, new_attr);
492 col += cells;
493 ptr += char_len;
glepnir40c1c332024-06-11 19:37:04 +0200494 }
495
496 if (ga != NULL)
497 {
zeertzjqafbe5352024-06-14 20:24:22 +0200498 ga_clear(ga);
499 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200500 }
501 if (rt_leader)
zeertzjqafbe5352024-06-14 20:24:22 +0200502 vim_free(rt_leader);
glepnir40c1c332024-06-11 19:37:04 +0200503}
504
505/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000506 * Redraw the popup menu, using "pum_first" and "pum_selected".
507 */
508 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100509pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000510{
511 int row = pum_row;
512 int col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000513 int attr_scroll = highlight_attr[HLF_PSB];
514 int attr_thumb = highlight_attr[HLF_PST];
zeertzjqafbe5352024-06-14 20:24:22 +0200515 hlf_T *hlfs; // array used for highlights
516 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000517 int attr;
518 int i;
519 int idx;
520 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000521 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000522 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000523 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100524 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000525 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000526 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000527
zeertzjqafbe5352024-06-14 20:24:22 +0200528 hlf_T hlfsNorm[3];
529 hlf_T hlfsSel[3];
Bram Moolenaare638acc2023-03-15 17:08:51 +0000530 // "word"
zeertzjqafbe5352024-06-14 20:24:22 +0200531 hlfsNorm[0] = HLF_PNI;
532 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000533 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200534 hlfsNorm[1] = HLF_PNK;
535 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000536 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200537 hlfsNorm[2] = HLF_PNX;
538 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000539
Bram Moolenaarae654382019-01-17 21:09:05 +0100540 if (call_update_screen)
541 {
542 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100543 // Do not redraw in pum_may_redraw() and don't draw in the area where
544 // the popup menu will be.
545 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100546 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100547 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100548 }
549
550 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100551 if (pum_first > pum_size - pum_height)
552 pum_first = pum_size - pum_height;
553
Bram Moolenaar4b779472005-10-04 09:12:31 +0000554 if (pum_scrollbar)
555 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100556 thumb_height = pum_height * pum_height / pum_size;
557 if (thumb_height == 0)
558 thumb_height = 1;
559 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000560 + (pum_size - pum_height) / 2)
561 / (pum_size - pum_height);
562 }
563
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100564#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200565 // The popup menu is drawn over popup menus with zindex under
566 // POPUPMENU_ZINDEX.
567 screen_zindex = POPUPMENU_ZINDEX;
568#endif
569
Bram Moolenaar4b779472005-10-04 09:12:31 +0000570 for (i = 0; i < pum_height; ++i)
571 {
572 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200573 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
574 hlf = hlfs[0]; // start with "word" highlight
575 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000576
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100577 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000578#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200579 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000580 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200581 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000582 screen_putchar(' ', row, pum_col + 1, attr);
583 }
584 else
585#endif
586 if (pum_col > 0)
587 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000588
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100589 // Display each entry, use two spaces for a Tab.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000590 // Do this 3 times:
591 // 0 - main text
592 // 1 - kind
593 // 2 - extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000594 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000595 totwidth = 0;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000596 for (round = 0; round < 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000597 {
zeertzjqafbe5352024-06-14 20:24:22 +0200598 hlf = hlfs[round];
599 attr = highlight_attr[hlf];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000600 width = 0;
601 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000602 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000603 {
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000604 case 0: p = pum_array[idx].pum_text; break;
605 case 1: p = pum_array[idx].pum_kind; break;
606 case 2: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000607 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000608 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100609 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000610 {
611 if (s == NULL)
612 s = p;
613 w = ptr2cells(p);
614 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
615 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100616 // Display the text that fits or comes before a Tab.
617 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000618 char_u *st;
619 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000620
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100621 if (saved != NUL)
622 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000623 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100624 if (saved != NUL)
625 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000626#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200627 if (pum_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000628 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000629 if (st != NULL)
630 {
631 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000632
633 if (rt != NULL)
634 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100635 char_u *rt_start = rt;
636 int size;
637
638 size = vim_strsize(rt);
639 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000640 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100641 do
642 {
643 size -= has_mbyte
644 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100645 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100646 } while (size > pum_width);
647
648 if (size < pum_width)
649 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100650 // Most left character requires
651 // 2-cells but only 1 cell is
652 // available on screen. Put a
653 // '<' on the left of the pum
654 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100655 *(--rt) = '<';
656 size++;
657 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000658 }
zeertzjqafbe5352024-06-14 20:24:22 +0200659 pum_screen_put_with_attr(row, col - size + 1, rt, (int)STRLEN(rt), hlf);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100660 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000661 }
662 vim_free(st);
663 }
664 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000665 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000666 else
667#endif
668 {
669 if (st != NULL)
670 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100671 int size = (int)STRLEN(st);
672 int cells = (*mb_string2cells)(st, size);
673
674 // only draw the text that fits
675 while (size > 0
676 && col + cells > pum_width + pum_col)
677 {
678 --size;
679 if (has_mbyte)
680 {
681 size -= (*mb_head_off)(st, st + size);
682 cells -= (*mb_ptr2cells)(st + size);
683 }
684 else
685 --cells;
686 }
zeertzjqafbe5352024-06-14 20:24:22 +0200687 pum_screen_put_with_attr(row, col, st, size, hlf);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000688 vim_free(st);
689 }
690 col += width;
691 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000692
693 if (*p != TAB)
694 break;
695
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100696 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000697#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200698 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000699 {
700 screen_puts_len((char_u *)" ", 2, row, col - 1,
701 attr);
702 col -= 2;
703 }
704 else
705#endif
706 {
707 screen_puts_len((char_u *)" ", 2, row, col, attr);
708 col += 2;
709 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000710 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100711 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000712 width = 0;
713 }
714 else
715 width += w;
716 }
717
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000718 if (round > 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000719 n = pum_kind_width + 1;
720 else
721 n = 1;
722
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100723 // Stop when there is nothing more to display.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000724 if (round == 2
725 || (round == 1 && pum_array[idx].pum_extra == NULL)
726 || (round == 0 && pum_array[idx].pum_kind == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000727 && pum_array[idx].pum_extra == NULL)
728 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000729 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000730#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200731 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000732 {
733 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
734 col + 1, ' ', ' ', attr);
zeertzjqa2324372024-06-15 15:08:27 +0200735 col = pum_col - pum_base_width - n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000736 }
737 else
738#endif
739 {
740 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000741 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000742 col = pum_col + pum_base_width + n;
743 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000744 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000745 }
746
Bram Moolenaarabc97732007-08-08 20:49:37 +0000747#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200748 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000749 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
750 ' ', attr);
751 else
752#endif
753 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
754 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000755 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000756 {
757#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200758 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000759 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100760 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000761 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000762 else
763#endif
764 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100765 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000766 ? attr_thumb : attr_scroll);
767 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000768
769 ++row;
770 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200771
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100772#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200773 screen_zindex = 0;
774#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000775}
776
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100777#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200778/*
779 * Position the info popup relative to the popup menu item.
780 */
781 void
782pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200783{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100784 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200785 int row = pum_row;
786 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200787 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200788
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200789 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200790 if (Columns - col < 20 && Columns - col < pum_col)
791 {
792 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200793 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200794 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200795 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200796 }
797 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200798 wp->w_maxwidth = Columns - col + 1;
799 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200800 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
801 {
802 // option value overrules computed value
803 wp->w_maxwidth = wp->w_maxwidth_opt;
804 used_maxwidth_opt = TRUE;
805 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200806
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200807 row -= popup_top_extra(wp);
808 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200809 {
810 if (pum_row < pum_win_row)
811 {
812 // menu above cursor line, align with bottom
813 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200814 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200815 }
816 else
817 // menu below cursor line, align with top
818 row += 1;
819 }
820 else
821 // align with the selected item
822 row += pum_selected - pum_first + 1;
823
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100824 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200825 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100826 // The popup is not going to fit or will overlap with the cursor
827 // position, hide the popup.
828 wp->w_popup_flags |= POPF_HIDDEN;
829 else
830 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200831}
832#endif
833
Bram Moolenaar4b779472005-10-04 09:12:31 +0000834/*
835 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000836 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000837 * This may be repeated when the preview window is used:
838 * "repeat" == 0: open preview window normally
839 * "repeat" == 1: open preview window but don't set the size
840 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000841 * Returns TRUE when the window was resized and the location of the popup menu
842 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000843 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000844 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200845pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000846{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000847 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000848 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200849#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200850 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +0200851 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +0200852#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100853#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200854 int has_info = FALSE;
855#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000856
Bram Moolenaar4b779472005-10-04 09:12:31 +0000857 pum_selected = n;
858
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000859 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000860 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000861 if (pum_first > pum_selected - 4)
862 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100863 // scroll down; when we did a jump it's probably a PageUp then
864 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000865 if (pum_first > pum_selected - 2)
866 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000867 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000868 if (pum_first < 0)
869 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000870 else if (pum_first > pum_selected)
871 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000872 }
873 else
874 pum_first = pum_selected;
875 }
876 else if (pum_first < pum_selected - pum_height + 5)
877 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100878 // scroll up; when we did a jump it's probably a PageDown then
879 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000880 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000881 {
882 pum_first += pum_height - 2;
883 if (pum_first < pum_selected - pum_height + 1)
884 pum_first = pum_selected - pum_height + 1;
885 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000886 else
887 pum_first = pum_selected - pum_height + 1;
888 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000889
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100890 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000891 if (context > 3)
892 context = 3;
893 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000894 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000895 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000896 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100897 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000898 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000899 if (pum_first < 0)
900 pum_first = 0;
901 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000902 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000903 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100904 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000905 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000906 }
907 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200908 // adjust for the number of lines displayed
909 if (pum_first > pum_size - pum_height)
910 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000911
Bram Moolenaar4033c552017-09-16 20:54:51 +0200912#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000913 /*
914 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100915 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000916 * Skip this when tried twice already.
917 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000918 * NOTE: Be very careful not to sync undo!
919 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000920 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000921 && Rows > 10
922 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +0200923 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000924 {
925 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200926 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100927# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200928 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200929# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100930# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200931# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100932# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200933 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +0200934 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200935 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +0200936 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200937 use_popup = USEPOPUP_NORMAL;
938 else
939 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100940 if (use_popup != USEPOPUP_NONE)
941 // don't use WinEnter or WinLeave autocommands for the info
942 // popup
943 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200944# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200945 // Open a preview window and set "curwin" to it.
946 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000947 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200948 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
949 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200950 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200951 // Prevent undo sync here, if an autocommand syncs undo weird
952 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100953 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200954 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100955 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100956 if (RedrawingDisabled > 0)
957 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000958 g_do_tagpreview = 0;
959
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200960 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100961# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200962 || (curwin->w_popup_flags & POPF_INFO)
963# endif
964 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000965 {
mathew20f61d92023-08-26 18:11:02 +0200966 int res = OK;
967
Bram Moolenaar50e53762016-10-27 14:49:15 +0200968 if (!resized
969 && curbuf->b_nwindows == 1
970 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200971 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000972 && curbuf->b_p_bh[0] == 'w')
973 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200974 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100975 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200976 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000977 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000978 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000979 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200980 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000981 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000982 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000983 --no_u_sync;
984 if (res == OK)
985 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200986 // Edit a new, empty buffer. Set options for a "wipeout"
987 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100988 set_option_value_give_err((char_u *)"swf",
989 0L, NULL, OPT_LOCAL);
990 set_option_value_give_err((char_u *)"bl",
991 0L, NULL, OPT_LOCAL);
992 set_option_value_give_err((char_u *)"bt",
993 0L, (char_u *)"nofile", OPT_LOCAL);
994 set_option_value_give_err((char_u *)"bh",
995 0L, (char_u *)"wipe", OPT_LOCAL);
996 set_option_value_give_err((char_u *)"diff",
997 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000998 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000999 }
1000 if (res == OK)
1001 {
1002 char_u *p, *e;
1003 linenr_T lnum = 0;
1004
1005 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1006 {
1007 e = vim_strchr(p, '\n');
1008 if (e == NULL)
1009 {
1010 ml_append(lnum++, p, 0, FALSE);
1011 break;
1012 }
mathewc51fa7b2023-08-23 20:55:17 +02001013 *e = NUL;
1014 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1015 *e = '\n';
1016 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001017 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001018 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001019 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001020
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001021 // Increase the height of the preview window to show the
1022 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001023 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001024 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001025 if (lnum > p_pvh)
1026 lnum = p_pvh;
1027 if (curwin->w_height < lnum)
1028 {
1029 win_setheight((int)lnum);
1030 resized = TRUE;
1031 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001032 }
1033
1034 curbuf->b_changed = 0;
1035 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001036 if (pum_selected != prev_selected)
1037 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001038# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001039 curwin->w_firstline = 1;
1040# endif
1041 curwin->w_topline = 1;
1042 }
1043 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1044 curwin->w_topline = curbuf->b_ml.ml_line_count;
1045 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001046 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001047# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001048 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001049 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001050 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001051 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001052 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001053 }
1054# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001055 if ((curwin != curwin_save && win_valid(curwin_save))
1056 || (curtab != curtab_save
1057 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001058 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001059 int save_redr_status;
1060
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001061 if (curtab != curtab_save && valid_tabpage(curtab_save))
1062 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1063
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001064 // When the first completion is done and the preview
1065 // window is not resized, skip the preview window's
1066 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001067 if (ins_compl_active() && !resized)
1068 curwin->w_redr_status = FALSE;
1069
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001070 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001071 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001072 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001073
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001074 // When the preview window was resized we need to
1075 // update the view on the buffer. Only go back to
1076 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001077 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001078 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001079 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001080 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001081 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001082 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001083 update_topline();
1084 }
1085
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001086 // Update the screen before drawing the popup menu.
1087 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001088 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001089
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001090 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001091 // it causes flicker. When resizing we need to draw
1092 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001093 // Also do not redraw the status line of the original
1094 // current window here, to avoid it gets drawn with
1095 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001096 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001097 save_redr_status = curwin_save->w_redr_status;
1098 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001099 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001100 pum_pretend_not_visible = FALSE;
1101 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001102 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001103
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001104 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001105 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001106# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001107 win_T *wp = curwin;
1108# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001109 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001110 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001111 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001112# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001113 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1114 popup_hide(wp);
1115# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001116 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001117
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001118 // May need to update the screen again when there are
1119 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001120 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001121 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001122 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001123 pum_pretend_not_visible = FALSE;
1124 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001125 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001126 }
1127 }
1128 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001129# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001130 if (WIN_IS_POPUP(curwin))
1131 // can't keep focus in a popup window
1132 win_enter(firstwin, TRUE);
1133# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001134# ifdef FEAT_PROP_POPUP
1135 if (use_popup != USEPOPUP_NONE)
1136 unblock_autocmds();
1137# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001138 }
1139#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001140 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001141#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001142 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001143 // hide any popup info window
1144 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001145#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001146
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001147 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001148}
1149
1150/*
1151 * Undisplay the popup menu (later).
1152 */
1153 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001154pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001155{
1156 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001157 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001158 redraw_tabline = TRUE;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001159 if (pum_in_cmdline)
1160 {
1161 clear_cmdline = TRUE;
1162 pum_in_cmdline = FALSE;
1163 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001164 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001165#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001166 // hide any popup info window
1167 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001168#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001169}
1170
1171/*
1172 * Clear the popup menu. Currently only resets the offset to the first
1173 * displayed item.
1174 */
1175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001176pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001177{
1178 pum_first = 0;
1179}
1180
1181/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001182 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1183 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1184 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001185 */
1186 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001187pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001188{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001189 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001190}
1191
Bram Moolenaare3226be2005-12-18 22:10:00 +00001192/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001193 * Return TRUE if the popup can be redrawn in the same position.
1194 */
1195 static int
1196pum_in_same_position(void)
1197{
1198 return pum_window != curwin
1199 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1200 && pum_win_height == curwin->w_height
1201 && pum_win_col == curwin->w_wincol
1202 && pum_win_width == curwin->w_width);
1203}
1204
1205/*
1206 * Return TRUE when pum_may_redraw() will call pum_redraw().
1207 * This means that the pum area should not be overwritten to avoid flicker.
1208 */
1209 int
1210pum_redraw_in_same_position(void)
1211{
1212 if (!pum_visible() || pum_will_redraw)
1213 return FALSE; // nothing to do
1214
1215 return pum_in_same_position();
1216}
1217
1218/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001219 * Reposition the popup menu to adjust for window layout changes.
1220 */
1221 void
1222pum_may_redraw(void)
1223{
1224 pumitem_T *array = pum_array;
1225 int len = pum_size;
1226 int selected = pum_selected;
1227
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001228 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001229 return; // nothing to do
1230
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001231 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001232 {
1233 // window position didn't change, redraw in the same position
1234 pum_redraw();
1235 }
1236 else
1237 {
1238 int wcol = curwin->w_wcol;
1239
1240 // Window layout changed, recompute the position.
1241 // Use the remembered w_wcol value, the cursor may have moved when a
1242 // completion was inserted, but we want the menu in the same position.
1243 pum_undisplay();
1244 curwin->w_wcol = pum_win_wcol;
1245 curwin->w_valid |= VALID_WCOL;
1246 pum_display(array, len, selected);
1247 curwin->w_wcol = wcol;
1248 }
1249}
1250
1251/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001252 * Return the height of the popup menu, the number of entries visible.
1253 * Only valid when pum_visible() returns TRUE!
1254 */
1255 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001256pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001257{
1258 return pum_height;
1259}
1260
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001261#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001262/*
1263 * Add size information about the pum to "dict".
1264 */
1265 void
1266pum_set_event_info(dict_T *dict)
1267{
1268 if (!pum_visible())
1269 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001270 (void)dict_add_number(dict, "height", pum_height);
1271 (void)dict_add_number(dict, "width", pum_width);
1272 (void)dict_add_number(dict, "row", pum_row);
1273 (void)dict_add_number(dict, "col", pum_col);
1274 (void)dict_add_number(dict, "size", pum_size);
1275 (void)dict_add_bool(dict, "scrollbar",
1276 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001277}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001278#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001279
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001280#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001281 static void
1282pum_position_at_mouse(int min_width)
1283{
1284 if (Rows - mouse_row > pum_size)
1285 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001286 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001287 pum_row = mouse_row + 1;
1288 if (pum_height > Rows - pum_row)
1289 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001290 if (pum_row + pum_height > cmdline_row)
1291 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001292 }
1293 else
1294 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001295 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001296 pum_row = mouse_row - pum_size;
1297 if (pum_row < 0)
1298 {
1299 pum_height += pum_row;
1300 pum_row = 0;
1301 }
1302 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001303
zeertzjq883018f2024-06-15 15:37:11 +02001304# ifdef FEAT_RIGHTLEFT
1305 if (pum_rl)
1306 {
1307 if (mouse_col + 1 >= pum_base_width
1308 || mouse_col + 1 > min_width)
1309 // Enough space to show at mouse column.
1310 pum_col = mouse_col;
1311 else
1312 // Not enough space, left align with window.
1313 pum_col = (pum_base_width > min_width
1314 ? min_width : pum_base_width) - 1;
1315 pum_width = pum_col + 1;
1316 }
1317 else
1318# endif
1319 {
1320 if (Columns - mouse_col >= pum_base_width
1321 || Columns - mouse_col > min_width)
1322 // Enough space to show at mouse column.
1323 pum_col = mouse_col;
1324 else
1325 // Not enough space, right align with window.
1326 pum_col = Columns - (pum_base_width > min_width
1327 ? min_width : pum_base_width);
1328 pum_width = Columns - pum_col;
1329 }
1330
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001331 if (pum_width > pum_base_width + 1)
1332 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001333
1334 // Do not redraw at cursor position.
1335 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001336}
1337
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001338#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001339
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001340#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001341static pumitem_T *balloon_array = NULL;
1342static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001343
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001344# define BALLOON_MIN_WIDTH 50
1345# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001346
Bram Moolenaar246fe032017-11-19 19:56:27 +01001347typedef struct {
1348 char_u *start;
1349 int bytelen;
1350 int cells;
1351 int indent;
1352} balpart_T;
1353
1354/*
1355 * Split a string into parts to display in the balloon.
1356 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1357 * strings and make a struct look good.
1358 * Resulting array is stored in "array" and returns the size of the array.
1359 */
1360 int
1361split_message(char_u *mesg, pumitem_T **array)
1362{
1363 garray_T ga;
1364 char_u *p;
1365 balpart_T *item;
1366 int quoted = FALSE;
1367 int height;
1368 int line;
1369 int item_idx;
1370 int indent = 0;
1371 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001372 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001373 int long_item_count = 0;
1374 int split_long_items = FALSE;
1375
1376 ga_init2(&ga, sizeof(balpart_T), 20);
1377 p = mesg;
1378
1379 while (*p != NUL)
1380 {
1381 if (ga_grow(&ga, 1) == FAIL)
1382 goto failed;
1383 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1384 item->start = p;
1385 item->indent = indent;
1386 item->cells = indent * 2;
1387 ++ga.ga_len;
1388 while (*p != NUL)
1389 {
1390 if (*p == '"')
1391 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001392 else if (*p == '\n')
1393 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001394 else if (*p == '\\' && p[1] != NUL)
1395 ++p;
1396 else if (!quoted)
1397 {
1398 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1399 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001400 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001401 if (*p == '{')
1402 ++indent;
1403 else if (*p == '}' && indent > 0)
1404 --indent;
1405 ++item->cells;
1406 p = skipwhite(p + 1);
1407 break;
1408 }
1409 }
1410 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001411 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001412 }
1413 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001414 if (*p == '\n')
1415 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001416 if (item->cells > max_cells)
1417 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001418 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001419 }
1420
1421 height = 2 + ga.ga_len;
1422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001423 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001424 if (long_item_count > 0 && height + long_item_count <= max_height)
1425 {
1426 split_long_items = TRUE;
1427 height += long_item_count;
1428 }
1429
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001430 // Limit to half the window height, it has to fit above or below the mouse
1431 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001432 if (height > max_height)
1433 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001434 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001435 if (*array == NULL)
1436 goto failed;
1437
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001438 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001439 (*array)->pum_text = vim_strsave((char_u *)"");
1440 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1441
1442 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1443 {
1444 int skip;
1445 int thislen;
1446 int copylen;
1447 int ind;
1448 int cells;
1449
1450 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001451 if (item->bytelen == 0)
1452 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1453 else
1454 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001455 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001456 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1457 {
1458 cells = item->indent * 2;
1459 for (p = item->start + skip;
1460 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001461 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001462 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1463 break;
1464 thislen = p - (item->start + skip);
1465 }
1466 else
1467 thislen = item->bytelen;
1468
1469 // put indent at the start
1470 p = alloc(thislen + item->indent * 2 + 1);
1471 if (p == NULL)
1472 {
1473 for (line = 0; line <= height - 1; ++line)
1474 vim_free((*array)[line].pum_text);
1475 vim_free(*array);
1476 goto failed;
1477 }
1478 for (ind = 0; ind < item->indent * 2; ++ind)
1479 p[ind] = ' ';
1480
1481 // exclude spaces at the end of the string
1482 for (copylen = thislen; copylen > 0; --copylen)
1483 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001484 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001485
1486 vim_strncpy(p + ind, item->start + skip, copylen);
1487 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001488 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001489 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001490 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001491 }
1492 ga_clear(&ga);
1493 return height;
1494
1495failed:
1496 ga_clear(&ga);
1497 return 0;
1498}
1499
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001500 void
1501ui_remove_balloon(void)
1502{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001503 if (balloon_array == NULL)
1504 return;
1505
1506 pum_undisplay();
1507 while (balloon_arraysize > 0)
1508 vim_free(balloon_array[--balloon_arraysize].pum_text);
1509 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001510}
1511
1512/*
1513 * Terminal version of a balloon, uses the popup menu code.
1514 */
1515 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001516ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001517{
1518 ui_remove_balloon();
1519
Bram Moolenaar246fe032017-11-19 19:56:27 +01001520 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001521 {
1522 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001523 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001524 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001525 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001526 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001527 listitem_T *li;
1528 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001529
Bram Moolenaar246fe032017-11-19 19:56:27 +01001530 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001531 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001532 if (balloon_array == NULL)
1533 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001534 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001535 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001538
1539 balloon_array[idx].pum_text = vim_strsave(
1540 text == NULL ? (char_u *)"" : text);
1541 }
1542 }
1543 else
1544 balloon_arraysize = split_message(mesg, &balloon_array);
1545
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001546 if (balloon_arraysize <= 0)
1547 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001548
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001549 pum_array = balloon_array;
1550 pum_size = balloon_arraysize;
1551 pum_compute_size();
1552 pum_scrollbar = 0;
1553 pum_height = balloon_arraysize;
zeertzjq883018f2024-06-15 15:37:11 +02001554# ifdef FEAT_RIGHTLEFT
1555 pum_rl = curwin->w_p_rl;
1556# endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001557
1558 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1559 pum_selected = -1;
1560 pum_first = 0;
1561 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001562}
1563
1564/*
1565 * Called when the mouse moved, may remove any displayed balloon.
1566 */
1567 void
1568ui_may_remove_balloon(void)
1569{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001570 // For now: remove the balloon whenever the mouse moves to another screen
1571 // cell.
1572 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001573}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001574#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001575
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001576#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001577/*
1578 * Select the pum entry at the mouse position.
1579 */
1580 static void
1581pum_select_mouse_pos(void)
1582{
1583 int idx = mouse_row - pum_row;
1584
1585 if (idx < 0 || idx >= pum_size)
1586 pum_selected = -1;
1587 else if (*pum_array[idx].pum_text != NUL)
1588 pum_selected = idx;
1589}
1590
1591/*
1592 * Execute the currently selected popup menu item.
1593 */
1594 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001595pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001596{
1597 vimmenu_T *mp;
1598 int idx = 0;
1599 exarg_T ea;
1600
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001601 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001602 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001603 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001604 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001605 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001606 break;
1607 }
1608}
1609
1610/*
1611 * Open the terminal version of the popup menu and don't return until it is
1612 * closed.
1613 */
1614 void
1615pum_show_popupmenu(vimmenu_T *menu)
1616{
1617 vimmenu_T *mp;
1618 int idx = 0;
1619 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001620# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001621 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001622# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001623 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001624
1625 pum_undisplay();
1626 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001627 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001628
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001629 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001630 if (menu_is_separator(mp->dname)
1631 || (mp->modes & mp->enabled & mode))
1632 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001633
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001634 // When there are only Terminal mode menus, using "popup Edit" results in
1635 // pum_size being zero.
1636 if (pum_size <= 0)
1637 {
zeertzjq276410e2023-05-07 21:59:33 +01001638 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001639 return;
1640 }
1641
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001642 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001643 if (array == NULL)
1644 return;
1645
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001646 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001647 {
1648 char_u *s = NULL;
1649
1650 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001651 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001652 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001653 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001654 s = mp->dname;
1655 if (s != NULL)
1656 {
1657 s = vim_strsave(s);
1658 if (s != NULL)
1659 array[idx++].pum_text = s;
1660 }
1661 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001662
1663 pum_array = array;
1664 pum_compute_size();
1665 pum_scrollbar = 0;
1666 pum_height = pum_size;
zeertzjq883018f2024-06-15 15:37:11 +02001667# ifdef FEAT_RIGHTLEFT
1668 pum_rl = curwin->w_p_rl;
1669# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001670 pum_position_at_mouse(20);
1671
1672 pum_selected = -1;
1673 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001674# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001675 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001676 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001677# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001678
1679 for (;;)
1680 {
1681 int c;
1682
1683 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001684 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001685 out_flush();
1686
1687 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001688
zeertzjq92a16782022-07-26 12:24:41 +01001689 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1690 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001691 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001692 break;
1693 else if (c == CAR || c == NL)
1694 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001695 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001696 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001697 break;
1698 }
1699 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1700 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001701 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001702 while (pum_selected > 0)
1703 {
1704 --pum_selected;
1705 if (*array[pum_selected].pum_text != NUL)
1706 break;
1707 }
1708 }
1709 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1710 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001711 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001712 while (pum_selected < pum_size - 1)
1713 {
1714 ++pum_selected;
1715 if (*array[pum_selected].pum_text != NUL)
1716 break;
1717 }
1718 }
1719 else if (c == K_RIGHTMOUSE)
1720 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001721 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001722 vungetc(c);
1723 break;
1724 }
1725 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1726 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001727 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001728 pum_select_mouse_pos();
1729 }
1730 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1731 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001732 // left mouse click: select clicked item, if any, and close;
1733 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001734 pum_select_mouse_pos();
1735 if (pum_selected >= 0)
1736 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001737 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001738 break;
1739 }
1740 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1741 break;
1742 }
1743 }
1744
Bram Moolenaar38455a92020-12-24 18:39:02 +01001745 for (idx = 0; idx < pum_size; ++idx)
1746 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001747 vim_free(array);
1748 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001749# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001750 p_bevalterm = save_bevalterm;
1751 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001752# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001753}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001754
1755 void
1756pum_make_popup(char_u *path_name, int use_mouse_pos)
1757{
1758 vimmenu_T *menu;
1759
1760 if (!use_mouse_pos)
1761 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001762 // Hack: set mouse position at the cursor so that the menu pops up
1763 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01001764 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
zeertzjq883018f2024-06-15 15:37:11 +02001765 mouse_col =
1766# ifdef FEAT_RIGHTLEFT
1767 curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
1768# endif
1769 curwin->w_wincol + curwin->w_wcol;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001770 }
1771
1772 menu = gui_find_menu(path_name);
1773 if (menu != NULL)
1774 pum_show_popupmenu(menu);
1775}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001776#endif