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 | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 32 | * Get option value for"key", which is "line" or "col". |
| 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) |
| 50 | return dict_get_number(dict, key); |
| 51 | |
| 52 | setcursor_mayforce(TRUE); |
| 53 | s = val + 6; |
| 54 | if (*s != NUL) |
| 55 | { |
| 56 | n = strtol((char *)s, (char **)&endp, 10); |
| 57 | if (endp != NULL && *skipwhite(endp) != NUL) |
| 58 | { |
| 59 | semsg(_(e_invexpr2), val); |
| 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (STRCMP(key, "line") == 0) |
| 65 | n = screen_screenrow() + 1 + n; |
| 66 | else // "col" |
| 67 | n = screen_screencol() + 1 + n; |
| 68 | |
| 69 | if (n < 1) |
| 70 | n = 1; |
| 71 | return n; |
| 72 | } |
| 73 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 74 | static void |
| 75 | get_pos_options(win_T *wp, dict_T *dict) |
| 76 | { |
| 77 | char_u *str; |
| 78 | int nr; |
| 79 | |
| 80 | nr = popup_options_one(dict, (char_u *)"line"); |
| 81 | if (nr > 0) |
| 82 | wp->w_wantline = nr; |
| 83 | nr = popup_options_one(dict, (char_u *)"col"); |
| 84 | if (nr > 0) |
| 85 | wp->w_wantcol = nr; |
| 86 | |
| 87 | str = dict_get_string(dict, (char_u *)"pos", FALSE); |
| 88 | if (str != NULL) |
| 89 | { |
| 90 | for (nr = 0; |
| 91 | nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T)); |
| 92 | ++nr) |
| 93 | if (STRCMP(str, poppos_entries[nr].pp_name) == 0) |
| 94 | { |
| 95 | wp->w_popup_pos = poppos_entries[nr].pp_val; |
| 96 | nr = -1; |
| 97 | break; |
| 98 | } |
| 99 | if (nr != -1) |
| 100 | semsg(_(e_invarg2), str); |
| 101 | } |
| 102 | } |
| 103 | |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 104 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 105 | * Go through the options in "dict" and apply them to buffer "buf" displayed in |
| 106 | * popup window "wp". |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 107 | * When called from f_popup_atcursor() "atcursor" is TRUE. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 108 | */ |
| 109 | static void |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 110 | apply_options(win_T *wp, buf_T *buf UNUSED, dict_T *dict, int atcursor) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 111 | { |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 112 | int nr; |
| 113 | char_u *str; |
| 114 | dictitem_T *di; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 115 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 116 | wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth"); |
| 117 | wp->w_minheight = dict_get_number(dict, (char_u *)"minheight"); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 118 | wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth"); |
| 119 | wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight"); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 120 | |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 121 | if (atcursor) |
| 122 | { |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 123 | wp->w_popup_pos = POPPOS_BOTLEFT; |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 124 | setcursor_mayforce(TRUE); |
| 125 | wp->w_wantline = screen_screenrow(); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 126 | if (wp->w_wantline == 0) // cursor in first line |
| 127 | { |
| 128 | wp->w_wantline = 2; |
| 129 | wp->w_popup_pos = POPPOS_TOPLEFT; |
| 130 | } |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 131 | wp->w_wantcol = screen_screencol() + 1; |
| 132 | } |
| 133 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 134 | get_pos_options(wp, dict); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 135 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 136 | wp->w_zindex = dict_get_number(dict, (char_u *)"zindex"); |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 138 | #if defined(FEAT_TIMERS) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 139 | // Add timer to close the popup after some time. |
| 140 | nr = dict_get_number(dict, (char_u *)"time"); |
| 141 | if (nr > 0) |
| 142 | { |
| 143 | char_u cbbuf[50]; |
| 144 | char_u *ptr = cbbuf; |
| 145 | typval_T tv; |
| 146 | |
| 147 | vim_snprintf((char *)cbbuf, sizeof(cbbuf), |
| 148 | "{_ -> popup_close(%d)}", wp->w_id); |
| 149 | if (get_lambda_tv(&ptr, &tv, TRUE) == OK) |
| 150 | { |
| 151 | wp->w_popup_timer = create_timer(nr, 0); |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 152 | wp->w_popup_timer->tr_callback = get_callback(&tv); |
| 153 | clear_tv(&tv); |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 154 | } |
| 155 | } |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 156 | #endif |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 157 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 158 | // Option values resulting in setting an option. |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 159 | str = dict_get_string(dict, (char_u *)"highlight", FALSE); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 160 | if (str != NULL) |
| 161 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
| 162 | str, OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 163 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 164 | di = dict_find(dict, (char_u *)"wrap", -1); |
| 165 | if (di != NULL) |
| 166 | { |
| 167 | nr = dict_get_number(dict, (char_u *)"wrap"); |
| 168 | wp->w_p_wrap = nr != 0; |
| 169 | } |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 170 | |
| 171 | di = dict_find(dict, (char_u *)"filter", -1); |
| 172 | if (di != NULL) |
| 173 | { |
| 174 | callback_T callback = get_callback(&di->di_tv); |
| 175 | |
| 176 | if (callback.cb_name != NULL) |
| 177 | set_callback(&wp->w_filter_cb, &callback); |
| 178 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 182 | * Add lines to the popup from a list of strings. |
| 183 | */ |
| 184 | static void |
| 185 | add_popup_strings(buf_T *buf, list_T *l) |
| 186 | { |
| 187 | listitem_T *li; |
| 188 | linenr_T lnum = 0; |
| 189 | char_u *p; |
| 190 | |
| 191 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 192 | if (li->li_tv.v_type == VAR_STRING) |
| 193 | { |
| 194 | p = li->li_tv.vval.v_string; |
| 195 | ml_append_buf(buf, lnum++, |
| 196 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Add lines to the popup from a list of dictionaries. |
| 202 | */ |
| 203 | static void |
| 204 | add_popup_dicts(buf_T *buf, list_T *l) |
| 205 | { |
| 206 | listitem_T *li; |
| 207 | listitem_T *pli; |
| 208 | linenr_T lnum = 0; |
| 209 | char_u *p; |
| 210 | dict_T *dict; |
| 211 | |
| 212 | // first add the text lines |
| 213 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 214 | { |
| 215 | if (li->li_tv.v_type != VAR_DICT) |
| 216 | { |
| 217 | emsg(_(e_dictreq)); |
| 218 | return; |
| 219 | } |
| 220 | dict = li->li_tv.vval.v_dict; |
| 221 | p = dict == NULL ? NULL |
| 222 | : dict_get_string(dict, (char_u *)"text", FALSE); |
| 223 | ml_append_buf(buf, lnum++, |
| 224 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 225 | } |
| 226 | |
| 227 | // add the text properties |
| 228 | lnum = 1; |
| 229 | for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum) |
| 230 | { |
| 231 | dictitem_T *di; |
| 232 | list_T *plist; |
| 233 | |
| 234 | dict = li->li_tv.vval.v_dict; |
| 235 | di = dict_find(dict, (char_u *)"props", -1); |
| 236 | if (di != NULL) |
| 237 | { |
| 238 | if (di->di_tv.v_type != VAR_LIST) |
| 239 | { |
| 240 | emsg(_(e_listreq)); |
| 241 | return; |
| 242 | } |
| 243 | plist = di->di_tv.vval.v_list; |
| 244 | if (plist != NULL) |
| 245 | { |
| 246 | for (pli = plist->lv_first; pli != NULL; pli = pli->li_next) |
| 247 | { |
| 248 | if (pli->li_tv.v_type != VAR_DICT) |
| 249 | { |
| 250 | emsg(_(e_dictreq)); |
| 251 | return; |
| 252 | } |
| 253 | dict = pli->li_tv.vval.v_dict; |
| 254 | if (dict != NULL) |
| 255 | { |
| 256 | int col = dict_get_number(dict, (char_u *)"col"); |
| 257 | |
| 258 | prop_add_common( lnum, col, dict, buf, NULL); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 267 | * Adjust the position and size of the popup to fit on the screen. |
| 268 | */ |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 269 | void |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 270 | popup_adjust_position(win_T *wp) |
| 271 | { |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 272 | linenr_T lnum; |
| 273 | int wrapped = 0; |
| 274 | int maxwidth; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 275 | int center_vert = FALSE; |
| 276 | int center_hor = FALSE; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 277 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 278 | wp->w_winrow = 0; |
| 279 | wp->w_wincol = 0; |
| 280 | if (wp->w_popup_pos == POPPOS_CENTER) |
| 281 | { |
| 282 | // center after computing the size |
| 283 | center_vert = TRUE; |
| 284 | center_hor = TRUE; |
| 285 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 286 | else |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 287 | { |
| 288 | if (wp->w_wantline == 0) |
| 289 | center_vert = TRUE; |
| 290 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 291 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 292 | { |
| 293 | wp->w_winrow = wp->w_wantline - 1; |
| 294 | if (wp->w_winrow >= Rows) |
| 295 | wp->w_winrow = Rows - 1; |
| 296 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 297 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 298 | if (wp->w_wantcol == 0) |
| 299 | center_hor = TRUE; |
| 300 | else if (wp->w_popup_pos == POPPOS_TOPLEFT |
| 301 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 302 | { |
| 303 | wp->w_wincol = wp->w_wantcol - 1; |
| 304 | if (wp->w_wincol >= Columns - 3) |
| 305 | wp->w_wincol = Columns - 3; |
| 306 | } |
| 307 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 308 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 309 | // When centering or right aligned, use maximum width. |
| 310 | // When left aligned use the space available. |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 311 | maxwidth = Columns - wp->w_wincol; |
| 312 | if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth) |
| 313 | maxwidth = wp->w_maxwidth; |
| 314 | |
| 315 | // Compute width based on longest text line and the 'wrap' option. |
| 316 | // TODO: more accurate wrapping |
| 317 | wp->w_width = 0; |
| 318 | for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum) |
| 319 | { |
| 320 | int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE)); |
| 321 | |
| 322 | while (wp->w_p_wrap && len > maxwidth) |
| 323 | { |
| 324 | ++wrapped; |
| 325 | len -= maxwidth; |
| 326 | wp->w_width = maxwidth; |
| 327 | } |
| 328 | if (wp->w_width < len) |
| 329 | wp->w_width = len; |
| 330 | } |
| 331 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 332 | if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth) |
| 333 | wp->w_width = wp->w_minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 334 | if (wp->w_width > maxwidth) |
| 335 | wp->w_width = maxwidth; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 336 | if (center_hor) |
| 337 | wp->w_wincol = (Columns - wp->w_width) / 2; |
| 338 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 339 | || wp->w_popup_pos == POPPOS_TOPRIGHT) |
| 340 | { |
| 341 | // Right aligned: move to the right if needed. |
| 342 | // No truncation, because that would change the height. |
| 343 | if (wp->w_width < wp->w_wantcol) |
| 344 | wp->w_wincol = wp->w_wantcol - wp->w_width; |
| 345 | } |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 346 | |
| 347 | if (wp->w_height <= 1) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 348 | wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 349 | if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight) |
| 350 | wp->w_height = wp->w_minheight; |
| 351 | if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight) |
| 352 | wp->w_height = wp->w_maxheight; |
| 353 | if (wp->w_height > Rows - wp->w_winrow) |
| 354 | wp->w_height = Rows - wp->w_winrow; |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 355 | |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 356 | if (center_vert) |
| 357 | wp->w_winrow = (Rows - wp->w_height) / 2; |
| 358 | else if (wp->w_popup_pos == POPPOS_BOTRIGHT |
| 359 | || wp->w_popup_pos == POPPOS_BOTLEFT) |
| 360 | { |
| 361 | if (wp->w_height <= wp->w_wantline) |
| 362 | // bottom aligned: may move down |
| 363 | wp->w_winrow = wp->w_wantline - wp->w_height; |
| 364 | else |
| 365 | // not enough space, make top aligned |
| 366 | wp->w_winrow = wp->w_wantline + 1; |
| 367 | } |
| 368 | |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 369 | wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 373 | * popup_create({text}, {options}) |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 374 | * popup_atcursor({text}, {options}) |
| 375 | * When called from f_popup_atcursor() "atcursor" is TRUE. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 376 | */ |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 377 | static void |
| 378 | popup_create(typval_T *argvars, typval_T *rettv, int atcursor) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 379 | { |
| 380 | win_T *wp; |
| 381 | buf_T *buf; |
| 382 | dict_T *d; |
| 383 | int nr; |
| 384 | |
| 385 | // Check arguments look OK. |
| 386 | if (!(argvars[0].v_type == VAR_STRING |
| 387 | && argvars[0].vval.v_string != NULL |
| 388 | && STRLEN(argvars[0].vval.v_string) > 0) |
| 389 | && !(argvars[0].v_type == VAR_LIST |
| 390 | && argvars[0].vval.v_list != NULL |
| 391 | && argvars[0].vval.v_list->lv_len > 0)) |
| 392 | { |
| 393 | emsg(_(e_listreq)); |
| 394 | return; |
| 395 | } |
| 396 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 397 | { |
| 398 | emsg(_(e_dictreq)); |
| 399 | return; |
| 400 | } |
| 401 | d = argvars[1].vval.v_dict; |
| 402 | |
| 403 | // Create the window and buffer. |
| 404 | wp = win_alloc_popup_win(); |
| 405 | if (wp == NULL) |
| 406 | return; |
| 407 | rettv->vval.v_number = wp->w_id; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 408 | wp->w_popup_pos = POPPOS_TOPLEFT; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 409 | |
| 410 | buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY); |
| 411 | if (buf == NULL) |
| 412 | return; |
| 413 | ml_open(buf); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 414 | |
| 415 | win_init_popup_win(wp, buf); |
| 416 | |
| 417 | set_local_options_default(wp); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 418 | set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 419 | (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 420 | set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 421 | (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 422 | buf->b_p_ul = -1; // no undo |
| 423 | buf->b_p_swf = FALSE; // no swap file |
| 424 | buf->b_p_bl = FALSE; // unlisted buffer |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 425 | buf->b_locked = TRUE; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 426 | wp->w_p_wrap = TRUE; // 'wrap' is default on |
| 427 | |
Bram Moolenaar | 54fabd4 | 2019-05-30 19:03:22 +0200 | [diff] [blame] | 428 | // Avoid that 'buftype' is reset when this buffer is entered. |
| 429 | buf->b_p_initialized = TRUE; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 430 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 431 | nr = (int)dict_get_number(d, (char_u *)"tab"); |
| 432 | if (nr == 0) |
| 433 | { |
| 434 | // popup on current tab |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 435 | wp->w_next = curtab->tp_first_popupwin; |
| 436 | curtab->tp_first_popupwin = wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 437 | } |
| 438 | else if (nr < 0) |
| 439 | { |
| 440 | // global popup |
| 441 | wp->w_next = first_popupwin; |
| 442 | first_popupwin = wp; |
| 443 | } |
| 444 | else |
| 445 | // TODO: find tab page "nr" |
| 446 | emsg("Not implemented yet"); |
| 447 | |
| 448 | // Add text to the buffer. |
| 449 | if (argvars[0].v_type == VAR_STRING) |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 450 | { |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 451 | // just a string |
| 452 | ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 453 | } |
| 454 | else |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 455 | { |
| 456 | list_T *l = argvars[0].vval.v_list; |
| 457 | |
| 458 | if (l->lv_first->li_tv.v_type == VAR_STRING) |
| 459 | // list of strings |
| 460 | add_popup_strings(buf, l); |
| 461 | else |
| 462 | // list of dictionaries |
| 463 | add_popup_dicts(buf, l); |
| 464 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 465 | |
| 466 | // Delete the line of the empty buffer. |
| 467 | curbuf = buf; |
| 468 | ml_delete(buf->b_ml.ml_line_count, FALSE); |
| 469 | curbuf = curwin->w_buffer; |
| 470 | |
| 471 | // Deal with options. |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 472 | apply_options(wp, buf, argvars[1].vval.v_dict, atcursor); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 473 | |
| 474 | // set default values |
| 475 | if (wp->w_zindex == 0) |
| 476 | wp->w_zindex = 50; |
| 477 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 478 | popup_adjust_position(wp); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 479 | |
| 480 | wp->w_vsep_width = 0; |
| 481 | |
| 482 | redraw_all_later(NOT_VALID); |
| 483 | } |
| 484 | |
| 485 | /* |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 486 | * popup_create({text}, {options}) |
| 487 | */ |
| 488 | void |
| 489 | f_popup_create(typval_T *argvars, typval_T *rettv) |
| 490 | { |
| 491 | popup_create(argvars, rettv, FALSE); |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * popup_atcursor({text}, {options}) |
| 496 | */ |
| 497 | void |
| 498 | f_popup_atcursor(typval_T *argvars, typval_T *rettv) |
| 499 | { |
| 500 | popup_create(argvars, rettv, TRUE); |
| 501 | } |
| 502 | |
| 503 | /* |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 504 | * Find the popup window with window-ID "id". |
| 505 | * If the popup window does not exist NULL is returned. |
| 506 | * If the window is not a popup window, and error message is given. |
| 507 | */ |
| 508 | static win_T * |
| 509 | find_popup_win(int id) |
| 510 | { |
| 511 | win_T *wp = win_id2wp(id); |
| 512 | |
| 513 | if (wp != NULL && !bt_popup(wp->w_buffer)) |
| 514 | { |
| 515 | semsg(_("E993: window %d is not a popup window"), id); |
| 516 | return NULL; |
| 517 | } |
| 518 | return wp; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Return TRUE if there any popups that are not hidden. |
| 523 | */ |
| 524 | int |
| 525 | popup_any_visible(void) |
| 526 | { |
| 527 | win_T *wp; |
| 528 | |
| 529 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 530 | if ((wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 531 | return TRUE; |
| 532 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 533 | if ((wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 534 | return TRUE; |
| 535 | return FALSE; |
| 536 | } |
| 537 | |
| 538 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 539 | * popup_close({id}) |
| 540 | */ |
| 541 | void |
| 542 | f_popup_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 543 | { |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 544 | int id = (int)tv_get_number(argvars); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 545 | |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 546 | popup_close(id); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * popup_hide({id}) |
| 551 | */ |
| 552 | void |
| 553 | f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED) |
| 554 | { |
| 555 | int id = (int)tv_get_number(argvars); |
| 556 | win_T *wp = find_popup_win(id); |
| 557 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 558 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 559 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 560 | wp->w_popup_flags |= POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 561 | --wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 562 | redraw_all_later(NOT_VALID); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * popup_show({id}) |
| 568 | */ |
| 569 | void |
| 570 | f_popup_show(typval_T *argvars, typval_T *rettv UNUSED) |
| 571 | { |
| 572 | int id = (int)tv_get_number(argvars); |
| 573 | win_T *wp = find_popup_win(id); |
| 574 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 575 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 576 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 577 | wp->w_popup_flags &= ~POPF_HIDDEN; |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 578 | ++wp->w_buffer->b_nwindows; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 579 | redraw_all_later(NOT_VALID); |
| 580 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 583 | static void |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 584 | popup_free(win_T *wp) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 585 | { |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 586 | wp->w_buffer->b_locked = FALSE; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 587 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 588 | clear_cmdline = TRUE; |
| 589 | win_free_popup(wp); |
| 590 | redraw_all_later(NOT_VALID); |
| 591 | } |
| 592 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 593 | /* |
| 594 | * Close a popup window by Window-id. |
| 595 | */ |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 596 | void |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 597 | popup_close(int id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 598 | { |
| 599 | win_T *wp; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 600 | tabpage_T *tp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 601 | win_T *prev = NULL; |
| 602 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 603 | // go through global popups |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 604 | for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next) |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 605 | if (wp->w_id == id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 606 | { |
| 607 | if (prev == NULL) |
| 608 | first_popupwin = wp->w_next; |
| 609 | else |
| 610 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 611 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 612 | return; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 615 | // go through tab-local popups |
| 616 | FOR_ALL_TABPAGES(tp) |
| 617 | popup_close_tabpage(tp, id); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Close a popup window with Window-id "id" in tabpage "tp". |
| 622 | */ |
| 623 | void |
| 624 | popup_close_tabpage(tabpage_T *tp, int id) |
| 625 | { |
| 626 | win_T *wp; |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 627 | win_T **root = &tp->tp_first_popupwin; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 628 | win_T *prev = NULL; |
| 629 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 630 | for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next) |
| 631 | if (wp->w_id == id) |
| 632 | { |
| 633 | if (prev == NULL) |
| 634 | *root = wp->w_next; |
| 635 | else |
| 636 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 637 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 638 | return; |
| 639 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void |
| 643 | close_all_popups(void) |
| 644 | { |
| 645 | while (first_popupwin != NULL) |
| 646 | popup_close(first_popupwin->w_id); |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 647 | while (curtab->tp_first_popupwin != NULL) |
| 648 | popup_close(curtab->tp_first_popupwin->w_id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void |
| 652 | ex_popupclear(exarg_T *eap UNUSED) |
| 653 | { |
| 654 | close_all_popups(); |
| 655 | } |
| 656 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 657 | /* |
| 658 | * popup_move({id}, {options}) |
| 659 | */ |
| 660 | void |
| 661 | f_popup_move(typval_T *argvars, typval_T *rettv UNUSED) |
| 662 | { |
| 663 | dict_T *d; |
| 664 | int nr; |
| 665 | int id = (int)tv_get_number(argvars); |
| 666 | win_T *wp = find_popup_win(id); |
| 667 | |
| 668 | if (wp == NULL) |
| 669 | return; // invalid {id} |
| 670 | |
| 671 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 672 | { |
| 673 | emsg(_(e_dictreq)); |
| 674 | return; |
| 675 | } |
| 676 | d = argvars[1].vval.v_dict; |
| 677 | |
| 678 | if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0) |
| 679 | wp->w_minwidth = nr; |
| 680 | if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0) |
| 681 | wp->w_minheight = nr; |
| 682 | if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0) |
| 683 | wp->w_maxwidth = nr; |
| 684 | if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0) |
| 685 | wp->w_maxheight = nr; |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 686 | get_pos_options(wp, d); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 687 | |
| 688 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 689 | clear_cmdline = TRUE; |
| 690 | popup_adjust_position(wp); |
| 691 | redraw_all_later(NOT_VALID); |
| 692 | } |
| 693 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 694 | /* |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 695 | * popup_getpos({id}) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 696 | */ |
| 697 | void |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 698 | f_popup_getpos(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 699 | { |
| 700 | dict_T *dict; |
| 701 | int id = (int)tv_get_number(argvars); |
| 702 | win_T *wp = find_popup_win(id); |
| 703 | |
| 704 | if (rettv_dict_alloc(rettv) == OK) |
| 705 | { |
| 706 | if (wp == NULL) |
| 707 | return; // invalid {id} |
| 708 | dict = rettv->vval.v_dict; |
| 709 | dict_add_number(dict, "line", wp->w_winrow + 1); |
| 710 | dict_add_number(dict, "col", wp->w_wincol + 1); |
| 711 | dict_add_number(dict, "width", wp->w_width); |
| 712 | dict_add_number(dict, "height", wp->w_height); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 713 | dict_add_number(dict, "visible", |
| 714 | (wp->w_popup_flags & POPF_HIDDEN) == 0); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * f_popup_getoptions({id}) |
| 720 | */ |
| 721 | void |
| 722 | f_popup_getoptions(typval_T *argvars, typval_T *rettv) |
| 723 | { |
| 724 | dict_T *dict; |
| 725 | int id = (int)tv_get_number(argvars); |
| 726 | win_T *wp = find_popup_win(id); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 727 | int i; |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 728 | |
| 729 | if (rettv_dict_alloc(rettv) == OK) |
| 730 | { |
| 731 | if (wp == NULL) |
| 732 | return; |
| 733 | |
| 734 | dict = rettv->vval.v_dict; |
| 735 | dict_add_number(dict, "line", wp->w_wantline); |
| 736 | dict_add_number(dict, "col", wp->w_wantcol); |
| 737 | dict_add_number(dict, "minwidth", wp->w_minwidth); |
| 738 | dict_add_number(dict, "minheight", wp->w_minheight); |
| 739 | dict_add_number(dict, "maxheight", wp->w_maxheight); |
| 740 | dict_add_number(dict, "maxwidth", wp->w_maxwidth); |
| 741 | dict_add_number(dict, "zindex", wp->w_zindex); |
Bram Moolenaar | ac1f1bc | 2019-05-30 21:24:26 +0200 | [diff] [blame] | 742 | |
| 743 | for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T)); |
| 744 | ++i) |
| 745 | if (wp->w_popup_pos == poppos_entries[i].pp_val) |
| 746 | { |
| 747 | dict_add_string(dict, "pos", |
| 748 | (char_u *)poppos_entries[i].pp_name); |
| 749 | break; |
| 750 | } |
| 751 | |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 752 | # if defined(FEAT_TIMERS) |
| 753 | dict_add_number(dict, "time", wp->w_popup_timer != NULL |
| 754 | ? (long)wp->w_popup_timer->tr_interval : 0L); |
| 755 | # endif |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 756 | } |
| 757 | } |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 758 | |
| 759 | int |
| 760 | not_in_popup_window() |
| 761 | { |
| 762 | if (bt_popup(curwin->w_buffer)) |
| 763 | { |
| 764 | emsg(_("E994: Not allowed in a popup window")); |
| 765 | return TRUE; |
| 766 | } |
| 767 | return FALSE; |
| 768 | } |
| 769 | |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 770 | /* |
| 771 | * Reset all the POPF_HANDLED flags in global popup windows and popup windows |
| 772 | * in the current tab. |
| 773 | */ |
| 774 | void |
| 775 | popup_reset_handled() |
| 776 | { |
| 777 | win_T *wp; |
| 778 | |
| 779 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 780 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 781 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 782 | wp->w_popup_flags &= ~POPF_HANDLED; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Find the next visible popup where POPF_HANDLED is not set. |
| 787 | * Must have called popup_reset_handled() first. |
| 788 | * When "lowest" is TRUE find the popup with the lowest zindex, otherwise the |
| 789 | * popup with the highest zindex. |
| 790 | */ |
| 791 | win_T * |
| 792 | find_next_popup(int lowest) |
| 793 | { |
| 794 | win_T *wp; |
| 795 | win_T *found_wp; |
| 796 | int found_zindex; |
| 797 | |
| 798 | found_zindex = lowest ? INT_MAX : 0; |
| 799 | found_wp = NULL; |
| 800 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
| 801 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 802 | && (lowest ? wp->w_zindex < found_zindex |
| 803 | : wp->w_zindex > found_zindex)) |
| 804 | { |
| 805 | found_zindex = wp->w_zindex; |
| 806 | found_wp = wp; |
| 807 | } |
| 808 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
| 809 | if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0 |
| 810 | && (lowest ? wp->w_zindex < found_zindex |
| 811 | : wp->w_zindex > found_zindex)) |
| 812 | { |
| 813 | found_zindex = wp->w_zindex; |
| 814 | found_wp = wp; |
| 815 | } |
| 816 | |
| 817 | if (found_wp != NULL) |
| 818 | found_wp->w_popup_flags |= POPF_HANDLED; |
| 819 | return found_wp; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Invoke the filter callback for window "wp" with typed character "c". |
| 824 | * Uses the global "mod_mask" for modifiers. |
| 825 | * Returns the return value of the filter. |
| 826 | * Careful: The filter may make "wp" invalid! |
| 827 | */ |
| 828 | static int |
| 829 | invoke_popup_filter(win_T *wp, int c) |
| 830 | { |
| 831 | int res; |
| 832 | typval_T rettv; |
| 833 | int dummy; |
| 834 | typval_T argv[3]; |
| 835 | char_u buf[NUMBUFLEN]; |
| 836 | |
| 837 | argv[0].v_type = VAR_NUMBER; |
| 838 | argv[0].vval.v_number = (varnumber_T)wp->w_id; |
| 839 | |
| 840 | // Convert the number to a string, so that the function can use: |
| 841 | // if a:c == "\<F2>" |
| 842 | buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL; |
| 843 | argv[1].v_type = VAR_STRING; |
| 844 | argv[1].vval.v_string = vim_strsave(buf); |
| 845 | |
| 846 | argv[2].v_type = VAR_UNKNOWN; |
| 847 | |
| 848 | call_callback(&wp->w_filter_cb, -1, |
| 849 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL); |
| 850 | res = tv_get_number(&rettv); |
| 851 | vim_free(argv[1].vval.v_string); |
| 852 | clear_tv(&rettv); |
| 853 | return res; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * Called when "c" was typed: invoke popup filter callbacks. |
| 858 | * Returns TRUE when the character was consumed, |
| 859 | */ |
| 860 | int |
| 861 | popup_do_filter(int c) |
| 862 | { |
| 863 | int res = FALSE; |
| 864 | win_T *wp; |
| 865 | |
| 866 | popup_reset_handled(); |
| 867 | |
| 868 | while (!res && (wp = find_next_popup(FALSE)) != NULL) |
| 869 | if (wp->w_filter_cb.cb_name != NULL) |
| 870 | res = invoke_popup_filter(wp, c); |
| 871 | |
| 872 | return res; |
| 873 | } |
| 874 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 875 | #endif // FEAT_TEXT_PROP |