blob: eecddc5be60cd26a1540198dd5af112e652be9c0 [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 Moolenaar24959102022-05-07 20:01:16 +0100168 if (State == MODE_CMDLINE)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100169 // for cmdline pum, no need for context lines
170 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200171 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100172 // Leave two lines of context if possible
glepnirc942f842024-12-13 12:13:23 +0100173 context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
Bram Moolenaara5e66212017-09-29 22:42:33 +0200174
Bram Moolenaar491ac282018-06-17 14:47:55 +0200175 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200176 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200177 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200178 pum_height = size;
179 }
180 else
181 {
182 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200183 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200184 }
185 if (p_ph > 0 && pum_height > p_ph)
186 {
187 pum_row += pum_height - p_ph;
188 pum_height = p_ph;
189 }
190 }
191 else
192 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100193 // pum below "pum_win_row"
Bram 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 {
glepnired4eb742025-06-10 20:23:42 +0200199 // Leave three lines of context if possible
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100200 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;
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200229 if (p_pmw > 0 && max_width > p_pmw)
glepnir88d75932025-03-27 20:09:07 +0100230 max_width = p_pmw;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000231
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100232 // Calculate column
Bram Moolenaar24959102022-05-07 20:01:16 +0100233 if (State == MODE_CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000234 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000235 cursor_col = cmdline_compl_startcol();
236 else
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100237 {
238 // w_wcol includes virtual text "above"
239 int wcol = curwin->w_wcol % curwin->w_width;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000240#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200241 if (pum_rl)
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100242 cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
243 else
Bram Moolenaarabc97732007-08-08 20:49:37 +0000244#endif
Bram Moolenaar6ac2e432023-03-31 19:32:29 +0100245 cursor_col = curwin->w_wincol + wcol;
246 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000247
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100248 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200249 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000250 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200251 pum_scrollbar = 1;
252 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000253 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000254 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200255 pum_scrollbar = 0;
256
257 if (def_width < max_width)
258 def_width = max_width;
259
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100260 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000261#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200262 && !pum_rl)
263 || (pum_rl && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000264#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200265 ))
266 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100267 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100268 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000269
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100270 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200271#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200272 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200273 pum_width = pum_col - pum_scrollbar + 1;
274 else
275#endif
276 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000277
glepnirc942f842024-12-13 12:13:23 +0100278 content_width = max_width + pum_kind_width + pum_extra_width + 1;
279 if (pum_width > content_width && pum_width > p_pw)
glepnir88d75932025-03-27 20:09:07 +0100280 {
glepnirc942f842024-12-13 12:13:23 +0100281 // Reduce width to fit item
glepnir88d75932025-03-27 20:09:07 +0100282 pum_width = MAX(content_width, p_pw);
283 if (p_pmw > 0 && pum_width > p_pmw)
284 pum_width = p_pmw;
285 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100286 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100287#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200288 && !pum_rl)
289 || (pum_rl && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100290 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100291#endif
292 ))
293 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100294 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200296 if (pum_rl
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100297 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100298 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100299 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100300 if (pum_col >= Columns)
301 pum_col = Columns - 1;
302 }
zeertzjq883018f2024-06-15 15:37:11 +0200303 else if (!pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304#endif
305 {
glepnirc942f842024-12-13 12:13:23 +0100306 right_edge_col = Columns - max_width - pum_scrollbar;
307 if (curwin->w_wincol > right_edge_col && max_width <= p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100308 // use full width to end of the screen
glepnirc942f842024-12-13 12:13:23 +0100309 pum_col = MAX(0, right_edge_col);
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100310 }
311
312#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200313 if (pum_rl)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100314 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100315 else
316#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100317 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100318
Bram Moolenaar42443c72018-02-10 18:28:52 +0100319 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100320 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100321 pum_width = p_pw;
glepnir88d75932025-03-27 20:09:07 +0100322 if (p_pmw > 0 && pum_width > p_pmw)
323 pum_width = p_pmw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100324#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200325 if (pum_rl)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100326 {
327 if (pum_width > pum_col)
328 pum_width = pum_col;
329 }
330 else
331#endif
332 {
333 if (pum_width >= Columns - pum_col)
334 pum_width = Columns - pum_col - 1;
335 }
336 }
glepnirc942f842024-12-13 12:13:23 +0100337 else if (pum_width > content_width && pum_width > p_pw)
glepnir88d75932025-03-27 20:09:07 +0100338 {
Christian Brabandt618c4d32024-12-13 12:30:20 +0100339 pum_width = MAX(content_width, p_pw);
glepnir88d75932025-03-27 20:09:07 +0100340 if (p_pmw > 0 && pum_width > p_pmw)
341 pum_width = p_pmw;
342 }
glepnir532c5ae2025-03-28 19:21:59 +0100343 else if (p_pmw > 0 && pum_width > p_pmw)
344 {
345 pum_width = p_pmw;
346 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100347 }
348
Bram Moolenaara5e66212017-09-29 22:42:33 +0200349 }
350 else if (Columns < def_width)
351 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100352 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200353#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200354 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200355 pum_col = Columns - 1;
356 else
357#endif
358 pum_col = 0;
359 pum_width = Columns - 1;
glepnir88d75932025-03-27 20:09:07 +0100360 if (p_pmw > 0 && pum_width > p_pmw)
361 pum_width = p_pmw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200362 }
363 else
364 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100365 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100366 max_width = p_pw; // truncate
glepnir88d75932025-03-27 20:09:07 +0100367 if (p_pmw > 0 && max_width > p_pmw)
368 max_width = p_pmw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200369#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200370 if (pum_rl)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200371 pum_col = max_width - 1;
372 else
373#endif
374 pum_col = Columns - max_width;
375 pum_width = max_width - pum_scrollbar;
376 }
377
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100378 // Set selected item and redraw. If the window size changed need to
379 // redo the positioning. Limit this to two times, when there is not
380 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200381 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100382
383 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000384}
385
386/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100387 * Set a flag that when pum_redraw() is called it first calls update_screen().
388 * This will avoid clearing and redrawing the popup menu, prevent flicker.
389 */
390 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000391pum_call_update_screen(void)
Bram Moolenaarae654382019-01-17 21:09:05 +0100392{
393 call_update_screen = TRUE;
394
395 // Update the cursor position to be able to compute the popup menu
396 // position. The cursor line length may have changed because of the
397 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100398 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100399 validate_cursor();
400}
401
402/*
403 * Return TRUE if we are going to redraw the popup menu and the screen position
404 * "row"/"col" is under the popup menu.
405 */
406 int
Bakudankun65555002021-11-17 20:40:16 +0000407pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100408{
Bakudankun65555002021-11-17 20:40:16 +0000409 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100410 && row >= pum_row
411 && row < pum_row + pum_height
412 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200413 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100414}
415
416/*
zeertzjq63901e82024-06-16 16:51:25 +0200417 * Computes attributes of text on the popup menu.
418 * Returns attributes for every cell, or NULL if all attributes are the same.
glepnir40c1c332024-06-11 19:37:04 +0200419 */
zeertzjq63901e82024-06-16 16:51:25 +0200420 static int *
zeertzjq41008522024-07-27 13:21:49 +0200421pum_compute_text_attrs(char_u *text, hlf_T hlf, int user_hlattr)
glepnir40c1c332024-06-11 19:37:04 +0200422{
423 int i;
zeertzjqd8c93402024-06-17 18:25:32 +0200424 size_t leader_len;
zeertzjq63901e82024-06-16 16:51:25 +0200425 int char_cells;
glepnir40c1c332024-06-11 19:37:04 +0200426 int new_attr;
glepnir40c1c332024-06-11 19:37:04 +0200427 char_u *ptr = text;
zeertzjq63901e82024-06-16 16:51:25 +0200428 int cell_idx = 0;
glepnir40c1c332024-06-11 19:37:04 +0200429 garray_T *ga = NULL;
zeertzjq63901e82024-06-16 16:51:25 +0200430 int *attrs = NULL;
zeertzjqd8c93402024-06-17 18:25:32 +0200431 char_u *leader = NULL;
432 int in_fuzzy;
Girish Palya4ec46f32025-03-04 20:56:30 +0100433 int matched_len = -1;
zeertzjq63901e82024-06-16 16:51:25 +0200434 int_u char_pos = 0;
glepnir89a107e2024-12-22 15:16:40 +0100435 int is_select = FALSE;
glepnir40c1c332024-06-11 19:37:04 +0200436
zeertzjq3a0cc362025-01-13 07:27:43 +0100437 if (*text == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
zeertzjqafbe5352024-06-14 20:24:22 +0200438 || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
439 && highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
zeertzjq63901e82024-06-16 16:51:25 +0200440 return NULL;
glepnir40c1c332024-06-11 19:37:04 +0200441
glepnir89a107e2024-12-22 15:16:40 +0100442 is_select = hlf == HLF_PSI;
zeertzjqd8c93402024-06-17 18:25:32 +0200443 leader = State == MODE_CMDLINE ? cmdline_compl_pattern()
444 : ins_compl_leader();
445 if (leader == NULL || *leader == NUL)
446 return NULL;
447
zeertzjq63901e82024-06-16 16:51:25 +0200448 attrs = ALLOC_MULT(int, vim_strsize(text));
449 if (attrs == NULL)
450 return NULL;
451
zeertzjqd8c93402024-06-17 18:25:32 +0200452 in_fuzzy = State == MODE_CMDLINE ? cmdline_compl_is_fuzzy()
453 : (get_cot_flags() & COT_FUZZY) != 0;
454 leader_len = STRLEN(leader);
glepnir40c1c332024-06-11 19:37:04 +0200455
glepnir1c296022024-06-13 17:21:24 +0200456 if (in_fuzzy)
zeertzjq63901e82024-06-16 16:51:25 +0200457 ga = fuzzy_match_str_with_pos(text, leader);
glepnir40c1c332024-06-11 19:37:04 +0200458
zeertzjq63901e82024-06-16 16:51:25 +0200459 while (*ptr != NUL)
glepnir40c1c332024-06-11 19:37:04 +0200460 {
zeertzjqafbe5352024-06-14 20:24:22 +0200461 new_attr = highlight_attr[hlf];
glepnir40c1c332024-06-11 19:37:04 +0200462
zeertzjqafbe5352024-06-14 20:24:22 +0200463 if (ga != NULL)
464 {
465 // Handle fuzzy matching
466 for (i = 0; i < ga->ga_len; i++)
467 {
zeertzjq63901e82024-06-16 16:51:25 +0200468 if (char_pos == ((int_u *)ga->ga_data)[i])
zeertzjqafbe5352024-06-14 20:24:22 +0200469 {
glepnir89a107e2024-12-22 15:16:40 +0100470 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
glepnir9eff3ee2025-01-11 16:47:34 +0100471 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
zeertzjqafbe5352024-06-14 20:24:22 +0200472 break;
473 }
474 }
475 }
Girish Palya4ec46f32025-03-04 20:56:30 +0100476 else
glepnir9eff3ee2025-01-11 16:47:34 +0100477 {
Girish Palya4ec46f32025-03-04 20:56:30 +0100478 if (matched_len < 0 && MB_STRNICMP(ptr, leader, leader_len) == 0)
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +0200479 matched_len = (int)leader_len;
Girish Palya4ec46f32025-03-04 20:56:30 +0100480 if (matched_len > 0)
481 {
482 new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
483 new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
484 matched_len--;
485 }
glepnir9eff3ee2025-01-11 16:47:34 +0100486 }
glepnir40c1c332024-06-11 19:37:04 +0200487
glepnir9eff3ee2025-01-11 16:47:34 +0100488 new_attr = hl_combine_attr(highlight_attr[HLF_PNI], new_attr);
zeertzjq41008522024-07-27 13:21:49 +0200489 if (user_hlattr > 0)
490 new_attr = hl_combine_attr(new_attr, user_hlattr);
glepnir8754efe2024-07-26 18:15:27 +0200491
zeertzjq63901e82024-06-16 16:51:25 +0200492 char_cells = mb_ptr2cells(ptr);
493 for (i = 0; i < char_cells; i++)
494 attrs[cell_idx + i] = new_attr;
495 cell_idx += char_cells;
496
497 MB_PTR_ADV(ptr);
498 char_pos++;
glepnir40c1c332024-06-11 19:37:04 +0200499 }
500
501 if (ga != NULL)
502 {
zeertzjqafbe5352024-06-14 20:24:22 +0200503 ga_clear(ga);
504 vim_free(ga);
glepnir40c1c332024-06-11 19:37:04 +0200505 }
zeertzjq63901e82024-06-16 16:51:25 +0200506 return attrs;
507}
508
509/*
510 * Displays text on the popup menu with specific attributes.
511 */
512 static void
513pum_screen_puts_with_attrs(
514 int row,
515 int col,
516 int cells UNUSED,
517 char_u *text,
518 int textlen,
glepnir8754efe2024-07-26 18:15:27 +0200519 int *attrs)
zeertzjq63901e82024-06-16 16:51:25 +0200520{
521 int col_start = col;
522 char_u *ptr = text;
523 int char_len;
524 int attr;
525
526 // Render text with proper attributes
527 while (*ptr != NUL && ptr < text + textlen)
528 {
529 char_len = mb_ptr2len(ptr);
530#ifdef FEAT_RIGHTLEFT
531 if (pum_rl)
532 attr = attrs[col_start + cells - col - 1];
533 else
534#endif
535 attr = attrs[col - col_start];
zeertzjqd9be94c2024-07-14 10:20:20 +0200536 screen_puts_len(ptr, char_len, row, col, attr);
zeertzjq63901e82024-06-16 16:51:25 +0200537 col += mb_ptr2cells(ptr);
538 ptr += char_len;
539 }
glepnir40c1c332024-06-11 19:37:04 +0200540}
541
glepnir6a89c942024-10-01 20:32:12 +0200542
543 static inline void
544pum_align_order(int *order)
545{
546 int is_default = cia_flags == 0;
547 order[0] = is_default ? CPT_ABBR : cia_flags / 100;
548 order[1] = is_default ? CPT_KIND : (cia_flags / 10) % 10;
549 order[2] = is_default ? CPT_MENU : cia_flags % 10;
550}
551
552 static inline char_u *
553pum_get_item(int index, int type)
554{
555 switch(type)
556 {
557 case CPT_ABBR: return pum_array[index].pum_text;
558 case CPT_KIND: return pum_array[index].pum_kind;
559 case CPT_MENU: return pum_array[index].pum_extra;
560 }
561 return NULL;
562}
563
glepnir89a107e2024-12-22 15:16:40 +0100564 static inline int
565pum_user_attr_combine(int idx, int type, int attr)
566{
567 int user_attr[] = {
568 pum_array[idx].pum_user_abbr_hlattr,
569 pum_array[idx].pum_user_kind_hlattr,
570 };
571
572 return user_attr[type] > 0 ? hl_combine_attr(attr, user_attr[type]) : attr;
573}
574
glepnirc3e71d42025-04-29 18:27:05 +0200575#ifdef FEAT_RIGHTLEFT
576/*
577 * Display RTL text with proper attributes in the popup menu.
578 * Returns the adjusted column position after drawing.
579 */
580 static int
581pum_display_rtl_text(
582 int row,
583 int col,
584 char_u *st,
585 int attr,
586 int *attrs,
587 int width,
588 int width_limit,
589 int totwidth,
glepnir0816f172025-05-18 20:14:53 +0200590 int next_isempty,
591 int selected)
glepnirc3e71d42025-04-29 18:27:05 +0200592{
glepnir0816f172025-05-18 20:14:53 +0200593 char_u *rt = NULL;
594 int cells = 0;
glepnirc3e71d42025-04-29 18:27:05 +0200595 int over_cell = 0;
596 int truncated = FALSE;
597 int pad = next_isempty ? 0 : 2;
glepnir0816f172025-05-18 20:14:53 +0200598 int remaining = 0;
599 int trunc_attr = highlight_attr[selected ? HLF_PSI : HLF_PNI];
glepnirc3e71d42025-04-29 18:27:05 +0200600 int truncrl = curwin->w_fill_chars.truncrl != NUL
601 ? curwin->w_fill_chars.truncrl : '<';
602
603 if (st == NULL)
604 return col;
605
606 rt = reverse_text(st);
607 if (rt == NULL)
608 {
609 VIM_CLEAR(st);
610 return col;
611 }
612
613 char_u *rt_start = rt;
614 cells = mb_string2cells(rt, -1);
615 truncated = width_limit == p_pmw && width_limit - totwidth < cells + pad;
616
617 // only draw the text that fits
618 if (cells > width_limit)
619 {
620 do
621 {
622 cells -= has_mbyte ? (*mb_ptr2cells)(rt) : 1;
623 MB_PTR_ADV(rt);
624 } while (cells > width_limit);
625
626 if (cells < width_limit)
627 {
628 // Most left character requires 2-cells
629 // but only 1 cell is available on screen.
630 // Put a '<' on the left of the pum item.
631 *(--rt) = '<';
632 cells++;
633 }
634 }
635
636 if (truncated)
637 {
638 char_u *orig_rt = rt;
639 int size = 0;
640
641 remaining = width_limit - totwidth - 1;
642 cells = mb_string2cells(rt, -1);
643 if (cells > remaining)
644 {
645 while (cells > remaining)
646 {
647 MB_PTR_ADV(orig_rt);
648 cells -= has_mbyte ? (*mb_ptr2cells)(orig_rt) : 1;
649 }
650 }
651 size = (int)STRLEN(orig_rt);
652 if (cells < remaining)
653 over_cell = remaining - cells;
654
655 cells = mb_string2cells(orig_rt, size);
656 width = cells + over_cell + 1;
657 rt = orig_rt;
658
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200659 screen_putchar(truncrl, row, col - width + 1, trunc_attr);
glepnirc3e71d42025-04-29 18:27:05 +0200660
661 if (over_cell > 0)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200662 screen_fill(row, row + 1, col - width + 2,
663 col - width + 2 + over_cell, ' ', ' ', attr);
glepnirc3e71d42025-04-29 18:27:05 +0200664 }
665
666 if (attrs == NULL)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200667 screen_puts_len(rt, (int)STRLEN(rt), row, col - cells + 1, attr);
glepnirc3e71d42025-04-29 18:27:05 +0200668 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200669 pum_screen_puts_with_attrs(row, col - cells + 1, cells, rt,
670 (int)STRLEN(rt), attrs);
glepnirc3e71d42025-04-29 18:27:05 +0200671
672 vim_free(rt_start);
673 VIM_CLEAR(st);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200674 return col - width;
glepnirc3e71d42025-04-29 18:27:05 +0200675}
676#endif
677
678/*
679 * Display LTR text with proper attributes in the popup menu.
680 * Returns the adjusted column position after drawing.
681 */
682 static int
683pum_display_ltr_text(
684 int row,
685 int col,
686 char_u *st,
687 int attr,
688 int *attrs,
689 int width, // width already calculated in outer loop
690 int width_limit,
691 int totwidth,
glepnir0816f172025-05-18 20:14:53 +0200692 int next_isempty,
693 int selected)
glepnirc3e71d42025-04-29 18:27:05 +0200694{
glepnir0816f172025-05-18 20:14:53 +0200695 int size = 0;
696 int cells = 0;
glepnirc3e71d42025-04-29 18:27:05 +0200697 char_u *st_end = NULL;
698 int over_cell = 0;
699 int pad = next_isempty ? 0 : 2;
glepnir0816f172025-05-18 20:14:53 +0200700 int truncated = FALSE;
701 int remaining = 0;
702 int trunc_attr = highlight_attr[selected ? HLF_PSI : HLF_PNI];
glepnirc3e71d42025-04-29 18:27:05 +0200703 int trunc = curwin->w_fill_chars.trunc != NUL
704 ? curwin->w_fill_chars.trunc : '>';
705
706 if (st == NULL)
707 return col;
708
709 size = (int)STRLEN(st);
710 cells = (*mb_string2cells)(st, size);
711 truncated = width_limit == p_pmw && width_limit - totwidth < cells + pad;
712
713 // only draw the text that fits
714 while (size > 0 && col + cells > width_limit + pum_col)
715 {
716 --size;
717 if (has_mbyte)
718 {
719 size -= (*mb_head_off)(st, st + size);
720 cells -= (*mb_ptr2cells)(st + size);
721 }
722 else
723 --cells;
724 }
725
726 // truncated
727 if (truncated)
728 {
729 remaining = width_limit - totwidth - 1;
730 if (cells > remaining)
731 {
732 st_end = st + size;
733 while (st_end > st && cells > remaining)
734 {
735 MB_PTR_BACK(st, st_end);
736 cells -= has_mbyte ? (*mb_ptr2cells)(st_end) : 1;
737 }
738 size = st_end - st;
739 }
740
741 if (cells < remaining)
742 over_cell = remaining - cells;
743 cells = mb_string2cells(st, size);
744 width = cells + over_cell + 1;
745 }
746
747 if (attrs == NULL)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200748 screen_puts_len(st, size, row, col, attr);
glepnirc3e71d42025-04-29 18:27:05 +0200749 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200750 pum_screen_puts_with_attrs(row, col, cells, st, size, attrs);
glepnirc3e71d42025-04-29 18:27:05 +0200751
752 if (truncated)
753 {
754 if (over_cell > 0)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200755 screen_fill(row, row + 1, col + cells,
756 col + cells + over_cell, ' ', ' ', attr);
glepnirc3e71d42025-04-29 18:27:05 +0200757
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200758 screen_putchar(trunc, row, col + cells + over_cell, trunc_attr);
glepnirc3e71d42025-04-29 18:27:05 +0200759 }
760
761 VIM_CLEAR(st);
762 return col + width;
763}
764
765
766/*
767 * Process and display a single popup menu item (text/kind/extra).
768 * Returns the new column position after drawing.
769 */
770 static int
771pum_process_item(
772 int row,
773 int col,
774 int idx,
775 int j, // Current position in order array
776 int *order, // Order array
777 hlf_T hlf,
778 int attr,
779 int *totwidth_ptr,
780 int next_isempty)
781{
782 int item_type = order[j];
783 char_u *s = NULL;
784 char_u *p = pum_get_item(idx, item_type);
785 int width = 0; // item width
786 int w; // char width
glepnir0816f172025-05-18 20:14:53 +0200787 int selected = idx == pum_selected;
glepnirc3e71d42025-04-29 18:27:05 +0200788
789 for ( ; ; MB_PTR_ADV(p))
790 {
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200791 if (s == NULL)
792 s = p;
793 w = ptr2cells(p);
794 if (*p != NUL && *p != TAB && *totwidth_ptr + w <= pum_width)
795 {
796 width += w;
797 continue;
798 }
glepnirc3e71d42025-04-29 18:27:05 +0200799
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200800 // Display the text that fits or comes before a Tab.
801 // First convert it to printable characters.
802 char_u *st;
803 int *attrs = NULL;
804 int saved = *p;
glepnirc3e71d42025-04-29 18:27:05 +0200805
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200806 if (saved != NUL)
807 *p = NUL;
808 st = transstr(s);
809 if (saved != NUL)
810 *p = saved;
glepnirc3e71d42025-04-29 18:27:05 +0200811
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200812 if (item_type == CPT_ABBR)
813 attrs = pum_compute_text_attrs(st, hlf,
814 pum_array[idx].pum_user_abbr_hlattr);
glepnirc3e71d42025-04-29 18:27:05 +0200815#ifdef FEAT_RIGHTLEFT
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200816 if (pum_rl)
817 col = pum_display_rtl_text(row, col, st, attr, attrs,
glepnir0816f172025-05-18 20:14:53 +0200818 width, pum_width, *totwidth_ptr, next_isempty, selected);
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200819 else
glepnirc3e71d42025-04-29 18:27:05 +0200820#endif
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200821 col = pum_display_ltr_text(row, col, st, attr, attrs,
glepnir0816f172025-05-18 20:14:53 +0200822 width, pum_width, *totwidth_ptr, next_isempty, selected);
glepnirc3e71d42025-04-29 18:27:05 +0200823
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200824 if (attrs != NULL)
825 VIM_CLEAR(attrs);
glepnirc3e71d42025-04-29 18:27:05 +0200826
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200827 if (*p != TAB)
828 break;
glepnirc3e71d42025-04-29 18:27:05 +0200829
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200830 // Display two spaces for a Tab.
glepnirc3e71d42025-04-29 18:27:05 +0200831#ifdef FEAT_RIGHTLEFT
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200832 if (pum_rl)
833 {
834 screen_puts_len((char_u *)" ", 2, row, col - 1, attr);
835 col -= 2;
836 }
837 else
glepnirc3e71d42025-04-29 18:27:05 +0200838#endif
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200839 {
840 screen_puts_len((char_u *)" ", 2, row, col, attr);
841 col += 2;
842 }
843 *totwidth_ptr += 2;
844 s = NULL; // start text at next char
845 width = 0;
glepnirc3e71d42025-04-29 18:27:05 +0200846 }
847
848 return col;
849}
850
851/*
852 * Draw the scrollbar for the popup menu.
853 */
854 static void
855pum_draw_scrollbar(
856 int row,
857 int i,
858 int thumb_pos,
859 int thumb_height)
860{
861 if (pum_scrollbar <= 0)
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200862 return;
glepnirc3e71d42025-04-29 18:27:05 +0200863
864 int attr = (i >= thumb_pos && i < thumb_pos + thumb_height) ?
865 highlight_attr[HLF_PST] : highlight_attr[HLF_PSB];
866
867#ifdef FEAT_RIGHTLEFT
868 if (pum_rl)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200869 screen_putchar(' ', row, pum_col - pum_width, attr);
glepnirc3e71d42025-04-29 18:27:05 +0200870 else
871#endif
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200872 screen_putchar(' ', row, pum_col + pum_width, attr);
glepnirc3e71d42025-04-29 18:27:05 +0200873}
874
glepnir40c1c332024-06-11 19:37:04 +0200875/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000876 * Redraw the popup menu, using "pum_first" and "pum_selected".
877 */
878 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100879pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000880{
881 int row = pum_row;
882 int col;
zeertzjqafbe5352024-06-14 20:24:22 +0200883 hlf_T *hlfs; // array used for highlights
884 hlf_T hlf;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000885 int attr;
glepnir6a89c942024-10-01 20:32:12 +0200886 int i, j;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000887 int idx;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000888 char_u *p = NULL;
glepnirc3e71d42025-04-29 18:27:05 +0200889 int totwidth;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000890 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100891 int thumb_height = 1;
glepnir6a89c942024-10-01 20:32:12 +0200892 int item_type;
893 int order[3];
894 int next_isempty = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000895 int n;
glepnir6a89c942024-10-01 20:32:12 +0200896 int items_width_array[3] = { pum_base_width, pum_kind_width,
897 pum_extra_width };
898 int basic_width; // first item width
899 int last_isabbr = FALSE;
glepnir0fe17f82024-10-08 22:26:44 +0200900 int orig_attr = -1;
glepnir89a107e2024-12-22 15:16:40 +0100901 int scroll_range = pum_size - pum_height;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000902
zeertzjqafbe5352024-06-14 20:24:22 +0200903 hlf_T hlfsNorm[3];
904 hlf_T hlfsSel[3];
glepnir6a89c942024-10-01 20:32:12 +0200905 // "word"/"abbr"
zeertzjqafbe5352024-06-14 20:24:22 +0200906 hlfsNorm[0] = HLF_PNI;
907 hlfsSel[0] = HLF_PSI;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000908 // "kind"
zeertzjqafbe5352024-06-14 20:24:22 +0200909 hlfsNorm[1] = HLF_PNK;
910 hlfsSel[1] = HLF_PSK;
Bram Moolenaare638acc2023-03-15 17:08:51 +0000911 // "extra text"
zeertzjqafbe5352024-06-14 20:24:22 +0200912 hlfsNorm[2] = HLF_PNX;
913 hlfsSel[2] = HLF_PSX;
Gianmaria Bajo6a7c7742023-03-10 16:35:53 +0000914
Bram Moolenaarae654382019-01-17 21:09:05 +0100915 if (call_update_screen)
916 {
917 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100918 // Do not redraw in pum_may_redraw() and don't draw in the area where
919 // the popup menu will be.
920 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100921 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100922 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100923 }
924
925 // never display more than we have
glepnir89a107e2024-12-22 15:16:40 +0100926 pum_first = MIN(pum_first, scroll_range);
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100927
Bram Moolenaar4b779472005-10-04 09:12:31 +0000928 if (pum_scrollbar)
929 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100930 thumb_height = pum_height * pum_height / pum_size;
931 if (thumb_height == 0)
932 thumb_height = 1;
933 thumb_pos = (pum_first * (pum_height - thumb_height)
glepnir89a107e2024-12-22 15:16:40 +0100934 + scroll_range / 2) / scroll_range;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000935 }
936
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100937#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200938 // The popup menu is drawn over popup menus with zindex under
939 // POPUPMENU_ZINDEX.
940 screen_zindex = POPUPMENU_ZINDEX;
941#endif
942
Bram Moolenaar4b779472005-10-04 09:12:31 +0000943 for (i = 0; i < pum_height; ++i)
944 {
945 idx = i + pum_first;
zeertzjqafbe5352024-06-14 20:24:22 +0200946 hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
947 hlf = hlfs[0]; // start with "word" highlight
948 attr = highlight_attr[hlf];
Bram Moolenaar4b779472005-10-04 09:12:31 +0000949
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100950 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000951#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200952 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000953 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200954 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200955 screen_putchar(' ', row, pum_col + 1, attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000956 }
957 else
958#endif
959 if (pum_col > 0)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200960 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000961
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100962 // Display each entry, use two spaces for a Tab.
glepnir6a89c942024-10-01 20:32:12 +0200963 // Do this 3 times and order from p_cia
Bram Moolenaar4b779472005-10-04 09:12:31 +0000964 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000965 totwidth = 0;
glepnir6a89c942024-10-01 20:32:12 +0200966 pum_align_order(order);
967 basic_width = items_width_array[order[0]];
968 last_isabbr = order[2] == CPT_ABBR;
969 for (j = 0; j < 3; ++j)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000970 {
glepnir6a89c942024-10-01 20:32:12 +0200971 item_type = order[j];
972 hlf = hlfs[item_type];
zeertzjqafbe5352024-06-14 20:24:22 +0200973 attr = highlight_attr[hlf];
glepnir0fe17f82024-10-08 22:26:44 +0200974 orig_attr = attr;
glepnir89a107e2024-12-22 15:16:40 +0100975 if (item_type < 2) // try combine attr with user custom
976 attr = pum_user_attr_combine(idx, item_type, attr);
glepnir6a89c942024-10-01 20:32:12 +0200977 p = pum_get_item(idx, item_type);
glepnir32f2bb62025-04-15 19:06:58 +0200978
979 if (j + 1 < 3)
980 next_isempty = pum_get_item(idx, order[j + 1]) == NULL;
981
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000982 if (p != NULL)
glepnirc3e71d42025-04-29 18:27:05 +0200983 // Process and display the item
984 col = pum_process_item(row, col, idx, j, order, hlf, attr,
985 &totwidth, next_isempty);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000986
glepnir6a89c942024-10-01 20:32:12 +0200987 if (j > 0)
988 n = items_width_array[order[1]] + (last_isabbr ? 0 : 1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000989 else
glepnir6a89c942024-10-01 20:32:12 +0200990 n = order[j] == CPT_ABBR ? 1 : 0;
991
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100992 // Stop when there is nothing more to display.
glepnir6a89c942024-10-01 20:32:12 +0200993 if (j == 2
994 || (next_isempty && (j == 1 || (j == 0
995 && pum_get_item(idx, order[j + 2]) == NULL)))
glepnira6d9e3c2024-10-03 11:01:19 +0200996 || basic_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000997 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000998#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +0200999 if (pum_rl)
Bram Moolenaarabc97732007-08-08 20:49:37 +00001000 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001001 screen_fill(row, row + 1, pum_col - basic_width - n + 1,
1002 col + 1, ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +02001003 col = pum_col - basic_width - n;
Bram Moolenaarabc97732007-08-08 20:49:37 +00001004 }
1005 else
1006#endif
1007 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001008 screen_fill(row, row + 1, col, pum_col + basic_width + n,
1009 ' ', ' ', orig_attr);
glepnir6a89c942024-10-01 20:32:12 +02001010 col = pum_col + basic_width + n;
Bram Moolenaarabc97732007-08-08 20:49:37 +00001011 }
glepnir6a89c942024-10-01 20:32:12 +02001012 totwidth = basic_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001013 }
1014
Bram Moolenaarabc97732007-08-08 20:49:37 +00001015#ifdef FEAT_RIGHTLEFT
zeertzjq883018f2024-06-15 15:37:11 +02001016 if (pum_rl)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001017 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
1018 ' ', orig_attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +00001019 else
1020#endif
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001021 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
1022 orig_attr);
glepnirc3e71d42025-04-29 18:27:05 +02001023 pum_draw_scrollbar(row, i, thumb_pos, thumb_height);
Bram Moolenaar4b779472005-10-04 09:12:31 +00001024
1025 ++row;
1026 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001027
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001028#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02001029 screen_zindex = 0;
1030#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001031}
1032
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001033#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001034/*
1035 * Position the info popup relative to the popup menu item.
1036 */
1037 void
1038pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001039{
Bram Moolenaar202c3f72019-11-21 12:12:35 +01001040 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001041 int row = pum_row;
1042 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001043 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001044
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001045 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001046 if (Columns - col < 20 && Columns - col < pum_col)
1047 {
1048 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001049 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001050 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001051 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001052 }
1053 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001054 wp->w_maxwidth = Columns - col + 1;
1055 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001056 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
1057 {
1058 // option value overrules computed value
1059 wp->w_maxwidth = wp->w_maxwidth_opt;
1060 used_maxwidth_opt = TRUE;
1061 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001062
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001063 row -= popup_top_extra(wp);
1064 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001065 {
1066 if (pum_row < pum_win_row)
1067 {
1068 // menu above cursor line, align with bottom
1069 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001070 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001071 }
1072 else
1073 // menu below cursor line, align with top
1074 row += 1;
1075 }
1076 else
1077 // align with the selected item
1078 row += pum_selected - pum_first + 1;
1079
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01001080 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +02001081 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01001082 // The popup is not going to fit or will overlap with the cursor
1083 // position, hide the popup.
1084 wp->w_popup_flags |= POPF_HIDDEN;
1085 else
1086 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001087}
1088#endif
1089
Bram Moolenaar4b779472005-10-04 09:12:31 +00001090/*
1091 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001092 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001093 * This may be repeated when the preview window is used:
1094 * "repeat" == 0: open preview window normally
1095 * "repeat" == 1: open preview window but don't set the size
1096 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001097 * Returns TRUE when the window was resized and the location of the popup menu
1098 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001099 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001100 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001101pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001102{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001103 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001104 int context = pum_height / 2;
glepnir8d0bb6d2024-12-24 09:44:35 +01001105 int scroll_offset;
Bram Moolenaareaf35242019-08-20 23:14:15 +02001106#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001107 int prev_selected = pum_selected;
zeertzjq529b9ad2024-06-05 20:27:06 +02001108 unsigned cur_cot_flags = get_cot_flags();
Bram Moolenaareaf35242019-08-20 23:14:15 +02001109#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001110#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001111 int has_info = FALSE;
1112#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001113
Bram Moolenaar4b779472005-10-04 09:12:31 +00001114 pum_selected = n;
glepnir8d0bb6d2024-12-24 09:44:35 +01001115 scroll_offset = pum_selected - pum_height;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001116
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001117 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001118 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00001119 if (pum_first > pum_selected - 4)
1120 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001121 // scroll down; when we did a jump it's probably a PageUp then
1122 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +00001123 if (pum_first > pum_selected - 2)
1124 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001125 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001126 if (pum_first < 0)
1127 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001128 else if (pum_first > pum_selected)
1129 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001130 }
1131 else
1132 pum_first = pum_selected;
1133 }
glepnir89a107e2024-12-22 15:16:40 +01001134 else if (pum_first < scroll_offset + 5)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001135 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001136 // scroll up; when we did a jump it's probably a PageDown then
1137 // scroll a whole page
glepnir89a107e2024-12-22 15:16:40 +01001138 if (pum_first < scroll_offset + 3)
glepnir8d0bb6d2024-12-24 09:44:35 +01001139 pum_first = MAX(pum_first + pum_height - 2, scroll_offset + 1);
Bram Moolenaare3226be2005-12-18 22:10:00 +00001140 else
glepnir89a107e2024-12-22 15:16:40 +01001141 pum_first = scroll_offset + 1;
Bram Moolenaare3226be2005-12-18 22:10:00 +00001142 }
Bram Moolenaar4b779472005-10-04 09:12:31 +00001143
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001144 // Give a few lines of context when possible.
glepnir89a107e2024-12-22 15:16:40 +01001145 context = MIN(context, 3);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001146 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001147 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001148 if (pum_first > pum_selected - context)
glepnir89a107e2024-12-22 15:16:40 +01001149 pum_first = MAX(pum_selected - context, 0); // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001150 else if (pum_first < pum_selected + context - pum_height + 1)
glepnir89a107e2024-12-22 15:16:40 +01001151 pum_first = pum_selected + context - pum_height + 1; // up
Bram Moolenaar4b779472005-10-04 09:12:31 +00001152 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001153 // adjust for the number of lines displayed
glepnirc942f842024-12-13 12:13:23 +01001154 pum_first = MIN(pum_first, pum_size - pum_height);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001155
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001157 /*
1158 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +01001159 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001160 * Skip this when tried twice already.
1161 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001162 * NOTE: Be very careful not to sync undo!
1163 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001164 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001165 && Rows > 10
1166 && repeat <= 1
zeertzjq529b9ad2024-06-05 20:27:06 +02001167 && (cur_cot_flags & COT_ANY_PREVIEW))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001168 {
1169 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001170 tabpage_T *curtab_save = curtab;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001171# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001172 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001173# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +01001174# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001175# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001176# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001177 has_info = TRUE;
zeertzjq529b9ad2024-06-05 20:27:06 +02001178 if (cur_cot_flags & COT_POPUPHIDDEN)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001179 use_popup = USEPOPUP_HIDDEN;
zeertzjq529b9ad2024-06-05 20:27:06 +02001180 else if (cur_cot_flags & COT_POPUP)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001181 use_popup = USEPOPUP_NORMAL;
1182 else
1183 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001184 if (use_popup != USEPOPUP_NONE)
1185 // don't use WinEnter or WinLeave autocommands for the info
1186 // popup
1187 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +02001188# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001189 // Open a preview window and set "curwin" to it.
1190 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001191 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +02001192 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
1193 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +02001194 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001195 // Prevent undo sync here, if an autocommand syncs undo weird
1196 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +01001197 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001198 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001199 --no_u_sync;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001200 if (RedrawingDisabled > 0)
1201 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001202 g_do_tagpreview = 0;
1203
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001204 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001205# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001206 || (curwin->w_popup_flags & POPF_INFO)
1207# endif
1208 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001209 {
mathew20f61d92023-08-26 18:11:02 +02001210 int res = OK;
1211
Bram Moolenaar50e53762016-10-27 14:49:15 +02001212 if (!resized
1213 && curbuf->b_nwindows == 1
1214 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +02001215 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001216 && curbuf->b_p_bh[0] == 'w')
1217 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001218 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001219 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +02001220 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001221 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001222 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001223 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001224 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001225 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001226 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001227 --no_u_sync;
1228 if (res == OK)
1229 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001230 // Edit a new, empty buffer. Set options for a "wipeout"
1231 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001232 set_option_value_give_err((char_u *)"swf",
1233 0L, NULL, OPT_LOCAL);
1234 set_option_value_give_err((char_u *)"bl",
1235 0L, NULL, OPT_LOCAL);
1236 set_option_value_give_err((char_u *)"bt",
1237 0L, (char_u *)"nofile", OPT_LOCAL);
1238 set_option_value_give_err((char_u *)"bh",
1239 0L, (char_u *)"wipe", OPT_LOCAL);
1240 set_option_value_give_err((char_u *)"diff",
1241 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001242 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001243 }
1244 if (res == OK)
1245 {
1246 char_u *p, *e;
1247 linenr_T lnum = 0;
1248
1249 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
1250 {
1251 e = vim_strchr(p, '\n');
1252 if (e == NULL)
1253 {
1254 ml_append(lnum++, p, 0, FALSE);
1255 break;
1256 }
mathewc51fa7b2023-08-23 20:55:17 +02001257 *e = NUL;
1258 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
1259 *e = '\n';
1260 p = e + 1;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001261 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001262 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +02001263 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001264
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001265 // Increase the height of the preview window to show the
1266 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001267 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001268 {
glepnir89a107e2024-12-22 15:16:40 +01001269 lnum = MIN(lnum, p_pvh);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001270 if (curwin->w_height < lnum)
1271 {
1272 win_setheight((int)lnum);
1273 resized = TRUE;
1274 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001275 }
1276
1277 curbuf->b_changed = 0;
1278 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001279 if (pum_selected != prev_selected)
1280 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001281# ifdef FEAT_PROP_POPUP
Girish Palya12b1eb52025-02-24 21:39:42 +01001282 curwin->w_firstline = 0;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +02001283# endif
1284 curwin->w_topline = 1;
1285 }
1286 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1287 curwin->w_topline = curbuf->b_ml.ml_line_count;
1288 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001289 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001290# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001291 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001292 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001293 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001294 if (win_valid(curwin_save))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001295 redraw_win_later(curwin_save, UPD_SOME_VALID);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +02001296 }
1297# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001298 if ((curwin != curwin_save && win_valid(curwin_save))
1299 || (curtab != curtab_save
1300 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001301 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001302 int save_redr_status;
1303
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02001304 if (curtab != curtab_save && valid_tabpage(curtab_save))
1305 goto_tabpage_tp(curtab_save, FALSE, FALSE);
1306
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001307 // When the first completion is done and the preview
1308 // window is not resized, skip the preview window's
1309 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +02001310 if (ins_compl_active() && !resized)
1311 curwin->w_redr_status = FALSE;
1312
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001313 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001314 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001315 redraw_later(UPD_SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001316
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001317 // When the preview window was resized we need to
1318 // update the view on the buffer. Only go back to
1319 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001320 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001321 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001322 {
Bram Moolenaare7d13762015-10-30 14:23:33 +01001323 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001324 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001325 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001326 update_topline();
1327 }
1328
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001329 // Update the screen before drawing the popup menu.
1330 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001331 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001332
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001333 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001334 // it causes flicker. When resizing we need to draw
1335 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001336 // Also do not redraw the status line of the original
1337 // current window here, to avoid it gets drawn with
1338 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001339 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001340 save_redr_status = curwin_save->w_redr_status;
1341 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001342 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001343 pum_pretend_not_visible = FALSE;
1344 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +02001345 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001346
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00001347 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +01001348 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001349# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001350 win_T *wp = curwin;
1351# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001352 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001353 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001354 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001355# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001356 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1357 popup_hide(wp);
1358# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001359 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001360
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001361 // May need to update the screen again when there are
1362 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001363 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001364 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001365 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001366 pum_pretend_not_visible = FALSE;
1367 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001368 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001369 }
1370 }
1371 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001372# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001373 if (WIN_IS_POPUP(curwin))
1374 // can't keep focus in a popup window
1375 win_enter(firstwin, TRUE);
1376# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001377# ifdef FEAT_PROP_POPUP
1378 if (use_popup != USEPOPUP_NONE)
1379 unblock_autocmds();
1380# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001381 }
1382#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001383 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001384#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001385 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001386 // hide any popup info window
1387 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001388#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001389
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001390 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001391}
1392
1393/*
1394 * Undisplay the popup menu (later).
1395 */
1396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001397pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001398{
1399 pum_array = NULL;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001400 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001401 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02001402#if defined(FEAT_TABPANEL)
1403 redraw_tabpanel = TRUE;
1404#endif
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001405 if (pum_in_cmdline)
1406 {
1407 clear_cmdline = TRUE;
1408 pum_in_cmdline = FALSE;
1409 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001410 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001411#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001412 // hide any popup info window
1413 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001414#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001415}
1416
1417/*
1418 * Clear the popup menu. Currently only resets the offset to the first
1419 * displayed item.
1420 */
1421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001422pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001423{
1424 pum_first = 0;
1425}
1426
1427/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001428 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1429 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1430 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001431 */
1432 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001433pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001434{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001435 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001436}
1437
Bram Moolenaare3226be2005-12-18 22:10:00 +00001438/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001439 * Return TRUE if the popup can be redrawn in the same position.
1440 */
1441 static int
1442pum_in_same_position(void)
1443{
1444 return pum_window != curwin
1445 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1446 && pum_win_height == curwin->w_height
1447 && pum_win_col == curwin->w_wincol
1448 && pum_win_width == curwin->w_width);
1449}
1450
1451/*
1452 * Return TRUE when pum_may_redraw() will call pum_redraw().
1453 * This means that the pum area should not be overwritten to avoid flicker.
1454 */
1455 int
1456pum_redraw_in_same_position(void)
1457{
1458 if (!pum_visible() || pum_will_redraw)
1459 return FALSE; // nothing to do
1460
1461 return pum_in_same_position();
1462}
1463
1464/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001465 * Reposition the popup menu to adjust for window layout changes.
1466 */
1467 void
1468pum_may_redraw(void)
1469{
1470 pumitem_T *array = pum_array;
1471 int len = pum_size;
1472 int selected = pum_selected;
1473
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001474 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001475 return; // nothing to do
1476
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001477 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001478 {
glepnirc942f842024-12-13 12:13:23 +01001479 pum_redraw(); // Redraw window in same position
Bram Moolenaar491ac282018-06-17 14:47:55 +02001480 }
1481 else
1482 {
1483 int wcol = curwin->w_wcol;
1484
1485 // Window layout changed, recompute the position.
1486 // Use the remembered w_wcol value, the cursor may have moved when a
1487 // completion was inserted, but we want the menu in the same position.
1488 pum_undisplay();
1489 curwin->w_wcol = pum_win_wcol;
1490 curwin->w_valid |= VALID_WCOL;
1491 pum_display(array, len, selected);
1492 curwin->w_wcol = wcol;
1493 }
1494}
1495
1496/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001497 * Return the height of the popup menu, the number of entries visible.
1498 * Only valid when pum_visible() returns TRUE!
1499 */
1500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001501pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001502{
1503 return pum_height;
1504}
1505
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001506#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001507/*
1508 * Add size information about the pum to "dict".
1509 */
1510 void
1511pum_set_event_info(dict_T *dict)
1512{
1513 if (!pum_visible())
1514 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001515 (void)dict_add_number(dict, "height", pum_height);
1516 (void)dict_add_number(dict, "width", pum_width);
1517 (void)dict_add_number(dict, "row", pum_row);
1518 (void)dict_add_number(dict, "col", pum_col);
1519 (void)dict_add_number(dict, "size", pum_size);
1520 (void)dict_add_bool(dict, "scrollbar",
1521 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001522}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001523#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001524
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001525#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001526 static void
1527pum_position_at_mouse(int min_width)
1528{
zeertzjq761a4202024-07-04 17:26:37 +02001529 if (Rows - mouse_row > pum_size || Rows - mouse_row > mouse_row)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001530 {
zeertzjq761a4202024-07-04 17:26:37 +02001531 // Enough space below the mouse row,
1532 // or there is more space below the mouse row than above.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001533 pum_row = mouse_row + 1;
1534 if (pum_height > Rows - pum_row)
1535 pum_height = Rows - pum_row;
Luuk van Baaldcd40cf2023-04-23 16:24:08 +01001536 if (pum_row + pum_height > cmdline_row)
1537 pum_in_cmdline = TRUE;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001538 }
1539 else
1540 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001541 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001542 pum_row = mouse_row - pum_size;
1543 if (pum_row < 0)
1544 {
1545 pum_height += pum_row;
1546 pum_row = 0;
1547 }
1548 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001549
zeertzjq883018f2024-06-15 15:37:11 +02001550# ifdef FEAT_RIGHTLEFT
1551 if (pum_rl)
1552 {
1553 if (mouse_col + 1 >= pum_base_width
1554 || mouse_col + 1 > min_width)
1555 // Enough space to show at mouse column.
1556 pum_col = mouse_col;
1557 else
1558 // Not enough space, left align with window.
glepnirc942f842024-12-13 12:13:23 +01001559 pum_col = MIN(pum_base_width, min_width) - 1;
zeertzjq883018f2024-06-15 15:37:11 +02001560 pum_width = pum_col + 1;
1561 }
1562 else
1563# endif
1564 {
1565 if (Columns - mouse_col >= pum_base_width
1566 || Columns - mouse_col > min_width)
1567 // Enough space to show at mouse column.
1568 pum_col = mouse_col;
1569 else
1570 // Not enough space, right align with window.
glepnirc942f842024-12-13 12:13:23 +01001571 pum_col = Columns - MIN(pum_base_width, min_width);
zeertzjq883018f2024-06-15 15:37:11 +02001572 pum_width = Columns - pum_col;
1573 }
1574
glepnir89a107e2024-12-22 15:16:40 +01001575 pum_width = MIN(pum_width, pum_base_width + 1);
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001576 // Do not redraw at cursor position.
1577 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001578}
1579
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001580#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001581
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001582#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001583static pumitem_T *balloon_array = NULL;
1584static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001585
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001586# define BALLOON_MIN_WIDTH 50
1587# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001588
Bram Moolenaar246fe032017-11-19 19:56:27 +01001589typedef struct {
1590 char_u *start;
1591 int bytelen;
1592 int cells;
1593 int indent;
1594} balpart_T;
1595
1596/*
1597 * Split a string into parts to display in the balloon.
1598 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1599 * strings and make a struct look good.
1600 * Resulting array is stored in "array" and returns the size of the array.
1601 */
1602 int
1603split_message(char_u *mesg, pumitem_T **array)
1604{
1605 garray_T ga;
1606 char_u *p;
1607 balpart_T *item;
1608 int quoted = FALSE;
1609 int height;
1610 int line;
1611 int item_idx;
1612 int indent = 0;
1613 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001614 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001615 int long_item_count = 0;
1616 int split_long_items = FALSE;
1617
1618 ga_init2(&ga, sizeof(balpart_T), 20);
1619 p = mesg;
1620
1621 while (*p != NUL)
1622 {
1623 if (ga_grow(&ga, 1) == FAIL)
1624 goto failed;
1625 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1626 item->start = p;
1627 item->indent = indent;
1628 item->cells = indent * 2;
1629 ++ga.ga_len;
1630 while (*p != NUL)
1631 {
1632 if (*p == '"')
1633 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001634 else if (*p == '\n')
1635 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001636 else if (*p == '\\' && p[1] != NUL)
1637 ++p;
1638 else if (!quoted)
1639 {
1640 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1641 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001642 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001643 if (*p == '{')
1644 ++indent;
1645 else if (*p == '}' && indent > 0)
1646 --indent;
1647 ++item->cells;
1648 p = skipwhite(p + 1);
1649 break;
1650 }
1651 }
1652 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001653 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001654 }
1655 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001656 if (*p == '\n')
1657 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001658 if (item->cells > max_cells)
1659 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001660 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001661 }
1662
1663 height = 2 + ga.ga_len;
1664
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001665 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001666 if (long_item_count > 0 && height + long_item_count <= max_height)
1667 {
1668 split_long_items = TRUE;
1669 height += long_item_count;
1670 }
1671
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001672 // Limit to half the window height, it has to fit above or below the mouse
1673 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001674 if (height > max_height)
1675 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001676 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001677 if (*array == NULL)
1678 goto failed;
1679
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001680 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001681 (*array)->pum_text = vim_strsave((char_u *)"");
1682 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1683
1684 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1685 {
1686 int skip;
1687 int thislen;
1688 int copylen;
1689 int ind;
1690 int cells;
1691
1692 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001693 if (item->bytelen == 0)
1694 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1695 else
1696 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001697 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001698 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1699 {
1700 cells = item->indent * 2;
1701 for (p = item->start + skip;
1702 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001703 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001704 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1705 break;
1706 thislen = p - (item->start + skip);
1707 }
1708 else
1709 thislen = item->bytelen;
1710
1711 // put indent at the start
1712 p = alloc(thislen + item->indent * 2 + 1);
1713 if (p == NULL)
1714 {
1715 for (line = 0; line <= height - 1; ++line)
1716 vim_free((*array)[line].pum_text);
1717 vim_free(*array);
1718 goto failed;
1719 }
1720 for (ind = 0; ind < item->indent * 2; ++ind)
1721 p[ind] = ' ';
1722
1723 // exclude spaces at the end of the string
1724 for (copylen = thislen; copylen > 0; --copylen)
1725 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001726 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001727
1728 vim_strncpy(p + ind, item->start + skip, copylen);
1729 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001730 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001731 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001732 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001733 }
1734 ga_clear(&ga);
1735 return height;
1736
1737failed:
1738 ga_clear(&ga);
1739 return 0;
1740}
1741
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001742 void
1743ui_remove_balloon(void)
1744{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001745 if (balloon_array == NULL)
1746 return;
1747
1748 pum_undisplay();
1749 while (balloon_arraysize > 0)
1750 vim_free(balloon_array[--balloon_arraysize].pum_text);
1751 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001752}
1753
1754/*
1755 * Terminal version of a balloon, uses the popup menu code.
1756 */
1757 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001758ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001759{
1760 ui_remove_balloon();
1761
Bram Moolenaar246fe032017-11-19 19:56:27 +01001762 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001763 {
1764 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001765 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001766 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001767 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001768 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001769 listitem_T *li;
1770 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001771
Bram Moolenaar246fe032017-11-19 19:56:27 +01001772 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001773 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001774 if (balloon_array == NULL)
1775 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001776 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001777 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001779 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001780
1781 balloon_array[idx].pum_text = vim_strsave(
1782 text == NULL ? (char_u *)"" : text);
1783 }
1784 }
1785 else
1786 balloon_arraysize = split_message(mesg, &balloon_array);
1787
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001788 if (balloon_arraysize <= 0)
1789 return;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001790
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001791 pum_array = balloon_array;
1792 pum_size = balloon_arraysize;
1793 pum_compute_size();
1794 pum_scrollbar = 0;
1795 pum_height = balloon_arraysize;
zeertzjq883018f2024-06-15 15:37:11 +02001796# ifdef FEAT_RIGHTLEFT
1797 pum_rl = curwin->w_p_rl;
1798# endif
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001799
1800 pum_position_at_mouse(BALLOON_MIN_WIDTH);
1801 pum_selected = -1;
1802 pum_first = 0;
1803 pum_redraw();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001804}
1805
1806/*
1807 * Called when the mouse moved, may remove any displayed balloon.
1808 */
1809 void
1810ui_may_remove_balloon(void)
1811{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001812 // For now: remove the balloon whenever the mouse moves to another screen
1813 // cell.
1814 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001815}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001816#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001817
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001818#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001819/*
1820 * Select the pum entry at the mouse position.
1821 */
1822 static void
1823pum_select_mouse_pos(void)
1824{
1825 int idx = mouse_row - pum_row;
1826
zeertzjq761a4202024-07-04 17:26:37 +02001827 if (idx < 0 || idx >= pum_height)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001828 pum_selected = -1;
1829 else if (*pum_array[idx].pum_text != NUL)
1830 pum_selected = idx;
1831}
1832
1833/*
1834 * Execute the currently selected popup menu item.
1835 */
1836 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001837pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001838{
1839 vimmenu_T *mp;
1840 int idx = 0;
1841 exarg_T ea;
1842
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001843 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001844 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001845 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001846 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001847 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001848 break;
1849 }
1850}
1851
1852/*
1853 * Open the terminal version of the popup menu and don't return until it is
1854 * closed.
1855 */
1856 void
1857pum_show_popupmenu(vimmenu_T *menu)
1858{
1859 vimmenu_T *mp;
1860 int idx = 0;
1861 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001862# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001863 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001864# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001865 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001866
1867 pum_undisplay();
1868 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001869 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001870
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001871 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001872 if (menu_is_separator(mp->dname)
1873 || (mp->modes & mp->enabled & mode))
1874 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001875
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001876 // When there are only Terminal mode menus, using "popup Edit" results in
1877 // pum_size being zero.
1878 if (pum_size <= 0)
1879 {
zeertzjq276410e2023-05-07 21:59:33 +01001880 emsg(_(e_menu_only_exists_in_another_mode));
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001881 return;
1882 }
1883
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001884 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001885 if (array == NULL)
1886 return;
1887
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001888 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001889 {
1890 char_u *s = NULL;
1891
1892 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001893 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001894 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001895 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001896 s = mp->dname;
1897 if (s != NULL)
1898 {
1899 s = vim_strsave(s);
1900 if (s != NULL)
1901 array[idx++].pum_text = s;
1902 }
1903 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001904
1905 pum_array = array;
1906 pum_compute_size();
1907 pum_scrollbar = 0;
1908 pum_height = pum_size;
zeertzjq883018f2024-06-15 15:37:11 +02001909# ifdef FEAT_RIGHTLEFT
1910 pum_rl = curwin->w_p_rl;
1911# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001912 pum_position_at_mouse(20);
1913
1914 pum_selected = -1;
1915 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001916# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001917 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001918 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001919# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001920
1921 for (;;)
1922 {
1923 int c;
1924
1925 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001926 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001927 out_flush();
1928
1929 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001930
zeertzjq92a16782022-07-26 12:24:41 +01001931 // Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
1932 // closed the popup menu.
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001933 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001934 break;
1935 else if (c == CAR || c == NL)
1936 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001937 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001938 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001939 break;
1940 }
1941 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1942 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001943 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001944 while (pum_selected > 0)
1945 {
1946 --pum_selected;
1947 if (*array[pum_selected].pum_text != NUL)
1948 break;
1949 }
1950 }
1951 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1952 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001953 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001954 while (pum_selected < pum_size - 1)
1955 {
1956 ++pum_selected;
1957 if (*array[pum_selected].pum_text != NUL)
1958 break;
1959 }
1960 }
1961 else if (c == K_RIGHTMOUSE)
1962 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001963 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001964 vungetc(c);
1965 break;
1966 }
1967 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1968 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001969 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001970 pum_select_mouse_pos();
1971 }
1972 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1973 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001974 // left mouse click: select clicked item, if any, and close;
1975 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001976 pum_select_mouse_pos();
1977 if (pum_selected >= 0)
1978 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001979 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001980 break;
1981 }
1982 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1983 break;
1984 }
1985 }
1986
Bram Moolenaar38455a92020-12-24 18:39:02 +01001987 for (idx = 0; idx < pum_size; ++idx)
1988 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001989 vim_free(array);
1990 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001991# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001992 p_bevalterm = save_bevalterm;
1993 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001994# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001995}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001996
1997 void
1998pum_make_popup(char_u *path_name, int use_mouse_pos)
1999{
2000 vimmenu_T *menu;
2001
2002 if (!use_mouse_pos)
2003 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002004 // Hack: set mouse position at the cursor so that the menu pops up
2005 // around there.
zeertzjq4e1ca0d2023-04-27 19:36:55 +01002006 mouse_row = W_WINROW(curwin) + curwin->w_wrow;
zeertzjq883018f2024-06-15 15:37:11 +02002007 mouse_col =
2008# ifdef FEAT_RIGHTLEFT
2009 curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
2010# endif
2011 curwin->w_wincol + curwin->w_wcol;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01002012 }
2013
2014 menu = gui_find_menu(path_name);
2015 if (menu != NULL)
2016 pum_show_popupmenu(menu);
2017}
Bram Moolenaar4b779472005-10-04 09:12:31 +00002018#endif