blob: da8241b915c82c617ecab841b12ebd02f5eed08e [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/*
zeertzjq63901e82024-06-16 16:51:25 +0200424 * Computes attributes of text on the popup menu.
425 * Returns attributes for every cell, or NULL if all attributes are the same.
glepnir40c1c332024-06-11 19:37:04 +0200426 */
zeertzjq63901e82024-06-16 16:51:25 +0200427 static int *
zeertzjq41008522024-07-27 13:21:49 +0200428pum_compute_text_attrs(char_u *text, hlf_T hlf, int user_hlattr)
glepnir40c1c332024-06-11 19:37:04 +0200429{
430 int i;
zeertzjqd8c93402024-06-17 18:25:32 +0200431 size_t leader_len;
zeertzjq63901e82024-06-16 16:51:25 +0200432 int char_cells;
glepnir40c1c332024-06-11 19:37:04 +0200433 int new_attr;
glepnir40c1c332024-06-11 19:37:04 +0200434 char_u *ptr = text;
zeertzjq63901e82024-06-16 16:51:25 +0200435 int cell_idx = 0;
glepnir40c1c332024-06-11 19:37:04 +0200436 garray_T *ga = NULL;
zeertzjq63901e82024-06-16 16:51:25 +0200437 int *attrs = NULL;
zeertzjqd8c93402024-06-17 18:25:32 +0200438 char_u *leader = NULL;
439 int in_fuzzy;
zeertzjq63901e82024-06-16 16:51:25 +0200440 int matched_start = FALSE;
441 int_u char_pos = 0;
glepnir40c1c332024-06-11 19:37:04 +0200442
zeertzjqd8c93402024-06-17 18:25:32 +0200443 if ((hlf != HLF_PSI && hlf != HLF_PNI)
zeertzjqafbe5352024-06-14 20:24:22 +0200444 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
445 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
zeertzjq63901e82024-06-16 16:51:25 +0200446 return NULL;
glepnir40c1c332024-06-11 19:37:04 +0200447
zeertzjqd8c93402024-06-17 18:25:32 +0200448 leader = State == MODE_CMDLINE ? cmdline_compl_pattern()
449 : ins_compl_leader();
450 if (leader == NULL || *leader == NUL)
451 return NULL;
452
zeertzjq63901e82024-06-16 16:51:25 +0200453 attrs = ALLOC_MULT(int, vim_strsize(text));
454 if (attrs == NULL)
455 return NULL;
456
zeertzjqd8c93402024-06-17 18:25:32 +0200457 in_fuzzy = State == MODE_CMDLINE ? cmdline_compl_is_fuzzy()
458 : (get_cot_flags() & COT_FUZZY) != 0;
459 leader_len = STRLEN(leader);
glepnir40c1c332024-06-11 19:37:04 +0200460
glepnir1c296022024-06-13 17:21:24 +0200461 if (in_fuzzy)
zeertzjq63901e82024-06-16 16:51:25 +0200462 ga = fuzzy_match_str_with_pos(text, leader);
463 else
glepnirf1891382024-06-17 18:35:25 +0200464 matched_start = MB_STRNICMP(text, leader, leader_len) == 0;
glepnir40c1c332024-06-11 19:37:04 +0200465
zeertzjq63901e82024-06-16 16:51:25 +0200466 while (*ptr != NUL)
glepnir40c1c332024-06-11 19:37:04 +0200467 {
zeertzjqafbe5352024-06-14 20:24:22 +0200468 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200469
zeertzjqafbe5352024-06-14 20:24:22 +0200470 if (ga != NULL)
471 {
472 // Handle fuzzy matching
473 for (i = 0; i < ga->ga_len; i++)
474 {
zeertzjq63901e82024-06-16 16:51:25 +0200475 if (char_pos == ((int_u *)ga->ga_data)[i])
zeertzjqafbe5352024-06-14 20:24:22 +0200476 {
477 new_attr = highlight_attr[hlf == HLF_PSI
478 ? HLF_PMSI : HLF_PMNI];
479 break;
480 }
481 }
482 }
zeertzjq63901e82024-06-16 16:51:25 +0200483 else if (matched_start && ptr < text + leader_len)
zeertzjqafbe5352024-06-14 20:24:22 +0200484 new_attr = highlight_attr[hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI];
glepnir40c1c332024-06-11 19:37:04 +0200485
zeertzjq41008522024-07-27 13:21:49 +0200486 if (user_hlattr > 0)
487 new_attr = hl_combine_attr(new_attr, user_hlattr);
glepnir8754efe2024-07-26 18:15:27 +0200488
zeertzjq63901e82024-06-16 16:51:25 +0200489 char_cells = mb_ptr2cells(ptr);
490 for (i = 0; i < char_cells; i++)
491 attrs[cell_idx + i] = new_attr;
492 cell_idx += char_cells;
493
494 MB_PTR_ADV(ptr);
495 char_pos++;
glepnir40c1c332024-06-11 19:37:04 +0200496 }
497
498 if (ga != NULL)
499 {
zeertzjqafbe5352024-06-14 20:24:22 +0200500 ga_clear(ga);
501 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200502 }
zeertzjq63901e82024-06-16 16:51:25 +0200503 return attrs;
504}
505
506/*
507 * Displays text on the popup menu with specific attributes.
508 */
509 static void
510pum_screen_puts_with_attrs(
511 int row,
512 int col,
513 int cells UNUSED,
514 char_u *text,
515 int textlen,
glepnir8754efe2024-07-26 18:15:27 +0200516 int *attrs)
zeertzjq63901e82024-06-16 16:51:25 +0200517{
518 int col_start = col;
519 char_u *ptr = text;
520 int char_len;
521 int attr;
522
523 // Render text with proper attributes
524 while (*ptr != NUL && ptr < text + textlen)
525 {
526 char_len = mb_ptr2len(ptr);
527#ifdef FEAT_RIGHTLEFT
528 if (pum_rl)
529 attr = attrs[col_start + cells - col - 1];
530 else
531#endif
532 attr = attrs[col - col_start];
zeertzjqd9be94c2024-07-14 10:20:20 +0200533 screen_puts_len(ptr, char_len, row, col, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200534 col += mb_ptr2cells(ptr);
535 ptr += char_len;
536 }
glepnir40c1c332024-06-11 19:37:04 +0200537}
538
539/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000540 * Redraw the popup menu, using "pum_first" and "pum_selected".
541 */
542 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100543pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000544{
545 int row = pum_row;
546 int col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000547 int attr_scroll = highlight_attr[HLF_PSB];
548 int attr_thumb = highlight_attr[HLF_PST];
zeertzjqafbe5352024-06-14 20:24:22 +0200549 hlf_T *hlfs; // array used for highlights
550 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000551 int attr;
552 int i;
553 int idx;
554 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000555 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000556 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000557 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100558 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000559 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000560 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000561
zeertzjqafbe5352024-06-14 20:24:22 +0200562 hlf_T hlfsNorm[3];
563 hlf_T hlfsSel[3];
Bram Moolenaare638acc2023-03-15 17:08:51 +0000564 // "word"
zeertzjqafbe5352024-06-14 20:24:22 +0200565 hlfsNorm[0] = HLF_PNI;
566 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000567 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200568 hlfsNorm[1] = HLF_PNK;
569 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000570 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200571 hlfsNorm[2] = HLF_PNX;
572 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000573
Bram Moolenaarae654382019-01-17 21:09:05 +0100574 if (call_update_screen)
575 {
576 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100577 // Do not redraw in pum_may_redraw() and don't draw in the area where
578 // the popup menu will be.
579 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100580 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100581 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100582 }
583
584 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100585 if (pum_first > pum_size - pum_height)
586 pum_first = pum_size - pum_height;
587
Bram Moolenaar4b779472005-10-04 09:12:31 +0000588 if (pum_scrollbar)
589 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100590 thumb_height = pum_height * pum_height / pum_size;
591 if (thumb_height == 0)
592 thumb_height = 1;
593 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000594 + (pum_size - pum_height) / 2)
595 / (pum_size - pum_height);
596 }
597
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100598#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200599 // The popup menu is drawn over popup menus with zindex under
600 // POPUPMENU_ZINDEX.
601 screen_zindex = POPUPMENU_ZINDEX;
602#endif
603
Bram Moolenaar4b779472005-10-04 09:12:31 +0000604 for (i = 0; i < pum_height; ++i)
605 {
606 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200607 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
608 hlf = hlfs[0]; // start with "word" highlight
609 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000610
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100611 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000612#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200613 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000614 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200615 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000616 screen_putchar(' ', row, pum_col + 1, attr);
617 }
618 else
619#endif
620 if (pum_col > 0)
621 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000622
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100623 // Display each entry, use two spaces for a Tab.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000624 // Do this 3 times:
625 // 0 - main text
626 // 1 - kind
627 // 2 - extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000628 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000629 totwidth = 0;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000630 for (round = 0; round < 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000631 {
zeertzjqafbe5352024-06-14 20:24:22 +0200632 hlf = hlfs[round];
633 attr = highlight_attr[hlf];
zeertzjq41008522024-07-27 13:21:49 +0200634 if (pum_array[idx].pum_user_hlattr > 0)
635 attr = hl_combine_attr(attr, pum_array[idx].pum_user_hlattr);
glepnir38f99a12024-08-23 18:31:06 +0200636 if (round == 1 && pum_array[idx].pum_user_kind_hlattr > 0)
637 attr = hl_combine_attr(attr, pum_array[idx].pum_user_kind_hlattr);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000638 width = 0;
639 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000640 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000641 {
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000642 case 0: p = pum_array[idx].pum_text; break;
643 case 1: p = pum_array[idx].pum_kind; break;
644 case 2: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000645 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000646 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100647 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000648 {
649 if (s == NULL)
650 s = p;
651 w = ptr2cells(p);
652 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
653 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100654 // Display the text that fits or comes before a Tab.
655 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000656 char_u *st;
zeertzjq63901e82024-06-16 16:51:25 +0200657 int *attrs;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000658 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000659
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100660 if (saved != NUL)
661 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000662 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100663 if (saved != NUL)
664 *p = saved;
zeertzjq63901e82024-06-16 16:51:25 +0200665
zeertzjq41008522024-07-27 13:21:49 +0200666 int user_hlattr = pum_array[idx].pum_user_hlattr;
667 attrs = pum_compute_text_attrs(st, hlf, user_hlattr);
zeertzjq63901e82024-06-16 16:51:25 +0200668
Bram Moolenaarabc97732007-08-08 20:49:37 +0000669#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200670 if (pum_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000671 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000672 if (st != NULL)
673 {
674 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000675
676 if (rt != NULL)
677 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100678 char_u *rt_start = rt;
zeertzjq63901e82024-06-16 16:51:25 +0200679 int cells;
Bram Moolenaard836bb92010-01-19 18:06:03 +0100680
zeertzjq63901e82024-06-16 16:51:25 +0200681 cells = vim_strsize(rt);
682 if (cells > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000683 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100684 do
685 {
zeertzjq63901e82024-06-16 16:51:25 +0200686 cells -= has_mbyte
687 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100688 MB_PTR_ADV(rt);
zeertzjq63901e82024-06-16 16:51:25 +0200689 } while (cells > pum_width);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100690
zeertzjq63901e82024-06-16 16:51:25 +0200691 if (cells < pum_width)
Bram Moolenaard836bb92010-01-19 18:06:03 +0100692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100693 // Most left character requires
694 // 2-cells but only 1 cell is
695 // available on screen. Put a
696 // '<' on the left of the pum
697 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100698 *(--rt) = '<';
zeertzjq63901e82024-06-16 16:51:25 +0200699 cells++;
Bram Moolenaard836bb92010-01-19 18:06:03 +0100700 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000701 }
zeertzjq63901e82024-06-16 16:51:25 +0200702
703 if (attrs == NULL)
704 screen_puts_len(rt, (int)STRLEN(rt),
705 row, col - cells + 1, attr);
706 else
707 pum_screen_puts_with_attrs(row,
708 col - cells + 1, cells, rt,
glepnir8754efe2024-07-26 18:15:27 +0200709 (int)STRLEN(rt), attrs);
zeertzjq63901e82024-06-16 16:51:25 +0200710
Bram Moolenaard836bb92010-01-19 18:06:03 +0100711 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000712 }
713 vim_free(st);
714 }
715 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000716 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000717 else
718#endif
719 {
720 if (st != NULL)
721 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100722 int size = (int)STRLEN(st);
723 int cells = (*mb_string2cells)(st, size);
724
725 // only draw the text that fits
726 while (size > 0
727 && col + cells > pum_width + pum_col)
728 {
729 --size;
730 if (has_mbyte)
731 {
732 size -= (*mb_head_off)(st, st + size);
733 cells -= (*mb_ptr2cells)(st + size);
734 }
735 else
736 --cells;
737 }
zeertzjq63901e82024-06-16 16:51:25 +0200738
739 if (attrs == NULL)
740 screen_puts_len(st, size, row, col, attr);
741 else
742 pum_screen_puts_with_attrs(row, col, cells,
glepnir8754efe2024-07-26 18:15:27 +0200743 st, size, attrs);
zeertzjq63901e82024-06-16 16:51:25 +0200744
Bram Moolenaarabc97732007-08-08 20:49:37 +0000745 vim_free(st);
746 }
747 col += width;
748 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000749
zeertzjq63901e82024-06-16 16:51:25 +0200750 vim_free(attrs);
751
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000752 if (*p != TAB)
753 break;
754
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100755 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000756#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200757 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000758 {
759 screen_puts_len((char_u *)" ", 2, row, col - 1,
760 attr);
761 col -= 2;
762 }
763 else
764#endif
765 {
766 screen_puts_len((char_u *)" ", 2, row, col, attr);
767 col += 2;
768 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000769 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100770 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000771 width = 0;
772 }
773 else
774 width += w;
775 }
776
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000777 if (round > 0)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000778 n = pum_kind_width + 1;
779 else
780 n = 1;
781
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100782 // Stop when there is nothing more to display.
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000783 if (round == 2
784 || (round == 1 && pum_array[idx].pum_extra == NULL)
785 || (round == 0 && pum_array[idx].pum_kind == NULL
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000786 && pum_array[idx].pum_extra == NULL)
787 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000788 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000789#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200790 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000791 {
792 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
793 col + 1, ' ', ' ', attr);
zeertzjqa2324372024-06-15 15:08:27 +0200794 col = pum_col - pum_base_width - n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000795 }
796 else
797#endif
798 {
799 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000800 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000801 col = pum_col + pum_base_width + n;
802 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000803 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000804 }
805
Bram Moolenaarabc97732007-08-08 20:49:37 +0000806#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200807 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000808 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
809 ' ', attr);
810 else
811#endif
812 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
813 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000814 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000815 {
816#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200817 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000818 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100819 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000820 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000821 else
822#endif
823 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100824 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000825 ? attr_thumb : attr_scroll);
826 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000827
828 ++row;
829 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200830
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100831#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200832 screen_zindex = 0;
833#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000834}
835
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100836#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200837/*
838 * Position the info popup relative to the popup menu item.
839 */
840 void
841pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200842{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100843 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200844 int row = pum_row;
845 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200846 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200847
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200848 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200849 if (Columns - col < 20 && Columns - col < pum_col)
850 {
851 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200852 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200853 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200854 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200855 }
856 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200857 wp->w_maxwidth = Columns - col + 1;
858 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200859 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
860 {
861 // option value overrules computed value
862 wp->w_maxwidth = wp->w_maxwidth_opt;
863 used_maxwidth_opt = TRUE;
864 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200865
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200866 row -= popup_top_extra(wp);
867 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200868 {
869 if (pum_row < pum_win_row)
870 {
871 // menu above cursor line, align with bottom
872 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200873 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200874 }
875 else
876 // menu below cursor line, align with top
877 row += 1;
878 }
879 else
880 // align with the selected item
881 row += pum_selected - pum_first + 1;
882
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100883 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200884 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100885 // The popup is not going to fit or will overlap with the cursor
886 // position, hide the popup.
887 wp->w_popup_flags |= POPF_HIDDEN;
888 else
889 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200890}
891#endif
892
Bram Moolenaar4b779472005-10-04 09:12:31 +0000893/*
894 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000895 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000896 * This may be repeated when the preview window is used:
897 * "repeat" == 0: open preview window normally
898 * "repeat" == 1: open preview window but don't set the size
899 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000900 * Returns TRUE when the window was resized and the location of the popup menu
901 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000902 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000903 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200904pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000905{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000906 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000907 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200908#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200909 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +0200910 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +0200911#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100912#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200913 int has_info = FALSE;
914#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000915
Bram Moolenaar4b779472005-10-04 09:12:31 +0000916 pum_selected = n;
917
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000918 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000919 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000920 if (pum_first > pum_selected - 4)
921 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100922 // scroll down; when we did a jump it's probably a PageUp then
923 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000924 if (pum_first > pum_selected - 2)
925 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000926 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000927 if (pum_first < 0)
928 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000929 else if (pum_first > pum_selected)
930 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000931 }
932 else
933 pum_first = pum_selected;
934 }
935 else if (pum_first < pum_selected - pum_height + 5)
936 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100937 // scroll up; when we did a jump it's probably a PageDown then
938 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000939 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000940 {
941 pum_first += pum_height - 2;
942 if (pum_first < pum_selected - pum_height + 1)
943 pum_first = pum_selected - pum_height + 1;
944 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000945 else
946 pum_first = pum_selected - pum_height + 1;
947 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000948
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100949 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000950 if (context > 3)
951 context = 3;
952 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000953 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000954 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000955 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100956 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000957 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000958 if (pum_first < 0)
959 pum_first = 0;
960 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000961 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000962 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100963 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000964 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000965 }
966 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200967 // adjust for the number of lines displayed
968 if (pum_first > pum_size - pum_height)
969 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000970
Bram Moolenaar4033c552017-09-16 20:54:51 +0200971#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000972 /*
973 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100974 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000975 * Skip this when tried twice already.
976 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000977 * NOTE: Be very careful not to sync undo!
978 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000979 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000980 && Rows > 10
981 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +0200982 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000983 {
984 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200985 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100986# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200987 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200988# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100989# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200990# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100991# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200992 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +0200993 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200994 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +0200995 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200996 use_popup = USEPOPUP_NORMAL;
997 else
998 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100999 if (use_popup != USEPOPUP_NONE)
1000 // don't use WinEnter or WinLeave autocommands for the info
1001 // popup
1002 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +02001003# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001004 // Open a preview window and set "curwin" to it.
1005 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001006 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +02001007 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
1008 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +02001009 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001010 // Prevent undo sync here, if an autocommand syncs undo weird
1011 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +01001012 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001013 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001014 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001015 if (RedrawingDisabled > 0)
1016 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001017 g_do_tagpreview = 0;
1018
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001019 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001020# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001021 || (curwin->w_popup_flags & POPF_INFO)
1022# endif
1023 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001024 {
mathew20f61d92023-08-26 18:11:02 +02001025 int res = OK;
1026
Bram Moolenaar50e53762016-10-27 14:49:15 +02001027 if (!resized
1028 && curbuf->b_nwindows == 1
1029 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02001030 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001031 && curbuf->b_p_bh[0] == 'w')
1032 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001033 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001034 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02001035 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001036 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001037 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001038 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001039 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001040 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001041 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001042 --no_u_sync;
1043 if (res == OK)
1044 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001045 // Edit a new, empty buffer. Set options for a "wipeout"
1046 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001047 set_option_value_give_err((char_u *)"swf",
1048 0L, NULL, OPT_LOCAL);
1049 set_option_value_give_err((char_u *)"bl",
1050 0L, NULL, OPT_LOCAL);
1051 set_option_value_give_err((char_u *)"bt",
1052 0L, (char_u *)"nofile", OPT_LOCAL);
1053 set_option_value_give_err((char_u *)"bh",
1054 0L, (char_u *)"wipe", OPT_LOCAL);
1055 set_option_value_give_err((char_u *)"diff",
1056 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001057 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001058 }
1059 if (res == OK)
1060 {
1061 char_u *p, *e;
1062 linenr_T lnum = 0;
1063
1064 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1065 {
1066 e = vim_strchr(p, '\n');
1067 if (e == NULL)
1068 {
1069 ml_append(lnum++, p, 0, FALSE);
1070 break;
1071 }
mathewc51fa7b2023-08-23 20:55:17 +02001072 *e = NUL;
1073 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1074 *e = '\n';
1075 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001076 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001077 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001078 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001079
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001080 // Increase the height of the preview window to show the
1081 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001082 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001083 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001084 if (lnum > p_pvh)
1085 lnum = p_pvh;
1086 if (curwin->w_height < lnum)
1087 {
1088 win_setheight((int)lnum);
1089 resized = TRUE;
1090 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001091 }
1092
1093 curbuf->b_changed = 0;
1094 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001095 if (pum_selected != prev_selected)
1096 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001097# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001098 curwin->w_firstline = 1;
1099# endif
1100 curwin->w_topline = 1;
1101 }
1102 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1103 curwin->w_topline = curbuf->b_ml.ml_line_count;
1104 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001105 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001106# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001107 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001108 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001109 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001110 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001111 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001112 }
1113# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001114 if ((curwin != curwin_save && win_valid(curwin_save))
1115 || (curtab != curtab_save
1116 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001117 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001118 int save_redr_status;
1119
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001120 if (curtab != curtab_save && valid_tabpage(curtab_save))
1121 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1122
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001123 // When the first completion is done and the preview
1124 // window is not resized, skip the preview window's
1125 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001126 if (ins_compl_active() && !resized)
1127 curwin->w_redr_status = FALSE;
1128
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001129 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001130 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001131 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001132
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001133 // When the preview window was resized we need to
1134 // update the view on the buffer. Only go back to
1135 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001136 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001137 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001138 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001139 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001140 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001141 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001142 update_topline();
1143 }
1144
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001145 // Update the screen before drawing the popup menu.
1146 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001147 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001148
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001149 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001150 // it causes flicker. When resizing we need to draw
1151 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001152 // Also do not redraw the status line of the original
1153 // current window here, to avoid it gets drawn with
1154 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001155 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001156 save_redr_status = curwin_save->w_redr_status;
1157 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001158 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001159 pum_pretend_not_visible = FALSE;
1160 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001161 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001162
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001163 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001164 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001165# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001166 win_T *wp = curwin;
1167# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001168 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001169 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001170 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001171# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001172 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1173 popup_hide(wp);
1174# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001175 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001177 // May need to update the screen again when there are
1178 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001179 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001180 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001181 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001182 pum_pretend_not_visible = FALSE;
1183 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001184 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001185 }
1186 }
1187 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001188# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001189 if (WIN_IS_POPUP(curwin))
1190 // can't keep focus in a popup window
1191 win_enter(firstwin, TRUE);
1192# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001193# ifdef FEAT_PROP_POPUP
1194 if (use_popup != USEPOPUP_NONE)
1195 unblock_autocmds();
1196# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001197 }
1198#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001199 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001200#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001201 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001202 // hide any popup info window
1203 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001204#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001205
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001206 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001207}
1208
1209/*
1210 * Undisplay the popup menu (later).
1211 */
1212 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001213pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001214{
1215 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001216 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001217 redraw_tabline = TRUE;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001218 if (pum_in_cmdline)
1219 {
1220 clear_cmdline = TRUE;
1221 pum_in_cmdline = FALSE;
1222 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001223 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001224#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001225 // hide any popup info window
1226 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001227#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001228}
1229
1230/*
1231 * Clear the popup menu. Currently only resets the offset to the first
1232 * displayed item.
1233 */
1234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001235pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001236{
1237 pum_first = 0;
1238}
1239
1240/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001241 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1242 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1243 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001244 */
1245 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001246pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001247{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001248 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001249}
1250
Bram Moolenaare3226be2005-12-18 22:10:00 +00001251/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001252 * Return TRUE if the popup can be redrawn in the same position.
1253 */
1254 static int
1255pum_in_same_position(void)
1256{
1257 return pum_window != curwin
1258 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1259 && pum_win_height == curwin->w_height
1260 && pum_win_col == curwin->w_wincol
1261 && pum_win_width == curwin->w_width);
1262}
1263
1264/*
1265 * Return TRUE when pum_may_redraw() will call pum_redraw().
1266 * This means that the pum area should not be overwritten to avoid flicker.
1267 */
1268 int
1269pum_redraw_in_same_position(void)
1270{
1271 if (!pum_visible() || pum_will_redraw)
1272 return FALSE; // nothing to do
1273
1274 return pum_in_same_position();
1275}
1276
1277/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001278 * Reposition the popup menu to adjust for window layout changes.
1279 */
1280 void
1281pum_may_redraw(void)
1282{
1283 pumitem_T *array = pum_array;
1284 int len = pum_size;
1285 int selected = pum_selected;
1286
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001287 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001288 return; // nothing to do
1289
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001290 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001291 {
1292 // window position didn't change, redraw in the same position
1293 pum_redraw();
1294 }
1295 else
1296 {
1297 int wcol = curwin->w_wcol;
1298
1299 // Window layout changed, recompute the position.
1300 // Use the remembered w_wcol value, the cursor may have moved when a
1301 // completion was inserted, but we want the menu in the same position.
1302 pum_undisplay();
1303 curwin->w_wcol = pum_win_wcol;
1304 curwin->w_valid |= VALID_WCOL;
1305 pum_display(array, len, selected);
1306 curwin->w_wcol = wcol;
1307 }
1308}
1309
1310/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001311 * Return the height of the popup menu, the number of entries visible.
1312 * Only valid when pum_visible() returns TRUE!
1313 */
1314 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001315pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316{
1317 return pum_height;
1318}
1319
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001320#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001321/*
1322 * Add size information about the pum to "dict".
1323 */
1324 void
1325pum_set_event_info(dict_T *dict)
1326{
1327 if (!pum_visible())
1328 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001329 (void)dict_add_number(dict, "height", pum_height);
1330 (void)dict_add_number(dict, "width", pum_width);
1331 (void)dict_add_number(dict, "row", pum_row);
1332 (void)dict_add_number(dict, "col", pum_col);
1333 (void)dict_add_number(dict, "size", pum_size);
1334 (void)dict_add_bool(dict, "scrollbar",
1335 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001336}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001337#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001338
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001339#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001340 static void
1341pum_position_at_mouse(int min_width)
1342{
zeertzjq761a4202024-07-04 17:26:37 +02001343 if (Rows - mouse_row > pum_size || Rows - mouse_row > mouse_row)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001344 {
zeertzjq761a4202024-07-04 17:26:37 +02001345 // Enough space below the mouse row,
1346 // or there is more space below the mouse row than above.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001347 pum_row = mouse_row + 1;
1348 if (pum_height > Rows - pum_row)
1349 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001350 if (pum_row + pum_height > cmdline_row)
1351 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001352 }
1353 else
1354 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001355 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001356 pum_row = mouse_row - pum_size;
1357 if (pum_row < 0)
1358 {
1359 pum_height += pum_row;
1360 pum_row = 0;
1361 }
1362 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001363
zeertzjq883018f2024-06-15 15:37:11 +02001364# ifdef FEAT_RIGHTLEFT
1365 if (pum_rl)
1366 {
1367 if (mouse_col + 1 >= pum_base_width
1368 || mouse_col + 1 > min_width)
1369 // Enough space to show at mouse column.
1370 pum_col = mouse_col;
1371 else
1372 // Not enough space, left align with window.
1373 pum_col = (pum_base_width > min_width
1374 ? min_width : pum_base_width) - 1;
1375 pum_width = pum_col + 1;
1376 }
1377 else
1378# endif
1379 {
1380 if (Columns - mouse_col >= pum_base_width
1381 || Columns - mouse_col > min_width)
1382 // Enough space to show at mouse column.
1383 pum_col = mouse_col;
1384 else
1385 // Not enough space, right align with window.
1386 pum_col = Columns - (pum_base_width > min_width
1387 ? min_width : pum_base_width);
1388 pum_width = Columns - pum_col;
1389 }
1390
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001391 if (pum_width > pum_base_width + 1)
1392 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001393
1394 // Do not redraw at cursor position.
1395 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001396}
1397
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001398#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001399
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001400#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001401static pumitem_T *balloon_array = NULL;
1402static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001403
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001404# define BALLOON_MIN_WIDTH 50
1405# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001406
Bram Moolenaar246fe032017-11-19 19:56:27 +01001407typedef struct {
1408 char_u *start;
1409 int bytelen;
1410 int cells;
1411 int indent;
1412} balpart_T;
1413
1414/*
1415 * Split a string into parts to display in the balloon.
1416 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1417 * strings and make a struct look good.
1418 * Resulting array is stored in "array" and returns the size of the array.
1419 */
1420 int
1421split_message(char_u *mesg, pumitem_T **array)
1422{
1423 garray_T ga;
1424 char_u *p;
1425 balpart_T *item;
1426 int quoted = FALSE;
1427 int height;
1428 int line;
1429 int item_idx;
1430 int indent = 0;
1431 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001432 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001433 int long_item_count = 0;
1434 int split_long_items = FALSE;
1435
1436 ga_init2(&ga, sizeof(balpart_T), 20);
1437 p = mesg;
1438
1439 while (*p != NUL)
1440 {
1441 if (ga_grow(&ga, 1) == FAIL)
1442 goto failed;
1443 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1444 item->start = p;
1445 item->indent = indent;
1446 item->cells = indent * 2;
1447 ++ga.ga_len;
1448 while (*p != NUL)
1449 {
1450 if (*p == '"')
1451 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001452 else if (*p == '\n')
1453 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 else if (*p == '\\' && p[1] != NUL)
1455 ++p;
1456 else if (!quoted)
1457 {
1458 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1459 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001460 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001461 if (*p == '{')
1462 ++indent;
1463 else if (*p == '}' && indent > 0)
1464 --indent;
1465 ++item->cells;
1466 p = skipwhite(p + 1);
1467 break;
1468 }
1469 }
1470 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001471 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001472 }
1473 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001474 if (*p == '\n')
1475 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001476 if (item->cells > max_cells)
1477 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001478 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001479 }
1480
1481 height = 2 + ga.ga_len;
1482
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001483 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001484 if (long_item_count > 0 && height + long_item_count <= max_height)
1485 {
1486 split_long_items = TRUE;
1487 height += long_item_count;
1488 }
1489
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001490 // Limit to half the window height, it has to fit above or below the mouse
1491 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001492 if (height > max_height)
1493 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001494 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001495 if (*array == NULL)
1496 goto failed;
1497
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001498 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001499 (*array)->pum_text = vim_strsave((char_u *)"");
1500 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1501
1502 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1503 {
1504 int skip;
1505 int thislen;
1506 int copylen;
1507 int ind;
1508 int cells;
1509
1510 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001511 if (item->bytelen == 0)
1512 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1513 else
1514 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001515 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001516 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1517 {
1518 cells = item->indent * 2;
1519 for (p = item->start + skip;
1520 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001521 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001522 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1523 break;
1524 thislen = p - (item->start + skip);
1525 }
1526 else
1527 thislen = item->bytelen;
1528
1529 // put indent at the start
1530 p = alloc(thislen + item->indent * 2 + 1);
1531 if (p == NULL)
1532 {
1533 for (line = 0; line <= height - 1; ++line)
1534 vim_free((*array)[line].pum_text);
1535 vim_free(*array);
1536 goto failed;
1537 }
1538 for (ind = 0; ind < item->indent * 2; ++ind)
1539 p[ind] = ' ';
1540
1541 // exclude spaces at the end of the string
1542 for (copylen = thislen; copylen > 0; --copylen)
1543 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001544 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001545
1546 vim_strncpy(p + ind, item->start + skip, copylen);
1547 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001548 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001549 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001550 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001551 }
1552 ga_clear(&ga);
1553 return height;
1554
1555failed:
1556 ga_clear(&ga);
1557 return 0;
1558}
1559
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001560 void
1561ui_remove_balloon(void)
1562{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001563 if (balloon_array == NULL)
1564 return;
1565
1566 pum_undisplay();
1567 while (balloon_arraysize > 0)
1568 vim_free(balloon_array[--balloon_arraysize].pum_text);
1569 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001570}
1571
1572/*
1573 * Terminal version of a balloon, uses the popup menu code.
1574 */
1575 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001576ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001577{
1578 ui_remove_balloon();
1579
Bram Moolenaar246fe032017-11-19 19:56:27 +01001580 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001581 {
1582 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001583 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001584 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001585 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001586 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001587 listitem_T *li;
1588 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001589
Bram Moolenaar246fe032017-11-19 19:56:27 +01001590 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001591 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001592 if (balloon_array == NULL)
1593 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001594 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001595 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001598
1599 balloon_array[idx].pum_text = vim_strsave(
1600 text == NULL ? (char_u *)"" : text);
1601 }
1602 }
1603 else
1604 balloon_arraysize = split_message(mesg, &balloon_array);
1605
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001606 if (balloon_arraysize <= 0)
1607 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001608
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001609 pum_array = balloon_array;
1610 pum_size = balloon_arraysize;
1611 pum_compute_size();
1612 pum_scrollbar = 0;
1613 pum_height = balloon_arraysize;
zeertzjq883018f2024-06-15 15:37:11 +02001614# ifdef FEAT_RIGHTLEFT
1615 pum_rl = curwin->w_p_rl;
1616# endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001617
1618 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1619 pum_selected = -1;
1620 pum_first = 0;
1621 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001622}
1623
1624/*
1625 * Called when the mouse moved, may remove any displayed balloon.
1626 */
1627 void
1628ui_may_remove_balloon(void)
1629{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001630 // For now: remove the balloon whenever the mouse moves to another screen
1631 // cell.
1632 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001633}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001634#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001635
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001636#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001637/*
1638 * Select the pum entry at the mouse position.
1639 */
1640 static void
1641pum_select_mouse_pos(void)
1642{
1643 int idx = mouse_row - pum_row;
1644
zeertzjq761a4202024-07-04 17:26:37 +02001645 if (idx < 0 || idx >= pum_height)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001646 pum_selected = -1;
1647 else if (*pum_array[idx].pum_text != NUL)
1648 pum_selected = idx;
1649}
1650
1651/*
1652 * Execute the currently selected popup menu item.
1653 */
1654 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001655pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001656{
1657 vimmenu_T *mp;
1658 int idx = 0;
1659 exarg_T ea;
1660
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001661 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001662 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001663 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001664 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001665 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001666 break;
1667 }
1668}
1669
1670/*
1671 * Open the terminal version of the popup menu and don't return until it is
1672 * closed.
1673 */
1674 void
1675pum_show_popupmenu(vimmenu_T *menu)
1676{
1677 vimmenu_T *mp;
1678 int idx = 0;
1679 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001680# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001681 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001682# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001683 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001684
1685 pum_undisplay();
1686 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001687 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001688
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001689 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001690 if (menu_is_separator(mp->dname)
1691 || (mp->modes & mp->enabled & mode))
1692 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001693
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001694 // When there are only Terminal mode menus, using "popup Edit" results in
1695 // pum_size being zero.
1696 if (pum_size <= 0)
1697 {
zeertzjq276410e2023-05-07 21:59:33 +01001698 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001699 return;
1700 }
1701
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001702 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001703 if (array == NULL)
1704 return;
1705
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001706 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001707 {
1708 char_u *s = NULL;
1709
1710 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001711 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001712 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001713 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001714 s = mp->dname;
1715 if (s != NULL)
1716 {
1717 s = vim_strsave(s);
1718 if (s != NULL)
1719 array[idx++].pum_text = s;
1720 }
1721 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001722
1723 pum_array = array;
1724 pum_compute_size();
1725 pum_scrollbar = 0;
1726 pum_height = pum_size;
zeertzjq883018f2024-06-15 15:37:11 +02001727# ifdef FEAT_RIGHTLEFT
1728 pum_rl = curwin->w_p_rl;
1729# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001730 pum_position_at_mouse(20);
1731
1732 pum_selected = -1;
1733 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001734# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001735 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001736 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001737# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001738
1739 for (;;)
1740 {
1741 int c;
1742
1743 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001744 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001745 out_flush();
1746
1747 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001748
zeertzjq92a16782022-07-26 12:24:41 +01001749 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1750 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001751 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001752 break;
1753 else if (c == CAR || c == NL)
1754 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001755 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001756 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001757 break;
1758 }
1759 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1760 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001761 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001762 while (pum_selected > 0)
1763 {
1764 --pum_selected;
1765 if (*array[pum_selected].pum_text != NUL)
1766 break;
1767 }
1768 }
1769 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001771 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001772 while (pum_selected < pum_size - 1)
1773 {
1774 ++pum_selected;
1775 if (*array[pum_selected].pum_text != NUL)
1776 break;
1777 }
1778 }
1779 else if (c == K_RIGHTMOUSE)
1780 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001781 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001782 vungetc(c);
1783 break;
1784 }
1785 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1786 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001787 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001788 pum_select_mouse_pos();
1789 }
1790 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1791 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001792 // left mouse click: select clicked item, if any, and close;
1793 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001794 pum_select_mouse_pos();
1795 if (pum_selected >= 0)
1796 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001797 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001798 break;
1799 }
1800 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1801 break;
1802 }
1803 }
1804
Bram Moolenaar38455a92020-12-24 18:39:02 +01001805 for (idx = 0; idx < pum_size; ++idx)
1806 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001807 vim_free(array);
1808 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001809# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001810 p_bevalterm = save_bevalterm;
1811 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001812# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001813}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001814
1815 void
1816pum_make_popup(char_u *path_name, int use_mouse_pos)
1817{
1818 vimmenu_T *menu;
1819
1820 if (!use_mouse_pos)
1821 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001822 // Hack: set mouse position at the cursor so that the menu pops up
1823 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01001824 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
zeertzjq883018f2024-06-15 15:37:11 +02001825 mouse_col =
1826# ifdef FEAT_RIGHTLEFT
1827 curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
1828# endif
1829 curwin->w_wincol + curwin->w_wcol;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001830 }
1831
1832 menu = gui_find_menu(path_name);
1833 if (menu != NULL)
1834 pum_show_popupmenu(menu);
1835}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001836#endif