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