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