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