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 | |
| 18 | /* |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 19 | * Get option value for"key", which is "line" or "col". |
| 20 | * Handles "cursor+N" and "cursor-N". |
| 21 | */ |
| 22 | static int |
| 23 | popup_options_pos(dict_T *dict, char_u *key) |
| 24 | { |
| 25 | dictitem_T *di; |
| 26 | char_u *val; |
| 27 | char_u *s; |
| 28 | char_u *endp; |
| 29 | int n = 0; |
| 30 | |
| 31 | di = dict_find(dict, key, -1); |
| 32 | if (di == NULL) |
| 33 | return 0; |
| 34 | |
| 35 | val = tv_get_string(&di->di_tv); |
| 36 | if (STRNCMP(val, "cursor", 6) != 0) |
| 37 | return dict_get_number(dict, key); |
| 38 | |
| 39 | setcursor_mayforce(TRUE); |
| 40 | s = val + 6; |
| 41 | if (*s != NUL) |
| 42 | { |
| 43 | n = strtol((char *)s, (char **)&endp, 10); |
| 44 | if (endp != NULL && *skipwhite(endp) != NUL) |
| 45 | { |
| 46 | semsg(_(e_invexpr2), val); |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (STRCMP(key, "line") == 0) |
| 52 | n = screen_screenrow() + 1 + n; |
| 53 | else // "col" |
| 54 | n = screen_screencol() + 1 + n; |
| 55 | |
| 56 | if (n < 1) |
| 57 | n = 1; |
| 58 | return n; |
| 59 | } |
| 60 | |
| 61 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 62 | * Go through the options in "dict" and apply them to buffer "buf" displayed in |
| 63 | * popup window "wp". |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 64 | * When called from f_popup_atcursor() "atcursor" is TRUE. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 65 | */ |
| 66 | static void |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 67 | 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] | 68 | { |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 69 | int nr; |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 70 | char_u *str; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 71 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 72 | wp->w_minwidth = dict_get_number(dict, (char_u *)"minwidth"); |
| 73 | wp->w_minheight = dict_get_number(dict, (char_u *)"minheight"); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 74 | wp->w_maxwidth = dict_get_number(dict, (char_u *)"maxwidth"); |
| 75 | wp->w_maxheight = dict_get_number(dict, (char_u *)"maxheight"); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 76 | |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 77 | if (atcursor) |
| 78 | { |
| 79 | setcursor_mayforce(TRUE); |
| 80 | wp->w_wantline = screen_screenrow(); |
| 81 | wp->w_wantcol = screen_screencol() + 1; |
| 82 | } |
| 83 | |
| 84 | nr = popup_options_pos(dict, (char_u *)"line"); |
| 85 | if (nr > 0) |
| 86 | wp->w_wantline = nr; |
| 87 | nr = popup_options_pos(dict, (char_u *)"col"); |
| 88 | if (nr > 0) |
| 89 | wp->w_wantcol = nr; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 90 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 91 | wp->w_zindex = dict_get_number(dict, (char_u *)"zindex"); |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 92 | |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 93 | #if defined(FEAT_TIMERS) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 94 | // Add timer to close the popup after some time. |
| 95 | nr = dict_get_number(dict, (char_u *)"time"); |
| 96 | if (nr > 0) |
| 97 | { |
| 98 | char_u cbbuf[50]; |
| 99 | char_u *ptr = cbbuf; |
| 100 | typval_T tv; |
| 101 | |
| 102 | vim_snprintf((char *)cbbuf, sizeof(cbbuf), |
| 103 | "{_ -> popup_close(%d)}", wp->w_id); |
| 104 | if (get_lambda_tv(&ptr, &tv, TRUE) == OK) |
| 105 | { |
| 106 | wp->w_popup_timer = create_timer(nr, 0); |
| 107 | wp->w_popup_timer->tr_callback = |
| 108 | vim_strsave(partial_name(tv.vval.v_partial)); |
| 109 | func_ref(wp->w_popup_timer->tr_callback); |
| 110 | wp->w_popup_timer->tr_partial = tv.vval.v_partial; |
| 111 | } |
| 112 | } |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 113 | #endif |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 114 | |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 115 | str = dict_get_string(dict, (char_u *)"highlight", TRUE); |
| 116 | if (str != NULL) |
| 117 | set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1, |
| 118 | str, OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 122 | * Add lines to the popup from a list of strings. |
| 123 | */ |
| 124 | static void |
| 125 | add_popup_strings(buf_T *buf, list_T *l) |
| 126 | { |
| 127 | listitem_T *li; |
| 128 | linenr_T lnum = 0; |
| 129 | char_u *p; |
| 130 | |
| 131 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 132 | if (li->li_tv.v_type == VAR_STRING) |
| 133 | { |
| 134 | p = li->li_tv.vval.v_string; |
| 135 | ml_append_buf(buf, lnum++, |
| 136 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Add lines to the popup from a list of dictionaries. |
| 142 | */ |
| 143 | static void |
| 144 | add_popup_dicts(buf_T *buf, list_T *l) |
| 145 | { |
| 146 | listitem_T *li; |
| 147 | listitem_T *pli; |
| 148 | linenr_T lnum = 0; |
| 149 | char_u *p; |
| 150 | dict_T *dict; |
| 151 | |
| 152 | // first add the text lines |
| 153 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 154 | { |
| 155 | if (li->li_tv.v_type != VAR_DICT) |
| 156 | { |
| 157 | emsg(_(e_dictreq)); |
| 158 | return; |
| 159 | } |
| 160 | dict = li->li_tv.vval.v_dict; |
| 161 | p = dict == NULL ? NULL |
| 162 | : dict_get_string(dict, (char_u *)"text", FALSE); |
| 163 | ml_append_buf(buf, lnum++, |
| 164 | p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE); |
| 165 | } |
| 166 | |
| 167 | // add the text properties |
| 168 | lnum = 1; |
| 169 | for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum) |
| 170 | { |
| 171 | dictitem_T *di; |
| 172 | list_T *plist; |
| 173 | |
| 174 | dict = li->li_tv.vval.v_dict; |
| 175 | di = dict_find(dict, (char_u *)"props", -1); |
| 176 | if (di != NULL) |
| 177 | { |
| 178 | if (di->di_tv.v_type != VAR_LIST) |
| 179 | { |
| 180 | emsg(_(e_listreq)); |
| 181 | return; |
| 182 | } |
| 183 | plist = di->di_tv.vval.v_list; |
| 184 | if (plist != NULL) |
| 185 | { |
| 186 | for (pli = plist->lv_first; pli != NULL; pli = pli->li_next) |
| 187 | { |
| 188 | if (pli->li_tv.v_type != VAR_DICT) |
| 189 | { |
| 190 | emsg(_(e_dictreq)); |
| 191 | return; |
| 192 | } |
| 193 | dict = pli->li_tv.vval.v_dict; |
| 194 | if (dict != NULL) |
| 195 | { |
| 196 | int col = dict_get_number(dict, (char_u *)"col"); |
| 197 | |
| 198 | prop_add_common( lnum, col, dict, buf, NULL); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 207 | * Adjust the position and size of the popup to fit on the screen. |
| 208 | */ |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 209 | void |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 210 | popup_adjust_position(win_T *wp) |
| 211 | { |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 212 | linenr_T lnum; |
| 213 | int wrapped = 0; |
| 214 | int maxwidth; |
| 215 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 216 | // TODO: Compute the size and position properly. |
| 217 | if (wp->w_wantline > 0) |
| 218 | wp->w_winrow = wp->w_wantline - 1; |
| 219 | else |
| 220 | // TODO: better default |
| 221 | wp->w_winrow = Rows > 5 ? Rows / 2 - 2 : 0; |
| 222 | if (wp->w_winrow >= Rows) |
| 223 | wp->w_winrow = Rows - 1; |
| 224 | |
| 225 | if (wp->w_wantcol > 0) |
| 226 | wp->w_wincol = wp->w_wantcol - 1; |
| 227 | else |
| 228 | // TODO: better default |
| 229 | wp->w_wincol = Columns > 20 ? Columns / 2 - 10 : 0; |
| 230 | if (wp->w_wincol >= Columns - 3) |
| 231 | wp->w_wincol = Columns - 3; |
| 232 | |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 233 | maxwidth = Columns - wp->w_wincol; |
| 234 | if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth) |
| 235 | maxwidth = wp->w_maxwidth; |
| 236 | |
| 237 | // Compute width based on longest text line and the 'wrap' option. |
| 238 | // TODO: more accurate wrapping |
| 239 | wp->w_width = 0; |
| 240 | for (lnum = 1; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum) |
| 241 | { |
| 242 | int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE)); |
| 243 | |
| 244 | while (wp->w_p_wrap && len > maxwidth) |
| 245 | { |
| 246 | ++wrapped; |
| 247 | len -= maxwidth; |
| 248 | wp->w_width = maxwidth; |
| 249 | } |
| 250 | if (wp->w_width < len) |
| 251 | wp->w_width = len; |
| 252 | } |
| 253 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 254 | if (wp->w_minwidth > 0 && wp->w_width < wp->w_minwidth) |
| 255 | wp->w_width = wp->w_minwidth; |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 256 | if (wp->w_width > maxwidth) |
| 257 | wp->w_width = maxwidth; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 258 | |
| 259 | if (wp->w_height <= 1) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 260 | wp->w_height = wp->w_buffer->b_ml.ml_line_count + wrapped; |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 261 | if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight) |
| 262 | wp->w_height = wp->w_minheight; |
| 263 | if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight) |
| 264 | wp->w_height = wp->w_maxheight; |
| 265 | if (wp->w_height > Rows - wp->w_winrow) |
| 266 | wp->w_height = Rows - wp->w_winrow; |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 267 | |
| 268 | wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 272 | * popup_create({text}, {options}) |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 273 | * popup_atcursor({text}, {options}) |
| 274 | * When called from f_popup_atcursor() "atcursor" is TRUE. |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 275 | */ |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 276 | static void |
| 277 | popup_create(typval_T *argvars, typval_T *rettv, int atcursor) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 278 | { |
| 279 | win_T *wp; |
| 280 | buf_T *buf; |
| 281 | dict_T *d; |
| 282 | int nr; |
| 283 | |
| 284 | // Check arguments look OK. |
| 285 | if (!(argvars[0].v_type == VAR_STRING |
| 286 | && argvars[0].vval.v_string != NULL |
| 287 | && STRLEN(argvars[0].vval.v_string) > 0) |
| 288 | && !(argvars[0].v_type == VAR_LIST |
| 289 | && argvars[0].vval.v_list != NULL |
| 290 | && argvars[0].vval.v_list->lv_len > 0)) |
| 291 | { |
| 292 | emsg(_(e_listreq)); |
| 293 | return; |
| 294 | } |
| 295 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 296 | { |
| 297 | emsg(_(e_dictreq)); |
| 298 | return; |
| 299 | } |
| 300 | d = argvars[1].vval.v_dict; |
| 301 | |
| 302 | // Create the window and buffer. |
| 303 | wp = win_alloc_popup_win(); |
| 304 | if (wp == NULL) |
| 305 | return; |
| 306 | rettv->vval.v_number = wp->w_id; |
| 307 | wp->w_p_wrap = TRUE; // 'wrap' is default on |
| 308 | |
| 309 | buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY); |
| 310 | if (buf == NULL) |
| 311 | return; |
| 312 | ml_open(buf); |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 313 | |
| 314 | win_init_popup_win(wp, buf); |
| 315 | |
| 316 | set_local_options_default(wp); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 317 | set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 318 | (char_u *)"popup", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 319 | set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1, |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 320 | (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 321 | buf->b_p_ul = -1; // no undo |
| 322 | buf->b_p_swf = FALSE; // no swap file |
| 323 | buf->b_p_bl = FALSE; // unlisted buffer |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 324 | buf->b_locked = TRUE; |
Bram Moolenaar | 54fabd4 | 2019-05-30 19:03:22 +0200 | [diff] [blame] | 325 | // Avoid that 'buftype' is reset when this buffer is entered. |
| 326 | buf->b_p_initialized = TRUE; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 327 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 328 | nr = (int)dict_get_number(d, (char_u *)"tab"); |
| 329 | if (nr == 0) |
| 330 | { |
| 331 | // popup on current tab |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 332 | wp->w_next = curtab->tp_first_popupwin; |
| 333 | curtab->tp_first_popupwin = wp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 334 | } |
| 335 | else if (nr < 0) |
| 336 | { |
| 337 | // global popup |
| 338 | wp->w_next = first_popupwin; |
| 339 | first_popupwin = wp; |
| 340 | } |
| 341 | else |
| 342 | // TODO: find tab page "nr" |
| 343 | emsg("Not implemented yet"); |
| 344 | |
| 345 | // Add text to the buffer. |
| 346 | if (argvars[0].v_type == VAR_STRING) |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 347 | { |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 348 | // just a string |
| 349 | 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] | 350 | } |
| 351 | else |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 352 | { |
| 353 | list_T *l = argvars[0].vval.v_list; |
| 354 | |
| 355 | if (l->lv_first->li_tv.v_type == VAR_STRING) |
| 356 | // list of strings |
| 357 | add_popup_strings(buf, l); |
| 358 | else |
| 359 | // list of dictionaries |
| 360 | add_popup_dicts(buf, l); |
| 361 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 362 | |
| 363 | // Delete the line of the empty buffer. |
| 364 | curbuf = buf; |
| 365 | ml_delete(buf->b_ml.ml_line_count, FALSE); |
| 366 | curbuf = curwin->w_buffer; |
| 367 | |
| 368 | // Deal with options. |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 369 | apply_options(wp, buf, argvars[1].vval.v_dict, atcursor); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 370 | |
| 371 | // set default values |
| 372 | if (wp->w_zindex == 0) |
| 373 | wp->w_zindex = 50; |
| 374 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 375 | popup_adjust_position(wp); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 376 | |
| 377 | wp->w_vsep_width = 0; |
| 378 | |
| 379 | redraw_all_later(NOT_VALID); |
| 380 | } |
| 381 | |
| 382 | /* |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame^] | 383 | * popup_create({text}, {options}) |
| 384 | */ |
| 385 | void |
| 386 | f_popup_create(typval_T *argvars, typval_T *rettv) |
| 387 | { |
| 388 | popup_create(argvars, rettv, FALSE); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * popup_atcursor({text}, {options}) |
| 393 | */ |
| 394 | void |
| 395 | f_popup_atcursor(typval_T *argvars, typval_T *rettv) |
| 396 | { |
| 397 | popup_create(argvars, rettv, TRUE); |
| 398 | } |
| 399 | |
| 400 | /* |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 401 | * Find the popup window with window-ID "id". |
| 402 | * If the popup window does not exist NULL is returned. |
| 403 | * If the window is not a popup window, and error message is given. |
| 404 | */ |
| 405 | static win_T * |
| 406 | find_popup_win(int id) |
| 407 | { |
| 408 | win_T *wp = win_id2wp(id); |
| 409 | |
| 410 | if (wp != NULL && !bt_popup(wp->w_buffer)) |
| 411 | { |
| 412 | semsg(_("E993: window %d is not a popup window"), id); |
| 413 | return NULL; |
| 414 | } |
| 415 | return wp; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Return TRUE if there any popups that are not hidden. |
| 420 | */ |
| 421 | int |
| 422 | popup_any_visible(void) |
| 423 | { |
| 424 | win_T *wp; |
| 425 | |
| 426 | for (wp = first_popupwin; wp != NULL; wp = wp->w_next) |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 427 | if ((wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 428 | return TRUE; |
| 429 | for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next) |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 430 | if ((wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 431 | return TRUE; |
| 432 | return FALSE; |
| 433 | } |
| 434 | |
| 435 | /* |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 436 | * popup_close({id}) |
| 437 | */ |
| 438 | void |
| 439 | f_popup_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 440 | { |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 441 | int id = (int)tv_get_number(argvars); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 442 | |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 443 | popup_close(id); |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * popup_hide({id}) |
| 448 | */ |
| 449 | void |
| 450 | f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED) |
| 451 | { |
| 452 | int id = (int)tv_get_number(argvars); |
| 453 | win_T *wp = find_popup_win(id); |
| 454 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 455 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 456 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 457 | wp->w_popup_flags |= POPF_HIDDEN; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 458 | redraw_all_later(NOT_VALID); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * popup_show({id}) |
| 464 | */ |
| 465 | void |
| 466 | f_popup_show(typval_T *argvars, typval_T *rettv UNUSED) |
| 467 | { |
| 468 | int id = (int)tv_get_number(argvars); |
| 469 | win_T *wp = find_popup_win(id); |
| 470 | |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 471 | if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 472 | { |
Bram Moolenaar | bf0ecb2 | 2019-05-27 10:04:40 +0200 | [diff] [blame] | 473 | wp->w_popup_flags &= ~POPF_HIDDEN; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 474 | redraw_all_later(NOT_VALID); |
| 475 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 478 | static void |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 479 | popup_free(win_T *wp) |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 480 | { |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 481 | wp->w_buffer->b_locked = FALSE; |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 482 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 483 | clear_cmdline = TRUE; |
| 484 | win_free_popup(wp); |
| 485 | redraw_all_later(NOT_VALID); |
| 486 | } |
| 487 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 488 | /* |
| 489 | * Close a popup window by Window-id. |
| 490 | */ |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 491 | void |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 492 | popup_close(int id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 493 | { |
| 494 | win_T *wp; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 495 | tabpage_T *tp; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 496 | win_T *prev = NULL; |
| 497 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 498 | // go through global popups |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 499 | for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next) |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 500 | if (wp->w_id == id) |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 501 | { |
| 502 | if (prev == NULL) |
| 503 | first_popupwin = wp->w_next; |
| 504 | else |
| 505 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 506 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 507 | return; |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 510 | // go through tab-local popups |
| 511 | FOR_ALL_TABPAGES(tp) |
| 512 | popup_close_tabpage(tp, id); |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Close a popup window with Window-id "id" in tabpage "tp". |
| 517 | */ |
| 518 | void |
| 519 | popup_close_tabpage(tabpage_T *tp, int id) |
| 520 | { |
| 521 | win_T *wp; |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 522 | win_T **root = &tp->tp_first_popupwin; |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 523 | win_T *prev = NULL; |
| 524 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 525 | for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next) |
| 526 | if (wp->w_id == id) |
| 527 | { |
| 528 | if (prev == NULL) |
| 529 | *root = wp->w_next; |
| 530 | else |
| 531 | prev->w_next = wp->w_next; |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 532 | popup_free(wp); |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 533 | return; |
| 534 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | void |
| 538 | close_all_popups(void) |
| 539 | { |
| 540 | while (first_popupwin != NULL) |
| 541 | popup_close(first_popupwin->w_id); |
Bram Moolenaar | 9c27b1c | 2019-05-26 18:48:13 +0200 | [diff] [blame] | 542 | while (curtab->tp_first_popupwin != NULL) |
| 543 | popup_close(curtab->tp_first_popupwin->w_id); |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void |
| 547 | ex_popupclear(exarg_T *eap UNUSED) |
| 548 | { |
| 549 | close_all_popups(); |
| 550 | } |
| 551 | |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 552 | /* |
| 553 | * popup_move({id}, {options}) |
| 554 | */ |
| 555 | void |
| 556 | f_popup_move(typval_T *argvars, typval_T *rettv UNUSED) |
| 557 | { |
| 558 | dict_T *d; |
| 559 | int nr; |
| 560 | int id = (int)tv_get_number(argvars); |
| 561 | win_T *wp = find_popup_win(id); |
| 562 | |
| 563 | if (wp == NULL) |
| 564 | return; // invalid {id} |
| 565 | |
| 566 | if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL) |
| 567 | { |
| 568 | emsg(_(e_dictreq)); |
| 569 | return; |
| 570 | } |
| 571 | d = argvars[1].vval.v_dict; |
| 572 | |
| 573 | if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0) |
| 574 | wp->w_minwidth = nr; |
| 575 | if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0) |
| 576 | wp->w_minheight = nr; |
| 577 | if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0) |
| 578 | wp->w_maxwidth = nr; |
| 579 | if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0) |
| 580 | wp->w_maxheight = nr; |
| 581 | if ((nr = dict_get_number(d, (char_u *)"line")) > 0) |
| 582 | wp->w_wantline = nr; |
| 583 | if ((nr = dict_get_number(d, (char_u *)"col")) > 0) |
| 584 | wp->w_wantcol = nr; |
| 585 | // TODO: "pos" |
| 586 | |
| 587 | if (wp->w_winrow + wp->w_height >= cmdline_row) |
| 588 | clear_cmdline = TRUE; |
| 589 | popup_adjust_position(wp); |
| 590 | redraw_all_later(NOT_VALID); |
| 591 | } |
| 592 | |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 593 | /* |
| 594 | * popup_getposition({id}) |
| 595 | */ |
| 596 | void |
| 597 | f_popup_getposition(typval_T *argvars, typval_T *rettv) |
| 598 | { |
| 599 | dict_T *dict; |
| 600 | int id = (int)tv_get_number(argvars); |
| 601 | win_T *wp = find_popup_win(id); |
| 602 | |
| 603 | if (rettv_dict_alloc(rettv) == OK) |
| 604 | { |
| 605 | if (wp == NULL) |
| 606 | return; // invalid {id} |
| 607 | dict = rettv->vval.v_dict; |
| 608 | dict_add_number(dict, "line", wp->w_winrow + 1); |
| 609 | dict_add_number(dict, "col", wp->w_wincol + 1); |
| 610 | dict_add_number(dict, "width", wp->w_width); |
| 611 | dict_add_number(dict, "height", wp->w_height); |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 612 | dict_add_number(dict, "visible", |
| 613 | (wp->w_popup_flags & POPF_HIDDEN) == 0); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * f_popup_getoptions({id}) |
| 619 | */ |
| 620 | void |
| 621 | f_popup_getoptions(typval_T *argvars, typval_T *rettv) |
| 622 | { |
| 623 | dict_T *dict; |
| 624 | int id = (int)tv_get_number(argvars); |
| 625 | win_T *wp = find_popup_win(id); |
| 626 | |
| 627 | if (rettv_dict_alloc(rettv) == OK) |
| 628 | { |
| 629 | if (wp == NULL) |
| 630 | return; |
| 631 | |
| 632 | dict = rettv->vval.v_dict; |
| 633 | dict_add_number(dict, "line", wp->w_wantline); |
| 634 | dict_add_number(dict, "col", wp->w_wantcol); |
| 635 | dict_add_number(dict, "minwidth", wp->w_minwidth); |
| 636 | dict_add_number(dict, "minheight", wp->w_minheight); |
| 637 | dict_add_number(dict, "maxheight", wp->w_maxheight); |
| 638 | dict_add_number(dict, "maxwidth", wp->w_maxwidth); |
| 639 | dict_add_number(dict, "zindex", wp->w_zindex); |
| 640 | # if defined(FEAT_TIMERS) |
| 641 | dict_add_number(dict, "time", wp->w_popup_timer != NULL |
| 642 | ? (long)wp->w_popup_timer->tr_interval : 0L); |
| 643 | # endif |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 644 | } |
| 645 | } |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 646 | #endif // FEAT_TEXT_PROP |