Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read a list of people who contributed. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Implementation of popup windows. See ":help popup". |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 16 | #if defined(FEAT_TEXT_PROP) || defined(PROTO) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 17 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 18 | typedef struct { |
| 19 | char *pp_name; |
| 20 | poppos_T pp_val; |
| 21 | } poppos_entry_T; |
| 22 | |
| 23 | static poppos_entry_T poppos_entries[] = { |
| 24 | {"botleft", POPPOS_BOTLEFT}, |
| 25 | {"topleft", POPPOS_TOPLEFT}, |
| 26 | {"botright", POPPOS_BOTRIGHT}, |
| 27 | {"topright", POPPOS_TOPRIGHT}, |
| 28 | {"center", POPPOS_CENTER} |
| 29 | }; |
| 30 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 31 | /* |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 32 | * Get option value for "key", which is "line" or "col". |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 33 | * Handles "cursor+N" and "cursor-N". |
| 34 | */ |
| 35 | static int |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 36 | popup_options_one(dict_T *dict, char_u *key) |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 37 | { |
| 38 | dictitem_T *di; |
| 39 | char_u *val; |
| 40 | char_u *s; |
| 41 | char_u *endp; |
| 42 | int n = 0; |
| 43 | |
| 44 | di = dict_find(dict, key, -1); |
| 45 | if (di == NULL) |
| 46 | return 0; |
| 47 | |
| 48 | val = tv_get_string(&di->di_tv); |
| 49 | if (STRNCMP(val, "cursor", 6) != 0) |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 50 | return dict_get_number_check(dict, key); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 51 | |
| 52 | setcursor_mayforce(TRUE); |
| 53 | s = val + 6; |
| 54 | if (*s != NUL) |
| 55 | { |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 56 | endp = s; |
| 57 | if (*skipwhite(s) == '+' || *skipwhite(s) == '-') |
| 58 | n = strtol((char *)s, (char **)&endp, 10); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 59 | if (endp != NULL && *skipwhite(endp) != NUL) |
| 60 | { |
| 61 | semsg(_(e_invexpr2), val); |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (STRCMP(key, "line") == 0) |
| 67 | n = screen_screenrow() + 1 + n; |
| 68 | else // "col" |
| 69 | n = screen_screencol() + 1 + n; |
| 70 | |
| 71 | if (n < 1) |
| 72 | n = 1; |
| 73 | return n; |
| 74 | } |
| 75 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 76 | static void |
| 77 | get_pos_options(win_T *wp, dict_T *dict) |
| 78 | { |
| 79 | char_u *str; |
| 80 | int nr; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 81 | dictitem_T *di; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 82 | |
| 83 | nr = popup_options_one(dict, (char_u *)"line"); |
| 84 | if (nr > 0) |
| 85 | wp->w_wantline = nr; |
| 86 | nr = popup_options_one(dict, (char_u *)"col"); |
| 87 | if (nr > 0) |
| 88 | wp->w_wantcol = nr; |
| 89 | |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 90 | di = dict_find(dict, (char_u *)"fixed", -1); |
| 91 | if (di != NULL) |
| 92 | wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 93 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 94 | str = dict_get_string(dict, (char_u *)"pos", FALSE); |
| 95 | if (str != NULL) |
| 96 | { |
| 97 | for (nr = 0; |
| 98 | nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T)); |
| 99 | ++nr) |
| 100 | if (STRCMP(str, poppos_entries[nr].pp_name) == 0) |
| 101 | { |
| 102 | wp->w_popup_pos = poppos_entries[nr].pp_val; |
| 103 | nr = -1; |
| 104 | break; |
| 105 | } |
| 106 | if (nr != -1) |
| 107 | semsg(_(e_invarg2), str); |
| 108 | } |
| 109 | } |
| 110 | |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 111 | static void |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 112 | set_padding_border(dict_T *dict, int *array, char *name, int max_val) |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 113 | { |
| 114 | dictitem_T *di; |
| 115 | |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 116 | di = dict_find(dict, (char_u *)name, -1); |
| 117 | if (di != NULL) |
| 118 | { |
| 119 | if (di->di_tv.v_type != VAR_LIST) |
| 120 | emsg(_(e_listreq)); |
| 121 | else |
| 122 | { |
| 123 | list_T *list = di->di_tv.vval.v_list; |
| 124 | listitem_T *li; |
| 125 | int i; |
| 126 | int nr; |
| 127 | |
| 128 | for (i = 0; i < 4; ++i) |
| 129 | array[i] = 1; |
| 130 | if (list != NULL) |
| 131 | for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len; |
| 132 | ++i, li = li->li_next) |
| 133 | { |
| 134 | nr = (int)tv_get_number(&li->li_tv); |
| 135 | if (nr >= 0) |
| 136 | array[i] = nr > max_val ? max_val : nr; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 142 | /* |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 143 | * Used when popup options contain "moved": set default moved values. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 144 | */ |
| 145 | static void |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 146 | set_moved_values(win_T *wp) |
| 147 | { |
| 148 | wp->w_popup_curwin = curwin; |
| 149 | wp->w_popup_lnum = curwin->w_cursor.lnum; |
| 150 | wp->w_popup_mincol = curwin->w_cursor.col; |
| 151 | wp->w_popup_maxcol = curwin->w_cursor.col; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Used when popup options contain "moved" with "word" or "WORD". |
| 156 | */ |
| 157 | static void |
| 158 | set_moved_columns(win_T *wp, int flags) |
| 159 | { |
| 160 | char_u *ptr; |
| 161 | int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR); |
| 162 | |
| 163 | if (len > 0) |
| 164 | { |
| 165 | wp->w_popup_mincol = (int)(ptr - ml_get_curline()); |
| 166 | wp->w_popup_maxcol = wp->w_popup_mincol + len - 1; |
| 167 | } |
| 168 | } |
| 169 | |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 170 | /* |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 171 | * Used when popup options contain "mousemoved": set default moved values. |
| 172 | */ |
| 173 | static void |
| 174 | set_mousemoved_values(win_T *wp) |
| 175 | { |
| 176 | wp->w_popup_mouse_row = mouse_row; |
| 177 | wp->w_popup_mouse_mincol = mouse_col; |
| 178 | wp->w_popup_mouse_maxcol = mouse_col; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Used when popup options contain "moved" with "word" or "WORD". |
| 183 | */ |
| 184 | static void |
| 185 | set_mousemoved_columns(win_T *wp, int flags) |
| 186 | { |
Bram Moolenaar | b05caa7 | 2019-07-10 21:55:54 +0200 | [diff] [blame] | 187 | win_T *textwp; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 188 | char_u *text; |
| 189 | int col; |
Bram Moolenaar | b05caa7 | 2019-07-10 21:55:54 +0200 | [diff] [blame] | 190 | pos_T pos; |
| 191 | colnr_T mcol; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 192 | |
| 193 | if (find_word_under_cursor(mouse_row, mouse_col, TRUE, flags, |
Bram Moolenaar | b05caa7 | 2019-07-10 21:55:54 +0200 | [diff] [blame] | 194 | &textwp, &pos.lnum, &text, NULL, &col) == OK) |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 195 | { |
Bram Moolenaar | b05caa7 | 2019-07-10 21:55:54 +0200 | [diff] [blame] | 196 | // convert text column to mouse column |
| 197 | pos.col = col; |
| 198 | pos.coladd = 0; |
| 199 | getvcol(textwp, &pos, &mcol, NULL, NULL); |
| 200 | wp->w_popup_mouse_mincol = mcol; |
| 201 | |
Bram Moolenaar | 1072768 | 2019-07-12 13:59:20 +0200 | [diff] [blame] | 202 | pos.col = col + (colnr_T)STRLEN(text) - 1; |
Bram Moolenaar | b05caa7 | 2019-07-10 21:55:54 +0200 | [diff] [blame] | 203 | getvcol(textwp, &pos, NULL, NULL, &mcol); |
| 204 | wp->w_popup_mouse_maxcol = mcol; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 205 | vim_free(text); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 210 | * Return TRUE if "row"/"col" is on the border of the popup. |
| 211 | * The values are relative to the top-left corner. |
| 212 | */ |
| 213 | int |
| 214 | popup_on_border(win_T *wp, int row, int col) |
| 215 | { |
| 216 | return (row == 0 && wp->w_popup_border[0] > 0) |
| 217 | || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0) |
| 218 | || (col == 0 && wp->w_popup_border[3] > 0) |
| 219 | || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0); |
| 220 | } |
| 221 | |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 222 | /* |
| 223 | * Return TRUE if "row"/"col" is on the "X" button of the popup. |
| 224 | * The values are relative to the top-left corner. |
| 225 | * Caller should check w_popup_close is POPCLOSE_BUTTON. |
| 226 | */ |
| 227 | int |
| 228 | popup_on_X_button(win_T *wp, int row, int col) |
| 229 | { |
| 230 | return row == 0 && col == popup_width(wp) - 1; |
| 231 | } |
| 232 | |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 233 | // Values set when dragging a popup window starts. |
| 234 | static int drag_start_row; |
| 235 | static int drag_start_col; |
| 236 | static int drag_start_wantline; |
| 237 | static int drag_start_wantcol; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 238 | static int drag_on_resize_handle; |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Mouse down on border of popup window: start dragging it. |
| 242 | * Uses mouse_col and mouse_row. |
| 243 | */ |
| 244 | void |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 245 | popup_start_drag(win_T *wp, int row, int col) |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 246 | { |
| 247 | drag_start_row = mouse_row; |
| 248 | drag_start_col = mouse_col; |
| 249 | // TODO: handle using different corner |
| 250 | if (wp->w_wantline == 0) |
| 251 | drag_start_wantline = wp->w_winrow + 1; |
| 252 | else |
| 253 | drag_start_wantline = wp->w_wantline; |
| 254 | if (wp->w_wantcol == 0) |
| 255 | drag_start_wantcol = wp->w_wincol + 1; |
| 256 | else |
| 257 | drag_start_wantcol = wp->w_wantcol; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 258 | |
| 259 | // Stop centering the popup |
| 260 | if (wp->w_popup_pos == POPPOS_CENTER) |
| 261 | wp->w_popup_pos = POPPOS_TOPLEFT; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 262 | |
| 263 | drag_on_resize_handle = wp->w_popup_border[1] > 0 |
| 264 | && wp->w_popup_border[2] > 0 |
| 265 | && row == popup_height(wp) - 1 |
| 266 | && col == popup_width(wp) - 1; |
| 267 | |
| 268 | if (wp->w_popup_pos != POPPOS_TOPLEFT && drag_on_resize_handle) |
| 269 | { |
| 270 | if (wp->w_popup_pos == POPPOS_TOPRIGHT |
| 271 | || wp->w_popup_pos == POPPOS_BOTRIGHT) |
| 272 | wp->w_wantcol = wp->w_wincol + 1; |
| 273 | if (wp->w_popup_pos == POPPOS_BOTLEFT) |
| 274 | wp->w_wantline = wp->w_winrow + 1; |
| 275 | wp->w_popup_pos = POPPOS_TOPLEFT; |
| 276 | } |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 280 | * Mouse moved while dragging a popup window: adjust the window popup position |
| 281 | * or resize. |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 282 | */ |
| 283 | void |
| 284 | popup_drag(win_T *wp) |
| 285 | { |
| 286 | // The popup may be closed before dragging stops. |
| 287 | if (!win_valid_popup(wp)) |
| 288 | return; |
| 289 | |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 290 | if ((wp->w_popup_flags & POPF_RESIZE) && drag_on_resize_handle) |
| 291 | { |
| 292 | int width_inc = mouse_col - drag_start_col; |
| 293 | int height_inc = mouse_row - drag_start_row; |
| 294 | |
| 295 | if (width_inc != 0) |
| 296 | { |
| 297 | int width = wp->w_width + width_inc; |
| 298 | |
| 299 | if (width < 1) |
| 300 | width = 1; |
| 301 | wp->w_minwidth = width; |
| 302 | wp->w_maxwidth = width; |
| 303 | drag_start_col = mouse_col; |
| 304 | } |
| 305 | |
| 306 | if (height_inc != 0) |
| 307 | { |
| 308 | int height = wp->w_height + height_inc; |
| 309 | |
| 310 | if (height < 1) |
| 311 | height = 1; |
| 312 | wp->w_minheight = height; |
| 313 | wp->w_maxheight = height; |
| 314 | drag_start_row = mouse_row; |
| 315 | } |
| 316 | |
| 317 | popup_adjust_position(wp); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | if (!(wp->w_popup_flags & POPF_DRAG)) |
| 322 | return; |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 323 | wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row); |
| 324 | if (wp->w_wantline < 1) |
| 325 | wp->w_wantline = 1; |
| 326 | if (wp->w_wantline > Rows) |
| 327 | wp->w_wantline = Rows; |
| 328 | wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col); |
| 329 | if (wp->w_wantcol < 1) |
| 330 | wp->w_wantcol = 1; |
| 331 | if (wp->w_wantcol > Columns) |
| 332 | wp->w_wantcol = Columns; |
| 333 | |
| 334 | popup_adjust_position(wp); |
| 335 | } |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 336 | |
Bram Moolenaar | f9c85f5 | 2019-06-29 07:41:35 +0200 | [diff] [blame] | 337 | /* |
| 338 | * Set w_firstline to match the current "wp->w_topline". |
| 339 | */ |
| 340 | void |
| 341 | popup_set_firstline(win_T *wp) |
| 342 | { |
| 343 | int height = wp->w_height; |
| 344 | |
| 345 | wp->w_firstline = wp->w_topline; |
| 346 | popup_adjust_position(wp); |
| 347 | |
| 348 | // we don't want the popup to get smaller, decrement the first line |
| 349 | // until it doesn't |
| 350 | while (wp->w_firstline > 1 && wp->w_height < height) |
| 351 | { |
| 352 | --wp->w_firstline; |
| 353 | popup_adjust_position(wp); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* |
Bram Moolenaar | 13b11ed | 2019-08-01 15:52:45 +0200 | [diff] [blame] | 358 | * Return TRUE if the position is in the popup window scrollbar. |
| 359 | */ |
| 360 | int |
| 361 | popup_is_in_scrollbar(win_T *wp, int row, int col) |
| 362 | { |
| 363 | return wp->w_has_scrollbar |
| 364 | && row >= wp->w_popup_border[0] |
| 365 | && row < popup_height(wp) - wp->w_popup_border[2] |
| 366 | && col == popup_width(wp) - wp->w_popup_border[1] - 1; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /* |
Bram Moolenaar | f9c85f5 | 2019-06-29 07:41:35 +0200 | [diff] [blame] | 371 | * Handle a click in a popup window, if it is in the scrollbar. |
| 372 | */ |
| 373 | void |
| 374 | popup_handle_scrollbar_click(win_T *wp, int row, int col) |
| 375 | { |
| 376 | int height = popup_height(wp); |
| 377 | int old_topline = wp->w_topline; |
| 378 | |
Bram Moolenaar | 13b11ed | 2019-08-01 15:52:45 +0200 | [diff] [blame] | 379 | if (popup_is_in_scrollbar(wp, row, col)) |
Bram Moolenaar | f9c85f5 | 2019-06-29 07:41:35 +0200 | [diff] [blame] | 380 | { |
| 381 | if (row >= height / 2) |
| 382 | { |
| 383 | // Click in lower half, scroll down. |
| 384 | if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count) |
| 385 | ++wp->w_topline; |
| 386 | } |
| 387 | else if (wp->w_topline > 1) |
| 388 | // click on upper half, scroll up. |
| 389 | --wp->w_topline; |
| 390 | if (wp->w_topline != old_topline) |
| 391 | { |
| 392 | popup_set_firstline(wp); |
| 393 | redraw_win_later(wp, NOT_VALID); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 398 | #if defined(FEAT_TIMERS) |
| 399 | static void |
| 400 | popup_add_timeout(win_T *wp, int time) |
| 401 | { |
| 402 | char_u cbbuf[50]; |
| 403 | char_u *ptr = cbbuf; |
| 404 | typval_T tv; |
| 405 | |
| 406 | vim_snprintf((char *)cbbuf, sizeof(cbbuf), |
| 407 | "{_ -> popup_close(%d)}", wp->w_id); |
| 408 | if (get_lambda_tv(&ptr, &tv, TRUE) == OK) |
| 409 | { |
| 410 | wp->w_popup_timer = create_timer(time, 0); |
| 411 | wp->w_popup_timer->tr_callback = get_callback(&tv); |
| 412 | clear_tv(&tv); |
| 413 | } |
| 414 | } |
| 415 | #endif |
| 416 | |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 417 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 418 | * Shared between popup_create() and f_popup_move(). |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 419 | */ |
| 420 | static void |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 421 | apply_move_options(win_T *wp, dict_T *d) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 422 | { |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 423 | int nr; |
| 424 | |
| 425 | if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0) |
| 426 | wp->w_minwidth = nr; |
| 427 | if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0) |
| 428 | wp->w_minheight = nr; |
| 429 | if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0) |
| 430 | wp->w_maxwidth = nr; |
| 431 | if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0) |
| 432 | wp->w_maxheight = nr; |
| 433 | get_pos_options(wp, d); |
| 434 | } |
| 435 | |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 436 | static void |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 437 | handle_moved_argument(win_T *wp, dictitem_T *di, int mousemoved) |
| 438 | { |
| 439 | if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL) |
| 440 | { |
| 441 | char_u *s = di->di_tv.vval.v_string; |
| 442 | int flags = 0; |
| 443 | |
| 444 | if (STRCMP(s, "word") == 0) |
| 445 | flags = FIND_IDENT | FIND_STRING; |
| 446 | else if (STRCMP(s, "WORD") == 0) |
| 447 | flags = FIND_STRING; |
| 448 | else if (STRCMP(s, "expr") == 0) |
| 449 | flags = FIND_IDENT | FIND_STRING | FIND_EVAL; |
| 450 | else if (STRCMP(s, "any") != 0) |
| 451 | semsg(_(e_invarg2), s); |
| 452 | if (flags != 0) |
| 453 | { |
| 454 | if (mousemoved) |
| 455 | set_mousemoved_columns(wp, flags); |
| 456 | else |
| 457 | set_moved_columns(wp, flags); |
| 458 | } |
| 459 | } |
| 460 | else if (di->di_tv.v_type == VAR_LIST |
| 461 | && di->di_tv.vval.v_list != NULL |
| 462 | && di->di_tv.vval.v_list->lv_len == 2) |
| 463 | { |
| 464 | list_T *l = di->di_tv.vval.v_list; |
| 465 | int mincol = tv_get_number(&l->lv_first->li_tv); |
| 466 | int maxcol = tv_get_number(&l->lv_first->li_next->li_tv); |
| 467 | |
| 468 | if (mousemoved) |
| 469 | { |
| 470 | wp->w_popup_mouse_mincol = mincol; |
| 471 | wp->w_popup_mouse_maxcol = maxcol; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | wp->w_popup_mincol = mincol; |
| 476 | wp->w_popup_maxcol = maxcol; |
| 477 | } |
| 478 | } |
| 479 | else |
| 480 | semsg(_(e_invarg2), tv_get_string(&di->di_tv)); |
| 481 | } |
| 482 | |
| 483 | static void |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 484 | check_highlight(dict_T *dict, char *name, char_u **pval) |
| 485 | { |
| 486 | dictitem_T *di; |
| 487 | char_u *str; |
| 488 | |
| 489 | di = dict_find(dict, (char_u *)name, -1); |
| 490 | if (di != NULL) |
| 491 | { |
| 492 | if (di->di_tv.v_type != VAR_STRING) |
| 493 | semsg(_(e_invargval), name); |
| 494 | else |
| 495 | { |
| 496 | str = tv_get_string(&di->di_tv); |
| 497 | if (*str != NUL) |
| 498 | *pval = vim_strsave(str); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 503 | /* |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 504 | * Scroll to show the line with the cursor. This assumes lines don't wrap. |
| 505 | */ |
| 506 | static void |
| 507 | popup_show_curline(win_T *wp) |
| 508 | { |
| 509 | if (wp->w_cursor.lnum < wp->w_topline) |
| 510 | wp->w_topline = wp->w_cursor.lnum; |
| 511 | else if (wp->w_cursor.lnum >= wp->w_botline) |
| 512 | wp->w_topline = wp->w_cursor.lnum - wp->w_height + 1; |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 513 | |
| 514 | // Don't use "firstline" now. |
| 515 | wp->w_firstline = 0; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Get the sign group name for window "wp". |
| 520 | * Returns a pointer to a static buffer, overwritten on the next call. |
| 521 | */ |
| 522 | static char_u * |
| 523 | popup_get_sign_name(win_T *wp) |
| 524 | { |
| 525 | static char buf[30]; |
| 526 | |
| 527 | vim_snprintf(buf, sizeof(buf), "popup-%d", wp->w_id); |
| 528 | return (char_u *)buf; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 532 | * Highlight the line with the cursor. |
| 533 | * Also scrolls the text to put the cursor line in view. |
| 534 | */ |
| 535 | static void |
| 536 | popup_highlight_curline(win_T *wp) |
| 537 | { |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 538 | int sign_id = 0; |
| 539 | char_u *sign_name = popup_get_sign_name(wp); |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 540 | |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 541 | buf_delete_signs(wp->w_buffer, (char_u *)"popupmenu"); |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 542 | |
| 543 | if ((wp->w_popup_flags & POPF_CURSORLINE) != 0) |
| 544 | { |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 545 | popup_show_curline(wp); |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 546 | |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 547 | if (!sign_exists_by_name(sign_name)) |
| 548 | { |
| 549 | char *linehl = "PopupSelected"; |
| 550 | |
| 551 | if (syn_name2id((char_u *)linehl) == 0) |
| 552 | linehl = "PmenuSel"; |
| 553 | sign_define_by_name(sign_name, NULL, |
| 554 | (char_u *)linehl, NULL, NULL); |
| 555 | } |
| 556 | |
| 557 | sign_place(&sign_id, (char_u *)"popupmenu", sign_name, |
| 558 | wp->w_buffer, wp->w_cursor.lnum, SIGN_DEF_PRIO); |
| 559 | redraw_win_later(wp, NOT_VALID); |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 560 | } |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 561 | else |
| 562 | sign_undefine_by_name(sign_name, FALSE); |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 566 | * Shared between popup_create() and f_popup_setoptions(). |
| 567 | */ |
| 568 | static void |
| 569 | apply_general_options(win_T *wp, dict_T *dict) |
| 570 | { |
| 571 | dictitem_T *di; |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 572 | int nr; |
| 573 | char_u *str; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 574 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 575 | // TODO: flip |
| 576 | |
| 577 | di = dict_find(dict, (char_u *)"firstline", -1); |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 578 | if (di != NULL) |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 579 | wp->w_firstline = dict_get_number(dict, (char_u *)"firstline"); |
| 580 | if (wp->w_firstline < 1) |
| 581 | wp->w_firstline = 1; |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 582 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 583 | di = dict_find(dict, (char_u *)"scrollbar", -1); |
| 584 | if (di != NULL) |
| 585 | wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar"); |
| 586 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 587 | str = dict_get_string(dict, (char_u *)"title", FALSE); |
| 588 | if (str != NULL) |
| 589 | { |
| 590 | vim_free(wp->w_popup_title); |
| 591 | wp->w_popup_title = vim_strsave(str); |
| 592 | } |
| 593 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 594 | di = dict_find(dict, (char_u *)"wrap", -1); |
| 595 | if (di != NULL) |
| 596 | { |
| 597 | nr = dict_get_number(dict, (char_u *)"wrap"); |
| 598 | wp->w_p_wrap = nr != 0; |
| 599 | } |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 600 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 601 | di = dict_find(dict, (char_u *)"drag", -1); |
| 602 | if (di != NULL) |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 603 | { |
| 604 | nr = dict_get_number(dict, (char_u *)"drag"); |
| 605 | if (nr) |
| 606 | wp->w_popup_flags |= POPF_DRAG; |
| 607 | else |
| 608 | wp->w_popup_flags &= ~POPF_DRAG; |
| 609 | } |
| 610 | |
| 611 | di = dict_find(dict, (char_u *)"resize", -1); |
| 612 | if (di != NULL) |
| 613 | { |
| 614 | nr = dict_get_number(dict, (char_u *)"resize"); |
| 615 | if (nr) |
| 616 | wp->w_popup_flags |= POPF_RESIZE; |
| 617 | else |
| 618 | wp->w_popup_flags &= ~POPF_RESIZE; |
| 619 | } |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 620 | |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 621 | di = dict_find(dict, (char_u *)"close", -1); |
| 622 | if (di != NULL) |
| 623 | { |
| 624 | int ok = TRUE; |
| 625 | |
| 626 | if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL) |
| 627 | { |
| 628 | char_u *s = di->di_tv.vval.v_string; |
| 629 | |
| 630 | if (STRCMP(s, "none") == 0) |
| 631 | wp->w_popup_close = POPCLOSE_NONE; |
| 632 | else if (STRCMP(s, "button") == 0) |
| 633 | wp->w_popup_close = POPCLOSE_BUTTON; |
| 634 | else if (STRCMP(s, "click") == 0) |
| 635 | wp->w_popup_close = POPCLOSE_CLICK; |
| 636 | else |
| 637 | ok = FALSE; |
| 638 | } |
| 639 | else |
| 640 | ok = FALSE; |
| 641 | if (!ok) |
| 642 | semsg(_(e_invargNval), "close", tv_get_string(&di->di_tv)); |
| 643 | } |
| 644 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 645 | str = dict_get_string(dict, (char_u *)"highlight", FALSE); |
| 646 | if (str != NULL) |
| 647 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
| 648 | str, OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 649 | |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 650 | set_string_option_direct_in_win(wp, (char_u *)"signcolumn", -1, |
| 651 | (char_u *)"no", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 652 | set_padding_border(dict, wp->w_popup_padding, "padding", 999); |
| 653 | set_padding_border(dict, wp->w_popup_border, "border", 1); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 654 | |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 655 | di = dict_find(dict, (char_u *)"borderhighlight", -1); |
| 656 | if (di != NULL) |
| 657 | { |
Bram Moolenaar | 403d090 | 2019-07-17 21:37:32 +0200 | [diff] [blame] | 658 | if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL) |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 659 | emsg(_(e_listreq)); |
| 660 | else |
| 661 | { |
| 662 | list_T *list = di->di_tv.vval.v_list; |
| 663 | listitem_T *li; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 664 | int i; |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 665 | |
Bram Moolenaar | 403d090 | 2019-07-17 21:37:32 +0200 | [diff] [blame] | 666 | for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len; |
| 667 | ++i, li = li->li_next) |
| 668 | { |
| 669 | str = tv_get_string(&li->li_tv); |
| 670 | if (*str != NUL) |
| 671 | wp->w_border_highlight[i] = vim_strsave(str); |
| 672 | } |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 673 | if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL) |
| 674 | for (i = 1; i < 4; ++i) |
Bram Moolenaar | 403d090 | 2019-07-17 21:37:32 +0200 | [diff] [blame] | 675 | wp->w_border_highlight[i] = |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 676 | vim_strsave(wp->w_border_highlight[0]); |
| 677 | } |
| 678 | } |
| 679 | |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 680 | di = dict_find(dict, (char_u *)"borderchars", -1); |
| 681 | if (di != NULL) |
| 682 | { |
| 683 | if (di->di_tv.v_type != VAR_LIST) |
| 684 | emsg(_(e_listreq)); |
| 685 | else |
| 686 | { |
| 687 | list_T *list = di->di_tv.vval.v_list; |
| 688 | listitem_T *li; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 689 | int i; |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 690 | |
| 691 | if (list != NULL) |
| 692 | for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len; |
| 693 | ++i, li = li->li_next) |
| 694 | { |
| 695 | str = tv_get_string(&li->li_tv); |
| 696 | if (*str != NUL) |
| 697 | wp->w_border_char[i] = mb_ptr2char(str); |
| 698 | } |
| 699 | if (list->lv_len == 1) |
| 700 | for (i = 1; i < 8; ++i) |
| 701 | wp->w_border_char[i] = wp->w_border_char[0]; |
| 702 | if (list->lv_len == 2) |
| 703 | { |
| 704 | for (i = 4; i < 8; ++i) |
| 705 | wp->w_border_char[i] = wp->w_border_char[1]; |
| 706 | for (i = 1; i < 4; ++i) |
| 707 | wp->w_border_char[i] = wp->w_border_char[0]; |
| 708 | } |
| 709 | } |
| 710 | } |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 711 | |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 712 | check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight); |
| 713 | check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight); |
| 714 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 715 | di = dict_find(dict, (char_u *)"zindex", -1); |
| 716 | if (di != NULL) |
| 717 | { |
| 718 | wp->w_zindex = dict_get_number(dict, (char_u *)"zindex"); |
| 719 | if (wp->w_zindex < 1) |
| 720 | wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX; |
| 721 | if (wp->w_zindex > 32000) |
| 722 | wp->w_zindex = 32000; |
| 723 | } |
| 724 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 725 | di = dict_find(dict, (char_u *)"mask", -1); |
| 726 | if (di != NULL) |
| 727 | { |
Bram Moolenaar | cfdbc5a | 2019-07-17 21:27:52 +0200 | [diff] [blame] | 728 | int ok = FALSE; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 729 | |
Bram Moolenaar | cfdbc5a | 2019-07-17 21:27:52 +0200 | [diff] [blame] | 730 | if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL) |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 731 | { |
| 732 | listitem_T *li; |
| 733 | |
Bram Moolenaar | cfdbc5a | 2019-07-17 21:27:52 +0200 | [diff] [blame] | 734 | ok = TRUE; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 735 | for (li = di->di_tv.vval.v_list->lv_first; li != NULL; |
| 736 | li = li->li_next) |
| 737 | { |
| 738 | if (li->li_tv.v_type != VAR_LIST |
| 739 | || li->li_tv.vval.v_list == NULL |
| 740 | || li->li_tv.vval.v_list->lv_len != 4) |
| 741 | { |
| 742 | ok = FALSE; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | if (ok) |
| 748 | { |
| 749 | wp->w_popup_mask = di->di_tv.vval.v_list; |
| 750 | ++wp->w_popup_mask->lv_refcount; |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 751 | VIM_CLEAR(wp->w_popup_mask_cells); |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 752 | } |
| 753 | else |
| 754 | semsg(_(e_invargval), "mask"); |
| 755 | } |
| 756 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 757 | #if defined(FEAT_TIMERS) |
| 758 | // Add timer to close the popup after some time. |
| 759 | nr = dict_get_number(dict, (char_u *)"time"); |
| 760 | if (nr > 0) |
| 761 | popup_add_timeout(wp, nr); |
| 762 | #endif |
| 763 | |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 764 | di = dict_find(dict, (char_u *)"moved", -1); |
| 765 | if (di != NULL) |
| 766 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 767 | set_moved_values(wp); |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 768 | handle_moved_argument(wp, di, FALSE); |
| 769 | } |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 770 | |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 771 | di = dict_find(dict, (char_u *)"mousemoved", -1); |
| 772 | if (di != NULL) |
| 773 | { |
| 774 | set_mousemoved_values(wp); |
| 775 | handle_moved_argument(wp, di, TRUE); |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 776 | } |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 777 | |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 778 | di = dict_find(dict, (char_u *)"cursorline", -1); |
| 779 | if (di != NULL) |
| 780 | { |
| 781 | if (di->di_tv.v_type == VAR_NUMBER) |
| 782 | { |
| 783 | if (di->di_tv.vval.v_number != 0) |
| 784 | wp->w_popup_flags |= POPF_CURSORLINE; |
| 785 | else |
| 786 | wp->w_popup_flags &= ~POPF_CURSORLINE; |
| 787 | } |
| 788 | else |
| 789 | semsg(_(e_invargval), "cursorline"); |
| 790 | } |
| 791 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 792 | di = dict_find(dict, (char_u *)"filter", -1); |
| 793 | if (di != NULL) |
| 794 | { |
| 795 | callback_T callback = get_callback(&di->di_tv); |
| 796 | |
| 797 | if (callback.cb_name != NULL) |
| 798 | { |
| 799 | free_callback(&wp->w_filter_cb); |
| 800 | set_callback(&wp->w_filter_cb, &callback); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | di = dict_find(dict, (char_u *)"callback", -1); |
| 805 | if (di != NULL) |
| 806 | { |
| 807 | callback_T callback = get_callback(&di->di_tv); |
| 808 | |
| 809 | if (callback.cb_name != NULL) |
| 810 | { |
| 811 | free_callback(&wp->w_close_cb); |
| 812 | set_callback(&wp->w_close_cb, &callback); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Go through the options in "dict" and apply them to popup window "wp". |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 819 | * Only used when creating a new popup window. |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 820 | */ |
| 821 | static void |
| 822 | apply_options(win_T *wp, dict_T *dict) |
| 823 | { |
| 824 | int nr; |
| 825 | |
| 826 | apply_move_options(wp, dict); |
| 827 | apply_general_options(wp, dict); |
| 828 | |
Bram Moolenaar | 6313c4f | 2019-06-16 20:39:13 +0200 | [diff] [blame] | 829 | nr = dict_get_number(dict, (char_u *)"hidden"); |
| 830 | if (nr > 0) |
| 831 | { |
| 832 | wp->w_popup_flags |= POPF_HIDDEN; |
| 833 | --wp->w_buffer->b_nwindows; |
| 834 | } |
| 835 | |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 836 | popup_mask_refresh = TRUE; |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 837 | popup_highlight_curline(wp); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 841 | * Add lines to the popup from a list of strings. |
| 842 | */ |
| 843 | static void |
| 844 | add_popup_strings(buf_T *buf, list_T *l) |
| 845 | { |
| 846 | listitem_T *li; |
| 847 | linenr_T lnum = 0; |
| 848 | char_u *p; |
| 849 | |
| 850 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 851 | if (li->li_tv.v_type == VAR_STRING) |
| 852 | { |
| 853 | p = li->li_tv.vval.v_string; |
| 854 | ml_append_buf(buf, lnum++, |
| 855 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Add lines to the popup from a list of dictionaries. |
| 861 | */ |
| 862 | static void |
| 863 | add_popup_dicts(buf_T *buf, list_T *l) |
| 864 | { |
| 865 | listitem_T *li; |
| 866 | listitem_T *pli; |
| 867 | linenr_T lnum = 0; |
| 868 | char_u *p; |
| 869 | dict_T *dict; |
| 870 | |
| 871 | // first add the text lines |
| 872 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 873 | { |
| 874 | if (li->li_tv.v_type != VAR_DICT) |
| 875 | { |
| 876 | emsg(_(e_dictreq)); |
| 877 | return; |
| 878 | } |
| 879 | dict = li->li_tv.vval.v_dict; |
| 880 | p = dict == NULL ? NULL |
| 881 | : dict_get_string(dict, (char_u *)"text", FALSE); |
| 882 | ml_append_buf(buf, lnum++, |
| 883 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 884 | } |
| 885 | |
| 886 | // add the text properties |
| 887 | lnum = 1; |
| 888 | for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum) |
| 889 | { |
| 890 | dictitem_T *di; |
| 891 | list_T *plist; |
| 892 | |
| 893 | dict = li->li_tv.vval.v_dict; |
| 894 | di = dict_find(dict, (char_u *)"props", -1); |
| 895 | if (di != NULL) |
| 896 | { |
| 897 | if (di->di_tv.v_type != VAR_LIST) |
| 898 | { |
| 899 | emsg(_(e_listreq)); |
| 900 | return; |
| 901 | } |
| 902 | plist = di->di_tv.vval.v_list; |
| 903 | if (plist != NULL) |
| 904 | { |
| 905 | for (pli = plist->lv_first; pli != NULL; pli = pli->li_next) |
| 906 | { |
| 907 | if (pli->li_tv.v_type != VAR_DICT) |
| 908 | { |
| 909 | emsg(_(e_dictreq)); |
| 910 | return; |
| 911 | } |
| 912 | dict = pli->li_tv.vval.v_dict; |
| 913 | if (dict != NULL) |
| 914 | { |
| 915 | int col = dict_get_number(dict, (char_u *)"col"); |
| 916 | |
| 917 | prop_add_common( lnum, col, dict, buf, NULL); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | /* |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 926 | * Get the padding plus border at the top, adjusted to 1 if there is a title. |
| 927 | */ |
| 928 | static int |
| 929 | popup_top_extra(win_T *wp) |
| 930 | { |
| 931 | int extra = wp->w_popup_border[0] + wp->w_popup_padding[0]; |
| 932 | |
| 933 | if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL) |
| 934 | return 1; |
| 935 | return extra; |
| 936 | } |
| 937 | |
| 938 | /* |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 939 | * Return the height of popup window "wp", including border and padding. |
| 940 | */ |
| 941 | int |
| 942 | popup_height(win_T *wp) |
| 943 | { |
| 944 | return wp->w_height |
| 945 | + popup_top_extra(wp) |
| 946 | + wp->w_popup_padding[2] + wp->w_popup_border[2]; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Return the width of popup window "wp", including border, padding and |
| 951 | * scrollbar. |
| 952 | */ |
| 953 | int |
| 954 | popup_width(win_T *wp) |
| 955 | { |
Bram Moolenaar | 017c269 | 2019-07-13 14:17:51 +0200 | [diff] [blame] | 956 | // w_leftcol is how many columns of the core are left of the screen |
| 957 | // w_popup_rightoff is how many columns of the core are right of the screen |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 958 | return wp->w_width + wp->w_leftcol |
| 959 | + wp->w_popup_padding[3] + wp->w_popup_border[3] |
| 960 | + wp->w_popup_padding[1] + wp->w_popup_border[1] |
| 961 | + wp->w_has_scrollbar |
| 962 | + wp->w_popup_rightoff; |
| 963 | } |
| 964 | |
| 965 | /* |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 966 | * Adjust the position and size of the popup to fit on the screen. |
| 967 | */ |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 968 | void |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 969 | popup_adjust_position(win_T *wp) |
| 970 | { |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 971 | linenr_T lnum; |
| 972 | int wrapped = 0; |
| 973 | int maxwidth; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 974 | int maxspace; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 975 | int center_vert = FALSE; |
| 976 | int center_hor = FALSE; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 977 | int allow_adjust_left = !wp->w_popup_fixed; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 978 | int top_extra = popup_top_extra(wp); |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 979 | int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1]; |
| 980 | int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2]; |
| 981 | int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3]; |
| 982 | int extra_height = top_extra + bot_extra; |
| 983 | int extra_width = left_extra + right_extra; |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 984 | int org_winrow = wp->w_winrow; |
| 985 | int org_wincol = wp->w_wincol; |
| 986 | int org_width = wp->w_width; |
| 987 | int org_height = wp->w_height; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 988 | int org_leftcol = wp->w_leftcol; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 989 | int org_leftoff = wp->w_popup_leftoff; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 990 | int minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 991 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 992 | wp->w_winrow = 0; |
| 993 | wp->w_wincol = 0; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 994 | wp->w_leftcol = 0; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 995 | wp->w_popup_leftoff = 0; |
| 996 | wp->w_popup_rightoff = 0; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 997 | if (wp->w_popup_pos == POPPOS_CENTER) |
| 998 | { |
| 999 | // center after computing the size |
| 1000 | center_vert = TRUE; |
| 1001 | center_hor = TRUE; |
| 1002 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1003 | else |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1004 | { |
| 1005 | if (wp->w_wantline == 0) |
| 1006 | center_vert = TRUE; |
| 1007 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 1008 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 1009 | { |
| 1010 | wp->w_winrow = wp->w_wantline - 1; |
| 1011 | if (wp->w_winrow >= Rows) |
| 1012 | wp->w_winrow = Rows - 1; |
| 1013 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1014 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1015 | if (wp->w_wantcol == 0) |
| 1016 | center_hor = TRUE; |
| 1017 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 1018 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 1019 | { |
| 1020 | wp->w_wincol = wp->w_wantcol - 1; |
| 1021 | if (wp->w_wincol >= Columns - 3) |
| 1022 | wp->w_wincol = Columns - 3; |
| 1023 | } |
| 1024 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1025 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1026 | // When centering or right aligned, use maximum width. |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1027 | // When left aligned use the space available, but shift to the left when we |
| 1028 | // hit the right of the screen. |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1029 | maxspace = Columns - wp->w_wincol - left_extra; |
| 1030 | maxwidth = maxspace; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1031 | if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth) |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1032 | { |
| 1033 | allow_adjust_left = FALSE; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1034 | maxwidth = wp->w_maxwidth; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1035 | } |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1036 | |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1037 | // start at the desired first line |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1038 | if (wp->w_firstline != 0) |
| 1039 | wp->w_topline = wp->w_firstline; |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1040 | if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count) |
| 1041 | wp->w_topline = wp->w_buffer->b_ml.ml_line_count; |
| 1042 | |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1043 | // Compute width based on longest text line and the 'wrap' option. |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1044 | // Use a minimum width of one, so that something shows when there is no |
| 1045 | // text. |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1046 | // TODO: more accurate wrapping |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1047 | wp->w_width = 1; |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1048 | for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1049 | { |
Bram Moolenaar | 331bafd | 2019-07-20 17:46:05 +0200 | [diff] [blame] | 1050 | int len; |
| 1051 | int w_width = wp->w_width; |
| 1052 | |
| 1053 | // Count Tabs for what they are worth and compute the length based on |
| 1054 | // the maximum width (matters when 'showbreak' is set). |
| 1055 | if (wp->w_width < maxwidth) |
| 1056 | wp->w_width = maxwidth; |
| 1057 | len = win_linetabsize(wp, ml_get_buf(wp->w_buffer, lnum, FALSE), |
Bram Moolenaar | e089c3f | 2019-07-09 20:25:25 +0200 | [diff] [blame] | 1058 | (colnr_T)MAXCOL); |
Bram Moolenaar | 331bafd | 2019-07-20 17:46:05 +0200 | [diff] [blame] | 1059 | wp->w_width = w_width; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1060 | |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1061 | if (wp->w_p_wrap) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1062 | { |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1063 | while (len > maxwidth) |
| 1064 | { |
| 1065 | ++wrapped; |
| 1066 | len -= maxwidth; |
| 1067 | wp->w_width = maxwidth; |
| 1068 | } |
| 1069 | } |
| 1070 | else if (len > maxwidth |
| 1071 | && allow_adjust_left |
| 1072 | && (wp->w_popup_pos == POPPOS_TOPLEFT |
| 1073 | || wp->w_popup_pos == POPPOS_BOTLEFT)) |
| 1074 | { |
| 1075 | // adjust leftwise to fit text on screen |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 1076 | int shift_by = len - maxwidth; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1077 | |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 1078 | if (shift_by > wp->w_wincol) |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1079 | { |
| 1080 | int truncate_shift = shift_by - wp->w_wincol; |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 1081 | |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1082 | len -= truncate_shift; |
| 1083 | shift_by -= truncate_shift; |
| 1084 | } |
| 1085 | |
| 1086 | wp->w_wincol -= shift_by; |
| 1087 | maxwidth += shift_by; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1088 | wp->w_width = maxwidth; |
| 1089 | } |
| 1090 | if (wp->w_width < len) |
Bram Moolenaar | 017c269 | 2019-07-13 14:17:51 +0200 | [diff] [blame] | 1091 | { |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1092 | wp->w_width = len; |
Bram Moolenaar | 017c269 | 2019-07-13 14:17:51 +0200 | [diff] [blame] | 1093 | if (wp->w_maxwidth > 0 && wp->w_width > wp->w_maxwidth) |
| 1094 | wp->w_width = wp->w_maxwidth; |
| 1095 | } |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1096 | // do not use the width of lines we're not going to show |
Bram Moolenaar | e296e31 | 2019-07-03 23:20:18 +0200 | [diff] [blame] | 1097 | if (wp->w_maxheight > 0 |
| 1098 | && lnum - wp->w_topline + 1 + wrapped > wp->w_maxheight) |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1099 | break; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1102 | wp->w_has_scrollbar = wp->w_want_scrollbar |
| 1103 | && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count); |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1104 | if (wp->w_has_scrollbar) |
| 1105 | ++right_extra; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1106 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 1107 | minwidth = wp->w_minwidth; |
| 1108 | if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL) |
| 1109 | { |
| 1110 | int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width; |
| 1111 | |
| 1112 | if (minwidth < title_len) |
| 1113 | minwidth = title_len; |
| 1114 | } |
| 1115 | |
| 1116 | if (minwidth > 0 && wp->w_width < minwidth) |
| 1117 | wp->w_width = minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1118 | if (wp->w_width > maxwidth) |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1119 | { |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1120 | if (wp->w_width > maxspace && !wp->w_p_wrap) |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1121 | // some columns cut off on the right |
| 1122 | wp->w_popup_rightoff = wp->w_width - maxspace; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 1123 | wp->w_width = maxwidth; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1124 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1125 | if (center_hor) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1126 | { |
| 1127 | wp->w_wincol = (Columns - wp->w_width - extra_width) / 2; |
| 1128 | if (wp->w_wincol < 0) |
| 1129 | wp->w_wincol = 0; |
| 1130 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1131 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 1132 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 1133 | { |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1134 | int leftoff = wp->w_wantcol - (wp->w_width + extra_width); |
| 1135 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1136 | // Right aligned: move to the right if needed. |
| 1137 | // No truncation, because that would change the height. |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1138 | if (leftoff >= 0) |
| 1139 | wp->w_wincol = leftoff; |
| 1140 | else if (wp->w_popup_fixed) |
| 1141 | { |
| 1142 | // "col" specifies the right edge, but popup doesn't fit, skip some |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1143 | // columns when displaying the window, minus left border and |
| 1144 | // padding. |
| 1145 | if (-leftoff > left_extra) |
| 1146 | wp->w_leftcol = -leftoff - left_extra; |
| 1147 | wp->w_width -= wp->w_leftcol; |
| 1148 | wp->w_popup_leftoff = -leftoff; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1149 | if (wp->w_width < 0) |
| 1150 | wp->w_width = 0; |
| 1151 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1152 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1153 | |
Bram Moolenaar | 8edf0e3 | 2019-07-30 21:19:26 +0200 | [diff] [blame] | 1154 | if (wp->w_p_wrap || (!wp->w_popup_fixed |
| 1155 | && (wp->w_popup_pos == POPPOS_TOPLEFT |
| 1156 | || wp->w_popup_pos == POPPOS_BOTLEFT))) |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1157 | { |
| 1158 | int want_col = 0; |
| 1159 | |
Bram Moolenaar | 8c8b88d | 2019-07-30 20:32:41 +0200 | [diff] [blame] | 1160 | // try to show the right border and any scrollbar |
| 1161 | want_col = left_extra + wp->w_width + right_extra; |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1162 | if (want_col > 0 && wp->w_wincol > 0 |
| 1163 | && wp->w_wincol + want_col >= Columns) |
| 1164 | { |
| 1165 | wp->w_wincol = Columns - want_col; |
| 1166 | if (wp->w_wincol < 0) |
| 1167 | wp->w_wincol = 0; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1171 | wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline |
| 1172 | + 1 + wrapped; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1173 | if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight) |
| 1174 | wp->w_height = wp->w_minheight; |
| 1175 | if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight) |
| 1176 | wp->w_height = wp->w_maxheight; |
| 1177 | if (wp->w_height > Rows - wp->w_winrow) |
| 1178 | wp->w_height = Rows - wp->w_winrow; |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 1179 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1180 | if (center_vert) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1181 | { |
| 1182 | wp->w_winrow = (Rows - wp->w_height - extra_height) / 2; |
| 1183 | if (wp->w_winrow < 0) |
| 1184 | wp->w_winrow = 0; |
| 1185 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1186 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 1187 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 1188 | { |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 1189 | if ((wp->w_height + extra_height) <= wp->w_wantline) |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1190 | // bottom aligned: may move down |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 1191 | wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1192 | else |
| 1193 | // not enough space, make top aligned |
| 1194 | wp->w_winrow = wp->w_wantline + 1; |
| 1195 | } |
| 1196 | |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 1197 | wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1198 | |
| 1199 | // Need to update popup_mask if the position or size changed. |
Bram Moolenaar | acc682b | 2019-06-08 17:15:51 +0200 | [diff] [blame] | 1200 | // And redraw windows that were behind the popup. |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1201 | if (org_winrow != wp->w_winrow |
| 1202 | || org_wincol != wp->w_wincol |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1203 | || org_leftcol != wp->w_leftcol |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 1204 | || org_leftoff != wp->w_popup_leftoff |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1205 | || org_width != wp->w_width |
| 1206 | || org_height != wp->w_height) |
| 1207 | { |
Bram Moolenaar | 4c063a0 | 2019-06-10 21:24:12 +0200 | [diff] [blame] | 1208 | redraw_all_later(VALID); |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1209 | redraw_win_later(wp, NOT_VALID); |
| 1210 | if (wp->w_popup_flags & POPF_ON_CMDLINE) |
| 1211 | clear_cmdline = TRUE; |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1212 | popup_mask_refresh = TRUE; |
| 1213 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1216 | typedef enum |
| 1217 | { |
| 1218 | TYPE_NORMAL, |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1219 | TYPE_ATCURSOR, |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1220 | TYPE_BEVAL, |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1221 | TYPE_NOTIFICATION, |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1222 | TYPE_DIALOG, |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1223 | TYPE_MENU, |
| 1224 | TYPE_PREVIEW |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1225 | } create_type_T; |
| 1226 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1227 | /* |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1228 | * Make "buf" empty and set the contents to "text". |
| 1229 | * Used by popup_create() and popup_settext(). |
| 1230 | */ |
| 1231 | static void |
| 1232 | popup_set_buffer_text(buf_T *buf, typval_T text) |
| 1233 | { |
| 1234 | int lnum; |
| 1235 | |
| 1236 | // Clear the buffer, then replace the lines. |
| 1237 | curbuf = buf; |
| 1238 | for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 1239 | ml_delete(lnum, FALSE); |
| 1240 | curbuf = curwin->w_buffer; |
| 1241 | |
| 1242 | // Add text to the buffer. |
| 1243 | if (text.v_type == VAR_STRING) |
| 1244 | { |
| 1245 | // just a string |
| 1246 | ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE); |
| 1247 | } |
| 1248 | else |
| 1249 | { |
| 1250 | list_T *l = text.vval.v_list; |
| 1251 | |
| 1252 | if (l->lv_len > 0) |
| 1253 | { |
| 1254 | if (l->lv_first->li_tv.v_type == VAR_STRING) |
| 1255 | // list of strings |
| 1256 | add_popup_strings(buf, l); |
| 1257 | else |
| 1258 | // list of dictionaries |
| 1259 | add_popup_dicts(buf, l); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | // delete the line that was in the empty buffer |
| 1264 | curbuf = buf; |
| 1265 | ml_delete(buf->b_ml.ml_line_count, FALSE); |
| 1266 | curbuf = curwin->w_buffer; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1270 | * Parse the 'previewpopup' option and apply the values to window "wp" if it |
| 1271 | * not NULL. |
| 1272 | * Return FAIL if the parsing fails. |
| 1273 | */ |
| 1274 | int |
| 1275 | parse_previewpopup(win_T *wp) |
| 1276 | { |
| 1277 | char_u *p; |
| 1278 | |
| 1279 | for (p = p_pvp; *p != NUL; p += (*p == ',' ? 1 : 0)) |
| 1280 | { |
| 1281 | char_u *e, *dig; |
| 1282 | char_u *s = p; |
| 1283 | int x; |
| 1284 | |
| 1285 | e = vim_strchr(p, ':'); |
| 1286 | if (e == NULL || e[1] == NUL) |
| 1287 | return FAIL; |
| 1288 | |
| 1289 | p = vim_strchr(e, ','); |
| 1290 | if (p == NULL) |
| 1291 | p = e + STRLEN(e); |
| 1292 | dig = e + 1; |
| 1293 | x = getdigits(&dig); |
| 1294 | if (dig != p) |
| 1295 | return FAIL; |
| 1296 | |
| 1297 | if (STRNCMP(s, "height:", 7) == 0) |
| 1298 | { |
| 1299 | if (wp != NULL) |
| 1300 | { |
| 1301 | wp->w_minheight = x; |
| 1302 | wp->w_maxheight = x; |
| 1303 | } |
| 1304 | } |
| 1305 | else if (STRNCMP(s, "width:", 6) == 0) |
| 1306 | { |
| 1307 | if (wp != NULL) |
| 1308 | { |
| 1309 | wp->w_minwidth = x; |
| 1310 | wp->w_maxwidth = x; |
| 1311 | } |
| 1312 | } |
| 1313 | else |
| 1314 | return FAIL; |
| 1315 | } |
| 1316 | return OK; |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * Set w_wantline and w_wantcol for the cursor position in the current window. |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1321 | * Keep at least "width" columns from the right of the screen. |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1322 | */ |
| 1323 | void |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1324 | popup_set_wantpos(win_T *wp, int width) |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1325 | { |
| 1326 | setcursor_mayforce(TRUE); |
| 1327 | wp->w_wantline = curwin->w_winrow + curwin->w_wrow; |
| 1328 | if (wp->w_wantline == 0) // cursor in first line |
| 1329 | { |
| 1330 | wp->w_wantline = 2; |
| 1331 | wp->w_popup_pos = POPPOS_TOPLEFT; |
| 1332 | } |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1333 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1334 | wp->w_wantcol = curwin->w_wincol + curwin->w_wcol + 1; |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1335 | if (wp->w_wantcol > Columns - width) |
| 1336 | { |
| 1337 | wp->w_wantcol = Columns - width; |
| 1338 | if (wp->w_wantcol < 1) |
| 1339 | wp->w_wantcol = 1; |
| 1340 | } |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1341 | popup_adjust_position(wp); |
| 1342 | } |
| 1343 | |
| 1344 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1345 | * popup_create({text}, {options}) |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1346 | * popup_atcursor({text}, {options}) |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1347 | * etc. |
| 1348 | * When creating a preview window popup "argvars" and "rettv" are NULL. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1349 | */ |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1350 | static win_T * |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1351 | popup_create(typval_T *argvars, typval_T *rettv, create_type_T type) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1352 | { |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1353 | win_T *wp; |
| 1354 | tabpage_T *tp = NULL; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1355 | int tabnr = 0; |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1356 | int new_buffer; |
| 1357 | buf_T *buf = NULL; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1358 | dict_T *d = NULL; |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1359 | int nr; |
| 1360 | int i; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1361 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1362 | if (argvars != NULL) |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1363 | { |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 1364 | // Check that arguments look OK. |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1365 | if (argvars[0].v_type == VAR_NUMBER) |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1366 | { |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1367 | buf = buflist_findnr( argvars[0].vval.v_number); |
| 1368 | if (buf == NULL) |
| 1369 | { |
| 1370 | semsg(_(e_nobufnr), argvars[0].vval.v_number); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | } |
| 1374 | else if (!(argvars[0].v_type == VAR_STRING |
| 1375 | && argvars[0].vval.v_string != NULL) |
| 1376 | && !(argvars[0].v_type == VAR_LIST |
| 1377 | && argvars[0].vval.v_list != NULL)) |
| 1378 | { |
| 1379 | emsg(_(e_listreq)); |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1380 | return NULL; |
| 1381 | } |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1382 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1383 | { |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1384 | emsg(_(e_dictreq)); |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1385 | return NULL; |
| 1386 | } |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1387 | d = argvars[1].vval.v_dict; |
| 1388 | } |
| 1389 | |
| 1390 | if (d != NULL) |
| 1391 | { |
| 1392 | if (dict_find(d, (char_u *)"tabpage", -1) != NULL) |
| 1393 | tabnr = (int)dict_get_number(d, (char_u *)"tabpage"); |
| 1394 | else if (type == TYPE_NOTIFICATION) |
| 1395 | tabnr = -1; // notifications are global by default |
| 1396 | else |
| 1397 | tabnr = 0; |
| 1398 | if (tabnr > 0) |
| 1399 | { |
| 1400 | tp = find_tabpage(tabnr); |
| 1401 | if (tp == NULL) |
| 1402 | { |
| 1403 | semsg(_("E997: Tabpage not found: %d"), tabnr); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | } |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1409 | // Create the window and buffer. |
| 1410 | wp = win_alloc_popup_win(); |
| 1411 | if (wp == NULL) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1412 | return NULL; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1413 | if (rettv != NULL) |
| 1414 | rettv->vval.v_number = wp->w_id; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1415 | wp->w_popup_pos = POPPOS_TOPLEFT; |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1416 | wp->w_popup_flags = POPF_IS_POPUP; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1417 | |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1418 | if (buf != NULL) |
| 1419 | { |
| 1420 | // use existing buffer |
| 1421 | new_buffer = FALSE; |
Bram Moolenaar | 7866b87 | 2019-07-01 22:21:01 +0200 | [diff] [blame] | 1422 | win_init_popup_win(wp, buf); |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1423 | buffer_ensure_loaded(buf); |
| 1424 | } |
| 1425 | else |
| 1426 | { |
| 1427 | // create a new buffer associated with the popup |
| 1428 | new_buffer = TRUE; |
| 1429 | buf = buflist_new(NULL, NULL, (linenr_T)0, |
| 1430 | BLN_NEW|BLN_LISTED|BLN_DUMMY); |
| 1431 | if (buf == NULL) |
| 1432 | return NULL; |
| 1433 | ml_open(buf); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 1434 | |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1435 | win_init_popup_win(wp, buf); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 1436 | |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1437 | set_local_options_default(wp); |
| 1438 | set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1439 | (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1440 | set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1, |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1441 | (char_u *)"wipe", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1442 | buf->b_p_ul = -1; // no undo |
| 1443 | buf->b_p_swf = FALSE; // no swap file |
| 1444 | buf->b_p_bl = FALSE; // unlisted buffer |
| 1445 | buf->b_locked = TRUE; |
| 1446 | wp->w_p_wrap = TRUE; // 'wrap' is default on |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1447 | |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1448 | // Avoid that 'buftype' is reset when this buffer is entered. |
| 1449 | buf->b_p_initialized = TRUE; |
| 1450 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1451 | |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1452 | if (tp != NULL) |
| 1453 | { |
| 1454 | // popup on specified tab page |
| 1455 | wp->w_next = tp->tp_first_popupwin; |
| 1456 | tp->tp_first_popupwin = wp; |
| 1457 | } |
| 1458 | else if (tabnr == 0) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1459 | { |
Bram Moolenaar | fc06cbb | 2019-06-15 14:14:31 +0200 | [diff] [blame] | 1460 | // popup on current tab page |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 1461 | wp->w_next = curtab->tp_first_popupwin; |
| 1462 | curtab->tp_first_popupwin = wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1463 | } |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1464 | else // (tabnr < 0) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1465 | { |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1466 | win_T *prev = first_popupwin; |
| 1467 | |
| 1468 | // Global popup: add at the end, so that it gets displayed on top of |
| 1469 | // older ones with the same zindex. Matters for notifications. |
| 1470 | if (first_popupwin == NULL) |
| 1471 | first_popupwin = wp; |
| 1472 | else |
| 1473 | { |
| 1474 | while (prev->w_next != NULL) |
| 1475 | prev = prev->w_next; |
| 1476 | prev->w_next = wp; |
| 1477 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1478 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1479 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1480 | if (new_buffer && argvars != NULL) |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1481 | popup_set_buffer_text(buf, argvars[0]); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1482 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1483 | if (type == TYPE_ATCURSOR || type == TYPE_PREVIEW) |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1484 | { |
| 1485 | wp->w_popup_pos = POPPOS_BOTLEFT; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1486 | } |
| 1487 | if (type == TYPE_ATCURSOR) |
| 1488 | { |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1489 | popup_set_wantpos(wp, 0); |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1490 | set_moved_values(wp); |
| 1491 | set_moved_columns(wp, FIND_STRING); |
| 1492 | } |
| 1493 | |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1494 | if (type == TYPE_BEVAL) |
| 1495 | { |
| 1496 | wp->w_popup_pos = POPPOS_BOTLEFT; |
| 1497 | |
| 1498 | // by default use the mouse position |
| 1499 | wp->w_wantline = mouse_row; |
| 1500 | if (wp->w_wantline <= 0) // mouse on first line |
| 1501 | { |
| 1502 | wp->w_wantline = 2; |
| 1503 | wp->w_popup_pos = POPPOS_TOPLEFT; |
| 1504 | } |
| 1505 | wp->w_wantcol = mouse_col + 1; |
| 1506 | set_mousemoved_values(wp); |
| 1507 | set_mousemoved_columns(wp, FIND_IDENT + FIND_STRING + FIND_EVAL); |
| 1508 | } |
| 1509 | |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1510 | // set default values |
| 1511 | wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX; |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 1512 | wp->w_popup_close = POPCLOSE_NONE; |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1513 | |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1514 | if (type == TYPE_NOTIFICATION) |
| 1515 | { |
| 1516 | win_T *twp, *nextwin; |
| 1517 | int height = buf->b_ml.ml_line_count + 3; |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1518 | |
| 1519 | // Try to not overlap with another global popup. Guess we need 3 |
| 1520 | // more screen lines than buffer lines. |
| 1521 | wp->w_wantline = 1; |
| 1522 | for (twp = first_popupwin; twp != NULL; twp = nextwin) |
| 1523 | { |
| 1524 | nextwin = twp->w_next; |
| 1525 | if (twp != wp |
| 1526 | && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX |
| 1527 | && twp->w_winrow <= wp->w_wantline - 1 + height |
| 1528 | && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1) |
| 1529 | { |
| 1530 | // move to below this popup and restart the loop to check for |
| 1531 | // overlap with other popups |
| 1532 | wp->w_wantline = twp->w_winrow + popup_height(twp) + 1; |
| 1533 | nextwin = first_popupwin; |
| 1534 | } |
| 1535 | } |
| 1536 | if (wp->w_wantline + height > Rows) |
| 1537 | { |
| 1538 | // can't avoid overlap, put on top in the hope that message goes |
| 1539 | // away soon. |
| 1540 | wp->w_wantline = 1; |
| 1541 | } |
| 1542 | |
| 1543 | wp->w_wantcol = 10; |
| 1544 | wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX; |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1545 | wp->w_minwidth = 20; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 1546 | wp->w_popup_flags |= POPF_DRAG; |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 1547 | wp->w_popup_close = POPCLOSE_CLICK; |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1548 | for (i = 0; i < 4; ++i) |
| 1549 | wp->w_popup_border[i] = 1; |
| 1550 | wp->w_popup_padding[1] = 1; |
| 1551 | wp->w_popup_padding[3] = 1; |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1552 | |
| 1553 | nr = syn_name2id((char_u *)"PopupNotification"); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1554 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1555 | (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"), |
| 1556 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1557 | } |
| 1558 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1559 | if (type == TYPE_DIALOG || type == TYPE_MENU) |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1560 | { |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1561 | wp->w_popup_pos = POPPOS_CENTER; |
| 1562 | wp->w_zindex = POPUPWIN_DIALOG_ZINDEX; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 1563 | wp->w_popup_flags |= POPF_DRAG; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1564 | for (i = 0; i < 4; ++i) |
| 1565 | { |
| 1566 | wp->w_popup_border[i] = 1; |
Bram Moolenaar | 0346413 | 2019-07-14 16:28:13 +0200 | [diff] [blame] | 1567 | wp->w_popup_padding[i] = (i & 1) ? 1 : 0; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1571 | if (type == TYPE_MENU) |
| 1572 | { |
| 1573 | typval_T tv; |
| 1574 | callback_T callback; |
| 1575 | |
| 1576 | tv.v_type = VAR_STRING; |
| 1577 | tv.vval.v_string = (char_u *)"popup_filter_menu"; |
| 1578 | callback = get_callback(&tv); |
| 1579 | if (callback.cb_name != NULL) |
| 1580 | set_callback(&wp->w_filter_cb, &callback); |
| 1581 | |
| 1582 | wp->w_p_wrap = 0; |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 1583 | wp->w_popup_flags |= POPF_CURSORLINE; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1586 | if (type == TYPE_PREVIEW) |
| 1587 | { |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 1588 | wp->w_popup_flags |= POPF_DRAG | POPF_RESIZE; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1589 | wp->w_popup_close = POPCLOSE_BUTTON; |
| 1590 | for (i = 0; i < 4; ++i) |
| 1591 | wp->w_popup_border[i] = 1; |
| 1592 | parse_previewpopup(wp); |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1593 | popup_set_wantpos(wp, wp->w_minwidth); |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1596 | for (i = 0; i < 4; ++i) |
| 1597 | VIM_CLEAR(wp->w_border_highlight[i]); |
| 1598 | for (i = 0; i < 8; ++i) |
| 1599 | wp->w_border_char[i] = 0; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1600 | wp->w_want_scrollbar = 1; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1601 | wp->w_popup_fixed = 0; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1602 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 1603 | if (d != NULL) |
| 1604 | // Deal with options. |
| 1605 | apply_options(wp, d); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1606 | |
Bram Moolenaar | 0fcf26b | 2019-06-23 01:03:51 +0200 | [diff] [blame] | 1607 | #ifdef FEAT_TIMERS |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1608 | if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL) |
| 1609 | popup_add_timeout(wp, 3000); |
Bram Moolenaar | 0fcf26b | 2019-06-23 01:03:51 +0200 | [diff] [blame] | 1610 | #endif |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1611 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1612 | popup_adjust_position(wp); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1613 | |
| 1614 | wp->w_vsep_width = 0; |
| 1615 | |
| 1616 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1617 | popup_mask_refresh = TRUE; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1618 | |
| 1619 | return wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 1623 | * popup_clear() |
| 1624 | */ |
| 1625 | void |
| 1626 | f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 1627 | { |
| 1628 | close_all_popups(); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1632 | * popup_create({text}, {options}) |
| 1633 | */ |
| 1634 | void |
| 1635 | f_popup_create(typval_T *argvars, typval_T *rettv) |
| 1636 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1637 | popup_create(argvars, rettv, TYPE_NORMAL); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * popup_atcursor({text}, {options}) |
| 1642 | */ |
| 1643 | void |
| 1644 | f_popup_atcursor(typval_T *argvars, typval_T *rettv) |
| 1645 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1646 | popup_create(argvars, rettv, TYPE_ATCURSOR); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1650 | * popup_beval({text}, {options}) |
| 1651 | */ |
| 1652 | void |
| 1653 | f_popup_beval(typval_T *argvars, typval_T *rettv) |
| 1654 | { |
| 1655 | popup_create(argvars, rettv, TYPE_BEVAL); |
| 1656 | } |
| 1657 | |
| 1658 | /* |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1659 | * Invoke the close callback for window "wp" with value "result". |
| 1660 | * Careful: The callback may make "wp" invalid! |
| 1661 | */ |
| 1662 | static void |
| 1663 | invoke_popup_callback(win_T *wp, typval_T *result) |
| 1664 | { |
| 1665 | typval_T rettv; |
| 1666 | int dummy; |
| 1667 | typval_T argv[3]; |
| 1668 | |
| 1669 | argv[0].v_type = VAR_NUMBER; |
| 1670 | argv[0].vval.v_number = (varnumber_T)wp->w_id; |
| 1671 | |
| 1672 | if (result != NULL && result->v_type != VAR_UNKNOWN) |
| 1673 | copy_tv(result, &argv[1]); |
| 1674 | else |
| 1675 | { |
| 1676 | argv[1].v_type = VAR_NUMBER; |
| 1677 | argv[1].vval.v_number = 0; |
| 1678 | } |
| 1679 | |
| 1680 | argv[2].v_type = VAR_UNKNOWN; |
| 1681 | |
| 1682 | call_callback(&wp->w_close_cb, -1, |
| 1683 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL); |
| 1684 | if (result != NULL) |
| 1685 | clear_tv(&argv[1]); |
| 1686 | clear_tv(&rettv); |
| 1687 | } |
| 1688 | |
| 1689 | /* |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 1690 | * Close popup "wp" and invoke any close callback for it. |
| 1691 | */ |
| 1692 | static void |
| 1693 | popup_close_and_callback(win_T *wp, typval_T *arg) |
| 1694 | { |
| 1695 | int id = wp->w_id; |
| 1696 | |
| 1697 | if (wp->w_close_cb.cb_name != NULL) |
| 1698 | // Careful: This may make "wp" invalid. |
| 1699 | invoke_popup_callback(wp, arg); |
| 1700 | |
| 1701 | popup_close(id); |
| 1702 | } |
| 1703 | |
| 1704 | /* |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 1705 | * Close popup "wp" because of a mouse click. |
| 1706 | */ |
| 1707 | void |
| 1708 | popup_close_for_mouse_click(win_T *wp) |
| 1709 | { |
| 1710 | typval_T res; |
| 1711 | |
| 1712 | res.v_type = VAR_NUMBER; |
| 1713 | res.vval.v_number = -2; |
| 1714 | popup_close_and_callback(wp, &res); |
| 1715 | } |
| 1716 | |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1717 | static void |
| 1718 | check_mouse_moved(win_T *wp, win_T *mouse_wp) |
| 1719 | { |
| 1720 | // Close the popup when all if these are true: |
| 1721 | // - the mouse is not on this popup |
| 1722 | // - "mousemoved" was used |
| 1723 | // - the mouse is no longer on the same screen row or the mouse column is |
| 1724 | // outside of the relevant text |
| 1725 | if (wp != mouse_wp |
| 1726 | && wp->w_popup_mouse_row != 0 |
| 1727 | && (wp->w_popup_mouse_row != mouse_row |
| 1728 | || mouse_col < wp->w_popup_mouse_mincol |
| 1729 | || mouse_col > wp->w_popup_mouse_maxcol)) |
| 1730 | { |
| 1731 | typval_T res; |
| 1732 | |
| 1733 | res.v_type = VAR_NUMBER; |
| 1734 | res.vval.v_number = -2; |
Bram Moolenaar | 3e35d05 | 2019-07-07 20:43:34 +0200 | [diff] [blame] | 1735 | // Careful: this makes "wp" invalid. |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1736 | popup_close_and_callback(wp, &res); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Called when the mouse moved: may close a popup with "mousemoved". |
| 1742 | */ |
| 1743 | void |
| 1744 | popup_handle_mouse_moved(void) |
| 1745 | { |
Bram Moolenaar | 3e35d05 | 2019-07-07 20:43:34 +0200 | [diff] [blame] | 1746 | win_T *wp, *nextwp; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1747 | win_T *mouse_wp; |
| 1748 | int row = mouse_row; |
| 1749 | int col = mouse_col; |
| 1750 | |
| 1751 | // find the window where the mouse is in |
| 1752 | mouse_wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 1753 | |
Bram Moolenaar | 3e35d05 | 2019-07-07 20:43:34 +0200 | [diff] [blame] | 1754 | for (wp = first_popupwin; wp != NULL; wp = nextwp) |
| 1755 | { |
| 1756 | nextwp = wp->w_next; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1757 | check_mouse_moved(wp, mouse_wp); |
Bram Moolenaar | 3e35d05 | 2019-07-07 20:43:34 +0200 | [diff] [blame] | 1758 | } |
| 1759 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = nextwp) |
| 1760 | { |
| 1761 | nextwp = wp->w_next; |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1762 | check_mouse_moved(wp, mouse_wp); |
Bram Moolenaar | 3e35d05 | 2019-07-07 20:43:34 +0200 | [diff] [blame] | 1763 | } |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 1766 | /* |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1767 | * In a filter: check if the typed key is a mouse event that is used for |
| 1768 | * dragging the popup. |
| 1769 | */ |
| 1770 | static void |
| 1771 | filter_handle_drag(win_T *wp, int c, typval_T *rettv) |
| 1772 | { |
| 1773 | int row = mouse_row; |
| 1774 | int col = mouse_col; |
| 1775 | |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 1776 | if ((wp->w_popup_flags & POPF_DRAG) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1777 | && is_mouse_key(c) |
| 1778 | && (wp == popup_dragwin |
| 1779 | || wp == mouse_find_win(&row, &col, FIND_POPUP))) |
| 1780 | // do not consume the key, allow for dragging the popup |
| 1781 | rettv->vval.v_number = 0; |
| 1782 | } |
| 1783 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1784 | /* |
| 1785 | * popup_filter_menu({text}, {options}) |
| 1786 | */ |
| 1787 | void |
| 1788 | f_popup_filter_menu(typval_T *argvars, typval_T *rettv) |
| 1789 | { |
| 1790 | int id = tv_get_number(&argvars[0]); |
| 1791 | win_T *wp = win_id2wp(id); |
| 1792 | char_u *key = tv_get_string(&argvars[1]); |
| 1793 | typval_T res; |
| 1794 | int c; |
| 1795 | linenr_T old_lnum; |
| 1796 | |
| 1797 | // If the popup has been closed do not consume the key. |
| 1798 | if (wp == NULL) |
| 1799 | return; |
| 1800 | |
| 1801 | c = *key; |
| 1802 | if (c == K_SPECIAL && key[1] != NUL) |
| 1803 | c = TO_SPECIAL(key[1], key[2]); |
| 1804 | |
| 1805 | // consume all keys until done |
| 1806 | rettv->vval.v_number = 1; |
| 1807 | res.v_type = VAR_NUMBER; |
| 1808 | |
| 1809 | old_lnum = wp->w_cursor.lnum; |
| 1810 | if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1) |
| 1811 | --wp->w_cursor.lnum; |
| 1812 | if ((c == 'j' || c == 'J' || c == K_DOWN) |
| 1813 | && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count) |
| 1814 | ++wp->w_cursor.lnum; |
| 1815 | if (old_lnum != wp->w_cursor.lnum) |
| 1816 | { |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 1817 | // caller will call popup_highlight_curline() |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C) |
| 1822 | { |
| 1823 | // Cancelled, invoke callback with -1 |
| 1824 | res.vval.v_number = -1; |
| 1825 | popup_close_and_callback(wp, &res); |
| 1826 | return; |
| 1827 | } |
| 1828 | if (c == ' ' || c == K_KENTER || c == CAR || c == NL) |
| 1829 | { |
| 1830 | // Invoke callback with current index. |
| 1831 | res.vval.v_number = wp->w_cursor.lnum; |
| 1832 | popup_close_and_callback(wp, &res); |
| 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | filter_handle_drag(wp, c, rettv); |
| 1837 | } |
| 1838 | |
| 1839 | /* |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1840 | * popup_filter_yesno({text}, {options}) |
| 1841 | */ |
| 1842 | void |
| 1843 | f_popup_filter_yesno(typval_T *argvars, typval_T *rettv) |
| 1844 | { |
| 1845 | int id = tv_get_number(&argvars[0]); |
| 1846 | win_T *wp = win_id2wp(id); |
| 1847 | char_u *key = tv_get_string(&argvars[1]); |
| 1848 | typval_T res; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1849 | int c; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1850 | |
| 1851 | // If the popup has been closed don't consume the key. |
| 1852 | if (wp == NULL) |
| 1853 | return; |
| 1854 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1855 | c = *key; |
| 1856 | if (c == K_SPECIAL && key[1] != NUL) |
| 1857 | c = TO_SPECIAL(key[1], key[2]); |
| 1858 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1859 | // consume all keys until done |
| 1860 | rettv->vval.v_number = 1; |
| 1861 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1862 | if (c == 'y' || c == 'Y') |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1863 | res.vval.v_number = 1; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1864 | else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC) |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1865 | res.vval.v_number = 0; |
| 1866 | else |
| 1867 | { |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1868 | filter_handle_drag(wp, c, rettv); |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1869 | return; |
| 1870 | } |
| 1871 | |
| 1872 | // Invoke callback |
| 1873 | res.v_type = VAR_NUMBER; |
| 1874 | popup_close_and_callback(wp, &res); |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * popup_dialog({text}, {options}) |
| 1879 | */ |
| 1880 | void |
| 1881 | f_popup_dialog(typval_T *argvars, typval_T *rettv) |
| 1882 | { |
| 1883 | popup_create(argvars, rettv, TYPE_DIALOG); |
| 1884 | } |
| 1885 | |
| 1886 | /* |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1887 | * popup_menu({text}, {options}) |
| 1888 | */ |
| 1889 | void |
| 1890 | f_popup_menu(typval_T *argvars, typval_T *rettv) |
| 1891 | { |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 1892 | popup_create(argvars, rettv, TYPE_MENU); |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1896 | * popup_notification({text}, {options}) |
| 1897 | */ |
| 1898 | void |
| 1899 | f_popup_notification(typval_T *argvars, typval_T *rettv) |
| 1900 | { |
| 1901 | popup_create(argvars, rettv, TYPE_NOTIFICATION); |
| 1902 | } |
| 1903 | |
| 1904 | /* |
| 1905 | * Find the popup window with window-ID "id". |
| 1906 | * If the popup window does not exist NULL is returned. |
| 1907 | * If the window is not a popup window, and error message is given. |
| 1908 | */ |
| 1909 | static win_T * |
| 1910 | find_popup_win(int id) |
| 1911 | { |
| 1912 | win_T *wp = win_id2wp(id); |
| 1913 | |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1914 | if (wp != NULL && !WIN_IS_POPUP(wp)) |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1915 | { |
| 1916 | semsg(_("E993: window %d is not a popup window"), id); |
| 1917 | return NULL; |
| 1918 | } |
| 1919 | return wp; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1923 | * popup_close({id}) |
| 1924 | */ |
| 1925 | void |
| 1926 | f_popup_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 1927 | { |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1928 | int id = (int)tv_get_number(argvars); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1929 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1930 | |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1931 | if (wp != NULL) |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 1932 | popup_close_and_callback(wp, &argvars[1]); |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | * popup_hide({id}) |
| 1937 | */ |
| 1938 | void |
| 1939 | f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED) |
| 1940 | { |
| 1941 | int id = (int)tv_get_number(argvars); |
| 1942 | win_T *wp = find_popup_win(id); |
| 1943 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1944 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1945 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1946 | wp->w_popup_flags |= POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 1947 | --wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1948 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1949 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * popup_show({id}) |
| 1955 | */ |
| 1956 | void |
| 1957 | f_popup_show(typval_T *argvars, typval_T *rettv UNUSED) |
| 1958 | { |
| 1959 | int id = (int)tv_get_number(argvars); |
| 1960 | win_T *wp = find_popup_win(id); |
| 1961 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1962 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1963 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1964 | wp->w_popup_flags &= ~POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 1965 | ++wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1966 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1967 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1968 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1971 | /* |
| 1972 | * popup_settext({id}, {text}) |
| 1973 | */ |
| 1974 | void |
| 1975 | f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED) |
| 1976 | { |
| 1977 | int id = (int)tv_get_number(&argvars[0]); |
| 1978 | win_T *wp = find_popup_win(id); |
| 1979 | |
| 1980 | if (wp != NULL) |
| 1981 | { |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1982 | if (argvars[1].v_type != VAR_STRING && argvars[1].v_type != VAR_LIST) |
| 1983 | semsg(_(e_invarg2), tv_get_string(&argvars[1])); |
| 1984 | else |
| 1985 | { |
| 1986 | popup_set_buffer_text(wp->w_buffer, argvars[1]); |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1987 | redraw_win_later(wp, NOT_VALID); |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 1988 | popup_adjust_position(wp); |
| 1989 | } |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1993 | static void |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1994 | popup_free(win_T *wp) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1995 | { |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 1996 | sign_undefine_by_name(popup_get_sign_name(wp), FALSE); |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 1997 | wp->w_buffer->b_locked = FALSE; |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 1998 | if (wp->w_winrow + popup_height(wp) >= cmdline_row) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1999 | clear_cmdline = TRUE; |
| 2000 | win_free_popup(wp); |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 2001 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 2002 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 2003 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2006 | /* |
| 2007 | * Close a popup window by Window-id. |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 2008 | * Does not invoke the callback. |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2009 | */ |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2010 | void |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2011 | popup_close(int id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2012 | { |
| 2013 | win_T *wp; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2014 | tabpage_T *tp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2015 | win_T *prev = NULL; |
| 2016 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2017 | // go through global popups |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2018 | for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next) |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2019 | if (wp->w_id == id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2020 | { |
| 2021 | if (prev == NULL) |
| 2022 | first_popupwin = wp->w_next; |
| 2023 | else |
| 2024 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 2025 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2026 | return; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2027 | } |
| 2028 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2029 | // go through tab-local popups |
| 2030 | FOR_ALL_TABPAGES(tp) |
| 2031 | popup_close_tabpage(tp, id); |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * Close a popup window with Window-id "id" in tabpage "tp". |
| 2036 | */ |
| 2037 | void |
| 2038 | popup_close_tabpage(tabpage_T *tp, int id) |
| 2039 | { |
| 2040 | win_T *wp; |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 2041 | win_T **root = &tp->tp_first_popupwin; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2042 | win_T *prev = NULL; |
| 2043 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2044 | for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next) |
| 2045 | if (wp->w_id == id) |
| 2046 | { |
| 2047 | if (prev == NULL) |
| 2048 | *root = wp->w_next; |
| 2049 | else |
| 2050 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 2051 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 2052 | return; |
| 2053 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | void |
| 2057 | close_all_popups(void) |
| 2058 | { |
| 2059 | while (first_popupwin != NULL) |
| 2060 | popup_close(first_popupwin->w_id); |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 2061 | while (curtab->tp_first_popupwin != NULL) |
| 2062 | popup_close(curtab->tp_first_popupwin->w_id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2063 | } |
| 2064 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 2065 | /* |
| 2066 | * popup_move({id}, {options}) |
| 2067 | */ |
| 2068 | void |
| 2069 | f_popup_move(typval_T *argvars, typval_T *rettv UNUSED) |
| 2070 | { |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2071 | dict_T *dict; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 2072 | int id = (int)tv_get_number(argvars); |
| 2073 | win_T *wp = find_popup_win(id); |
| 2074 | |
| 2075 | if (wp == NULL) |
| 2076 | return; // invalid {id} |
| 2077 | |
| 2078 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 2079 | { |
| 2080 | emsg(_(e_dictreq)); |
| 2081 | return; |
| 2082 | } |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2083 | dict = argvars[1].vval.v_dict; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 2084 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2085 | apply_move_options(wp, dict); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 2086 | |
| 2087 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 2088 | clear_cmdline = TRUE; |
| 2089 | popup_adjust_position(wp); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 2090 | } |
| 2091 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2092 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2093 | * popup_setoptions({id}, {options}) |
| 2094 | */ |
| 2095 | void |
| 2096 | f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 2097 | { |
| 2098 | dict_T *dict; |
| 2099 | int id = (int)tv_get_number(argvars); |
| 2100 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2101 | linenr_T old_firstline; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2102 | |
| 2103 | if (wp == NULL) |
| 2104 | return; // invalid {id} |
| 2105 | |
| 2106 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 2107 | { |
| 2108 | emsg(_(e_dictreq)); |
| 2109 | return; |
| 2110 | } |
| 2111 | dict = argvars[1].vval.v_dict; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2112 | old_firstline = wp->w_firstline; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2113 | |
| 2114 | apply_move_options(wp, dict); |
| 2115 | apply_general_options(wp, dict); |
| 2116 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2117 | if (old_firstline != wp->w_firstline) |
| 2118 | redraw_win_later(wp, NOT_VALID); |
Bram Moolenaar | ad24a71 | 2019-06-17 20:05:45 +0200 | [diff] [blame] | 2119 | popup_mask_refresh = TRUE; |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 2120 | popup_highlight_curline(wp); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2121 | popup_adjust_position(wp); |
| 2122 | } |
| 2123 | |
| 2124 | /* |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 2125 | * popup_getpos({id}) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2126 | */ |
| 2127 | void |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 2128 | f_popup_getpos(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2129 | { |
| 2130 | dict_T *dict; |
| 2131 | int id = (int)tv_get_number(argvars); |
| 2132 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 2133 | int top_extra; |
| 2134 | int left_extra; |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2135 | |
| 2136 | if (rettv_dict_alloc(rettv) == OK) |
| 2137 | { |
| 2138 | if (wp == NULL) |
| 2139 | return; // invalid {id} |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2140 | top_extra = popup_top_extra(wp); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 2141 | left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3]; |
| 2142 | |
Bram Moolenaar | 7b73d7e | 2019-07-26 21:26:34 +0200 | [diff] [blame] | 2143 | // we know how much space we need, avoid resizing halfway |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2144 | dict = rettv->vval.v_dict; |
Bram Moolenaar | 7b73d7e | 2019-07-26 21:26:34 +0200 | [diff] [blame] | 2145 | hash_lock_size(&dict->dv_hashtab, 11); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 2146 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2147 | dict_add_number(dict, "line", wp->w_winrow + 1); |
| 2148 | dict_add_number(dict, "col", wp->w_wincol + 1); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 2149 | dict_add_number(dict, "width", wp->w_width + left_extra |
| 2150 | + wp->w_popup_border[1] + wp->w_popup_padding[1]); |
| 2151 | dict_add_number(dict, "height", wp->w_height + top_extra |
| 2152 | + wp->w_popup_border[2] + wp->w_popup_padding[2]); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 2153 | |
| 2154 | dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra); |
| 2155 | dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra); |
| 2156 | dict_add_number(dict, "core_width", wp->w_width); |
| 2157 | dict_add_number(dict, "core_height", wp->w_height); |
| 2158 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2159 | dict_add_number(dict, "scrollbar", wp->w_has_scrollbar); |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 2160 | dict_add_number(dict, "firstline", wp->w_topline); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2161 | dict_add_number(dict, "visible", |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 2162 | win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0); |
Bram Moolenaar | 7b73d7e | 2019-07-26 21:26:34 +0200 | [diff] [blame] | 2163 | |
| 2164 | hash_unlock(&dict->dv_hashtab); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2165 | } |
| 2166 | } |
Bram Moolenaar | b4f0628 | 2019-07-12 21:07:54 +0200 | [diff] [blame] | 2167 | /* |
| 2168 | * popup_locate({row}, {col}) |
| 2169 | */ |
| 2170 | void |
| 2171 | f_popup_locate(typval_T *argvars, typval_T *rettv) |
| 2172 | { |
| 2173 | int row = tv_get_number(&argvars[0]) - 1; |
| 2174 | int col = tv_get_number(&argvars[1]) - 1; |
| 2175 | win_T *wp; |
| 2176 | |
| 2177 | wp = mouse_find_win(&row, &col, FIND_POPUP); |
| 2178 | if (WIN_IS_POPUP(wp)) |
| 2179 | rettv->vval.v_number = wp->w_id; |
| 2180 | } |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2181 | |
| 2182 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2183 | * For popup_getoptions(): add a "border" or "padding" entry to "dict". |
| 2184 | */ |
| 2185 | static void |
| 2186 | get_padding_border(dict_T *dict, int *array, char *name) |
| 2187 | { |
| 2188 | list_T *list; |
| 2189 | int i; |
| 2190 | |
| 2191 | if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0) |
| 2192 | return; |
| 2193 | |
| 2194 | list = list_alloc(); |
| 2195 | if (list != NULL) |
| 2196 | { |
| 2197 | dict_add_list(dict, name, list); |
| 2198 | if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1) |
| 2199 | for (i = 0; i < 4; ++i) |
| 2200 | list_append_number(list, array[i]); |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * For popup_getoptions(): add a "borderhighlight" entry to "dict". |
| 2206 | */ |
| 2207 | static void |
| 2208 | get_borderhighlight(dict_T *dict, win_T *wp) |
| 2209 | { |
| 2210 | list_T *list; |
| 2211 | int i; |
| 2212 | |
| 2213 | for (i = 0; i < 4; ++i) |
| 2214 | if (wp->w_border_highlight[i] != NULL) |
| 2215 | break; |
| 2216 | if (i == 4) |
| 2217 | return; |
| 2218 | |
| 2219 | list = list_alloc(); |
| 2220 | if (list != NULL) |
| 2221 | { |
| 2222 | dict_add_list(dict, "borderhighlight", list); |
| 2223 | for (i = 0; i < 4; ++i) |
| 2224 | list_append_string(list, wp->w_border_highlight[i], -1); |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /* |
| 2229 | * For popup_getoptions(): add a "borderchars" entry to "dict". |
| 2230 | */ |
| 2231 | static void |
| 2232 | get_borderchars(dict_T *dict, win_T *wp) |
| 2233 | { |
| 2234 | list_T *list; |
| 2235 | int i; |
| 2236 | char_u buf[NUMBUFLEN]; |
| 2237 | int len; |
| 2238 | |
| 2239 | for (i = 0; i < 8; ++i) |
| 2240 | if (wp->w_border_char[i] != 0) |
| 2241 | break; |
| 2242 | if (i == 8) |
| 2243 | return; |
| 2244 | |
| 2245 | list = list_alloc(); |
| 2246 | if (list != NULL) |
| 2247 | { |
| 2248 | dict_add_list(dict, "borderchars", list); |
| 2249 | for (i = 0; i < 8; ++i) |
| 2250 | { |
| 2251 | len = mb_char2bytes(wp->w_border_char[i], buf); |
| 2252 | list_append_string(list, buf, len); |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 2258 | * For popup_getoptions(): add a "moved" and "mousemoved" entry to "dict". |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2259 | */ |
| 2260 | static void |
| 2261 | get_moved_list(dict_T *dict, win_T *wp) |
| 2262 | { |
| 2263 | list_T *list; |
| 2264 | |
| 2265 | list = list_alloc(); |
| 2266 | if (list != NULL) |
| 2267 | { |
| 2268 | dict_add_list(dict, "moved", list); |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 2269 | list_append_number(list, wp->w_popup_lnum); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2270 | list_append_number(list, wp->w_popup_mincol); |
| 2271 | list_append_number(list, wp->w_popup_maxcol); |
| 2272 | } |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 2273 | list = list_alloc(); |
| 2274 | if (list != NULL) |
| 2275 | { |
| 2276 | dict_add_list(dict, "mousemoved", list); |
| 2277 | list_append_number(list, wp->w_popup_mouse_row); |
| 2278 | list_append_number(list, wp->w_popup_mouse_mincol); |
| 2279 | list_append_number(list, wp->w_popup_mouse_maxcol); |
| 2280 | } |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /* |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 2284 | * popup_getoptions({id}) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2285 | */ |
| 2286 | void |
| 2287 | f_popup_getoptions(typval_T *argvars, typval_T *rettv) |
| 2288 | { |
| 2289 | dict_T *dict; |
| 2290 | int id = (int)tv_get_number(argvars); |
| 2291 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 2292 | tabpage_T *tp; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 2293 | int i; |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2294 | |
| 2295 | if (rettv_dict_alloc(rettv) == OK) |
| 2296 | { |
| 2297 | if (wp == NULL) |
| 2298 | return; |
| 2299 | |
| 2300 | dict = rettv->vval.v_dict; |
| 2301 | dict_add_number(dict, "line", wp->w_wantline); |
| 2302 | dict_add_number(dict, "col", wp->w_wantcol); |
| 2303 | dict_add_number(dict, "minwidth", wp->w_minwidth); |
| 2304 | dict_add_number(dict, "minheight", wp->w_minheight); |
| 2305 | dict_add_number(dict, "maxheight", wp->w_maxheight); |
| 2306 | dict_add_number(dict, "maxwidth", wp->w_maxwidth); |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 2307 | dict_add_number(dict, "firstline", wp->w_firstline); |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2308 | dict_add_number(dict, "scrollbar", wp->w_want_scrollbar); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2309 | dict_add_number(dict, "zindex", wp->w_zindex); |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 2310 | dict_add_number(dict, "fixed", wp->w_popup_fixed); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2311 | dict_add_string(dict, "title", wp->w_popup_title); |
| 2312 | dict_add_number(dict, "wrap", wp->w_p_wrap); |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 2313 | dict_add_number(dict, "drag", (wp->w_popup_flags & POPF_DRAG) != 0); |
| 2314 | dict_add_number(dict, "resize", (wp->w_popup_flags & POPF_RESIZE) != 0); |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 2315 | dict_add_number(dict, "cursorline", |
| 2316 | (wp->w_popup_flags & POPF_CURSORLINE) != 0); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2317 | dict_add_string(dict, "highlight", wp->w_p_wcr); |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 2318 | if (wp->w_scrollbar_highlight != NULL) |
| 2319 | dict_add_string(dict, "scrollbarhighlight", |
| 2320 | wp->w_scrollbar_highlight); |
| 2321 | if (wp->w_thumb_highlight != NULL) |
| 2322 | dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2323 | |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 2324 | // find the tabpage that holds this popup |
| 2325 | i = 1; |
| 2326 | FOR_ALL_TABPAGES(tp) |
| 2327 | { |
| 2328 | win_T *p; |
| 2329 | |
| 2330 | for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next) |
| 2331 | if (p->w_id == id) |
| 2332 | break; |
| 2333 | if (p != NULL) |
| 2334 | break; |
| 2335 | ++i; |
| 2336 | } |
| 2337 | if (tp == NULL) |
| 2338 | i = -1; // must be global |
| 2339 | else if (tp == curtab) |
| 2340 | i = 0; |
| 2341 | dict_add_number(dict, "tabpage", i); |
| 2342 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 2343 | get_padding_border(dict, wp->w_popup_padding, "padding"); |
| 2344 | get_padding_border(dict, wp->w_popup_border, "border"); |
| 2345 | get_borderhighlight(dict, wp); |
| 2346 | get_borderchars(dict, wp); |
| 2347 | get_moved_list(dict, wp); |
| 2348 | |
| 2349 | if (wp->w_filter_cb.cb_name != NULL) |
| 2350 | dict_add_callback(dict, "filter", &wp->w_filter_cb); |
| 2351 | if (wp->w_close_cb.cb_name != NULL) |
| 2352 | dict_add_callback(dict, "callback", &wp->w_close_cb); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 2353 | |
| 2354 | for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T)); |
| 2355 | ++i) |
| 2356 | if (wp->w_popup_pos == poppos_entries[i].pp_val) |
| 2357 | { |
| 2358 | dict_add_string(dict, "pos", |
| 2359 | (char_u *)poppos_entries[i].pp_name); |
| 2360 | break; |
| 2361 | } |
| 2362 | |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 2363 | dict_add_string(dict, "close", (char_u *)( |
| 2364 | wp->w_popup_close == POPCLOSE_BUTTON ? "button" |
| 2365 | : wp->w_popup_close == POPCLOSE_CLICK ? "click" : "none")); |
| 2366 | |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 2367 | # if defined(FEAT_TIMERS) |
| 2368 | dict_add_number(dict, "time", wp->w_popup_timer != NULL |
| 2369 | ? (long)wp->w_popup_timer->tr_interval : 0L); |
| 2370 | # endif |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 2371 | } |
| 2372 | } |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 2373 | |
| 2374 | int |
Bram Moolenaar | 8cdbd5b | 2019-06-16 15:50:45 +0200 | [diff] [blame] | 2375 | error_if_popup_window() |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 2376 | { |
Bram Moolenaar | 5b8cfed | 2019-06-30 22:16:10 +0200 | [diff] [blame] | 2377 | if (WIN_IS_POPUP(curwin)) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 2378 | { |
| 2379 | emsg(_("E994: Not allowed in a popup window")); |
| 2380 | return TRUE; |
| 2381 | } |
| 2382 | return FALSE; |
| 2383 | } |
| 2384 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2385 | /* |
| 2386 | * Reset all the POPF_HANDLED flags in global popup windows and popup windows |
Bram Moolenaar | fc06cbb | 2019-06-15 14:14:31 +0200 | [diff] [blame] | 2387 | * in the current tab page. |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2388 | */ |
| 2389 | void |
| 2390 | popup_reset_handled() |
| 2391 | { |
| 2392 | win_T *wp; |
| 2393 | |
| 2394 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 2395 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 2396 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 2397 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 2398 | } |
| 2399 | |
| 2400 | /* |
| 2401 | * Find the next visible popup where POPF_HANDLED is not set. |
| 2402 | * Must have called popup_reset_handled() first. |
| 2403 | * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the |
| 2404 | * popup with the highest zindex. |
| 2405 | */ |
| 2406 | win_T * |
| 2407 | find_next_popup(int lowest) |
| 2408 | { |
| 2409 | win_T *wp; |
| 2410 | win_T *found_wp; |
| 2411 | int found_zindex; |
| 2412 | |
| 2413 | found_zindex = lowest ? INT_MAX : 0; |
| 2414 | found_wp = NULL; |
| 2415 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 2416 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 2417 | && (lowest ? wp->w_zindex < found_zindex |
| 2418 | : wp->w_zindex > found_zindex)) |
| 2419 | { |
| 2420 | found_zindex = wp->w_zindex; |
| 2421 | found_wp = wp; |
| 2422 | } |
| 2423 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 2424 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 2425 | && (lowest ? wp->w_zindex < found_zindex |
| 2426 | : wp->w_zindex > found_zindex)) |
| 2427 | { |
| 2428 | found_zindex = wp->w_zindex; |
| 2429 | found_wp = wp; |
| 2430 | } |
| 2431 | |
| 2432 | if (found_wp != NULL) |
| 2433 | found_wp->w_popup_flags |= POPF_HANDLED; |
| 2434 | return found_wp; |
| 2435 | } |
| 2436 | |
| 2437 | /* |
| 2438 | * Invoke the filter callback for window "wp" with typed character "c". |
| 2439 | * Uses the global "mod_mask" for modifiers. |
| 2440 | * Returns the return value of the filter. |
| 2441 | * Careful: The filter may make "wp" invalid! |
| 2442 | */ |
| 2443 | static int |
| 2444 | invoke_popup_filter(win_T *wp, int c) |
| 2445 | { |
| 2446 | int res; |
| 2447 | typval_T rettv; |
| 2448 | int dummy; |
| 2449 | typval_T argv[3]; |
| 2450 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 2451 | linenr_T old_lnum = wp->w_cursor.lnum; |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2452 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 2453 | // Emergency exit: CTRL-C closes the popup. |
| 2454 | if (c == Ctrl_C) |
| 2455 | { |
| 2456 | rettv.v_type = VAR_NUMBER; |
| 2457 | rettv.vval.v_number = -1; |
| 2458 | popup_close_and_callback(wp, &rettv); |
| 2459 | return 1; |
| 2460 | } |
| 2461 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2462 | argv[0].v_type = VAR_NUMBER; |
| 2463 | argv[0].vval.v_number = (varnumber_T)wp->w_id; |
| 2464 | |
| 2465 | // Convert the number to a string, so that the function can use: |
| 2466 | // if a:c == "\<F2>" |
| 2467 | buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL; |
| 2468 | argv[1].v_type = VAR_STRING; |
| 2469 | argv[1].vval.v_string = vim_strsave(buf); |
| 2470 | |
| 2471 | argv[2].v_type = VAR_UNKNOWN; |
| 2472 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 2473 | // NOTE: The callback might close the popup, thus make "wp" invalid. |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2474 | call_callback(&wp->w_filter_cb, -1, |
| 2475 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL); |
Bram Moolenaar | cb5ff34 | 2019-07-20 16:51:19 +0200 | [diff] [blame] | 2476 | if (win_valid_popup(wp) && old_lnum != wp->w_cursor.lnum) |
Bram Moolenaar | df9c6ca | 2019-07-18 13:46:42 +0200 | [diff] [blame] | 2477 | popup_highlight_curline(wp); |
| 2478 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2479 | res = tv_get_number(&rettv); |
| 2480 | vim_free(argv[1].vval.v_string); |
| 2481 | clear_tv(&rettv); |
| 2482 | return res; |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * Called when "c" was typed: invoke popup filter callbacks. |
| 2487 | * Returns TRUE when the character was consumed, |
| 2488 | */ |
| 2489 | int |
| 2490 | popup_do_filter(int c) |
| 2491 | { |
| 2492 | int res = FALSE; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 2493 | win_T *wp; |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 2494 | |
| 2495 | popup_reset_handled(); |
| 2496 | |
| 2497 | while (!res && (wp = find_next_popup(FALSE)) != NULL) |
| 2498 | if (wp->w_filter_cb.cb_name != NULL) |
| 2499 | res = invoke_popup_filter(wp, c); |
| 2500 | |
| 2501 | return res; |
| 2502 | } |
| 2503 | |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 2504 | /* |
| 2505 | * Called when the cursor moved: check if any popup needs to be closed if the |
| 2506 | * cursor moved far enough. |
| 2507 | */ |
| 2508 | void |
| 2509 | popup_check_cursor_pos() |
| 2510 | { |
| 2511 | win_T *wp; |
| 2512 | typval_T tv; |
| 2513 | |
| 2514 | popup_reset_handled(); |
| 2515 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 2516 | if (wp->w_popup_curwin != NULL |
| 2517 | && (curwin != wp->w_popup_curwin |
| 2518 | || curwin->w_cursor.lnum != wp->w_popup_lnum |
| 2519 | || curwin->w_cursor.col < wp->w_popup_mincol |
| 2520 | || curwin->w_cursor.col > wp->w_popup_maxcol)) |
| 2521 | { |
| 2522 | tv.v_type = VAR_NUMBER; |
| 2523 | tv.vval.v_number = -1; |
| 2524 | popup_close_and_callback(wp, &tv); |
| 2525 | } |
| 2526 | } |
| 2527 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2528 | /* |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2529 | * Update "w_popup_mask_cells". |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2530 | */ |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2531 | static void |
| 2532 | popup_update_mask(win_T *wp, int width, int height) |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2533 | { |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2534 | listitem_T *lio, *li; |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2535 | char_u *cells; |
| 2536 | int row, col; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2537 | |
| 2538 | if (wp->w_popup_mask == NULL) |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2539 | return; |
| 2540 | if (wp->w_popup_mask_cells != NULL |
| 2541 | && wp->w_popup_mask_height == height |
| 2542 | && wp->w_popup_mask_width == width) |
| 2543 | return; // cache is still valid |
| 2544 | |
| 2545 | vim_free(wp->w_popup_mask_cells); |
| 2546 | wp->w_popup_mask_cells = alloc_clear(width * height); |
| 2547 | if (wp->w_popup_mask_cells == NULL) |
| 2548 | return; |
| 2549 | cells = wp->w_popup_mask_cells; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2550 | |
| 2551 | for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next) |
| 2552 | { |
| 2553 | int cols, cole; |
| 2554 | int lines, linee; |
| 2555 | |
| 2556 | li = lio->li_tv.vval.v_list->lv_first; |
| 2557 | cols = tv_get_number(&li->li_tv); |
| 2558 | if (cols < 0) |
| 2559 | cols = width + cols + 1; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2560 | li = li->li_next; |
| 2561 | cole = tv_get_number(&li->li_tv); |
| 2562 | if (cole < 0) |
| 2563 | cole = width + cole + 1; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2564 | li = li->li_next; |
| 2565 | lines = tv_get_number(&li->li_tv); |
| 2566 | if (lines < 0) |
| 2567 | lines = height + lines + 1; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2568 | li = li->li_next; |
| 2569 | linee = tv_get_number(&li->li_tv); |
| 2570 | if (linee < 0) |
| 2571 | linee = height + linee + 1; |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2572 | |
| 2573 | for (row = lines - 1; row < linee && row < height; ++row) |
| 2574 | for (col = cols - 1; col < cole && col < width; ++col) |
| 2575 | cells[row * width + col] = 1; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2576 | } |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Return TRUE if "col" / "line" matches with an entry in w_popup_mask. |
| 2581 | * "col" and "line" are screen coordinates. |
| 2582 | */ |
| 2583 | static int |
| 2584 | popup_masked(win_T *wp, int width, int height, int screencol, int screenline) |
| 2585 | { |
| 2586 | int col = screencol - wp->w_wincol + wp->w_popup_leftoff; |
| 2587 | int line = screenline - wp->w_winrow; |
| 2588 | |
| 2589 | return col >= 0 && col < width |
| 2590 | && line >= 0 && line < height |
| 2591 | && wp->w_popup_mask_cells[line * width + col]; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | /* |
| 2595 | * Set flags in popup_transparent[] for window "wp" to "val". |
| 2596 | */ |
| 2597 | static void |
| 2598 | update_popup_transparent(win_T *wp, int val) |
| 2599 | { |
| 2600 | if (wp->w_popup_mask != NULL) |
| 2601 | { |
| 2602 | int width = popup_width(wp); |
| 2603 | int height = popup_height(wp); |
| 2604 | listitem_T *lio, *li; |
| 2605 | int cols, cole; |
| 2606 | int lines, linee; |
| 2607 | int col, line; |
| 2608 | |
| 2609 | for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next) |
| 2610 | { |
| 2611 | li = lio->li_tv.vval.v_list->lv_first; |
| 2612 | cols = tv_get_number(&li->li_tv); |
| 2613 | if (cols < 0) |
| 2614 | cols = width + cols + 1; |
| 2615 | li = li->li_next; |
| 2616 | cole = tv_get_number(&li->li_tv); |
| 2617 | if (cole < 0) |
| 2618 | cole = width + cole + 1; |
| 2619 | li = li->li_next; |
| 2620 | lines = tv_get_number(&li->li_tv); |
| 2621 | if (lines < 0) |
| 2622 | lines = height + lines + 1; |
| 2623 | li = li->li_next; |
| 2624 | linee = tv_get_number(&li->li_tv); |
| 2625 | if (linee < 0) |
| 2626 | linee = height + linee + 1; |
| 2627 | |
| 2628 | --cols; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2629 | cols -= wp->w_popup_leftoff; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 2630 | if (cols < 0) |
| 2631 | cols = 0; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2632 | cole -= wp->w_popup_leftoff; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2633 | --lines; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 2634 | if (lines < 0) |
| 2635 | lines = 0; |
Bram Moolenaar | b420747 | 2019-07-12 16:05:45 +0200 | [diff] [blame] | 2636 | for (line = lines; line < linee |
| 2637 | && line + wp->w_winrow < screen_Rows; ++line) |
| 2638 | for (col = cols; col < cole |
| 2639 | && col + wp->w_wincol < screen_Columns; ++col) |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2640 | popup_transparent[(line + wp->w_winrow) * screen_Columns |
| 2641 | + col + wp->w_wincol] = val; |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | /* |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2647 | * Update "popup_mask" if needed. |
| 2648 | * Also recomputes the popup size and positions. |
| 2649 | * Also updates "popup_visible". |
| 2650 | * Also marks window lines for redrawing. |
| 2651 | */ |
| 2652 | void |
| 2653 | may_update_popup_mask(int type) |
| 2654 | { |
| 2655 | win_T *wp; |
| 2656 | short *mask; |
| 2657 | int line, col; |
| 2658 | int redraw_all = FALSE; |
| 2659 | |
| 2660 | // Need to recompute when switching tabs. |
| 2661 | // Also recompute when the type is CLEAR or NOT_VALID, something basic |
| 2662 | // (such as the screen size) must have changed. |
| 2663 | if (popup_mask_tab != curtab || type >= NOT_VALID) |
| 2664 | { |
| 2665 | popup_mask_refresh = TRUE; |
| 2666 | redraw_all = TRUE; |
| 2667 | } |
| 2668 | if (!popup_mask_refresh) |
| 2669 | { |
| 2670 | // Check if any buffer has changed. |
| 2671 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 2672 | if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2673 | popup_mask_refresh = TRUE; |
| 2674 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 2675 | if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2676 | popup_mask_refresh = TRUE; |
| 2677 | if (!popup_mask_refresh) |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | // Need to update the mask, something has changed. |
| 2682 | popup_mask_refresh = FALSE; |
| 2683 | popup_mask_tab = curtab; |
| 2684 | popup_visible = FALSE; |
| 2685 | |
| 2686 | // If redrawing everything, just update "popup_mask". |
| 2687 | // If redrawing only what is needed, update "popup_mask_next" and then |
| 2688 | // compare with "popup_mask" to see what changed. |
| 2689 | if (type >= SOME_VALID) |
| 2690 | mask = popup_mask; |
| 2691 | else |
| 2692 | mask = popup_mask_next; |
| 2693 | vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short)); |
| 2694 | |
| 2695 | // Find the window with the lowest zindex that hasn't been handled yet, |
| 2696 | // so that the window with a higher zindex overwrites the value in |
| 2697 | // popup_mask. |
| 2698 | popup_reset_handled(); |
| 2699 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 2700 | { |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2701 | int width; |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2702 | int height; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2703 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2704 | popup_visible = TRUE; |
| 2705 | |
| 2706 | // Recompute the position if the text changed. |
| 2707 | if (redraw_all |
| 2708 | || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2709 | popup_adjust_position(wp); |
| 2710 | |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2711 | width = popup_width(wp); |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2712 | height = popup_height(wp); |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2713 | popup_update_mask(wp, width, height); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2714 | for (line = wp->w_winrow; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2715 | line < wp->w_winrow + height && line < screen_Rows; ++line) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2716 | for (col = wp->w_wincol; |
Bram Moolenaar | e865dcb | 2019-07-26 22:15:50 +0200 | [diff] [blame] | 2717 | col < wp->w_wincol + width - wp->w_popup_leftoff |
| 2718 | && col < screen_Columns; ++col) |
| 2719 | if (wp->w_popup_mask_cells == NULL |
| 2720 | || !popup_masked(wp, width, height, col, line)) |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2721 | mask[line * screen_Columns + col] = wp->w_zindex; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | // Only check which lines are to be updated if not already |
| 2725 | // updating all lines. |
| 2726 | if (mask == popup_mask_next) |
Bram Moolenaar | 9d5ffce | 2019-07-26 21:01:29 +0200 | [diff] [blame] | 2727 | { |
| 2728 | int *plines_cache = ALLOC_CLEAR_MULT(int, Rows); |
| 2729 | win_T *prev_wp = NULL; |
| 2730 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2731 | for (line = 0; line < screen_Rows; ++line) |
| 2732 | { |
| 2733 | int col_done = 0; |
| 2734 | |
| 2735 | for (col = 0; col < screen_Columns; ++col) |
| 2736 | { |
| 2737 | int off = line * screen_Columns + col; |
| 2738 | |
| 2739 | if (popup_mask[off] != popup_mask_next[off]) |
| 2740 | { |
| 2741 | popup_mask[off] = popup_mask_next[off]; |
| 2742 | |
| 2743 | if (line >= cmdline_row) |
| 2744 | { |
| 2745 | // the command line needs to be cleared if text below |
| 2746 | // the popup is now visible. |
| 2747 | if (!msg_scrolled && popup_mask_next[off] == 0) |
| 2748 | clear_cmdline = TRUE; |
| 2749 | } |
| 2750 | else if (col >= col_done) |
| 2751 | { |
| 2752 | linenr_T lnum; |
| 2753 | int line_cp = line; |
| 2754 | int col_cp = col; |
| 2755 | |
| 2756 | // The screen position "line" / "col" needs to be |
| 2757 | // redrawn. Figure out what window that is and update |
| 2758 | // w_redraw_top and w_redr_bot. Only needs to be done |
| 2759 | // once for each window line. |
| 2760 | wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP); |
| 2761 | if (wp != NULL) |
| 2762 | { |
Bram Moolenaar | 9d5ffce | 2019-07-26 21:01:29 +0200 | [diff] [blame] | 2763 | if (wp != prev_wp) |
| 2764 | { |
| 2765 | vim_memset(plines_cache, 0, sizeof(int) * Rows); |
| 2766 | prev_wp = wp; |
| 2767 | } |
| 2768 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2769 | if (line_cp >= wp->w_height) |
| 2770 | // In (or below) status line |
| 2771 | wp->w_redr_status = TRUE; |
| 2772 | // compute the position in the buffer line from the |
| 2773 | // position on the screen |
| 2774 | else if (mouse_comp_pos(wp, &line_cp, &col_cp, |
Bram Moolenaar | 9d5ffce | 2019-07-26 21:01:29 +0200 | [diff] [blame] | 2775 | &lnum, plines_cache)) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2776 | // past bottom |
| 2777 | wp->w_redr_status = TRUE; |
| 2778 | else |
| 2779 | redrawWinline(wp, lnum); |
| 2780 | |
| 2781 | // This line is going to be redrawn, no need to |
| 2782 | // check until the right side of the window. |
| 2783 | col_done = wp->w_wincol + wp->w_width - 1; |
| 2784 | } |
| 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | } |
Bram Moolenaar | 9d5ffce | 2019-07-26 21:01:29 +0200 | [diff] [blame] | 2789 | |
| 2790 | vim_free(plines_cache); |
| 2791 | } |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * Return a string of "len" spaces in IObuff. |
| 2796 | */ |
| 2797 | static char_u * |
| 2798 | get_spaces(int len) |
| 2799 | { |
| 2800 | vim_memset(IObuff, ' ', (size_t)len); |
| 2801 | IObuff[len] = NUL; |
| 2802 | return IObuff; |
| 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * Update popup windows. They are drawn on top of normal windows. |
| 2807 | * "win_update" is called for each popup window, lowest zindex first. |
| 2808 | */ |
| 2809 | void |
| 2810 | update_popups(void (*win_update)(win_T *wp)) |
| 2811 | { |
| 2812 | win_T *wp; |
| 2813 | int top_off; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2814 | int left_extra; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2815 | int total_width; |
| 2816 | int total_height; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2817 | int top_padding; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2818 | int popup_attr; |
| 2819 | int border_attr[4]; |
| 2820 | int border_char[8]; |
| 2821 | char_u buf[MB_MAXBYTES]; |
| 2822 | int row; |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2823 | int wincol; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2824 | int padcol = 0; |
| 2825 | int padwidth = 0; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2826 | int i; |
Bram Moolenaar | 6efd76a | 2019-06-26 04:06:57 +0200 | [diff] [blame] | 2827 | int sb_thumb_top = 0; |
| 2828 | int sb_thumb_height = 0; |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 2829 | int attr_scroll = 0; |
| 2830 | int attr_thumb = 0; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2831 | |
| 2832 | // Find the window with the lowest zindex that hasn't been updated yet, |
| 2833 | // so that the window with a higher zindex is drawn later, thus goes on |
| 2834 | // top. |
| 2835 | popup_reset_handled(); |
| 2836 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 2837 | { |
| 2838 | // This drawing uses the zindex of the popup window, so that it's on |
| 2839 | // top of the text but doesn't draw when another popup with higher |
| 2840 | // zindex is on top of the character. |
| 2841 | screen_zindex = wp->w_zindex; |
| 2842 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2843 | // Set flags in popup_transparent[] for masked cells. |
| 2844 | update_popup_transparent(wp, 1); |
| 2845 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2846 | // adjust w_winrow and w_wincol for border and padding, since |
| 2847 | // win_update() doesn't handle them. |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2848 | top_off = popup_top_extra(wp); |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2849 | left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3] |
| 2850 | - wp->w_popup_leftoff; |
| 2851 | if (wp->w_wincol + left_extra < 0) |
| 2852 | left_extra = -wp->w_wincol; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2853 | wp->w_winrow += top_off; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2854 | wp->w_wincol += left_extra; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2855 | |
Bram Moolenaar | c2a4316 | 2019-06-26 01:03:53 +0200 | [diff] [blame] | 2856 | // Draw the popup text, unless it's off screen. |
| 2857 | if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns) |
| 2858 | win_update(wp); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2859 | |
| 2860 | wp->w_winrow -= top_off; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2861 | wp->w_wincol -= left_extra; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2862 | |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2863 | total_width = popup_width(wp); |
| 2864 | total_height = popup_height(wp); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2865 | popup_attr = get_wcr_attr(wp); |
| 2866 | |
Bram Moolenaar | 13d5c3f | 2019-07-28 21:42:38 +0200 | [diff] [blame] | 2867 | if (wp->w_winrow + total_height > cmdline_row) |
| 2868 | wp->w_popup_flags |= POPF_ON_CMDLINE; |
| 2869 | else |
| 2870 | wp->w_popup_flags &= ~POPF_ON_CMDLINE; |
| 2871 | |
| 2872 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2873 | // We can only use these line drawing characters when 'encoding' is |
| 2874 | // "utf-8" and 'ambiwidth' is "single". |
| 2875 | if (enc_utf8 && *p_ambw == 's') |
| 2876 | { |
| 2877 | border_char[0] = border_char[2] = 0x2550; |
| 2878 | border_char[1] = border_char[3] = 0x2551; |
| 2879 | border_char[4] = 0x2554; |
| 2880 | border_char[5] = 0x2557; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 2881 | border_char[6] = (wp->w_popup_flags & POPF_RESIZE) |
| 2882 | ? 0x21f2 : 0x255d; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2883 | border_char[7] = 0x255a; |
| 2884 | } |
| 2885 | else |
| 2886 | { |
| 2887 | border_char[0] = border_char[2] = '-'; |
| 2888 | border_char[1] = border_char[3] = '|'; |
| 2889 | for (i = 4; i < 8; ++i) |
| 2890 | border_char[i] = '+'; |
Bram Moolenaar | 9bcb70c | 2019-08-01 21:11:05 +0200 | [diff] [blame] | 2891 | if (wp->w_popup_flags & POPF_RESIZE) |
| 2892 | border_char[6] = '@'; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2893 | } |
| 2894 | for (i = 0; i < 8; ++i) |
| 2895 | if (wp->w_border_char[i] != 0) |
| 2896 | border_char[i] = wp->w_border_char[i]; |
| 2897 | |
| 2898 | for (i = 0; i < 4; ++i) |
| 2899 | { |
| 2900 | border_attr[i] = popup_attr; |
| 2901 | if (wp->w_border_highlight[i] != NULL) |
| 2902 | border_attr[i] = syn_name2attr(wp->w_border_highlight[i]); |
| 2903 | } |
| 2904 | |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2905 | wincol = wp->w_wincol - wp->w_popup_leftoff; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2906 | top_padding = wp->w_popup_padding[0]; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2907 | if (wp->w_popup_border[0] > 0) |
| 2908 | { |
| 2909 | // top border |
| 2910 | screen_fill(wp->w_winrow, wp->w_winrow + 1, |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2911 | wincol < 0 ? 0 : wincol, wincol + total_width, |
| 2912 | wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0 |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2913 | ? border_char[4] : border_char[0], |
| 2914 | border_char[0], border_attr[0]); |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2915 | if (wp->w_popup_border[1] > 0 && wp->w_popup_rightoff == 0) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2916 | { |
| 2917 | buf[mb_char2bytes(border_char[5], buf)] = NUL; |
| 2918 | screen_puts(buf, wp->w_winrow, |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2919 | wincol + total_width - 1, border_attr[1]); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2920 | } |
| 2921 | } |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2922 | else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0) |
| 2923 | top_padding = 1; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2924 | |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2925 | if (top_padding > 0 || wp->w_popup_padding[2] > 0) |
| 2926 | { |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2927 | padcol = wincol + wp->w_popup_border[3]; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2928 | padwidth = wp->w_wincol + total_width - wp->w_popup_border[1] |
| 2929 | - wp->w_has_scrollbar; |
| 2930 | if (padcol < 0) |
| 2931 | { |
| 2932 | padwidth += padcol; |
| 2933 | padcol = 0; |
| 2934 | } |
| 2935 | } |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2936 | if (top_padding > 0) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2937 | { |
| 2938 | // top padding |
| 2939 | row = wp->w_winrow + wp->w_popup_border[0]; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2940 | screen_fill(row, row + top_padding, padcol, padwidth, |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2941 | ' ', ' ', popup_attr); |
| 2942 | } |
| 2943 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2944 | // Title goes on top of border or padding. |
| 2945 | if (wp->w_popup_title != NULL) |
| 2946 | screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1, |
| 2947 | wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr); |
| 2948 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2949 | // Compute scrollbar thumb position and size. |
| 2950 | if (wp->w_has_scrollbar) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2951 | { |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2952 | linenr_T linecount = wp->w_buffer->b_ml.ml_line_count; |
| 2953 | |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 2954 | sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2) |
| 2955 | / linecount; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2956 | if (sb_thumb_height == 0) |
| 2957 | sb_thumb_height = 1; |
Bram Moolenaar | 437a746 | 2019-07-05 20:17:22 +0200 | [diff] [blame] | 2958 | if (linecount <= wp->w_height) |
| 2959 | // it just fits, avoid divide by zero |
| 2960 | sb_thumb_top = 0; |
| 2961 | else |
| 2962 | sb_thumb_top = (wp->w_topline - 1 |
| 2963 | + (linecount / wp->w_height) / 2) |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 2964 | * (wp->w_height - sb_thumb_height) |
| 2965 | / (linecount - wp->w_height); |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 2966 | if (wp->w_scrollbar_highlight != NULL) |
| 2967 | attr_scroll = syn_name2attr(wp->w_scrollbar_highlight); |
| 2968 | else |
| 2969 | attr_scroll = highlight_attr[HLF_PSB]; |
| 2970 | if (wp->w_thumb_highlight != NULL) |
| 2971 | attr_thumb = syn_name2attr(wp->w_thumb_highlight); |
| 2972 | else |
| 2973 | attr_thumb = highlight_attr[HLF_PST]; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | for (i = wp->w_popup_border[0]; |
| 2977 | i < total_height - wp->w_popup_border[2]; ++i) |
| 2978 | { |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2979 | int pad_left; |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2980 | // left and right padding only needed next to the body |
| 2981 | int do_padding = |
| 2982 | i >= wp->w_popup_border[0] + wp->w_popup_padding[0] |
| 2983 | && i < total_height - wp->w_popup_border[2] |
| 2984 | - wp->w_popup_padding[2]; |
| 2985 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2986 | row = wp->w_winrow + i; |
| 2987 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2988 | // left border |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2989 | if (wp->w_popup_border[3] > 0 && wincol >= 0) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2990 | { |
| 2991 | buf[mb_char2bytes(border_char[3], buf)] = NUL; |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2992 | screen_puts(buf, row, wincol, border_attr[3]); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2993 | } |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2994 | if (do_padding && wp->w_popup_padding[3] > 0) |
| 2995 | { |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 2996 | int col = wincol + wp->w_popup_border[3]; |
| 2997 | |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2998 | // left padding |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 2999 | pad_left = wp->w_popup_padding[3]; |
| 3000 | if (col < 0) |
| 3001 | { |
| 3002 | pad_left += col; |
| 3003 | col = 0; |
| 3004 | } |
| 3005 | if (pad_left > 0) |
| 3006 | screen_puts(get_spaces(pad_left), row, col, popup_attr); |
| 3007 | } |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 3008 | // scrollbar |
| 3009 | if (wp->w_has_scrollbar) |
| 3010 | { |
| 3011 | int line = i - top_off; |
| 3012 | int scroll_col = wp->w_wincol + total_width - 1 |
| 3013 | - wp->w_popup_border[1]; |
| 3014 | |
| 3015 | if (line >= 0 && line < wp->w_height) |
| 3016 | screen_putchar(' ', row, scroll_col, |
| 3017 | line >= sb_thumb_top |
| 3018 | && line < sb_thumb_top + sb_thumb_height |
| 3019 | ? attr_thumb : attr_scroll); |
| 3020 | else |
| 3021 | screen_putchar(' ', row, scroll_col, popup_attr); |
| 3022 | } |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3023 | // right border |
| 3024 | if (wp->w_popup_border[1] > 0) |
| 3025 | { |
| 3026 | buf[mb_char2bytes(border_char[1], buf)] = NUL; |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 3027 | screen_puts(buf, row, wincol + total_width - 1, border_attr[1]); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3028 | } |
| 3029 | // right padding |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 3030 | if (do_padding && wp->w_popup_padding[1] > 0) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3031 | screen_puts(get_spaces(wp->w_popup_padding[1]), row, |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 3032 | wincol + wp->w_popup_border[3] |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 3033 | + wp->w_popup_padding[3] + wp->w_width + wp->w_leftcol, |
| 3034 | popup_attr); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | if (wp->w_popup_padding[2] > 0) |
| 3038 | { |
| 3039 | // bottom padding |
| 3040 | row = wp->w_winrow + wp->w_popup_border[0] |
| 3041 | + wp->w_popup_padding[0] + wp->w_height; |
| 3042 | screen_fill(row, row + wp->w_popup_padding[2], |
Bram Moolenaar | d529ba5 | 2019-07-02 23:13:53 +0200 | [diff] [blame] | 3043 | padcol, padwidth, ' ', ' ', popup_attr); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | if (wp->w_popup_border[2] > 0) |
| 3047 | { |
| 3048 | // bottom border |
| 3049 | row = wp->w_winrow + total_height - 1; |
| 3050 | screen_fill(row , row + 1, |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 3051 | wincol < 0 ? 0 : wincol, |
| 3052 | wincol + total_width, |
| 3053 | wp->w_popup_border[3] != 0 && wp->w_popup_leftoff == 0 |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3054 | ? border_char[7] : border_char[2], |
| 3055 | border_char[2], border_attr[2]); |
| 3056 | if (wp->w_popup_border[1] > 0) |
| 3057 | { |
| 3058 | buf[mb_char2bytes(border_char[6], buf)] = NUL; |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 3059 | screen_puts(buf, row, wincol + total_width - 1, border_attr[2]); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3060 | } |
| 3061 | } |
| 3062 | |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 3063 | if (wp->w_popup_close == POPCLOSE_BUTTON) |
| 3064 | { |
| 3065 | // close button goes on top of anything at the top-right corner |
| 3066 | buf[mb_char2bytes('X', buf)] = NUL; |
Bram Moolenaar | ba45f1f | 2019-07-03 22:50:41 +0200 | [diff] [blame] | 3067 | screen_puts(buf, wp->w_winrow, wincol + total_width - 1, |
Bram Moolenaar | 2e62b56 | 2019-06-30 18:07:00 +0200 | [diff] [blame] | 3068 | wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr); |
| 3069 | } |
| 3070 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 3071 | update_popup_transparent(wp, 0); |
| 3072 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 3073 | // Back to the normal zindex. |
| 3074 | screen_zindex = 0; |
| 3075 | } |
| 3076 | } |
| 3077 | |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 3078 | /* |
| 3079 | * Mark references in callbacks of one popup window. |
| 3080 | */ |
| 3081 | static int |
| 3082 | set_ref_in_one_popup(win_T *wp, int copyID) |
| 3083 | { |
| 3084 | int abort = FALSE; |
| 3085 | typval_T tv; |
| 3086 | |
| 3087 | if (wp->w_close_cb.cb_partial != NULL) |
| 3088 | { |
| 3089 | tv.v_type = VAR_PARTIAL; |
| 3090 | tv.vval.v_partial = wp->w_close_cb.cb_partial; |
| 3091 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 3092 | } |
| 3093 | if (wp->w_filter_cb.cb_partial != NULL) |
| 3094 | { |
| 3095 | tv.v_type = VAR_PARTIAL; |
| 3096 | tv.vval.v_partial = wp->w_filter_cb.cb_partial; |
| 3097 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 3098 | } |
Bram Moolenaar | 7be3ab2 | 2019-06-23 01:46:15 +0200 | [diff] [blame] | 3099 | abort = abort || set_ref_in_list(wp->w_popup_mask, copyID); |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 3100 | return abort; |
| 3101 | } |
| 3102 | |
| 3103 | /* |
| 3104 | * Set reference in callbacks of popup windows. |
| 3105 | */ |
| 3106 | int |
| 3107 | set_ref_in_popups(int copyID) |
| 3108 | { |
| 3109 | int abort = FALSE; |
| 3110 | win_T *wp; |
| 3111 | tabpage_T *tp; |
| 3112 | |
| 3113 | for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next) |
| 3114 | abort = abort || set_ref_in_one_popup(wp, copyID); |
| 3115 | |
| 3116 | FOR_ALL_TABPAGES(tp) |
| 3117 | { |
| 3118 | for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next) |
| 3119 | abort = abort || set_ref_in_one_popup(wp, copyID); |
| 3120 | if (abort) |
| 3121 | break; |
| 3122 | } |
| 3123 | return abort; |
| 3124 | } |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 3125 | |
| 3126 | /* |
| 3127 | * Find an existing popup used as the preview window, in the current tab page. |
| 3128 | * Return NULL if not found. |
| 3129 | */ |
| 3130 | win_T * |
| 3131 | popup_find_preview_window(void) |
| 3132 | { |
| 3133 | win_T *wp; |
| 3134 | |
| 3135 | // Preview window popup is always local to tab page. |
| 3136 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 3137 | if (wp->w_p_pvw) |
| 3138 | return wp; |
Bram Moolenaar | 56c0c47 | 2019-07-28 17:57:43 +0200 | [diff] [blame] | 3139 | return NULL; |
| 3140 | } |
| 3141 | |
| 3142 | void |
| 3143 | f_popup_getpreview(typval_T *argvars UNUSED, typval_T *rettv) |
| 3144 | { |
| 3145 | win_T *wp = popup_find_preview_window(); |
| 3146 | |
| 3147 | rettv->vval.v_number = wp == NULL ? 0 : wp->w_id; |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | int |
| 3151 | popup_is_popup(win_T *wp) |
| 3152 | { |
| 3153 | return wp->w_popup_flags != 0; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Create a popup to be used as the preview window. |
| 3158 | * NOTE: this makes the popup the current window, so that the file can be |
| 3159 | * edited. However, it must not remain to be the current window, the caller |
| 3160 | * must make sure of that. |
| 3161 | */ |
| 3162 | int |
| 3163 | popup_create_preview_window(void) |
| 3164 | { |
| 3165 | win_T *wp = popup_create(NULL, NULL, TYPE_PREVIEW); |
| 3166 | |
| 3167 | if (wp == NULL) |
| 3168 | return FAIL; |
| 3169 | wp->w_p_pvw = TRUE; |
| 3170 | |
| 3171 | // Set the width to a reasonable value, so that w_topline can be computed. |
| 3172 | if (wp->w_minwidth > 0) |
| 3173 | wp->w_width = wp->w_minwidth; |
| 3174 | else if (wp->w_maxwidth > 0) |
| 3175 | wp->w_width = wp->w_maxwidth; |
| 3176 | else |
| 3177 | wp->w_width = curwin->w_width; |
| 3178 | |
| 3179 | // Will switch to another buffer soon, dummy one can be wiped. |
| 3180 | wp->w_buffer->b_locked = FALSE; |
| 3181 | |
| 3182 | win_enter(wp, FALSE); |
| 3183 | return OK; |
| 3184 | } |
| 3185 | |
| 3186 | void |
| 3187 | popup_close_preview() |
| 3188 | { |
| 3189 | win_T *wp = popup_find_preview_window(); |
| 3190 | |
| 3191 | if (wp != NULL) |
| 3192 | { |
| 3193 | typval_T res; |
| 3194 | |
| 3195 | res.v_type = VAR_NUMBER; |
| 3196 | res.vval.v_number = -1; |
| 3197 | popup_close_and_callback(wp, &res); |
| 3198 | } |
| 3199 | } |
| 3200 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 3201 | #endif // FEAT_TEXT_PROP |