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