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