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! |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 72 | * When possible the leftmost character is aligned with cursor column. |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 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 | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 86 | int cursor_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 | { |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 96 | def_width = p_pw; |
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 | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 202 | cursor_col = curwin->w_wincol + curwin->w_width |
| 203 | - curwin->w_wcol - 1; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 204 | else |
| 205 | #endif |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 206 | cursor_col = curwin->w_wincol + curwin->w_wcol; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 207 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 208 | /* if there are more items than room we need a scrollbar */ |
| 209 | if (pum_height < size) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 210 | { |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 211 | pum_scrollbar = 1; |
| 212 | ++max_width; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 213 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 214 | else |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 215 | pum_scrollbar = 0; |
| 216 | |
| 217 | if (def_width < max_width) |
| 218 | def_width = max_width; |
| 219 | |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 220 | if (((cursor_col < Columns - p_pw |
| 221 | || cursor_col < Columns - max_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 222 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 223 | && !curwin->w_p_rl) |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 224 | || (curwin->w_p_rl |
| 225 | && (cursor_col > p_pw || cursor_col > max_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 226 | #endif |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 227 | )) |
| 228 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 229 | /* align pum with "cursor_col" */ |
| 230 | pum_col = cursor_col; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 231 | |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 232 | /* start with the maximum space available */ |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 233 | #ifdef FEAT_RIGHTLEFT |
| 234 | if (curwin->w_p_rl) |
| 235 | pum_width = pum_col - pum_scrollbar + 1; |
| 236 | else |
| 237 | #endif |
| 238 | pum_width = Columns - pum_col - pum_scrollbar; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 239 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 240 | if (pum_width > max_width + pum_kind_width + pum_extra_width + 1 |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 241 | && pum_width > p_pw) |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 242 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 243 | /* the width is more than needed for the items, make it |
| 244 | * narrower */ |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 245 | pum_width = max_width + pum_kind_width + pum_extra_width + 1; |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 246 | if (pum_width < p_pw) |
| 247 | pum_width = p_pw; |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 248 | } |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 249 | else if (((cursor_col > p_pw || cursor_col > max_width) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 250 | #ifdef FEAT_RIGHTLEFT |
| 251 | && !curwin->w_p_rl) |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 252 | || (curwin->w_p_rl && (cursor_col < Columns - p_pw |
| 253 | || cursor_col < Columns - max_width) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 254 | #endif |
| 255 | )) |
| 256 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 257 | /* align pum edge with "cursor_col" */ |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 258 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 259 | if (curwin->w_p_rl |
Bram Moolenaar | bb008dd | 2018-02-24 18:59:55 +0100 | [diff] [blame] | 260 | && W_ENDCOL(curwin) < max_width + pum_scrollbar + 1) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 261 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 262 | pum_col = cursor_col + max_width + pum_scrollbar + 1; |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 263 | if (pum_col >= Columns) |
| 264 | pum_col = Columns - 1; |
| 265 | } |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 266 | else if (!curwin->w_p_rl) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 267 | #endif |
| 268 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 269 | if (curwin->w_wincol > Columns - max_width - pum_scrollbar |
| 270 | && max_width <= p_pw) |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 271 | { |
Bram Moolenaar | 2b10bcb | 2018-02-24 21:25:44 +0100 | [diff] [blame] | 272 | /* use full width to end of the screen */ |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 273 | pum_col = Columns - max_width - pum_scrollbar; |
| 274 | if (pum_col < 0) |
| 275 | pum_col = 0; |
| 276 | } |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | #ifdef FEAT_RIGHTLEFT |
| 280 | if (curwin->w_p_rl) |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 281 | pum_width = pum_col - pum_scrollbar + 1; |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 282 | else |
| 283 | #endif |
Bram Moolenaar | 4287ed3 | 2018-02-17 20:35:29 +0100 | [diff] [blame] | 284 | pum_width = Columns - pum_col - pum_scrollbar; |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 285 | |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 286 | if (pum_width < p_pw) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 287 | { |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 288 | pum_width = p_pw; |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 289 | #ifdef FEAT_RIGHTLEFT |
| 290 | if (curwin->w_p_rl) |
| 291 | { |
| 292 | if (pum_width > pum_col) |
| 293 | pum_width = pum_col; |
| 294 | } |
| 295 | else |
| 296 | #endif |
| 297 | { |
| 298 | if (pum_width >= Columns - pum_col) |
| 299 | pum_width = Columns - pum_col - 1; |
| 300 | } |
| 301 | } |
| 302 | else if (pum_width > max_width + pum_kind_width |
| 303 | + pum_extra_width + 1 |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 304 | && pum_width > p_pw) |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 305 | { |
| 306 | pum_width = max_width + pum_kind_width |
| 307 | + pum_extra_width + 1; |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 308 | if (pum_width < p_pw) |
| 309 | pum_width = p_pw; |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 313 | } |
| 314 | else if (Columns < def_width) |
| 315 | { |
| 316 | /* not enough room, will use what we have */ |
| 317 | #ifdef FEAT_RIGHTLEFT |
| 318 | if (curwin->w_p_rl) |
| 319 | pum_col = Columns - 1; |
| 320 | else |
| 321 | #endif |
| 322 | pum_col = 0; |
| 323 | pum_width = Columns - 1; |
| 324 | } |
| 325 | else |
| 326 | { |
Bram Moolenaar | 42443c7 | 2018-02-10 18:28:52 +0100 | [diff] [blame] | 327 | if (max_width > p_pw) |
| 328 | max_width = p_pw; /* truncate */ |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 329 | #ifdef FEAT_RIGHTLEFT |
| 330 | if (curwin->w_p_rl) |
| 331 | pum_col = max_width - 1; |
| 332 | else |
| 333 | #endif |
| 334 | pum_col = Columns - max_width; |
| 335 | pum_width = max_width - pum_scrollbar; |
| 336 | } |
| 337 | |
Bram Moolenaar | a5e6621 | 2017-09-29 22:42:33 +0200 | [diff] [blame] | 338 | /* Set selected item and redraw. If the window size changed need to |
| 339 | * redo the positioning. Limit this to two times, when there is not |
| 340 | * much room the window size will keep changing. */ |
| 341 | } while (pum_set_selected(selected, redo_count) && ++redo_count <= 2); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Redraw the popup menu, using "pum_first" and "pum_selected". |
| 346 | */ |
| 347 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 348 | pum_redraw(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 349 | { |
| 350 | int row = pum_row; |
| 351 | int col; |
| 352 | int attr_norm = highlight_attr[HLF_PNI]; |
| 353 | int attr_select = highlight_attr[HLF_PSI]; |
| 354 | int attr_scroll = highlight_attr[HLF_PSB]; |
| 355 | int attr_thumb = highlight_attr[HLF_PST]; |
| 356 | int attr; |
| 357 | int i; |
| 358 | int idx; |
| 359 | char_u *s; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 360 | char_u *p = NULL; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 361 | int totwidth, width, w; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 362 | int thumb_pos = 0; |
| 363 | int thumb_heigth = 1; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 364 | int round; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 365 | int n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 366 | |
Bram Moolenaar | 4c1e626 | 2013-11-06 04:04:33 +0100 | [diff] [blame] | 367 | /* Never display more than we have */ |
| 368 | if (pum_first > pum_size - pum_height) |
| 369 | pum_first = pum_size - pum_height; |
| 370 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 371 | if (pum_scrollbar) |
| 372 | { |
| 373 | thumb_heigth = pum_height * pum_height / pum_size; |
| 374 | if (thumb_heigth == 0) |
| 375 | thumb_heigth = 1; |
| 376 | thumb_pos = (pum_first * (pum_height - thumb_heigth) |
| 377 | + (pum_size - pum_height) / 2) |
| 378 | / (pum_size - pum_height); |
| 379 | } |
| 380 | |
| 381 | for (i = 0; i < pum_height; ++i) |
| 382 | { |
| 383 | idx = i + pum_first; |
| 384 | attr = (idx == pum_selected) ? attr_select : attr_norm; |
| 385 | |
| 386 | /* prepend a space if there is room */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 387 | #ifdef FEAT_RIGHTLEFT |
| 388 | if (curwin->w_p_rl) |
| 389 | { |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 390 | if (pum_col < curwin->w_wincol + curwin->w_width - 1) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 391 | screen_putchar(' ', row, pum_col + 1, attr); |
| 392 | } |
| 393 | else |
| 394 | #endif |
| 395 | if (pum_col > 0) |
| 396 | screen_putchar(' ', row, pum_col - 1, attr); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 397 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 398 | /* Display each entry, use two spaces for a Tab. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 399 | * Do this 3 times: For the main text, kind and extra info */ |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 400 | col = pum_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 401 | totwidth = 0; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 402 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 403 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 404 | width = 0; |
| 405 | s = NULL; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 406 | switch (round) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 407 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 408 | case 1: p = pum_array[idx].pum_text; break; |
| 409 | case 2: p = pum_array[idx].pum_kind; break; |
| 410 | case 3: p = pum_array[idx].pum_extra; break; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 411 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 412 | if (p != NULL) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 413 | for ( ; ; MB_PTR_ADV(p)) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 414 | { |
| 415 | if (s == NULL) |
| 416 | s = p; |
| 417 | w = ptr2cells(p); |
| 418 | if (*p == NUL || *p == TAB || totwidth + w > pum_width) |
| 419 | { |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 420 | /* Display the text that fits or comes before a Tab. |
| 421 | * First convert it to printable characters. */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 422 | char_u *st; |
| 423 | int saved = *p; |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 424 | |
Bram Moolenaar | aef8c3d | 2018-03-03 18:59:16 +0100 | [diff] [blame] | 425 | if (saved != NUL) |
| 426 | *p = NUL; |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 427 | st = transstr(s); |
Bram Moolenaar | aef8c3d | 2018-03-03 18:59:16 +0100 | [diff] [blame] | 428 | if (saved != NUL) |
| 429 | *p = saved; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 430 | #ifdef FEAT_RIGHTLEFT |
| 431 | if (curwin->w_p_rl) |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 432 | { |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 433 | if (st != NULL) |
| 434 | { |
| 435 | char_u *rt = reverse_text(st); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 436 | |
| 437 | if (rt != NULL) |
| 438 | { |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 439 | char_u *rt_start = rt; |
| 440 | int size; |
| 441 | |
| 442 | size = vim_strsize(rt); |
| 443 | if (size > pum_width) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 444 | { |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 445 | do |
| 446 | { |
| 447 | size -= has_mbyte |
| 448 | ? (*mb_ptr2cells)(rt) : 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 449 | MB_PTR_ADV(rt); |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 450 | } while (size > pum_width); |
| 451 | |
| 452 | if (size < pum_width) |
| 453 | { |
| 454 | /* Most left character requires |
| 455 | * 2-cells but only 1 cell is |
| 456 | * available on screen. Put a |
| 457 | * '<' on the left of the pum |
| 458 | * item */ |
| 459 | *(--rt) = '<'; |
| 460 | size++; |
| 461 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 462 | } |
Bram Moolenaar | d836bb9 | 2010-01-19 18:06:03 +0100 | [diff] [blame] | 463 | screen_puts_len(rt, (int)STRLEN(rt), |
| 464 | row, col - size + 1, attr); |
| 465 | vim_free(rt_start); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 466 | } |
| 467 | vim_free(st); |
| 468 | } |
| 469 | col -= width; |
Bram Moolenaar | 7cc36e9 | 2007-03-27 10:42:05 +0000 | [diff] [blame] | 470 | } |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 471 | else |
| 472 | #endif |
| 473 | { |
| 474 | if (st != NULL) |
| 475 | { |
| 476 | screen_puts_len(st, (int)STRLEN(st), row, col, |
| 477 | attr); |
| 478 | vim_free(st); |
| 479 | } |
| 480 | col += width; |
| 481 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 482 | |
| 483 | if (*p != TAB) |
| 484 | break; |
| 485 | |
| 486 | /* Display two spaces for a Tab. */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 487 | #ifdef FEAT_RIGHTLEFT |
| 488 | if (curwin->w_p_rl) |
| 489 | { |
| 490 | screen_puts_len((char_u *)" ", 2, row, col - 1, |
| 491 | attr); |
| 492 | col -= 2; |
| 493 | } |
| 494 | else |
| 495 | #endif |
| 496 | { |
| 497 | screen_puts_len((char_u *)" ", 2, row, col, attr); |
| 498 | col += 2; |
| 499 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 500 | totwidth += 2; |
| 501 | s = NULL; /* start text at next char */ |
| 502 | width = 0; |
| 503 | } |
| 504 | else |
| 505 | width += w; |
| 506 | } |
| 507 | |
| 508 | if (round > 1) |
| 509 | n = pum_kind_width + 1; |
| 510 | else |
| 511 | n = 1; |
| 512 | |
| 513 | /* Stop when there is nothing more to display. */ |
| 514 | if (round == 3 |
| 515 | || (round == 2 && pum_array[idx].pum_extra == NULL) |
| 516 | || (round == 1 && pum_array[idx].pum_kind == NULL |
| 517 | && pum_array[idx].pum_extra == NULL) |
| 518 | || pum_base_width + n >= pum_width) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 519 | break; |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 520 | #ifdef FEAT_RIGHTLEFT |
| 521 | if (curwin->w_p_rl) |
| 522 | { |
| 523 | screen_fill(row, row + 1, pum_col - pum_base_width - n + 1, |
| 524 | col + 1, ' ', ' ', attr); |
| 525 | col = pum_col - pum_base_width - n + 1; |
| 526 | } |
| 527 | else |
| 528 | #endif |
| 529 | { |
| 530 | screen_fill(row, row + 1, col, pum_col + pum_base_width + n, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 531 | ' ', ' ', attr); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 532 | col = pum_col + pum_base_width + n; |
| 533 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 534 | totwidth = pum_base_width + n; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 537 | #ifdef FEAT_RIGHTLEFT |
| 538 | if (curwin->w_p_rl) |
| 539 | screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ', |
| 540 | ' ', attr); |
| 541 | else |
| 542 | #endif |
| 543 | screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ', |
| 544 | attr); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 545 | if (pum_scrollbar > 0) |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 546 | { |
| 547 | #ifdef FEAT_RIGHTLEFT |
| 548 | if (curwin->w_p_rl) |
| 549 | screen_putchar(' ', row, pum_col - pum_width, |
| 550 | i >= thumb_pos && i < thumb_pos + thumb_heigth |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 551 | ? attr_thumb : attr_scroll); |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 552 | else |
| 553 | #endif |
| 554 | screen_putchar(' ', row, pum_col + pum_width, |
| 555 | i >= thumb_pos && i < thumb_pos + thumb_heigth |
| 556 | ? attr_thumb : attr_scroll); |
| 557 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 558 | |
| 559 | ++row; |
| 560 | } |
| 561 | } |
| 562 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 563 | /* |
| 564 | * 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] | 565 | * necessary. When "n" is out of range don't scroll. |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 566 | * This may be repeated when the preview window is used: |
| 567 | * "repeat" == 0: open preview window normally |
| 568 | * "repeat" == 1: open preview window but don't set the size |
| 569 | * "repeat" == 2: don't open preview window |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 570 | * Returns TRUE when the window was resized and the location of the popup menu |
| 571 | * must be recomputed. |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 572 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 573 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 574 | pum_set_selected(int n, int repeat) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 575 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 576 | int resized = FALSE; |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 577 | int context = pum_height / 2; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 578 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 579 | pum_selected = n; |
| 580 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 581 | if (pum_selected >= 0 && pum_selected < pum_size) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 582 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 583 | if (pum_first > pum_selected - 4) |
| 584 | { |
| 585 | /* 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] | 586 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 587 | if (pum_first > pum_selected - 2) |
| 588 | { |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 589 | pum_first -= pum_height - 2; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 590 | if (pum_first < 0) |
| 591 | pum_first = 0; |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 592 | else if (pum_first > pum_selected) |
| 593 | pum_first = pum_selected; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 594 | } |
| 595 | else |
| 596 | pum_first = pum_selected; |
| 597 | } |
| 598 | else if (pum_first < pum_selected - pum_height + 5) |
| 599 | { |
| 600 | /* 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] | 601 | * scroll a whole page */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 602 | if (pum_first < pum_selected - pum_height + 1 + 2) |
Bram Moolenaar | 7df351e | 2006-01-23 22:30:28 +0000 | [diff] [blame] | 603 | { |
| 604 | pum_first += pum_height - 2; |
| 605 | if (pum_first < pum_selected - pum_height + 1) |
| 606 | pum_first = pum_selected - pum_height + 1; |
| 607 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 608 | else |
| 609 | pum_first = pum_selected - pum_height + 1; |
| 610 | } |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 611 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 612 | /* Give a few lines of context when possible. */ |
| 613 | if (context > 3) |
| 614 | context = 3; |
| 615 | if (pum_height > 2) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 616 | { |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 617 | if (pum_first > pum_selected - context) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 618 | { |
| 619 | /* scroll down */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 620 | pum_first = pum_selected - context; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 621 | if (pum_first < 0) |
| 622 | pum_first = 0; |
| 623 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 624 | else if (pum_first < pum_selected + context - pum_height + 1) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 625 | { |
| 626 | /* scroll up */ |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 627 | pum_first = pum_selected + context - pum_height + 1; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 630 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 631 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Show extra info in the preview window if there is something and |
| 634 | * 'completeopt' contains "preview". |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 635 | * Skip this when tried twice already. |
| 636 | * Skip this also when there is not much room. |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 637 | * NOTE: Be very careful not to sync undo! |
| 638 | */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 639 | if (pum_array[pum_selected].pum_info != NULL |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 640 | && Rows > 10 |
| 641 | && repeat <= 1 |
| 642 | && vim_strchr(p_cot, 'p') != NULL) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 643 | { |
| 644 | win_T *curwin_save = curwin; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 645 | tabpage_T *curtab_save = curtab; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 646 | int res = OK; |
| 647 | |
Bram Moolenaar | ee236d0 | 2010-10-27 17:11:15 +0200 | [diff] [blame] | 648 | /* Open a preview window. 3 lines by default. Prefer |
| 649 | * 'previewheight' if set and smaller. */ |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 650 | g_do_tagpreview = 3; |
Bram Moolenaar | ee236d0 | 2010-10-27 17:11:15 +0200 | [diff] [blame] | 651 | if (p_pvh > 0 && p_pvh < g_do_tagpreview) |
| 652 | g_do_tagpreview = p_pvh; |
Bram Moolenaar | c804515 | 2014-07-09 19:58:24 +0200 | [diff] [blame] | 653 | ++RedrawingDisabled; |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 654 | /* Prevent undo sync here, if an autocommand syncs undo weird |
| 655 | * things can happen to the undo tree. */ |
| 656 | ++no_u_sync; |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 657 | resized = prepare_tagpreview(FALSE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 658 | --no_u_sync; |
Bram Moolenaar | c804515 | 2014-07-09 19:58:24 +0200 | [diff] [blame] | 659 | --RedrawingDisabled; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 660 | g_do_tagpreview = 0; |
| 661 | |
| 662 | if (curwin->w_p_pvw) |
| 663 | { |
Bram Moolenaar | 50e5376 | 2016-10-27 14:49:15 +0200 | [diff] [blame] | 664 | if (!resized |
| 665 | && curbuf->b_nwindows == 1 |
| 666 | && curbuf->b_fname == NULL |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 667 | && curbuf->b_p_bt[0] == 'n' && curbuf->b_p_bt[2] == 'f' |
| 668 | && curbuf->b_p_bh[0] == 'w') |
| 669 | { |
| 670 | /* Already a "wipeout" buffer, make it empty. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 671 | while (!BUFEMPTY()) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 672 | ml_delete((linenr_T)1, FALSE); |
| 673 | } |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 674 | else |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 675 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 676 | /* Don't want to sync undo in the current buffer. */ |
| 677 | ++no_u_sync; |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 678 | res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL); |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 679 | --no_u_sync; |
| 680 | if (res == OK) |
| 681 | { |
| 682 | /* Edit a new, empty buffer. Set options for a "wipeout" |
| 683 | * buffer. */ |
| 684 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 685 | set_option_value((char_u *)"bt", 0L, |
| 686 | (char_u *)"nofile", OPT_LOCAL); |
| 687 | set_option_value((char_u *)"bh", 0L, |
| 688 | (char_u *)"wipe", OPT_LOCAL); |
| 689 | set_option_value((char_u *)"diff", 0L, |
Bram Moolenaar | 7f51474 | 2007-06-28 19:33:43 +0000 | [diff] [blame] | 690 | NULL, OPT_LOCAL); |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 691 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 692 | } |
| 693 | if (res == OK) |
| 694 | { |
| 695 | char_u *p, *e; |
| 696 | linenr_T lnum = 0; |
| 697 | |
| 698 | for (p = pum_array[pum_selected].pum_info; *p != NUL; ) |
| 699 | { |
| 700 | e = vim_strchr(p, '\n'); |
| 701 | if (e == NULL) |
| 702 | { |
| 703 | ml_append(lnum++, p, 0, FALSE); |
| 704 | break; |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | *e = NUL; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 709 | ml_append(lnum++, p, (int)(e - p + 1), FALSE); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 710 | *e = '\n'; |
| 711 | p = e + 1; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /* Increase the height of the preview window to show the |
| 716 | * text, but no more than 'previewheight' lines. */ |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 717 | if (repeat == 0) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 718 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 719 | if (lnum > p_pvh) |
| 720 | lnum = p_pvh; |
| 721 | if (curwin->w_height < lnum) |
| 722 | { |
| 723 | win_setheight((int)lnum); |
| 724 | resized = TRUE; |
| 725 | } |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | curbuf->b_changed = 0; |
| 729 | curbuf->b_p_ma = FALSE; |
Bram Moolenaar | 9c27fc3 | 2010-03-17 14:48:24 +0100 | [diff] [blame] | 730 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 731 | curwin->w_cursor.col = 0; |
| 732 | |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 733 | if ((curwin != curwin_save && win_valid(curwin_save)) |
| 734 | || (curtab != curtab_save |
| 735 | && valid_tabpage(curtab_save))) |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 736 | { |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 737 | if (curtab != curtab_save && valid_tabpage(curtab_save)) |
| 738 | goto_tabpage_tp(curtab_save, FALSE, FALSE); |
| 739 | |
Bram Moolenaar | 2bace3e | 2014-07-23 21:10:43 +0200 | [diff] [blame] | 740 | /* When the first completion is done and the preview |
| 741 | * window is not resized, skip the preview window's |
| 742 | * status line redrawing. */ |
| 743 | if (ins_compl_active() && !resized) |
| 744 | curwin->w_redr_status = FALSE; |
| 745 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 746 | /* Return cursor to where we were */ |
| 747 | validate_cursor(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 748 | redraw_later(SOME_VALID); |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 749 | |
| 750 | /* When the preview window was resized we need to |
| 751 | * update the view on the buffer. Only go back to |
| 752 | * the window when needed, otherwise it will always be |
| 753 | * redraw. */ |
| 754 | if (resized) |
| 755 | { |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 756 | ++no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 757 | win_enter(curwin_save, TRUE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 758 | --no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 759 | update_topline(); |
| 760 | } |
| 761 | |
| 762 | /* Update the screen before drawing the popup menu. |
| 763 | * Enable updating the status lines. */ |
| 764 | pum_do_redraw = TRUE; |
| 765 | update_screen(0); |
| 766 | pum_do_redraw = FALSE; |
| 767 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 768 | if (!resized && win_valid(curwin_save)) |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 769 | { |
| 770 | ++no_u_sync; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 771 | win_enter(curwin_save, TRUE); |
Bram Moolenaar | e7d1376 | 2015-10-30 14:23:33 +0100 | [diff] [blame] | 772 | --no_u_sync; |
| 773 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 774 | |
| 775 | /* May need to update the screen again when there are |
| 776 | * autocommands involved. */ |
| 777 | pum_do_redraw = TRUE; |
| 778 | update_screen(0); |
| 779 | pum_do_redraw = FALSE; |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | #endif |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 787 | if (!resized) |
| 788 | pum_redraw(); |
| 789 | |
| 790 | return resized; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Undisplay the popup menu (later). |
| 795 | */ |
| 796 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 797 | pum_undisplay(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 798 | { |
| 799 | pum_array = NULL; |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 800 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 2d69460 | 2006-08-22 19:48:48 +0000 | [diff] [blame] | 801 | redraw_tabline = TRUE; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 802 | status_redraw_all(); |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Clear the popup menu. Currently only resets the offset to the first |
| 807 | * displayed item. |
| 808 | */ |
| 809 | void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 810 | pum_clear(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 811 | { |
| 812 | pum_first = 0; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Return TRUE if the popup menu is displayed. |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 817 | * 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] | 818 | */ |
| 819 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 820 | pum_visible(void) |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 821 | { |
Bram Moolenaar | 96d2c5b | 2006-03-11 21:27:59 +0000 | [diff] [blame] | 822 | return !pum_do_redraw && pum_array != NULL; |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 825 | /* |
| 826 | * Return the height of the popup menu, the number of entries visible. |
| 827 | * Only valid when pum_visible() returns TRUE! |
| 828 | */ |
| 829 | int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 830 | pum_get_height(void) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 831 | { |
| 832 | return pum_height; |
| 833 | } |
| 834 | |
Bram Moolenaar | aef8c3d | 2018-03-03 18:59:16 +0100 | [diff] [blame] | 835 | # if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO) |
| 836 | static void |
| 837 | pum_position_at_mouse(int min_width) |
| 838 | { |
| 839 | if (Rows - mouse_row > pum_size) |
| 840 | { |
| 841 | /* Enough space below the mouse row. */ |
| 842 | pum_row = mouse_row + 1; |
| 843 | if (pum_height > Rows - pum_row) |
| 844 | pum_height = Rows - pum_row; |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | /* Show above the mouse row, reduce height if it does not fit. */ |
| 849 | pum_row = mouse_row - pum_size; |
| 850 | if (pum_row < 0) |
| 851 | { |
| 852 | pum_height += pum_row; |
| 853 | pum_row = 0; |
| 854 | } |
| 855 | } |
| 856 | if (Columns - mouse_col >= pum_base_width |
| 857 | || Columns - mouse_col > min_width) |
| 858 | /* Enough space to show at mouse column. */ |
| 859 | pum_col = mouse_col; |
| 860 | else |
| 861 | /* Not enough space, right align with window. */ |
| 862 | pum_col = Columns - (pum_base_width > min_width |
| 863 | ? min_width : pum_base_width); |
| 864 | |
| 865 | pum_width = Columns - pum_col; |
| 866 | if (pum_width > pum_base_width + 1) |
| 867 | pum_width = pum_base_width + 1; |
| 868 | } |
| 869 | |
| 870 | # endif |
| 871 | |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 872 | # if defined(FEAT_BEVAL_TERM) || defined(PROTO) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 873 | static pumitem_T *balloon_array = NULL; |
| 874 | static int balloon_arraysize; |
| 875 | static int balloon_mouse_row = 0; |
| 876 | static int balloon_mouse_col = 0; |
| 877 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 878 | #define BALLOON_MIN_WIDTH 50 |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 879 | #define BALLOON_MIN_HEIGHT 10 |
| 880 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 881 | typedef struct { |
| 882 | char_u *start; |
| 883 | int bytelen; |
| 884 | int cells; |
| 885 | int indent; |
| 886 | } balpart_T; |
| 887 | |
| 888 | /* |
| 889 | * Split a string into parts to display in the balloon. |
| 890 | * Aimed at output from gdb. Attempts to split at white space, preserve quoted |
| 891 | * strings and make a struct look good. |
| 892 | * Resulting array is stored in "array" and returns the size of the array. |
| 893 | */ |
| 894 | int |
| 895 | split_message(char_u *mesg, pumitem_T **array) |
| 896 | { |
| 897 | garray_T ga; |
| 898 | char_u *p; |
| 899 | balpart_T *item; |
| 900 | int quoted = FALSE; |
| 901 | int height; |
| 902 | int line; |
| 903 | int item_idx; |
| 904 | int indent = 0; |
| 905 | int max_cells = 0; |
| 906 | int max_height = Rows / 2 - 2; |
| 907 | int long_item_count = 0; |
| 908 | int split_long_items = FALSE; |
| 909 | |
| 910 | ga_init2(&ga, sizeof(balpart_T), 20); |
| 911 | p = mesg; |
| 912 | |
| 913 | while (*p != NUL) |
| 914 | { |
| 915 | if (ga_grow(&ga, 1) == FAIL) |
| 916 | goto failed; |
| 917 | item = ((balpart_T *)ga.ga_data) + ga.ga_len; |
| 918 | item->start = p; |
| 919 | item->indent = indent; |
| 920 | item->cells = indent * 2; |
| 921 | ++ga.ga_len; |
| 922 | while (*p != NUL) |
| 923 | { |
| 924 | if (*p == '"') |
| 925 | quoted = !quoted; |
| 926 | else if (*p == '\\' && p[1] != NUL) |
| 927 | ++p; |
| 928 | else if (!quoted) |
| 929 | { |
| 930 | if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}') |
| 931 | { |
| 932 | /* Looks like a good point to break. */ |
| 933 | if (*p == '{') |
| 934 | ++indent; |
| 935 | else if (*p == '}' && indent > 0) |
| 936 | --indent; |
| 937 | ++item->cells; |
| 938 | p = skipwhite(p + 1); |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | item->cells += ptr2cells(p); |
| 943 | p += MB_PTR2LEN(p); |
| 944 | } |
| 945 | item->bytelen = p - item->start; |
| 946 | if (item->cells > max_cells) |
| 947 | max_cells = item->cells; |
Bram Moolenaar | a3571eb | 2017-11-26 16:53:16 +0100 | [diff] [blame] | 948 | long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH; |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | height = 2 + ga.ga_len; |
| 952 | |
| 953 | /* If there are long items and the height is below the limit: split lines */ |
| 954 | if (long_item_count > 0 && height + long_item_count <= max_height) |
| 955 | { |
| 956 | split_long_items = TRUE; |
| 957 | height += long_item_count; |
| 958 | } |
| 959 | |
| 960 | /* Limit to half the window height, it has to fit above or below the mouse |
| 961 | * position. */ |
| 962 | if (height > max_height) |
| 963 | height = max_height; |
| 964 | *array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * height); |
| 965 | if (*array == NULL) |
| 966 | goto failed; |
| 967 | |
| 968 | /* Add an empty line above and below, looks better. */ |
| 969 | (*array)->pum_text = vim_strsave((char_u *)""); |
| 970 | (*array + height - 1)->pum_text = vim_strsave((char_u *)""); |
| 971 | |
| 972 | for (line = 1, item_idx = 0; line < height - 1; ++item_idx) |
| 973 | { |
| 974 | int skip; |
| 975 | int thislen; |
| 976 | int copylen; |
| 977 | int ind; |
| 978 | int cells; |
| 979 | |
| 980 | item = ((balpart_T *)ga.ga_data) + item_idx; |
| 981 | for (skip = 0; skip < item->bytelen; skip += thislen) |
| 982 | { |
| 983 | if (split_long_items && item->cells >= BALLOON_MIN_WIDTH) |
| 984 | { |
| 985 | cells = item->indent * 2; |
| 986 | for (p = item->start + skip; p < item->start + item->bytelen; |
| 987 | p += MB_PTR2LEN(p)) |
| 988 | if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH) |
| 989 | break; |
| 990 | thislen = p - (item->start + skip); |
| 991 | } |
| 992 | else |
| 993 | thislen = item->bytelen; |
| 994 | |
| 995 | /* put indent at the start */ |
| 996 | p = alloc(thislen + item->indent * 2 + 1); |
| 997 | for (ind = 0; ind < item->indent * 2; ++ind) |
| 998 | p[ind] = ' '; |
| 999 | |
| 1000 | /* exclude spaces at the end of the string */ |
| 1001 | for (copylen = thislen; copylen > 0; --copylen) |
| 1002 | if (item->start[skip + copylen - 1] != ' ') |
| 1003 | break; |
| 1004 | |
| 1005 | vim_strncpy(p + ind, item->start + skip, copylen); |
| 1006 | (*array)[line].pum_text = p; |
| 1007 | item->indent = 0; /* wrapped line has no indent */ |
| 1008 | ++line; |
| 1009 | } |
| 1010 | } |
| 1011 | ga_clear(&ga); |
| 1012 | return height; |
| 1013 | |
| 1014 | failed: |
| 1015 | ga_clear(&ga); |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1019 | void |
| 1020 | ui_remove_balloon(void) |
| 1021 | { |
| 1022 | if (balloon_array != NULL) |
| 1023 | { |
| 1024 | pum_undisplay(); |
| 1025 | while (balloon_arraysize > 0) |
| 1026 | vim_free(balloon_array[--balloon_arraysize].pum_text); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1027 | VIM_CLEAR(balloon_array); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Terminal version of a balloon, uses the popup menu code. |
| 1033 | */ |
| 1034 | void |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1035 | ui_post_balloon(char_u *mesg, list_T *list) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1036 | { |
| 1037 | ui_remove_balloon(); |
| 1038 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1039 | if (mesg == NULL && list == NULL) |
| 1040 | return; |
| 1041 | if (list != NULL) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1042 | { |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1043 | listitem_T *li; |
| 1044 | int idx; |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1045 | |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1046 | balloon_arraysize = list->lv_len; |
| 1047 | balloon_array = (pumitem_T *)alloc_clear( |
| 1048 | (unsigned)sizeof(pumitem_T) * list->lv_len); |
| 1049 | if (balloon_array == NULL) |
| 1050 | return; |
| 1051 | for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx) |
| 1052 | { |
| 1053 | char_u *text = get_tv_string_chk(&li->li_tv); |
| 1054 | |
| 1055 | balloon_array[idx].pum_text = vim_strsave( |
| 1056 | text == NULL ? (char_u *)"" : text); |
| 1057 | } |
| 1058 | } |
| 1059 | else |
| 1060 | balloon_arraysize = split_message(mesg, &balloon_array); |
| 1061 | |
| 1062 | if (balloon_arraysize > 0) |
| 1063 | { |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1064 | pum_array = balloon_array; |
| 1065 | pum_size = balloon_arraysize; |
| 1066 | pum_compute_size(); |
| 1067 | pum_scrollbar = 0; |
| 1068 | pum_height = balloon_arraysize; |
| 1069 | |
Bram Moolenaar | aef8c3d | 2018-03-03 18:59:16 +0100 | [diff] [blame] | 1070 | pum_position_at_mouse(BALLOON_MIN_WIDTH); |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1071 | pum_selected = -1; |
| 1072 | pum_first = 0; |
| 1073 | pum_redraw(); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Called when the mouse moved, may remove any displayed balloon. |
| 1079 | */ |
| 1080 | void |
| 1081 | ui_may_remove_balloon(void) |
| 1082 | { |
| 1083 | if (mouse_row != balloon_mouse_row || mouse_col != balloon_mouse_col) |
| 1084 | ui_remove_balloon(); |
| 1085 | } |
| 1086 | # endif |
Bram Moolenaar | a8f04aa | 2018-02-10 15:36:55 +0100 | [diff] [blame] | 1087 | |
Bram Moolenaar | aef8c3d | 2018-03-03 18:59:16 +0100 | [diff] [blame] | 1088 | # if defined(FEAT_TERM_POPUP_MENU) || defined(PROTO) |
| 1089 | /* |
| 1090 | * Select the pum entry at the mouse position. |
| 1091 | */ |
| 1092 | static void |
| 1093 | pum_select_mouse_pos(void) |
| 1094 | { |
| 1095 | int idx = mouse_row - pum_row; |
| 1096 | |
| 1097 | if (idx < 0 || idx >= pum_size) |
| 1098 | pum_selected = -1; |
| 1099 | else if (*pum_array[idx].pum_text != NUL) |
| 1100 | pum_selected = idx; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Execute the currently selected popup menu item. |
| 1105 | */ |
| 1106 | static void |
| 1107 | pum_execute_menu(vimmenu_T *menu) |
| 1108 | { |
| 1109 | vimmenu_T *mp; |
| 1110 | int idx = 0; |
| 1111 | exarg_T ea; |
| 1112 | |
| 1113 | for (mp = menu->children; mp != NULL; mp = mp->next) |
| 1114 | if (idx++ == pum_selected) |
| 1115 | { |
| 1116 | vim_memset(&ea, 0, sizeof(ea)); |
| 1117 | execute_menu(&ea, mp); |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Open the terminal version of the popup menu and don't return until it is |
| 1124 | * closed. |
| 1125 | */ |
| 1126 | void |
| 1127 | pum_show_popupmenu(vimmenu_T *menu) |
| 1128 | { |
| 1129 | vimmenu_T *mp; |
| 1130 | int idx = 0; |
| 1131 | pumitem_T *array; |
| 1132 | #ifdef FEAT_BEVAL_TERM |
| 1133 | int save_bevalterm = p_bevalterm; |
| 1134 | #endif |
| 1135 | |
| 1136 | pum_undisplay(); |
| 1137 | pum_size = 0; |
| 1138 | |
| 1139 | for (mp = menu->children; mp != NULL; mp = mp->next) |
| 1140 | ++pum_size; |
| 1141 | |
| 1142 | array = (pumitem_T *)alloc_clear((unsigned)sizeof(pumitem_T) * pum_size); |
| 1143 | if (array == NULL) |
| 1144 | return; |
| 1145 | |
| 1146 | for (mp = menu->children; mp != NULL; mp = mp->next) |
| 1147 | if (menu_is_separator(mp->dname)) |
| 1148 | array[idx++].pum_text = (char_u *)""; |
| 1149 | else |
| 1150 | array[idx++].pum_text = mp->dname; |
| 1151 | |
| 1152 | pum_array = array; |
| 1153 | pum_compute_size(); |
| 1154 | pum_scrollbar = 0; |
| 1155 | pum_height = pum_size; |
| 1156 | pum_position_at_mouse(20); |
| 1157 | |
| 1158 | pum_selected = -1; |
| 1159 | pum_first = 0; |
| 1160 | # ifdef FEAT_BEVAL_TERM |
| 1161 | p_bevalterm = TRUE; /* track mouse movement */ |
| 1162 | mch_setmouse(TRUE); |
| 1163 | # endif |
| 1164 | |
| 1165 | for (;;) |
| 1166 | { |
| 1167 | int c; |
| 1168 | |
| 1169 | pum_redraw(); |
| 1170 | setcursor(); |
| 1171 | out_flush(); |
| 1172 | |
| 1173 | c = vgetc(); |
| 1174 | if (c == ESC) |
| 1175 | break; |
| 1176 | else if (c == CAR || c == NL) |
| 1177 | { |
| 1178 | /* enter: select current item, if any, and close */ |
| 1179 | pum_execute_menu(menu); |
| 1180 | break; |
| 1181 | } |
| 1182 | else if (c == 'k' || c == K_UP || c == K_MOUSEUP) |
| 1183 | { |
| 1184 | /* cursor up: select previous item */ |
| 1185 | while (pum_selected > 0) |
| 1186 | { |
| 1187 | --pum_selected; |
| 1188 | if (*array[pum_selected].pum_text != NUL) |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
| 1192 | else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN) |
| 1193 | { |
| 1194 | /* cursor down: select next item */ |
| 1195 | while (pum_selected < pum_size - 1) |
| 1196 | { |
| 1197 | ++pum_selected; |
| 1198 | if (*array[pum_selected].pum_text != NUL) |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | else if (c == K_RIGHTMOUSE) |
| 1203 | { |
| 1204 | /* Right mouse down: reposition the menu. */ |
| 1205 | vungetc(c); |
| 1206 | break; |
| 1207 | } |
| 1208 | else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE) |
| 1209 | { |
| 1210 | /* mouse moved: selec item in the mouse row */ |
| 1211 | pum_select_mouse_pos(); |
| 1212 | } |
| 1213 | else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE) |
| 1214 | { |
| 1215 | /* left mouse click: select clicked item, if any, and close; |
| 1216 | * right mouse release: select clicked item, close if any */ |
| 1217 | pum_select_mouse_pos(); |
| 1218 | if (pum_selected >= 0) |
| 1219 | { |
| 1220 | pum_execute_menu(menu); |
| 1221 | break; |
| 1222 | } |
| 1223 | if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM) |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | vim_free(array); |
| 1229 | pum_undisplay(); |
| 1230 | # ifdef FEAT_BEVAL_TERM |
| 1231 | p_bevalterm = save_bevalterm; |
| 1232 | mch_setmouse(TRUE); |
| 1233 | # endif |
| 1234 | } |
| 1235 | # endif |
| 1236 | |
Bram Moolenaar | 4b77947 | 2005-10-04 09:12:31 +0000 | [diff] [blame] | 1237 | #endif |