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