blob: 443223de712ffd92ce2ec2d917a6a5cd3a3d9f1e [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;
glepnirc942f842024-12-13 12:13:23 +0100103 int cline_visible_offset;
104 int content_width;
105 int right_edge_col;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000106 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200107#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100108 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100109#endif
zeertzjq883018f2024-06-15 15:37:11 +0200110
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100111#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200112 pum_rl = State != MODE_CMDLINE && curwin->w_p_rl;
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100113#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000114
Bram Moolenaara5e66212017-09-29 22:42:33 +0200115 do
116 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100117 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200118 above_row = 0;
119 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000120
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100121 // Pretend the pum is already there to avoid that must_redraw is set
122 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200123 pum_array = (pumitem_T *)1;
124 validate_cursor_col();
125 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000126
Bram Moolenaar491ac282018-06-17 14:47:55 +0200127 // Remember the essential parts of the window position and size, so we
128 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200129 pum_window = curwin;
Bram Moolenaar24959102022-05-07 20:01:16 +0100130 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000131 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000132 pum_win_row = cmdline_row;
133 else
134 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200135 pum_win_height = curwin->w_height;
136 pum_win_col = curwin->w_wincol;
137 pum_win_wcol = curwin->w_wcol;
138 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000139
Bram Moolenaar4033c552017-09-16 20:54:51 +0200140#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200141 FOR_ALL_WINDOWS(pvwin)
142 if (pvwin->w_p_pvw)
143 break;
144 if (pvwin != NULL)
145 {
146 if (W_WINROW(pvwin) < W_WINROW(curwin))
147 above_row = W_WINROW(pvwin) + pvwin->w_height;
148 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
149 below_row = W_WINROW(pvwin);
150 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100151#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000152
Bram Moolenaara5e66212017-09-29 22:42:33 +0200153 /*
154 * Figure out the size and position of the pum.
155 */
glepnirc942f842024-12-13 12:13:23 +0100156 pum_height = MIN(size, 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 // Leave two lines of context if possible
glepnirc942f842024-12-13 12:13:23 +0100172 context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
Bram Moolenaara5e66212017-09-29 22:42:33 +0200173
Bram Moolenaar491ac282018-06-17 14:47:55 +0200174 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200175 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200176 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200177 pum_height = size;
178 }
179 else
180 {
181 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200182 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200183 }
184 if (p_ph > 0 && pum_height > p_ph)
185 {
186 pum_row += pum_height - p_ph;
187 pum_height = p_ph;
188 }
189 }
190 else
191 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100192 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200193
Bram Moolenaar24959102022-05-07 20:01:16 +0100194 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100195 // for cmdline pum, no need for context lines
196 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200197 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100198 {
199 // Leave two lines of context if possible
200 validate_cheight();
glepnirc942f842024-12-13 12:13:23 +0100201 cline_visible_offset = curwin->w_cline_row +
202 curwin->w_cline_height - curwin->w_wrow;
203 context_lines = MIN(3, cline_visible_offset);
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100204 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200205
Bram Moolenaar491ac282018-06-17 14:47:55 +0200206 pum_row = pum_win_row + context_lines;
glepnirc942f842024-12-13 12:13:23 +0100207 pum_height = MIN(below_row - pum_row, size);
Bram Moolenaara5e66212017-09-29 22:42:33 +0200208 if (p_ph > 0 && pum_height > p_ph)
209 pum_height = p_ph;
210 }
211
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100212 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200213 if (pum_height < 1 || (pum_height == 1 && size > 1))
214 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000215
Bram Moolenaar4033c552017-09-16 20:54:51 +0200216#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100217 // If there is a preview window above avoid drawing over it.
218 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000219 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100220 pum_row = above_row;
221 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000222 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200223#endif
224
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100225 pum_array = array;
226 pum_size = size;
227 pum_compute_size();
228 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000229
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100230 // Calculate column
Bram Moolenaar24959102022-05-07 20:01:16 +0100231 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000232 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000233 cursor_col = cmdline_compl_startcol();
234 else
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100235 {
236 // w_wcol includes virtual text "above"
237 int wcol = curwin->w_wcol % curwin->w_width;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000238#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200239 if (pum_rl)
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100240 cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
241 else
Bram Moolenaarabc97732007-08-08 20:49:37 +0000242#endif
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100243 cursor_col = curwin->w_wincol + wcol;
244 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000245
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100246 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200247 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000248 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200249 pum_scrollbar = 1;
250 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000251 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000252 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200253 pum_scrollbar = 0;
254
255 if (def_width < max_width)
256 def_width = max_width;
257
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100258 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000259#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200260 && !pum_rl)
261 || (pum_rl && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000262#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200263 ))
264 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100265 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100266 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000267
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100268 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200269#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200270 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200271 pum_width = pum_col - pum_scrollbar + 1;
272 else
273#endif
274 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000275
glepnirc942f842024-12-13 12:13:23 +0100276 content_width = max_width + pum_kind_width + pum_extra_width + 1;
277 if (pum_width > content_width && pum_width > p_pw)
278 // Reduce width to fit item
279 pum_width = MAX(content_width , p_pw);
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100280 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100281#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200282 && !pum_rl)
283 || (pum_rl && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100284 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100285#endif
286 ))
287 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100288 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100289#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200290 if (pum_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100291 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100292 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100293 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100294 if (pum_col >= Columns)
295 pum_col = Columns - 1;
296 }
zeertzjq883018f2024-06-15 15:37:11 +0200297 else if (!pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100298#endif
299 {
glepnirc942f842024-12-13 12:13:23 +0100300 right_edge_col = Columns - max_width - pum_scrollbar;
301 if (curwin->w_wincol > right_edge_col && max_width <= p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100302 // use full width to end of the screen
glepnirc942f842024-12-13 12:13:23 +0100303 pum_col = MAX(0, right_edge_col);
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304 }
305
306#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200307 if (pum_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100308 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100309 else
310#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100311 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100312
Bram Moolenaar42443c72018-02-10 18:28:52 +0100313 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100314 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100315 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100316#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200317 if (pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100318 {
319 if (pum_width > pum_col)
320 pum_width = pum_col;
321 }
322 else
323#endif
324 {
325 if (pum_width >= Columns - pum_col)
326 pum_width = Columns - pum_col - 1;
327 }
328 }
glepnirc942f842024-12-13 12:13:23 +0100329 else if (pum_width > content_width && pum_width > p_pw)
Christian Brabandt618c4d32024-12-13 12:30:20 +0100330 pum_width = MAX(content_width, p_pw);
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100331 }
332
Bram Moolenaara5e66212017-09-29 22:42:33 +0200333 }
334 else if (Columns < def_width)
335 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100336 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200337#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200338 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200339 pum_col = Columns - 1;
340 else
341#endif
342 pum_col = 0;
343 pum_width = Columns - 1;
344 }
345 else
346 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100347 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100348 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200349#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200350 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200351 pum_col = max_width - 1;
352 else
353#endif
354 pum_col = Columns - max_width;
355 pum_width = max_width - pum_scrollbar;
356 }
357
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100358 // Set selected item and redraw. If the window size changed need to
359 // redo the positioning. Limit this to two times, when there is not
360 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200361 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100362
363 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000364}
365
366/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100367 * Set a flag that when pum_redraw() is called it first calls update_screen().
368 * This will avoid clearing and redrawing the popup menu, prevent flicker.
369 */
370 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000371pum_call_update_screen(void)
Bram Moolenaarae654382019-01-17 21:09:05 +0100372{
373 call_update_screen = TRUE;
374
375 // Update the cursor position to be able to compute the popup menu
376 // position. The cursor line length may have changed because of the
377 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100378 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100379 validate_cursor();
380}
381
382/*
383 * Return TRUE if we are going to redraw the popup menu and the screen position
384 * "row"/"col" is under the popup menu.
385 */
386 int
Bakudankun65555002021-11-17 20:40:16 +0000387pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100388{
Bakudankun65555002021-11-17 20:40:16 +0000389 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100390 && row >= pum_row
391 && row < pum_row + pum_height
392 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200393 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100394}
395
396/*
zeertzjq63901e82024-06-16 16:51:25 +0200397 * Computes attributes of text on the popup menu.
398 * Returns attributes for every cell, or NULL if all attributes are the same.
glepnir40c1c332024-06-11 19:37:04 +0200399 */
zeertzjq63901e82024-06-16 16:51:25 +0200400 static int *
zeertzjq41008522024-07-27 13:21:49 +0200401pum_compute_text_attrs(char_u *text, hlf_T hlf, int user_hlattr)
glepnir40c1c332024-06-11 19:37:04 +0200402{
403 int i;
zeertzjqd8c93402024-06-17 18:25:32 +0200404 size_t leader_len;
zeertzjq63901e82024-06-16 16:51:25 +0200405 int char_cells;
glepnir40c1c332024-06-11 19:37:04 +0200406 int new_attr;
glepnir40c1c332024-06-11 19:37:04 +0200407 char_u *ptr = text;
zeertzjq63901e82024-06-16 16:51:25 +0200408 int cell_idx = 0;
glepnir40c1c332024-06-11 19:37:04 +0200409 garray_T *ga = NULL;
zeertzjq63901e82024-06-16 16:51:25 +0200410 int *attrs = NULL;
zeertzjqd8c93402024-06-17 18:25:32 +0200411 char_u *leader = NULL;
412 int in_fuzzy;
Girish Palya4ec46f32025-03-04 20:56:30 +0100413 int matched_len = -1;
zeertzjq63901e82024-06-16 16:51:25 +0200414 int_u char_pos = 0;
glepnir89a107e2024-12-22 15:16:40 +0100415 int is_select = FALSE;
glepnir40c1c332024-06-11 19:37:04 +0200416
zeertzjq3a0cc362025-01-13 07:27:43 +0100417 if (*text == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
zeertzjqafbe5352024-06-14 20:24:22 +0200418 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
419 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
zeertzjq63901e82024-06-16 16:51:25 +0200420 return NULL;
glepnir40c1c332024-06-11 19:37:04 +0200421
glepnir89a107e2024-12-22 15:16:40 +0100422 is_select = hlf == HLF_PSI;
zeertzjqd8c93402024-06-17 18:25:32 +0200423 leader = State == MODE_CMDLINE ? cmdline_compl_pattern()
424 : ins_compl_leader();
425 if (leader == NULL || *leader == NUL)
426 return NULL;
427
zeertzjq63901e82024-06-16 16:51:25 +0200428 attrs = ALLOC_MULT(int, vim_strsize(text));
429 if (attrs == NULL)
430 return NULL;
431
zeertzjqd8c93402024-06-17 18:25:32 +0200432 in_fuzzy = State == MODE_CMDLINE ? cmdline_compl_is_fuzzy()
433 : (get_cot_flags() & COT_FUZZY) != 0;
434 leader_len = STRLEN(leader);
glepnir40c1c332024-06-11 19:37:04 +0200435
glepnir1c296022024-06-13 17:21:24 +0200436 if (in_fuzzy)
zeertzjq63901e82024-06-16 16:51:25 +0200437 ga = fuzzy_match_str_with_pos(text, leader);
glepnir40c1c332024-06-11 19:37:04 +0200438
zeertzjq63901e82024-06-16 16:51:25 +0200439 while (*ptr != NUL)
glepnir40c1c332024-06-11 19:37:04 +0200440 {
zeertzjqafbe5352024-06-14 20:24:22 +0200441 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200442
zeertzjqafbe5352024-06-14 20:24:22 +0200443 if (ga != NULL)
444 {
445 // Handle fuzzy matching
446 for (i = 0; i < ga->ga_len; i++)
447 {
zeertzjq63901e82024-06-16 16:51:25 +0200448 if (char_pos == ((int_u *)ga->ga_data)[i])
zeertzjqafbe5352024-06-14 20:24:22 +0200449 {
glepnir89a107e2024-12-22 15:16:40 +0100450 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
glepnir9eff3ee2025-01-11 16:47:34 +0100451 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
zeertzjqafbe5352024-06-14 20:24:22 +0200452 break;
453 }
454 }
455 }
Girish Palya4ec46f32025-03-04 20:56:30 +0100456 else
glepnir9eff3ee2025-01-11 16:47:34 +0100457 {
Girish Palya4ec46f32025-03-04 20:56:30 +0100458 if (matched_len < 0 && MB_STRNICMP(ptr, leader, leader_len) == 0)
459 matched_len = leader_len;
460 if (matched_len > 0)
461 {
462 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
463 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
464 matched_len--;
465 }
glepnir9eff3ee2025-01-11 16:47:34 +0100466 }
glepnir40c1c332024-06-11 19:37:04 +0200467
glepnir9eff3ee2025-01-11 16:47:34 +0100468 new_attr = hl_combine_attr(highlight_attr[HLF_PNI], new_attr);
zeertzjq41008522024-07-27 13:21:49 +0200469 if (user_hlattr > 0)
470 new_attr = hl_combine_attr(new_attr, user_hlattr);
glepnir8754efe2024-07-26 18:15:27 +0200471
zeertzjq63901e82024-06-16 16:51:25 +0200472 char_cells = mb_ptr2cells(ptr);
473 for (i = 0; i < char_cells; i++)
474 attrs[cell_idx + i] = new_attr;
475 cell_idx += char_cells;
476
477 MB_PTR_ADV(ptr);
478 char_pos++;
glepnir40c1c332024-06-11 19:37:04 +0200479 }
480
481 if (ga != NULL)
482 {
zeertzjqafbe5352024-06-14 20:24:22 +0200483 ga_clear(ga);
484 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200485 }
zeertzjq63901e82024-06-16 16:51:25 +0200486 return attrs;
487}
488
489/*
490 * Displays text on the popup menu with specific attributes.
491 */
492 static void
493pum_screen_puts_with_attrs(
494 int row,
495 int col,
496 int cells UNUSED,
497 char_u *text,
498 int textlen,
glepnir8754efe2024-07-26 18:15:27 +0200499 int *attrs)
zeertzjq63901e82024-06-16 16:51:25 +0200500{
501 int col_start = col;
502 char_u *ptr = text;
503 int char_len;
504 int attr;
505
506 // Render text with proper attributes
507 while (*ptr != NUL && ptr < text + textlen)
508 {
509 char_len = mb_ptr2len(ptr);
510#ifdef FEAT_RIGHTLEFT
511 if (pum_rl)
512 attr = attrs[col_start + cells - col - 1];
513 else
514#endif
515 attr = attrs[col - col_start];
zeertzjqd9be94c2024-07-14 10:20:20 +0200516 screen_puts_len(ptr, char_len, row, col, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200517 col += mb_ptr2cells(ptr);
518 ptr += char_len;
519 }
glepnir40c1c332024-06-11 19:37:04 +0200520}
521
glepnir6a89c942024-10-01 20:32:12 +0200522
523 static inline void
524pum_align_order(int *order)
525{
526 int is_default = cia_flags == 0;
527 order[0] = is_default ? CPT_ABBR : cia_flags / 100;
528 order[1] = is_default ? CPT_KIND : (cia_flags / 10) % 10;
529 order[2] = is_default ? CPT_MENU : cia_flags % 10;
530}
531
532 static inline char_u *
533pum_get_item(int index, int type)
534{
535 switch(type)
536 {
537 case CPT_ABBR: return pum_array[index].pum_text;
538 case CPT_KIND: return pum_array[index].pum_kind;
539 case CPT_MENU: return pum_array[index].pum_extra;
540 }
541 return NULL;
542}
543
glepnir89a107e2024-12-22 15:16:40 +0100544 static inline int
545pum_user_attr_combine(int idx, int type, int attr)
546{
547 int user_attr[] = {
548 pum_array[idx].pum_user_abbr_hlattr,
549 pum_array[idx].pum_user_kind_hlattr,
550 };
551
552 return user_attr[type] > 0 ? hl_combine_attr(attr, user_attr[type]) : attr;
553}
554
glepnir40c1c332024-06-11 19:37:04 +0200555/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000556 * Redraw the popup menu, using "pum_first" and "pum_selected".
557 */
558 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100559pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000560{
561 int row = pum_row;
562 int col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000563 int attr_scroll = highlight_attr[HLF_PSB];
564 int attr_thumb = highlight_attr[HLF_PST];
zeertzjqafbe5352024-06-14 20:24:22 +0200565 hlf_T *hlfs; // array used for highlights
566 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000567 int attr;
glepnir6a89c942024-10-01 20:32:12 +0200568 int i, j;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000569 int idx;
570 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000571 char_u *p = NULL;
glepnir6a89c942024-10-01 20:32:12 +0200572 int totwidth, width, w; // total-width item-width char-width
Bram Moolenaar4b779472005-10-04 09:12:31 +0000573 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100574 int thumb_height = 1;
glepnir6a89c942024-10-01 20:32:12 +0200575 int item_type;
576 int order[3];
577 int next_isempty = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000578 int n;
glepnir6a89c942024-10-01 20:32:12 +0200579 int items_width_array[3] = { pum_base_width, pum_kind_width,
580 pum_extra_width };
581 int basic_width; // first item width
582 int last_isabbr = FALSE;
glepnir0fe17f82024-10-08 22:26:44 +0200583 int orig_attr = -1;
glepnir89a107e2024-12-22 15:16:40 +0100584 int scroll_range = pum_size - pum_height;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000585
zeertzjqafbe5352024-06-14 20:24:22 +0200586 hlf_T hlfsNorm[3];
587 hlf_T hlfsSel[3];
glepnir6a89c942024-10-01 20:32:12 +0200588 // "word"/"abbr"
zeertzjqafbe5352024-06-14 20:24:22 +0200589 hlfsNorm[0] = HLF_PNI;
590 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000591 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200592 hlfsNorm[1] = HLF_PNK;
593 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000594 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200595 hlfsNorm[2] = HLF_PNX;
596 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000597
Bram Moolenaarae654382019-01-17 21:09:05 +0100598 if (call_update_screen)
599 {
600 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100601 // Do not redraw in pum_may_redraw() and don't draw in the area where
602 // the popup menu will be.
603 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100604 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100605 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100606 }
607
608 // never display more than we have
glepnir89a107e2024-12-22 15:16:40 +0100609 pum_first = MIN(pum_first, scroll_range);
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100610
Bram Moolenaar4b779472005-10-04 09:12:31 +0000611 if (pum_scrollbar)
612 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100613 thumb_height = pum_height * pum_height / pum_size;
614 if (thumb_height == 0)
615 thumb_height = 1;
616 thumb_pos = (pum_first * (pum_height - thumb_height)
glepnir89a107e2024-12-22 15:16:40 +0100617 + scroll_range / 2) / scroll_range;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000618 }
619
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100620#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200621 // The popup menu is drawn over popup menus with zindex under
622 // POPUPMENU_ZINDEX.
623 screen_zindex = POPUPMENU_ZINDEX;
624#endif
625
Bram Moolenaar4b779472005-10-04 09:12:31 +0000626 for (i = 0; i < pum_height; ++i)
627 {
628 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200629 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
630 hlf = hlfs[0]; // start with "word" highlight
631 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000632
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100633 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000634#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200635 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000636 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200637 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000638 screen_putchar(' ', row, pum_col + 1, attr);
639 }
640 else
641#endif
642 if (pum_col > 0)
643 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000644
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100645 // Display each entry, use two spaces for a Tab.
glepnir6a89c942024-10-01 20:32:12 +0200646 // Do this 3 times and order from p_cia
Bram Moolenaar4b779472005-10-04 09:12:31 +0000647 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000648 totwidth = 0;
glepnir6a89c942024-10-01 20:32:12 +0200649 pum_align_order(order);
650 basic_width = items_width_array[order[0]];
651 last_isabbr = order[2] == CPT_ABBR;
652 for (j = 0; j < 3; ++j)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000653 {
glepnir6a89c942024-10-01 20:32:12 +0200654 item_type = order[j];
655 hlf = hlfs[item_type];
zeertzjqafbe5352024-06-14 20:24:22 +0200656 attr = highlight_attr[hlf];
glepnir0fe17f82024-10-08 22:26:44 +0200657 orig_attr = attr;
glepnir89a107e2024-12-22 15:16:40 +0100658 if (item_type < 2) // try combine attr with user custom
659 attr = pum_user_attr_combine(idx, item_type, attr);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000660 width = 0;
661 s = NULL;
glepnir6a89c942024-10-01 20:32:12 +0200662 p = pum_get_item(idx, item_type);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000663 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100664 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000665 {
666 if (s == NULL)
667 s = p;
668 w = ptr2cells(p);
zeertzjq3a0cc362025-01-13 07:27:43 +0100669 if (*p != NUL && *p != TAB && totwidth + w <= pum_width)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000670 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100671 width += w;
672 continue;
673 }
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000674
zeertzjq3a0cc362025-01-13 07:27:43 +0100675 // Display the text that fits or comes before a Tab.
676 // First convert it to printable characters.
677 char_u *st;
678 int *attrs = NULL;
679 int saved = *p;
zeertzjq63901e82024-06-16 16:51:25 +0200680
zeertzjq3a0cc362025-01-13 07:27:43 +0100681 if (saved != NUL)
682 *p = NUL;
683 st = transstr(s);
684 if (saved != NUL)
685 *p = saved;
686
687 if (item_type == CPT_ABBR)
688 attrs = pum_compute_text_attrs(st, hlf,
689 pum_array[idx].pum_user_abbr_hlattr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000690#ifdef FEAT_RIGHTLEFT
zeertzjq3a0cc362025-01-13 07:27:43 +0100691 if (pum_rl)
692 {
693 if (st != NULL)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000694 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100695 char_u *rt = reverse_text(st);
696
697 if (rt != NULL)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000698 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100699 char_u *rt_start = rt;
700 int cells;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000701
zeertzjq3a0cc362025-01-13 07:27:43 +0100702 cells = vim_strsize(rt);
703 if (cells > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000704 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100705 do
Bram Moolenaarabc97732007-08-08 20:49:37 +0000706 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100707 cells -= has_mbyte
zeertzjq63901e82024-06-16 16:51:25 +0200708 ? (*mb_ptr2cells)(rt) : 1;
zeertzjq3a0cc362025-01-13 07:27:43 +0100709 MB_PTR_ADV(rt);
710 } while (cells > pum_width);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100711
zeertzjq3a0cc362025-01-13 07:27:43 +0100712 if (cells < pum_width)
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100713 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100714 // Most left character requires 2-cells
715 // but only 1 cell is available on
716 // screen. Put a '<' on the left of
717 // the pum item.
718 *(--rt) = '<';
719 cells++;
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100720 }
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100721 }
zeertzjq63901e82024-06-16 16:51:25 +0200722
723 if (attrs == NULL)
zeertzjq3a0cc362025-01-13 07:27:43 +0100724 screen_puts_len(rt, (int)STRLEN(rt), row,
725 col - cells + 1, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200726 else
zeertzjq3a0cc362025-01-13 07:27:43 +0100727 pum_screen_puts_with_attrs(row,
728 col - cells + 1, cells, rt,
729 (int)STRLEN(rt), attrs);
zeertzjq63901e82024-06-16 16:51:25 +0200730
zeertzjq3a0cc362025-01-13 07:27:43 +0100731 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000732 }
zeertzjq3a0cc362025-01-13 07:27:43 +0100733 vim_free(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000734 }
zeertzjq3a0cc362025-01-13 07:27:43 +0100735 col -= width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000736 }
737 else
zeertzjq3a0cc362025-01-13 07:27:43 +0100738#endif
739 {
740 if (st != NULL)
741 {
742 int size = (int)STRLEN(st);
743 int cells = (*mb_string2cells)(st, size);
744
745 // only draw the text that fits
746 while (size > 0
747 && col + cells > pum_width + pum_col)
748 {
749 --size;
750 if (has_mbyte)
751 {
752 size -= (*mb_head_off)(st, st + size);
753 cells -= (*mb_ptr2cells)(st + size);
754 }
755 else
756 --cells;
757 }
758
759 if (attrs == NULL)
760 screen_puts_len(st, size, row, col, attr);
761 else
762 pum_screen_puts_with_attrs(row, col, cells,
763 st, size, attrs);
764
765 vim_free(st);
766 }
767 col += width;
768 }
769
770 if (attrs != NULL)
771 VIM_CLEAR(attrs);
772
773 if (*p != TAB)
774 break;
775
776 // Display two spaces for a Tab.
777#ifdef FEAT_RIGHTLEFT
778 if (pum_rl)
779 {
780 screen_puts_len((char_u *)" ", 2, row, col - 1, attr);
781 col -= 2;
782 }
783 else
784#endif
785 {
786 screen_puts_len((char_u *)" ", 2, row, col, attr);
787 col += 2;
788 }
789 totwidth += 2;
790 s = NULL; // start text at next char
791 width = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000792 }
793
glepnir6a89c942024-10-01 20:32:12 +0200794 if (j > 0)
795 n = items_width_array[order[1]] + (last_isabbr ? 0 : 1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000796 else
glepnir6a89c942024-10-01 20:32:12 +0200797 n = order[j] == CPT_ABBR ? 1 : 0;
798
799 if (j + 1 < 3)
800 next_isempty = pum_get_item(idx, order[j + 1]) == NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000801
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100802 // Stop when there is nothing more to display.
glepnir6a89c942024-10-01 20:32:12 +0200803 if (j == 2
804 || (next_isempty && (j == 1 || (j == 0
805 && pum_get_item(idx, order[j + 2]) == NULL)))
glepnira6d9e3c2024-10-03 11:01:19 +0200806 || basic_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000807 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000808#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200809 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000810 {
glepnir6a89c942024-10-01 20:32:12 +0200811 screen_fill(row, row + 1, pum_col - basic_width - n + 1,
glepnirc942f842024-12-13 12:13:23 +0100812 col + 1, ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +0200813 col = pum_col - basic_width - n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000814 }
815 else
816#endif
817 {
glepnir6a89c942024-10-01 20:32:12 +0200818 screen_fill(row, row + 1, col, pum_col + basic_width + n,
glepnirc942f842024-12-13 12:13:23 +0100819 ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +0200820 col = pum_col + basic_width + n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000821 }
glepnir6a89c942024-10-01 20:32:12 +0200822 totwidth = basic_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000823 }
824
Bram Moolenaarabc97732007-08-08 20:49:37 +0000825#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200826 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000827 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
glepnirc942f842024-12-13 12:13:23 +0100828 ' ', orig_attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000829 else
830#endif
831 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
glepnirc942f842024-12-13 12:13:23 +0100832 orig_attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000833 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000834 {
835#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200836 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000837 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100838 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000839 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000840 else
841#endif
842 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100843 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000844 ? attr_thumb : attr_scroll);
845 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000846
847 ++row;
848 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200849
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100850#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200851 screen_zindex = 0;
852#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000853}
854
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100855#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200856/*
857 * Position the info popup relative to the popup menu item.
858 */
859 void
860pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200861{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100862 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200863 int row = pum_row;
864 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200865 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200866
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200867 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200868 if (Columns - col < 20 && Columns - col < pum_col)
869 {
870 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200871 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200872 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200873 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200874 }
875 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200876 wp->w_maxwidth = Columns - col + 1;
877 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200878 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
879 {
880 // option value overrules computed value
881 wp->w_maxwidth = wp->w_maxwidth_opt;
882 used_maxwidth_opt = TRUE;
883 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200884
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200885 row -= popup_top_extra(wp);
886 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200887 {
888 if (pum_row < pum_win_row)
889 {
890 // menu above cursor line, align with bottom
891 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200892 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200893 }
894 else
895 // menu below cursor line, align with top
896 row += 1;
897 }
898 else
899 // align with the selected item
900 row += pum_selected - pum_first + 1;
901
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100902 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200903 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100904 // The popup is not going to fit or will overlap with the cursor
905 // position, hide the popup.
906 wp->w_popup_flags |= POPF_HIDDEN;
907 else
908 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200909}
910#endif
911
Bram Moolenaar4b779472005-10-04 09:12:31 +0000912/*
913 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000914 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000915 * This may be repeated when the preview window is used:
916 * "repeat" == 0: open preview window normally
917 * "repeat" == 1: open preview window but don't set the size
918 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000919 * Returns TRUE when the window was resized and the location of the popup menu
920 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000921 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000922 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200923pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000924{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000925 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000926 int context = pum_height / 2;
glepnir8d0bb6d2024-12-24 09:44:35 +0100927 int scroll_offset;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200928#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200929 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +0200930 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +0200931#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100932#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200933 int has_info = FALSE;
934#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000935
Bram Moolenaar4b779472005-10-04 09:12:31 +0000936 pum_selected = n;
glepnir8d0bb6d2024-12-24 09:44:35 +0100937 scroll_offset = pum_selected - pum_height;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000938
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000939 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000940 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000941 if (pum_first > pum_selected - 4)
942 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100943 // scroll down; when we did a jump it's probably a PageUp then
944 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000945 if (pum_first > pum_selected - 2)
946 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000947 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000948 if (pum_first < 0)
949 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000950 else if (pum_first > pum_selected)
951 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000952 }
953 else
954 pum_first = pum_selected;
955 }
glepnir89a107e2024-12-22 15:16:40 +0100956 else if (pum_first < scroll_offset + 5)
Bram Moolenaare3226be2005-12-18 22:10:00 +0000957 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100958 // scroll up; when we did a jump it's probably a PageDown then
959 // scroll a whole page
glepnir89a107e2024-12-22 15:16:40 +0100960 if (pum_first < scroll_offset + 3)
glepnir8d0bb6d2024-12-24 09:44:35 +0100961 pum_first = MAX(pum_first + pum_height - 2, scroll_offset + 1);
Bram Moolenaare3226be2005-12-18 22:10:00 +0000962 else
glepnir89a107e2024-12-22 15:16:40 +0100963 pum_first = scroll_offset + 1;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000964 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000965
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100966 // Give a few lines of context when possible.
glepnir89a107e2024-12-22 15:16:40 +0100967 context = MIN(context, 3);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000968 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000969 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000970 if (pum_first > pum_selected - context)
glepnir89a107e2024-12-22 15:16:40 +0100971 pum_first = MAX(pum_selected - context, 0); // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000972 else if (pum_first < pum_selected + context - pum_height + 1)
glepnir89a107e2024-12-22 15:16:40 +0100973 pum_first = pum_selected + context - pum_height + 1; // up
Bram Moolenaar4b779472005-10-04 09:12:31 +0000974 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200975 // adjust for the number of lines displayed
glepnirc942f842024-12-13 12:13:23 +0100976 pum_first = MIN(pum_first, pum_size - pum_height);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000977
Bram Moolenaar4033c552017-09-16 20:54:51 +0200978#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000979 /*
980 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100981 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000982 * Skip this when tried twice already.
983 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000984 * NOTE: Be very careful not to sync undo!
985 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000986 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000987 && Rows > 10
988 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +0200989 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000990 {
991 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200992 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100993# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200994 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200995# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100996# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200997# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100998# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200999 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +02001000 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001001 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +02001002 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001003 use_popup = USEPOPUP_NORMAL;
1004 else
1005 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001006 if (use_popup != USEPOPUP_NONE)
1007 // don't use WinEnter or WinLeave autocommands for the info
1008 // popup
1009 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +02001010# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001011 // Open a preview window and set "curwin" to it.
1012 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001013 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +02001014 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
1015 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +02001016 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001017 // Prevent undo sync here, if an autocommand syncs undo weird
1018 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +01001019 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001020 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001021 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001022 if (RedrawingDisabled > 0)
1023 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001024 g_do_tagpreview = 0;
1025
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001026 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001027# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001028 || (curwin->w_popup_flags & POPF_INFO)
1029# endif
1030 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001031 {
mathew20f61d92023-08-26 18:11:02 +02001032 int res = OK;
1033
Bram Moolenaar50e53762016-10-27 14:49:15 +02001034 if (!resized
1035 && curbuf->b_nwindows == 1
1036 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02001037 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001038 && curbuf->b_p_bh[0] == 'w')
1039 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001040 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001041 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02001042 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001043 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001044 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001045 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001046 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001047 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001048 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001049 --no_u_sync;
1050 if (res == OK)
1051 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001052 // Edit a new, empty buffer. Set options for a "wipeout"
1053 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001054 set_option_value_give_err((char_u *)"swf",
1055 0L, NULL, OPT_LOCAL);
1056 set_option_value_give_err((char_u *)"bl",
1057 0L, NULL, OPT_LOCAL);
1058 set_option_value_give_err((char_u *)"bt",
1059 0L, (char_u *)"nofile", OPT_LOCAL);
1060 set_option_value_give_err((char_u *)"bh",
1061 0L, (char_u *)"wipe", OPT_LOCAL);
1062 set_option_value_give_err((char_u *)"diff",
1063 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001064 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001065 }
1066 if (res == OK)
1067 {
1068 char_u *p, *e;
1069 linenr_T lnum = 0;
1070
1071 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1072 {
1073 e = vim_strchr(p, '\n');
1074 if (e == NULL)
1075 {
1076 ml_append(lnum++, p, 0, FALSE);
1077 break;
1078 }
mathewc51fa7b2023-08-23 20:55:17 +02001079 *e = NUL;
1080 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1081 *e = '\n';
1082 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001083 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001084 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001085 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001086
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001087 // Increase the height of the preview window to show the
1088 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001089 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001090 {
glepnir89a107e2024-12-22 15:16:40 +01001091 lnum = MIN(lnum, p_pvh);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001092 if (curwin->w_height < lnum)
1093 {
1094 win_setheight((int)lnum);
1095 resized = TRUE;
1096 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001097 }
1098
1099 curbuf->b_changed = 0;
1100 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001101 if (pum_selected != prev_selected)
1102 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001103# ifdef FEAT_PROP_POPUP
Girish Palya12b1eb52025-02-24 21:39:42 +01001104 curwin->w_firstline = 0;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001105# endif
1106 curwin->w_topline = 1;
1107 }
1108 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1109 curwin->w_topline = curbuf->b_ml.ml_line_count;
1110 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001111 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001112# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001113 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001114 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001115 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001116 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001117 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001118 }
1119# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001120 if ((curwin != curwin_save && win_valid(curwin_save))
1121 || (curtab != curtab_save
1122 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001123 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001124 int save_redr_status;
1125
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001126 if (curtab != curtab_save && valid_tabpage(curtab_save))
1127 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1128
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001129 // When the first completion is done and the preview
1130 // window is not resized, skip the preview window's
1131 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001132 if (ins_compl_active() && !resized)
1133 curwin->w_redr_status = FALSE;
1134
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001135 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001136 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001137 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001138
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001139 // When the preview window was resized we need to
1140 // update the view on the buffer. Only go back to
1141 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001142 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001143 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001144 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001145 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001146 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001147 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001148 update_topline();
1149 }
1150
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001151 // Update the screen before drawing the popup menu.
1152 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001153 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001154
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001155 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001156 // it causes flicker. When resizing we need to draw
1157 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001158 // Also do not redraw the status line of the original
1159 // current window here, to avoid it gets drawn with
1160 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001161 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001162 save_redr_status = curwin_save->w_redr_status;
1163 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001164 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001165 pum_pretend_not_visible = FALSE;
1166 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001167 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001168
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001169 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001170 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001171# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001172 win_T *wp = curwin;
1173# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001174 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001175 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001176 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001177# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001178 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1179 popup_hide(wp);
1180# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001181 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001182
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001183 // May need to update the screen again when there are
1184 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001185 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001186 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001187 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001188 pum_pretend_not_visible = FALSE;
1189 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001190 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001191 }
1192 }
1193 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001194# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001195 if (WIN_IS_POPUP(curwin))
1196 // can't keep focus in a popup window
1197 win_enter(firstwin, TRUE);
1198# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001199# ifdef FEAT_PROP_POPUP
1200 if (use_popup != USEPOPUP_NONE)
1201 unblock_autocmds();
1202# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001203 }
1204#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001205 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001206#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001207 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001208 // hide any popup info window
1209 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001210#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001211
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001212 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001213}
1214
1215/*
1216 * Undisplay the popup menu (later).
1217 */
1218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001219pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001220{
1221 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001222 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001223 redraw_tabline = TRUE;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001224 if (pum_in_cmdline)
1225 {
1226 clear_cmdline = TRUE;
1227 pum_in_cmdline = FALSE;
1228 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001229 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001230#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001231 // hide any popup info window
1232 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001233#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001234}
1235
1236/*
1237 * Clear the popup menu. Currently only resets the offset to the first
1238 * displayed item.
1239 */
1240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001241pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001242{
1243 pum_first = 0;
1244}
1245
1246/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001247 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1248 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1249 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001250 */
1251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001252pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001253{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001254 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001255}
1256
Bram Moolenaare3226be2005-12-18 22:10:00 +00001257/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001258 * Return TRUE if the popup can be redrawn in the same position.
1259 */
1260 static int
1261pum_in_same_position(void)
1262{
1263 return pum_window != curwin
1264 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1265 && pum_win_height == curwin->w_height
1266 && pum_win_col == curwin->w_wincol
1267 && pum_win_width == curwin->w_width);
1268}
1269
1270/*
1271 * Return TRUE when pum_may_redraw() will call pum_redraw().
1272 * This means that the pum area should not be overwritten to avoid flicker.
1273 */
1274 int
1275pum_redraw_in_same_position(void)
1276{
1277 if (!pum_visible() || pum_will_redraw)
1278 return FALSE; // nothing to do
1279
1280 return pum_in_same_position();
1281}
1282
1283/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001284 * Reposition the popup menu to adjust for window layout changes.
1285 */
1286 void
1287pum_may_redraw(void)
1288{
1289 pumitem_T *array = pum_array;
1290 int len = pum_size;
1291 int selected = pum_selected;
1292
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001293 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001294 return; // nothing to do
1295
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001296 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001297 {
glepnirc942f842024-12-13 12:13:23 +01001298 pum_redraw(); // Redraw window in same position
Bram Moolenaar491ac282018-06-17 14:47:55 +02001299 }
1300 else
1301 {
1302 int wcol = curwin->w_wcol;
1303
1304 // Window layout changed, recompute the position.
1305 // Use the remembered w_wcol value, the cursor may have moved when a
1306 // completion was inserted, but we want the menu in the same position.
1307 pum_undisplay();
1308 curwin->w_wcol = pum_win_wcol;
1309 curwin->w_valid |= VALID_WCOL;
1310 pum_display(array, len, selected);
1311 curwin->w_wcol = wcol;
1312 }
1313}
1314
1315/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001316 * Return the height of the popup menu, the number of entries visible.
1317 * Only valid when pum_visible() returns TRUE!
1318 */
1319 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001320pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001321{
1322 return pum_height;
1323}
1324
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001325#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001326/*
1327 * Add size information about the pum to "dict".
1328 */
1329 void
1330pum_set_event_info(dict_T *dict)
1331{
1332 if (!pum_visible())
1333 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001334 (void)dict_add_number(dict, "height", pum_height);
1335 (void)dict_add_number(dict, "width", pum_width);
1336 (void)dict_add_number(dict, "row", pum_row);
1337 (void)dict_add_number(dict, "col", pum_col);
1338 (void)dict_add_number(dict, "size", pum_size);
1339 (void)dict_add_bool(dict, "scrollbar",
1340 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001341}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001342#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001343
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001344#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001345 static void
1346pum_position_at_mouse(int min_width)
1347{
zeertzjq761a4202024-07-04 17:26:37 +02001348 if (Rows - mouse_row > pum_size || Rows - mouse_row > mouse_row)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001349 {
zeertzjq761a4202024-07-04 17:26:37 +02001350 // Enough space below the mouse row,
1351 // or there is more space below the mouse row than above.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001352 pum_row = mouse_row + 1;
1353 if (pum_height > Rows - pum_row)
1354 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001355 if (pum_row + pum_height > cmdline_row)
1356 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001357 }
1358 else
1359 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001360 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001361 pum_row = mouse_row - pum_size;
1362 if (pum_row < 0)
1363 {
1364 pum_height += pum_row;
1365 pum_row = 0;
1366 }
1367 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001368
zeertzjq883018f2024-06-15 15:37:11 +02001369# ifdef FEAT_RIGHTLEFT
1370 if (pum_rl)
1371 {
1372 if (mouse_col + 1 >= pum_base_width
1373 || mouse_col + 1 > min_width)
1374 // Enough space to show at mouse column.
1375 pum_col = mouse_col;
1376 else
1377 // Not enough space, left align with window.
glepnirc942f842024-12-13 12:13:23 +01001378 pum_col = MIN(pum_base_width, min_width) - 1;
zeertzjq883018f2024-06-15 15:37:11 +02001379 pum_width = pum_col + 1;
1380 }
1381 else
1382# endif
1383 {
1384 if (Columns - mouse_col >= pum_base_width
1385 || Columns - mouse_col > min_width)
1386 // Enough space to show at mouse column.
1387 pum_col = mouse_col;
1388 else
1389 // Not enough space, right align with window.
glepnirc942f842024-12-13 12:13:23 +01001390 pum_col = Columns - MIN(pum_base_width, min_width);
zeertzjq883018f2024-06-15 15:37:11 +02001391 pum_width = Columns - pum_col;
1392 }
1393
glepnir89a107e2024-12-22 15:16:40 +01001394 pum_width = MIN(pum_width, pum_base_width + 1);
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001395 // Do not redraw at cursor position.
1396 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001397}
1398
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001399#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001400
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001401#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001402static pumitem_T *balloon_array = NULL;
1403static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001404
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001405# define BALLOON_MIN_WIDTH 50
1406# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001407
Bram Moolenaar246fe032017-11-19 19:56:27 +01001408typedef struct {
1409 char_u *start;
1410 int bytelen;
1411 int cells;
1412 int indent;
1413} balpart_T;
1414
1415/*
1416 * Split a string into parts to display in the balloon.
1417 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1418 * strings and make a struct look good.
1419 * Resulting array is stored in "array" and returns the size of the array.
1420 */
1421 int
1422split_message(char_u *mesg, pumitem_T **array)
1423{
1424 garray_T ga;
1425 char_u *p;
1426 balpart_T *item;
1427 int quoted = FALSE;
1428 int height;
1429 int line;
1430 int item_idx;
1431 int indent = 0;
1432 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001433 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001434 int long_item_count = 0;
1435 int split_long_items = FALSE;
1436
1437 ga_init2(&ga, sizeof(balpart_T), 20);
1438 p = mesg;
1439
1440 while (*p != NUL)
1441 {
1442 if (ga_grow(&ga, 1) == FAIL)
1443 goto failed;
1444 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1445 item->start = p;
1446 item->indent = indent;
1447 item->cells = indent * 2;
1448 ++ga.ga_len;
1449 while (*p != NUL)
1450 {
1451 if (*p == '"')
1452 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001453 else if (*p == '\n')
1454 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001455 else if (*p == '\\' && p[1] != NUL)
1456 ++p;
1457 else if (!quoted)
1458 {
1459 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1460 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001461 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001462 if (*p == '{')
1463 ++indent;
1464 else if (*p == '}' && indent > 0)
1465 --indent;
1466 ++item->cells;
1467 p = skipwhite(p + 1);
1468 break;
1469 }
1470 }
1471 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001472 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001473 }
1474 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001475 if (*p == '\n')
1476 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001477 if (item->cells > max_cells)
1478 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001479 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001480 }
1481
1482 height = 2 + ga.ga_len;
1483
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001484 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001485 if (long_item_count > 0 && height + long_item_count <= max_height)
1486 {
1487 split_long_items = TRUE;
1488 height += long_item_count;
1489 }
1490
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001491 // Limit to half the window height, it has to fit above or below the mouse
1492 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001493 if (height > max_height)
1494 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001495 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001496 if (*array == NULL)
1497 goto failed;
1498
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001499 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001500 (*array)->pum_text = vim_strsave((char_u *)"");
1501 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1502
1503 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1504 {
1505 int skip;
1506 int thislen;
1507 int copylen;
1508 int ind;
1509 int cells;
1510
1511 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001512 if (item->bytelen == 0)
1513 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1514 else
1515 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001516 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001517 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1518 {
1519 cells = item->indent * 2;
1520 for (p = item->start + skip;
1521 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001522 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001523 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1524 break;
1525 thislen = p - (item->start + skip);
1526 }
1527 else
1528 thislen = item->bytelen;
1529
1530 // put indent at the start
1531 p = alloc(thislen + item->indent * 2 + 1);
1532 if (p == NULL)
1533 {
1534 for (line = 0; line <= height - 1; ++line)
1535 vim_free((*array)[line].pum_text);
1536 vim_free(*array);
1537 goto failed;
1538 }
1539 for (ind = 0; ind < item->indent * 2; ++ind)
1540 p[ind] = ' ';
1541
1542 // exclude spaces at the end of the string
1543 for (copylen = thislen; copylen > 0; --copylen)
1544 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001545 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001546
1547 vim_strncpy(p + ind, item->start + skip, copylen);
1548 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001549 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001550 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001551 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001552 }
1553 ga_clear(&ga);
1554 return height;
1555
1556failed:
1557 ga_clear(&ga);
1558 return 0;
1559}
1560
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001561 void
1562ui_remove_balloon(void)
1563{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001564 if (balloon_array == NULL)
1565 return;
1566
1567 pum_undisplay();
1568 while (balloon_arraysize > 0)
1569 vim_free(balloon_array[--balloon_arraysize].pum_text);
1570 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001571}
1572
1573/*
1574 * Terminal version of a balloon, uses the popup menu code.
1575 */
1576 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001577ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001578{
1579 ui_remove_balloon();
1580
Bram Moolenaar246fe032017-11-19 19:56:27 +01001581 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001582 {
1583 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001584 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001585 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001586 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001587 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001588 listitem_T *li;
1589 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001590
Bram Moolenaar246fe032017-11-19 19:56:27 +01001591 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001592 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001593 if (balloon_array == NULL)
1594 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001595 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001596 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001599
1600 balloon_array[idx].pum_text = vim_strsave(
1601 text == NULL ? (char_u *)"" : text);
1602 }
1603 }
1604 else
1605 balloon_arraysize = split_message(mesg, &balloon_array);
1606
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001607 if (balloon_arraysize <= 0)
1608 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001609
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001610 pum_array = balloon_array;
1611 pum_size = balloon_arraysize;
1612 pum_compute_size();
1613 pum_scrollbar = 0;
1614 pum_height = balloon_arraysize;
zeertzjq883018f2024-06-15 15:37:11 +02001615# ifdef FEAT_RIGHTLEFT
1616 pum_rl = curwin->w_p_rl;
1617# endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001618
1619 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1620 pum_selected = -1;
1621 pum_first = 0;
1622 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001623}
1624
1625/*
1626 * Called when the mouse moved, may remove any displayed balloon.
1627 */
1628 void
1629ui_may_remove_balloon(void)
1630{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001631 // For now: remove the balloon whenever the mouse moves to another screen
1632 // cell.
1633 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001634}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001635#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001636
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001637#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001638/*
1639 * Select the pum entry at the mouse position.
1640 */
1641 static void
1642pum_select_mouse_pos(void)
1643{
1644 int idx = mouse_row - pum_row;
1645
zeertzjq761a4202024-07-04 17:26:37 +02001646 if (idx < 0 || idx >= pum_height)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001647 pum_selected = -1;
1648 else if (*pum_array[idx].pum_text != NUL)
1649 pum_selected = idx;
1650}
1651
1652/*
1653 * Execute the currently selected popup menu item.
1654 */
1655 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001656pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001657{
1658 vimmenu_T *mp;
1659 int idx = 0;
1660 exarg_T ea;
1661
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001662 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001663 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001664 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001665 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001666 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001667 break;
1668 }
1669}
1670
1671/*
1672 * Open the terminal version of the popup menu and don't return until it is
1673 * closed.
1674 */
1675 void
1676pum_show_popupmenu(vimmenu_T *menu)
1677{
1678 vimmenu_T *mp;
1679 int idx = 0;
1680 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001681# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001682 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001683# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001684 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001685
1686 pum_undisplay();
1687 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001688 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001689
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001690 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001691 if (menu_is_separator(mp->dname)
1692 || (mp->modes & mp->enabled & mode))
1693 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001694
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001695 // When there are only Terminal mode menus, using "popup Edit" results in
1696 // pum_size being zero.
1697 if (pum_size <= 0)
1698 {
zeertzjq276410e2023-05-07 21:59:33 +01001699 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001700 return;
1701 }
1702
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001703 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001704 if (array == NULL)
1705 return;
1706
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001707 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001708 {
1709 char_u *s = NULL;
1710
1711 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001712 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001713 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001714 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001715 s = mp->dname;
1716 if (s != NULL)
1717 {
1718 s = vim_strsave(s);
1719 if (s != NULL)
1720 array[idx++].pum_text = s;
1721 }
1722 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001723
1724 pum_array = array;
1725 pum_compute_size();
1726 pum_scrollbar = 0;
1727 pum_height = pum_size;
zeertzjq883018f2024-06-15 15:37:11 +02001728# ifdef FEAT_RIGHTLEFT
1729 pum_rl = curwin->w_p_rl;
1730# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001731 pum_position_at_mouse(20);
1732
1733 pum_selected = -1;
1734 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001735# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001736 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001737 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001738# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001739
1740 for (;;)
1741 {
1742 int c;
1743
1744 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001745 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001746 out_flush();
1747
1748 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001749
zeertzjq92a16782022-07-26 12:24:41 +01001750 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1751 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001752 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001753 break;
1754 else if (c == CAR || c == NL)
1755 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001756 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001757 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001758 break;
1759 }
1760 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1761 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001762 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001763 while (pum_selected > 0)
1764 {
1765 --pum_selected;
1766 if (*array[pum_selected].pum_text != NUL)
1767 break;
1768 }
1769 }
1770 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1771 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001772 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001773 while (pum_selected < pum_size - 1)
1774 {
1775 ++pum_selected;
1776 if (*array[pum_selected].pum_text != NUL)
1777 break;
1778 }
1779 }
1780 else if (c == K_RIGHTMOUSE)
1781 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001782 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001783 vungetc(c);
1784 break;
1785 }
1786 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1787 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001788 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001789 pum_select_mouse_pos();
1790 }
1791 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1792 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001793 // left mouse click: select clicked item, if any, and close;
1794 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001795 pum_select_mouse_pos();
1796 if (pum_selected >= 0)
1797 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001798 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001799 break;
1800 }
1801 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1802 break;
1803 }
1804 }
1805
Bram Moolenaar38455a92020-12-24 18:39:02 +01001806 for (idx = 0; idx < pum_size; ++idx)
1807 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001808 vim_free(array);
1809 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001810# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001811 p_bevalterm = save_bevalterm;
1812 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001813# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001814}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001815
1816 void
1817pum_make_popup(char_u *path_name, int use_mouse_pos)
1818{
1819 vimmenu_T *menu;
1820
1821 if (!use_mouse_pos)
1822 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001823 // Hack: set mouse position at the cursor so that the menu pops up
1824 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01001825 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
zeertzjq883018f2024-06-15 15:37:11 +02001826 mouse_col =
1827# ifdef FEAT_RIGHTLEFT
1828 curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
1829# endif
1830 curwin->w_wincol + curwin->w_wcol;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001831 }
1832
1833 menu = gui_find_menu(path_name);
1834 if (menu != NULL)
1835 pum_show_popupmenu(menu);
1836}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001837#endif