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 | |
| 16 | #ifdef FEAT_TEXT_PROP |
| 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 | /* |
| 171 | * Return TRUE if "row"/"col" is on the border of the popup. |
| 172 | * The values are relative to the top-left corner. |
| 173 | */ |
| 174 | int |
| 175 | popup_on_border(win_T *wp, int row, int col) |
| 176 | { |
| 177 | return (row == 0 && wp->w_popup_border[0] > 0) |
| 178 | || (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0) |
| 179 | || (col == 0 && wp->w_popup_border[3] > 0) |
| 180 | || (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0); |
| 181 | } |
| 182 | |
| 183 | // Values set when dragging a popup window starts. |
| 184 | static int drag_start_row; |
| 185 | static int drag_start_col; |
| 186 | static int drag_start_wantline; |
| 187 | static int drag_start_wantcol; |
| 188 | |
| 189 | /* |
| 190 | * Mouse down on border of popup window: start dragging it. |
| 191 | * Uses mouse_col and mouse_row. |
| 192 | */ |
| 193 | void |
| 194 | popup_start_drag(win_T *wp) |
| 195 | { |
| 196 | drag_start_row = mouse_row; |
| 197 | drag_start_col = mouse_col; |
| 198 | // TODO: handle using different corner |
| 199 | if (wp->w_wantline == 0) |
| 200 | drag_start_wantline = wp->w_winrow + 1; |
| 201 | else |
| 202 | drag_start_wantline = wp->w_wantline; |
| 203 | if (wp->w_wantcol == 0) |
| 204 | drag_start_wantcol = wp->w_wincol + 1; |
| 205 | else |
| 206 | drag_start_wantcol = wp->w_wantcol; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 207 | |
| 208 | // Stop centering the popup |
| 209 | if (wp->w_popup_pos == POPPOS_CENTER) |
| 210 | wp->w_popup_pos = POPPOS_TOPLEFT; |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Mouse moved while dragging a popup window: adjust the window popup position. |
| 215 | */ |
| 216 | void |
| 217 | popup_drag(win_T *wp) |
| 218 | { |
| 219 | // The popup may be closed before dragging stops. |
| 220 | if (!win_valid_popup(wp)) |
| 221 | return; |
| 222 | |
| 223 | wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row); |
| 224 | if (wp->w_wantline < 1) |
| 225 | wp->w_wantline = 1; |
| 226 | if (wp->w_wantline > Rows) |
| 227 | wp->w_wantline = Rows; |
| 228 | wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col); |
| 229 | if (wp->w_wantcol < 1) |
| 230 | wp->w_wantcol = 1; |
| 231 | if (wp->w_wantcol > Columns) |
| 232 | wp->w_wantcol = Columns; |
| 233 | |
| 234 | popup_adjust_position(wp); |
| 235 | } |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 236 | |
Bram Moolenaar | f9c85f5 | 2019-06-29 07:41:35 +0200 | [diff] [blame] | 237 | /* |
| 238 | * Set w_firstline to match the current "wp->w_topline". |
| 239 | */ |
| 240 | void |
| 241 | popup_set_firstline(win_T *wp) |
| 242 | { |
| 243 | int height = wp->w_height; |
| 244 | |
| 245 | wp->w_firstline = wp->w_topline; |
| 246 | popup_adjust_position(wp); |
| 247 | |
| 248 | // we don't want the popup to get smaller, decrement the first line |
| 249 | // until it doesn't |
| 250 | while (wp->w_firstline > 1 && wp->w_height < height) |
| 251 | { |
| 252 | --wp->w_firstline; |
| 253 | popup_adjust_position(wp); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Handle a click in a popup window, if it is in the scrollbar. |
| 259 | */ |
| 260 | void |
| 261 | popup_handle_scrollbar_click(win_T *wp, int row, int col) |
| 262 | { |
| 263 | int height = popup_height(wp); |
| 264 | int old_topline = wp->w_topline; |
| 265 | |
| 266 | if (wp->w_has_scrollbar == 0) |
| 267 | return; |
| 268 | if (row >= wp->w_popup_border[0] |
| 269 | && row < height - wp->w_popup_border[2] |
| 270 | && col == popup_width(wp) - 1) |
| 271 | { |
| 272 | if (row >= height / 2) |
| 273 | { |
| 274 | // Click in lower half, scroll down. |
| 275 | if (wp->w_topline < wp->w_buffer->b_ml.ml_line_count) |
| 276 | ++wp->w_topline; |
| 277 | } |
| 278 | else if (wp->w_topline > 1) |
| 279 | // click on upper half, scroll up. |
| 280 | --wp->w_topline; |
| 281 | if (wp->w_topline != old_topline) |
| 282 | { |
| 283 | popup_set_firstline(wp); |
| 284 | redraw_win_later(wp, NOT_VALID); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 289 | #if defined(FEAT_TIMERS) |
| 290 | static void |
| 291 | popup_add_timeout(win_T *wp, int time) |
| 292 | { |
| 293 | char_u cbbuf[50]; |
| 294 | char_u *ptr = cbbuf; |
| 295 | typval_T tv; |
| 296 | |
| 297 | vim_snprintf((char *)cbbuf, sizeof(cbbuf), |
| 298 | "{_ -> popup_close(%d)}", wp->w_id); |
| 299 | if (get_lambda_tv(&ptr, &tv, TRUE) == OK) |
| 300 | { |
| 301 | wp->w_popup_timer = create_timer(time, 0); |
| 302 | wp->w_popup_timer->tr_callback = get_callback(&tv); |
| 303 | clear_tv(&tv); |
| 304 | } |
| 305 | } |
| 306 | #endif |
| 307 | |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 308 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 309 | * Shared between popup_create() and f_popup_move(). |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 310 | */ |
| 311 | static void |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 312 | apply_move_options(win_T *wp, dict_T *d) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 313 | { |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 314 | int nr; |
| 315 | |
| 316 | if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0) |
| 317 | wp->w_minwidth = nr; |
| 318 | if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0) |
| 319 | wp->w_minheight = nr; |
| 320 | if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0) |
| 321 | wp->w_maxwidth = nr; |
| 322 | if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0) |
| 323 | wp->w_maxheight = nr; |
| 324 | get_pos_options(wp, d); |
| 325 | } |
| 326 | |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 327 | static void |
| 328 | check_highlight(dict_T *dict, char *name, char_u **pval) |
| 329 | { |
| 330 | dictitem_T *di; |
| 331 | char_u *str; |
| 332 | |
| 333 | di = dict_find(dict, (char_u *)name, -1); |
| 334 | if (di != NULL) |
| 335 | { |
| 336 | if (di->di_tv.v_type != VAR_STRING) |
| 337 | semsg(_(e_invargval), name); |
| 338 | else |
| 339 | { |
| 340 | str = tv_get_string(&di->di_tv); |
| 341 | if (*str != NUL) |
| 342 | *pval = vim_strsave(str); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 347 | /* |
| 348 | * Shared between popup_create() and f_popup_setoptions(). |
| 349 | */ |
| 350 | static void |
| 351 | apply_general_options(win_T *wp, dict_T *dict) |
| 352 | { |
| 353 | dictitem_T *di; |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 354 | int nr; |
| 355 | char_u *str; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 356 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 357 | // TODO: flip |
| 358 | |
| 359 | di = dict_find(dict, (char_u *)"firstline", -1); |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 360 | if (di != NULL) |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 361 | wp->w_firstline = dict_get_number(dict, (char_u *)"firstline"); |
| 362 | if (wp->w_firstline < 1) |
| 363 | wp->w_firstline = 1; |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 364 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 365 | di = dict_find(dict, (char_u *)"scrollbar", -1); |
| 366 | if (di != NULL) |
| 367 | wp->w_want_scrollbar = dict_get_number(dict, (char_u *)"scrollbar"); |
| 368 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 369 | str = dict_get_string(dict, (char_u *)"title", FALSE); |
| 370 | if (str != NULL) |
| 371 | { |
| 372 | vim_free(wp->w_popup_title); |
| 373 | wp->w_popup_title = vim_strsave(str); |
| 374 | } |
| 375 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 376 | di = dict_find(dict, (char_u *)"wrap", -1); |
| 377 | if (di != NULL) |
| 378 | { |
| 379 | nr = dict_get_number(dict, (char_u *)"wrap"); |
| 380 | wp->w_p_wrap = nr != 0; |
| 381 | } |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 382 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 383 | di = dict_find(dict, (char_u *)"drag", -1); |
| 384 | if (di != NULL) |
| 385 | wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag"); |
Bram Moolenaar | b53fb31 | 2019-06-13 23:59:52 +0200 | [diff] [blame] | 386 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 387 | str = dict_get_string(dict, (char_u *)"highlight", FALSE); |
| 388 | if (str != NULL) |
| 389 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
| 390 | str, OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 391 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 392 | set_padding_border(dict, wp->w_popup_padding, "padding", 999); |
| 393 | set_padding_border(dict, wp->w_popup_border, "border", 1); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 394 | |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 395 | di = dict_find(dict, (char_u *)"borderhighlight", -1); |
| 396 | if (di != NULL) |
| 397 | { |
| 398 | if (di->di_tv.v_type != VAR_LIST) |
| 399 | emsg(_(e_listreq)); |
| 400 | else |
| 401 | { |
| 402 | list_T *list = di->di_tv.vval.v_list; |
| 403 | listitem_T *li; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 404 | int i; |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 405 | |
| 406 | if (list != NULL) |
| 407 | for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len; |
| 408 | ++i, li = li->li_next) |
| 409 | { |
| 410 | str = tv_get_string(&li->li_tv); |
| 411 | if (*str != NUL) |
| 412 | wp->w_border_highlight[i] = vim_strsave(str); |
| 413 | } |
| 414 | if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL) |
| 415 | for (i = 1; i < 4; ++i) |
| 416 | wp->w_border_highlight[i] = |
| 417 | vim_strsave(wp->w_border_highlight[0]); |
| 418 | } |
| 419 | } |
| 420 | |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 421 | di = dict_find(dict, (char_u *)"borderchars", -1); |
| 422 | if (di != NULL) |
| 423 | { |
| 424 | if (di->di_tv.v_type != VAR_LIST) |
| 425 | emsg(_(e_listreq)); |
| 426 | else |
| 427 | { |
| 428 | list_T *list = di->di_tv.vval.v_list; |
| 429 | listitem_T *li; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 430 | int i; |
Bram Moolenaar | 790498b | 2019-06-01 22:15:29 +0200 | [diff] [blame] | 431 | |
| 432 | if (list != NULL) |
| 433 | for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len; |
| 434 | ++i, li = li->li_next) |
| 435 | { |
| 436 | str = tv_get_string(&li->li_tv); |
| 437 | if (*str != NUL) |
| 438 | wp->w_border_char[i] = mb_ptr2char(str); |
| 439 | } |
| 440 | if (list->lv_len == 1) |
| 441 | for (i = 1; i < 8; ++i) |
| 442 | wp->w_border_char[i] = wp->w_border_char[0]; |
| 443 | if (list->lv_len == 2) |
| 444 | { |
| 445 | for (i = 4; i < 8; ++i) |
| 446 | wp->w_border_char[i] = wp->w_border_char[1]; |
| 447 | for (i = 1; i < 4; ++i) |
| 448 | wp->w_border_char[i] = wp->w_border_char[0]; |
| 449 | } |
| 450 | } |
| 451 | } |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 452 | |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 453 | check_highlight(dict, "scrollbarhighlight", &wp->w_scrollbar_highlight); |
| 454 | check_highlight(dict, "thumbhighlight", &wp->w_thumb_highlight); |
| 455 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 456 | di = dict_find(dict, (char_u *)"zindex", -1); |
| 457 | if (di != NULL) |
| 458 | { |
| 459 | wp->w_zindex = dict_get_number(dict, (char_u *)"zindex"); |
| 460 | if (wp->w_zindex < 1) |
| 461 | wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX; |
| 462 | if (wp->w_zindex > 32000) |
| 463 | wp->w_zindex = 32000; |
| 464 | } |
| 465 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 466 | di = dict_find(dict, (char_u *)"mask", -1); |
| 467 | if (di != NULL) |
| 468 | { |
| 469 | int ok = TRUE; |
| 470 | |
| 471 | if (di->di_tv.v_type != VAR_LIST) |
| 472 | ok = FALSE; |
| 473 | else if (di->di_tv.vval.v_list != NULL) |
| 474 | { |
| 475 | listitem_T *li; |
| 476 | |
| 477 | for (li = di->di_tv.vval.v_list->lv_first; li != NULL; |
| 478 | li = li->li_next) |
| 479 | { |
| 480 | if (li->li_tv.v_type != VAR_LIST |
| 481 | || li->li_tv.vval.v_list == NULL |
| 482 | || li->li_tv.vval.v_list->lv_len != 4) |
| 483 | { |
| 484 | ok = FALSE; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | if (ok) |
| 490 | { |
| 491 | wp->w_popup_mask = di->di_tv.vval.v_list; |
| 492 | ++wp->w_popup_mask->lv_refcount; |
| 493 | } |
| 494 | else |
| 495 | semsg(_(e_invargval), "mask"); |
| 496 | } |
| 497 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 498 | #if defined(FEAT_TIMERS) |
| 499 | // Add timer to close the popup after some time. |
| 500 | nr = dict_get_number(dict, (char_u *)"time"); |
| 501 | if (nr > 0) |
| 502 | popup_add_timeout(wp, nr); |
| 503 | #endif |
| 504 | |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 505 | di = dict_find(dict, (char_u *)"moved", -1); |
| 506 | if (di != NULL) |
| 507 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 508 | set_moved_values(wp); |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 509 | if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL) |
| 510 | { |
| 511 | char_u *s = di->di_tv.vval.v_string; |
| 512 | int flags = 0; |
| 513 | |
| 514 | if (STRCMP(s, "word") == 0) |
| 515 | flags = FIND_IDENT | FIND_STRING; |
| 516 | else if (STRCMP(s, "WORD") == 0) |
| 517 | flags = FIND_STRING; |
| 518 | else if (STRCMP(s, "any") != 0) |
| 519 | semsg(_(e_invarg2), s); |
| 520 | if (flags != 0) |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 521 | set_moved_columns(wp, flags); |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 522 | } |
| 523 | else if (di->di_tv.v_type == VAR_LIST |
| 524 | && di->di_tv.vval.v_list != NULL |
| 525 | && di->di_tv.vval.v_list->lv_len == 2) |
| 526 | { |
| 527 | list_T *l = di->di_tv.vval.v_list; |
| 528 | |
| 529 | wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv); |
| 530 | wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv); |
| 531 | } |
| 532 | else |
| 533 | semsg(_(e_invarg2), tv_get_string(&di->di_tv)); |
| 534 | } |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 535 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 536 | di = dict_find(dict, (char_u *)"filter", -1); |
| 537 | if (di != NULL) |
| 538 | { |
| 539 | callback_T callback = get_callback(&di->di_tv); |
| 540 | |
| 541 | if (callback.cb_name != NULL) |
| 542 | { |
| 543 | free_callback(&wp->w_filter_cb); |
| 544 | set_callback(&wp->w_filter_cb, &callback); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | di = dict_find(dict, (char_u *)"callback", -1); |
| 549 | if (di != NULL) |
| 550 | { |
| 551 | callback_T callback = get_callback(&di->di_tv); |
| 552 | |
| 553 | if (callback.cb_name != NULL) |
| 554 | { |
| 555 | free_callback(&wp->w_close_cb); |
| 556 | set_callback(&wp->w_close_cb, &callback); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Go through the options in "dict" and apply them to popup window "wp". |
| 563 | */ |
| 564 | static void |
| 565 | apply_options(win_T *wp, dict_T *dict) |
| 566 | { |
| 567 | int nr; |
| 568 | |
| 569 | apply_move_options(wp, dict); |
| 570 | apply_general_options(wp, dict); |
| 571 | |
Bram Moolenaar | 6313c4f | 2019-06-16 20:39:13 +0200 | [diff] [blame] | 572 | nr = dict_get_number(dict, (char_u *)"hidden"); |
| 573 | if (nr > 0) |
| 574 | { |
| 575 | wp->w_popup_flags |= POPF_HIDDEN; |
| 576 | --wp->w_buffer->b_nwindows; |
| 577 | } |
| 578 | |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 579 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 583 | * Add lines to the popup from a list of strings. |
| 584 | */ |
| 585 | static void |
| 586 | add_popup_strings(buf_T *buf, list_T *l) |
| 587 | { |
| 588 | listitem_T *li; |
| 589 | linenr_T lnum = 0; |
| 590 | char_u *p; |
| 591 | |
| 592 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 593 | if (li->li_tv.v_type == VAR_STRING) |
| 594 | { |
| 595 | p = li->li_tv.vval.v_string; |
| 596 | ml_append_buf(buf, lnum++, |
| 597 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Add lines to the popup from a list of dictionaries. |
| 603 | */ |
| 604 | static void |
| 605 | add_popup_dicts(buf_T *buf, list_T *l) |
| 606 | { |
| 607 | listitem_T *li; |
| 608 | listitem_T *pli; |
| 609 | linenr_T lnum = 0; |
| 610 | char_u *p; |
| 611 | dict_T *dict; |
| 612 | |
| 613 | // first add the text lines |
| 614 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 615 | { |
| 616 | if (li->li_tv.v_type != VAR_DICT) |
| 617 | { |
| 618 | emsg(_(e_dictreq)); |
| 619 | return; |
| 620 | } |
| 621 | dict = li->li_tv.vval.v_dict; |
| 622 | p = dict == NULL ? NULL |
| 623 | : dict_get_string(dict, (char_u *)"text", FALSE); |
| 624 | ml_append_buf(buf, lnum++, |
| 625 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 626 | } |
| 627 | |
| 628 | // add the text properties |
| 629 | lnum = 1; |
| 630 | for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum) |
| 631 | { |
| 632 | dictitem_T *di; |
| 633 | list_T *plist; |
| 634 | |
| 635 | dict = li->li_tv.vval.v_dict; |
| 636 | di = dict_find(dict, (char_u *)"props", -1); |
| 637 | if (di != NULL) |
| 638 | { |
| 639 | if (di->di_tv.v_type != VAR_LIST) |
| 640 | { |
| 641 | emsg(_(e_listreq)); |
| 642 | return; |
| 643 | } |
| 644 | plist = di->di_tv.vval.v_list; |
| 645 | if (plist != NULL) |
| 646 | { |
| 647 | for (pli = plist->lv_first; pli != NULL; pli = pli->li_next) |
| 648 | { |
| 649 | if (pli->li_tv.v_type != VAR_DICT) |
| 650 | { |
| 651 | emsg(_(e_dictreq)); |
| 652 | return; |
| 653 | } |
| 654 | dict = pli->li_tv.vval.v_dict; |
| 655 | if (dict != NULL) |
| 656 | { |
| 657 | int col = dict_get_number(dict, (char_u *)"col"); |
| 658 | |
| 659 | prop_add_common( lnum, col, dict, buf, NULL); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /* |
Bram Moolenaar | 451d4b5 | 2019-06-12 20:22:27 +0200 | [diff] [blame] | 668 | * Return the height of popup window "wp", including border and padding. |
| 669 | */ |
| 670 | int |
| 671 | popup_height(win_T *wp) |
| 672 | { |
| 673 | return wp->w_height |
| 674 | + wp->w_popup_padding[0] + wp->w_popup_border[0] |
| 675 | + wp->w_popup_padding[2] + wp->w_popup_border[2]; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Return the width of popup window "wp", including border and padding. |
| 680 | */ |
| 681 | int |
| 682 | popup_width(win_T *wp) |
| 683 | { |
| 684 | return wp->w_width |
| 685 | + wp->w_popup_padding[3] + wp->w_popup_border[3] |
Bram Moolenaar | f9c85f5 | 2019-06-29 07:41:35 +0200 | [diff] [blame] | 686 | + wp->w_popup_padding[1] + wp->w_popup_border[1] |
| 687 | + wp->w_has_scrollbar; |
Bram Moolenaar | 451d4b5 | 2019-06-12 20:22:27 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 691 | * Get the padding plus border at the top, adjusted to 1 if there is a title. |
| 692 | */ |
| 693 | static int |
| 694 | popup_top_extra(win_T *wp) |
| 695 | { |
| 696 | int extra = wp->w_popup_border[0] + wp->w_popup_padding[0]; |
| 697 | |
| 698 | if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL) |
| 699 | return 1; |
| 700 | return extra; |
| 701 | } |
| 702 | |
| 703 | /* |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 704 | * Adjust the position and size of the popup to fit on the screen. |
| 705 | */ |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 706 | void |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 707 | popup_adjust_position(win_T *wp) |
| 708 | { |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 709 | linenr_T lnum; |
| 710 | int wrapped = 0; |
| 711 | int maxwidth; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 712 | int center_vert = FALSE; |
| 713 | int center_hor = FALSE; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 714 | int allow_adjust_left = !wp->w_popup_fixed; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 715 | int top_extra = popup_top_extra(wp); |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 716 | int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1]; |
| 717 | int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2]; |
| 718 | int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3]; |
| 719 | int extra_height = top_extra + bot_extra; |
| 720 | int extra_width = left_extra + right_extra; |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 721 | int org_winrow = wp->w_winrow; |
| 722 | int org_wincol = wp->w_wincol; |
| 723 | int org_width = wp->w_width; |
| 724 | int org_height = wp->w_height; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 725 | int org_leftcol = wp->w_leftcol; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 726 | int minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 727 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 728 | wp->w_winrow = 0; |
| 729 | wp->w_wincol = 0; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 730 | wp->w_leftcol = 0; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 731 | if (wp->w_popup_pos == POPPOS_CENTER) |
| 732 | { |
| 733 | // center after computing the size |
| 734 | center_vert = TRUE; |
| 735 | center_hor = TRUE; |
| 736 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 737 | else |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 738 | { |
| 739 | if (wp->w_wantline == 0) |
| 740 | center_vert = TRUE; |
| 741 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 742 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 743 | { |
| 744 | wp->w_winrow = wp->w_wantline - 1; |
| 745 | if (wp->w_winrow >= Rows) |
| 746 | wp->w_winrow = Rows - 1; |
| 747 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 748 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 749 | if (wp->w_wantcol == 0) |
| 750 | center_hor = TRUE; |
| 751 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 752 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 753 | { |
| 754 | wp->w_wincol = wp->w_wantcol - 1; |
| 755 | if (wp->w_wincol >= Columns - 3) |
| 756 | wp->w_wincol = Columns - 3; |
| 757 | } |
| 758 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 759 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 760 | // When centering or right aligned, use maximum width. |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 761 | // When left aligned use the space available, but shift to the left when we |
| 762 | // hit the right of the screen. |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 763 | maxwidth = Columns - wp->w_wincol - left_extra; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 764 | if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth) |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 765 | { |
| 766 | allow_adjust_left = FALSE; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 767 | maxwidth = wp->w_maxwidth; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 768 | } |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 769 | |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 770 | // start at the desired first line |
| 771 | wp->w_topline = wp->w_firstline; |
| 772 | if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count) |
| 773 | wp->w_topline = wp->w_buffer->b_ml.ml_line_count; |
| 774 | |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 775 | // Compute width based on longest text line and the 'wrap' option. |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 776 | // Use a minimum width of one, so that something shows when there is no |
| 777 | // text. |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 778 | // TODO: more accurate wrapping |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 779 | wp->w_width = 1; |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 780 | 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] | 781 | { |
| 782 | int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE)); |
| 783 | |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 784 | if (wp->w_p_wrap) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 785 | { |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 786 | while (len > maxwidth) |
| 787 | { |
| 788 | ++wrapped; |
| 789 | len -= maxwidth; |
| 790 | wp->w_width = maxwidth; |
| 791 | } |
| 792 | } |
| 793 | else if (len > maxwidth |
| 794 | && allow_adjust_left |
| 795 | && (wp->w_popup_pos == POPPOS_TOPLEFT |
| 796 | || wp->w_popup_pos == POPPOS_BOTLEFT)) |
| 797 | { |
| 798 | // adjust leftwise to fit text on screen |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 799 | int shift_by = len - maxwidth; |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 800 | |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 801 | if (shift_by > wp->w_wincol) |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 802 | { |
| 803 | int truncate_shift = shift_by - wp->w_wincol; |
Bram Moolenaar | 51c3131 | 2019-06-15 22:27:23 +0200 | [diff] [blame] | 804 | |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 805 | len -= truncate_shift; |
| 806 | shift_by -= truncate_shift; |
| 807 | } |
| 808 | |
| 809 | wp->w_wincol -= shift_by; |
| 810 | maxwidth += shift_by; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 811 | wp->w_width = maxwidth; |
| 812 | } |
| 813 | if (wp->w_width < len) |
| 814 | wp->w_width = len; |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 815 | // do not use the width of lines we're not going to show |
| 816 | if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count |
| 817 | - wp->w_topline + 1 + wrapped > wp->w_maxheight) |
| 818 | break; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 821 | wp->w_has_scrollbar = wp->w_want_scrollbar |
| 822 | && (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count); |
| 823 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 824 | minwidth = wp->w_minwidth; |
| 825 | if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL) |
| 826 | { |
| 827 | int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width; |
| 828 | |
| 829 | if (minwidth < title_len) |
| 830 | minwidth = title_len; |
| 831 | } |
| 832 | |
| 833 | if (minwidth > 0 && wp->w_width < minwidth) |
| 834 | wp->w_width = minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 835 | if (wp->w_width > maxwidth) |
| 836 | wp->w_width = maxwidth; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 837 | if (center_hor) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 838 | { |
| 839 | wp->w_wincol = (Columns - wp->w_width - extra_width) / 2; |
| 840 | if (wp->w_wincol < 0) |
| 841 | wp->w_wincol = 0; |
| 842 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 843 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 844 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 845 | { |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 846 | int leftoff = wp->w_wantcol - (wp->w_width + extra_width); |
| 847 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 848 | // Right aligned: move to the right if needed. |
| 849 | // No truncation, because that would change the height. |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 850 | if (leftoff >= 0) |
| 851 | wp->w_wincol = leftoff; |
| 852 | else if (wp->w_popup_fixed) |
| 853 | { |
| 854 | // "col" specifies the right edge, but popup doesn't fit, skip some |
| 855 | // columns when displaying the window. |
| 856 | wp->w_leftcol = -leftoff; |
| 857 | wp->w_width += leftoff; |
| 858 | if (wp->w_width < 0) |
| 859 | wp->w_width = 0; |
| 860 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 861 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 862 | |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 863 | wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline |
| 864 | + 1 + wrapped; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 865 | if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight) |
| 866 | wp->w_height = wp->w_minheight; |
| 867 | if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight) |
| 868 | wp->w_height = wp->w_maxheight; |
| 869 | if (wp->w_height > Rows - wp->w_winrow) |
| 870 | wp->w_height = Rows - wp->w_winrow; |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 871 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 872 | if (center_vert) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 873 | { |
| 874 | wp->w_winrow = (Rows - wp->w_height - extra_height) / 2; |
| 875 | if (wp->w_winrow < 0) |
| 876 | wp->w_winrow = 0; |
| 877 | } |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 878 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 879 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 880 | { |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 881 | if ((wp->w_height + extra_height) <= wp->w_wantline) |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 882 | // bottom aligned: may move down |
Bram Moolenaar | 399d898 | 2019-06-02 15:34:29 +0200 | [diff] [blame] | 883 | wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 884 | else |
| 885 | // not enough space, make top aligned |
| 886 | wp->w_winrow = wp->w_wantline + 1; |
| 887 | } |
| 888 | |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 889 | wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 890 | |
| 891 | // Need to update popup_mask if the position or size changed. |
Bram Moolenaar | acc682b | 2019-06-08 17:15:51 +0200 | [diff] [blame] | 892 | // And redraw windows that were behind the popup. |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 893 | if (org_winrow != wp->w_winrow |
| 894 | || org_wincol != wp->w_wincol |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 895 | || org_leftcol != wp->w_leftcol |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 896 | || org_width != wp->w_width |
| 897 | || org_height != wp->w_height) |
| 898 | { |
Bram Moolenaar | 4c063a0 | 2019-06-10 21:24:12 +0200 | [diff] [blame] | 899 | redraw_all_later(VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 900 | popup_mask_refresh = TRUE; |
| 901 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 902 | } |
| 903 | |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 904 | typedef enum |
| 905 | { |
| 906 | TYPE_NORMAL, |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 907 | TYPE_ATCURSOR, |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 908 | TYPE_NOTIFICATION, |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 909 | TYPE_DIALOG, |
| 910 | TYPE_MENU |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 911 | } create_type_T; |
| 912 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 913 | /* |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 914 | * Make "buf" empty and set the contents to "text". |
| 915 | * Used by popup_create() and popup_settext(). |
| 916 | */ |
| 917 | static void |
| 918 | popup_set_buffer_text(buf_T *buf, typval_T text) |
| 919 | { |
| 920 | int lnum; |
| 921 | |
| 922 | // Clear the buffer, then replace the lines. |
| 923 | curbuf = buf; |
| 924 | for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 925 | ml_delete(lnum, FALSE); |
| 926 | curbuf = curwin->w_buffer; |
| 927 | |
| 928 | // Add text to the buffer. |
| 929 | if (text.v_type == VAR_STRING) |
| 930 | { |
| 931 | // just a string |
| 932 | ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE); |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | list_T *l = text.vval.v_list; |
| 937 | |
| 938 | if (l->lv_len > 0) |
| 939 | { |
| 940 | if (l->lv_first->li_tv.v_type == VAR_STRING) |
| 941 | // list of strings |
| 942 | add_popup_strings(buf, l); |
| 943 | else |
| 944 | // list of dictionaries |
| 945 | add_popup_dicts(buf, l); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | // delete the line that was in the empty buffer |
| 950 | curbuf = buf; |
| 951 | ml_delete(buf->b_ml.ml_line_count, FALSE); |
| 952 | curbuf = curwin->w_buffer; |
| 953 | } |
| 954 | |
| 955 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 956 | * popup_create({text}, {options}) |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 957 | * popup_atcursor({text}, {options}) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 958 | */ |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 959 | static win_T * |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 960 | popup_create(typval_T *argvars, typval_T *rettv, create_type_T type) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 961 | { |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 962 | win_T *wp; |
| 963 | tabpage_T *tp = NULL; |
| 964 | int tabnr; |
| 965 | buf_T *buf; |
| 966 | dict_T *d; |
| 967 | int nr; |
| 968 | int i; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 969 | |
| 970 | // Check arguments look OK. |
Bram Moolenaar | 7b29dd8 | 2019-06-02 13:22:11 +0200 | [diff] [blame] | 971 | if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL) |
| 972 | && !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL)) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 973 | { |
| 974 | emsg(_(e_listreq)); |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 975 | return NULL; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 976 | } |
| 977 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 978 | { |
| 979 | emsg(_(e_dictreq)); |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 980 | return NULL; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 981 | } |
| 982 | d = argvars[1].vval.v_dict; |
| 983 | |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 984 | if (dict_find(d, (char_u *)"tabpage", -1) != NULL) |
| 985 | tabnr = (int)dict_get_number(d, (char_u *)"tabpage"); |
| 986 | else if (type == TYPE_NOTIFICATION) |
| 987 | tabnr = -1; // notifications are global by default |
| 988 | else |
| 989 | tabnr = 0; |
| 990 | if (tabnr > 0) |
| 991 | { |
| 992 | tp = find_tabpage(tabnr); |
| 993 | if (tp == NULL) |
| 994 | { |
Bram Moolenaar | b2cda0d | 2019-06-24 05:06:36 +0200 | [diff] [blame] | 995 | semsg(_("E997: Tabpage not found: %d"), tabnr); |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 996 | return NULL; |
| 997 | } |
| 998 | } |
| 999 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1000 | // Create the window and buffer. |
| 1001 | wp = win_alloc_popup_win(); |
| 1002 | if (wp == NULL) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1003 | return NULL; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1004 | rettv->vval.v_number = wp->w_id; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1005 | wp->w_popup_pos = POPPOS_TOPLEFT; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1006 | |
| 1007 | buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY); |
| 1008 | if (buf == NULL) |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1009 | return NULL; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1010 | ml_open(buf); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 1011 | |
| 1012 | win_init_popup_win(wp, buf); |
| 1013 | |
| 1014 | set_local_options_default(wp); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 1015 | set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1016 | (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 1017 | set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1018 | (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1019 | buf->b_p_ul = -1; // no undo |
| 1020 | buf->b_p_swf = FALSE; // no swap file |
| 1021 | buf->b_p_bl = FALSE; // unlisted buffer |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 1022 | buf->b_locked = TRUE; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1023 | wp->w_p_wrap = TRUE; // 'wrap' is default on |
| 1024 | |
Bram Moolenaar | 54fabd4 | 2019-05-30 19:03:22 +0200 | [diff] [blame] | 1025 | // Avoid that 'buftype' is reset when this buffer is entered. |
| 1026 | buf->b_p_initialized = TRUE; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1027 | |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1028 | if (tp != NULL) |
| 1029 | { |
| 1030 | // popup on specified tab page |
| 1031 | wp->w_next = tp->tp_first_popupwin; |
| 1032 | tp->tp_first_popupwin = wp; |
| 1033 | } |
| 1034 | else if (tabnr == 0) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1035 | { |
Bram Moolenaar | fc06cbb | 2019-06-15 14:14:31 +0200 | [diff] [blame] | 1036 | // popup on current tab page |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 1037 | wp->w_next = curtab->tp_first_popupwin; |
| 1038 | curtab->tp_first_popupwin = wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1039 | } |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1040 | else // (tabnr < 0) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1041 | { |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1042 | win_T *prev = first_popupwin; |
| 1043 | |
| 1044 | // Global popup: add at the end, so that it gets displayed on top of |
| 1045 | // older ones with the same zindex. Matters for notifications. |
| 1046 | if (first_popupwin == NULL) |
| 1047 | first_popupwin = wp; |
| 1048 | else |
| 1049 | { |
| 1050 | while (prev->w_next != NULL) |
| 1051 | prev = prev->w_next; |
| 1052 | prev->w_next = wp; |
| 1053 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1054 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1055 | |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1056 | popup_set_buffer_text(buf, argvars[0]); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1057 | |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1058 | if (type == TYPE_ATCURSOR) |
| 1059 | { |
| 1060 | wp->w_popup_pos = POPPOS_BOTLEFT; |
| 1061 | setcursor_mayforce(TRUE); |
| 1062 | wp->w_wantline = screen_screenrow(); |
| 1063 | if (wp->w_wantline == 0) // cursor in first line |
| 1064 | { |
| 1065 | wp->w_wantline = 2; |
| 1066 | wp->w_popup_pos = POPPOS_TOPLEFT; |
| 1067 | } |
| 1068 | wp->w_wantcol = screen_screencol() + 1; |
| 1069 | set_moved_values(wp); |
| 1070 | set_moved_columns(wp, FIND_STRING); |
| 1071 | } |
| 1072 | |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1073 | // set default values |
| 1074 | wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX; |
| 1075 | |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1076 | if (type == TYPE_NOTIFICATION) |
| 1077 | { |
| 1078 | win_T *twp, *nextwin; |
| 1079 | int height = buf->b_ml.ml_line_count + 3; |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1080 | |
| 1081 | // Try to not overlap with another global popup. Guess we need 3 |
| 1082 | // more screen lines than buffer lines. |
| 1083 | wp->w_wantline = 1; |
| 1084 | for (twp = first_popupwin; twp != NULL; twp = nextwin) |
| 1085 | { |
| 1086 | nextwin = twp->w_next; |
| 1087 | if (twp != wp |
| 1088 | && twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX |
| 1089 | && twp->w_winrow <= wp->w_wantline - 1 + height |
| 1090 | && twp->w_winrow + popup_height(twp) > wp->w_wantline - 1) |
| 1091 | { |
| 1092 | // move to below this popup and restart the loop to check for |
| 1093 | // overlap with other popups |
| 1094 | wp->w_wantline = twp->w_winrow + popup_height(twp) + 1; |
| 1095 | nextwin = first_popupwin; |
| 1096 | } |
| 1097 | } |
| 1098 | if (wp->w_wantline + height > Rows) |
| 1099 | { |
| 1100 | // can't avoid overlap, put on top in the hope that message goes |
| 1101 | // away soon. |
| 1102 | wp->w_wantline = 1; |
| 1103 | } |
| 1104 | |
| 1105 | wp->w_wantcol = 10; |
| 1106 | wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX; |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1107 | wp->w_minwidth = 20; |
| 1108 | wp->w_popup_drag = 1; |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1109 | for (i = 0; i < 4; ++i) |
| 1110 | wp->w_popup_border[i] = 1; |
| 1111 | wp->w_popup_padding[1] = 1; |
| 1112 | wp->w_popup_padding[3] = 1; |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1113 | |
| 1114 | nr = syn_name2id((char_u *)"PopupNotification"); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1115 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
Bram Moolenaar | dfa97f2 | 2019-06-15 14:31:55 +0200 | [diff] [blame] | 1116 | (char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"), |
| 1117 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1120 | if (type == TYPE_DIALOG || type == TYPE_MENU) |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1121 | { |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1122 | wp->w_popup_pos = POPPOS_CENTER; |
| 1123 | wp->w_zindex = POPUPWIN_DIALOG_ZINDEX; |
| 1124 | wp->w_popup_drag = 1; |
| 1125 | for (i = 0; i < 4; ++i) |
| 1126 | { |
| 1127 | wp->w_popup_border[i] = 1; |
| 1128 | wp->w_popup_padding[i] = 1; |
| 1129 | } |
| 1130 | } |
| 1131 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1132 | if (type == TYPE_MENU) |
| 1133 | { |
| 1134 | typval_T tv; |
| 1135 | callback_T callback; |
| 1136 | |
| 1137 | tv.v_type = VAR_STRING; |
| 1138 | tv.vval.v_string = (char_u *)"popup_filter_menu"; |
| 1139 | callback = get_callback(&tv); |
| 1140 | if (callback.cb_name != NULL) |
| 1141 | set_callback(&wp->w_filter_cb, &callback); |
| 1142 | |
| 1143 | wp->w_p_wrap = 0; |
| 1144 | } |
| 1145 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1146 | for (i = 0; i < 4; ++i) |
| 1147 | VIM_CLEAR(wp->w_border_highlight[i]); |
| 1148 | for (i = 0; i < 8; ++i) |
| 1149 | wp->w_border_char[i] = 0; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1150 | wp->w_want_scrollbar = 1; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1151 | wp->w_popup_fixed = 0; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1152 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1153 | // Deal with options. |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1154 | apply_options(wp, argvars[1].vval.v_dict); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1155 | |
Bram Moolenaar | 0fcf26b | 2019-06-23 01:03:51 +0200 | [diff] [blame] | 1156 | #ifdef FEAT_TIMERS |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1157 | if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL) |
| 1158 | popup_add_timeout(wp, 3000); |
Bram Moolenaar | 0fcf26b | 2019-06-23 01:03:51 +0200 | [diff] [blame] | 1159 | #endif |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1160 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1161 | popup_adjust_position(wp); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1162 | |
| 1163 | wp->w_vsep_width = 0; |
| 1164 | |
| 1165 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1166 | popup_mask_refresh = TRUE; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1167 | |
| 1168 | return wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 1172 | * popup_clear() |
| 1173 | */ |
| 1174 | void |
| 1175 | f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 1176 | { |
| 1177 | close_all_popups(); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1181 | * popup_create({text}, {options}) |
| 1182 | */ |
| 1183 | void |
| 1184 | f_popup_create(typval_T *argvars, typval_T *rettv) |
| 1185 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1186 | popup_create(argvars, rettv, TYPE_NORMAL); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * popup_atcursor({text}, {options}) |
| 1191 | */ |
| 1192 | void |
| 1193 | f_popup_atcursor(typval_T *argvars, typval_T *rettv) |
| 1194 | { |
Bram Moolenaar | 1762731 | 2019-06-02 19:53:44 +0200 | [diff] [blame] | 1195 | popup_create(argvars, rettv, TYPE_ATCURSOR); |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1199 | * Invoke the close callback for window "wp" with value "result". |
| 1200 | * Careful: The callback may make "wp" invalid! |
| 1201 | */ |
| 1202 | static void |
| 1203 | invoke_popup_callback(win_T *wp, typval_T *result) |
| 1204 | { |
| 1205 | typval_T rettv; |
| 1206 | int dummy; |
| 1207 | typval_T argv[3]; |
| 1208 | |
| 1209 | argv[0].v_type = VAR_NUMBER; |
| 1210 | argv[0].vval.v_number = (varnumber_T)wp->w_id; |
| 1211 | |
| 1212 | if (result != NULL && result->v_type != VAR_UNKNOWN) |
| 1213 | copy_tv(result, &argv[1]); |
| 1214 | else |
| 1215 | { |
| 1216 | argv[1].v_type = VAR_NUMBER; |
| 1217 | argv[1].vval.v_number = 0; |
| 1218 | } |
| 1219 | |
| 1220 | argv[2].v_type = VAR_UNKNOWN; |
| 1221 | |
| 1222 | call_callback(&wp->w_close_cb, -1, |
| 1223 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL); |
| 1224 | if (result != NULL) |
| 1225 | clear_tv(&argv[1]); |
| 1226 | clear_tv(&rettv); |
| 1227 | } |
| 1228 | |
| 1229 | /* |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 1230 | * Close popup "wp" and invoke any close callback for it. |
| 1231 | */ |
| 1232 | static void |
| 1233 | popup_close_and_callback(win_T *wp, typval_T *arg) |
| 1234 | { |
| 1235 | int id = wp->w_id; |
| 1236 | |
| 1237 | if (wp->w_close_cb.cb_name != NULL) |
| 1238 | // Careful: This may make "wp" invalid. |
| 1239 | invoke_popup_callback(wp, arg); |
| 1240 | |
| 1241 | popup_close(id); |
| 1242 | } |
| 1243 | |
| 1244 | /* |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1245 | * In a filter: check if the typed key is a mouse event that is used for |
| 1246 | * dragging the popup. |
| 1247 | */ |
| 1248 | static void |
| 1249 | filter_handle_drag(win_T *wp, int c, typval_T *rettv) |
| 1250 | { |
| 1251 | int row = mouse_row; |
| 1252 | int col = mouse_col; |
| 1253 | |
| 1254 | if (wp->w_popup_drag |
| 1255 | && is_mouse_key(c) |
| 1256 | && (wp == popup_dragwin |
| 1257 | || wp == mouse_find_win(&row, &col, FIND_POPUP))) |
| 1258 | // do not consume the key, allow for dragging the popup |
| 1259 | rettv->vval.v_number = 0; |
| 1260 | } |
| 1261 | |
| 1262 | static void |
| 1263 | popup_highlight_curline(win_T *wp) |
| 1264 | { |
| 1265 | int id; |
| 1266 | char buf[100]; |
| 1267 | |
| 1268 | match_delete(wp, 1, FALSE); |
| 1269 | |
| 1270 | id = syn_name2id((char_u *)"PopupSelected"); |
| 1271 | vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum); |
| 1272 | match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"), |
| 1273 | (char_u *)buf, 10, 1, NULL, NULL); |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * popup_filter_menu({text}, {options}) |
| 1278 | */ |
| 1279 | void |
| 1280 | f_popup_filter_menu(typval_T *argvars, typval_T *rettv) |
| 1281 | { |
| 1282 | int id = tv_get_number(&argvars[0]); |
| 1283 | win_T *wp = win_id2wp(id); |
| 1284 | char_u *key = tv_get_string(&argvars[1]); |
| 1285 | typval_T res; |
| 1286 | int c; |
| 1287 | linenr_T old_lnum; |
| 1288 | |
| 1289 | // If the popup has been closed do not consume the key. |
| 1290 | if (wp == NULL) |
| 1291 | return; |
| 1292 | |
| 1293 | c = *key; |
| 1294 | if (c == K_SPECIAL && key[1] != NUL) |
| 1295 | c = TO_SPECIAL(key[1], key[2]); |
| 1296 | |
| 1297 | // consume all keys until done |
| 1298 | rettv->vval.v_number = 1; |
| 1299 | res.v_type = VAR_NUMBER; |
| 1300 | |
| 1301 | old_lnum = wp->w_cursor.lnum; |
| 1302 | if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1) |
| 1303 | --wp->w_cursor.lnum; |
| 1304 | if ((c == 'j' || c == 'J' || c == K_DOWN) |
| 1305 | && wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count) |
| 1306 | ++wp->w_cursor.lnum; |
| 1307 | if (old_lnum != wp->w_cursor.lnum) |
| 1308 | { |
| 1309 | popup_highlight_curline(wp); |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C) |
| 1314 | { |
| 1315 | // Cancelled, invoke callback with -1 |
| 1316 | res.vval.v_number = -1; |
| 1317 | popup_close_and_callback(wp, &res); |
| 1318 | return; |
| 1319 | } |
| 1320 | if (c == ' ' || c == K_KENTER || c == CAR || c == NL) |
| 1321 | { |
| 1322 | // Invoke callback with current index. |
| 1323 | res.vval.v_number = wp->w_cursor.lnum; |
| 1324 | popup_close_and_callback(wp, &res); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | filter_handle_drag(wp, c, rettv); |
| 1329 | } |
| 1330 | |
| 1331 | /* |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1332 | * popup_filter_yesno({text}, {options}) |
| 1333 | */ |
| 1334 | void |
| 1335 | f_popup_filter_yesno(typval_T *argvars, typval_T *rettv) |
| 1336 | { |
| 1337 | int id = tv_get_number(&argvars[0]); |
| 1338 | win_T *wp = win_id2wp(id); |
| 1339 | char_u *key = tv_get_string(&argvars[1]); |
| 1340 | typval_T res; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1341 | int c; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1342 | |
| 1343 | // If the popup has been closed don't consume the key. |
| 1344 | if (wp == NULL) |
| 1345 | return; |
| 1346 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1347 | c = *key; |
| 1348 | if (c == K_SPECIAL && key[1] != NUL) |
| 1349 | c = TO_SPECIAL(key[1], key[2]); |
| 1350 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1351 | // consume all keys until done |
| 1352 | rettv->vval.v_number = 1; |
| 1353 | |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1354 | if (c == 'y' || c == 'Y') |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1355 | res.vval.v_number = 1; |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1356 | else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC) |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1357 | res.vval.v_number = 0; |
| 1358 | else |
| 1359 | { |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1360 | filter_handle_drag(wp, c, rettv); |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1361 | return; |
| 1362 | } |
| 1363 | |
| 1364 | // Invoke callback |
| 1365 | res.v_type = VAR_NUMBER; |
| 1366 | popup_close_and_callback(wp, &res); |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * popup_dialog({text}, {options}) |
| 1371 | */ |
| 1372 | void |
| 1373 | f_popup_dialog(typval_T *argvars, typval_T *rettv) |
| 1374 | { |
| 1375 | popup_create(argvars, rettv, TYPE_DIALOG); |
| 1376 | } |
| 1377 | |
| 1378 | /* |
Bram Moolenaar | a730e55 | 2019-06-16 19:05:31 +0200 | [diff] [blame] | 1379 | * popup_menu({text}, {options}) |
| 1380 | */ |
| 1381 | void |
| 1382 | f_popup_menu(typval_T *argvars, typval_T *rettv) |
| 1383 | { |
| 1384 | win_T *wp = popup_create(argvars, rettv, TYPE_MENU); |
| 1385 | |
| 1386 | if (wp != NULL) |
| 1387 | popup_highlight_curline(wp); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1391 | * popup_notification({text}, {options}) |
| 1392 | */ |
| 1393 | void |
| 1394 | f_popup_notification(typval_T *argvars, typval_T *rettv) |
| 1395 | { |
| 1396 | popup_create(argvars, rettv, TYPE_NOTIFICATION); |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * Find the popup window with window-ID "id". |
| 1401 | * If the popup window does not exist NULL is returned. |
| 1402 | * If the window is not a popup window, and error message is given. |
| 1403 | */ |
| 1404 | static win_T * |
| 1405 | find_popup_win(int id) |
| 1406 | { |
| 1407 | win_T *wp = win_id2wp(id); |
| 1408 | |
| 1409 | if (wp != NULL && !bt_popup(wp->w_buffer)) |
| 1410 | { |
| 1411 | semsg(_("E993: window %d is not a popup window"), id); |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | return wp; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1418 | * popup_close({id}) |
| 1419 | */ |
| 1420 | void |
| 1421 | f_popup_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 1422 | { |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1423 | int id = (int)tv_get_number(argvars); |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1424 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1425 | |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1426 | if (wp != NULL) |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 1427 | popup_close_and_callback(wp, &argvars[1]); |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * popup_hide({id}) |
| 1432 | */ |
| 1433 | void |
| 1434 | f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED) |
| 1435 | { |
| 1436 | int id = (int)tv_get_number(argvars); |
| 1437 | win_T *wp = find_popup_win(id); |
| 1438 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1439 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1440 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1441 | wp->w_popup_flags |= POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 1442 | --wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1443 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1444 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * popup_show({id}) |
| 1450 | */ |
| 1451 | void |
| 1452 | f_popup_show(typval_T *argvars, typval_T *rettv UNUSED) |
| 1453 | { |
| 1454 | int id = (int)tv_get_number(argvars); |
| 1455 | win_T *wp = find_popup_win(id); |
| 1456 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1457 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1458 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 1459 | wp->w_popup_flags &= ~POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 1460 | ++wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1461 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1462 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1463 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Bram Moolenaar | dc2ce58 | 2019-06-16 15:32:14 +0200 | [diff] [blame] | 1466 | /* |
| 1467 | * popup_settext({id}, {text}) |
| 1468 | */ |
| 1469 | void |
| 1470 | f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED) |
| 1471 | { |
| 1472 | int id = (int)tv_get_number(&argvars[0]); |
| 1473 | win_T *wp = find_popup_win(id); |
| 1474 | |
| 1475 | if (wp != NULL) |
| 1476 | { |
| 1477 | popup_set_buffer_text(wp->w_buffer, argvars[1]); |
| 1478 | popup_adjust_position(wp); |
| 1479 | } |
| 1480 | } |
| 1481 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1482 | static void |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1483 | popup_free(win_T *wp) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1484 | { |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 1485 | wp->w_buffer->b_locked = FALSE; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1486 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 1487 | clear_cmdline = TRUE; |
| 1488 | win_free_popup(wp); |
| 1489 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1490 | popup_mask_refresh = TRUE; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1493 | /* |
| 1494 | * Close a popup window by Window-id. |
Bram Moolenaar | 9eaac89 | 2019-06-01 22:49:29 +0200 | [diff] [blame] | 1495 | * Does not invoke the callback. |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1496 | */ |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1497 | void |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1498 | popup_close(int id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1499 | { |
| 1500 | win_T *wp; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1501 | tabpage_T *tp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1502 | win_T *prev = NULL; |
| 1503 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1504 | // go through global popups |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1505 | for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next) |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1506 | if (wp->w_id == id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1507 | { |
| 1508 | if (prev == NULL) |
| 1509 | first_popupwin = wp->w_next; |
| 1510 | else |
| 1511 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1512 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1513 | return; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1516 | // go through tab-local popups |
| 1517 | FOR_ALL_TABPAGES(tp) |
| 1518 | popup_close_tabpage(tp, id); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * Close a popup window with Window-id "id" in tabpage "tp". |
| 1523 | */ |
| 1524 | void |
| 1525 | popup_close_tabpage(tabpage_T *tp, int id) |
| 1526 | { |
| 1527 | win_T *wp; |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 1528 | win_T **root = &tp->tp_first_popupwin; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1529 | win_T *prev = NULL; |
| 1530 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1531 | for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next) |
| 1532 | if (wp->w_id == id) |
| 1533 | { |
| 1534 | if (prev == NULL) |
| 1535 | *root = wp->w_next; |
| 1536 | else |
| 1537 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 1538 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 1539 | return; |
| 1540 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | void |
| 1544 | close_all_popups(void) |
| 1545 | { |
| 1546 | while (first_popupwin != NULL) |
| 1547 | popup_close(first_popupwin->w_id); |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 1548 | while (curtab->tp_first_popupwin != NULL) |
| 1549 | popup_close(curtab->tp_first_popupwin->w_id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1552 | /* |
| 1553 | * popup_move({id}, {options}) |
| 1554 | */ |
| 1555 | void |
| 1556 | f_popup_move(typval_T *argvars, typval_T *rettv UNUSED) |
| 1557 | { |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1558 | dict_T *dict; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1559 | int id = (int)tv_get_number(argvars); |
| 1560 | win_T *wp = find_popup_win(id); |
| 1561 | |
| 1562 | if (wp == NULL) |
| 1563 | return; // invalid {id} |
| 1564 | |
| 1565 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 1566 | { |
| 1567 | emsg(_(e_dictreq)); |
| 1568 | return; |
| 1569 | } |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1570 | dict = argvars[1].vval.v_dict; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1571 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1572 | apply_move_options(wp, dict); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1573 | |
| 1574 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 1575 | clear_cmdline = TRUE; |
| 1576 | popup_adjust_position(wp); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1579 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1580 | * popup_setoptions({id}, {options}) |
| 1581 | */ |
| 1582 | void |
| 1583 | f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 1584 | { |
| 1585 | dict_T *dict; |
| 1586 | int id = (int)tv_get_number(argvars); |
| 1587 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1588 | linenr_T old_firstline; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1589 | |
| 1590 | if (wp == NULL) |
| 1591 | return; // invalid {id} |
| 1592 | |
| 1593 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 1594 | { |
| 1595 | emsg(_(e_dictreq)); |
| 1596 | return; |
| 1597 | } |
| 1598 | dict = argvars[1].vval.v_dict; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1599 | old_firstline = wp->w_firstline; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1600 | |
| 1601 | apply_move_options(wp, dict); |
| 1602 | apply_general_options(wp, dict); |
| 1603 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1604 | if (old_firstline != wp->w_firstline) |
| 1605 | redraw_win_later(wp, NOT_VALID); |
Bram Moolenaar | ad24a71 | 2019-06-17 20:05:45 +0200 | [diff] [blame] | 1606 | popup_mask_refresh = TRUE; |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1607 | popup_adjust_position(wp); |
| 1608 | } |
| 1609 | |
| 1610 | /* |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1611 | * popup_getpos({id}) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1612 | */ |
| 1613 | void |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1614 | f_popup_getpos(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1615 | { |
| 1616 | dict_T *dict; |
| 1617 | int id = (int)tv_get_number(argvars); |
| 1618 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 1619 | int top_extra; |
| 1620 | int left_extra; |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1621 | |
| 1622 | if (rettv_dict_alloc(rettv) == OK) |
| 1623 | { |
| 1624 | if (wp == NULL) |
| 1625 | return; // invalid {id} |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 1626 | top_extra = popup_top_extra(wp); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 1627 | left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3]; |
| 1628 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1629 | dict = rettv->vval.v_dict; |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 1630 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1631 | dict_add_number(dict, "line", wp->w_winrow + 1); |
| 1632 | dict_add_number(dict, "col", wp->w_wincol + 1); |
Bram Moolenaar | 68d48f4 | 2019-06-12 22:42:41 +0200 | [diff] [blame] | 1633 | dict_add_number(dict, "width", wp->w_width + left_extra |
| 1634 | + wp->w_popup_border[1] + wp->w_popup_padding[1]); |
| 1635 | dict_add_number(dict, "height", wp->w_height + top_extra |
| 1636 | + wp->w_popup_border[2] + wp->w_popup_padding[2]); |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 1637 | |
| 1638 | dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra); |
| 1639 | dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra); |
| 1640 | dict_add_number(dict, "core_width", wp->w_width); |
| 1641 | dict_add_number(dict, "core_height", wp->w_height); |
| 1642 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1643 | dict_add_number(dict, "scrollbar", wp->w_has_scrollbar); |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 1644 | dict_add_number(dict, "firstline", wp->w_topline); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1645 | dict_add_number(dict, "visible", |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 1646 | win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1651 | * For popup_getoptions(): add a "border" or "padding" entry to "dict". |
| 1652 | */ |
| 1653 | static void |
| 1654 | get_padding_border(dict_T *dict, int *array, char *name) |
| 1655 | { |
| 1656 | list_T *list; |
| 1657 | int i; |
| 1658 | |
| 1659 | if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0) |
| 1660 | return; |
| 1661 | |
| 1662 | list = list_alloc(); |
| 1663 | if (list != NULL) |
| 1664 | { |
| 1665 | dict_add_list(dict, name, list); |
| 1666 | if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1) |
| 1667 | for (i = 0; i < 4; ++i) |
| 1668 | list_append_number(list, array[i]); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * For popup_getoptions(): add a "borderhighlight" entry to "dict". |
| 1674 | */ |
| 1675 | static void |
| 1676 | get_borderhighlight(dict_T *dict, win_T *wp) |
| 1677 | { |
| 1678 | list_T *list; |
| 1679 | int i; |
| 1680 | |
| 1681 | for (i = 0; i < 4; ++i) |
| 1682 | if (wp->w_border_highlight[i] != NULL) |
| 1683 | break; |
| 1684 | if (i == 4) |
| 1685 | return; |
| 1686 | |
| 1687 | list = list_alloc(); |
| 1688 | if (list != NULL) |
| 1689 | { |
| 1690 | dict_add_list(dict, "borderhighlight", list); |
| 1691 | for (i = 0; i < 4; ++i) |
| 1692 | list_append_string(list, wp->w_border_highlight[i], -1); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * For popup_getoptions(): add a "borderchars" entry to "dict". |
| 1698 | */ |
| 1699 | static void |
| 1700 | get_borderchars(dict_T *dict, win_T *wp) |
| 1701 | { |
| 1702 | list_T *list; |
| 1703 | int i; |
| 1704 | char_u buf[NUMBUFLEN]; |
| 1705 | int len; |
| 1706 | |
| 1707 | for (i = 0; i < 8; ++i) |
| 1708 | if (wp->w_border_char[i] != 0) |
| 1709 | break; |
| 1710 | if (i == 8) |
| 1711 | return; |
| 1712 | |
| 1713 | list = list_alloc(); |
| 1714 | if (list != NULL) |
| 1715 | { |
| 1716 | dict_add_list(dict, "borderchars", list); |
| 1717 | for (i = 0; i < 8; ++i) |
| 1718 | { |
| 1719 | len = mb_char2bytes(wp->w_border_char[i], buf); |
| 1720 | list_append_string(list, buf, len); |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * For popup_getoptions(): add a "moved" entry to "dict". |
| 1727 | */ |
| 1728 | static void |
| 1729 | get_moved_list(dict_T *dict, win_T *wp) |
| 1730 | { |
| 1731 | list_T *list; |
| 1732 | |
| 1733 | list = list_alloc(); |
| 1734 | if (list != NULL) |
| 1735 | { |
| 1736 | dict_add_list(dict, "moved", list); |
| 1737 | list_append_number(list, wp->w_popup_mincol); |
| 1738 | list_append_number(list, wp->w_popup_maxcol); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | /* |
Bram Moolenaar | 33796b3 | 2019-06-08 16:01:13 +0200 | [diff] [blame] | 1743 | * popup_getoptions({id}) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1744 | */ |
| 1745 | void |
| 1746 | f_popup_getoptions(typval_T *argvars, typval_T *rettv) |
| 1747 | { |
| 1748 | dict_T *dict; |
| 1749 | int id = (int)tv_get_number(argvars); |
| 1750 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1751 | tabpage_T *tp; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1752 | int i; |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1753 | |
| 1754 | if (rettv_dict_alloc(rettv) == OK) |
| 1755 | { |
| 1756 | if (wp == NULL) |
| 1757 | return; |
| 1758 | |
| 1759 | dict = rettv->vval.v_dict; |
| 1760 | dict_add_number(dict, "line", wp->w_wantline); |
| 1761 | dict_add_number(dict, "col", wp->w_wantcol); |
| 1762 | dict_add_number(dict, "minwidth", wp->w_minwidth); |
| 1763 | dict_add_number(dict, "minheight", wp->w_minheight); |
| 1764 | dict_add_number(dict, "maxheight", wp->w_maxheight); |
| 1765 | dict_add_number(dict, "maxwidth", wp->w_maxwidth); |
Bram Moolenaar | 8d24104 | 2019-06-12 23:40:01 +0200 | [diff] [blame] | 1766 | dict_add_number(dict, "firstline", wp->w_firstline); |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 1767 | dict_add_number(dict, "scrollbar", wp->w_want_scrollbar); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1768 | dict_add_number(dict, "zindex", wp->w_zindex); |
Bram Moolenaar | 042fb4b | 2019-06-02 14:49:56 +0200 | [diff] [blame] | 1769 | dict_add_number(dict, "fixed", wp->w_popup_fixed); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1770 | dict_add_string(dict, "title", wp->w_popup_title); |
| 1771 | dict_add_number(dict, "wrap", wp->w_p_wrap); |
| 1772 | dict_add_number(dict, "drag", wp->w_popup_drag); |
| 1773 | dict_add_string(dict, "highlight", wp->w_p_wcr); |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 1774 | if (wp->w_scrollbar_highlight != NULL) |
| 1775 | dict_add_string(dict, "scrollbarhighlight", |
| 1776 | wp->w_scrollbar_highlight); |
| 1777 | if (wp->w_thumb_highlight != NULL) |
| 1778 | dict_add_string(dict, "thumbhighlight", wp->w_thumb_highlight); |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1779 | |
Bram Moolenaar | a3fce62 | 2019-06-20 02:31:49 +0200 | [diff] [blame] | 1780 | // find the tabpage that holds this popup |
| 1781 | i = 1; |
| 1782 | FOR_ALL_TABPAGES(tp) |
| 1783 | { |
| 1784 | win_T *p; |
| 1785 | |
| 1786 | for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next) |
| 1787 | if (p->w_id == id) |
| 1788 | break; |
| 1789 | if (p != NULL) |
| 1790 | break; |
| 1791 | ++i; |
| 1792 | } |
| 1793 | if (tp == NULL) |
| 1794 | i = -1; // must be global |
| 1795 | else if (tp == curtab) |
| 1796 | i = 0; |
| 1797 | dict_add_number(dict, "tabpage", i); |
| 1798 | |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 1799 | get_padding_border(dict, wp->w_popup_padding, "padding"); |
| 1800 | get_padding_border(dict, wp->w_popup_border, "border"); |
| 1801 | get_borderhighlight(dict, wp); |
| 1802 | get_borderchars(dict, wp); |
| 1803 | get_moved_list(dict, wp); |
| 1804 | |
| 1805 | if (wp->w_filter_cb.cb_name != NULL) |
| 1806 | dict_add_callback(dict, "filter", &wp->w_filter_cb); |
| 1807 | if (wp->w_close_cb.cb_name != NULL) |
| 1808 | dict_add_callback(dict, "callback", &wp->w_close_cb); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 1809 | |
| 1810 | for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T)); |
| 1811 | ++i) |
| 1812 | if (wp->w_popup_pos == poppos_entries[i].pp_val) |
| 1813 | { |
| 1814 | dict_add_string(dict, "pos", |
| 1815 | (char_u *)poppos_entries[i].pp_name); |
| 1816 | break; |
| 1817 | } |
| 1818 | |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 1819 | # if defined(FEAT_TIMERS) |
| 1820 | dict_add_number(dict, "time", wp->w_popup_timer != NULL |
| 1821 | ? (long)wp->w_popup_timer->tr_interval : 0L); |
| 1822 | # endif |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 1823 | } |
| 1824 | } |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 1825 | |
| 1826 | int |
Bram Moolenaar | 8cdbd5b | 2019-06-16 15:50:45 +0200 | [diff] [blame] | 1827 | error_if_popup_window() |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 1828 | { |
| 1829 | if (bt_popup(curwin->w_buffer)) |
| 1830 | { |
| 1831 | emsg(_("E994: Not allowed in a popup window")); |
| 1832 | return TRUE; |
| 1833 | } |
| 1834 | return FALSE; |
| 1835 | } |
| 1836 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1837 | /* |
| 1838 | * 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] | 1839 | * in the current tab page. |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1840 | */ |
| 1841 | void |
| 1842 | popup_reset_handled() |
| 1843 | { |
| 1844 | win_T *wp; |
| 1845 | |
| 1846 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 1847 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 1848 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 1849 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 1850 | } |
| 1851 | |
| 1852 | /* |
| 1853 | * Find the next visible popup where POPF_HANDLED is not set. |
| 1854 | * Must have called popup_reset_handled() first. |
| 1855 | * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the |
| 1856 | * popup with the highest zindex. |
| 1857 | */ |
| 1858 | win_T * |
| 1859 | find_next_popup(int lowest) |
| 1860 | { |
| 1861 | win_T *wp; |
| 1862 | win_T *found_wp; |
| 1863 | int found_zindex; |
| 1864 | |
| 1865 | found_zindex = lowest ? INT_MAX : 0; |
| 1866 | found_wp = NULL; |
| 1867 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 1868 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 1869 | && (lowest ? wp->w_zindex < found_zindex |
| 1870 | : wp->w_zindex > found_zindex)) |
| 1871 | { |
| 1872 | found_zindex = wp->w_zindex; |
| 1873 | found_wp = wp; |
| 1874 | } |
| 1875 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 1876 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 1877 | && (lowest ? wp->w_zindex < found_zindex |
| 1878 | : wp->w_zindex > found_zindex)) |
| 1879 | { |
| 1880 | found_zindex = wp->w_zindex; |
| 1881 | found_wp = wp; |
| 1882 | } |
| 1883 | |
| 1884 | if (found_wp != NULL) |
| 1885 | found_wp->w_popup_flags |= POPF_HANDLED; |
| 1886 | return found_wp; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Invoke the filter callback for window "wp" with typed character "c". |
| 1891 | * Uses the global "mod_mask" for modifiers. |
| 1892 | * Returns the return value of the filter. |
| 1893 | * Careful: The filter may make "wp" invalid! |
| 1894 | */ |
| 1895 | static int |
| 1896 | invoke_popup_filter(win_T *wp, int c) |
| 1897 | { |
| 1898 | int res; |
| 1899 | typval_T rettv; |
| 1900 | int dummy; |
| 1901 | typval_T argv[3]; |
| 1902 | char_u buf[NUMBUFLEN]; |
| 1903 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1904 | // Emergency exit: CTRL-C closes the popup. |
| 1905 | if (c == Ctrl_C) |
| 1906 | { |
| 1907 | rettv.v_type = VAR_NUMBER; |
| 1908 | rettv.vval.v_number = -1; |
| 1909 | popup_close_and_callback(wp, &rettv); |
| 1910 | return 1; |
| 1911 | } |
| 1912 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1913 | argv[0].v_type = VAR_NUMBER; |
| 1914 | argv[0].vval.v_number = (varnumber_T)wp->w_id; |
| 1915 | |
| 1916 | // Convert the number to a string, so that the function can use: |
| 1917 | // if a:c == "\<F2>" |
| 1918 | buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL; |
| 1919 | argv[1].v_type = VAR_STRING; |
| 1920 | argv[1].vval.v_string = vim_strsave(buf); |
| 1921 | |
| 1922 | argv[2].v_type = VAR_UNKNOWN; |
| 1923 | |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1924 | // NOTE: The callback might close the popup, thus make "wp" invalid. |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1925 | call_callback(&wp->w_filter_cb, -1, |
| 1926 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL); |
| 1927 | res = tv_get_number(&rettv); |
| 1928 | vim_free(argv[1].vval.v_string); |
| 1929 | clear_tv(&rettv); |
| 1930 | return res; |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * Called when "c" was typed: invoke popup filter callbacks. |
| 1935 | * Returns TRUE when the character was consumed, |
| 1936 | */ |
| 1937 | int |
| 1938 | popup_do_filter(int c) |
| 1939 | { |
| 1940 | int res = FALSE; |
Bram Moolenaar | a42d945 | 2019-06-15 21:46:30 +0200 | [diff] [blame] | 1941 | win_T *wp; |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1942 | |
| 1943 | popup_reset_handled(); |
| 1944 | |
| 1945 | while (!res && (wp = find_next_popup(FALSE)) != NULL) |
| 1946 | if (wp->w_filter_cb.cb_name != NULL) |
| 1947 | res = invoke_popup_filter(wp, c); |
| 1948 | |
| 1949 | return res; |
| 1950 | } |
| 1951 | |
Bram Moolenaar | 3397f74 | 2019-06-02 18:40:06 +0200 | [diff] [blame] | 1952 | /* |
| 1953 | * Called when the cursor moved: check if any popup needs to be closed if the |
| 1954 | * cursor moved far enough. |
| 1955 | */ |
| 1956 | void |
| 1957 | popup_check_cursor_pos() |
| 1958 | { |
| 1959 | win_T *wp; |
| 1960 | typval_T tv; |
| 1961 | |
| 1962 | popup_reset_handled(); |
| 1963 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 1964 | if (wp->w_popup_curwin != NULL |
| 1965 | && (curwin != wp->w_popup_curwin |
| 1966 | || curwin->w_cursor.lnum != wp->w_popup_lnum |
| 1967 | || curwin->w_cursor.col < wp->w_popup_mincol |
| 1968 | || curwin->w_cursor.col > wp->w_popup_maxcol)) |
| 1969 | { |
| 1970 | tv.v_type = VAR_NUMBER; |
| 1971 | tv.vval.v_number = -1; |
| 1972 | popup_close_and_callback(wp, &tv); |
| 1973 | } |
| 1974 | } |
| 1975 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 1976 | /* |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 1977 | * Return TRUE if "col" / "line" matches with an entry in w_popup_mask. |
| 1978 | * "col" and "line" are screen coordinates. |
| 1979 | */ |
| 1980 | static int |
| 1981 | popup_masked(win_T *wp, int screencol, int screenline) |
| 1982 | { |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 1983 | int col = screencol - wp->w_wincol + 1 + wp->w_leftcol; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 1984 | int line = screenline - wp->w_winrow + 1; |
| 1985 | listitem_T *lio, *li; |
| 1986 | int width, height; |
| 1987 | |
| 1988 | if (wp->w_popup_mask == NULL) |
| 1989 | return FALSE; |
| 1990 | width = popup_width(wp); |
| 1991 | height = popup_height(wp); |
| 1992 | |
| 1993 | for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next) |
| 1994 | { |
| 1995 | int cols, cole; |
| 1996 | int lines, linee; |
| 1997 | |
| 1998 | li = lio->li_tv.vval.v_list->lv_first; |
| 1999 | cols = tv_get_number(&li->li_tv); |
| 2000 | if (cols < 0) |
| 2001 | cols = width + cols + 1; |
| 2002 | if (col < cols) |
| 2003 | continue; |
| 2004 | li = li->li_next; |
| 2005 | cole = tv_get_number(&li->li_tv); |
| 2006 | if (cole < 0) |
| 2007 | cole = width + cole + 1; |
| 2008 | if (col > cole) |
| 2009 | continue; |
| 2010 | li = li->li_next; |
| 2011 | lines = tv_get_number(&li->li_tv); |
| 2012 | if (lines < 0) |
| 2013 | lines = height + lines + 1; |
| 2014 | if (line < lines) |
| 2015 | continue; |
| 2016 | li = li->li_next; |
| 2017 | linee = tv_get_number(&li->li_tv); |
| 2018 | if (linee < 0) |
| 2019 | linee = height + linee + 1; |
| 2020 | if (line > linee) |
| 2021 | continue; |
| 2022 | return TRUE; |
| 2023 | } |
| 2024 | return FALSE; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * Set flags in popup_transparent[] for window "wp" to "val". |
| 2029 | */ |
| 2030 | static void |
| 2031 | update_popup_transparent(win_T *wp, int val) |
| 2032 | { |
| 2033 | if (wp->w_popup_mask != NULL) |
| 2034 | { |
| 2035 | int width = popup_width(wp); |
| 2036 | int height = popup_height(wp); |
| 2037 | listitem_T *lio, *li; |
| 2038 | int cols, cole; |
| 2039 | int lines, linee; |
| 2040 | int col, line; |
| 2041 | |
| 2042 | for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next) |
| 2043 | { |
| 2044 | li = lio->li_tv.vval.v_list->lv_first; |
| 2045 | cols = tv_get_number(&li->li_tv); |
| 2046 | if (cols < 0) |
| 2047 | cols = width + cols + 1; |
| 2048 | li = li->li_next; |
| 2049 | cole = tv_get_number(&li->li_tv); |
| 2050 | if (cole < 0) |
| 2051 | cole = width + cole + 1; |
| 2052 | li = li->li_next; |
| 2053 | lines = tv_get_number(&li->li_tv); |
| 2054 | if (lines < 0) |
| 2055 | lines = height + lines + 1; |
| 2056 | li = li->li_next; |
| 2057 | linee = tv_get_number(&li->li_tv); |
| 2058 | if (linee < 0) |
| 2059 | linee = height + linee + 1; |
| 2060 | |
| 2061 | --cols; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 2062 | cols -= wp->w_leftcol; |
| 2063 | if (cols < 0) |
| 2064 | cols = 0; |
| 2065 | cole -= wp->w_leftcol; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2066 | --lines; |
Bram Moolenaar | 711d02c | 2019-06-28 04:06:50 +0200 | [diff] [blame] | 2067 | if (lines < 0) |
| 2068 | lines = 0; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2069 | for (line = lines; line < linee && line < screen_Rows; ++line) |
| 2070 | for (col = cols; col < cole && col < screen_Columns; ++col) |
| 2071 | popup_transparent[(line + wp->w_winrow) * screen_Columns |
| 2072 | + col + wp->w_wincol] = val; |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | /* |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2078 | * Update "popup_mask" if needed. |
| 2079 | * Also recomputes the popup size and positions. |
| 2080 | * Also updates "popup_visible". |
| 2081 | * Also marks window lines for redrawing. |
| 2082 | */ |
| 2083 | void |
| 2084 | may_update_popup_mask(int type) |
| 2085 | { |
| 2086 | win_T *wp; |
| 2087 | short *mask; |
| 2088 | int line, col; |
| 2089 | int redraw_all = FALSE; |
| 2090 | |
| 2091 | // Need to recompute when switching tabs. |
| 2092 | // Also recompute when the type is CLEAR or NOT_VALID, something basic |
| 2093 | // (such as the screen size) must have changed. |
| 2094 | if (popup_mask_tab != curtab || type >= NOT_VALID) |
| 2095 | { |
| 2096 | popup_mask_refresh = TRUE; |
| 2097 | redraw_all = TRUE; |
| 2098 | } |
| 2099 | if (!popup_mask_refresh) |
| 2100 | { |
| 2101 | // Check if any buffer has changed. |
| 2102 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 2103 | if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2104 | popup_mask_refresh = TRUE; |
| 2105 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 2106 | if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2107 | popup_mask_refresh = TRUE; |
| 2108 | if (!popup_mask_refresh) |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | // Need to update the mask, something has changed. |
| 2113 | popup_mask_refresh = FALSE; |
| 2114 | popup_mask_tab = curtab; |
| 2115 | popup_visible = FALSE; |
| 2116 | |
| 2117 | // If redrawing everything, just update "popup_mask". |
| 2118 | // If redrawing only what is needed, update "popup_mask_next" and then |
| 2119 | // compare with "popup_mask" to see what changed. |
| 2120 | if (type >= SOME_VALID) |
| 2121 | mask = popup_mask; |
| 2122 | else |
| 2123 | mask = popup_mask_next; |
| 2124 | vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short)); |
| 2125 | |
| 2126 | // Find the window with the lowest zindex that hasn't been handled yet, |
| 2127 | // so that the window with a higher zindex overwrites the value in |
| 2128 | // popup_mask. |
| 2129 | popup_reset_handled(); |
| 2130 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 2131 | { |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2132 | int height = popup_height(wp); |
| 2133 | int width = popup_width(wp); |
| 2134 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2135 | popup_visible = TRUE; |
| 2136 | |
| 2137 | // Recompute the position if the text changed. |
| 2138 | if (redraw_all |
| 2139 | || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer)) |
| 2140 | popup_adjust_position(wp); |
| 2141 | |
| 2142 | for (line = wp->w_winrow; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2143 | line < wp->w_winrow + height && line < screen_Rows; ++line) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2144 | for (col = wp->w_wincol; |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2145 | col < wp->w_wincol + width && col < screen_Columns; ++col) |
| 2146 | if (!popup_masked(wp, col, line)) |
| 2147 | mask[line * screen_Columns + col] = wp->w_zindex; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | // Only check which lines are to be updated if not already |
| 2151 | // updating all lines. |
| 2152 | if (mask == popup_mask_next) |
| 2153 | for (line = 0; line < screen_Rows; ++line) |
| 2154 | { |
| 2155 | int col_done = 0; |
| 2156 | |
| 2157 | for (col = 0; col < screen_Columns; ++col) |
| 2158 | { |
| 2159 | int off = line * screen_Columns + col; |
| 2160 | |
| 2161 | if (popup_mask[off] != popup_mask_next[off]) |
| 2162 | { |
| 2163 | popup_mask[off] = popup_mask_next[off]; |
| 2164 | |
| 2165 | if (line >= cmdline_row) |
| 2166 | { |
| 2167 | // the command line needs to be cleared if text below |
| 2168 | // the popup is now visible. |
| 2169 | if (!msg_scrolled && popup_mask_next[off] == 0) |
| 2170 | clear_cmdline = TRUE; |
| 2171 | } |
| 2172 | else if (col >= col_done) |
| 2173 | { |
| 2174 | linenr_T lnum; |
| 2175 | int line_cp = line; |
| 2176 | int col_cp = col; |
| 2177 | |
| 2178 | // The screen position "line" / "col" needs to be |
| 2179 | // redrawn. Figure out what window that is and update |
| 2180 | // w_redraw_top and w_redr_bot. Only needs to be done |
| 2181 | // once for each window line. |
| 2182 | wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP); |
| 2183 | if (wp != NULL) |
| 2184 | { |
| 2185 | if (line_cp >= wp->w_height) |
| 2186 | // In (or below) status line |
| 2187 | wp->w_redr_status = TRUE; |
| 2188 | // compute the position in the buffer line from the |
| 2189 | // position on the screen |
| 2190 | else if (mouse_comp_pos(wp, &line_cp, &col_cp, |
| 2191 | &lnum)) |
| 2192 | // past bottom |
| 2193 | wp->w_redr_status = TRUE; |
| 2194 | else |
| 2195 | redrawWinline(wp, lnum); |
| 2196 | |
| 2197 | // This line is going to be redrawn, no need to |
| 2198 | // check until the right side of the window. |
| 2199 | col_done = wp->w_wincol + wp->w_width - 1; |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * Return a string of "len" spaces in IObuff. |
| 2209 | */ |
| 2210 | static char_u * |
| 2211 | get_spaces(int len) |
| 2212 | { |
| 2213 | vim_memset(IObuff, ' ', (size_t)len); |
| 2214 | IObuff[len] = NUL; |
| 2215 | return IObuff; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Update popup windows. They are drawn on top of normal windows. |
| 2220 | * "win_update" is called for each popup window, lowest zindex first. |
| 2221 | */ |
| 2222 | void |
| 2223 | update_popups(void (*win_update)(win_T *wp)) |
| 2224 | { |
| 2225 | win_T *wp; |
| 2226 | int top_off; |
| 2227 | int left_off; |
| 2228 | int total_width; |
| 2229 | int total_height; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2230 | int top_padding; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2231 | int popup_attr; |
| 2232 | int border_attr[4]; |
| 2233 | int border_char[8]; |
| 2234 | char_u buf[MB_MAXBYTES]; |
| 2235 | int row; |
| 2236 | int i; |
Bram Moolenaar | 6efd76a | 2019-06-26 04:06:57 +0200 | [diff] [blame] | 2237 | int sb_thumb_top = 0; |
| 2238 | int sb_thumb_height = 0; |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 2239 | int attr_scroll = 0; |
| 2240 | int attr_thumb = 0; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2241 | |
| 2242 | // Find the window with the lowest zindex that hasn't been updated yet, |
| 2243 | // so that the window with a higher zindex is drawn later, thus goes on |
| 2244 | // top. |
| 2245 | popup_reset_handled(); |
| 2246 | while ((wp = find_next_popup(TRUE)) != NULL) |
| 2247 | { |
| 2248 | // This drawing uses the zindex of the popup window, so that it's on |
| 2249 | // top of the text but doesn't draw when another popup with higher |
| 2250 | // zindex is on top of the character. |
| 2251 | screen_zindex = wp->w_zindex; |
| 2252 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2253 | // Set flags in popup_transparent[] for masked cells. |
| 2254 | update_popup_transparent(wp, 1); |
| 2255 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2256 | // adjust w_winrow and w_wincol for border and padding, since |
| 2257 | // win_update() doesn't handle them. |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2258 | top_off = popup_top_extra(wp); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2259 | left_off = wp->w_popup_padding[3] + wp->w_popup_border[3]; |
| 2260 | wp->w_winrow += top_off; |
| 2261 | wp->w_wincol += left_off; |
| 2262 | |
Bram Moolenaar | c2a4316 | 2019-06-26 01:03:53 +0200 | [diff] [blame] | 2263 | // Draw the popup text, unless it's off screen. |
| 2264 | if (wp->w_winrow < screen_Rows && wp->w_wincol < screen_Columns) |
| 2265 | win_update(wp); |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2266 | |
| 2267 | wp->w_winrow -= top_off; |
| 2268 | wp->w_wincol -= left_off; |
| 2269 | |
| 2270 | total_width = wp->w_popup_border[3] + wp->w_popup_padding[3] |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2271 | + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1] |
| 2272 | + wp->w_has_scrollbar; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2273 | total_height = popup_top_extra(wp) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2274 | + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2]; |
| 2275 | popup_attr = get_wcr_attr(wp); |
| 2276 | |
| 2277 | // We can only use these line drawing characters when 'encoding' is |
| 2278 | // "utf-8" and 'ambiwidth' is "single". |
| 2279 | if (enc_utf8 && *p_ambw == 's') |
| 2280 | { |
| 2281 | border_char[0] = border_char[2] = 0x2550; |
| 2282 | border_char[1] = border_char[3] = 0x2551; |
| 2283 | border_char[4] = 0x2554; |
| 2284 | border_char[5] = 0x2557; |
| 2285 | border_char[6] = 0x255d; |
| 2286 | border_char[7] = 0x255a; |
| 2287 | } |
| 2288 | else |
| 2289 | { |
| 2290 | border_char[0] = border_char[2] = '-'; |
| 2291 | border_char[1] = border_char[3] = '|'; |
| 2292 | for (i = 4; i < 8; ++i) |
| 2293 | border_char[i] = '+'; |
| 2294 | } |
| 2295 | for (i = 0; i < 8; ++i) |
| 2296 | if (wp->w_border_char[i] != 0) |
| 2297 | border_char[i] = wp->w_border_char[i]; |
| 2298 | |
| 2299 | for (i = 0; i < 4; ++i) |
| 2300 | { |
| 2301 | border_attr[i] = popup_attr; |
| 2302 | if (wp->w_border_highlight[i] != NULL) |
| 2303 | border_attr[i] = syn_name2attr(wp->w_border_highlight[i]); |
| 2304 | } |
| 2305 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2306 | top_padding = wp->w_popup_padding[0]; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2307 | if (wp->w_popup_border[0] > 0) |
| 2308 | { |
| 2309 | // top border |
| 2310 | screen_fill(wp->w_winrow, wp->w_winrow + 1, |
| 2311 | wp->w_wincol, |
| 2312 | wp->w_wincol + total_width, |
| 2313 | wp->w_popup_border[3] != 0 |
| 2314 | ? border_char[4] : border_char[0], |
| 2315 | border_char[0], border_attr[0]); |
| 2316 | if (wp->w_popup_border[1] > 0) |
| 2317 | { |
| 2318 | buf[mb_char2bytes(border_char[5], buf)] = NUL; |
| 2319 | screen_puts(buf, wp->w_winrow, |
| 2320 | wp->w_wincol + total_width - 1, border_attr[1]); |
| 2321 | } |
| 2322 | } |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2323 | else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0) |
| 2324 | top_padding = 1; |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2325 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2326 | if (top_padding > 0) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2327 | { |
| 2328 | // top padding |
| 2329 | row = wp->w_winrow + wp->w_popup_border[0]; |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2330 | screen_fill(row, row + top_padding, |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2331 | wp->w_wincol + wp->w_popup_border[3], |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2332 | wp->w_wincol + total_width - wp->w_popup_border[1] |
| 2333 | - wp->w_has_scrollbar, |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2334 | ' ', ' ', popup_attr); |
| 2335 | } |
| 2336 | |
Bram Moolenaar | eb2310d | 2019-06-16 20:09:10 +0200 | [diff] [blame] | 2337 | // Title goes on top of border or padding. |
| 2338 | if (wp->w_popup_title != NULL) |
| 2339 | screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1, |
| 2340 | wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr); |
| 2341 | |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2342 | // Compute scrollbar thumb position and size. |
| 2343 | if (wp->w_has_scrollbar) |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2344 | { |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2345 | linenr_T linecount = wp->w_buffer->b_ml.ml_line_count; |
| 2346 | |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 2347 | sb_thumb_height = (wp->w_height * wp->w_height + linecount / 2) |
| 2348 | / linecount; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2349 | if (sb_thumb_height == 0) |
| 2350 | sb_thumb_height = 1; |
Bram Moolenaar | 68acb41 | 2019-06-26 03:40:36 +0200 | [diff] [blame] | 2351 | sb_thumb_top = (wp->w_topline - 1 + (linecount / wp->w_height) / 2) |
| 2352 | * (wp->w_height - sb_thumb_height) |
| 2353 | / (linecount - wp->w_height); |
Bram Moolenaar | 4cd583c | 2019-06-26 05:13:57 +0200 | [diff] [blame] | 2354 | if (wp->w_scrollbar_highlight != NULL) |
| 2355 | attr_scroll = syn_name2attr(wp->w_scrollbar_highlight); |
| 2356 | else |
| 2357 | attr_scroll = highlight_attr[HLF_PSB]; |
| 2358 | if (wp->w_thumb_highlight != NULL) |
| 2359 | attr_thumb = syn_name2attr(wp->w_thumb_highlight); |
| 2360 | else |
| 2361 | attr_thumb = highlight_attr[HLF_PST]; |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | for (i = wp->w_popup_border[0]; |
| 2365 | i < total_height - wp->w_popup_border[2]; ++i) |
| 2366 | { |
| 2367 | row = wp->w_winrow + i; |
| 2368 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2369 | // left border |
| 2370 | if (wp->w_popup_border[3] > 0) |
| 2371 | { |
| 2372 | buf[mb_char2bytes(border_char[3], buf)] = NUL; |
| 2373 | screen_puts(buf, row, wp->w_wincol, border_attr[3]); |
| 2374 | } |
| 2375 | // left padding |
| 2376 | if (wp->w_popup_padding[3] > 0) |
| 2377 | screen_puts(get_spaces(wp->w_popup_padding[3]), row, |
| 2378 | wp->w_wincol + wp->w_popup_border[3], popup_attr); |
Bram Moolenaar | 75fb085 | 2019-06-25 05:15:58 +0200 | [diff] [blame] | 2379 | // scrollbar |
| 2380 | if (wp->w_has_scrollbar) |
| 2381 | { |
| 2382 | int line = i - top_off; |
| 2383 | int scroll_col = wp->w_wincol + total_width - 1 |
| 2384 | - wp->w_popup_border[1]; |
| 2385 | |
| 2386 | if (line >= 0 && line < wp->w_height) |
| 2387 | screen_putchar(' ', row, scroll_col, |
| 2388 | line >= sb_thumb_top |
| 2389 | && line < sb_thumb_top + sb_thumb_height |
| 2390 | ? attr_thumb : attr_scroll); |
| 2391 | else |
| 2392 | screen_putchar(' ', row, scroll_col, popup_attr); |
| 2393 | } |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2394 | // right border |
| 2395 | if (wp->w_popup_border[1] > 0) |
| 2396 | { |
| 2397 | buf[mb_char2bytes(border_char[1], buf)] = NUL; |
| 2398 | screen_puts(buf, row, |
| 2399 | wp->w_wincol + total_width - 1, border_attr[1]); |
| 2400 | } |
| 2401 | // right padding |
| 2402 | if (wp->w_popup_padding[1] > 0) |
| 2403 | screen_puts(get_spaces(wp->w_popup_padding[1]), row, |
| 2404 | wp->w_wincol + wp->w_popup_border[3] |
| 2405 | + wp->w_popup_padding[3] + wp->w_width, popup_attr); |
| 2406 | } |
| 2407 | |
| 2408 | if (wp->w_popup_padding[2] > 0) |
| 2409 | { |
| 2410 | // bottom padding |
| 2411 | row = wp->w_winrow + wp->w_popup_border[0] |
| 2412 | + wp->w_popup_padding[0] + wp->w_height; |
| 2413 | screen_fill(row, row + wp->w_popup_padding[2], |
| 2414 | wp->w_wincol + wp->w_popup_border[3], |
| 2415 | wp->w_wincol + total_width - wp->w_popup_border[1], |
| 2416 | ' ', ' ', popup_attr); |
| 2417 | } |
| 2418 | |
| 2419 | if (wp->w_popup_border[2] > 0) |
| 2420 | { |
| 2421 | // bottom border |
| 2422 | row = wp->w_winrow + total_height - 1; |
| 2423 | screen_fill(row , row + 1, |
| 2424 | wp->w_wincol, |
| 2425 | wp->w_wincol + total_width, |
| 2426 | wp->w_popup_border[3] != 0 |
| 2427 | ? border_char[7] : border_char[2], |
| 2428 | border_char[2], border_attr[2]); |
| 2429 | if (wp->w_popup_border[1] > 0) |
| 2430 | { |
| 2431 | buf[mb_char2bytes(border_char[6], buf)] = NUL; |
| 2432 | screen_puts(buf, row, |
| 2433 | wp->w_wincol + total_width - 1, border_attr[2]); |
| 2434 | } |
| 2435 | } |
| 2436 | |
Bram Moolenaar | c662ec9 | 2019-06-23 00:15:57 +0200 | [diff] [blame] | 2437 | update_popup_transparent(wp, 0); |
| 2438 | |
Bram Moolenaar | a540f8a | 2019-06-14 19:23:57 +0200 | [diff] [blame] | 2439 | // Back to the normal zindex. |
| 2440 | screen_zindex = 0; |
| 2441 | } |
| 2442 | } |
| 2443 | |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 2444 | /* |
| 2445 | * Mark references in callbacks of one popup window. |
| 2446 | */ |
| 2447 | static int |
| 2448 | set_ref_in_one_popup(win_T *wp, int copyID) |
| 2449 | { |
| 2450 | int abort = FALSE; |
| 2451 | typval_T tv; |
| 2452 | |
| 2453 | if (wp->w_close_cb.cb_partial != NULL) |
| 2454 | { |
| 2455 | tv.v_type = VAR_PARTIAL; |
| 2456 | tv.vval.v_partial = wp->w_close_cb.cb_partial; |
| 2457 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 2458 | } |
| 2459 | if (wp->w_filter_cb.cb_partial != NULL) |
| 2460 | { |
| 2461 | tv.v_type = VAR_PARTIAL; |
| 2462 | tv.vval.v_partial = wp->w_filter_cb.cb_partial; |
| 2463 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
| 2464 | } |
Bram Moolenaar | 7be3ab2 | 2019-06-23 01:46:15 +0200 | [diff] [blame] | 2465 | abort = abort || set_ref_in_list(wp->w_popup_mask, copyID); |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 2466 | return abort; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * Set reference in callbacks of popup windows. |
| 2471 | */ |
| 2472 | int |
| 2473 | set_ref_in_popups(int copyID) |
| 2474 | { |
| 2475 | int abort = FALSE; |
| 2476 | win_T *wp; |
| 2477 | tabpage_T *tp; |
| 2478 | |
| 2479 | for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next) |
| 2480 | abort = abort || set_ref_in_one_popup(wp, copyID); |
| 2481 | |
| 2482 | FOR_ALL_TABPAGES(tp) |
| 2483 | { |
| 2484 | for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next) |
| 2485 | abort = abort || set_ref_in_one_popup(wp, copyID); |
| 2486 | if (abort) |
| 2487 | break; |
| 2488 | } |
| 2489 | return abort; |
| 2490 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 2491 | #endif // FEAT_TEXT_PROP |