Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 26 | static int pum_scrollbar; /* TRUE when scrollbar present */ |
| 27 | |
| 28 | static int pum_row; /* top row of pum */ |
| 29 | static int pum_col; /* left column of pum */ |
| 30 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 31 | static int pum_do_redraw = FALSE; /* do redraw anyway */ |
| 32 | |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 33 | static int pum_set_selected __ARGS((int n, int repeat)); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 34 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 35 | #define PUM_DEF_HEIGHT 10 |
| 36 | #define PUM_DEF_WIDTH 15 |
| 37 | |
| 38 | /* |
| 39 | * Show the popup menu with items "array[size]". |
| 40 | * "array" must remain valid until pum_undisplay() is called! |
| 41 | * When possible the leftmost character is aligned with screen column "col". |
| 42 | * The menu appears above the screen line "row" or at "row" + "height" - 1. |
| 43 | */ |
| 44 | void |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 45 | pum_display(array, size, selected) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 46 | pumitem_T *array; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 47 | int size; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 48 | int selected; /* index of initially selected item, none if |
| 49 | out of range */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 50 | { |
| 51 | int w; |
| 52 | int def_width; |
| 53 | int max_width; |
| 54 | int kind_width; |
| 55 | int extra_width; |
| 56 | int i; |
| 57 | int top_clear; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 58 | int row; |
| 59 | int height; |
| 60 | int col; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 61 | int above_row = cmdline_row; |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 62 | int redo_count = 0; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 63 | |
| 64 | redo: |
| 65 | def_width = PUM_DEF_WIDTH; |
| 66 | max_width = 0; |
| 67 | kind_width = 0; |
| 68 | extra_width = 0; |
| 69 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 70 | /* Pretend the pum is already there to avoid that must_redraw is set when |
| 71 | * 'cuc' is on. */ |
| 72 | pum_array = (pumitem_T *)1; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 73 | validate_cursor_col(); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 74 | pum_array = NULL; |
| 75 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 76 | row = curwin->w_cline_row + W_WINROW(curwin); |
| 77 | height = curwin->w_cline_height; |
| 78 | col = curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol; |
| 79 | |
| 80 | if (firstwin->w_p_pvw) |
| 81 | top_clear = firstwin->w_height; |
| 82 | else |
| 83 | top_clear = 0; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 84 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 85 | /* When the preview window is at the bottom stop just above it. Also |
| 86 | * avoid drawing over the status line so that it's clear there is a window |
| 87 | * boundary. */ |
| 88 | if (lastwin->w_p_pvw) |
| 89 | above_row -= lastwin->w_height + lastwin->w_status_height + 1; |
| 90 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 91 | /* |
| 92 | * Figure out the size and position of the pum. |
| 93 | */ |
| 94 | if (size < PUM_DEF_HEIGHT) |
| 95 | pum_height = size; |
| 96 | else |
| 97 | pum_height = PUM_DEF_HEIGHT; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 98 | if (p_ph > 0 && pum_height > p_ph) |
| 99 | pum_height = p_ph; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 100 | |
| 101 | /* Put the pum below "row" if possible. If there are few lines decide on |
| 102 | * where there is more room. */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 103 | if (row >= above_row - pum_height |
| 104 | && row > (above_row - top_clear - height) / 2) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 105 | { |
| 106 | /* pum above "row" */ |
| 107 | if (row >= size) |
| 108 | { |
| 109 | pum_row = row - size; |
| 110 | pum_height = size; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | pum_row = 0; |
| 115 | pum_height = row; |
| 116 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 117 | if (p_ph > 0 && pum_height > p_ph) |
| 118 | { |
| 119 | pum_row += pum_height - p_ph; |
| 120 | pum_height = p_ph; |
| 121 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
| 125 | /* pum below "row" */ |
| 126 | pum_row = row + height; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 127 | if (size > above_row - pum_row) |
| 128 | pum_height = above_row - pum_row; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 129 | else |
| 130 | pum_height = size; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 131 | if (p_ph > 0 && pum_height > p_ph) |
| 132 | pum_height = p_ph; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* don't display when we only have room for one line */ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 136 | if (pum_height < 1 || (pum_height == 1 && size > 1)) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 137 | return; |
| 138 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 139 | /* If there is a preview window at the top avoid drawing over it. */ |
| 140 | if (firstwin->w_p_pvw |
| 141 | && pum_row < firstwin->w_height |
| 142 | && pum_height > firstwin->w_height + 4) |
| 143 | { |
| 144 | pum_row += firstwin->w_height; |
| 145 | pum_height -= firstwin->w_height; |
| 146 | } |
| 147 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 148 | /* Compute the width of the widest match and the widest extra. */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 149 | for (i = 0; i < size; ++i) |
| 150 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 151 | w = vim_strsize(array[i].pum_text); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 152 | if (max_width < w) |
| 153 | max_width = w; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 154 | if (array[i].pum_kind != NULL) |
| 155 | { |
| 156 | w = vim_strsize(array[i].pum_kind) + 1; |
| 157 | if (kind_width < w) |
| 158 | kind_width = w; |
| 159 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 160 | if (array[i].pum_extra != NULL) |
| 161 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 162 | w = vim_strsize(array[i].pum_extra) + 1; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 163 | if (extra_width < w) |
| 164 | extra_width = w; |
| 165 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 166 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 167 | pum_base_width = max_width; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 168 | pum_kind_width = kind_width; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 169 | |
| 170 | /* if there are more items than room we need a scrollbar */ |
| 171 | if (pum_height < size) |
| 172 | { |
| 173 | pum_scrollbar = 1; |
| 174 | ++max_width; |
| 175 | } |
| 176 | else |
| 177 | pum_scrollbar = 0; |
| 178 | |
| 179 | if (def_width < max_width) |
| 180 | def_width = max_width; |
| 181 | |
| 182 | if (col < Columns - PUM_DEF_WIDTH || col < Columns - max_width) |
| 183 | { |
| 184 | /* align pum column with "col" */ |
| 185 | pum_col = col; |
| 186 | pum_width = Columns - pum_col - pum_scrollbar; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 187 | if (pum_width > max_width + kind_width + extra_width + 1 |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 188 | && pum_width > PUM_DEF_WIDTH) |
| 189 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 190 | pum_width = max_width + kind_width + extra_width + 1; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 191 | if (pum_width < PUM_DEF_WIDTH) |
| 192 | pum_width = PUM_DEF_WIDTH; |
| 193 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 194 | } |
| 195 | else if (Columns < def_width) |
| 196 | { |
| 197 | /* not enough room, will use what we have */ |
| 198 | pum_col = 0; |
| 199 | pum_width = Columns - 1; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | if (max_width > PUM_DEF_WIDTH) |
| 204 | max_width = PUM_DEF_WIDTH; /* truncate */ |
| 205 | pum_col = Columns - max_width; |
| 206 | pum_width = max_width - pum_scrollbar; |
| 207 | } |
| 208 | |
| 209 | pum_array = array; |
| 210 | pum_size = size; |
| 211 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 212 | /* Set selected item and redraw. If the window size changed need to redo |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 213 | * the positioning. Limit this to two times, when there is not much |
| 214 | * room the window size will keep changing. */ |
| 215 | if (pum_set_selected(selected, redo_count) && ++redo_count <= 2) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 216 | goto redo; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Redraw the popup menu, using "pum_first" and "pum_selected". |
| 221 | */ |
| 222 | void |
| 223 | pum_redraw() |
| 224 | { |
| 225 | int row = pum_row; |
| 226 | int col; |
| 227 | int attr_norm = highlight_attr[HLF_PNI]; |
| 228 | int attr_select = highlight_attr[HLF_PSI]; |
| 229 | int attr_scroll = highlight_attr[HLF_PSB]; |
| 230 | int attr_thumb = highlight_attr[HLF_PST]; |
| 231 | int attr; |
| 232 | int i; |
| 233 | int idx; |
| 234 | char_u *s; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 235 | char_u *p = NULL; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 236 | int totwidth, width, w; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 237 | int thumb_pos = 0; |
| 238 | int thumb_heigth = 1; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 239 | int round; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 240 | int n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 241 | |
| 242 | if (pum_scrollbar) |
| 243 | { |
| 244 | thumb_heigth = pum_height * pum_height / pum_size; |
| 245 | if (thumb_heigth == 0) |
| 246 | thumb_heigth = 1; |
| 247 | thumb_pos = (pum_first * (pum_height - thumb_heigth) |
| 248 | + (pum_size - pum_height) / 2) |
| 249 | / (pum_size - pum_height); |
| 250 | } |
| 251 | |
| 252 | for (i = 0; i < pum_height; ++i) |
| 253 | { |
| 254 | idx = i + pum_first; |
| 255 | attr = (idx == pum_selected) ? attr_select : attr_norm; |
| 256 | |
| 257 | /* prepend a space if there is room */ |
| 258 | if (pum_col > 0) |
| 259 | screen_putchar(' ', row, pum_col - 1, attr); |
| 260 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 261 | /* Display each entry, use two spaces for a Tab. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 262 | * Do this 3 times: For the main text, kind and extra info */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 263 | col = pum_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 264 | totwidth = 0; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 265 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 266 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 267 | width = 0; |
| 268 | s = NULL; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 269 | switch (round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 270 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 271 | case 1: p = pum_array[idx].pum_text; break; |
| 272 | case 2: p = pum_array[idx].pum_kind; break; |
| 273 | case 3: p = pum_array[idx].pum_extra; break; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 274 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 275 | if (p != NULL) |
| 276 | for ( ; ; mb_ptr_adv(p)) |
| 277 | { |
| 278 | if (s == NULL) |
| 279 | s = p; |
| 280 | w = ptr2cells(p); |
| 281 | if (*p == NUL || *p == TAB || totwidth + w > pum_width) |
| 282 | { |
| 283 | /* Display the text that fits or comes before a Tab. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 284 | screen_puts_len(s, (int)(p - s), row, col, attr); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 285 | col += width; |
| 286 | |
| 287 | if (*p != TAB) |
| 288 | break; |
| 289 | |
| 290 | /* Display two spaces for a Tab. */ |
| 291 | screen_puts_len((char_u *)" ", 2, row, col, attr); |
| 292 | col += 2; |
| 293 | totwidth += 2; |
| 294 | s = NULL; /* start text at next char */ |
| 295 | width = 0; |
| 296 | } |
| 297 | else |
| 298 | width += w; |
| 299 | } |
| 300 | |
| 301 | if (round > 1) |
| 302 | n = pum_kind_width + 1; |
| 303 | else |
| 304 | n = 1; |
| 305 | |
| 306 | /* Stop when there is nothing more to display. */ |
| 307 | if (round == 3 |
| 308 | || (round == 2 && pum_array[idx].pum_extra == NULL) |
| 309 | || (round == 1 && pum_array[idx].pum_kind == NULL |
| 310 | && pum_array[idx].pum_extra == NULL) |
| 311 | || pum_base_width + n >= pum_width) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 312 | break; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 313 | screen_fill(row, row + 1, col, pum_col + pum_base_width + n, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 314 | ' ', ' ', attr); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 315 | col = pum_col + pum_base_width + n; |
| 316 | totwidth = pum_base_width + n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', attr); |
| 320 | if (pum_scrollbar > 0) |
| 321 | screen_putchar(' ', row, pum_col + pum_width, |
| 322 | i >= thumb_pos && i < thumb_pos + thumb_heigth |
| 323 | ? attr_thumb : attr_scroll); |
| 324 | |
| 325 | ++row; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #if 0 /* not used yet */ |
| 330 | /* |
| 331 | * Return the index of the currently selected item. |
| 332 | */ |
| 333 | int |
| 334 | pum_get_selected() |
| 335 | { |
| 336 | return pum_selected; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | /* |
| 341 | * 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] | 342 | * necessary. When "n" is out of range don't scroll. |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 343 | * This may be repeated when the preview window is used: |
| 344 | * "repeat" == 0: open preview window normally |
| 345 | * "repeat" == 1: open preview window but don't set the size |
| 346 | * "repeat" == 2: don't open preview window |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 347 | * Returns TRUE when the window was resized and the location of the popup menu |
| 348 | * must be recomputed. |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 349 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 350 | static int |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 351 | pum_set_selected(n, repeat) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 352 | int n; |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 353 | int repeat; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 354 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 355 | int resized = FALSE; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 356 | int context = pum_height / 2; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 357 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 358 | pum_selected = n; |
| 359 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 360 | if (pum_selected >= 0 && pum_selected < pum_size) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 361 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 362 | if (pum_first > pum_selected - 4) |
| 363 | { |
| 364 | /* 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] | 365 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 366 | if (pum_first > pum_selected - 2) |
| 367 | { |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 368 | pum_first -= pum_height - 2; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 369 | if (pum_first < 0) |
| 370 | pum_first = 0; |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 371 | else if (pum_first > pum_selected) |
| 372 | pum_first = pum_selected; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 373 | } |
| 374 | else |
| 375 | pum_first = pum_selected; |
| 376 | } |
| 377 | else if (pum_first < pum_selected - pum_height + 5) |
| 378 | { |
| 379 | /* 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] | 380 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 381 | if (pum_first < pum_selected - pum_height + 1 + 2) |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 382 | { |
| 383 | pum_first += pum_height - 2; |
| 384 | if (pum_first < pum_selected - pum_height + 1) |
| 385 | pum_first = pum_selected - pum_height + 1; |
| 386 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 387 | else |
| 388 | pum_first = pum_selected - pum_height + 1; |
| 389 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 390 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 391 | /* Give a few lines of context when possible. */ |
| 392 | if (context > 3) |
| 393 | context = 3; |
| 394 | if (pum_height > 2) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 395 | { |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 396 | if (pum_first > pum_selected - context) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 397 | { |
| 398 | /* scroll down */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 399 | pum_first = pum_selected - context; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 400 | if (pum_first < 0) |
| 401 | pum_first = 0; |
| 402 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 403 | else if (pum_first < pum_selected + context - pum_height + 1) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 404 | { |
| 405 | /* scroll up */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 406 | pum_first = pum_selected + context - pum_height + 1; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 409 | |
| 410 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 411 | /* |
| 412 | * Show extra info in the preview window if there is something and |
| 413 | * 'completeopt' contains "preview". |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 414 | * Skip this when tried twice already. |
| 415 | * Skip this also when there is not much room. |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 416 | * NOTE: Be very careful not to sync undo! |
| 417 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 418 | if (pum_array[pum_selected].pum_info != NULL |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 419 | && Rows > 10 |
| 420 | && repeat <= 1 |
| 421 | && vim_strchr(p_cot, 'p') != NULL) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 422 | { |
| 423 | win_T *curwin_save = curwin; |
| 424 | int res = OK; |
| 425 | |
| 426 | /* Open a preview window. 3 lines by default. */ |
| 427 | g_do_tagpreview = 3; |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 428 | resized = prepare_tagpreview(FALSE); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 429 | g_do_tagpreview = 0; |
| 430 | |
| 431 | if (curwin->w_p_pvw) |
| 432 | { |
| 433 | if (curbuf->b_fname == NULL |
| 434 | && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f' |
| 435 | && curbuf->b_p_bh[0] == 'w') |
| 436 | { |
| 437 | /* Already a "wipeout" buffer, make it empty. */ |
| 438 | while (!bufempty()) |
| 439 | ml_delete((linenr_T)1, FALSE); |
| 440 | } |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 441 | else |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 442 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 443 | /* Don't want to sync undo in the current buffer. */ |
| 444 | ++no_u_sync; |
| 445 | res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0); |
| 446 | --no_u_sync; |
| 447 | if (res == OK) |
| 448 | { |
| 449 | /* Edit a new, empty buffer. Set options for a "wipeout" |
| 450 | * buffer. */ |
| 451 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 452 | set_option_value((char_u *)"bt", 0L, |
| 453 | (char_u *)"nofile", OPT_LOCAL); |
| 454 | set_option_value((char_u *)"bh", 0L, |
| 455 | (char_u *)"wipe", OPT_LOCAL); |
| 456 | set_option_value((char_u *)"diff", 0L, |
| 457 | (char_u *)"", OPT_LOCAL); |
| 458 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 459 | } |
| 460 | if (res == OK) |
| 461 | { |
| 462 | char_u *p, *e; |
| 463 | linenr_T lnum = 0; |
| 464 | |
| 465 | for (p = pum_array[pum_selected].pum_info; *p != NUL; ) |
| 466 | { |
| 467 | e = vim_strchr(p, '\n'); |
| 468 | if (e == NULL) |
| 469 | { |
| 470 | ml_append(lnum++, p, 0, FALSE); |
| 471 | break; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | *e = NUL; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 476 | ml_append(lnum++, p, (int)(e - p + 1), FALSE); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 477 | *e = '\n'; |
| 478 | p = e + 1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* Increase the height of the preview window to show the |
| 483 | * text, but no more than 'previewheight' lines. */ |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 484 | if (repeat == 0) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 485 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 486 | if (lnum > p_pvh) |
| 487 | lnum = p_pvh; |
| 488 | if (curwin->w_height < lnum) |
| 489 | { |
| 490 | win_setheight((int)lnum); |
| 491 | resized = TRUE; |
| 492 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | curbuf->b_changed = 0; |
| 496 | curbuf->b_p_ma = FALSE; |
| 497 | curwin->w_cursor.lnum = 0; |
| 498 | curwin->w_cursor.col = 0; |
| 499 | |
| 500 | if (curwin != curwin_save && win_valid(curwin_save)) |
| 501 | { |
| 502 | /* Return cursor to where we were */ |
| 503 | validate_cursor(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 504 | redraw_later(SOME_VALID); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 505 | |
| 506 | /* When the preview window was resized we need to |
| 507 | * update the view on the buffer. Only go back to |
| 508 | * the window when needed, otherwise it will always be |
| 509 | * redraw. */ |
| 510 | if (resized) |
| 511 | { |
| 512 | win_enter(curwin_save, TRUE); |
| 513 | update_topline(); |
| 514 | } |
| 515 | |
| 516 | /* Update the screen before drawing the popup menu. |
| 517 | * Enable updating the status lines. */ |
| 518 | pum_do_redraw = TRUE; |
| 519 | update_screen(0); |
| 520 | pum_do_redraw = FALSE; |
| 521 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 522 | if (!resized && win_valid(curwin_save)) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 523 | win_enter(curwin_save, TRUE); |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 524 | |
| 525 | /* May need to update the screen again when there are |
| 526 | * autocommands involved. */ |
| 527 | pum_do_redraw = TRUE; |
| 528 | update_screen(0); |
| 529 | pum_do_redraw = FALSE; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | #endif |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* Never display more than we have */ |
| 538 | if (pum_first > pum_size - pum_height) |
| 539 | pum_first = pum_size - pum_height; |
| 540 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 541 | if (!resized) |
| 542 | pum_redraw(); |
| 543 | |
| 544 | return resized; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Undisplay the popup menu (later). |
| 549 | */ |
| 550 | void |
| 551 | pum_undisplay() |
| 552 | { |
| 553 | pum_array = NULL; |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 554 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 2d69460 | 2006-08-22 19:48:48 +0000 | [diff] [blame] | 555 | #ifdef FEAT_WINDOWS |
| 556 | redraw_tabline = TRUE; |
| 557 | #endif |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 558 | status_redraw_all(); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Clear the popup menu. Currently only resets the offset to the first |
| 563 | * displayed item. |
| 564 | */ |
| 565 | void |
| 566 | pum_clear() |
| 567 | { |
| 568 | pum_first = 0; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Return TRUE if the popup menu is displayed. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 573 | * 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] | 574 | */ |
| 575 | int |
| 576 | pum_visible() |
| 577 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 578 | return !pum_do_redraw && pum_array != NULL; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Return the height of the popup menu, the number of entries visible. |
| 583 | * Only valid when pum_visible() returns TRUE! |
| 584 | */ |
| 585 | int |
| 586 | pum_get_height() |
| 587 | { |
| 588 | return pum_height; |
| 589 | } |
| 590 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 591 | #endif |