Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 2 | * |
| 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 Moolenaar | 76b92b2 | 2006-03-24 22:46:53 +0000 | [diff] [blame] | 11 | * popupmnu.c: Popup menu (PUM) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 12 | */ |
| 13 | #include "vim.h" |
| 14 | |
| 15 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 16 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 17 | static pumitem_T *pum_array = NULL; /* items of displayed pum */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 18 | static int pum_size; /* nr of items in "pum_array" */ |
| 19 | static int pum_selected; /* index of selected item or -1 */ |
| 20 | static int pum_first = 0; /* index of top item */ |
| 21 | |
| 22 | static int pum_height; /* nr of displayed pum items */ |
| 23 | static int pum_width; /* width of displayed pum items */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 24 | static int pum_base_width; /* width of pum items base */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 25 | static int pum_kind_width; /* width of pum items kind column */ |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 26 | static int pum_extra_width; /* width of extra stuff */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 27 | static int pum_scrollbar; /* TRUE when scrollbar present */ |
| 28 | |
| 29 | static int pum_row; /* top row of pum */ |
| 30 | static int pum_col; /* left column of pum */ |
| 31 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 32 | static int pum_do_redraw = FALSE; /* do redraw anyway */ |
| 33 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 34 | static int pum_set_selected(int n, int repeat); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 35 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 36 | #define PUM_DEF_HEIGHT 10 |
| 37 | #define PUM_DEF_WIDTH 15 |
| 38 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 39 | static void |
| 40 | pum_compute_size(void) |
| 41 | { |
| 42 | int i; |
| 43 | int w; |
| 44 | |
| 45 | /* Compute the width of the widest match and the widest extra. */ |
| 46 | pum_base_width = 0; |
| 47 | pum_kind_width = 0; |
| 48 | pum_extra_width = 0; |
| 49 | for (i = 0; i < pum_size; ++i) |
| 50 | { |
| 51 | w = vim_strsize(pum_array[i].pum_text); |
| 52 | if (pum_base_width < w) |
| 53 | pum_base_width = w; |
| 54 | if (pum_array[i].pum_kind != NULL) |
| 55 | { |
| 56 | w = vim_strsize(pum_array[i].pum_kind) + 1; |
| 57 | if (pum_kind_width < w) |
| 58 | pum_kind_width = w; |
| 59 | } |
| 60 | if (pum_array[i].pum_extra != NULL) |
| 61 | { |
| 62 | w = vim_strsize(pum_array[i].pum_extra) + 1; |
| 63 | if (pum_extra_width < w) |
| 64 | pum_extra_width = w; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 69 | /* |
| 70 | * Show the popup menu with items "array[size]". |
| 71 | * "array" must remain valid until pum_undisplay() is called! |
| 72 | * When possible the leftmost character is aligned with screen column "col". |
| 73 | * The menu appears above the screen line "row" or at "row" + "height" - 1. |
| 74 | */ |
| 75 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 76 | pum_display( |
| 77 | pumitem_T *array, |
| 78 | int size, |
| 79 | int selected) /* index of initially selected item, none if |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 80 | out of range */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 81 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 82 | int def_width; |
| 83 | int max_width; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 84 | int row; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 85 | int context_lines; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 86 | int col; |
Bram Moolenaar | 91e44a3 | 2016-11-04 20:08:52 +0100 | [diff] [blame] | 87 | int above_row; |
| 88 | int below_row; |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 89 | int redo_count = 0; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 90 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 91e44a3 | 2016-11-04 20:08:52 +0100 | [diff] [blame] | 91 | win_T *pvwin; |
Bram Moolenaar | aab3383 | 2016-11-04 22:08:29 +0100 | [diff] [blame] | 92 | #endif |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 93 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 94 | do |
| 95 | { |
| 96 | def_width = PUM_DEF_WIDTH; |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 97 | above_row = 0; |
| 98 | below_row = cmdline_row; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 99 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 100 | /* Pretend the pum is already there to avoid that must_redraw is set |
| 101 | * when 'cuc' is on. */ |
| 102 | pum_array = (pumitem_T *)1; |
| 103 | validate_cursor_col(); |
| 104 | pum_array = NULL; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 105 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 106 | row = curwin->w_wrow + W_WINROW(curwin); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 107 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 108 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 109 | FOR_ALL_WINDOWS(pvwin) |
| 110 | if (pvwin->w_p_pvw) |
| 111 | break; |
| 112 | if (pvwin != NULL) |
| 113 | { |
| 114 | if (W_WINROW(pvwin) < W_WINROW(curwin)) |
| 115 | above_row = W_WINROW(pvwin) + pvwin->w_height; |
| 116 | else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height) |
| 117 | below_row = W_WINROW(pvwin); |
| 118 | } |
Bram Moolenaar | 0106e3d | 2016-02-23 18:55:43 +0100 | [diff] [blame] | 119 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 120 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Figure out the size and position of the pum. |
| 123 | */ |
| 124 | if (size < PUM_DEF_HEIGHT) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 125 | pum_height = size; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 126 | else |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 127 | pum_height = PUM_DEF_HEIGHT; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 128 | if (p_ph > 0 && pum_height > p_ph) |
| 129 | pum_height = p_ph; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 130 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 131 | /* Put the pum below "row" if possible. If there are few lines decide |
| 132 | * on where there is more room. */ |
| 133 | if (row + 2 >= below_row - pum_height |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 134 | && row - above_row > (below_row - above_row) / 2) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 135 | { |
| 136 | /* pum above "row" */ |
| 137 | |
| 138 | /* Leave two lines of context if possible */ |
| 139 | if (curwin->w_wrow - curwin->w_cline_row >= 2) |
| 140 | context_lines = 2; |
| 141 | else |
| 142 | context_lines = curwin->w_wrow - curwin->w_cline_row; |
| 143 | |
| 144 | if (row >= size + context_lines) |
| 145 | { |
| 146 | pum_row = row - size - context_lines; |
| 147 | pum_height = size; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | pum_row = 0; |
| 152 | pum_height = row - context_lines; |
| 153 | } |
| 154 | if (p_ph > 0 && pum_height > p_ph) |
| 155 | { |
| 156 | pum_row += pum_height - p_ph; |
| 157 | pum_height = p_ph; |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | /* pum below "row" */ |
| 163 | |
| 164 | /* Leave two lines of context if possible */ |
| 165 | if (curwin->w_cline_row |
| 166 | + curwin->w_cline_height - curwin->w_wrow >= 3) |
| 167 | context_lines = 3; |
| 168 | else |
| 169 | context_lines = curwin->w_cline_row |
| 170 | + curwin->w_cline_height - curwin->w_wrow; |
| 171 | |
| 172 | pum_row = row + context_lines; |
| 173 | if (size > below_row - pum_row) |
| 174 | pum_height = below_row - pum_row; |
| 175 | else |
| 176 | pum_height = size; |
| 177 | if (p_ph > 0 && pum_height > p_ph) |
| 178 | pum_height = p_ph; |
| 179 | } |
| 180 | |
| 181 | /* don't display when we only have room for one line */ |
| 182 | if (pum_height < 1 || (pum_height == 1 && size > 1)) |
| 183 | return; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 184 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 185 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 186 | /* If there is a preview window at the above avoid drawing over it. */ |
| 187 | if (pvwin != NULL && pum_row < above_row && pum_height > above_row) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 188 | { |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 189 | pum_row += above_row; |
| 190 | pum_height -= above_row; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 191 | } |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 192 | #endif |
| 193 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 194 | pum_array = array; |
| 195 | pum_size = size; |
| 196 | pum_compute_size(); |
| 197 | max_width = pum_base_width; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 198 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 199 | /* Calculate column */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 200 | #ifdef FEAT_RIGHTLEFT |
| 201 | if (curwin->w_p_rl) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 202 | col = curwin->w_wincol + curwin->w_width - curwin->w_wcol - 1; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 203 | else |
| 204 | #endif |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 205 | col = curwin->w_wincol + curwin->w_wcol; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 206 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 207 | /* if there are more items than room we need a scrollbar */ |
| 208 | if (pum_height < size) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 209 | { |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 210 | pum_scrollbar = 1; |
| 211 | ++max_width; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 212 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 213 | else |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 214 | pum_scrollbar = 0; |
| 215 | |
| 216 | if (def_width < max_width) |
| 217 | def_width = max_width; |
| 218 | |
| 219 | if (((col < Columns - PUM_DEF_WIDTH || col < Columns - max_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 220 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 221 | && !curwin->w_p_rl) |
| 222 | || (curwin->w_p_rl && (col > PUM_DEF_WIDTH || col > max_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 223 | #endif |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 224 | )) |
| 225 | { |
| 226 | /* align pum column with "col" */ |
| 227 | pum_col = col; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 228 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 229 | #ifdef FEAT_RIGHTLEFT |
| 230 | if (curwin->w_p_rl) |
| 231 | pum_width = pum_col - pum_scrollbar + 1; |
| 232 | else |
| 233 | #endif |
| 234 | pum_width = Columns - pum_col - pum_scrollbar; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 235 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 236 | if (pum_width > max_width + pum_kind_width + pum_extra_width + 1 |
| 237 | && pum_width > PUM_DEF_WIDTH) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 238 | { |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 239 | pum_width = max_width + pum_kind_width + pum_extra_width + 1; |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 240 | if (pum_width < PUM_DEF_WIDTH) |
| 241 | pum_width = PUM_DEF_WIDTH; |
| 242 | } |
| 243 | } |
| 244 | else if (Columns < def_width) |
| 245 | { |
| 246 | /* not enough room, will use what we have */ |
| 247 | #ifdef FEAT_RIGHTLEFT |
| 248 | if (curwin->w_p_rl) |
| 249 | pum_col = Columns - 1; |
| 250 | else |
| 251 | #endif |
| 252 | pum_col = 0; |
| 253 | pum_width = Columns - 1; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | if (max_width > PUM_DEF_WIDTH) |
| 258 | max_width = PUM_DEF_WIDTH; /* truncate */ |
| 259 | #ifdef FEAT_RIGHTLEFT |
| 260 | if (curwin->w_p_rl) |
| 261 | pum_col = max_width - 1; |
| 262 | else |
| 263 | #endif |
| 264 | pum_col = Columns - max_width; |
| 265 | pum_width = max_width - pum_scrollbar; |
| 266 | } |
| 267 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 268 | /* Set selected item and redraw. If the window size changed need to |
| 269 | * redo the positioning. Limit this to two times, when there is not |
| 270 | * much room the window size will keep changing. */ |
| 271 | } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Redraw the popup menu, using "pum_first" and "pum_selected". |
| 276 | */ |
| 277 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 278 | pum_redraw(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 279 | { |
| 280 | int row = pum_row; |
| 281 | int col; |
| 282 | int attr_norm = highlight_attr[HLF_PNI]; |
| 283 | int attr_select = highlight_attr[HLF_PSI]; |
| 284 | int attr_scroll = highlight_attr[HLF_PSB]; |
| 285 | int attr_thumb = highlight_attr[HLF_PST]; |
| 286 | int attr; |
| 287 | int i; |
| 288 | int idx; |
| 289 | char_u *s; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 290 | char_u *p = NULL; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 291 | int totwidth, width, w; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 292 | int thumb_pos = 0; |
| 293 | int thumb_heigth = 1; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 294 | int round; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 295 | int n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 296 | |
Bram Moolenaar | 4c1e626 | 2013-11-06 04:04:33 +0100 | [diff] [blame] | 297 | /* Never display more than we have */ |
| 298 | if (pum_first > pum_size - pum_height) |
| 299 | pum_first = pum_size - pum_height; |
| 300 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 301 | if (pum_scrollbar) |
| 302 | { |
| 303 | thumb_heigth = pum_height * pum_height / pum_size; |
| 304 | if (thumb_heigth == 0) |
| 305 | thumb_heigth = 1; |
| 306 | thumb_pos = (pum_first * (pum_height - thumb_heigth) |
| 307 | + (pum_size - pum_height) / 2) |
| 308 | / (pum_size - pum_height); |
| 309 | } |
| 310 | |
| 311 | for (i = 0; i < pum_height; ++i) |
| 312 | { |
| 313 | idx = i + pum_first; |
| 314 | attr = (idx == pum_selected) ? attr_select : attr_norm; |
| 315 | |
| 316 | /* prepend a space if there is room */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 317 | #ifdef FEAT_RIGHTLEFT |
| 318 | if (curwin->w_p_rl) |
| 319 | { |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 320 | if (pum_col < curwin->w_wincol + curwin->w_width - 1) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 321 | screen_putchar(' ', row, pum_col + 1, attr); |
| 322 | } |
| 323 | else |
| 324 | #endif |
| 325 | if (pum_col > 0) |
| 326 | screen_putchar(' ', row, pum_col - 1, attr); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 327 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 328 | /* Display each entry, use two spaces for a Tab. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 329 | * Do this 3 times: For the main text, kind and extra info */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 330 | col = pum_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 331 | totwidth = 0; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 332 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 333 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 334 | width = 0; |
| 335 | s = NULL; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 336 | switch (round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 337 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 338 | case 1: p = pum_array[idx].pum_text; break; |
| 339 | case 2: p = pum_array[idx].pum_kind; break; |
| 340 | case 3: p = pum_array[idx].pum_extra; break; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 341 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 342 | if (p != NULL) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 343 | for ( ; ; MB_PTR_ADV(p)) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 344 | { |
| 345 | if (s == NULL) |
| 346 | s = p; |
| 347 | w = ptr2cells(p); |
| 348 | if (*p == NUL || *p == TAB || totwidth + w > pum_width) |
| 349 | { |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 350 | /* Display the text that fits or comes before a Tab. |
| 351 | * First convert it to printable characters. */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 352 | char_u *st; |
| 353 | int saved = *p; |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 354 | |
| 355 | *p = NUL; |
| 356 | st = transstr(s); |
| 357 | *p = saved; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 358 | #ifdef FEAT_RIGHTLEFT |
| 359 | if (curwin->w_p_rl) |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 360 | { |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 361 | if (st != NULL) |
| 362 | { |
| 363 | char_u *rt = reverse_text(st); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 364 | |
| 365 | if (rt != NULL) |
| 366 | { |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 367 | char_u *rt_start = rt; |
| 368 | int size; |
| 369 | |
| 370 | size = vim_strsize(rt); |
| 371 | if (size > pum_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 372 | { |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 373 | do |
| 374 | { |
| 375 | size -= has_mbyte |
| 376 | ? (*mb_ptr2cells)(rt) : 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 377 | MB_PTR_ADV(rt); |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 378 | } while (size > pum_width); |
| 379 | |
| 380 | if (size < pum_width) |
| 381 | { |
| 382 | /* Most left character requires |
| 383 | * 2-cells but only 1 cell is |
| 384 | * available on screen. Put a |
| 385 | * '<' on the left of the pum |
| 386 | * item */ |
| 387 | *(--rt) = '<'; |
| 388 | size++; |
| 389 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 390 | } |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 391 | screen_puts_len(rt, (int)STRLEN(rt), |
| 392 | row, col - size + 1, attr); |
| 393 | vim_free(rt_start); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 394 | } |
| 395 | vim_free(st); |
| 396 | } |
| 397 | col -= width; |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 398 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 399 | else |
| 400 | #endif |
| 401 | { |
| 402 | if (st != NULL) |
| 403 | { |
| 404 | screen_puts_len(st, (int)STRLEN(st), row, col, |
| 405 | attr); |
| 406 | vim_free(st); |
| 407 | } |
| 408 | col += width; |
| 409 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 410 | |
| 411 | if (*p != TAB) |
| 412 | break; |
| 413 | |
| 414 | /* Display two spaces for a Tab. */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 415 | #ifdef FEAT_RIGHTLEFT |
| 416 | if (curwin->w_p_rl) |
| 417 | { |
| 418 | screen_puts_len((char_u *)" ", 2, row, col - 1, |
| 419 | attr); |
| 420 | col -= 2; |
| 421 | } |
| 422 | else |
| 423 | #endif |
| 424 | { |
| 425 | screen_puts_len((char_u *)" ", 2, row, col, attr); |
| 426 | col += 2; |
| 427 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 428 | totwidth += 2; |
| 429 | s = NULL; /* start text at next char */ |
| 430 | width = 0; |
| 431 | } |
| 432 | else |
| 433 | width += w; |
| 434 | } |
| 435 | |
| 436 | if (round > 1) |
| 437 | n = pum_kind_width + 1; |
| 438 | else |
| 439 | n = 1; |
| 440 | |
| 441 | /* Stop when there is nothing more to display. */ |
| 442 | if (round == 3 |
| 443 | || (round == 2 && pum_array[idx].pum_extra == NULL) |
| 444 | || (round == 1 && pum_array[idx].pum_kind == NULL |
| 445 | && pum_array[idx].pum_extra == NULL) |
| 446 | || pum_base_width + n >= pum_width) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 447 | break; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 448 | #ifdef FEAT_RIGHTLEFT |
| 449 | if (curwin->w_p_rl) |
| 450 | { |
| 451 | screen_fill(row, row + 1, pum_col - pum_base_width - n + 1, |
| 452 | col + 1, ' ', ' ', attr); |
| 453 | col = pum_col - pum_base_width - n + 1; |
| 454 | } |
| 455 | else |
| 456 | #endif |
| 457 | { |
| 458 | screen_fill(row, row + 1, col, pum_col + pum_base_width + n, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 459 | ' ', ' ', attr); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 460 | col = pum_col + pum_base_width + n; |
| 461 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 462 | totwidth = pum_base_width + n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 465 | #ifdef FEAT_RIGHTLEFT |
| 466 | if (curwin->w_p_rl) |
| 467 | screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ', |
| 468 | ' ', attr); |
| 469 | else |
| 470 | #endif |
| 471 | screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', |
| 472 | attr); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 473 | if (pum_scrollbar > 0) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 474 | { |
| 475 | #ifdef FEAT_RIGHTLEFT |
| 476 | if (curwin->w_p_rl) |
| 477 | screen_putchar(' ', row, pum_col - pum_width, |
| 478 | i >= thumb_pos && i < thumb_pos + thumb_heigth |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 479 | ? attr_thumb : attr_scroll); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 480 | else |
| 481 | #endif |
| 482 | screen_putchar(' ', row, pum_col + pum_width, |
| 483 | i >= thumb_pos && i < thumb_pos + thumb_heigth |
| 484 | ? attr_thumb : attr_scroll); |
| 485 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 486 | |
| 487 | ++row; |
| 488 | } |
| 489 | } |
| 490 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 491 | /* |
| 492 | * Set the index of the currently selected item. The menu will scroll when |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 493 | * necessary. When "n" is out of range don't scroll. |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 494 | * This may be repeated when the preview window is used: |
| 495 | * "repeat" == 0: open preview window normally |
| 496 | * "repeat" == 1: open preview window but don't set the size |
| 497 | * "repeat" == 2: don't open preview window |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 498 | * Returns TRUE when the window was resized and the location of the popup menu |
| 499 | * must be recomputed. |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 500 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 501 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 502 | pum_set_selected(int n, int repeat) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 503 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 504 | int resized = FALSE; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 505 | int context = pum_height / 2; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 506 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 507 | pum_selected = n; |
| 508 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 509 | if (pum_selected >= 0 && pum_selected < pum_size) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 510 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 511 | if (pum_first > pum_selected - 4) |
| 512 | { |
| 513 | /* scroll down; when we did a jump it's probably a PageUp then |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 514 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 515 | if (pum_first > pum_selected - 2) |
| 516 | { |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 517 | pum_first -= pum_height - 2; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 518 | if (pum_first < 0) |
| 519 | pum_first = 0; |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 520 | else if (pum_first > pum_selected) |
| 521 | pum_first = pum_selected; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 522 | } |
| 523 | else |
| 524 | pum_first = pum_selected; |
| 525 | } |
| 526 | else if (pum_first < pum_selected - pum_height + 5) |
| 527 | { |
| 528 | /* scroll up; when we did a jump it's probably a PageDown then |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 529 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 530 | if (pum_first < pum_selected - pum_height + 1 + 2) |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 531 | { |
| 532 | pum_first += pum_height - 2; |
| 533 | if (pum_first < pum_selected - pum_height + 1) |
| 534 | pum_first = pum_selected - pum_height + 1; |
| 535 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 536 | else |
| 537 | pum_first = pum_selected - pum_height + 1; |
| 538 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 539 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 540 | /* Give a few lines of context when possible. */ |
| 541 | if (context > 3) |
| 542 | context = 3; |
| 543 | if (pum_height > 2) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 544 | { |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 545 | if (pum_first > pum_selected - context) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 546 | { |
| 547 | /* scroll down */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 548 | pum_first = pum_selected - context; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 549 | if (pum_first < 0) |
| 550 | pum_first = 0; |
| 551 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 552 | else if (pum_first < pum_selected + context - pum_height + 1) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 553 | { |
| 554 | /* scroll up */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 555 | pum_first = pum_selected + context - pum_height + 1; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 558 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 559 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 560 | /* |
| 561 | * Show extra info in the preview window if there is something and |
| 562 | * 'completeopt' contains "preview". |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 563 | * Skip this when tried twice already. |
| 564 | * Skip this also when there is not much room. |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 565 | * NOTE: Be very careful not to sync undo! |
| 566 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 567 | if (pum_array[pum_selected].pum_info != NULL |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 568 | && Rows > 10 |
| 569 | && repeat <= 1 |
| 570 | && vim_strchr(p_cot, 'p') != NULL) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 571 | { |
| 572 | win_T *curwin_save = curwin; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 573 | tabpage_T *curtab_save = curtab; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 574 | int res = OK; |
| 575 | |
Bram Moolenaar | ee236d0 | 2010-10-27 17:11:15 +0200 | [diff] [blame] | 576 | /* Open a preview window. 3 lines by default. Prefer |
| 577 | * 'previewheight' if set and smaller. */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 578 | g_do_tagpreview = 3; |
Bram Moolenaar | ee236d0 | 2010-10-27 17:11:15 +0200 | [diff] [blame] | 579 | if (p_pvh > 0 && p_pvh < g_do_tagpreview) |
| 580 | g_do_tagpreview = p_pvh; |
Bram Moolenaar | c804515 | 2014-07-09 19:58:24 +0200 | [diff] [blame] | 581 | ++RedrawingDisabled; |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 582 | /* Prevent undo sync here, if an autocommand syncs undo weird |
| 583 | * things can happen to the undo tree. */ |
| 584 | ++no_u_sync; |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 585 | resized = prepare_tagpreview(FALSE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 586 | --no_u_sync; |
Bram Moolenaar | c804515 | 2014-07-09 19:58:24 +0200 | [diff] [blame] | 587 | --RedrawingDisabled; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 588 | g_do_tagpreview = 0; |
| 589 | |
| 590 | if (curwin->w_p_pvw) |
| 591 | { |
Bram Moolenaar | 50e5376 | 2016-10-27 14:49:15 +0200 | [diff] [blame] | 592 | if (!resized |
| 593 | && curbuf->b_nwindows == 1 |
| 594 | && curbuf->b_fname == NULL |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 595 | && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f' |
| 596 | && curbuf->b_p_bh[0] == 'w') |
| 597 | { |
| 598 | /* Already a "wipeout" buffer, make it empty. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 599 | while (!BUFEMPTY()) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 600 | ml_delete((linenr_T)1, FALSE); |
| 601 | } |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 602 | else |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 603 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 604 | /* Don't want to sync undo in the current buffer. */ |
| 605 | ++no_u_sync; |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 606 | res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL); |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 607 | --no_u_sync; |
| 608 | if (res == OK) |
| 609 | { |
| 610 | /* Edit a new, empty buffer. Set options for a "wipeout" |
| 611 | * buffer. */ |
| 612 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 613 | set_option_value((char_u *)"bt", 0L, |
| 614 | (char_u *)"nofile", OPT_LOCAL); |
| 615 | set_option_value((char_u *)"bh", 0L, |
| 616 | (char_u *)"wipe", OPT_LOCAL); |
| 617 | set_option_value((char_u *)"diff", 0L, |
Bram Moolenaar | 7f51474 | 2007-06-28 19:33:43 +0000 | [diff] [blame] | 618 | NULL, OPT_LOCAL); |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 619 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 620 | } |
| 621 | if (res == OK) |
| 622 | { |
| 623 | char_u *p, *e; |
| 624 | linenr_T lnum = 0; |
| 625 | |
| 626 | for (p = pum_array[pum_selected].pum_info; *p != NUL; ) |
| 627 | { |
| 628 | e = vim_strchr(p, '\n'); |
| 629 | if (e == NULL) |
| 630 | { |
| 631 | ml_append(lnum++, p, 0, FALSE); |
| 632 | break; |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | *e = NUL; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 637 | ml_append(lnum++, p, (int)(e - p + 1), FALSE); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 638 | *e = '\n'; |
| 639 | p = e + 1; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* Increase the height of the preview window to show the |
| 644 | * text, but no more than 'previewheight' lines. */ |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 645 | if (repeat == 0) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 646 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 647 | if (lnum > p_pvh) |
| 648 | lnum = p_pvh; |
| 649 | if (curwin->w_height < lnum) |
| 650 | { |
| 651 | win_setheight((int)lnum); |
| 652 | resized = TRUE; |
| 653 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | curbuf->b_changed = 0; |
| 657 | curbuf->b_p_ma = FALSE; |
Bram Moolenaar | 9c27fc3 | 2010-03-17 14:48:24 +0100 | [diff] [blame] | 658 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 659 | curwin->w_cursor.col = 0; |
| 660 | |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 661 | if ((curwin != curwin_save && win_valid(curwin_save)) |
| 662 | || (curtab != curtab_save |
| 663 | && valid_tabpage(curtab_save))) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 664 | { |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 665 | if (curtab != curtab_save && valid_tabpage(curtab_save)) |
| 666 | goto_tabpage_tp(curtab_save, FALSE, FALSE); |
| 667 | |
Bram Moolenaar | 2bace3e | 2014-07-23 21:10:43 +0200 | [diff] [blame] | 668 | /* When the first completion is done and the preview |
| 669 | * window is not resized, skip the preview window's |
| 670 | * status line redrawing. */ |
| 671 | if (ins_compl_active() && !resized) |
| 672 | curwin->w_redr_status = FALSE; |
| 673 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 674 | /* Return cursor to where we were */ |
| 675 | validate_cursor(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 676 | redraw_later(SOME_VALID); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 677 | |
| 678 | /* When the preview window was resized we need to |
| 679 | * update the view on the buffer. Only go back to |
| 680 | * the window when needed, otherwise it will always be |
| 681 | * redraw. */ |
| 682 | if (resized) |
| 683 | { |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 684 | ++no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 685 | win_enter(curwin_save, TRUE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 686 | --no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 687 | update_topline(); |
| 688 | } |
| 689 | |
| 690 | /* Update the screen before drawing the popup menu. |
| 691 | * Enable updating the status lines. */ |
| 692 | pum_do_redraw = TRUE; |
| 693 | update_screen(0); |
| 694 | pum_do_redraw = FALSE; |
| 695 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 696 | if (!resized && win_valid(curwin_save)) |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 697 | { |
| 698 | ++no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 699 | win_enter(curwin_save, TRUE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 700 | --no_u_sync; |
| 701 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 702 | |
| 703 | /* May need to update the screen again when there are |
| 704 | * autocommands involved. */ |
| 705 | pum_do_redraw = TRUE; |
| 706 | update_screen(0); |
| 707 | pum_do_redraw = FALSE; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | #endif |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 715 | if (!resized) |
| 716 | pum_redraw(); |
| 717 | |
| 718 | return resized; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Undisplay the popup menu (later). |
| 723 | */ |
| 724 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 725 | pum_undisplay(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 726 | { |
| 727 | pum_array = NULL; |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 728 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 2d69460 | 2006-08-22 19:48:48 +0000 | [diff] [blame] | 729 | redraw_tabline = TRUE; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 730 | status_redraw_all(); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Clear the popup menu. Currently only resets the offset to the first |
| 735 | * displayed item. |
| 736 | */ |
| 737 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 738 | pum_clear(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 739 | { |
| 740 | pum_first = 0; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Return TRUE if the popup menu is displayed. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 745 | * Overruled when "pum_do_redraw" is set, used to redraw the status lines. |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 746 | */ |
| 747 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 748 | pum_visible(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 749 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 750 | return !pum_do_redraw && pum_array != NULL; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 753 | /* |
| 754 | * Return the height of the popup menu, the number of entries visible. |
| 755 | * Only valid when pum_visible() returns TRUE! |
| 756 | */ |
| 757 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 758 | pum_get_height(void) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 759 | { |
| 760 | return pum_height; |
| 761 | } |
| 762 | |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 763 | # if defined(FEAT_BEVAL_TERM) || defined(PROTO) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 764 | static pumitem_T *balloon_array = NULL; |
| 765 | static int balloon_arraysize; |
| 766 | static int balloon_mouse_row = 0; |
| 767 | static int balloon_mouse_col = 0; |
| 768 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 769 | #define BALLOON_MIN_WIDTH 50 |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 770 | #define BALLOON_MIN_HEIGHT 10 |
| 771 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 772 | typedef struct { |
| 773 | char_u *start; |
| 774 | int bytelen; |
| 775 | int cells; |
| 776 | int indent; |
| 777 | } balpart_T; |
| 778 | |
| 779 | /* |
| 780 | * Split a string into parts to display in the balloon. |
| 781 | * Aimed at output from gdb. Attempts to split at white space, preserve quoted |
| 782 | * strings and make a struct look good. |
| 783 | * Resulting array is stored in "array" and returns the size of the array. |
| 784 | */ |
| 785 | int |
| 786 | split_message(char_u *mesg, pumitem_T **array) |
| 787 | { |
| 788 | garray_T ga; |
| 789 | char_u *p; |
| 790 | balpart_T *item; |
| 791 | int quoted = FALSE; |
| 792 | int height; |
| 793 | int line; |
| 794 | int item_idx; |
| 795 | int indent = 0; |
| 796 | int max_cells = 0; |
| 797 | int max_height = Rows / 2 - 2; |
| 798 | int long_item_count = 0; |
| 799 | int split_long_items = FALSE; |
| 800 | |
| 801 | ga_init2(&ga, sizeof(balpart_T), 20); |
| 802 | p = mesg; |
| 803 | |
| 804 | while (*p != NUL) |
| 805 | { |
| 806 | if (ga_grow(&ga, 1) == FAIL) |
| 807 | goto failed; |
| 808 | item = ((balpart_T *)ga.ga_data) + ga.ga_len; |
| 809 | item->start = p; |
| 810 | item->indent = indent; |
| 811 | item->cells = indent * 2; |
| 812 | ++ga.ga_len; |
| 813 | while (*p != NUL) |
| 814 | { |
| 815 | if (*p == '"') |
| 816 | quoted = !quoted; |
| 817 | else if (*p == '\\' && p[1] != NUL) |
| 818 | ++p; |
| 819 | else if (!quoted) |
| 820 | { |
| 821 | if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}') |
| 822 | { |
| 823 | /* Looks like a good point to break. */ |
| 824 | if (*p == '{') |
| 825 | ++indent; |
| 826 | else if (*p == '}' && indent > 0) |
| 827 | --indent; |
| 828 | ++item->cells; |
| 829 | p = skipwhite(p + 1); |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | item->cells += ptr2cells(p); |
| 834 | p += MB_PTR2LEN(p); |
| 835 | } |
| 836 | item->bytelen = p - item->start; |
| 837 | if (item->cells > max_cells) |
| 838 | max_cells = item->cells; |
Bram Moolenaar | a3571eb | 2017-11-26 16:53:16 +0100 | [diff] [blame] | 839 | long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH; |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | height = 2 + ga.ga_len; |
| 843 | |
| 844 | /* If there are long items and the height is below the limit: split lines */ |
| 845 | if (long_item_count > 0 && height + long_item_count <= max_height) |
| 846 | { |
| 847 | split_long_items = TRUE; |
| 848 | height += long_item_count; |
| 849 | } |
| 850 | |
| 851 | /* Limit to half the window height, it has to fit above or below the mouse |
| 852 | * position. */ |
| 853 | if (height > max_height) |
| 854 | height = max_height; |
| 855 | *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height); |
| 856 | if (*array == NULL) |
| 857 | goto failed; |
| 858 | |
| 859 | /* Add an empty line above and below, looks better. */ |
| 860 | (*array)->pum_text = vim_strsave((char_u *)""); |
| 861 | (*array + height - 1)->pum_text = vim_strsave((char_u *)""); |
| 862 | |
| 863 | for (line = 1, item_idx = 0; line < height - 1; ++item_idx) |
| 864 | { |
| 865 | int skip; |
| 866 | int thislen; |
| 867 | int copylen; |
| 868 | int ind; |
| 869 | int cells; |
| 870 | |
| 871 | item = ((balpart_T *)ga.ga_data) + item_idx; |
| 872 | for (skip = 0; skip < item->bytelen; skip += thislen) |
| 873 | { |
| 874 | if (split_long_items && item->cells >= BALLOON_MIN_WIDTH) |
| 875 | { |
| 876 | cells = item->indent * 2; |
| 877 | for (p = item->start + skip; p < item->start + item->bytelen; |
| 878 | p += MB_PTR2LEN(p)) |
| 879 | if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH) |
| 880 | break; |
| 881 | thislen = p - (item->start + skip); |
| 882 | } |
| 883 | else |
| 884 | thislen = item->bytelen; |
| 885 | |
| 886 | /* put indent at the start */ |
| 887 | p = alloc(thislen + item->indent * 2 + 1); |
| 888 | for (ind = 0; ind < item->indent * 2; ++ind) |
| 889 | p[ind] = ' '; |
| 890 | |
| 891 | /* exclude spaces at the end of the string */ |
| 892 | for (copylen = thislen; copylen > 0; --copylen) |
| 893 | if (item->start[skip + copylen - 1] != ' ') |
| 894 | break; |
| 895 | |
| 896 | vim_strncpy(p + ind, item->start + skip, copylen); |
| 897 | (*array)[line].pum_text = p; |
| 898 | item->indent = 0; /* wrapped line has no indent */ |
| 899 | ++line; |
| 900 | } |
| 901 | } |
| 902 | ga_clear(&ga); |
| 903 | return height; |
| 904 | |
| 905 | failed: |
| 906 | ga_clear(&ga); |
| 907 | return 0; |
| 908 | } |
| 909 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 910 | void |
| 911 | ui_remove_balloon(void) |
| 912 | { |
| 913 | if (balloon_array != NULL) |
| 914 | { |
| 915 | pum_undisplay(); |
| 916 | while (balloon_arraysize > 0) |
| 917 | vim_free(balloon_array[--balloon_arraysize].pum_text); |
| 918 | vim_free(balloon_array); |
| 919 | balloon_array = NULL; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Terminal version of a balloon, uses the popup menu code. |
| 925 | */ |
| 926 | void |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 927 | ui_post_balloon(char_u *mesg, list_T *list) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 928 | { |
| 929 | ui_remove_balloon(); |
| 930 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 931 | if (mesg == NULL && list == NULL) |
| 932 | return; |
| 933 | if (list != NULL) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 934 | { |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 935 | listitem_T *li; |
| 936 | int idx; |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 937 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 938 | balloon_arraysize = list->lv_len; |
| 939 | balloon_array = (pumitem_T *)alloc_clear( |
| 940 | (unsigned)sizeof(pumitem_T) * list->lv_len); |
| 941 | if (balloon_array == NULL) |
| 942 | return; |
| 943 | for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx) |
| 944 | { |
| 945 | char_u *text = get_tv_string_chk(&li->li_tv); |
| 946 | |
| 947 | balloon_array[idx].pum_text = vim_strsave( |
| 948 | text == NULL ? (char_u *)"" : text); |
| 949 | } |
| 950 | } |
| 951 | else |
| 952 | balloon_arraysize = split_message(mesg, &balloon_array); |
| 953 | |
| 954 | if (balloon_arraysize > 0) |
| 955 | { |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 956 | pum_array = balloon_array; |
| 957 | pum_size = balloon_arraysize; |
| 958 | pum_compute_size(); |
| 959 | pum_scrollbar = 0; |
| 960 | pum_height = balloon_arraysize; |
| 961 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 962 | if (Rows - mouse_row > pum_size) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 963 | { |
| 964 | /* Enough space below the mouse row. */ |
| 965 | pum_row = mouse_row + 1; |
| 966 | if (pum_height > Rows - pum_row) |
| 967 | pum_height = Rows - pum_row; |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | /* Show above the mouse row, reduce height if it does not fit. */ |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 972 | pum_row = mouse_row - pum_size; |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 973 | if (pum_row < 0) |
| 974 | { |
| 975 | pum_height += pum_row; |
| 976 | pum_row = 0; |
| 977 | } |
| 978 | } |
| 979 | if (Columns - mouse_col >= pum_base_width |
| 980 | || Columns - mouse_col > BALLOON_MIN_WIDTH) |
| 981 | /* Enough space to show at mouse column. */ |
| 982 | pum_col = mouse_col; |
| 983 | else |
| 984 | /* Not enough space, right align with window. */ |
| 985 | pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH |
| 986 | ? BALLOON_MIN_WIDTH : pum_base_width); |
| 987 | |
| 988 | pum_width = Columns - pum_col; |
| 989 | if (pum_width > pum_base_width + 1) |
| 990 | pum_width = pum_base_width + 1; |
| 991 | |
| 992 | pum_selected = -1; |
| 993 | pum_first = 0; |
| 994 | pum_redraw(); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Called when the mouse moved, may remove any displayed balloon. |
| 1000 | */ |
| 1001 | void |
| 1002 | ui_may_remove_balloon(void) |
| 1003 | { |
| 1004 | if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col) |
| 1005 | ui_remove_balloon(); |
| 1006 | } |
| 1007 | # endif |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 1008 | #endif |