blob: 07c99f052bab311cb0c6d2395a3c6b4bfc4c34bd [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;
glepnir88d75932025-03-27 20:09:07 +0100118 if (p_pmw > 0 && def_width > p_pmw)
119 def_width = p_pmw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200120 above_row = 0;
121 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000122
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100123 // Pretend the pum is already there to avoid that must_redraw is set
124 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200125 pum_array = (pumitem_T *)1;
126 validate_cursor_col();
127 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000128
Bram Moolenaar491ac282018-06-17 14:47:55 +0200129 // Remember the essential parts of the window position and size, so we
130 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200131 pum_window = curwin;
Bram Moolenaar24959102022-05-07 20:01:16 +0100132 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000133 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000134 pum_win_row = cmdline_row;
135 else
136 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200137 pum_win_height = curwin->w_height;
138 pum_win_col = curwin->w_wincol;
139 pum_win_wcol = curwin->w_wcol;
140 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000141
Bram Moolenaar4033c552017-09-16 20:54:51 +0200142#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200143 FOR_ALL_WINDOWS(pvwin)
144 if (pvwin->w_p_pvw)
145 break;
146 if (pvwin != NULL)
147 {
148 if (W_WINROW(pvwin) < W_WINROW(curwin))
149 above_row = W_WINROW(pvwin) + pvwin->w_height;
150 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
151 below_row = W_WINROW(pvwin);
152 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100153#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000154
Bram Moolenaara5e66212017-09-29 22:42:33 +0200155 /*
156 * Figure out the size and position of the pum.
157 */
glepnirc942f842024-12-13 12:13:23 +0100158 pum_height = MIN(size, PUM_DEF_HEIGHT);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000159 if (p_ph > 0 && pum_height > p_ph)
160 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000161
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100162 // Put the pum below "pum_win_row" if possible. If there are few lines
163 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200164 if (pum_win_row + 2 >= below_row - pum_height
165 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200166 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100167 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200168
Bram Moolenaar24959102022-05-07 20:01:16 +0100169 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100170 // for cmdline pum, no need for context lines
171 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200172 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100173 // Leave two lines of context if possible
glepnirc942f842024-12-13 12:13:23 +0100174 context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
Bram Moolenaara5e66212017-09-29 22:42:33 +0200175
Bram Moolenaar491ac282018-06-17 14:47:55 +0200176 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200177 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200178 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200179 pum_height = size;
180 }
181 else
182 {
183 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200184 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200185 }
186 if (p_ph > 0 && pum_height > p_ph)
187 {
188 pum_row += pum_height - p_ph;
189 pum_height = p_ph;
190 }
191 }
192 else
193 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100194 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200195
Bram Moolenaar24959102022-05-07 20:01:16 +0100196 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100197 // for cmdline pum, no need for context lines
198 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200199 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100200 {
201 // Leave two lines of context if possible
202 validate_cheight();
glepnirc942f842024-12-13 12:13:23 +0100203 cline_visible_offset = curwin->w_cline_row +
204 curwin->w_cline_height - curwin->w_wrow;
205 context_lines = MIN(3, cline_visible_offset);
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100206 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200207
Bram Moolenaar491ac282018-06-17 14:47:55 +0200208 pum_row = pum_win_row + context_lines;
glepnirc942f842024-12-13 12:13:23 +0100209 pum_height = MIN(below_row - pum_row, size);
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210 if (p_ph > 0 && pum_height > p_ph)
211 pum_height = p_ph;
212 }
213
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100214 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200215 if (pum_height < 1 || (pum_height == 1 && size > 1))
216 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000217
Bram Moolenaar4033c552017-09-16 20:54:51 +0200218#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100219 // If there is a preview window above avoid drawing over it.
220 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000221 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100222 pum_row = above_row;
223 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000224 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200225#endif
226
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100227 pum_array = array;
228 pum_size = size;
229 pum_compute_size();
230 max_width = pum_base_width;
glepnir88d75932025-03-27 20:09:07 +0100231 if (p_pmw > 0 && max_width > p_pmw)
232 max_width = p_pmw;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000233
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100234 // Calculate column
Bram Moolenaar24959102022-05-07 20:01:16 +0100235 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000236 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000237 cursor_col = cmdline_compl_startcol();
238 else
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100239 {
240 // w_wcol includes virtual text "above"
241 int wcol = curwin->w_wcol % curwin->w_width;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000242#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200243 if (pum_rl)
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100244 cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
245 else
Bram Moolenaarabc97732007-08-08 20:49:37 +0000246#endif
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100247 cursor_col = curwin->w_wincol + wcol;
248 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000249
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100250 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200251 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000252 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200253 pum_scrollbar = 1;
254 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000255 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000256 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200257 pum_scrollbar = 0;
258
259 if (def_width < max_width)
260 def_width = max_width;
261
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100262 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000263#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200264 && !pum_rl)
265 || (pum_rl && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000266#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200267 ))
268 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100269 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100270 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000271
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100272 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200273#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200274 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200275 pum_width = pum_col - pum_scrollbar + 1;
276 else
277#endif
278 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000279
glepnirc942f842024-12-13 12:13:23 +0100280 content_width = max_width + pum_kind_width + pum_extra_width + 1;
281 if (pum_width > content_width && pum_width > p_pw)
glepnir88d75932025-03-27 20:09:07 +0100282 {
glepnirc942f842024-12-13 12:13:23 +0100283 // Reduce width to fit item
glepnir88d75932025-03-27 20:09:07 +0100284 pum_width = MAX(content_width, p_pw);
285 if (p_pmw > 0 && pum_width > p_pmw)
286 pum_width = p_pmw;
287 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100288 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100289#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200290 && !pum_rl)
291 || (pum_rl && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100292 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100293#endif
294 ))
295 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100296 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100297#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200298 if (pum_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100299 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100301 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100302 if (pum_col >= Columns)
303 pum_col = Columns - 1;
304 }
zeertzjq883018f2024-06-15 15:37:11 +0200305 else if (!pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100306#endif
307 {
glepnirc942f842024-12-13 12:13:23 +0100308 right_edge_col = Columns - max_width - pum_scrollbar;
309 if (curwin->w_wincol > right_edge_col && max_width <= p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100310 // use full width to end of the screen
glepnirc942f842024-12-13 12:13:23 +0100311 pum_col = MAX(0, right_edge_col);
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100312 }
313
314#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200315 if (pum_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100316 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100317 else
318#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100319 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100320
Bram Moolenaar42443c72018-02-10 18:28:52 +0100321 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100322 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100323 pum_width = p_pw;
glepnir88d75932025-03-27 20:09:07 +0100324 if (p_pmw > 0 && pum_width > p_pmw)
325 pum_width = p_pmw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100326#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200327 if (pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100328 {
329 if (pum_width > pum_col)
330 pum_width = pum_col;
331 }
332 else
333#endif
334 {
335 if (pum_width >= Columns - pum_col)
336 pum_width = Columns - pum_col - 1;
337 }
338 }
glepnirc942f842024-12-13 12:13:23 +0100339 else if (pum_width > content_width && pum_width > p_pw)
glepnir88d75932025-03-27 20:09:07 +0100340 {
Christian Brabandt618c4d32024-12-13 12:30:20 +0100341 pum_width = MAX(content_width, p_pw);
glepnir88d75932025-03-27 20:09:07 +0100342 if (p_pmw > 0 && pum_width > p_pmw)
343 pum_width = p_pmw;
344 }
glepnir532c5ae2025-03-28 19:21:59 +0100345 else if (p_pmw > 0 && pum_width > p_pmw)
346 {
347 pum_width = p_pmw;
348 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100349 }
350
Bram Moolenaara5e66212017-09-29 22:42:33 +0200351 }
352 else if (Columns < def_width)
353 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100354 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200355#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200356 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200357 pum_col = Columns - 1;
358 else
359#endif
360 pum_col = 0;
361 pum_width = Columns - 1;
glepnir88d75932025-03-27 20:09:07 +0100362 if (p_pmw > 0 && pum_width > p_pmw)
363 pum_width = p_pmw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200364 }
365 else
366 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100367 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100368 max_width = p_pw; // truncate
glepnir88d75932025-03-27 20:09:07 +0100369 if (p_pmw > 0 && max_width > p_pmw)
370 max_width = p_pmw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200371#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200372 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200373 pum_col = max_width - 1;
374 else
375#endif
376 pum_col = Columns - max_width;
377 pum_width = max_width - pum_scrollbar;
378 }
379
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100380 // Set selected item and redraw. If the window size changed need to
381 // redo the positioning. Limit this to two times, when there is not
382 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200383 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100384
385 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000386}
387
388/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100389 * Set a flag that when pum_redraw() is called it first calls update_screen().
390 * This will avoid clearing and redrawing the popup menu, prevent flicker.
391 */
392 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000393pum_call_update_screen(void)
Bram Moolenaarae654382019-01-17 21:09:05 +0100394{
395 call_update_screen = TRUE;
396
397 // Update the cursor position to be able to compute the popup menu
398 // position. The cursor line length may have changed because of the
399 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100400 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100401 validate_cursor();
402}
403
404/*
405 * Return TRUE if we are going to redraw the popup menu and the screen position
406 * "row"/"col" is under the popup menu.
407 */
408 int
Bakudankun65555002021-11-17 20:40:16 +0000409pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100410{
Bakudankun65555002021-11-17 20:40:16 +0000411 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100412 && row >= pum_row
413 && row < pum_row + pum_height
414 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200415 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100416}
417
418/*
zeertzjq63901e82024-06-16 16:51:25 +0200419 * Computes attributes of text on the popup menu.
420 * Returns attributes for every cell, or NULL if all attributes are the same.
glepnir40c1c332024-06-11 19:37:04 +0200421 */
zeertzjq63901e82024-06-16 16:51:25 +0200422 static int *
zeertzjq41008522024-07-27 13:21:49 +0200423pum_compute_text_attrs(char_u *text, hlf_T hlf, int user_hlattr)
glepnir40c1c332024-06-11 19:37:04 +0200424{
425 int i;
zeertzjqd8c93402024-06-17 18:25:32 +0200426 size_t leader_len;
zeertzjq63901e82024-06-16 16:51:25 +0200427 int char_cells;
glepnir40c1c332024-06-11 19:37:04 +0200428 int new_attr;
glepnir40c1c332024-06-11 19:37:04 +0200429 char_u *ptr = text;
zeertzjq63901e82024-06-16 16:51:25 +0200430 int cell_idx = 0;
glepnir40c1c332024-06-11 19:37:04 +0200431 garray_T *ga = NULL;
zeertzjq63901e82024-06-16 16:51:25 +0200432 int *attrs = NULL;
zeertzjqd8c93402024-06-17 18:25:32 +0200433 char_u *leader = NULL;
434 int in_fuzzy;
Girish Palya4ec46f32025-03-04 20:56:30 +0100435 int matched_len = -1;
zeertzjq63901e82024-06-16 16:51:25 +0200436 int_u char_pos = 0;
glepnir89a107e2024-12-22 15:16:40 +0100437 int is_select = FALSE;
glepnir40c1c332024-06-11 19:37:04 +0200438
zeertzjq3a0cc362025-01-13 07:27:43 +0100439 if (*text == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
zeertzjqafbe5352024-06-14 20:24:22 +0200440 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
441 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
zeertzjq63901e82024-06-16 16:51:25 +0200442 return NULL;
glepnir40c1c332024-06-11 19:37:04 +0200443
glepnir89a107e2024-12-22 15:16:40 +0100444 is_select = hlf == HLF_PSI;
zeertzjqd8c93402024-06-17 18:25:32 +0200445 leader = State == MODE_CMDLINE ? cmdline_compl_pattern()
446 : ins_compl_leader();
447 if (leader == NULL || *leader == NUL)
448 return NULL;
449
zeertzjq63901e82024-06-16 16:51:25 +0200450 attrs = ALLOC_MULT(int, vim_strsize(text));
451 if (attrs == NULL)
452 return NULL;
453
zeertzjqd8c93402024-06-17 18:25:32 +0200454 in_fuzzy = State == MODE_CMDLINE ? cmdline_compl_is_fuzzy()
455 : (get_cot_flags() & COT_FUZZY) != 0;
456 leader_len = STRLEN(leader);
glepnir40c1c332024-06-11 19:37:04 +0200457
glepnir1c296022024-06-13 17:21:24 +0200458 if (in_fuzzy)
zeertzjq63901e82024-06-16 16:51:25 +0200459 ga = fuzzy_match_str_with_pos(text, leader);
glepnir40c1c332024-06-11 19:37:04 +0200460
zeertzjq63901e82024-06-16 16:51:25 +0200461 while (*ptr != NUL)
glepnir40c1c332024-06-11 19:37:04 +0200462 {
zeertzjqafbe5352024-06-14 20:24:22 +0200463 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200464
zeertzjqafbe5352024-06-14 20:24:22 +0200465 if (ga != NULL)
466 {
467 // Handle fuzzy matching
468 for (i = 0; i < ga->ga_len; i++)
469 {
zeertzjq63901e82024-06-16 16:51:25 +0200470 if (char_pos == ((int_u *)ga->ga_data)[i])
zeertzjqafbe5352024-06-14 20:24:22 +0200471 {
glepnir89a107e2024-12-22 15:16:40 +0100472 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
glepnir9eff3ee2025-01-11 16:47:34 +0100473 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
zeertzjqafbe5352024-06-14 20:24:22 +0200474 break;
475 }
476 }
477 }
Girish Palya4ec46f32025-03-04 20:56:30 +0100478 else
glepnir9eff3ee2025-01-11 16:47:34 +0100479 {
Girish Palya4ec46f32025-03-04 20:56:30 +0100480 if (matched_len < 0 && MB_STRNICMP(ptr, leader, leader_len) == 0)
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +0200481 matched_len = (int)leader_len;
Girish Palya4ec46f32025-03-04 20:56:30 +0100482 if (matched_len > 0)
483 {
484 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
485 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
486 matched_len--;
487 }
glepnir9eff3ee2025-01-11 16:47:34 +0100488 }
glepnir40c1c332024-06-11 19:37:04 +0200489
glepnir9eff3ee2025-01-11 16:47:34 +0100490 new_attr = hl_combine_attr(highlight_attr[HLF_PNI], new_attr);
zeertzjq41008522024-07-27 13:21:49 +0200491 if (user_hlattr > 0)
492 new_attr = hl_combine_attr(new_attr, user_hlattr);
glepnir8754efe2024-07-26 18:15:27 +0200493
zeertzjq63901e82024-06-16 16:51:25 +0200494 char_cells = mb_ptr2cells(ptr);
495 for (i = 0; i < char_cells; i++)
496 attrs[cell_idx + i] = new_attr;
497 cell_idx += char_cells;
498
499 MB_PTR_ADV(ptr);
500 char_pos++;
glepnir40c1c332024-06-11 19:37:04 +0200501 }
502
503 if (ga != NULL)
504 {
zeertzjqafbe5352024-06-14 20:24:22 +0200505 ga_clear(ga);
506 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200507 }
zeertzjq63901e82024-06-16 16:51:25 +0200508 return attrs;
509}
510
511/*
512 * Displays text on the popup menu with specific attributes.
513 */
514 static void
515pum_screen_puts_with_attrs(
516 int row,
517 int col,
518 int cells UNUSED,
519 char_u *text,
520 int textlen,
glepnir8754efe2024-07-26 18:15:27 +0200521 int *attrs)
zeertzjq63901e82024-06-16 16:51:25 +0200522{
523 int col_start = col;
524 char_u *ptr = text;
525 int char_len;
526 int attr;
527
528 // Render text with proper attributes
529 while (*ptr != NUL && ptr < text + textlen)
530 {
531 char_len = mb_ptr2len(ptr);
532#ifdef FEAT_RIGHTLEFT
533 if (pum_rl)
534 attr = attrs[col_start + cells - col - 1];
535 else
536#endif
537 attr = attrs[col - col_start];
zeertzjqd9be94c2024-07-14 10:20:20 +0200538 screen_puts_len(ptr, char_len, row, col, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200539 col += mb_ptr2cells(ptr);
540 ptr += char_len;
541 }
glepnir40c1c332024-06-11 19:37:04 +0200542}
543
glepnir6a89c942024-10-01 20:32:12 +0200544
545 static inline void
546pum_align_order(int *order)
547{
548 int is_default = cia_flags == 0;
549 order[0] = is_default ? CPT_ABBR : cia_flags / 100;
550 order[1] = is_default ? CPT_KIND : (cia_flags / 10) % 10;
551 order[2] = is_default ? CPT_MENU : cia_flags % 10;
552}
553
554 static inline char_u *
555pum_get_item(int index, int type)
556{
557 switch(type)
558 {
559 case CPT_ABBR: return pum_array[index].pum_text;
560 case CPT_KIND: return pum_array[index].pum_kind;
561 case CPT_MENU: return pum_array[index].pum_extra;
562 }
563 return NULL;
564}
565
glepnir89a107e2024-12-22 15:16:40 +0100566 static inline int
567pum_user_attr_combine(int idx, int type, int attr)
568{
569 int user_attr[] = {
570 pum_array[idx].pum_user_abbr_hlattr,
571 pum_array[idx].pum_user_kind_hlattr,
572 };
573
574 return user_attr[type] > 0 ? hl_combine_attr(attr, user_attr[type]) : attr;
575}
576
glepnir40c1c332024-06-11 19:37:04 +0200577/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000578 * Redraw the popup menu, using "pum_first" and "pum_selected".
579 */
580 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100581pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000582{
583 int row = pum_row;
584 int col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000585 int attr_scroll = highlight_attr[HLF_PSB];
586 int attr_thumb = highlight_attr[HLF_PST];
zeertzjqafbe5352024-06-14 20:24:22 +0200587 hlf_T *hlfs; // array used for highlights
588 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000589 int attr;
glepnir6a89c942024-10-01 20:32:12 +0200590 int i, j;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000591 int idx;
592 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000593 char_u *p = NULL;
glepnir6a89c942024-10-01 20:32:12 +0200594 int totwidth, width, w; // total-width item-width char-width
Bram Moolenaar4b779472005-10-04 09:12:31 +0000595 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100596 int thumb_height = 1;
glepnir6a89c942024-10-01 20:32:12 +0200597 int item_type;
598 int order[3];
599 int next_isempty = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000600 int n;
glepnir6a89c942024-10-01 20:32:12 +0200601 int items_width_array[3] = { pum_base_width, pum_kind_width,
602 pum_extra_width };
603 int basic_width; // first item width
604 int last_isabbr = FALSE;
glepnir0fe17f82024-10-08 22:26:44 +0200605 int orig_attr = -1;
glepnir89a107e2024-12-22 15:16:40 +0100606 int scroll_range = pum_size - pum_height;
glepnirb8762042025-04-07 20:57:14 +0200607 int remaining = 0;
glepnird4dbf822025-04-12 18:35:34 +0200608 int fcs_trunc;
609
610#ifdef FEAT_RIGHTLEFT
611 if (pum_rl)
612 fcs_trunc = curwin->w_fill_chars.truncrl;
613 else
614#endif
615 fcs_trunc = curwin->w_fill_chars.trunc;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000616
zeertzjqafbe5352024-06-14 20:24:22 +0200617 hlf_T hlfsNorm[3];
618 hlf_T hlfsSel[3];
glepnir6a89c942024-10-01 20:32:12 +0200619 // "word"/"abbr"
zeertzjqafbe5352024-06-14 20:24:22 +0200620 hlfsNorm[0] = HLF_PNI;
621 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000622 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200623 hlfsNorm[1] = HLF_PNK;
624 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000625 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200626 hlfsNorm[2] = HLF_PNX;
627 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000628
Bram Moolenaarae654382019-01-17 21:09:05 +0100629 if (call_update_screen)
630 {
631 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100632 // Do not redraw in pum_may_redraw() and don't draw in the area where
633 // the popup menu will be.
634 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100635 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100636 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100637 }
638
639 // never display more than we have
glepnir89a107e2024-12-22 15:16:40 +0100640 pum_first = MIN(pum_first, scroll_range);
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100641
Bram Moolenaar4b779472005-10-04 09:12:31 +0000642 if (pum_scrollbar)
643 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100644 thumb_height = pum_height * pum_height / pum_size;
645 if (thumb_height == 0)
646 thumb_height = 1;
647 thumb_pos = (pum_first * (pum_height - thumb_height)
glepnir89a107e2024-12-22 15:16:40 +0100648 + scroll_range / 2) / scroll_range;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000649 }
650
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100651#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200652 // The popup menu is drawn over popup menus with zindex under
653 // POPUPMENU_ZINDEX.
654 screen_zindex = POPUPMENU_ZINDEX;
655#endif
656
Bram Moolenaar4b779472005-10-04 09:12:31 +0000657 for (i = 0; i < pum_height; ++i)
658 {
659 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200660 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
661 hlf = hlfs[0]; // start with "word" highlight
662 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000663
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100664 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000665#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200666 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000667 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200668 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000669 screen_putchar(' ', row, pum_col + 1, attr);
670 }
671 else
672#endif
673 if (pum_col > 0)
674 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000675
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100676 // Display each entry, use two spaces for a Tab.
glepnir6a89c942024-10-01 20:32:12 +0200677 // Do this 3 times and order from p_cia
Bram Moolenaar4b779472005-10-04 09:12:31 +0000678 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000679 totwidth = 0;
glepnir6a89c942024-10-01 20:32:12 +0200680 pum_align_order(order);
681 basic_width = items_width_array[order[0]];
682 last_isabbr = order[2] == CPT_ABBR;
683 for (j = 0; j < 3; ++j)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000684 {
glepnir6a89c942024-10-01 20:32:12 +0200685 item_type = order[j];
686 hlf = hlfs[item_type];
zeertzjqafbe5352024-06-14 20:24:22 +0200687 attr = highlight_attr[hlf];
glepnir0fe17f82024-10-08 22:26:44 +0200688 orig_attr = attr;
glepnir89a107e2024-12-22 15:16:40 +0100689 if (item_type < 2) // try combine attr with user custom
690 attr = pum_user_attr_combine(idx, item_type, attr);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000691 width = 0;
692 s = NULL;
glepnir6a89c942024-10-01 20:32:12 +0200693 p = pum_get_item(idx, item_type);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000694 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100695 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000696 {
697 if (s == NULL)
698 s = p;
699 w = ptr2cells(p);
zeertzjq3a0cc362025-01-13 07:27:43 +0100700 if (*p != NUL && *p != TAB && totwidth + w <= pum_width)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000701 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100702 width += w;
703 continue;
704 }
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000705
zeertzjq3a0cc362025-01-13 07:27:43 +0100706 // Display the text that fits or comes before a Tab.
707 // First convert it to printable characters.
708 char_u *st;
709 int *attrs = NULL;
710 int saved = *p;
zeertzjq63901e82024-06-16 16:51:25 +0200711
zeertzjq3a0cc362025-01-13 07:27:43 +0100712 if (saved != NUL)
713 *p = NUL;
714 st = transstr(s);
715 if (saved != NUL)
716 *p = saved;
717
718 if (item_type == CPT_ABBR)
719 attrs = pum_compute_text_attrs(st, hlf,
720 pum_array[idx].pum_user_abbr_hlattr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000721#ifdef FEAT_RIGHTLEFT
zeertzjq3a0cc362025-01-13 07:27:43 +0100722 if (pum_rl)
723 {
724 if (st != NULL)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000725 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100726 char_u *rt = reverse_text(st);
727
728 if (rt != NULL)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000729 {
glepnird4dbf822025-04-12 18:35:34 +0200730 char_u *rt_start = rt;
731 int cells;
732 int over_cell = 0;
733 int truncated = FALSE;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000734
glepnirb8762042025-04-07 20:57:14 +0200735 cells = mb_string2cells(rt , -1);
glepnird4dbf822025-04-12 18:35:34 +0200736 truncated = pum_width == p_pmw
737 && pum_width - totwidth < cells;
738
739 if (pum_width == p_pmw && !truncated
740 && (j + 1 < 3 && pum_get_item(idx, order[j + 1]) != NULL))
741 truncated = TRUE;
742
zeertzjq3a0cc362025-01-13 07:27:43 +0100743 if (cells > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000744 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100745 do
Bram Moolenaarabc97732007-08-08 20:49:37 +0000746 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100747 cells -= has_mbyte
zeertzjq63901e82024-06-16 16:51:25 +0200748 ? (*mb_ptr2cells)(rt) : 1;
zeertzjq3a0cc362025-01-13 07:27:43 +0100749 MB_PTR_ADV(rt);
750 } while (cells > pum_width);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100751
glepnirb8762042025-04-07 20:57:14 +0200752 if (cells < pum_width)
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100753 {
zeertzjq3a0cc362025-01-13 07:27:43 +0100754 // Most left character requires 2-cells
755 // but only 1 cell is available on
756 // screen. Put a '<' on the left of
757 // the pum item.
758 *(--rt) = '<';
759 cells++;
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100760 }
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100761 }
zeertzjq63901e82024-06-16 16:51:25 +0200762
glepnird4dbf822025-04-12 18:35:34 +0200763 if (truncated)
glepnirb8762042025-04-07 20:57:14 +0200764 {
765 char_u *orig_rt = rt;
glepnirb8762042025-04-07 20:57:14 +0200766 int size = 0;
767
768 remaining = pum_width - totwidth - 1;
769 cells = mb_string2cells(rt, -1);
770 if (cells > remaining)
771 {
772 while (cells > remaining)
773 {
774 MB_PTR_ADV(orig_rt);
775 cells -= has_mbyte ? (*mb_ptr2cells)(orig_rt) : 1;
776 }
777 }
778 size = (int)STRLEN(orig_rt);
779 if (cells < remaining)
780 over_cell = remaining - cells;
glepnird4dbf822025-04-12 18:35:34 +0200781
782 cells = mb_string2cells(orig_rt, size);
783 width = cells + over_cell + 1;
784 rt = orig_rt;
785
786 if (fcs_trunc != NUL)
787 screen_putchar(fcs_trunc, row, col - width + 1, attr);
glepnirb8762042025-04-07 20:57:14 +0200788 else
glepnird4dbf822025-04-12 18:35:34 +0200789 screen_putchar('<', row, col - width + 1, attr);
790
791 if (over_cell > 0)
792 screen_fill(row, row + 1, col - width + 2,
793 col - width + 2 + over_cell, ' ', ' ', attr);
glepnirb8762042025-04-07 20:57:14 +0200794 }
795
zeertzjq63901e82024-06-16 16:51:25 +0200796 if (attrs == NULL)
zeertzjq3a0cc362025-01-13 07:27:43 +0100797 screen_puts_len(rt, (int)STRLEN(rt), row,
798 col - cells + 1, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200799 else
zeertzjq3a0cc362025-01-13 07:27:43 +0100800 pum_screen_puts_with_attrs(row,
801 col - cells + 1, cells, rt,
802 (int)STRLEN(rt), attrs);
zeertzjq63901e82024-06-16 16:51:25 +0200803
zeertzjq3a0cc362025-01-13 07:27:43 +0100804 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000805 }
zeertzjq3a0cc362025-01-13 07:27:43 +0100806 vim_free(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000807 }
zeertzjq3a0cc362025-01-13 07:27:43 +0100808 col -= width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000809 }
810 else
zeertzjq3a0cc362025-01-13 07:27:43 +0100811#endif
812 {
813 if (st != NULL)
814 {
glepnird4dbf822025-04-12 18:35:34 +0200815 int size = (int)STRLEN(st);
816 int cells = (*mb_string2cells)(st, size);
817 char_u *st_end = NULL;
818 int over_cell = 0;
819 int truncated = pum_width == p_pmw
820 && pum_width - totwidth < cells;
821
822 if (pum_width == p_pmw && !truncated
823 && (j + 1 < 3 && pum_get_item(idx, order[j + 1]) != NULL))
824 truncated = TRUE;
zeertzjq3a0cc362025-01-13 07:27:43 +0100825
826 // only draw the text that fits
827 while (size > 0
828 && col + cells > pum_width + pum_col)
829 {
830 --size;
831 if (has_mbyte)
832 {
833 size -= (*mb_head_off)(st, st + size);
834 cells -= (*mb_ptr2cells)(st + size);
835 }
836 else
837 --cells;
838 }
839
glepnirb8762042025-04-07 20:57:14 +0200840 // truncated
glepnird4dbf822025-04-12 18:35:34 +0200841 if (truncated)
glepnir88d75932025-03-27 20:09:07 +0100842 {
glepnirb8762042025-04-07 20:57:14 +0200843 remaining = pum_width - totwidth - 1;
844 if (cells > remaining)
glepnir88d75932025-03-27 20:09:07 +0100845 {
glepnirb8762042025-04-07 20:57:14 +0200846 st_end = st + size;
847 while (st_end > st && cells > remaining)
glepnir88d75932025-03-27 20:09:07 +0100848 {
849 MB_PTR_BACK(st, st_end);
glepnirb8762042025-04-07 20:57:14 +0200850 cells -= has_mbyte ? (*mb_ptr2cells)(st_end) : 1;
glepnir88d75932025-03-27 20:09:07 +0100851 }
glepnirb8762042025-04-07 20:57:14 +0200852 size = st_end - st;
glepnir88d75932025-03-27 20:09:07 +0100853 }
glepnirb8762042025-04-07 20:57:14 +0200854
855 if (cells < remaining)
856 over_cell = remaining - cells;
glepnird4dbf822025-04-12 18:35:34 +0200857 cells = mb_string2cells(st, size);
858 width = cells + over_cell + 1;
glepnir88d75932025-03-27 20:09:07 +0100859 }
860
zeertzjq3a0cc362025-01-13 07:27:43 +0100861 if (attrs == NULL)
862 screen_puts_len(st, size, row, col, attr);
863 else
864 pum_screen_puts_with_attrs(row, col, cells,
865 st, size, attrs);
glepnird4dbf822025-04-12 18:35:34 +0200866 if (truncated)
867 {
868 if (over_cell > 0)
869 screen_fill(row, row + 1, col + cells,
870 col + cells + over_cell, ' ', ' ', attr);
871 if (fcs_trunc != NUL)
872 screen_putchar(fcs_trunc, row,
873 col + cells + over_cell, attr);
874 else
875 screen_putchar('>', row,
876 col + cells + over_cell, attr);
877 }
zeertzjq3a0cc362025-01-13 07:27:43 +0100878
879 vim_free(st);
880 }
881 col += width;
882 }
883
884 if (attrs != NULL)
885 VIM_CLEAR(attrs);
886
887 if (*p != TAB)
888 break;
889
890 // Display two spaces for a Tab.
891#ifdef FEAT_RIGHTLEFT
892 if (pum_rl)
893 {
894 screen_puts_len((char_u *)" ", 2, row, col - 1, attr);
895 col -= 2;
896 }
897 else
898#endif
899 {
900 screen_puts_len((char_u *)" ", 2, row, col, attr);
901 col += 2;
902 }
903 totwidth += 2;
904 s = NULL; // start text at next char
905 width = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000906 }
907
glepnir6a89c942024-10-01 20:32:12 +0200908 if (j > 0)
909 n = items_width_array[order[1]] + (last_isabbr ? 0 : 1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000910 else
glepnir6a89c942024-10-01 20:32:12 +0200911 n = order[j] == CPT_ABBR ? 1 : 0;
912
913 if (j + 1 < 3)
914 next_isempty = pum_get_item(idx, order[j + 1]) == NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000915
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100916 // Stop when there is nothing more to display.
glepnir6a89c942024-10-01 20:32:12 +0200917 if (j == 2
918 || (next_isempty && (j == 1 || (j == 0
919 && pum_get_item(idx, order[j + 2]) == NULL)))
glepnira6d9e3c2024-10-03 11:01:19 +0200920 || basic_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000921 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000922#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200923 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000924 {
glepnir6a89c942024-10-01 20:32:12 +0200925 screen_fill(row, row + 1, pum_col - basic_width - n + 1,
glepnirc942f842024-12-13 12:13:23 +0100926 col + 1, ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +0200927 col = pum_col - basic_width - n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000928 }
929 else
930#endif
931 {
glepnir6a89c942024-10-01 20:32:12 +0200932 screen_fill(row, row + 1, col, pum_col + basic_width + n,
glepnirc942f842024-12-13 12:13:23 +0100933 ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +0200934 col = pum_col + basic_width + n;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000935 }
glepnir6a89c942024-10-01 20:32:12 +0200936 totwidth = basic_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000937 }
938
Bram Moolenaarabc97732007-08-08 20:49:37 +0000939#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200940 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000941 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
glepnirc942f842024-12-13 12:13:23 +0100942 ' ', orig_attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000943 else
944#endif
945 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
glepnirc942f842024-12-13 12:13:23 +0100946 orig_attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000947 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000948 {
949#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200950 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000951 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100952 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000953 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000954 else
955#endif
956 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100957 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000958 ? attr_thumb : attr_scroll);
959 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000960
961 ++row;
962 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200963
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100964#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200965 screen_zindex = 0;
966#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000967}
968
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100969#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200970/*
971 * Position the info popup relative to the popup menu item.
972 */
973 void
974pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200975{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100976 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200977 int row = pum_row;
978 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200979 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200980
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200981 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200982 if (Columns - col < 20 && Columns - col < pum_col)
983 {
984 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200985 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200986 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200987 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200988 }
989 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200990 wp->w_maxwidth = Columns - col + 1;
991 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200992 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
993 {
994 // option value overrules computed value
995 wp->w_maxwidth = wp->w_maxwidth_opt;
996 used_maxwidth_opt = TRUE;
997 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200998
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200999 row -= popup_top_extra(wp);
1000 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001001 {
1002 if (pum_row < pum_win_row)
1003 {
1004 // menu above cursor line, align with bottom
1005 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001006 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001007 }
1008 else
1009 // menu below cursor line, align with top
1010 row += 1;
1011 }
1012 else
1013 // align with the selected item
1014 row += pum_selected - pum_first + 1;
1015
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01001016 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001017 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01001018 // The popup is not going to fit or will overlap with the cursor
1019 // position, hide the popup.
1020 wp->w_popup_flags |= POPF_HIDDEN;
1021 else
1022 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001023}
1024#endif
1025
Bram Moolenaar4b779472005-10-04 09:12:31 +00001026/*
1027 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001028 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001029 * This may be repeated when the preview window is used:
1030 * "repeat" == 0: open preview window normally
1031 * "repeat" == 1: open preview window but don't set the size
1032 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001033 * Returns TRUE when the window was resized and the location of the popup menu
1034 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001035 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001036 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001037pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001038{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001039 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001040 int context = pum_height / 2;
glepnir8d0bb6d2024-12-24 09:44:35 +01001041 int scroll_offset;
Bram Moolenaareaf35242019-08-20 23:14:15 +02001042#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001043 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +02001044 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +02001045#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001046#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001047 int has_info = FALSE;
1048#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001049
Bram Moolenaar4b779472005-10-04 09:12:31 +00001050 pum_selected = n;
glepnir8d0bb6d2024-12-24 09:44:35 +01001051 scroll_offset = pum_selected - pum_height;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001052
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001053 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001054 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00001055 if (pum_first > pum_selected - 4)
1056 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001057 // scroll down; when we did a jump it's probably a PageUp then
1058 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +00001059 if (pum_first > pum_selected - 2)
1060 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001061 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001062 if (pum_first < 0)
1063 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001064 else if (pum_first > pum_selected)
1065 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001066 }
1067 else
1068 pum_first = pum_selected;
1069 }
glepnir89a107e2024-12-22 15:16:40 +01001070 else if (pum_first < scroll_offset + 5)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001071 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001072 // scroll up; when we did a jump it's probably a PageDown then
1073 // scroll a whole page
glepnir89a107e2024-12-22 15:16:40 +01001074 if (pum_first < scroll_offset + 3)
glepnir8d0bb6d2024-12-24 09:44:35 +01001075 pum_first = MAX(pum_first + pum_height - 2, scroll_offset + 1);
Bram Moolenaare3226be2005-12-18 22:10:00 +00001076 else
glepnir89a107e2024-12-22 15:16:40 +01001077 pum_first = scroll_offset + 1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001078 }
Bram Moolenaar4b779472005-10-04 09:12:31 +00001079
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001080 // Give a few lines of context when possible.
glepnir89a107e2024-12-22 15:16:40 +01001081 context = MIN(context, 3);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001082 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001083 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001084 if (pum_first > pum_selected - context)
glepnir89a107e2024-12-22 15:16:40 +01001085 pum_first = MAX(pum_selected - context, 0); // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001086 else if (pum_first < pum_selected + context - pum_height + 1)
glepnir89a107e2024-12-22 15:16:40 +01001087 pum_first = pum_selected + context - pum_height + 1; // up
Bram Moolenaar4b779472005-10-04 09:12:31 +00001088 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001089 // adjust for the number of lines displayed
glepnirc942f842024-12-13 12:13:23 +01001090 pum_first = MIN(pum_first, pum_size - pum_height);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001091
Bram Moolenaar4033c552017-09-16 20:54:51 +02001092#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001093 /*
1094 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +01001095 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001096 * Skip this when tried twice already.
1097 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001098 * NOTE: Be very careful not to sync undo!
1099 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001100 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001101 && Rows > 10
1102 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +02001103 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001104 {
1105 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001106 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001107# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001108 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001109# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +01001110# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001111# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001112# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001113 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +02001114 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001115 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +02001116 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001117 use_popup = USEPOPUP_NORMAL;
1118 else
1119 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001120 if (use_popup != USEPOPUP_NONE)
1121 // don't use WinEnter or WinLeave autocommands for the info
1122 // popup
1123 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +02001124# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001125 // Open a preview window and set "curwin" to it.
1126 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001127 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +02001128 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
1129 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +02001130 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001131 // Prevent undo sync here, if an autocommand syncs undo weird
1132 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +01001133 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001134 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001135 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001136 if (RedrawingDisabled > 0)
1137 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001138 g_do_tagpreview = 0;
1139
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001140 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001141# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001142 || (curwin->w_popup_flags & POPF_INFO)
1143# endif
1144 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001145 {
mathew20f61d92023-08-26 18:11:02 +02001146 int res = OK;
1147
Bram Moolenaar50e53762016-10-27 14:49:15 +02001148 if (!resized
1149 && curbuf->b_nwindows == 1
1150 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02001151 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001152 && curbuf->b_p_bh[0] == 'w')
1153 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001154 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001155 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02001156 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001157 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001158 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001159 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001160 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001161 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001162 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001163 --no_u_sync;
1164 if (res == OK)
1165 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001166 // Edit a new, empty buffer. Set options for a "wipeout"
1167 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001168 set_option_value_give_err((char_u *)"swf",
1169 0L, NULL, OPT_LOCAL);
1170 set_option_value_give_err((char_u *)"bl",
1171 0L, NULL, OPT_LOCAL);
1172 set_option_value_give_err((char_u *)"bt",
1173 0L, (char_u *)"nofile", OPT_LOCAL);
1174 set_option_value_give_err((char_u *)"bh",
1175 0L, (char_u *)"wipe", OPT_LOCAL);
1176 set_option_value_give_err((char_u *)"diff",
1177 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001178 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001179 }
1180 if (res == OK)
1181 {
1182 char_u *p, *e;
1183 linenr_T lnum = 0;
1184
1185 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1186 {
1187 e = vim_strchr(p, '\n');
1188 if (e == NULL)
1189 {
1190 ml_append(lnum++, p, 0, FALSE);
1191 break;
1192 }
mathewc51fa7b2023-08-23 20:55:17 +02001193 *e = NUL;
1194 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1195 *e = '\n';
1196 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001197 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001198 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001199 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001200
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001201 // Increase the height of the preview window to show the
1202 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001203 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001204 {
glepnir89a107e2024-12-22 15:16:40 +01001205 lnum = MIN(lnum, p_pvh);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001206 if (curwin->w_height < lnum)
1207 {
1208 win_setheight((int)lnum);
1209 resized = TRUE;
1210 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001211 }
1212
1213 curbuf->b_changed = 0;
1214 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001215 if (pum_selected != prev_selected)
1216 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001217# ifdef FEAT_PROP_POPUP
Girish Palya12b1eb52025-02-24 21:39:42 +01001218 curwin->w_firstline = 0;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001219# endif
1220 curwin->w_topline = 1;
1221 }
1222 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1223 curwin->w_topline = curbuf->b_ml.ml_line_count;
1224 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001225 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001226# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001227 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001228 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001229 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001230 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001231 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001232 }
1233# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001234 if ((curwin != curwin_save && win_valid(curwin_save))
1235 || (curtab != curtab_save
1236 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001237 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001238 int save_redr_status;
1239
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001240 if (curtab != curtab_save && valid_tabpage(curtab_save))
1241 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1242
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001243 // When the first completion is done and the preview
1244 // window is not resized, skip the preview window's
1245 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001246 if (ins_compl_active() && !resized)
1247 curwin->w_redr_status = FALSE;
1248
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001249 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001250 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001251 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001252
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001253 // When the preview window was resized we need to
1254 // update the view on the buffer. Only go back to
1255 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001256 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001257 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001258 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001259 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001260 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001261 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001262 update_topline();
1263 }
1264
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001265 // Update the screen before drawing the popup menu.
1266 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001267 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001268
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001269 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001270 // it causes flicker. When resizing we need to draw
1271 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001272 // Also do not redraw the status line of the original
1273 // current window here, to avoid it gets drawn with
1274 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001275 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001276 save_redr_status = curwin_save->w_redr_status;
1277 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001278 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001279 pum_pretend_not_visible = FALSE;
1280 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001281 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001282
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001283 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001284 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001285# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001286 win_T *wp = curwin;
1287# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001288 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001289 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001290 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001291# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001292 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1293 popup_hide(wp);
1294# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001295 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001296
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001297 // May need to update the screen again when there are
1298 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001299 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001300 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001301 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001302 pum_pretend_not_visible = FALSE;
1303 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001304 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001305 }
1306 }
1307 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001308# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001309 if (WIN_IS_POPUP(curwin))
1310 // can't keep focus in a popup window
1311 win_enter(firstwin, TRUE);
1312# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001313# ifdef FEAT_PROP_POPUP
1314 if (use_popup != USEPOPUP_NONE)
1315 unblock_autocmds();
1316# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001317 }
1318#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001319 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001320#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001321 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001322 // hide any popup info window
1323 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001324#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001325
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001326 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001327}
1328
1329/*
1330 * Undisplay the popup menu (later).
1331 */
1332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001333pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001334{
1335 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001336 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001337 redraw_tabline = TRUE;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001338 if (pum_in_cmdline)
1339 {
1340 clear_cmdline = TRUE;
1341 pum_in_cmdline = FALSE;
1342 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001343 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001344#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001345 // hide any popup info window
1346 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001347#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001348}
1349
1350/*
1351 * Clear the popup menu. Currently only resets the offset to the first
1352 * displayed item.
1353 */
1354 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001355pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001356{
1357 pum_first = 0;
1358}
1359
1360/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001361 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1362 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1363 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001364 */
1365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001366pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001367{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001368 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001369}
1370
Bram Moolenaare3226be2005-12-18 22:10:00 +00001371/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001372 * Return TRUE if the popup can be redrawn in the same position.
1373 */
1374 static int
1375pum_in_same_position(void)
1376{
1377 return pum_window != curwin
1378 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1379 && pum_win_height == curwin->w_height
1380 && pum_win_col == curwin->w_wincol
1381 && pum_win_width == curwin->w_width);
1382}
1383
1384/*
1385 * Return TRUE when pum_may_redraw() will call pum_redraw().
1386 * This means that the pum area should not be overwritten to avoid flicker.
1387 */
1388 int
1389pum_redraw_in_same_position(void)
1390{
1391 if (!pum_visible() || pum_will_redraw)
1392 return FALSE; // nothing to do
1393
1394 return pum_in_same_position();
1395}
1396
1397/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001398 * Reposition the popup menu to adjust for window layout changes.
1399 */
1400 void
1401pum_may_redraw(void)
1402{
1403 pumitem_T *array = pum_array;
1404 int len = pum_size;
1405 int selected = pum_selected;
1406
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001407 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001408 return; // nothing to do
1409
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001410 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001411 {
glepnirc942f842024-12-13 12:13:23 +01001412 pum_redraw(); // Redraw window in same position
Bram Moolenaar491ac282018-06-17 14:47:55 +02001413 }
1414 else
1415 {
1416 int wcol = curwin->w_wcol;
1417
1418 // Window layout changed, recompute the position.
1419 // Use the remembered w_wcol value, the cursor may have moved when a
1420 // completion was inserted, but we want the menu in the same position.
1421 pum_undisplay();
1422 curwin->w_wcol = pum_win_wcol;
1423 curwin->w_valid |= VALID_WCOL;
1424 pum_display(array, len, selected);
1425 curwin->w_wcol = wcol;
1426 }
1427}
1428
1429/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001430 * Return the height of the popup menu, the number of entries visible.
1431 * Only valid when pum_visible() returns TRUE!
1432 */
1433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001434pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001435{
1436 return pum_height;
1437}
1438
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001439#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001440/*
1441 * Add size information about the pum to "dict".
1442 */
1443 void
1444pum_set_event_info(dict_T *dict)
1445{
1446 if (!pum_visible())
1447 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001448 (void)dict_add_number(dict, "height", pum_height);
1449 (void)dict_add_number(dict, "width", pum_width);
1450 (void)dict_add_number(dict, "row", pum_row);
1451 (void)dict_add_number(dict, "col", pum_col);
1452 (void)dict_add_number(dict, "size", pum_size);
1453 (void)dict_add_bool(dict, "scrollbar",
1454 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001455}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001456#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001457
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001458#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001459 static void
1460pum_position_at_mouse(int min_width)
1461{
zeertzjq761a4202024-07-04 17:26:37 +02001462 if (Rows - mouse_row > pum_size || Rows - mouse_row > mouse_row)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001463 {
zeertzjq761a4202024-07-04 17:26:37 +02001464 // Enough space below the mouse row,
1465 // or there is more space below the mouse row than above.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001466 pum_row = mouse_row + 1;
1467 if (pum_height > Rows - pum_row)
1468 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001469 if (pum_row + pum_height > cmdline_row)
1470 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001471 }
1472 else
1473 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001474 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001475 pum_row = mouse_row - pum_size;
1476 if (pum_row < 0)
1477 {
1478 pum_height += pum_row;
1479 pum_row = 0;
1480 }
1481 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001482
zeertzjq883018f2024-06-15 15:37:11 +02001483# ifdef FEAT_RIGHTLEFT
1484 if (pum_rl)
1485 {
1486 if (mouse_col + 1 >= pum_base_width
1487 || mouse_col + 1 > min_width)
1488 // Enough space to show at mouse column.
1489 pum_col = mouse_col;
1490 else
1491 // Not enough space, left align with window.
glepnirc942f842024-12-13 12:13:23 +01001492 pum_col = MIN(pum_base_width, min_width) - 1;
zeertzjq883018f2024-06-15 15:37:11 +02001493 pum_width = pum_col + 1;
1494 }
1495 else
1496# endif
1497 {
1498 if (Columns - mouse_col >= pum_base_width
1499 || Columns - mouse_col > min_width)
1500 // Enough space to show at mouse column.
1501 pum_col = mouse_col;
1502 else
1503 // Not enough space, right align with window.
glepnirc942f842024-12-13 12:13:23 +01001504 pum_col = Columns - MIN(pum_base_width, min_width);
zeertzjq883018f2024-06-15 15:37:11 +02001505 pum_width = Columns - pum_col;
1506 }
1507
glepnir89a107e2024-12-22 15:16:40 +01001508 pum_width = MIN(pum_width, pum_base_width + 1);
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001509 // Do not redraw at cursor position.
1510 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001511}
1512
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001513#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001514
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001515#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001516static pumitem_T *balloon_array = NULL;
1517static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001518
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001519# define BALLOON_MIN_WIDTH 50
1520# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001521
Bram Moolenaar246fe032017-11-19 19:56:27 +01001522typedef struct {
1523 char_u *start;
1524 int bytelen;
1525 int cells;
1526 int indent;
1527} balpart_T;
1528
1529/*
1530 * Split a string into parts to display in the balloon.
1531 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1532 * strings and make a struct look good.
1533 * Resulting array is stored in "array" and returns the size of the array.
1534 */
1535 int
1536split_message(char_u *mesg, pumitem_T **array)
1537{
1538 garray_T ga;
1539 char_u *p;
1540 balpart_T *item;
1541 int quoted = FALSE;
1542 int height;
1543 int line;
1544 int item_idx;
1545 int indent = 0;
1546 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001547 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001548 int long_item_count = 0;
1549 int split_long_items = FALSE;
1550
1551 ga_init2(&ga, sizeof(balpart_T), 20);
1552 p = mesg;
1553
1554 while (*p != NUL)
1555 {
1556 if (ga_grow(&ga, 1) == FAIL)
1557 goto failed;
1558 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1559 item->start = p;
1560 item->indent = indent;
1561 item->cells = indent * 2;
1562 ++ga.ga_len;
1563 while (*p != NUL)
1564 {
1565 if (*p == '"')
1566 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001567 else if (*p == '\n')
1568 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001569 else if (*p == '\\' && p[1] != NUL)
1570 ++p;
1571 else if (!quoted)
1572 {
1573 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1574 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001575 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001576 if (*p == '{')
1577 ++indent;
1578 else if (*p == '}' && indent > 0)
1579 --indent;
1580 ++item->cells;
1581 p = skipwhite(p + 1);
1582 break;
1583 }
1584 }
1585 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001586 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001587 }
1588 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001589 if (*p == '\n')
1590 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001591 if (item->cells > max_cells)
1592 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001593 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001594 }
1595
1596 height = 2 + ga.ga_len;
1597
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001598 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001599 if (long_item_count > 0 && height + long_item_count <= max_height)
1600 {
1601 split_long_items = TRUE;
1602 height += long_item_count;
1603 }
1604
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001605 // Limit to half the window height, it has to fit above or below the mouse
1606 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001607 if (height > max_height)
1608 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001609 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001610 if (*array == NULL)
1611 goto failed;
1612
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001613 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001614 (*array)->pum_text = vim_strsave((char_u *)"");
1615 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1616
1617 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1618 {
1619 int skip;
1620 int thislen;
1621 int copylen;
1622 int ind;
1623 int cells;
1624
1625 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001626 if (item->bytelen == 0)
1627 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1628 else
1629 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001630 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001631 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1632 {
1633 cells = item->indent * 2;
1634 for (p = item->start + skip;
1635 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001636 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001637 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1638 break;
1639 thislen = p - (item->start + skip);
1640 }
1641 else
1642 thislen = item->bytelen;
1643
1644 // put indent at the start
1645 p = alloc(thislen + item->indent * 2 + 1);
1646 if (p == NULL)
1647 {
1648 for (line = 0; line <= height - 1; ++line)
1649 vim_free((*array)[line].pum_text);
1650 vim_free(*array);
1651 goto failed;
1652 }
1653 for (ind = 0; ind < item->indent * 2; ++ind)
1654 p[ind] = ' ';
1655
1656 // exclude spaces at the end of the string
1657 for (copylen = thislen; copylen > 0; --copylen)
1658 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001659 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001660
1661 vim_strncpy(p + ind, item->start + skip, copylen);
1662 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001663 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001664 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001665 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001666 }
1667 ga_clear(&ga);
1668 return height;
1669
1670failed:
1671 ga_clear(&ga);
1672 return 0;
1673}
1674
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001675 void
1676ui_remove_balloon(void)
1677{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001678 if (balloon_array == NULL)
1679 return;
1680
1681 pum_undisplay();
1682 while (balloon_arraysize > 0)
1683 vim_free(balloon_array[--balloon_arraysize].pum_text);
1684 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001685}
1686
1687/*
1688 * Terminal version of a balloon, uses the popup menu code.
1689 */
1690 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001691ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001692{
1693 ui_remove_balloon();
1694
Bram Moolenaar246fe032017-11-19 19:56:27 +01001695 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001696 {
1697 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001698 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001699 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001700 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001701 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001702 listitem_T *li;
1703 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001704
Bram Moolenaar246fe032017-11-19 19:56:27 +01001705 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001706 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001707 if (balloon_array == NULL)
1708 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001709 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001710 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1711 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001712 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001713
1714 balloon_array[idx].pum_text = vim_strsave(
1715 text == NULL ? (char_u *)"" : text);
1716 }
1717 }
1718 else
1719 balloon_arraysize = split_message(mesg, &balloon_array);
1720
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001721 if (balloon_arraysize <= 0)
1722 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001723
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001724 pum_array = balloon_array;
1725 pum_size = balloon_arraysize;
1726 pum_compute_size();
1727 pum_scrollbar = 0;
1728 pum_height = balloon_arraysize;
zeertzjq883018f2024-06-15 15:37:11 +02001729# ifdef FEAT_RIGHTLEFT
1730 pum_rl = curwin->w_p_rl;
1731# endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001732
1733 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1734 pum_selected = -1;
1735 pum_first = 0;
1736 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001737}
1738
1739/*
1740 * Called when the mouse moved, may remove any displayed balloon.
1741 */
1742 void
1743ui_may_remove_balloon(void)
1744{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001745 // For now: remove the balloon whenever the mouse moves to another screen
1746 // cell.
1747 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001748}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001749#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001750
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001751#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001752/*
1753 * Select the pum entry at the mouse position.
1754 */
1755 static void
1756pum_select_mouse_pos(void)
1757{
1758 int idx = mouse_row - pum_row;
1759
zeertzjq761a4202024-07-04 17:26:37 +02001760 if (idx < 0 || idx >= pum_height)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001761 pum_selected = -1;
1762 else if (*pum_array[idx].pum_text != NUL)
1763 pum_selected = idx;
1764}
1765
1766/*
1767 * Execute the currently selected popup menu item.
1768 */
1769 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001770pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001771{
1772 vimmenu_T *mp;
1773 int idx = 0;
1774 exarg_T ea;
1775
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001776 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001777 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001778 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001779 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001780 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001781 break;
1782 }
1783}
1784
1785/*
1786 * Open the terminal version of the popup menu and don't return until it is
1787 * closed.
1788 */
1789 void
1790pum_show_popupmenu(vimmenu_T *menu)
1791{
1792 vimmenu_T *mp;
1793 int idx = 0;
1794 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001795# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001796 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001797# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001798 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001799
1800 pum_undisplay();
1801 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001802 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001803
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001804 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001805 if (menu_is_separator(mp->dname)
1806 || (mp->modes & mp->enabled & mode))
1807 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001808
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001809 // When there are only Terminal mode menus, using "popup Edit" results in
1810 // pum_size being zero.
1811 if (pum_size <= 0)
1812 {
zeertzjq276410e2023-05-07 21:59:33 +01001813 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001814 return;
1815 }
1816
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001817 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001818 if (array == NULL)
1819 return;
1820
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001821 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001822 {
1823 char_u *s = NULL;
1824
1825 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001826 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001827 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001828 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001829 s = mp->dname;
1830 if (s != NULL)
1831 {
1832 s = vim_strsave(s);
1833 if (s != NULL)
1834 array[idx++].pum_text = s;
1835 }
1836 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001837
1838 pum_array = array;
1839 pum_compute_size();
1840 pum_scrollbar = 0;
1841 pum_height = pum_size;
zeertzjq883018f2024-06-15 15:37:11 +02001842# ifdef FEAT_RIGHTLEFT
1843 pum_rl = curwin->w_p_rl;
1844# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001845 pum_position_at_mouse(20);
1846
1847 pum_selected = -1;
1848 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001849# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001850 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001851 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001852# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001853
1854 for (;;)
1855 {
1856 int c;
1857
1858 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001859 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001860 out_flush();
1861
1862 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001863
zeertzjq92a16782022-07-26 12:24:41 +01001864 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1865 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001866 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001867 break;
1868 else if (c == CAR || c == NL)
1869 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001870 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001871 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001872 break;
1873 }
1874 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1875 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001876 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001877 while (pum_selected > 0)
1878 {
1879 --pum_selected;
1880 if (*array[pum_selected].pum_text != NUL)
1881 break;
1882 }
1883 }
1884 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1885 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001886 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001887 while (pum_selected < pum_size - 1)
1888 {
1889 ++pum_selected;
1890 if (*array[pum_selected].pum_text != NUL)
1891 break;
1892 }
1893 }
1894 else if (c == K_RIGHTMOUSE)
1895 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001896 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001897 vungetc(c);
1898 break;
1899 }
1900 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1901 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001902 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001903 pum_select_mouse_pos();
1904 }
1905 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1906 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001907 // left mouse click: select clicked item, if any, and close;
1908 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001909 pum_select_mouse_pos();
1910 if (pum_selected >= 0)
1911 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001912 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001913 break;
1914 }
1915 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1916 break;
1917 }
1918 }
1919
Bram Moolenaar38455a92020-12-24 18:39:02 +01001920 for (idx = 0; idx < pum_size; ++idx)
1921 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001922 vim_free(array);
1923 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001924# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001925 p_bevalterm = save_bevalterm;
1926 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001927# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001928}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001929
1930 void
1931pum_make_popup(char_u *path_name, int use_mouse_pos)
1932{
1933 vimmenu_T *menu;
1934
1935 if (!use_mouse_pos)
1936 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001937 // Hack: set mouse position at the cursor so that the menu pops up
1938 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01001939 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
zeertzjq883018f2024-06-15 15:37:11 +02001940 mouse_col =
1941# ifdef FEAT_RIGHTLEFT
1942 curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
1943# endif
1944 curwin->w_wincol + curwin->w_wcol;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001945 }
1946
1947 menu = gui_find_menu(path_name);
1948 if (menu != NULL)
1949 pum_show_popupmenu(menu);
1950}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001951#endif