blob: 9a9bfab1a93525a75a73a0395fc96cc5ac5565af [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;
21
Bram Moolenaar63d9e732019-12-05 21:10:38 +010022static int pum_height; // nr of displayed pum items
23static int pum_width; // width of displayed pum items
24static int pum_base_width; // width of pum items base
25static int pum_kind_width; // width of pum items kind column
26static int pum_extra_width; // width of extra stuff
27static int pum_scrollbar; // TRUE when scrollbar present
Bram Moolenaar4b779472005-10-04 09:12:31 +000028
Bram Moolenaar63d9e732019-12-05 21:10:38 +010029static int pum_row; // top row of pum
30static int pum_col; // left column of pum
Bram Moolenaar4b779472005-10-04 09:12:31 +000031
Bram Moolenaar0e6e1792018-06-17 17:10:59 +020032static win_T *pum_window = NULL;
Bram Moolenaar491ac282018-06-17 14:47:55 +020033static int pum_win_row;
34static int pum_win_height;
35static int pum_win_col;
36static int pum_win_wcol;
37static int pum_win_width;
38
Bram Moolenaar04357fb2019-12-10 21:50:56 +010039// Some parts are not updated when a popup menu is visible. Setting this flag
40// makes pum_visible() return FALSE even when there is a popup menu.
41static int pum_pretend_not_visible = FALSE;
42
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010043static int pum_set_selected(int n, int repeat);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000044
Bram Moolenaar4b779472005-10-04 09:12:31 +000045#define PUM_DEF_HEIGHT 10
Bram Moolenaar4b779472005-10-04 09:12:31 +000046
Bram Moolenaar51b0f372017-11-18 18:52:04 +010047 static void
48pum_compute_size(void)
49{
50 int i;
51 int w;
52
Bram Moolenaar63d9e732019-12-05 21:10:38 +010053 // Compute the width of the widest match and the widest extra.
Bram Moolenaar51b0f372017-11-18 18:52:04 +010054 pum_base_width = 0;
55 pum_kind_width = 0;
56 pum_extra_width = 0;
57 for (i = 0; i < pum_size; ++i)
58 {
Bram Moolenaard58a6622020-05-02 14:52:57 +020059 if (pum_array[i].pum_text != NULL)
60 {
61 w = vim_strsize(pum_array[i].pum_text);
62 if (pum_base_width < w)
63 pum_base_width = w;
64 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +010065 if (pum_array[i].pum_kind != NULL)
66 {
67 w = vim_strsize(pum_array[i].pum_kind) + 1;
68 if (pum_kind_width < w)
69 pum_kind_width = w;
70 }
71 if (pum_array[i].pum_extra != NULL)
72 {
73 w = vim_strsize(pum_array[i].pum_extra) + 1;
74 if (pum_extra_width < w)
75 pum_extra_width = w;
76 }
77 }
78}
79
Bram Moolenaar4b779472005-10-04 09:12:31 +000080/*
81 * Show the popup menu with items "array[size]".
82 * "array" must remain valid until pum_undisplay() is called!
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010083 * When possible the leftmost character is aligned with cursor column.
Bram Moolenaar4b779472005-10-04 09:12:31 +000084 * The menu appears above the screen line "row" or at "row" + "height" - 1.
85 */
86 void
Bram Moolenaar05540972016-01-30 20:31:25 +010087pum_display(
88 pumitem_T *array,
89 int size,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010090 int selected) // index of initially selected item, none if
91 // out of range
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000092{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +000093 int def_width;
94 int max_width;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000095 int context_lines;
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +010096 int cursor_col;
Bram Moolenaar91e44a32016-11-04 20:08:52 +010097 int above_row;
98 int below_row;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000099 int redo_count = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaar91e44a32016-11-04 20:08:52 +0100101 win_T *pvwin;
Bram Moolenaaraab33832016-11-04 22:08:29 +0100102#endif
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100103#ifdef FEAT_RIGHTLEFT
104 int right_left = State == CMDLINE ? FALSE : curwin->w_p_rl;
105#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000106
Bram Moolenaara5e66212017-09-29 22:42:33 +0200107 do
108 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100109 def_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200110 above_row = 0;
111 below_row = cmdline_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000112
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100113 // Pretend the pum is already there to avoid that must_redraw is set
114 // when 'cuc' is on.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200115 pum_array = (pumitem_T *)1;
116 validate_cursor_col();
117 pum_array = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000118
Bram Moolenaar491ac282018-06-17 14:47:55 +0200119 // Remember the essential parts of the window position and size, so we
120 // can decide when to reposition the popup menu.
Bram Moolenaar0e6e1792018-06-17 17:10:59 +0200121 pum_window = curwin;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000122 if (State == CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000123 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000124 pum_win_row = cmdline_row;
125 else
126 pum_win_row = curwin->w_wrow + W_WINROW(curwin);
Bram Moolenaar491ac282018-06-17 14:47:55 +0200127 pum_win_height = curwin->w_height;
128 pum_win_col = curwin->w_wincol;
129 pum_win_wcol = curwin->w_wcol;
130 pum_win_width = curwin->w_width;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000131
Bram Moolenaar4033c552017-09-16 20:54:51 +0200132#if defined(FEAT_QUICKFIX)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200133 FOR_ALL_WINDOWS(pvwin)
134 if (pvwin->w_p_pvw)
135 break;
136 if (pvwin != NULL)
137 {
138 if (W_WINROW(pvwin) < W_WINROW(curwin))
139 above_row = W_WINROW(pvwin) + pvwin->w_height;
140 else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
141 below_row = W_WINROW(pvwin);
142 }
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100143#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000144
Bram Moolenaara5e66212017-09-29 22:42:33 +0200145 /*
146 * Figure out the size and position of the pum.
147 */
148 if (size < PUM_DEF_HEIGHT)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000149 pum_height = size;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000150 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200151 pum_height = PUM_DEF_HEIGHT;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000152 if (p_ph > 0 && pum_height > p_ph)
153 pum_height = p_ph;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000154
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100155 // Put the pum below "pum_win_row" if possible. If there are few lines
156 // decide on where there is more room.
Bram Moolenaar491ac282018-06-17 14:47:55 +0200157 if (pum_win_row + 2 >= below_row - pum_height
158 && pum_win_row - above_row > (below_row - above_row) / 2)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200159 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100160 // pum above "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200161
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100162 if (State == CMDLINE)
163 // for cmdline pum, no need for context lines
164 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200165 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100166 {
167 // Leave two lines of context if possible
168 if (curwin->w_wrow - curwin->w_cline_row >= 2)
169 context_lines = 2;
170 else
171 context_lines = curwin->w_wrow - curwin->w_cline_row;
172 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200173
Bram Moolenaar491ac282018-06-17 14:47:55 +0200174 if (pum_win_row >= size + context_lines)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200175 {
Bram Moolenaar491ac282018-06-17 14:47:55 +0200176 pum_row = pum_win_row - size - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200177 pum_height = size;
178 }
179 else
180 {
181 pum_row = 0;
Bram Moolenaar491ac282018-06-17 14:47:55 +0200182 pum_height = pum_win_row - context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200183 }
184 if (p_ph > 0 && pum_height > p_ph)
185 {
186 pum_row += pum_height - p_ph;
187 pum_height = p_ph;
188 }
189 }
190 else
191 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100192 // pum below "pum_win_row"
Bram Moolenaara5e66212017-09-29 22:42:33 +0200193
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100194 if (State == CMDLINE)
195 // for cmdline pum, no need for context lines
196 context_lines = 0;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200197 else
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100198 {
199 // Leave two lines of context if possible
200 validate_cheight();
201 if (curwin->w_cline_row
202 + curwin->w_cline_height - curwin->w_wrow >= 3)
203 context_lines = 3;
204 else
205 context_lines = curwin->w_cline_row
206 + curwin->w_cline_height - curwin->w_wrow;
207 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200208
Bram Moolenaar491ac282018-06-17 14:47:55 +0200209 pum_row = pum_win_row + context_lines;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200210 if (size > below_row - pum_row)
211 pum_height = below_row - pum_row;
212 else
213 pum_height = size;
214 if (p_ph > 0 && pum_height > p_ph)
215 pum_height = p_ph;
216 }
217
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100218 // don't display when we only have room for one line
Bram Moolenaara5e66212017-09-29 22:42:33 +0200219 if (pum_height < 1 || (pum_height == 1 && size > 1))
220 return;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000221
Bram Moolenaar4033c552017-09-16 20:54:51 +0200222#if defined(FEAT_QUICKFIX)
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100223 // If there is a preview window above avoid drawing over it.
224 if (pvwin != NULL && pum_row < above_row && pum_height > above_row)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000225 {
Bram Moolenaar614ab8a2018-12-01 11:59:00 +0100226 pum_row = above_row;
227 pum_height = pum_win_row - above_row;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000228 }
Bram Moolenaara5e66212017-09-29 22:42:33 +0200229#endif
230
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100231 pum_array = array;
232 pum_size = size;
233 pum_compute_size();
234 max_width = pum_base_width;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000235
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100236 // Calculate column
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000237#ifdef FEAT_WILDMENU
238 if (State == CMDLINE)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000239 // cmdline completion popup menu
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000240 cursor_col = cmdline_compl_startcol();
241 else
242#endif
Bram Moolenaarabc97732007-08-08 20:49:37 +0000243#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100244 if (right_left)
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100245 cursor_col = curwin->w_wincol + curwin->w_width
246 - curwin->w_wcol - 1;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000247 else
248#endif
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100249 cursor_col = curwin->w_wincol + curwin->w_wcol;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000250
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100251 // if there are more items than room we need a scrollbar
Bram Moolenaara5e66212017-09-29 22:42:33 +0200252 if (pum_height < size)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000253 {
Bram Moolenaara5e66212017-09-29 22:42:33 +0200254 pum_scrollbar = 1;
255 ++max_width;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000256 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000257 else
Bram Moolenaara5e66212017-09-29 22:42:33 +0200258 pum_scrollbar = 0;
259
260 if (def_width < max_width)
261 def_width = max_width;
262
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100263 if (((cursor_col < Columns - p_pw || cursor_col < Columns - max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000264#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100265 && !right_left)
266 || (right_left && (cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000267#endif
Bram Moolenaara5e66212017-09-29 22:42:33 +0200268 ))
269 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100270 // align pum with "cursor_col"
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100271 pum_col = cursor_col;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000272
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100273 // start with the maximum space available
Bram Moolenaara5e66212017-09-29 22:42:33 +0200274#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100275 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200276 pum_width = pum_col - pum_scrollbar + 1;
277 else
278#endif
279 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000280
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100281 if (pum_width > max_width + pum_kind_width + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100282 && pum_width > p_pw)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200283 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100284 // the width is more than needed for the items, make it
285 // narrower
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100286 pum_width = max_width + pum_kind_width + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100287 if (pum_width < p_pw)
288 pum_width = p_pw;
Bram Moolenaara5e66212017-09-29 22:42:33 +0200289 }
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100290 else if (((cursor_col > p_pw || cursor_col > max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100291#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100292 && !right_left)
293 || (right_left && (cursor_col < Columns - p_pw
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100294 || cursor_col < Columns - max_width)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100295#endif
296 ))
297 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100298 // align pum edge with "cursor_col"
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100299#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100300 if (right_left
Bram Moolenaarbb008dd2018-02-24 18:59:55 +0100301 && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100302 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100303 pum_col = cursor_col + max_width + pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100304 if (pum_col >= Columns)
305 pum_col = Columns - 1;
306 }
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100307 else if (!right_left)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100308#endif
309 {
Bram Moolenaar2b10bcb2018-02-24 21:25:44 +0100310 if (curwin->w_wincol > Columns - max_width - pum_scrollbar
311 && max_width <= p_pw)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100312 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100313 // use full width to end of the screen
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100314 pum_col = Columns - max_width - pum_scrollbar;
315 if (pum_col < 0)
316 pum_col = 0;
317 }
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100318 }
319
320#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100321 if (right_left)
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100322 pum_width = pum_col - pum_scrollbar + 1;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100323 else
324#endif
Bram Moolenaar4287ed32018-02-17 20:35:29 +0100325 pum_width = Columns - pum_col - pum_scrollbar;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100326
Bram Moolenaar42443c72018-02-10 18:28:52 +0100327 if (pum_width < p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100328 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100329 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100330#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100331 if (right_left)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100332 {
333 if (pum_width > pum_col)
334 pum_width = pum_col;
335 }
336 else
337#endif
338 {
339 if (pum_width >= Columns - pum_col)
340 pum_width = Columns - pum_col - 1;
341 }
342 }
343 else if (pum_width > max_width + pum_kind_width
344 + pum_extra_width + 1
Bram Moolenaar42443c72018-02-10 18:28:52 +0100345 && pum_width > p_pw)
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100346 {
347 pum_width = max_width + pum_kind_width
348 + pum_extra_width + 1;
Bram Moolenaar42443c72018-02-10 18:28:52 +0100349 if (pum_width < p_pw)
350 pum_width = p_pw;
Bram Moolenaara8f04aa2018-02-10 15:36:55 +0100351 }
352 }
353
Bram Moolenaara5e66212017-09-29 22:42:33 +0200354 }
355 else if (Columns < def_width)
356 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100357 // not enough room, will use what we have
Bram Moolenaara5e66212017-09-29 22:42:33 +0200358#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100359 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200360 pum_col = Columns - 1;
361 else
362#endif
363 pum_col = 0;
364 pum_width = Columns - 1;
365 }
366 else
367 {
Bram Moolenaar42443c72018-02-10 18:28:52 +0100368 if (max_width > p_pw)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100369 max_width = p_pw; // truncate
Bram Moolenaara5e66212017-09-29 22:42:33 +0200370#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100371 if (right_left)
Bram Moolenaara5e66212017-09-29 22:42:33 +0200372 pum_col = max_width - 1;
373 else
374#endif
375 pum_col = Columns - max_width;
376 pum_width = max_width - pum_scrollbar;
377 }
378
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100379 // Set selected item and redraw. If the window size changed need to
380 // redo the positioning. Limit this to two times, when there is not
381 // much room the window size will keep changing.
Bram Moolenaara5e66212017-09-29 22:42:33 +0200382 } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100383
384 pum_redraw();
Bram Moolenaar4b779472005-10-04 09:12:31 +0000385}
386
387/*
Bram Moolenaarae654382019-01-17 21:09:05 +0100388 * Set a flag that when pum_redraw() is called it first calls update_screen().
389 * This will avoid clearing and redrawing the popup menu, prevent flicker.
390 */
391 void
392pum_call_update_screen()
393{
394 call_update_screen = TRUE;
395
396 // Update the cursor position to be able to compute the popup menu
397 // position. The cursor line length may have changed because of the
398 // inserted completion.
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +0100399 curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
Bram Moolenaarae654382019-01-17 21:09:05 +0100400 validate_cursor();
401}
402
403/*
404 * Return TRUE if we are going to redraw the popup menu and the screen position
405 * "row"/"col" is under the popup menu.
406 */
407 int
Bakudankun65555002021-11-17 20:40:16 +0000408pum_under_menu(int row, int col, int only_redrawing)
Bram Moolenaarae654382019-01-17 21:09:05 +0100409{
Bakudankun65555002021-11-17 20:40:16 +0000410 return (!only_redrawing || pum_will_redraw)
Bram Moolenaarae654382019-01-17 21:09:05 +0100411 && row >= pum_row
412 && row < pum_row + pum_height
413 && col >= pum_col - 1
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200414 && col < pum_col + pum_width + pum_scrollbar;
Bram Moolenaarae654382019-01-17 21:09:05 +0100415}
416
417/*
Bram Moolenaar4b779472005-10-04 09:12:31 +0000418 * Redraw the popup menu, using "pum_first" and "pum_selected".
419 */
420 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100421pum_redraw(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000422{
423 int row = pum_row;
424 int col;
425 int attr_norm = highlight_attr[HLF_PNI];
426 int attr_select = highlight_attr[HLF_PSI];
427 int attr_scroll = highlight_attr[HLF_PSB];
428 int attr_thumb = highlight_attr[HLF_PST];
429 int attr;
430 int i;
431 int idx;
432 char_u *s;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000433 char_u *p = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000434 int totwidth, width, w;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000435 int thumb_pos = 0;
Bram Moolenaarbdace832019-03-02 10:13:42 +0100436 int thumb_height = 1;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000437 int round;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000438 int n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000439
Bram Moolenaarae654382019-01-17 21:09:05 +0100440 if (call_update_screen)
441 {
442 call_update_screen = FALSE;
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100443 // Do not redraw in pum_may_redraw() and don't draw in the area where
444 // the popup menu will be.
445 pum_will_redraw = TRUE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100446 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100447 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +0100448 }
449
450 // never display more than we have
Bram Moolenaar4c1e6262013-11-06 04:04:33 +0100451 if (pum_first > pum_size - pum_height)
452 pum_first = pum_size - pum_height;
453
Bram Moolenaar4b779472005-10-04 09:12:31 +0000454 if (pum_scrollbar)
455 {
Bram Moolenaarbdace832019-03-02 10:13:42 +0100456 thumb_height = pum_height * pum_height / pum_size;
457 if (thumb_height == 0)
458 thumb_height = 1;
459 thumb_pos = (pum_first * (pum_height - thumb_height)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000460 + (pum_size - pum_height) / 2)
461 / (pum_size - pum_height);
462 }
463
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100464#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200465 // The popup menu is drawn over popup menus with zindex under
466 // POPUPMENU_ZINDEX.
467 screen_zindex = POPUPMENU_ZINDEX;
468#endif
469
Bram Moolenaar4b779472005-10-04 09:12:31 +0000470 for (i = 0; i < pum_height; ++i)
471 {
472 idx = i + pum_first;
473 attr = (idx == pum_selected) ? attr_select : attr_norm;
474
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100475 // prepend a space if there is room
Bram Moolenaarabc97732007-08-08 20:49:37 +0000476#ifdef FEAT_RIGHTLEFT
477 if (curwin->w_p_rl)
478 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200479 if (pum_col < curwin->w_wincol + curwin->w_width - 1)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000480 screen_putchar(' ', row, pum_col + 1, attr);
481 }
482 else
483#endif
484 if (pum_col > 0)
485 screen_putchar(' ', row, pum_col - 1, attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000486
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100487 // Display each entry, use two spaces for a Tab.
488 // Do this 3 times: For the main text, kind and extra info
Bram Moolenaar4b779472005-10-04 09:12:31 +0000489 col = pum_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000490 totwidth = 0;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000491 for (round = 1; round <= 3; ++round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000492 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000493 width = 0;
494 s = NULL;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000495 switch (round)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000496 {
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000497 case 1: p = pum_array[idx].pum_text; break;
498 case 2: p = pum_array[idx].pum_kind; break;
499 case 3: p = pum_array[idx].pum_extra; break;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000500 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000501 if (p != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100502 for ( ; ; MB_PTR_ADV(p))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000503 {
504 if (s == NULL)
505 s = p;
506 w = ptr2cells(p);
507 if (*p == NUL || *p == TAB || totwidth + w > pum_width)
508 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100509 // Display the text that fits or comes before a Tab.
510 // First convert it to printable characters.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000511 char_u *st;
512 int saved = *p;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000513
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100514 if (saved != NUL)
515 *p = NUL;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000516 st = transstr(s);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +0100517 if (saved != NUL)
518 *p = saved;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000519#ifdef FEAT_RIGHTLEFT
520 if (curwin->w_p_rl)
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000521 {
Bram Moolenaarabc97732007-08-08 20:49:37 +0000522 if (st != NULL)
523 {
524 char_u *rt = reverse_text(st);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000525
526 if (rt != NULL)
527 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100528 char_u *rt_start = rt;
529 int size;
530
531 size = vim_strsize(rt);
532 if (size > pum_width)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000533 {
Bram Moolenaard836bb92010-01-19 18:06:03 +0100534 do
535 {
536 size -= has_mbyte
537 ? (*mb_ptr2cells)(rt) : 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100538 MB_PTR_ADV(rt);
Bram Moolenaard836bb92010-01-19 18:06:03 +0100539 } while (size > pum_width);
540
541 if (size < pum_width)
542 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100543 // Most left character requires
544 // 2-cells but only 1 cell is
545 // available on screen. Put a
546 // '<' on the left of the pum
547 // item
Bram Moolenaard836bb92010-01-19 18:06:03 +0100548 *(--rt) = '<';
549 size++;
550 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000551 }
Bram Moolenaard836bb92010-01-19 18:06:03 +0100552 screen_puts_len(rt, (int)STRLEN(rt),
553 row, col - size + 1, attr);
554 vim_free(rt_start);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000555 }
556 vim_free(st);
557 }
558 col -= width;
Bram Moolenaar7cc36e92007-03-27 10:42:05 +0000559 }
Bram Moolenaarabc97732007-08-08 20:49:37 +0000560 else
561#endif
562 {
563 if (st != NULL)
564 {
Bram Moolenaar714cbe52020-11-16 19:12:00 +0100565 int size = (int)STRLEN(st);
566 int cells = (*mb_string2cells)(st, size);
567
568 // only draw the text that fits
569 while (size > 0
570 && col + cells > pum_width + pum_col)
571 {
572 --size;
573 if (has_mbyte)
574 {
575 size -= (*mb_head_off)(st, st + size);
576 cells -= (*mb_ptr2cells)(st + size);
577 }
578 else
579 --cells;
580 }
581 screen_puts_len(st, size, row, col, attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000582 vim_free(st);
583 }
584 col += width;
585 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000586
587 if (*p != TAB)
588 break;
589
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100590 // Display two spaces for a Tab.
Bram Moolenaarabc97732007-08-08 20:49:37 +0000591#ifdef FEAT_RIGHTLEFT
592 if (curwin->w_p_rl)
593 {
594 screen_puts_len((char_u *)" ", 2, row, col - 1,
595 attr);
596 col -= 2;
597 }
598 else
599#endif
600 {
601 screen_puts_len((char_u *)" ", 2, row, col, attr);
602 col += 2;
603 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000604 totwidth += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100605 s = NULL; // start text at next char
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000606 width = 0;
607 }
608 else
609 width += w;
610 }
611
612 if (round > 1)
613 n = pum_kind_width + 1;
614 else
615 n = 1;
616
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100617 // Stop when there is nothing more to display.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000618 if (round == 3
619 || (round == 2 && pum_array[idx].pum_extra == NULL)
620 || (round == 1 && pum_array[idx].pum_kind == NULL
621 && pum_array[idx].pum_extra == NULL)
622 || pum_base_width + n >= pum_width)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000623 break;
Bram Moolenaarabc97732007-08-08 20:49:37 +0000624#ifdef FEAT_RIGHTLEFT
625 if (curwin->w_p_rl)
626 {
627 screen_fill(row, row + 1, pum_col - pum_base_width - n + 1,
628 col + 1, ' ', ' ', attr);
629 col = pum_col - pum_base_width - n + 1;
630 }
631 else
632#endif
633 {
634 screen_fill(row, row + 1, col, pum_col + pum_base_width + n,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000635 ' ', ' ', attr);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000636 col = pum_col + pum_base_width + n;
637 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000638 totwidth = pum_base_width + n;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000639 }
640
Bram Moolenaarabc97732007-08-08 20:49:37 +0000641#ifdef FEAT_RIGHTLEFT
642 if (curwin->w_p_rl)
643 screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
644 ' ', attr);
645 else
646#endif
647 screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
648 attr);
Bram Moolenaar4b779472005-10-04 09:12:31 +0000649 if (pum_scrollbar > 0)
Bram Moolenaarabc97732007-08-08 20:49:37 +0000650 {
651#ifdef FEAT_RIGHTLEFT
652 if (curwin->w_p_rl)
653 screen_putchar(' ', row, pum_col - pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100654 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaar4b779472005-10-04 09:12:31 +0000655 ? attr_thumb : attr_scroll);
Bram Moolenaarabc97732007-08-08 20:49:37 +0000656 else
657#endif
658 screen_putchar(' ', row, pum_col + pum_width,
Bram Moolenaarbdace832019-03-02 10:13:42 +0100659 i >= thumb_pos && i < thumb_pos + thumb_height
Bram Moolenaarabc97732007-08-08 20:49:37 +0000660 ? attr_thumb : attr_scroll);
661 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000662
663 ++row;
664 }
Bram Moolenaar33796b32019-06-08 16:01:13 +0200665
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100666#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +0200667 screen_zindex = 0;
668#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +0000669}
670
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100671#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200672/*
673 * Position the info popup relative to the popup menu item.
674 */
675 void
676pum_position_info_popup(win_T *wp)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200677{
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100678 int col = pum_col + pum_width + pum_scrollbar + 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200679 int row = pum_row;
680 int botpos = POPPOS_BOTLEFT;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200681 int used_maxwidth_opt = FALSE;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200682
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200683 wp->w_popup_pos = POPPOS_TOPLEFT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200684 if (Columns - col < 20 && Columns - col < pum_col)
685 {
686 col = pum_col - 1;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200687 wp->w_popup_pos = POPPOS_TOPRIGHT;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200688 botpos = POPPOS_BOTRIGHT;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200689 wp->w_maxwidth = pum_col - 1;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200690 }
691 else
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200692 wp->w_maxwidth = Columns - col + 1;
693 wp->w_maxwidth -= popup_extra_width(wp);
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200694 if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
695 {
696 // option value overrules computed value
697 wp->w_maxwidth = wp->w_maxwidth_opt;
698 used_maxwidth_opt = TRUE;
699 }
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200700
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200701 row -= popup_top_extra(wp);
702 if (wp->w_popup_flags & POPF_INFO_MENU)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200703 {
704 if (pum_row < pum_win_row)
705 {
706 // menu above cursor line, align with bottom
707 row += pum_height;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200708 wp->w_popup_pos = botpos;
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200709 }
710 else
711 // menu below cursor line, align with top
712 row += 1;
713 }
714 else
715 // align with the selected item
716 row += pum_selected - pum_first + 1;
717
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100718 wp->w_popup_flags &= ~POPF_HIDDEN;
Bram Moolenaarde2396f2020-07-18 21:40:41 +0200719 if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +0100720 // The popup is not going to fit or will overlap with the cursor
721 // position, hide the popup.
722 wp->w_popup_flags |= POPF_HIDDEN;
723 else
724 popup_set_wantpos_rowcol(wp, row, col);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200725}
726#endif
727
Bram Moolenaar4b779472005-10-04 09:12:31 +0000728/*
729 * Set the index of the currently selected item. The menu will scroll when
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000730 * necessary. When "n" is out of range don't scroll.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000731 * This may be repeated when the preview window is used:
732 * "repeat" == 0: open preview window normally
733 * "repeat" == 1: open preview window but don't set the size
734 * "repeat" == 2: don't open preview window
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000735 * Returns TRUE when the window was resized and the location of the popup menu
736 * must be recomputed.
Bram Moolenaar4b779472005-10-04 09:12:31 +0000737 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000738 static int
Bram Moolenaar9cb698d2019-08-21 15:30:45 +0200739pum_set_selected(int n, int repeat UNUSED)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000740{
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000741 int resized = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000742 int context = pum_height / 2;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200743#ifdef FEAT_QUICKFIX
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200744 int prev_selected = pum_selected;
Bram Moolenaareaf35242019-08-20 23:14:15 +0200745#endif
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100746#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200747 int has_info = FALSE;
748#endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000749
Bram Moolenaar4b779472005-10-04 09:12:31 +0000750 pum_selected = n;
751
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000752 if (pum_selected >= 0 && pum_selected < pum_size)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000753 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000754 if (pum_first > pum_selected - 4)
755 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100756 // scroll down; when we did a jump it's probably a PageUp then
757 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000758 if (pum_first > pum_selected - 2)
759 {
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000760 pum_first -= pum_height - 2;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000761 if (pum_first < 0)
762 pum_first = 0;
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000763 else if (pum_first > pum_selected)
764 pum_first = pum_selected;
Bram Moolenaare3226be2005-12-18 22:10:00 +0000765 }
766 else
767 pum_first = pum_selected;
768 }
769 else if (pum_first < pum_selected - pum_height + 5)
770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100771 // scroll up; when we did a jump it's probably a PageDown then
772 // scroll a whole page
Bram Moolenaare3226be2005-12-18 22:10:00 +0000773 if (pum_first < pum_selected - pum_height + 1 + 2)
Bram Moolenaar7df351e2006-01-23 22:30:28 +0000774 {
775 pum_first += pum_height - 2;
776 if (pum_first < pum_selected - pum_height + 1)
777 pum_first = pum_selected - pum_height + 1;
778 }
Bram Moolenaare3226be2005-12-18 22:10:00 +0000779 else
780 pum_first = pum_selected - pum_height + 1;
781 }
Bram Moolenaar4b779472005-10-04 09:12:31 +0000782
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100783 // Give a few lines of context when possible.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000784 if (context > 3)
785 context = 3;
786 if (pum_height > 2)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000787 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000788 if (pum_first > pum_selected - context)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000789 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100790 // scroll down
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000791 pum_first = pum_selected - context;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000792 if (pum_first < 0)
793 pum_first = 0;
794 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000795 else if (pum_first < pum_selected + context - pum_height + 1)
Bram Moolenaar4b779472005-10-04 09:12:31 +0000796 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100797 // scroll up
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000798 pum_first = pum_selected + context - pum_height + 1;
Bram Moolenaar4b779472005-10-04 09:12:31 +0000799 }
800 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200801 // adjust for the number of lines displayed
802 if (pum_first > pum_size - pum_height)
803 pum_first = pum_size - pum_height;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000804
Bram Moolenaar4033c552017-09-16 20:54:51 +0200805#if defined(FEAT_QUICKFIX)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000806 /*
807 * Show extra info in the preview window if there is something and
Bram Moolenaar202c3f72019-11-21 12:12:35 +0100808 * 'completeopt' contains "preview" or "popup" or "popuphidden".
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000809 * Skip this when tried twice already.
810 * Skip this also when there is not much room.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000811 * NOTE: Be very careful not to sync undo!
812 */
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000813 if (pum_array[pum_selected].pum_info != NULL
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000814 && Rows > 10
815 && repeat <= 1
816 && vim_strchr(p_cot, 'p') != NULL)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000817 {
818 win_T *curwin_save = curwin;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200819 tabpage_T *curtab_save = curtab;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000820 int res = OK;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100821# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200822 use_popup_T use_popup;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200823# else
Bram Moolenaara2c2ae42019-11-30 20:58:46 +0100824# define use_popup USEPOPUP_NONE
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200825# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100826# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200827 has_info = TRUE;
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200828 if (strstr((char *)p_cot, "popuphidden") != NULL)
829 use_popup = USEPOPUP_HIDDEN;
830 else if (strstr((char *)p_cot, "popup") != NULL)
831 use_popup = USEPOPUP_NORMAL;
832 else
833 use_popup = USEPOPUP_NONE;
Bram Moolenaar2dfae042020-11-15 14:09:37 +0100834 if (use_popup != USEPOPUP_NONE)
835 // don't use WinEnter or WinLeave autocommands for the info
836 // popup
837 block_autocmds();
Bram Moolenaard570ab92019-09-03 23:20:05 +0200838# endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200839 // Open a preview window and set "curwin" to it.
840 // 3 lines by default, prefer 'previewheight' if set and smaller.
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000841 g_do_tagpreview = 3;
Bram Moolenaaree236d02010-10-27 17:11:15 +0200842 if (p_pvh > 0 && p_pvh < g_do_tagpreview)
843 g_do_tagpreview = p_pvh;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200844 ++RedrawingDisabled;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200845 // Prevent undo sync here, if an autocommand syncs undo weird
846 // things can happen to the undo tree.
Bram Moolenaare7d13762015-10-30 14:23:33 +0100847 ++no_u_sync;
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200848 resized = prepare_tagpreview(FALSE, FALSE, use_popup);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100849 --no_u_sync;
Bram Moolenaarc8045152014-07-09 19:58:24 +0200850 --RedrawingDisabled;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000851 g_do_tagpreview = 0;
852
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200853 if (curwin->w_p_pvw
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100854# ifdef FEAT_PROP_POPUP
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200855 || (curwin->w_popup_flags & POPF_INFO)
856# endif
857 )
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000858 {
Bram Moolenaar50e53762016-10-27 14:49:15 +0200859 if (!resized
860 && curbuf->b_nwindows == 1
861 && curbuf->b_fname == NULL
Bram Moolenaar26910de2019-06-15 19:37:15 +0200862 && bt_nofile(curbuf)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000863 && curbuf->b_p_bh[0] == 'w')
864 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200865 // Already a "wipeout" buffer, make it empty.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100866 while (!BUFEMPTY())
Bram Moolenaarca70c072020-05-30 20:30:46 +0200867 ml_delete((linenr_T)1);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000868 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000869 else
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000870 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200871 // Don't want to sync undo in the current buffer.
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000872 ++no_u_sync;
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000873 res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000874 --no_u_sync;
875 if (res == OK)
876 {
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200877 // Edit a new, empty buffer. Set options for a "wipeout"
878 // buffer.
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100879 set_option_value_give_err((char_u *)"swf",
880 0L, NULL, OPT_LOCAL);
881 set_option_value_give_err((char_u *)"bl",
882 0L, NULL, OPT_LOCAL);
883 set_option_value_give_err((char_u *)"bt",
884 0L, (char_u *)"nofile", OPT_LOCAL);
885 set_option_value_give_err((char_u *)"bh",
886 0L, (char_u *)"wipe", OPT_LOCAL);
887 set_option_value_give_err((char_u *)"diff",
888 0L, NULL, OPT_LOCAL);
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000889 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000890 }
891 if (res == OK)
892 {
893 char_u *p, *e;
894 linenr_T lnum = 0;
895
896 for (p = pum_array[pum_selected].pum_info; *p != NUL; )
897 {
898 e = vim_strchr(p, '\n');
899 if (e == NULL)
900 {
901 ml_append(lnum++, p, 0, FALSE);
902 break;
903 }
904 else
905 {
906 *e = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000907 ml_append(lnum++, p, (int)(e - p + 1), FALSE);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000908 *e = '\n';
909 p = e + 1;
910 }
911 }
Bram Moolenaar576a4a62019-08-18 15:25:17 +0200912 // delete the empty last line
Bram Moolenaarca70c072020-05-30 20:30:46 +0200913 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000914
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100915 // Increase the height of the preview window to show the
916 // text, but no more than 'previewheight' lines.
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200917 if (repeat == 0 && use_popup == USEPOPUP_NONE)
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000918 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000919 if (lnum > p_pvh)
920 lnum = p_pvh;
921 if (curwin->w_height < lnum)
922 {
923 win_setheight((int)lnum);
924 resized = TRUE;
925 }
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000926 }
927
928 curbuf->b_changed = 0;
929 curbuf->b_p_ma = FALSE;
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200930 if (pum_selected != prev_selected)
931 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100932# ifdef FEAT_PROP_POPUP
Bram Moolenaarf0bc15c2019-08-18 19:23:45 +0200933 curwin->w_firstline = 1;
934# endif
935 curwin->w_topline = 1;
936 }
937 else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
938 curwin->w_topline = curbuf->b_ml.ml_line_count;
939 curwin->w_cursor.lnum = curwin->w_topline;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000940 curwin->w_cursor.col = 0;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100941# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200942 if (use_popup != USEPOPUP_NONE)
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200943 {
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200944 pum_position_info_popup(curwin);
Bram Moolenaarc1f87c92019-08-21 19:33:16 +0200945 if (win_valid(curwin_save))
946 redraw_win_later(curwin_save, SOME_VALID);
947 }
948# endif
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200949 if ((curwin != curwin_save && win_valid(curwin_save))
950 || (curtab != curtab_save
951 && valid_tabpage(curtab_save)))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000952 {
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200953 int save_redr_status;
954
Bram Moolenaar9ad89c62017-10-26 22:04:04 +0200955 if (curtab != curtab_save && valid_tabpage(curtab_save))
956 goto_tabpage_tp(curtab_save, FALSE, FALSE);
957
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100958 // When the first completion is done and the preview
959 // window is not resized, skip the preview window's
960 // status line redrawing.
Bram Moolenaar2bace3e2014-07-23 21:10:43 +0200961 if (ins_compl_active() && !resized)
962 curwin->w_redr_status = FALSE;
963
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100964 // Return cursor to where we were
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000965 validate_cursor();
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000966 redraw_later(SOME_VALID);
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000967
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100968 // When the preview window was resized we need to
969 // update the view on the buffer. Only go back to
970 // the window when needed, otherwise it will always be
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100971 // redrawn.
Bram Moolenaar5dd143e2019-08-15 21:34:34 +0200972 if (resized && win_valid(curwin_save))
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000973 {
Bram Moolenaare7d13762015-10-30 14:23:33 +0100974 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000975 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +0100976 --no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000977 update_topline();
978 }
979
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100980 // Update the screen before drawing the popup menu.
981 // Enable updating the status lines.
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100982 pum_pretend_not_visible = TRUE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200983
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100984 // But don't draw text at the new popup menu position,
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100985 // it causes flicker. When resizing we need to draw
986 // anyway, the position may change later.
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200987 // Also do not redraw the status line of the original
988 // current window here, to avoid it gets drawn with
989 // StatusLineNC for a moment and cause flicker.
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +0100990 pum_will_redraw = !resized;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200991 save_redr_status = curwin_save->w_redr_status;
992 curwin_save->w_redr_status = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000993 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +0100994 pum_pretend_not_visible = FALSE;
995 pum_will_redraw = FALSE;
Bram Moolenaarb80d2fb2021-04-27 20:06:57 +0200996 curwin_save->w_redr_status = save_redr_status;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +0000997
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000998 if (!resized && win_valid(curwin_save))
Bram Moolenaare7d13762015-10-30 14:23:33 +0100999 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001000# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001001 win_T *wp = curwin;
1002# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001003 ++no_u_sync;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001004 win_enter(curwin_save, TRUE);
Bram Moolenaare7d13762015-10-30 14:23:33 +01001005 --no_u_sync;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001006# ifdef FEAT_PROP_POPUP
Bram Moolenaardca7abe2019-10-20 18:17:57 +02001007 if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
1008 popup_hide(wp);
1009# endif
Bram Moolenaare7d13762015-10-30 14:23:33 +01001010 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001011
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001012 // May need to update the screen again when there are
1013 // autocommands involved.
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001014 pum_pretend_not_visible = TRUE;
Bram Moolenaar5e5a98d2019-12-15 14:55:33 +01001015 pum_will_redraw = !resized;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001016 update_screen(0);
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001017 pum_pretend_not_visible = FALSE;
1018 pum_will_redraw = FALSE;
Bram Moolenaarae654382019-01-17 21:09:05 +01001019 call_update_screen = FALSE;
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001020 }
1021 }
1022 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001023# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar5dd143e2019-08-15 21:34:34 +02001024 if (WIN_IS_POPUP(curwin))
1025 // can't keep focus in a popup window
1026 win_enter(firstwin, TRUE);
1027# endif
Bram Moolenaar2dfae042020-11-15 14:09:37 +01001028# ifdef FEAT_PROP_POPUP
1029 if (use_popup != USEPOPUP_NONE)
1030 unblock_autocmds();
1031# endif
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001032 }
1033#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001034 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001035#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001036 if (!has_info)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001037 // hide any popup info window
1038 popup_hide_info();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001039#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001040
Bram Moolenaar96d2c5b2006-03-11 21:27:59 +00001041 return resized;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001042}
1043
1044/*
1045 * Undisplay the popup menu (later).
1046 */
1047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001048pum_undisplay(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001049{
1050 pum_array = NULL;
Bram Moolenaar0b565e52018-05-14 23:08:32 +02001051 redraw_all_later(NOT_VALID);
Bram Moolenaar2d694602006-08-22 19:48:48 +00001052 redraw_tabline = TRUE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001053 status_redraw_all();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001054#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02001055 // hide any popup info window
1056 popup_hide_info();
Bram Moolenaar576a4a62019-08-18 15:25:17 +02001057#endif
Bram Moolenaar4b779472005-10-04 09:12:31 +00001058}
1059
1060/*
1061 * Clear the popup menu. Currently only resets the offset to the first
1062 * displayed item.
1063 */
1064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001065pum_clear(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001066{
1067 pum_first = 0;
1068}
1069
1070/*
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001071 * Return TRUE if the popup menu is displayed. Used to avoid some redrawing
1072 * that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
1073 * used to redraw the status lines.
Bram Moolenaar4b779472005-10-04 09:12:31 +00001074 */
1075 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001076pum_visible(void)
Bram Moolenaar4b779472005-10-04 09:12:31 +00001077{
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001078 return !pum_pretend_not_visible && pum_array != NULL;
Bram Moolenaar4b779472005-10-04 09:12:31 +00001079}
1080
Bram Moolenaare3226be2005-12-18 22:10:00 +00001081/*
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001082 * Return TRUE if the popup can be redrawn in the same position.
1083 */
1084 static int
1085pum_in_same_position(void)
1086{
1087 return pum_window != curwin
1088 || (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
1089 && pum_win_height == curwin->w_height
1090 && pum_win_col == curwin->w_wincol
1091 && pum_win_width == curwin->w_width);
1092}
1093
1094/*
1095 * Return TRUE when pum_may_redraw() will call pum_redraw().
1096 * This means that the pum area should not be overwritten to avoid flicker.
1097 */
1098 int
1099pum_redraw_in_same_position(void)
1100{
1101 if (!pum_visible() || pum_will_redraw)
1102 return FALSE; // nothing to do
1103
1104 return pum_in_same_position();
1105}
1106
1107/*
Bram Moolenaar491ac282018-06-17 14:47:55 +02001108 * Reposition the popup menu to adjust for window layout changes.
1109 */
1110 void
1111pum_may_redraw(void)
1112{
1113 pumitem_T *array = pum_array;
1114 int len = pum_size;
1115 int selected = pum_selected;
1116
Bram Moolenaar04357fb2019-12-10 21:50:56 +01001117 if (!pum_visible() || pum_will_redraw)
Bram Moolenaar491ac282018-06-17 14:47:55 +02001118 return; // nothing to do
1119
Bram Moolenaare0c03c82021-04-23 21:01:34 +02001120 if (pum_in_same_position())
Bram Moolenaar491ac282018-06-17 14:47:55 +02001121 {
1122 // window position didn't change, redraw in the same position
1123 pum_redraw();
1124 }
1125 else
1126 {
1127 int wcol = curwin->w_wcol;
1128
1129 // Window layout changed, recompute the position.
1130 // Use the remembered w_wcol value, the cursor may have moved when a
1131 // completion was inserted, but we want the menu in the same position.
1132 pum_undisplay();
1133 curwin->w_wcol = pum_win_wcol;
1134 curwin->w_valid |= VALID_WCOL;
1135 pum_display(array, len, selected);
1136 curwin->w_wcol = wcol;
1137 }
1138}
1139
1140/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00001141 * Return the height of the popup menu, the number of entries visible.
1142 * Only valid when pum_visible() returns TRUE!
1143 */
1144 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001145pum_get_height(void)
Bram Moolenaare3226be2005-12-18 22:10:00 +00001146{
1147 return pum_height;
1148}
1149
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001150#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001151/*
1152 * Add size information about the pum to "dict".
1153 */
1154 void
1155pum_set_event_info(dict_T *dict)
1156{
1157 if (!pum_visible())
1158 return;
Bram Moolenaarc0300632020-03-16 20:07:16 +01001159 (void)dict_add_number(dict, "height", pum_height);
1160 (void)dict_add_number(dict, "width", pum_width);
1161 (void)dict_add_number(dict, "row", pum_row);
1162 (void)dict_add_number(dict, "col", pum_col);
1163 (void)dict_add_number(dict, "size", pum_size);
1164 (void)dict_add_bool(dict, "scrollbar",
1165 pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001166}
Bram Moolenaar9cb698d2019-08-21 15:30:45 +02001167#endif
Bram Moolenaard7f246c2019-04-08 18:15:41 +02001168
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001169#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001170 static void
1171pum_position_at_mouse(int min_width)
1172{
1173 if (Rows - mouse_row > pum_size)
1174 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001175 // Enough space below the mouse row.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001176 pum_row = mouse_row + 1;
1177 if (pum_height > Rows - pum_row)
1178 pum_height = Rows - pum_row;
1179 }
1180 else
1181 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001182 // Show above the mouse row, reduce height if it does not fit.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001183 pum_row = mouse_row - pum_size;
1184 if (pum_row < 0)
1185 {
1186 pum_height += pum_row;
1187 pum_row = 0;
1188 }
1189 }
1190 if (Columns - mouse_col >= pum_base_width
1191 || Columns - mouse_col > min_width)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001192 // Enough space to show at mouse column.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001193 pum_col = mouse_col;
1194 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001195 // Not enough space, right align with window.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001196 pum_col = Columns - (pum_base_width > min_width
1197 ? min_width : pum_base_width);
1198
1199 pum_width = Columns - pum_col;
1200 if (pum_width > pum_base_width + 1)
1201 pum_width = pum_base_width + 1;
Bram Moolenaar0e6e1792018-06-17 17:10:59 +02001202
1203 // Do not redraw at cursor position.
1204 pum_window = NULL;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001205}
1206
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001207#endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001208
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001209#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001210static pumitem_T *balloon_array = NULL;
1211static int balloon_arraysize;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001212
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001213# define BALLOON_MIN_WIDTH 50
1214# define BALLOON_MIN_HEIGHT 10
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001215
Bram Moolenaar246fe032017-11-19 19:56:27 +01001216typedef struct {
1217 char_u *start;
1218 int bytelen;
1219 int cells;
1220 int indent;
1221} balpart_T;
1222
1223/*
1224 * Split a string into parts to display in the balloon.
1225 * Aimed at output from gdb. Attempts to split at white space, preserve quoted
1226 * strings and make a struct look good.
1227 * Resulting array is stored in "array" and returns the size of the array.
1228 */
1229 int
1230split_message(char_u *mesg, pumitem_T **array)
1231{
1232 garray_T ga;
1233 char_u *p;
1234 balpart_T *item;
1235 int quoted = FALSE;
1236 int height;
1237 int line;
1238 int item_idx;
1239 int indent = 0;
1240 int max_cells = 0;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001241 int max_height = Rows / 2 - 1;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001242 int long_item_count = 0;
1243 int split_long_items = FALSE;
1244
1245 ga_init2(&ga, sizeof(balpart_T), 20);
1246 p = mesg;
1247
1248 while (*p != NUL)
1249 {
1250 if (ga_grow(&ga, 1) == FAIL)
1251 goto failed;
1252 item = ((balpart_T *)ga.ga_data) + ga.ga_len;
1253 item->start = p;
1254 item->indent = indent;
1255 item->cells = indent * 2;
1256 ++ga.ga_len;
1257 while (*p != NUL)
1258 {
1259 if (*p == '"')
1260 quoted = !quoted;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001261 else if (*p == '\n')
1262 break;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001263 else if (*p == '\\' && p[1] != NUL)
1264 ++p;
1265 else if (!quoted)
1266 {
1267 if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
1268 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001269 // Looks like a good point to break.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001270 if (*p == '{')
1271 ++indent;
1272 else if (*p == '}' && indent > 0)
1273 --indent;
1274 ++item->cells;
1275 p = skipwhite(p + 1);
1276 break;
1277 }
1278 }
1279 item->cells += ptr2cells(p);
Bram Moolenaar1614a142019-10-06 22:00:13 +02001280 p += mb_ptr2len(p);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001281 }
1282 item->bytelen = p - item->start;
Bram Moolenaard1c1c822019-11-09 16:59:14 +01001283 if (*p == '\n')
1284 ++p;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001285 if (item->cells > max_cells)
1286 max_cells = item->cells;
Bram Moolenaara3571eb2017-11-26 16:53:16 +01001287 long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001288 }
1289
1290 height = 2 + ga.ga_len;
1291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001292 // If there are long items and the height is below the limit: split lines
Bram Moolenaar246fe032017-11-19 19:56:27 +01001293 if (long_item_count > 0 && height + long_item_count <= max_height)
1294 {
1295 split_long_items = TRUE;
1296 height += long_item_count;
1297 }
1298
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001299 // Limit to half the window height, it has to fit above or below the mouse
1300 // position.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001301 if (height > max_height)
1302 height = max_height;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001303 *array = ALLOC_CLEAR_MULT(pumitem_T, height);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001304 if (*array == NULL)
1305 goto failed;
1306
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001307 // Add an empty line above and below, looks better.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001308 (*array)->pum_text = vim_strsave((char_u *)"");
1309 (*array + height - 1)->pum_text = vim_strsave((char_u *)"");
1310
1311 for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
1312 {
1313 int skip;
1314 int thislen;
1315 int copylen;
1316 int ind;
1317 int cells;
1318
1319 item = ((balpart_T *)ga.ga_data) + item_idx;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001320 if (item->bytelen == 0)
1321 (*array)[line++].pum_text = vim_strsave((char_u *)"");
1322 else
1323 for (skip = 0; skip < item->bytelen; skip += thislen)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001324 {
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001325 if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
1326 {
1327 cells = item->indent * 2;
1328 for (p = item->start + skip;
1329 p < item->start + item->bytelen;
Bram Moolenaar1614a142019-10-06 22:00:13 +02001330 p += mb_ptr2len(p))
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001331 if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
1332 break;
1333 thislen = p - (item->start + skip);
1334 }
1335 else
1336 thislen = item->bytelen;
1337
1338 // put indent at the start
1339 p = alloc(thislen + item->indent * 2 + 1);
1340 if (p == NULL)
1341 {
1342 for (line = 0; line <= height - 1; ++line)
1343 vim_free((*array)[line].pum_text);
1344 vim_free(*array);
1345 goto failed;
1346 }
1347 for (ind = 0; ind < item->indent * 2; ++ind)
1348 p[ind] = ' ';
1349
1350 // exclude spaces at the end of the string
1351 for (copylen = thislen; copylen > 0; --copylen)
1352 if (item->start[skip + copylen - 1] != ' ')
Bram Moolenaar246fe032017-11-19 19:56:27 +01001353 break;
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001354
1355 vim_strncpy(p + ind, item->start + skip, copylen);
1356 (*array)[line].pum_text = p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001357 item->indent = 0; // wrapped line has no indent
Bram Moolenaar9ae862e2019-11-21 13:27:06 +01001358 ++line;
Bram Moolenaar246fe032017-11-19 19:56:27 +01001359 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001360 }
1361 ga_clear(&ga);
1362 return height;
1363
1364failed:
1365 ga_clear(&ga);
1366 return 0;
1367}
1368
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001369 void
1370ui_remove_balloon(void)
1371{
1372 if (balloon_array != NULL)
1373 {
1374 pum_undisplay();
1375 while (balloon_arraysize > 0)
1376 vim_free(balloon_array[--balloon_arraysize].pum_text);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001377 VIM_CLEAR(balloon_array);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001378 }
1379}
1380
1381/*
1382 * Terminal version of a balloon, uses the popup menu code.
1383 */
1384 void
Bram Moolenaar246fe032017-11-19 19:56:27 +01001385ui_post_balloon(char_u *mesg, list_T *list)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001386{
1387 ui_remove_balloon();
1388
Bram Moolenaar246fe032017-11-19 19:56:27 +01001389 if (mesg == NULL && list == NULL)
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001390 {
1391 pum_undisplay();
Bram Moolenaar246fe032017-11-19 19:56:27 +01001392 return;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001393 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001394 if (list != NULL)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001395 {
Bram Moolenaar246fe032017-11-19 19:56:27 +01001396 listitem_T *li;
1397 int idx;
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001398
Bram Moolenaar246fe032017-11-19 19:56:27 +01001399 balloon_arraysize = list->lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001400 balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001401 if (balloon_array == NULL)
1402 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001403 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001404 for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
1405 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001406 char_u *text = tv_get_string_chk(&li->li_tv);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001407
1408 balloon_array[idx].pum_text = vim_strsave(
1409 text == NULL ? (char_u *)"" : text);
1410 }
1411 }
1412 else
1413 balloon_arraysize = split_message(mesg, &balloon_array);
1414
1415 if (balloon_arraysize > 0)
1416 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001417 pum_array = balloon_array;
1418 pum_size = balloon_arraysize;
1419 pum_compute_size();
1420 pum_scrollbar = 0;
1421 pum_height = balloon_arraysize;
1422
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001423 pum_position_at_mouse(BALLOON_MIN_WIDTH);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001424 pum_selected = -1;
1425 pum_first = 0;
1426 pum_redraw();
1427 }
1428}
1429
1430/*
1431 * Called when the mouse moved, may remove any displayed balloon.
1432 */
1433 void
1434ui_may_remove_balloon(void)
1435{
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436 // For now: remove the balloon whenever the mouse moves to another screen
1437 // cell.
1438 ui_remove_balloon();
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001439}
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001440#endif
Bram Moolenaara8f04aa2018-02-10 15:36:55 +01001441
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001442#if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001443/*
1444 * Select the pum entry at the mouse position.
1445 */
1446 static void
1447pum_select_mouse_pos(void)
1448{
1449 int idx = mouse_row - pum_row;
1450
1451 if (idx < 0 || idx >= pum_size)
1452 pum_selected = -1;
1453 else if (*pum_array[idx].pum_text != NUL)
1454 pum_selected = idx;
1455}
1456
1457/*
1458 * Execute the currently selected popup menu item.
1459 */
1460 static void
Bram Moolenaar987723e2018-03-06 11:43:04 +01001461pum_execute_menu(vimmenu_T *menu, int mode)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001462{
1463 vimmenu_T *mp;
1464 int idx = 0;
1465 exarg_T ea;
1466
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001467 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar987723e2018-03-06 11:43:04 +01001468 if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001469 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02001470 CLEAR_FIELD(ea);
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02001471 execute_menu(&ea, mp, -1);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001472 break;
1473 }
1474}
1475
1476/*
1477 * Open the terminal version of the popup menu and don't return until it is
1478 * closed.
1479 */
1480 void
1481pum_show_popupmenu(vimmenu_T *menu)
1482{
1483 vimmenu_T *mp;
1484 int idx = 0;
1485 pumitem_T *array;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001486# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001487 int save_bevalterm = p_bevalterm;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001488# endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001489 int mode;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001490
1491 pum_undisplay();
1492 pum_size = 0;
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001493 mode = get_menu_mode_flag();
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001494
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001495 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001496 if (menu_is_separator(mp->dname)
1497 || (mp->modes & mp->enabled & mode))
1498 ++pum_size;
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001499
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001500 // When there are only Terminal mode menus, using "popup Edit" results in
1501 // pum_size being zero.
1502 if (pum_size <= 0)
1503 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001504 emsg(e_menu_only_exists_in_another_mode);
Bram Moolenaarf42b45d2019-01-06 13:11:05 +01001505 return;
1506 }
1507
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001508 array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001509 if (array == NULL)
1510 return;
1511
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001512 FOR_ALL_CHILD_MENUS(menu, mp)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001513 {
1514 char_u *s = NULL;
1515
1516 // Make a copy of the text, the menu may be redefined in a callback.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001517 if (menu_is_separator(mp->dname))
Bram Moolenaar38455a92020-12-24 18:39:02 +01001518 s = (char_u *)"";
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001519 else if (mp->modes & mp->enabled & mode)
Bram Moolenaar38455a92020-12-24 18:39:02 +01001520 s = mp->dname;
1521 if (s != NULL)
1522 {
1523 s = vim_strsave(s);
1524 if (s != NULL)
1525 array[idx++].pum_text = s;
1526 }
1527 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001528
1529 pum_array = array;
1530 pum_compute_size();
1531 pum_scrollbar = 0;
1532 pum_height = pum_size;
1533 pum_position_at_mouse(20);
1534
1535 pum_selected = -1;
1536 pum_first = 0;
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001537# ifdef FEAT_BEVAL_TERM
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001538 p_bevalterm = TRUE; // track mouse movement
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001539 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001540# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001541
1542 for (;;)
1543 {
1544 int c;
1545
1546 pum_redraw();
Bram Moolenaar987723e2018-03-06 11:43:04 +01001547 setcursor_mayforce(TRUE);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001548 out_flush();
1549
1550 c = vgetc();
Bram Moolenaar5c3fb042019-05-30 15:53:29 +02001551
1552 // Bail out when typing Esc, CTRL-C or some callback closed the popup
1553 // menu.
1554 if (c == ESC || c == Ctrl_C || pum_array == NULL)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001555 break;
1556 else if (c == CAR || c == NL)
1557 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001558 // enter: select current item, if any, and close
Bram Moolenaar987723e2018-03-06 11:43:04 +01001559 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001560 break;
1561 }
1562 else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
1563 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001564 // cursor up: select previous item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001565 while (pum_selected > 0)
1566 {
1567 --pum_selected;
1568 if (*array[pum_selected].pum_text != NUL)
1569 break;
1570 }
1571 }
1572 else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
1573 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001574 // cursor down: select next item
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001575 while (pum_selected < pum_size - 1)
1576 {
1577 ++pum_selected;
1578 if (*array[pum_selected].pum_text != NUL)
1579 break;
1580 }
1581 }
1582 else if (c == K_RIGHTMOUSE)
1583 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001584 // Right mouse down: reposition the menu.
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001585 vungetc(c);
1586 break;
1587 }
1588 else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
1589 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001590 // mouse moved: select item in the mouse row
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001591 pum_select_mouse_pos();
1592 }
1593 else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
1594 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001595 // left mouse click: select clicked item, if any, and close;
1596 // right mouse release: select clicked item, close if any
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001597 pum_select_mouse_pos();
1598 if (pum_selected >= 0)
1599 {
Bram Moolenaar987723e2018-03-06 11:43:04 +01001600 pum_execute_menu(menu, mode);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001601 break;
1602 }
1603 if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
1604 break;
1605 }
1606 }
1607
Bram Moolenaar38455a92020-12-24 18:39:02 +01001608 for (idx = 0; idx < pum_size; ++idx)
1609 vim_free(array[idx].pum_text);
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001610 vim_free(array);
1611 pum_undisplay();
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001612# ifdef FEAT_BEVAL_TERM
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001613 p_bevalterm = save_bevalterm;
1614 mch_setmouse(TRUE);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001615# endif
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01001616}
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001617
1618 void
1619pum_make_popup(char_u *path_name, int use_mouse_pos)
1620{
1621 vimmenu_T *menu;
1622
1623 if (!use_mouse_pos)
1624 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001625 // Hack: set mouse position at the cursor so that the menu pops up
1626 // around there.
Bram Moolenaar29a2c082018-03-05 21:06:23 +01001627 mouse_row = curwin->w_winrow + curwin->w_wrow;
1628 mouse_col = curwin->w_wincol + curwin->w_wcol;
1629 }
1630
1631 menu = gui_find_menu(path_name);
1632 if (menu != NULL)
1633 pum_show_popupmenu(menu);
1634}
Bram Moolenaar4b779472005-10-04 09:12:31 +00001635#endif