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 | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 763 | # if defined(FEAT_BEVALTERM) || defined(PROTO) |
| 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 | |
| 769 | #define BALLOON_MIN_WIDTH 40 |
| 770 | #define BALLOON_MIN_HEIGHT 10 |
| 771 | |
| 772 | void |
| 773 | ui_remove_balloon(void) |
| 774 | { |
| 775 | if (balloon_array != NULL) |
| 776 | { |
| 777 | pum_undisplay(); |
| 778 | while (balloon_arraysize > 0) |
| 779 | vim_free(balloon_array[--balloon_arraysize].pum_text); |
| 780 | vim_free(balloon_array); |
| 781 | balloon_array = NULL; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Terminal version of a balloon, uses the popup menu code. |
| 787 | */ |
| 788 | void |
| 789 | ui_post_balloon(char_u *mesg) |
| 790 | { |
| 791 | ui_remove_balloon(); |
| 792 | |
| 793 | /* TODO: split the text in multiple lines. */ |
| 794 | balloon_arraysize = 3; |
| 795 | balloon_array = (pumitem_T *)alloc_clear( |
| 796 | (unsigned)sizeof(pumitem_T) * balloon_arraysize); |
| 797 | if (balloon_array != NULL) |
| 798 | { |
| 799 | /* Add an empty line above and below, looks better. */ |
| 800 | balloon_array[0].pum_text = vim_strsave((char_u *)""); |
| 801 | balloon_array[1].pum_text = vim_strsave(mesg); |
| 802 | balloon_array[2].pum_text = vim_strsave((char_u *)""); |
| 803 | |
| 804 | pum_array = balloon_array; |
| 805 | pum_size = balloon_arraysize; |
| 806 | pum_compute_size(); |
| 807 | pum_scrollbar = 0; |
| 808 | pum_height = balloon_arraysize; |
| 809 | |
| 810 | if (Rows - mouse_row > BALLOON_MIN_HEIGHT) |
| 811 | { |
| 812 | /* Enough space below the mouse row. */ |
| 813 | pum_row = mouse_row + 1; |
| 814 | if (pum_height > Rows - pum_row) |
| 815 | pum_height = Rows - pum_row; |
| 816 | } |
| 817 | else |
| 818 | { |
| 819 | /* Show above the mouse row, reduce height if it does not fit. */ |
| 820 | pum_row = mouse_row - 1 - pum_size; |
| 821 | if (pum_row < 0) |
| 822 | { |
| 823 | pum_height += pum_row; |
| 824 | pum_row = 0; |
| 825 | } |
| 826 | } |
| 827 | if (Columns - mouse_col >= pum_base_width |
| 828 | || Columns - mouse_col > BALLOON_MIN_WIDTH) |
| 829 | /* Enough space to show at mouse column. */ |
| 830 | pum_col = mouse_col; |
| 831 | else |
| 832 | /* Not enough space, right align with window. */ |
| 833 | pum_col = Columns - (pum_base_width > BALLOON_MIN_WIDTH |
| 834 | ? BALLOON_MIN_WIDTH : pum_base_width); |
| 835 | |
| 836 | pum_width = Columns - pum_col; |
| 837 | if (pum_width > pum_base_width + 1) |
| 838 | pum_width = pum_base_width + 1; |
| 839 | |
| 840 | pum_selected = -1; |
| 841 | pum_first = 0; |
| 842 | pum_redraw(); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Called when the mouse moved, may remove any displayed balloon. |
| 848 | */ |
| 849 | void |
| 850 | ui_may_remove_balloon(void) |
| 851 | { |
| 852 | if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col) |
| 853 | ui_remove_balloon(); |
| 854 | } |
| 855 | # endif |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 856 | #endif |