vim.c (81667B)
1 #include <assert.h> 2 #include <inttypes.h> 3 #include <lauxlib.h> 4 #include <limits.h> 5 #include <stdbool.h> 6 #include <stddef.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 11 #include "klib/kvec.h" 12 #include "nvim/api/buffer.h" 13 #include "nvim/api/deprecated.h" 14 #include "nvim/api/keysets_defs.h" 15 #include "nvim/api/private/converter.h" 16 #include "nvim/api/private/defs.h" 17 #include "nvim/api/private/dispatch.h" 18 #include "nvim/api/private/helpers.h" 19 #include "nvim/api/private/validate.h" 20 #include "nvim/api/vim.h" 21 #include "nvim/ascii_defs.h" 22 #include "nvim/autocmd.h" 23 #include "nvim/autocmd_defs.h" 24 #include "nvim/buffer.h" 25 #include "nvim/buffer_defs.h" 26 #include "nvim/channel.h" 27 #include "nvim/channel_defs.h" 28 #include "nvim/context.h" 29 #include "nvim/cursor.h" 30 #include "nvim/decoration.h" 31 #include "nvim/drawline.h" 32 #include "nvim/drawscreen.h" 33 #include "nvim/errors.h" 34 #include "nvim/eval/typval.h" 35 #include "nvim/eval/typval_defs.h" 36 #include "nvim/eval/vars.h" 37 #include "nvim/ex_docmd.h" 38 #include "nvim/ex_eval.h" 39 #include "nvim/fold.h" 40 #include "nvim/getchar.h" 41 #include "nvim/getchar_defs.h" 42 #include "nvim/globals.h" 43 #include "nvim/grid.h" 44 #include "nvim/grid_defs.h" 45 #include "nvim/highlight.h" 46 #include "nvim/highlight_defs.h" 47 #include "nvim/highlight_group.h" 48 #include "nvim/insexpand.h" 49 #include "nvim/keycodes.h" 50 #include "nvim/log.h" 51 #include "nvim/lua/executor.h" 52 #include "nvim/lua/treesitter.h" 53 #include "nvim/macros_defs.h" 54 #include "nvim/mapping.h" 55 #include "nvim/mark.h" 56 #include "nvim/mark_defs.h" 57 #include "nvim/math.h" 58 #include "nvim/mbyte.h" 59 #include "nvim/memline.h" 60 #include "nvim/memory.h" 61 #include "nvim/memory_defs.h" 62 #include "nvim/message.h" 63 #include "nvim/message_defs.h" 64 #include "nvim/move.h" 65 #include "nvim/msgpack_rpc/channel.h" 66 #include "nvim/msgpack_rpc/channel_defs.h" 67 #include "nvim/msgpack_rpc/unpacker.h" 68 #include "nvim/normal.h" 69 #include "nvim/option.h" 70 #include "nvim/option_defs.h" 71 #include "nvim/option_vars.h" 72 #include "nvim/optionstr.h" 73 #include "nvim/os/input.h" 74 #include "nvim/os/os_defs.h" 75 #include "nvim/os/proc.h" 76 #include "nvim/popupmenu.h" 77 #include "nvim/pos_defs.h" 78 #include "nvim/register.h" 79 #include "nvim/runtime.h" 80 #include "nvim/sign_defs.h" 81 #include "nvim/state.h" 82 #include "nvim/statusline.h" 83 #include "nvim/statusline_defs.h" 84 #include "nvim/terminal.h" 85 #include "nvim/types_defs.h" 86 #include "nvim/ui.h" 87 #include "nvim/vim_defs.h" 88 #include "nvim/window.h" 89 90 #include "api/vim.c.generated.h" 91 92 /// Gets a highlight group by name 93 /// 94 /// similar to |hlID()|, but allocates a new ID if not present. 95 Integer nvim_get_hl_id_by_name(String name) 96 FUNC_API_SINCE(7) 97 { 98 return syn_check_group(name.data, name.size); 99 } 100 101 /// Gets all or specific highlight groups in a namespace. 102 /// 103 /// @note When the `link` attribute is defined in the highlight definition 104 /// map, other attributes will not be taking effect (see |:hi-link|). 105 /// 106 /// @param ns_id Get highlight groups for namespace ns_id |nvim_get_namespaces()|. 107 /// Use 0 to get global highlight groups |:highlight|. 108 /// @param opts Options dict: 109 /// - name: (string) Get a highlight definition by name. 110 /// - id: (integer) Get a highlight definition by id. 111 /// - link: (boolean, default true) Show linked group name instead of effective definition |:hi-link|. 112 /// - create: (boolean, default true) When highlight group doesn't exist create it. 113 /// 114 /// @param[out] err Error details, if any. 115 /// @return Highlight groups as a map from group name to a highlight definition map as in |nvim_set_hl()|, 116 /// or only a single highlight definition map if requested by name or id. 117 DictAs(get_hl_info) nvim_get_hl(Integer ns_id, Dict(get_highlight) *opts, Arena *arena, Error *err) 118 FUNC_API_SINCE(11) 119 { 120 return ns_get_hl_defs((NS)ns_id, opts, arena, err); 121 } 122 123 /// Sets a highlight group. 124 /// 125 /// @note Unlike the `:highlight` command which can update a highlight group, 126 /// this function completely replaces the definition. For example: 127 /// `nvim_set_hl(0, 'Visual', {})` will clear the highlight group 128 /// 'Visual'. 129 /// 130 /// @note The fg and bg keys also accept the string values `"fg"` or `"bg"` 131 /// which act as aliases to the corresponding foreground and background 132 /// values of the Normal group. If the Normal group has not been defined, 133 /// using these values results in an error. 134 /// 135 /// @note If `link` is used in combination with other attributes; only the 136 /// `link` will take effect (see |:hi-link|). 137 /// 138 /// @param ns_id Namespace id for this highlight |nvim_create_namespace()|. 139 /// Use 0 to set a highlight group globally |:highlight|. 140 /// Highlights from non-global namespaces are not active by default, use 141 /// |nvim_set_hl_ns()| or |nvim_win_set_hl_ns()| to activate them. 142 /// @param name Highlight group name, e.g. "ErrorMsg" 143 /// @param val Highlight definition map, accepts the following keys: 144 /// - bg: color name or "#RRGGBB", see note. 145 /// - bg_indexed: boolean (default false) If true, bg is a terminal palette index (0-255). 146 /// - blend: integer between 0 and 100 147 /// - cterm: cterm attribute map, like |highlight-args|. If not set, cterm attributes 148 /// will match those from the attribute map documented above. 149 /// - ctermbg: Sets background of cterm color |ctermbg| 150 /// - ctermfg: Sets foreground of cterm color |ctermfg| 151 /// - default: boolean Don't override existing definition |:hi-default| 152 /// - fg: color name or "#RRGGBB", see note. 153 /// - fg_indexed: boolean (default false) If true, fg is a terminal palette index (0-255). 154 /// - force: if true force update the highlight group when it exists. 155 /// - link: Name of highlight group to link to. |:hi-link| 156 /// - sp: color name or "#RRGGBB" 157 /// - altfont: boolean 158 /// - blink: boolean 159 /// - bold: boolean 160 /// - conceal: boolean Concealment at the UI level (terminal SGR), unrelated to |:syn-conceal|. 161 /// - dim: boolean 162 /// - italic: boolean 163 /// - nocombine: boolean 164 /// - overline: boolean 165 /// - reverse: boolean 166 /// - standout: boolean 167 /// - strikethrough: boolean 168 /// - undercurl: boolean 169 /// - underdashed: boolean 170 /// - underdotted: boolean 171 /// - underdouble: boolean 172 /// - underline: boolean 173 /// @param[out] err Error details, if any 174 /// 175 // TODO(bfredl): val should take update vs reset flag 176 void nvim_set_hl(uint64_t channel_id, Integer ns_id, String name, Dict(highlight) *val, Error *err) 177 FUNC_API_SINCE(7) 178 { 179 int hl_id = syn_check_group(name.data, name.size); 180 VALIDATE_S((hl_id != 0), "highlight name", name.data, { 181 return; 182 }); 183 int link_id = -1; 184 185 // Setting URLs directly through highlight attributes is not supported 186 if (HAS_KEY(val, highlight, url)) { 187 api_free_string(val->url); 188 val->url = NULL_STRING; 189 } 190 191 HlAttrs attrs = dict2hlattrs(val, true, &link_id, err); 192 if (!ERROR_SET(err)) { 193 WITH_SCRIPT_CONTEXT(channel_id, { 194 ns_hl_def((NS)ns_id, hl_id, attrs, link_id, val); 195 }); 196 } 197 } 198 199 /// Gets the active highlight namespace. 200 /// 201 /// @param opts Optional parameters 202 /// - winid: (number) |window-ID| for retrieving a window's highlight 203 /// namespace. A value of -1 is returned when |nvim_win_set_hl_ns()| 204 /// has not been called for the window (or was called with a namespace 205 /// of -1). 206 /// @param[out] err Error details, if any 207 /// @return Namespace id, or -1 208 Integer nvim_get_hl_ns(Dict(get_ns) *opts, Error *err) 209 FUNC_API_SINCE(12) 210 { 211 if (HAS_KEY(opts, get_ns, winid)) { 212 win_T *win = find_window_by_handle(opts->winid, err); 213 if (!win) { 214 return 0; 215 } 216 return win->w_ns_hl; 217 } else { 218 return ns_hl_global; 219 } 220 } 221 222 /// Set active namespace for highlights defined with |nvim_set_hl()|. This can be set for 223 /// a single window, see |nvim_win_set_hl_ns()|. 224 /// 225 /// @param ns_id the namespace to use 226 /// @param[out] err Error details, if any 227 void nvim_set_hl_ns(Integer ns_id, Error *err) 228 FUNC_API_SINCE(10) 229 { 230 VALIDATE_INT((ns_id >= 0), "namespace", ns_id, { 231 return; 232 }); 233 234 ns_hl_global = (NS)ns_id; 235 hl_check_ns(); 236 redraw_all_later(UPD_NOT_VALID); 237 } 238 239 /// Set active namespace for highlights defined with |nvim_set_hl()| while redrawing. 240 /// 241 /// This function is meant to be called while redrawing, primarily from 242 /// |nvim_set_decoration_provider()| on_win and on_line callbacks, which 243 /// are allowed to change the namespace during a redraw cycle. 244 /// 245 /// @param ns_id the namespace to activate 246 /// @param[out] err Error details, if any 247 void nvim_set_hl_ns_fast(Integer ns_id, Error *err) 248 FUNC_API_SINCE(10) 249 FUNC_API_FAST 250 { 251 ns_hl_fast = (NS)ns_id; 252 hl_check_ns(); 253 } 254 255 /// Sends input-keys to Nvim, subject to various quirks controlled by `mode` 256 /// flags. This is a blocking call, unlike |nvim_input()|. 257 /// 258 /// On execution error: does not fail, but updates v:errmsg. 259 /// 260 /// To input sequences like [<C-o>] use |nvim_replace_termcodes()| (typically 261 /// with escape_ks=false) to replace |keycodes|, then pass the result to 262 /// nvim_feedkeys(). 263 /// 264 /// Example: 265 /// 266 /// ```vim 267 /// :let key = nvim_replace_termcodes("<C-o>", v:true, v:false, v:true) 268 /// :call nvim_feedkeys(key, 'n', v:false) 269 /// ``` 270 /// 271 /// @param keys to be typed 272 /// @param mode behavior flags, see |feedkeys()| 273 /// @param escape_ks If true, escape K_SPECIAL bytes in `keys`. 274 /// This should be false if you already used 275 /// |nvim_replace_termcodes()|, and true otherwise. 276 /// @see feedkeys() 277 /// @see vim_strsave_escape_ks 278 void nvim_feedkeys(String keys, String mode, Boolean escape_ks) 279 FUNC_API_SINCE(1) 280 { 281 bool remap = true; 282 bool insert = false; 283 bool typed = false; 284 bool execute = false; 285 bool dangerous = false; 286 bool lowlevel = false; 287 288 for (size_t i = 0; i < mode.size; i++) { 289 switch (mode.data[i]) { 290 case 'n': 291 remap = false; break; 292 case 'm': 293 remap = true; break; 294 case 't': 295 typed = true; break; 296 case 'i': 297 insert = true; break; 298 case 'x': 299 execute = true; break; 300 case '!': 301 dangerous = true; break; 302 case 'L': 303 lowlevel = true; break; 304 } 305 } 306 307 if (keys.size == 0 && !execute) { 308 return; 309 } 310 311 char *keys_esc; 312 if (escape_ks) { 313 // Need to escape K_SPECIAL before putting the string in the 314 // typeahead buffer. 315 keys_esc = vim_strsave_escape_ks(keys.data); 316 } else { 317 keys_esc = keys.data; 318 } 319 if (lowlevel) { 320 input_enqueue_raw(keys_esc, strlen(keys_esc)); 321 } else { 322 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE), 323 insert ? 0 : typebuf.tb_len, !typed, false); 324 if (vgetc_busy) { 325 typebuf_was_filled = true; 326 } 327 } 328 329 if (escape_ks) { 330 xfree(keys_esc); 331 } 332 333 if (execute) { 334 int save_msg_scroll = msg_scroll; 335 336 // Avoid a 1 second delay when the keys start Insert mode. 337 msg_scroll = false; 338 if (!dangerous) { 339 ex_normal_busy++; 340 } 341 exec_normal(true, lowlevel); 342 if (!dangerous) { 343 ex_normal_busy--; 344 } 345 msg_scroll |= save_msg_scroll; 346 } 347 } 348 349 /// Queues raw user-input. Unlike |nvim_feedkeys()|, this uses a low-level input buffer and the call 350 /// is non-blocking (input is processed asynchronously by the eventloop). 351 /// 352 /// To input blocks of text, |nvim_paste()| is much faster and should be preferred. 353 /// 354 /// On execution error: does not fail, but updates v:errmsg. 355 /// 356 /// @note |keycodes| like [<CR>] are translated, so "<" is special. 357 /// To input a literal "<", send [<LT>]. 358 /// 359 /// @note For mouse events use |nvim_input_mouse()|. The pseudokey form 360 /// `<LeftMouse><col,row>` is deprecated since |api-level| 6. 361 /// 362 /// @param keys to be typed 363 /// @return Number of bytes actually written (can be fewer than 364 /// requested if the buffer becomes full). 365 Integer nvim_input(uint64_t channel_id, String keys) 366 FUNC_API_SINCE(1) FUNC_API_FAST 367 { 368 may_trigger_vim_suspend_resume(false); 369 return (Integer)input_enqueue(channel_id, keys); 370 } 371 372 /// Send mouse event from GUI. 373 /// 374 /// Non-blocking: does not wait on any result, but queues the event to be 375 /// processed soon by the event loop. 376 /// 377 /// @note Currently this doesn't support "scripting" multiple mouse events 378 /// by calling it multiple times in a loop: the intermediate mouse 379 /// positions will be ignored. It should be used to implement real-time 380 /// mouse input in a GUI. The deprecated pseudokey form 381 /// (`<LeftMouse><col,row>`) of |nvim_input()| has the same limitation. 382 /// 383 /// @param button Mouse button: one of "left", "right", "middle", "wheel", "move", 384 /// "x1", "x2". 385 /// @param action For ordinary buttons, one of "press", "drag", "release". 386 /// For the wheel, one of "up", "down", "left", "right". Ignored for "move". 387 /// @param modifier String of modifiers each represented by a single char. 388 /// The same specifiers are used as for a key press, except 389 /// that the "-" separator is optional, so "C-A-", "c-a" 390 /// and "CA" can all be used to specify Ctrl+Alt+click. 391 /// @param grid Grid number (used by |ui-multigrid| client), or 0 to let Nvim decide positioning of 392 /// windows. For more information, see [dev-ui-multigrid] 393 /// @param row Mouse row-position (zero-based, like redraw events) 394 /// @param col Mouse column-position (zero-based, like redraw events) 395 /// @param[out] err Error details, if any 396 void nvim_input_mouse(String button, String action, String modifier, Integer grid, Integer row, 397 Integer col, Error *err) 398 FUNC_API_SINCE(6) FUNC_API_FAST 399 { 400 may_trigger_vim_suspend_resume(false); 401 402 if (button.data == NULL || action.data == NULL) { 403 goto error; 404 } 405 406 int code = 0; 407 408 if (strequal(button.data, "left")) { 409 code = KE_LEFTMOUSE; 410 } else if (strequal(button.data, "middle")) { 411 code = KE_MIDDLEMOUSE; 412 } else if (strequal(button.data, "right")) { 413 code = KE_RIGHTMOUSE; 414 } else if (strequal(button.data, "wheel")) { 415 code = KE_MOUSEDOWN; 416 } else if (strequal(button.data, "x1")) { 417 code = KE_X1MOUSE; 418 } else if (strequal(button.data, "x2")) { 419 code = KE_X2MOUSE; 420 } else if (strequal(button.data, "move")) { 421 code = KE_MOUSEMOVE; 422 } else { 423 goto error; 424 } 425 426 if (code == KE_MOUSEDOWN) { 427 if (strequal(action.data, "down")) { 428 code = KE_MOUSEUP; 429 } else if (strequal(action.data, "up")) { 430 // code = KE_MOUSEDOWN 431 } else if (strequal(action.data, "left")) { 432 code = KE_MOUSERIGHT; 433 } else if (strequal(action.data, "right")) { 434 code = KE_MOUSELEFT; 435 } else { 436 goto error; 437 } 438 } else if (code != KE_MOUSEMOVE) { 439 if (strequal(action.data, "press")) { 440 // pass 441 } else if (strequal(action.data, "drag")) { 442 code += KE_LEFTDRAG - KE_LEFTMOUSE; 443 } else if (strequal(action.data, "release")) { 444 code += KE_LEFTRELEASE - KE_LEFTMOUSE; 445 } else { 446 goto error; 447 } 448 } 449 450 int modmask = 0; 451 for (size_t i = 0; i < modifier.size; i++) { 452 char byte = modifier.data[i]; 453 if (byte == '-') { 454 continue; 455 } 456 int mod = name_to_mod_mask(byte); 457 VALIDATE((mod != 0), "Invalid modifier: %c", byte, { 458 return; 459 }); 460 modmask |= mod; 461 } 462 463 input_enqueue_mouse(code, (uint8_t)modmask, (int)grid, (int)row, (int)col); 464 return; 465 466 error: 467 api_set_error(err, kErrorTypeValidation, 468 "invalid button or action"); 469 } 470 471 /// Replaces terminal codes and |keycodes| ([<CR>], [<Esc>], ...) in a string with 472 /// the internal representation. 473 /// 474 /// @note Lua can use |vim.keycode()| instead. 475 /// @see replace_termcodes 476 /// @see cpoptions 477 /// 478 /// @param str String to be converted. 479 /// @param from_part Legacy Vim parameter. Usually true. 480 /// @param do_lt Also translate [<lt>]. Ignored if `special` is false. 481 /// @param special Replace |keycodes|, e.g. [<CR>] becomes a "\r" char. 482 String nvim_replace_termcodes(String str, Boolean from_part, Boolean do_lt, Boolean special) 483 FUNC_API_SINCE(1) FUNC_API_RET_ALLOC 484 { 485 if (str.size == 0) { 486 // Empty string 487 return (String) { .data = NULL, .size = 0 }; 488 } 489 490 int flags = 0; 491 if (from_part) { 492 flags |= REPTERM_FROM_PART; 493 } 494 if (do_lt) { 495 flags |= REPTERM_DO_LT; 496 } 497 if (!special) { 498 flags |= REPTERM_NO_SPECIAL; 499 } 500 501 char *ptr = NULL; 502 replace_termcodes(str.data, str.size, &ptr, 0, flags, NULL, p_cpo); 503 return cstr_as_string(ptr); 504 } 505 506 /// Executes Lua code. Arguments are available as `...` inside the chunk. The chunk can return 507 /// a value. 508 /// 509 /// Only statements are executed. To evaluate an expression, prefix it with "return": `return 510 /// my_function(...)` 511 /// 512 /// Example: 513 /// ```lua 514 /// local peer = vim.fn.jobstart({ vim.v.progpath, '--clean', '--embed' }, { rpc=true }) 515 /// vim.print(vim.rpcrequest(peer, 'nvim_exec_lua', [[ 516 /// local a, b = ... 517 /// return ('result: %s'):format(a + b) 518 /// ]], 519 /// { 1, 3 } 520 /// ) 521 /// ) 522 /// ``` 523 /// 524 /// @param code Lua code to execute. 525 /// @param args Arguments to the Lua code. 526 /// @param[out] err Lua error raised while parsing or executing the Lua code. 527 /// 528 /// @return Value returned by the Lua code (if any), or NIL. 529 Object nvim_exec_lua(String code, Array args, Arena *arena, Error *err) 530 FUNC_API_SINCE(7) 531 FUNC_API_REMOTE_ONLY 532 { 533 // TODO(bfredl): convert directly from msgpack to lua and then back again 534 return nlua_exec(code, NULL, args, kRetObject, arena, err); 535 } 536 537 /// EXPERIMENTAL: this API may change or be removed in the future. 538 /// 539 /// Like |nvim_exec_lua()|, but can be called during |api-fast| contexts. 540 /// 541 /// Execute Lua code. Parameters (if any) are available as `...` inside the 542 /// chunk. The chunk can return a value. 543 /// 544 /// Only statements are executed. To evaluate an expression, prefix it 545 /// with `return`: return my_function(...) 546 /// 547 /// @param code Lua code to execute 548 /// @param args Arguments to the code 549 /// @param[out] err Details of an error encountered while parsing 550 /// or executing the Lua code. 551 /// 552 /// @return Return value of Lua code if present or NIL. 553 Object nvim__exec_lua_fast(String code, Array args, Arena *arena, Error *err) 554 FUNC_API_SINCE(14) 555 FUNC_API_REMOTE_ONLY 556 FUNC_API_FAST 557 { 558 return nvim_exec_lua(code, args, arena, err); 559 } 560 561 /// Calculates the number of display cells occupied by `text`. 562 /// Control characters including [<Tab>] count as one cell. 563 /// 564 /// @param text Some text 565 /// @param[out] err Error details, if any 566 /// @return Number of cells 567 Integer nvim_strwidth(String text, Error *err) 568 FUNC_API_SINCE(1) 569 { 570 VALIDATE_S((text.size <= INT_MAX), "text length", "(too long)", { 571 return 0; 572 }); 573 574 return (Integer)mb_string2cells(text.data); 575 } 576 577 /// Gets the paths contained in |runtime-search-path|. 578 /// 579 /// @return List of paths 580 ArrayOf(String) nvim_list_runtime_paths(Arena *arena, Error *err) 581 FUNC_API_SINCE(1) 582 { 583 return nvim_get_runtime_file(NULL_STRING, true, arena, err); 584 } 585 586 /// @nodoc 587 Array nvim__runtime_inspect(Arena *arena) 588 { 589 return runtime_inspect(arena); 590 } 591 592 typedef struct { 593 ArrayBuilder rv; 594 Arena *arena; 595 } RuntimeCookie; 596 597 /// Finds files in runtime directories, in 'runtimepath' order. 598 /// 599 /// "name" can contain wildcards. For example 600 /// `nvim_get_runtime_file("colors/*.{vim,lua}", true)` will return all color 601 /// scheme files. Always use forward slashes (/) in the search pattern for 602 /// subdirectories regardless of platform. 603 /// 604 /// It is not an error to not find any files. An empty array is returned then. 605 /// 606 /// @param name pattern of files to search for 607 /// @param all whether to return all matches or only the first 608 /// @return list of absolute paths to the found files 609 ArrayOf(String) nvim_get_runtime_file(String name, Boolean all, Arena *arena, Error *err) 610 FUNC_API_SINCE(7) 611 FUNC_API_FAST 612 { 613 RuntimeCookie cookie = { .rv = ARRAY_DICT_INIT, .arena = arena, }; 614 kvi_init(cookie.rv); 615 616 int flags = DIP_DIRFILE | (all ? DIP_ALL : 0); 617 618 TRY_WRAP(err, { 619 do_in_runtimepath((name.size ? name.data : ""), flags, find_runtime_cb, &cookie); 620 }); 621 622 return arena_take_arraybuilder(arena, &cookie.rv); 623 } 624 625 static bool find_runtime_cb(int num_fnames, char **fnames, bool all, void *c) 626 { 627 RuntimeCookie *cookie = (RuntimeCookie *)c; 628 for (int i = 0; i < num_fnames; i++) { 629 // TODO(bfredl): consider memory management of gen_expand_wildcards() itself 630 kvi_push(cookie->rv, CSTR_TO_ARENA_OBJ(cookie->arena, fnames[i])); 631 if (!all) { 632 return true; 633 } 634 } 635 636 return num_fnames > 0; 637 } 638 639 /// @nodoc 640 String nvim__get_lib_dir(void) 641 FUNC_API_RET_ALLOC 642 { 643 return cstr_as_string(get_lib_dir()); 644 } 645 646 /// Find files in runtime directories 647 /// 648 /// @param pat pattern of files to search for 649 /// @param all whether to return all matches or only the first 650 /// @param opts is_lua: only search Lua subdirs 651 /// @return list of absolute paths to the found files 652 ArrayOf(String) nvim__get_runtime(ArrayOf(String) pat, Boolean all, Dict(runtime) *opts, 653 Arena *arena, 654 Error *err) 655 FUNC_API_SINCE(8) 656 FUNC_API_FAST 657 { 658 VALIDATE(!opts->do_source || nlua_is_deferred_safe(), "%s", "'do_source' used in fast callback", 659 {}); 660 if (ERROR_SET(err)) { 661 return (Array)ARRAY_DICT_INIT; 662 } 663 664 ArrayOf(String) res = runtime_get_named(opts->is_lua, pat, all, arena); 665 666 if (opts->do_source) { 667 for (size_t i = 0; i < res.size; i++) { 668 String name = res.items[i].data.string; 669 do_source(name.data, false, DOSO_NONE, NULL); 670 } 671 } 672 673 return res; 674 } 675 676 /// Changes the global working directory. 677 /// 678 /// @param dir Directory path 679 /// @param[out] err Error details, if any 680 void nvim_set_current_dir(String dir, Error *err) 681 FUNC_API_SINCE(1) 682 { 683 VALIDATE_S((dir.size < MAXPATHL), "directory name", "(too long)", { 684 return; 685 }); 686 687 char string[MAXPATHL]; 688 memcpy(string, dir.data, dir.size); 689 string[dir.size] = NUL; 690 691 TRY_WRAP(err, { 692 changedir_func(string, kCdScopeGlobal); 693 }); 694 } 695 696 /// Gets the current line. 697 /// 698 /// @param[out] err Error details, if any 699 /// @return Current line string 700 String nvim_get_current_line(Arena *arena, Error *err) 701 FUNC_API_SINCE(1) 702 { 703 return buffer_get_line(curbuf->handle, curwin->w_cursor.lnum - 1, arena, err); 704 } 705 706 /// Sets the text on the current line. 707 /// 708 /// @param line Line contents 709 /// @param[out] err Error details, if any 710 void nvim_set_current_line(String line, Arena *arena, Error *err) 711 FUNC_API_SINCE(1) 712 FUNC_API_TEXTLOCK_ALLOW_CMDWIN 713 { 714 buffer_set_line(curbuf->handle, curwin->w_cursor.lnum - 1, line, arena, err); 715 } 716 717 /// Deletes the current line. 718 /// 719 /// @param[out] err Error details, if any 720 void nvim_del_current_line(Arena *arena, Error *err) 721 FUNC_API_SINCE(1) 722 FUNC_API_TEXTLOCK_ALLOW_CMDWIN 723 { 724 buffer_del_line(curbuf->handle, curwin->w_cursor.lnum - 1, arena, err); 725 } 726 727 /// Gets a global (g:) variable. 728 /// 729 /// @param name Variable name 730 /// @param[out] err Error details, if any 731 /// @return Variable value 732 Object nvim_get_var(String name, Arena *arena, Error *err) 733 FUNC_API_SINCE(1) 734 { 735 dictitem_T *di = tv_dict_find(get_globvar_dict(), name.data, (ptrdiff_t)name.size); 736 if (di == NULL) { // try to autoload script 737 bool found = script_autoload(name.data, name.size, false) && !aborting(); 738 VALIDATE(found, "Key not found: %s", name.data, { 739 return (Object)OBJECT_INIT; 740 }); 741 di = tv_dict_find(get_globvar_dict(), name.data, (ptrdiff_t)name.size); 742 } 743 VALIDATE((di != NULL), "Key not found: %s", name.data, { 744 return (Object)OBJECT_INIT; 745 }); 746 return vim_to_object(&di->di_tv, arena, true); 747 } 748 749 /// Sets a global (g:) variable. 750 /// 751 /// @param name Variable name 752 /// @param value Variable value 753 /// @param[out] err Error details, if any 754 void nvim_set_var(String name, Object value, Error *err) 755 FUNC_API_SINCE(1) 756 { 757 dict_set_var(get_globvar_dict(), name, value, false, false, NULL, err); 758 } 759 760 /// Removes a global (g:) variable. 761 /// 762 /// @param name Variable name 763 /// @param[out] err Error details, if any 764 void nvim_del_var(String name, Error *err) 765 FUNC_API_SINCE(1) 766 { 767 dict_set_var(get_globvar_dict(), name, NIL, true, false, NULL, err); 768 } 769 770 /// Gets a v: variable. 771 /// 772 /// @param name Variable name 773 /// @param[out] err Error details, if any 774 /// @return Variable value 775 Object nvim_get_vvar(String name, Arena *arena, Error *err) 776 FUNC_API_SINCE(1) 777 { 778 return dict_get_value(get_vimvar_dict(), name, arena, err); 779 } 780 781 /// Sets a v: variable, if it is not readonly. 782 /// 783 /// @param name Variable name 784 /// @param value Variable value 785 /// @param[out] err Error details, if any 786 void nvim_set_vvar(String name, Object value, Error *err) 787 FUNC_API_SINCE(6) 788 { 789 dict_set_var(get_vimvar_dict(), name, value, false, false, NULL, err); 790 } 791 792 /// Prints a message given by a list of `[text, hl_group]` "chunks". 793 /// 794 /// Example: 795 /// ```lua 796 /// vim.api.nvim_echo({ { 'chunk1-line1\nchunk1-line2\n' }, { 'chunk2-line1' } }, true, {}) 797 /// ``` 798 /// 799 /// @param chunks List of `[text, hl_group]` pairs, where each is a `text` string highlighted by 800 /// the (optional) name or ID `hl_group`. 801 /// @param history if true, add to |message-history|. 802 /// @param opts Optional parameters. 803 /// - id: message id for updating existing message. 804 /// - err: Treat the message like `:echoerr`. Sets `hl_group` to |hl-ErrorMsg| by default. 805 /// - kind: Set the |ui-messages| kind with which this message will be emitted. 806 /// - verbose: Message is controlled by the 'verbose' option. Nvim invoked with `-V3log` 807 /// will write the message to the "log" file instead of standard output. 808 /// - title: The title for |progress-message|. 809 /// - status: Current status of the |progress-message|. Can be 810 /// one of the following values 811 /// - success: The progress item completed successfully 812 /// - running: The progress is ongoing 813 /// - failed: The progress item failed 814 /// - cancel: The progressing process should be canceled. NOTE: Cancel must be handled by 815 /// progress initiator by listening for the `Progress` event 816 /// - percent: How much progress is done on the progress message 817 /// - data: dictionary containing additional information 818 /// @return Message id. 819 /// - -1 means nvim_echo didn't show a message 820 Union(Integer, String) nvim_echo(ArrayOf(Tuple(String, *HLGroupID)) chunks, Boolean history, 821 Dict(echo_opts) *opts, 822 Error *err) 823 FUNC_API_SINCE(7) 824 { 825 MsgID id = INTEGER_OBJ(-1); 826 HlMessage hl_msg = parse_hl_msg(chunks, opts->err, err); 827 if (ERROR_SET(err)) { 828 goto error; 829 } 830 831 char *kind = opts->kind.data; 832 if (opts->verbose) { 833 verbose_enter(); 834 } else if (kind == NULL) { 835 kind = opts->err ? "echoerr" : history ? "echomsg" : "echo"; 836 } 837 838 bool is_progress = strequal(kind, "progress"); 839 bool needs_clear = !history; 840 841 VALIDATE(is_progress 842 || (opts->status.size == 0 && opts->title.size == 0 && opts->percent == 0 843 && opts->data.size == 0), 844 "%s", 845 "title, status, percent and data fields can only be used with progress messages", 846 { 847 goto error; 848 }); 849 850 VALIDATE_EXP((!is_progress || strequal(opts->status.data, "success") 851 || strequal(opts->status.data, "failed") 852 || strequal(opts->status.data, "running") 853 || strequal(opts->status.data, "cancel")), 854 "status", "success|failed|running|cancel", opts->status.data, { 855 goto error; 856 }); 857 858 VALIDATE_RANGE(!is_progress || (opts->percent >= 0 && opts->percent <= 100), 859 "percent", { 860 goto error; 861 }); 862 863 MessageData msg_data = { .title = opts->title, .status = opts->status, 864 .percent = opts->percent, .data = opts->data }; 865 866 id = msg_multihl(opts->id, hl_msg, kind, history, opts->err, &msg_data, &needs_clear); 867 868 if (opts->verbose) { 869 verbose_leave(); 870 verbose_stop(); // flush now 871 } 872 873 if (is_progress) { 874 do_autocmd_progress(id, hl_msg, &msg_data); 875 } 876 877 if (!needs_clear) { 878 // history takes ownership of `hl_msg` 879 return id; 880 } 881 882 error: 883 hl_msg_free(hl_msg); 884 return id; 885 } 886 887 /// Gets the current list of buffers. 888 /// 889 /// Includes unlisted (unloaded/deleted) buffers, like `:ls!`. 890 /// Use |nvim_buf_is_loaded()| to check if a buffer is loaded. 891 /// 892 /// @return List of buffer ids 893 ArrayOf(Buffer) nvim_list_bufs(Arena *arena) 894 FUNC_API_SINCE(1) 895 { 896 size_t n = 0; 897 898 FOR_ALL_BUFFERS(b) { 899 n++; 900 } 901 902 Array rv = arena_array(arena, n); 903 904 FOR_ALL_BUFFERS(b) { 905 ADD_C(rv, BUFFER_OBJ(b->handle)); 906 } 907 908 return rv; 909 } 910 911 /// Gets the current buffer. 912 /// 913 /// @return Buffer id 914 Buffer nvim_get_current_buf(void) 915 FUNC_API_SINCE(1) 916 { 917 return curbuf->handle; 918 } 919 920 /// Sets the current window's buffer to `buffer`. 921 /// 922 /// @param buffer Buffer id 923 /// @param[out] err Error details, if any 924 void nvim_set_current_buf(Buffer buffer, Error *err) 925 FUNC_API_SINCE(1) 926 FUNC_API_TEXTLOCK 927 { 928 buf_T *buf = find_buffer_by_handle(buffer, err); 929 930 if (!buf) { 931 return; 932 } 933 934 TRY_WRAP(err, { 935 do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0); 936 }); 937 } 938 939 /// Gets the current list of all |window-ID|s in all tabpages. 940 /// 941 /// @return List of |window-ID|s 942 ArrayOf(Window) nvim_list_wins(Arena *arena) 943 FUNC_API_SINCE(1) 944 { 945 size_t n = 0; 946 947 FOR_ALL_TAB_WINDOWS(tp, wp) { 948 n++; 949 } 950 951 Array rv = arena_array(arena, n); 952 953 FOR_ALL_TAB_WINDOWS(tp, wp) { 954 ADD_C(rv, WINDOW_OBJ(wp->handle)); 955 } 956 957 return rv; 958 } 959 960 /// Gets the current window. 961 /// 962 /// @return |window-ID| 963 Window nvim_get_current_win(void) 964 FUNC_API_SINCE(1) 965 { 966 return curwin->handle; 967 } 968 969 /// Navigates to the given window (and tabpage, implicitly). 970 /// 971 /// @param window |window-ID| to focus 972 /// @param[out] err Error details, if any 973 void nvim_set_current_win(Window window, Error *err) 974 FUNC_API_SINCE(1) 975 FUNC_API_TEXTLOCK 976 { 977 win_T *win = find_window_by_handle(window, err); 978 979 if (!win) { 980 return; 981 } 982 983 TRY_WRAP(err, { 984 if (win->w_buffer != curbuf) { 985 reset_VIsual_and_resel(); 986 } 987 goto_tabpage_win(win_find_tabpage(win), win); 988 }); 989 } 990 991 /// Creates a new, empty, unnamed buffer. 992 /// 993 /// @param listed Sets 'buflisted' 994 /// @param scratch Creates a "throwaway" |scratch-buffer| for temporary work 995 /// (always 'nomodified'). Also sets 'nomodeline' on the buffer. 996 /// @param[out] err Error details, if any 997 /// @return Buffer id, or 0 on error 998 /// 999 /// @see buf_open_scratch 1000 Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) 1001 FUNC_API_SINCE(6) 1002 { 1003 Buffer ret = 0; 1004 1005 TRY_WRAP(err, { 1006 // Block autocommands for now so they don't mess with the buffer before we 1007 // finish configuring it. 1008 block_autocmds(); 1009 1010 buf_T *buf = buflist_new(NULL, NULL, 0, 1011 BLN_NOOPT | BLN_NEW | (listed ? BLN_LISTED : 0)); 1012 if (buf == NULL) { 1013 unblock_autocmds(); 1014 goto fail; 1015 } 1016 1017 // Open the memline for the buffer. This will avoid spurious autocmds when 1018 // a later nvim_buf_set_lines call would have needed to "open" the buffer. 1019 if (ml_open(buf) == FAIL) { 1020 unblock_autocmds(); 1021 goto fail; 1022 } 1023 1024 // Set last_changedtick to avoid triggering a TextChanged autocommand right 1025 // after it was added. 1026 buf->b_last_changedtick = buf_get_changedtick(buf); 1027 buf->b_last_changedtick_i = buf_get_changedtick(buf); 1028 buf->b_last_changedtick_pum = buf_get_changedtick(buf); 1029 1030 // Only strictly needed for scratch, but could just as well be consistent 1031 // and do this now. Buffer is created NOW, not when it later first happens 1032 // to reach a window or aucmd_prepbuf() .. 1033 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); 1034 1035 if (scratch) { 1036 set_option_direct_for(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("hide"), OPT_LOCAL, 0, 1037 kOptScopeBuf, buf); 1038 set_option_direct_for(kOptBuftype, STATIC_CSTR_AS_OPTVAL("nofile"), OPT_LOCAL, 0, 1039 kOptScopeBuf, buf); 1040 assert(buf->b_ml.ml_mfp->mf_fd < 0); // ml_open() should not have opened swapfile already 1041 buf->b_p_swf = false; 1042 buf->b_p_ml = false; 1043 } 1044 1045 unblock_autocmds(); 1046 1047 bufref_T bufref; 1048 set_bufref(&bufref, buf); 1049 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, false, buf) 1050 && !bufref_valid(&bufref)) { 1051 goto fail; 1052 } 1053 if (listed 1054 && apply_autocmds(EVENT_BUFADD, NULL, NULL, false, buf) 1055 && !bufref_valid(&bufref)) { 1056 goto fail; 1057 } 1058 1059 ret = buf->b_fnum; 1060 fail:; 1061 }); 1062 1063 if (ret == 0 && !ERROR_SET(err)) { 1064 api_set_error(err, kErrorTypeException, "Failed to create buffer"); 1065 } 1066 return ret; 1067 } 1068 1069 /// Open a terminal instance in a buffer 1070 /// 1071 /// By default (and currently the only option) the terminal will not be 1072 /// connected to an external process. Instead, input sent on the channel 1073 /// will be echoed directly by the terminal. This is useful to display 1074 /// ANSI terminal sequences returned as part of an RPC message, or similar. 1075 /// 1076 /// Note: to directly initiate the terminal using the right size, display the 1077 /// buffer in a configured window before calling this. For instance, for a 1078 /// floating display, first create an empty buffer using |nvim_create_buf()|, 1079 /// then display it using |nvim_open_win()|, and then call this function. 1080 /// Then |nvim_chan_send()| can be called immediately to process sequences 1081 /// in a virtual terminal having the intended size. 1082 /// 1083 /// Example: this `TermHl` command can be used to display and highlight raw ANSI termcodes, so you 1084 /// can use Nvim as a "scrollback pager" (for terminals like kitty): [ansi-colorize]() 1085 /// [terminal-scrollback-pager]() 1086 /// 1087 /// ```lua 1088 /// vim.api.nvim_create_user_command('TermHl', function() 1089 /// vim.api.nvim_open_term(0, {}) 1090 /// end, { desc = 'Highlights ANSI termcodes in curbuf' }) 1091 /// ``` 1092 /// 1093 /// @param buffer Buffer to use. Buffer contents (if any) will be written 1094 /// to the PTY. 1095 /// @param opts Optional parameters. 1096 /// - on_input: Lua callback for input sent, i e keypresses in terminal 1097 /// mode. Note: keypresses are sent raw as they would be to the pty 1098 /// master end. For instance, a carriage return is sent 1099 /// as a "\r", not as a "\n". |textlock| applies. It is possible 1100 /// to call |nvim_chan_send()| directly in the callback however. 1101 /// `["input", term, bufnr, data]` 1102 /// - force_crlf: (boolean, default true) Convert "\n" to "\r\n". 1103 /// @param[out] err Error details, if any 1104 /// @return Channel id, or 0 on error 1105 Integer nvim_open_term(Buffer buffer, Dict(open_term) *opts, Error *err) 1106 FUNC_API_SINCE(7) 1107 FUNC_API_TEXTLOCK_ALLOW_CMDWIN 1108 { 1109 buf_T *buf = api_buf_ensure_loaded(buffer, err); 1110 if (!buf) { 1111 return 0; 1112 } 1113 1114 if (buf == cmdwin_buf) { 1115 api_set_error(err, kErrorTypeException, "%s", e_cmdwin); 1116 return 0; 1117 } 1118 1119 bool may_read_buffer = true; 1120 if (buf->terminal) { 1121 if (terminal_running(buf->terminal)) { 1122 api_set_error(err, kErrorTypeException, 1123 "Terminal already connected to buffer %d", buf->handle); 1124 return 0; 1125 } 1126 buf_close_terminal(buf); 1127 may_read_buffer = false; 1128 } 1129 1130 LuaRef cb = LUA_NOREF; 1131 if (HAS_KEY(opts, open_term, on_input)) { 1132 cb = opts->on_input; 1133 opts->on_input = LUA_NOREF; 1134 } 1135 1136 Channel *chan = channel_alloc(kChannelStreamInternal); 1137 chan->stream.internal.cb = cb; 1138 chan->stream.internal.closed = false; 1139 TerminalOptions topts = { 1140 .data = chan, 1141 // NB: overridden in terminal_check_size if a window is already 1142 // displaying the buffer 1143 .width = (uint16_t)MAX(curwin->w_view_width - win_col_off(curwin), 0), 1144 .height = (uint16_t)curwin->w_view_height, 1145 .write_cb = term_write, 1146 .resize_cb = term_resize, 1147 .resume_cb = term_resume, 1148 .close_cb = term_close, 1149 .force_crlf = GET_BOOL_OR_TRUE(opts, open_term, force_crlf), 1150 }; 1151 1152 // Read existing buffer contents (if any) 1153 StringBuilder contents = KV_INITIAL_VALUE; 1154 if (may_read_buffer) { 1155 read_buffer_into(buf, 1, buf->b_ml.ml_line_count, &contents); 1156 } 1157 1158 channel_incref(chan); 1159 chan->term = terminal_alloc(buf, topts); 1160 terminal_open(&chan->term, buf); 1161 if (chan->term != NULL) { 1162 terminal_check_size(chan->term); 1163 } 1164 channel_decref(chan); 1165 1166 // Write buffer contents to channel. channel_send takes ownership of the 1167 // buffer so we do not need to free it. 1168 if (contents.size > 0) { 1169 const char *error = NULL; 1170 channel_send(chan->id, contents.items, contents.size, true, &error); 1171 VALIDATE(!error, "%s", error, {}); 1172 } 1173 1174 return (Integer)chan->id; 1175 } 1176 1177 static void term_write(const char *buf, size_t size, void *data) 1178 { 1179 Channel *chan = data; 1180 LuaRef cb = chan->stream.internal.cb; 1181 if (cb == LUA_NOREF) { 1182 return; 1183 } 1184 MAXSIZE_TEMP_ARRAY(args, 3); 1185 ADD_C(args, INTEGER_OBJ((Integer)chan->id)); 1186 ADD_C(args, BUFFER_OBJ(terminal_buf(chan->term))); 1187 ADD_C(args, STRING_OBJ(((String){ .data = (char *)buf, .size = size }))); 1188 textlock++; 1189 nlua_call_ref(cb, "input", args, kRetNilBool, NULL, NULL); 1190 textlock--; 1191 } 1192 1193 static void term_resize(uint16_t width, uint16_t height, void *data) 1194 { 1195 // TODO(bfredl): Lua callback 1196 } 1197 1198 static void term_resume(void *data) 1199 { 1200 } 1201 1202 static void term_close(void *data) 1203 { 1204 Channel *chan = data; 1205 terminal_destroy(&chan->term); 1206 api_free_luaref(chan->stream.internal.cb); 1207 chan->stream.internal.cb = LUA_NOREF; 1208 channel_decref(chan); 1209 } 1210 1211 /// Sends raw data to channel `chan`. |channel-bytes| 1212 /// - For a job, it writes it to the stdin of the process. 1213 /// - For the stdio channel |channel-stdio|, it writes to Nvim's stdout. 1214 /// - For an internal terminal instance (|nvim_open_term()|) it writes directly to terminal output. 1215 /// 1216 /// This function writes raw data, not RPC messages. Use |vim.rpcrequest()| and |vim.rpcnotify()| if 1217 /// the channel expects RPC messages (i.e. it was created with `rpc=true`). 1218 /// 1219 /// To write data to the |TUI| host terminal, see |nvim_ui_send()|. 1220 /// 1221 /// @param chan Channel id 1222 /// @param data Data to write. 8-bit clean: may contain NUL bytes. 1223 /// @param[out] err Error details, if any 1224 void nvim_chan_send(Integer chan, String data, Error *err) 1225 FUNC_API_SINCE(7) FUNC_API_REMOTE_ONLY FUNC_API_LUA_ONLY 1226 { 1227 const char *error = NULL; 1228 if (!data.size) { 1229 return; 1230 } 1231 1232 channel_send((uint64_t)chan, data.data, data.size, 1233 false, &error); 1234 VALIDATE(!error, "%s", error, {}); 1235 } 1236 1237 /// Gets the current list of |tab-ID|s. 1238 /// 1239 /// @return List of |tab-ID|s 1240 ArrayOf(Tabpage) nvim_list_tabpages(Arena *arena) 1241 FUNC_API_SINCE(1) 1242 { 1243 size_t n = 0; 1244 1245 FOR_ALL_TABS(tp) { 1246 n++; 1247 } 1248 1249 Array rv = arena_array(arena, n); 1250 1251 FOR_ALL_TABS(tp) { 1252 ADD_C(rv, TABPAGE_OBJ(tp->handle)); 1253 } 1254 1255 return rv; 1256 } 1257 1258 /// Gets the current tabpage. 1259 /// 1260 /// @return |tab-ID| 1261 Tabpage nvim_get_current_tabpage(void) 1262 FUNC_API_SINCE(1) 1263 { 1264 return curtab->handle; 1265 } 1266 1267 /// Sets the current tabpage. 1268 /// 1269 /// @param tabpage |tab-ID| to focus 1270 /// @param[out] err Error details, if any 1271 void nvim_set_current_tabpage(Tabpage tabpage, Error *err) 1272 FUNC_API_SINCE(1) 1273 FUNC_API_TEXTLOCK 1274 { 1275 tabpage_T *tp = find_tab_by_handle(tabpage, err); 1276 1277 if (!tp) { 1278 return; 1279 } 1280 1281 TRY_WRAP(err, { 1282 goto_tabpage_tp(tp, true, true); 1283 }); 1284 } 1285 1286 /// Pastes at cursor (in any mode), and sets "redo" so dot (|.|) will repeat the input. UIs call 1287 /// this to implement "paste", but it's also intended for use by scripts to input large, 1288 /// dot-repeatable blocks of text (as opposed to |nvim_input()| which is subject to mappings/events 1289 /// and is thus much slower). 1290 /// 1291 /// Invokes the |vim.paste()| handler, which handles each mode appropriately. 1292 /// 1293 /// Errors ('nomodifiable', `vim.paste()` failure, …) are reflected in `err` but do not affect the 1294 /// return value (which is strictly decided by `vim.paste()`). On error or cancel, subsequent calls 1295 /// are ignored ("drained") until the next paste is initiated (phase 1 or -1). 1296 /// 1297 /// Useful in mappings and scripts to insert multiline text. Example: 1298 /// 1299 /// ```lua 1300 /// vim.keymap.set('n', 'x', function() 1301 /// vim.api.nvim_paste([[ 1302 /// line1 1303 /// line2 1304 /// line3 1305 /// ]], false, -1) 1306 /// end, { buffer = true }) 1307 /// ``` 1308 /// 1309 /// @param data Multiline input. Lines break at LF ("\n"). May be binary (containing NUL bytes). 1310 /// @param crlf Also break lines at CR and CRLF. 1311 /// @param phase -1: paste in a single call (i.e. without streaming). 1312 /// To "stream" a paste, call `nvim_paste` sequentially with 1313 /// these `phase` values: 1314 /// - 1: starts the paste (exactly once) 1315 /// - 2: continues the paste (zero or more times) 1316 /// - 3: ends the paste (exactly once) 1317 /// @param[out] err Error details, if any 1318 /// @return 1319 /// - true: Client may continue pasting. 1320 /// - false: Client should cancel the paste. 1321 Boolean nvim_paste(uint64_t channel_id, String data, Boolean crlf, Integer phase, Arena *arena, 1322 Error *err) 1323 FUNC_API_SINCE(6) 1324 FUNC_API_TEXTLOCK_ALLOW_CMDWIN 1325 { 1326 static bool cancelled = false; 1327 1328 VALIDATE_INT((phase >= -1 && phase <= 3), "phase", phase, { 1329 return false; 1330 }); 1331 if (phase == -1 || phase == 1) { // Start of paste-stream. 1332 cancelled = false; 1333 } else if (cancelled) { 1334 // Skip remaining chunks. Report error only once per "stream". 1335 goto theend; 1336 } 1337 Array lines = string_to_array(data, crlf, arena); 1338 MAXSIZE_TEMP_ARRAY(args, 2); 1339 ADD_C(args, ARRAY_OBJ(lines)); 1340 ADD_C(args, INTEGER_OBJ(phase)); 1341 Object rv = NLUA_EXEC_STATIC("return vim.paste(...)", args, kRetNilBool, arena, err); 1342 // vim.paste() decides if client should cancel. 1343 if (ERROR_SET(err) || (rv.type == kObjectTypeBoolean && !rv.data.boolean)) { 1344 cancelled = true; 1345 } 1346 if (!cancelled && (phase == -1 || phase == 1)) { 1347 paste_store(channel_id, kFalse, NULL_STRING, crlf); 1348 } 1349 if (!cancelled) { 1350 paste_store(channel_id, kNone, data, crlf); 1351 } 1352 if (phase == 3 || phase == (cancelled ? 2 : -1)) { 1353 paste_store(channel_id, kTrue, NULL_STRING, crlf); 1354 } 1355 theend: 1356 ; 1357 bool retval = !cancelled; 1358 if (phase == -1 || phase == 3) { // End of paste-stream. 1359 cancelled = false; 1360 } 1361 return retval; 1362 } 1363 1364 /// Puts text at cursor, in any mode. For dot-repeatable input, use |nvim_paste()|. 1365 /// 1366 /// Compare |:put| and |p| which are always linewise. 1367 /// 1368 /// @param lines |readfile()|-style list of lines. |channel-lines| 1369 /// @param type Edit behavior: any |getregtype()| result, or: 1370 /// - "b" |blockwise-visual| mode (may include width, e.g. "b3") 1371 /// - "c" |charwise| mode 1372 /// - "l" |linewise| mode 1373 /// - "" guess by contents, see |setreg()| 1374 /// @param after If true insert after cursor (like |p|), or before (like |P|). 1375 /// @param follow If true place cursor at end of inserted text. 1376 /// @param[out] err Error details, if any 1377 void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow, Arena *arena, 1378 Error *err) 1379 FUNC_API_SINCE(6) 1380 FUNC_API_TEXTLOCK_ALLOW_CMDWIN 1381 { 1382 yankreg_T reg[1] = { 0 }; 1383 VALIDATE_S((prepare_yankreg_from_object(reg, type, lines.size)), "type", type.data, { 1384 return; 1385 }); 1386 if (lines.size == 0) { 1387 return; // Nothing to do. 1388 } 1389 1390 reg->y_array = arena_alloc(arena, lines.size * sizeof(String), true); 1391 reg->y_size = lines.size; 1392 for (size_t i = 0; i < lines.size; i++) { 1393 VALIDATE_T("line", kObjectTypeString, lines.items[i].type, { 1394 return; 1395 }); 1396 String line = lines.items[i].data.string; 1397 reg->y_array[i] = copy_string(line, arena); 1398 memchrsub(reg->y_array[i].data, NUL, NL, line.size); 1399 } 1400 1401 finish_yankreg_from_object(reg, false); 1402 1403 TRY_WRAP(err, { 1404 bool VIsual_was_active = VIsual_active; 1405 msg_silent++; // Avoid "N more lines" message. 1406 do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0); 1407 msg_silent--; 1408 VIsual_active = VIsual_was_active; 1409 }); 1410 } 1411 1412 /// Returns the 24-bit RGB value of a |nvim_get_color_map()| color name or 1413 /// "#rrggbb" hexadecimal string. 1414 /// 1415 /// Example: 1416 /// 1417 /// ```vim 1418 /// :echo nvim_get_color_by_name("Pink") 1419 /// :echo nvim_get_color_by_name("#cbcbcb") 1420 /// ``` 1421 /// 1422 /// @param name Color name or "#rrggbb" string 1423 /// @return 24-bit RGB value, or -1 for invalid argument. 1424 Integer nvim_get_color_by_name(String name) 1425 FUNC_API_SINCE(1) 1426 { 1427 int dummy; 1428 return name_to_color(name.data, &dummy); 1429 } 1430 1431 /// Returns a map of color names and RGB values. 1432 /// 1433 /// Keys are color names (e.g. "Aqua") and values are 24-bit RGB color values 1434 /// (e.g. 65535). 1435 /// 1436 /// @return Map of color names and RGB values. 1437 DictOf(Integer) nvim_get_color_map(Arena *arena) 1438 FUNC_API_SINCE(1) 1439 { 1440 DictOf(Integer) colors = arena_dict(arena, ARRAY_SIZE(color_name_table)); 1441 1442 for (int i = 0; color_name_table[i].name != NULL; i++) { 1443 PUT_C(colors, color_name_table[i].name, INTEGER_OBJ(color_name_table[i].color)); 1444 } 1445 return colors; 1446 } 1447 1448 /// Gets a map of the current editor state. 1449 /// 1450 /// @param opts Optional parameters. 1451 /// - types: List of |context-types| ("regs", "jumps", "bufs", 1452 /// "gvars", …) to gather, or empty for "all". 1453 /// @param[out] err Error details, if any 1454 /// 1455 /// @return map of global |context|. 1456 Dict nvim_get_context(Dict(context) *opts, Arena *arena, Error *err) 1457 FUNC_API_SINCE(6) 1458 { 1459 Array types = ARRAY_DICT_INIT; 1460 if (HAS_KEY(opts, context, types)) { 1461 types = opts->types; 1462 } 1463 1464 int int_types = types.size > 0 ? 0 : kCtxAll; 1465 if (types.size > 0) { 1466 for (size_t i = 0; i < types.size; i++) { 1467 if (types.items[i].type == kObjectTypeString) { 1468 const char *const s = types.items[i].data.string.data; 1469 if (strequal(s, "regs")) { 1470 int_types |= kCtxRegs; 1471 } else if (strequal(s, "jumps")) { 1472 int_types |= kCtxJumps; 1473 } else if (strequal(s, "bufs")) { 1474 int_types |= kCtxBufs; 1475 } else if (strequal(s, "gvars")) { 1476 int_types |= kCtxGVars; 1477 } else if (strequal(s, "sfuncs")) { 1478 int_types |= kCtxSFuncs; 1479 } else if (strequal(s, "funcs")) { 1480 int_types |= kCtxFuncs; 1481 } else { 1482 VALIDATE_S(false, "type", s, { 1483 return (Dict)ARRAY_DICT_INIT; 1484 }); 1485 } 1486 } 1487 } 1488 } 1489 1490 Context ctx = CONTEXT_INIT; 1491 ctx_save(&ctx, int_types); 1492 Dict dict = ctx_to_dict(&ctx, arena); 1493 ctx_free(&ctx); 1494 return dict; 1495 } 1496 1497 /// Sets the current editor state from the given |context| map. 1498 /// 1499 /// @param dict |Context| map. 1500 Object nvim_load_context(Dict dict, Error *err) 1501 FUNC_API_SINCE(6) 1502 { 1503 Context ctx = CONTEXT_INIT; 1504 1505 int save_did_emsg = did_emsg; 1506 did_emsg = false; 1507 1508 ctx_from_dict(dict, &ctx, err); 1509 if (!ERROR_SET(err)) { 1510 ctx_restore(&ctx, kCtxAll); 1511 } 1512 1513 ctx_free(&ctx); 1514 1515 did_emsg = save_did_emsg; 1516 return (Object)OBJECT_INIT; 1517 } 1518 1519 /// Gets the current mode. |mode()| 1520 /// "blocking" is true if Nvim is waiting for input. 1521 /// 1522 /// @returns Dict { "mode": String, "blocking": Boolean } 1523 DictAs(get_mode) nvim_get_mode(Arena *arena) 1524 FUNC_API_SINCE(2) FUNC_API_FAST 1525 { 1526 Dict rv = arena_dict(arena, 2); 1527 char *modestr = arena_alloc(arena, MODE_MAX_LENGTH, false); 1528 get_mode(modestr); 1529 bool blocked = input_blocking(); 1530 1531 PUT_C(rv, "mode", CSTR_AS_OBJ(modestr)); 1532 PUT_C(rv, "blocking", BOOLEAN_OBJ(blocked)); 1533 1534 return rv; 1535 } 1536 1537 /// Gets a list of global (non-buffer-local) |mapping| definitions. 1538 /// 1539 /// @param mode Mode short-name ("n", "i", "v", ...) 1540 /// @returns Array of |maparg()|-like dictionaries describing mappings. 1541 /// The "buffer" key is always zero. 1542 ArrayOf(DictAs(get_keymap)) nvim_get_keymap(String mode, Arena *arena) 1543 FUNC_API_SINCE(3) 1544 { 1545 return keymap_array(mode, NULL, arena); 1546 } 1547 1548 /// Sets a global |mapping| for the given mode. 1549 /// 1550 /// To set a buffer-local mapping, use |nvim_buf_set_keymap()|. 1551 /// 1552 /// Unlike |:map|, leading/trailing whitespace is accepted as part of the {lhs} or {rhs}. 1553 /// Empty {rhs} is [<Nop>]. |keycodes| are replaced as usual. 1554 /// 1555 /// Example: 1556 /// 1557 /// ```vim 1558 /// call nvim_set_keymap('n', ' <NL>', '', {'nowait': v:true}) 1559 /// ``` 1560 /// 1561 /// is equivalent to: 1562 /// 1563 /// ```vim 1564 /// nmap <nowait> <Space><NL> <Nop> 1565 /// ``` 1566 /// 1567 /// @param channel_id 1568 /// @param mode Mode short-name (map command prefix: "n", "i", "v", "x", …) 1569 /// or "!" for |:map!|, or empty string for |:map|. 1570 /// "ia", "ca" or "!a" for abbreviation in Insert mode, Cmdline mode, or both, respectively 1571 /// @param lhs Left-hand-side |{lhs}| of the mapping. 1572 /// @param rhs Right-hand-side |{rhs}| of the mapping. 1573 /// @param opts Optional parameters map: Accepts all |:map-arguments| as keys except [<buffer>], 1574 /// values are booleans (default false). Also: 1575 /// - "noremap" disables |recursive_mapping|, like |:noremap| 1576 /// - "desc" human-readable description. 1577 /// - "callback" Lua function called in place of {rhs}. 1578 /// - "replace_keycodes" (boolean) When "expr" is true, replace keycodes in the 1579 /// resulting string (see |nvim_replace_termcodes()|). Returning nil from the Lua 1580 /// "callback" is equivalent to returning an empty string. 1581 /// @param[out] err Error details, if any. 1582 void nvim_set_keymap(uint64_t channel_id, String mode, String lhs, String rhs, Dict(keymap) *opts, 1583 Error *err) 1584 FUNC_API_SINCE(6) 1585 { 1586 modify_keymap(channel_id, -1, false, mode, lhs, rhs, opts, err); 1587 } 1588 1589 /// Unmaps a global |mapping| for the given mode. 1590 /// 1591 /// To unmap a buffer-local mapping, use |nvim_buf_del_keymap()|. 1592 /// 1593 /// @see |nvim_set_keymap()| 1594 void nvim_del_keymap(uint64_t channel_id, String mode, String lhs, Error *err) 1595 FUNC_API_SINCE(6) 1596 { 1597 nvim_buf_del_keymap(channel_id, -1, mode, lhs, err); 1598 } 1599 1600 /// Returns a 2-tuple (Array), where item 0 is the current channel id and item 1601 /// 1 is the |api-metadata| map (Dict). 1602 /// 1603 /// @returns 2-tuple `[{channel-id}, {api-metadata}]` 1604 ArrayOf(Object, 2) nvim_get_api_info(uint64_t channel_id, Arena *arena) 1605 FUNC_API_SINCE(1) FUNC_API_FAST FUNC_API_REMOTE_ONLY 1606 { 1607 Array rv = arena_array(arena, 2); 1608 1609 assert(channel_id <= INT64_MAX); 1610 ADD_C(rv, INTEGER_OBJ((int64_t)channel_id)); 1611 ADD_C(rv, api_metadata()); 1612 1613 return rv; 1614 } 1615 1616 /// Self-identifies the client, and sets optional flags on the channel. Defines the `client` object 1617 /// returned by |nvim_get_chan_info()|. 1618 /// 1619 /// Clients should call this just after connecting, to provide hints for debugging and 1620 /// orchestration. (Note: Something is better than nothing! Fields are optional, but at least set 1621 /// `name`.) 1622 /// 1623 /// Can be called more than once; caller should merge old info if appropriate. Example: a library 1624 /// first identifies the channel, then a plugin using that library later identifies itself. 1625 /// 1626 /// @param channel_id 1627 /// @param name Client short-name. Sets the `client.name` field of |nvim_get_chan_info()|. 1628 /// @param version Dict describing the version, with these 1629 /// (optional) keys: 1630 /// - "major" major version (defaults to 0 if not set, for no release yet) 1631 /// - "minor" minor version 1632 /// - "patch" patch number 1633 /// - "prerelease" string describing a prerelease, like "dev" or "beta1" 1634 /// - "commit" hash or similar identifier of commit 1635 /// @param type Must be one of the following values. Client libraries should 1636 /// default to "remote" unless overridden by the user. 1637 /// - "remote" remote client connected "Nvim flavored" MessagePack-RPC (responses 1638 /// must be in reverse order of requests). |msgpack-rpc| 1639 /// - "msgpack-rpc" remote client connected to Nvim via fully MessagePack-RPC 1640 /// compliant protocol. 1641 /// - "ui" gui frontend 1642 /// - "embedder" application using Nvim as a component (for example, 1643 /// IDE/editor implementing a vim mode). 1644 /// - "host" plugin host, typically started by nvim 1645 /// - "plugin" single plugin, started by nvim 1646 /// @param methods Builtin methods in the client. For a host, this does not 1647 /// include plugin methods which will be discovered later. 1648 /// The key should be the method name, the values are dicts with 1649 /// these (optional) keys (more keys may be added in future 1650 /// versions of Nvim, thus unknown keys are ignored. Clients 1651 /// must only use keys defined in this or later versions of 1652 /// Nvim): 1653 /// - "async" if true, send as a notification. If false or unspecified, 1654 /// use a blocking request 1655 /// - "nargs" Number of arguments. Could be a single integer or an array 1656 /// of two integers, minimum and maximum inclusive. 1657 /// 1658 /// @param attributes Arbitrary string:string map of informal client properties. 1659 /// Suggested keys: 1660 /// - "pid": Process id. 1661 /// - "website": Client homepage URL (e.g. GitHub repository) 1662 /// - "license": License description ("Apache 2", "GPLv3", "MIT", …) 1663 /// - "logo": URI or path to image, preferably small logo or icon. 1664 /// .png or .svg format is preferred. 1665 /// 1666 /// @param[out] err Error details, if any 1667 void nvim_set_client_info(uint64_t channel_id, String name, Dict version, String type, Dict methods, 1668 Dict attributes, Arena *arena, Error *err) 1669 FUNC_API_SINCE(4) FUNC_API_REMOTE_ONLY 1670 { 1671 MAXSIZE_TEMP_DICT(info, 5); 1672 PUT_C(info, "name", STRING_OBJ(name)); 1673 1674 bool has_major = false; 1675 for (size_t i = 0; i < version.size; i++) { 1676 if (strequal(version.items[i].key.data, "major")) { 1677 has_major = true; 1678 break; 1679 } 1680 } 1681 if (!has_major) { 1682 Dict v = arena_dict(arena, version.size + 1); 1683 if (version.size) { 1684 memcpy(v.items, version.items, version.size * sizeof(v.items[0])); 1685 v.size = version.size; 1686 } 1687 PUT_C(v, "major", INTEGER_OBJ(0)); 1688 version = v; 1689 } 1690 PUT_C(info, "version", DICT_OBJ(version)); 1691 1692 PUT_C(info, "type", STRING_OBJ(type)); 1693 PUT_C(info, "methods", DICT_OBJ(methods)); 1694 PUT_C(info, "attributes", DICT_OBJ(attributes)); 1695 1696 rpc_set_client_info(channel_id, copy_dict(info, NULL)); 1697 } 1698 1699 /// Gets information about a channel. 1700 /// 1701 /// See |nvim_list_uis()| for an example of how to get channel info. 1702 /// 1703 /// @param chan channel_id, or 0 for current channel 1704 /// @returns Channel info dict with these keys: 1705 /// - "id" Channel id. 1706 /// - "argv" (optional) Job arguments list. 1707 /// - "stream" Stream underlying the channel. 1708 /// - "stdio" stdin and stdout of this Nvim instance 1709 /// - "stderr" stderr of this Nvim instance 1710 /// - "socket" TCP/IP socket or named pipe 1711 /// - "job" Job with communication over its stdio. 1712 /// - "mode" How data received on the channel is interpreted. 1713 /// - "bytes" Send and receive raw bytes. 1714 /// - "terminal" |terminal| instance interprets ASCII sequences. 1715 /// - "rpc" |RPC| communication on the channel is active. 1716 /// - "pty" (optional) Name of pseudoterminal. On a POSIX system this is a device path like 1717 /// "/dev/pts/1". If unknown, the key will still be present if a pty is used (e.g. 1718 /// for conpty on Windows). 1719 /// - "buffer" (optional) Buffer connected to |terminal| instance. 1720 /// - "client" (optional) Info about the peer (client on the other end of the channel), as set 1721 /// by |nvim_set_client_info()|. 1722 /// 1723 Dict nvim_get_chan_info(uint64_t channel_id, Integer chan, Arena *arena, Error *err) 1724 FUNC_API_SINCE(4) 1725 { 1726 if (chan < 0) { 1727 return (Dict)ARRAY_DICT_INIT; 1728 } 1729 1730 if (chan == 0 && !is_internal_call(channel_id)) { 1731 assert(channel_id <= INT64_MAX); 1732 chan = (Integer)channel_id; 1733 } 1734 return channel_info((uint64_t)chan, arena); 1735 } 1736 1737 /// Get information about all open channels. 1738 /// 1739 /// @returns Array of Dictionaries, each describing a channel with 1740 /// the format specified at |nvim_get_chan_info()|. 1741 ArrayOf(Dict) nvim_list_chans(Arena *arena) 1742 FUNC_API_SINCE(4) 1743 { 1744 return channel_all_info(arena); 1745 } 1746 1747 // Functions used for testing purposes 1748 1749 /// Returns object given as argument. 1750 /// 1751 /// This API function is used for testing. One should not rely on its presence 1752 /// in plugins. 1753 /// 1754 /// @param[in] obj Object to return. 1755 /// 1756 /// @return its argument. 1757 Object nvim__id(Object obj, Arena *arena) 1758 { 1759 return copy_object(obj, arena); 1760 } 1761 1762 /// Returns array given as argument. 1763 /// 1764 /// This API function is used for testing. One should not rely on its presence 1765 /// in plugins. 1766 /// 1767 /// @param[in] arr Array to return. 1768 /// 1769 /// @return its argument. 1770 Array nvim__id_array(Array arr, Arena *arena) 1771 { 1772 return copy_array(arr, arena); 1773 } 1774 1775 /// Returns dict given as argument. 1776 /// 1777 /// This API function is used for testing. One should not rely on its presence 1778 /// in plugins. 1779 /// 1780 /// @param[in] dct Dict to return. 1781 /// 1782 /// @return its argument. 1783 Dict nvim__id_dict(Dict dct, Arena *arena) 1784 { 1785 return copy_dict(dct, arena); 1786 } 1787 1788 /// Returns floating-point value given as argument. 1789 /// 1790 /// This API function is used for testing. One should not rely on its presence 1791 /// in plugins. 1792 /// 1793 /// @param[in] flt Value to return. 1794 /// 1795 /// @return its argument. 1796 Float nvim__id_float(Float flt) 1797 { 1798 return flt; 1799 } 1800 1801 /// Gets internal stats. 1802 /// 1803 /// @return Map of various internal stats. 1804 Dict nvim__stats(Arena *arena) 1805 { 1806 Dict rv = arena_dict(arena, 6); 1807 PUT_C(rv, "fsync", INTEGER_OBJ(g_stats.fsync)); 1808 PUT_C(rv, "log_skip", INTEGER_OBJ(g_stats.log_skip)); 1809 PUT_C(rv, "lua_refcount", INTEGER_OBJ(nlua_get_global_ref_count())); 1810 PUT_C(rv, "redraw", INTEGER_OBJ(g_stats.redraw)); 1811 PUT_C(rv, "arena_alloc_count", INTEGER_OBJ((Integer)arena_alloc_count)); 1812 PUT_C(rv, "ts_query_parse_count", INTEGER_OBJ((Integer)tslua_query_parse_count)); 1813 return rv; 1814 } 1815 1816 /// Gets a list of dictionaries representing attached UIs. 1817 /// 1818 /// Example: The Nvim builtin |TUI| sets its channel info as described in |startup-tui|. In 1819 /// particular, it sets `client.name` to "nvim-tui". So you can check if the TUI is running by 1820 /// inspecting the client name of each UI: 1821 /// 1822 /// ```lua 1823 /// vim.print(vim.api.nvim_get_chan_info(vim.api.nvim_list_uis()[1].chan).client.name) 1824 /// ``` 1825 /// 1826 /// @return Array of UI dictionaries, each with these keys: 1827 /// - "height" Requested height of the UI 1828 /// - "width" Requested width of the UI 1829 /// - "rgb" true if the UI uses RGB colors (false implies |cterm-colors|) 1830 /// - "ext_..." Requested UI extensions, see |ui-option| 1831 /// - "chan" |channel-id| of remote UI 1832 ArrayOf(Dict) nvim_list_uis(Arena *arena) 1833 FUNC_API_SINCE(4) 1834 { 1835 return ui_array(arena); 1836 } 1837 1838 /// Gets the immediate children of process `pid`. 1839 /// 1840 /// @return Array of child process ids, empty if process not found. 1841 Array nvim_get_proc_children(Integer pid, Arena *arena, Error *err) 1842 FUNC_API_SINCE(4) 1843 { 1844 Array rvobj = ARRAY_DICT_INIT; 1845 int *proc_list = NULL; 1846 1847 VALIDATE_INT((pid > 0 && pid <= INT_MAX), "pid", pid, { 1848 goto end; 1849 }); 1850 1851 size_t proc_count; 1852 int rv = os_proc_children((int)pid, &proc_list, &proc_count); 1853 if (rv == 2) { 1854 // syscall failed (possibly because of kernel options), try shelling out. 1855 DLOG("fallback to vim._os_proc_children()"); 1856 MAXSIZE_TEMP_ARRAY(a, 1); 1857 ADD_C(a, INTEGER_OBJ(pid)); 1858 Object o = NLUA_EXEC_STATIC("return vim._os_proc_children(...)", a, kRetObject, arena, err); 1859 if (o.type == kObjectTypeArray) { 1860 rvobj = o.data.array; 1861 } else if (!ERROR_SET(err)) { 1862 api_set_error(err, kErrorTypeException, 1863 "Failed to get process children. pid=%" PRId64 " error=%d", 1864 pid, rv); 1865 } 1866 } else { 1867 rvobj = arena_array(arena, proc_count); 1868 for (size_t i = 0; i < proc_count; i++) { 1869 ADD_C(rvobj, INTEGER_OBJ(proc_list[i])); 1870 } 1871 } 1872 1873 end: 1874 xfree(proc_list); 1875 return rvobj; 1876 } 1877 1878 /// Gets info describing process `pid`. 1879 /// 1880 /// @return Map of process properties, or NIL if process not found. 1881 Object nvim_get_proc(Integer pid, Arena *arena, Error *err) 1882 FUNC_API_SINCE(4) 1883 { 1884 Object rvobj = NIL; 1885 1886 VALIDATE_INT((pid > 0 && pid <= INT_MAX), "pid", pid, { 1887 return NIL; 1888 }); 1889 1890 #ifdef MSWIN 1891 rvobj = DICT_OBJ(os_proc_info((int)pid, arena)); 1892 if (rvobj.data.dict.size == 0) { // Process not found. 1893 return NIL; 1894 } 1895 #else 1896 // Cross-platform process info APIs are miserable, so use `ps` instead. 1897 MAXSIZE_TEMP_ARRAY(a, 1); 1898 ADD(a, INTEGER_OBJ(pid)); 1899 Object o = NLUA_EXEC_STATIC("return vim._os_proc_info(...)", a, kRetObject, arena, err); 1900 if (o.type == kObjectTypeArray && o.data.array.size == 0) { 1901 return NIL; // Process not found. 1902 } else if (o.type == kObjectTypeDict) { 1903 rvobj = o; 1904 } else if (!ERROR_SET(err)) { 1905 api_set_error(err, kErrorTypeException, 1906 "Failed to get process info. pid=%" PRId64, pid); 1907 } 1908 #endif 1909 return rvobj; 1910 } 1911 1912 /// Selects an item in the completion popup menu. 1913 /// 1914 /// If neither |ins-completion| nor |cmdline-completion| popup menu is active 1915 /// this API call is silently ignored. 1916 /// Useful for an external UI using |ui-popupmenu| to control the popup menu with the mouse. 1917 /// Can also be used in a mapping; use [<Cmd>] |:map-cmd| or a Lua mapping to ensure the mapping 1918 /// doesn't end completion mode. 1919 /// 1920 /// @param item Index (zero-based) of the item to select. Value of -1 selects nothing 1921 /// and restores the original text. 1922 /// @param insert For |ins-completion|, whether the selection should be inserted in the buffer. 1923 /// Ignored for |cmdline-completion|. 1924 /// @param finish Finish the completion and dismiss the popup menu. Implies {insert}. 1925 /// @param opts Optional parameters. Reserved for future use. 1926 /// @param[out] err Error details, if any 1927 void nvim_select_popupmenu_item(Integer item, Boolean insert, Boolean finish, Dict(empty) *opts, 1928 Error *err) 1929 FUNC_API_SINCE(6) 1930 { 1931 if (finish) { 1932 insert = true; 1933 } 1934 1935 pum_ext_select_item((int)item, insert, finish); 1936 } 1937 1938 /// NB: if your UI doesn't use hlstate, this will not return hlstate first time. 1939 Array nvim__inspect_cell(Integer grid, Integer row, Integer col, Arena *arena, Error *err) 1940 { 1941 Array ret = ARRAY_DICT_INIT; 1942 1943 // TODO(bfredl): if grid == 0 we should read from the compositor's buffer. 1944 // The only problem is that it does not yet exist. 1945 ScreenGrid *g = &default_grid; 1946 if (grid == pum_grid.handle) { 1947 g = &pum_grid; 1948 } else if (grid > 1) { 1949 win_T *wp = get_win_by_grid_handle((handle_T)grid); 1950 VALIDATE_INT((wp != NULL && wp->w_grid_alloc.chars != NULL), "grid handle", grid, { 1951 return ret; 1952 }); 1953 g = &wp->w_grid_alloc; 1954 } 1955 1956 if (row < 0 || row >= g->rows 1957 || col < 0 || col >= g->cols) { 1958 return ret; 1959 } 1960 ret = arena_array(arena, 3); 1961 size_t off = g->line_offset[(size_t)row] + (size_t)col; 1962 char *sc_buf = arena_alloc(arena, MAX_SCHAR_SIZE, false); 1963 schar_get(sc_buf, g->chars[off]); 1964 ADD_C(ret, CSTR_AS_OBJ(sc_buf)); 1965 int attr = g->attrs[off]; 1966 ADD_C(ret, DICT_OBJ(hl_get_attr_by_id(attr, true, arena, err))); 1967 // will not work first time 1968 if (!highlight_use_hlstate()) { 1969 ADD_C(ret, ARRAY_OBJ(hl_inspect(attr, arena))); 1970 } 1971 return ret; 1972 } 1973 1974 /// @nodoc 1975 void nvim__screenshot(String path) 1976 FUNC_API_FAST 1977 { 1978 ui_call_screenshot(path); 1979 } 1980 1981 /// For testing. The condition in schar_cache_clear_if_full is hard to 1982 /// reach, so this function can be used to force a cache clear in a test. 1983 void nvim__invalidate_glyph_cache(void) 1984 { 1985 schar_cache_clear(); 1986 must_redraw = UPD_CLEAR; 1987 } 1988 1989 /// @nodoc 1990 Object nvim__unpack(String str, Arena *arena, Error *err) 1991 FUNC_API_FAST 1992 { 1993 return unpack(str.data, str.size, arena, err); 1994 } 1995 1996 /// Deletes an uppercase/file named mark. See |mark-motions|. 1997 /// 1998 /// @note Lowercase name (or other buffer-local mark) is an error. 1999 /// @param name Mark name 2000 /// @return true if the mark was deleted, else false. 2001 /// @see |nvim_buf_del_mark()| 2002 /// @see |nvim_get_mark()| 2003 Boolean nvim_del_mark(String name, Error *err) 2004 FUNC_API_SINCE(8) 2005 { 2006 bool res = false; 2007 VALIDATE_S((name.size == 1), "mark name (must be a single char)", name.data, { 2008 return res; 2009 }); 2010 // Only allow file/uppercase marks 2011 // TODO(muniter): Refactor this ASCII_ISUPPER macro to a proper function 2012 VALIDATE_S((ASCII_ISUPPER(*name.data) || ascii_isdigit(*name.data)), 2013 "mark name (must be file/uppercase)", name.data, { 2014 return res; 2015 }); 2016 res = set_mark(NULL, name, 0, 0, err); 2017 return res; 2018 } 2019 2020 /// Returns a `(row, col, buffer, buffername)` tuple representing the position 2021 /// of the uppercase/file named mark. "End of line" column position is returned 2022 /// as |v:maxcol| (big number). See |mark-motions|. 2023 /// 2024 /// Marks are (1,0)-indexed. |api-indexing| 2025 /// 2026 /// @note Lowercase name (or other buffer-local mark) is an error. 2027 /// @param name Mark name 2028 /// @param opts Optional parameters. Reserved for future use. 2029 /// @return 4-tuple (row, col, buffer, buffername), (0, 0, 0, '') if the mark is 2030 /// not set. 2031 /// @see |nvim_buf_set_mark()| 2032 /// @see |nvim_del_mark()| 2033 Tuple(Integer, Integer, Buffer, String) nvim_get_mark(String name, Dict(empty) *opts, Arena *arena, 2034 Error *err) 2035 FUNC_API_SINCE(8) 2036 { 2037 Array rv = ARRAY_DICT_INIT; 2038 2039 VALIDATE_S((name.size == 1), "mark name (must be a single char)", name.data, { 2040 return rv; 2041 }); 2042 VALIDATE_S((ASCII_ISUPPER(*name.data) || ascii_isdigit(*name.data)), 2043 "mark name (must be file/uppercase)", name.data, { 2044 return rv; 2045 }); 2046 2047 xfmark_T *mark = mark_get_global(false, *name.data); // false avoids loading the mark buffer 2048 pos_T pos = mark->fmark.mark; 2049 bool allocated = false; 2050 int bufnr; 2051 char *filename; 2052 2053 // Marks are from an open buffer it fnum is non zero 2054 if (mark->fmark.fnum != 0) { 2055 bufnr = mark->fmark.fnum; 2056 filename = buflist_nr2name(bufnr, true, true); 2057 allocated = true; 2058 // Marks comes from shada 2059 } else { 2060 filename = mark->fname; 2061 bufnr = 0; 2062 } 2063 2064 bool exists = filename != NULL; 2065 Integer row; 2066 Integer col; 2067 2068 if (!exists || pos.lnum <= 0) { 2069 if (allocated) { 2070 xfree(filename); 2071 allocated = false; 2072 } 2073 filename = ""; 2074 bufnr = 0; 2075 row = 0; 2076 col = 0; 2077 } else { 2078 row = pos.lnum; 2079 col = pos.col; 2080 } 2081 2082 rv = arena_array(arena, 4); 2083 ADD_C(rv, INTEGER_OBJ(row)); 2084 ADD_C(rv, INTEGER_OBJ(col)); 2085 ADD_C(rv, INTEGER_OBJ(bufnr)); 2086 ADD_C(rv, CSTR_TO_ARENA_OBJ(arena, filename)); 2087 2088 if (allocated) { 2089 xfree(filename); 2090 } 2091 2092 return rv; 2093 } 2094 2095 /// Evaluates statusline string. 2096 /// 2097 /// @param str Statusline string (see 'statusline'). 2098 /// @param opts Optional parameters. 2099 /// - winid: (number) |window-ID| of the window to use as context for statusline. 2100 /// - maxwidth: (number) Maximum width of statusline. 2101 /// - fillchar: (string) Character to fill blank spaces in the statusline (see 2102 /// 'fillchars'). Treated as single-width even if it isn't. 2103 /// - highlights: (boolean) Return highlight information. 2104 /// - use_winbar: (boolean) Evaluate winbar instead of statusline. 2105 /// - use_tabline: (boolean) Evaluate tabline instead of statusline. When true, {winid} 2106 /// is ignored. Mutually exclusive with {use_winbar}. 2107 /// - use_statuscol_lnum: (number) Evaluate statuscolumn for this line number instead of statusline. 2108 /// 2109 /// @param[out] err Error details, if any. 2110 /// @return Dict containing statusline information, with these keys: 2111 /// - str: (string) Characters that will be displayed on the statusline. 2112 /// - width: (number) Display width of the statusline. 2113 /// - highlights: Array containing highlight information of the statusline. Only included when 2114 /// the "highlights" key in {opts} is true. Each element of the array is a 2115 /// |Dict| with these keys: 2116 /// - start: (number) Byte index (0-based) of first character that uses the highlight. 2117 /// - group: (string) Deprecated. Use `groups` instead. 2118 /// - groups: (array) Names of stacked highlight groups (highest priority last). 2119 DictAs(eval_statusline_ret) nvim_eval_statusline(String str, Dict(eval_statusline) *opts, 2120 Arena *arena, Error *err) 2121 FUNC_API_SINCE(8) FUNC_API_FAST 2122 { 2123 Dict result = ARRAY_DICT_INIT; 2124 2125 int maxwidth; 2126 schar_T fillchar = 0; 2127 int statuscol_lnum = 0; 2128 2129 if (str.size < 2 || memcmp(str.data, "%!", 2) != 0) { 2130 const char *const errmsg = check_stl_option(str.data); 2131 VALIDATE(!errmsg, "%s", errmsg, { 2132 return result; 2133 }); 2134 } 2135 2136 Window window = opts->winid; 2137 2138 if (HAS_KEY(opts, eval_statusline, fillchar)) { 2139 VALIDATE_EXP((*opts->fillchar.data != 0 2140 && ((size_t)utfc_ptr2len(opts->fillchar.data) == opts->fillchar.size)), 2141 "fillchar", "single character", NULL, { 2142 return result; 2143 }); 2144 int c; 2145 fillchar = utfc_ptr2schar(opts->fillchar.data, &c); 2146 // TODO(bfredl): actually check c is single width 2147 } 2148 2149 int use_bools = (int)opts->use_winbar + (int)opts->use_tabline; 2150 2151 win_T *wp = opts->use_tabline ? curwin : find_window_by_handle(window, err); 2152 if (wp == NULL) { 2153 api_set_error(err, kErrorTypeException, "unknown winid %d", window); 2154 return result; 2155 } 2156 2157 if (HAS_KEY(opts, eval_statusline, use_statuscol_lnum)) { 2158 statuscol_lnum = (int)opts->use_statuscol_lnum; 2159 VALIDATE_RANGE(statuscol_lnum > 0 && statuscol_lnum <= wp->w_buffer->b_ml.ml_line_count, 2160 "use_statuscol_lnum", { 2161 return result; 2162 }); 2163 use_bools++; 2164 } 2165 VALIDATE(use_bools <= 1, "%s", 2166 "Can only use one of 'use_winbar', 'use_tabline' and 'use_statuscol_lnum'", { 2167 return result; 2168 }); 2169 2170 int stc_hl_id = 0; 2171 int scl_hl_id = 0; 2172 statuscol_T statuscol = { 0 }; 2173 SignTextAttrs sattrs[SIGN_SHOW_MAX] = { 0 }; 2174 2175 if (statuscol_lnum) { 2176 int line_id = 0; 2177 int cul_id = 0; 2178 int num_id = 0; 2179 linenr_T lnum = statuscol_lnum; 2180 foldinfo_T cursorline_fi = { 0 }; 2181 decor_redraw_signs(wp, wp->w_buffer, lnum - 1, sattrs, &line_id, &cul_id, &num_id); 2182 2183 statuscol.sattrs = sattrs; 2184 statuscol.foldinfo = fold_info(wp, lnum); 2185 win_update_cursorline(wp, &cursorline_fi); 2186 statuscol.sign_cul_id = use_cursor_line_highlight(wp, lnum) ? cul_id : 0; 2187 scl_hl_id = use_cursor_line_highlight(wp, lnum) ? HLF_CLS : HLF_SC; 2188 2189 if (num_id) { 2190 stc_hl_id = num_id; 2191 } else if (use_cursor_line_highlight(wp, lnum)) { 2192 stc_hl_id = HLF_CLN; 2193 } else if (wp->w_p_rnu) { 2194 stc_hl_id = (lnum < wp->w_cursor.lnum ? HLF_LNA : HLF_LNB); 2195 } else { 2196 stc_hl_id = HLF_N; 2197 } 2198 2199 set_vim_var_nr(VV_LNUM, lnum); 2200 set_vim_var_nr(VV_RELNUM, labs(get_cursor_rel_lnum(wp, lnum))); 2201 set_vim_var_nr(VV_VIRTNUM, 0); 2202 } else if (fillchar == 0 && !opts->use_tabline) { 2203 if (opts->use_winbar) { 2204 fillchar = wp->w_p_fcs_chars.wbr; 2205 } else { 2206 hlf_T group; 2207 fillchar = fillchar_status(&group, wp); 2208 } 2209 } 2210 2211 if (HAS_KEY(opts, eval_statusline, maxwidth)) { 2212 maxwidth = (int)opts->maxwidth; 2213 } else { 2214 maxwidth = statuscol_lnum ? win_col_off(wp) 2215 : (opts->use_tabline 2216 || (!opts->use_winbar 2217 && global_stl_height() > 0)) ? Columns : wp->w_width; 2218 } 2219 2220 result = arena_dict(arena, 3); 2221 char *buf = arena_alloc(arena, MAXPATHL, false); 2222 stl_hlrec_t *hltab; 2223 size_t hltab_len = 0; 2224 2225 // Temporarily reset 'cursorbind' to prevent side effects from moving the cursor away and back. 2226 int p_crb_save = wp->w_p_crb; 2227 wp->w_p_crb = false; 2228 2229 int width = build_stl_str_hl(wp, buf, MAXPATHL, str.data, -1, 0, fillchar, maxwidth, 2230 opts->highlights ? &hltab : NULL, &hltab_len, NULL, 2231 statuscol_lnum ? &statuscol : NULL); 2232 2233 PUT_C(result, "width", INTEGER_OBJ(width)); 2234 2235 // Restore original value of 'cursorbind' 2236 wp->w_p_crb = p_crb_save; 2237 2238 if (opts->highlights) { 2239 Array hl_values = arena_array(arena, hltab_len + 1); 2240 char user_group[15]; // strlen("User") + strlen("2147483647") + NUL 2241 2242 // If first character doesn't have a defined highlight, 2243 // add the default highlight at the beginning of the highlight list 2244 const char *dfltname = get_default_stl_hl(opts->use_tabline ? NULL : wp, 2245 opts->use_winbar, stc_hl_id); 2246 if (hltab->start == NULL || (hltab->start - buf) != 0) { 2247 Dict hl_info = arena_dict(arena, 3); 2248 PUT_C(hl_info, "start", INTEGER_OBJ(0)); 2249 PUT_C(hl_info, "group", CSTR_AS_OBJ(dfltname)); 2250 Array groups = arena_array(arena, 1); 2251 ADD_C(groups, CSTR_AS_OBJ(dfltname)); 2252 PUT_C(hl_info, "groups", ARRAY_OBJ(groups)); 2253 ADD_C(hl_values, DICT_OBJ(hl_info)); 2254 } 2255 2256 for (stl_hlrec_t *sp = hltab; sp->start != NULL; sp++) { 2257 const char *grpname; 2258 if (sp->userhl == 0) { 2259 grpname = get_default_stl_hl(opts->use_tabline ? NULL : wp, opts->use_winbar, stc_hl_id); 2260 } else if (sp->userhl < 0) { 2261 grpname = syn_id2name(-sp->userhl); 2262 } else { 2263 snprintf(user_group, sizeof(user_group), "User%d", sp->userhl); 2264 grpname = arena_strdup(arena, user_group); 2265 } 2266 2267 const char *combine = sp->item == STL_SIGNCOL ? syn_id2name(scl_hl_id) 2268 : sp->item == STL_FOLDCOL ? grpname : dfltname; 2269 Dict hl_info = arena_dict(arena, 3); 2270 PUT_C(hl_info, "start", INTEGER_OBJ(sp->start - buf)); 2271 PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname)); 2272 Array groups = arena_array(arena, 1 + (combine != grpname)); 2273 if (combine != grpname) { 2274 ADD_C(groups, CSTR_AS_OBJ(combine)); 2275 } 2276 ADD_C(groups, CSTR_AS_OBJ(grpname)); 2277 PUT_C(hl_info, "groups", ARRAY_OBJ(groups)); 2278 ADD_C(hl_values, DICT_OBJ(hl_info)); 2279 } 2280 PUT_C(result, "highlights", ARRAY_OBJ(hl_values)); 2281 } 2282 PUT_C(result, "str", CSTR_AS_OBJ(buf)); 2283 2284 return result; 2285 } 2286 2287 /// EXPERIMENTAL: this API may change in the future. 2288 /// 2289 /// Sets info for the completion item at the given index. If the info text was shown in a window, 2290 /// returns the window and buffer ids, or empty dict if not shown. 2291 /// 2292 /// @param index Completion candidate index 2293 /// @param opts Optional parameters. 2294 /// - info: (string) info text. 2295 /// @return Dict containing these keys: 2296 /// - winid: (number) floating window id 2297 /// - bufnr: (number) buffer id in floating window 2298 DictOf(Float) nvim__complete_set(Integer index, Dict(complete_set) *opts, Arena *arena, Error *err) 2299 { 2300 Dict rv = arena_dict(arena, 2); 2301 if ((get_cot_flags() & kOptCotFlagPopup) == 0) { 2302 api_set_error(err, kErrorTypeException, "completeopt option does not include popup"); 2303 return rv; 2304 } 2305 if (HAS_KEY(opts, complete_set, info)) { 2306 win_T *wp = pum_set_info((int)index, opts->info.data); 2307 if (wp) { 2308 PUT_C(rv, "winid", WINDOW_OBJ(wp->handle)); 2309 PUT_C(rv, "bufnr", BUFFER_OBJ(wp->w_buffer->handle)); 2310 } 2311 } 2312 return rv; 2313 } 2314 2315 static void redraw_status(win_T *wp, Dict(redraw) *opts, bool *flush) 2316 { 2317 if (opts->statuscolumn && *wp->w_p_stc != NUL) { 2318 wp->w_nrwidth_line_count = 0; 2319 changed_window_setting(wp); 2320 } 2321 2322 int old_row_offset = wp->w_grid.row_offset; 2323 win_grid_alloc(wp); 2324 2325 // Flush later in case winbar was just hidden or shown for the first time, or 2326 // statuscolumn is being drawn. 2327 if (wp->w_lines_valid == 0 || wp->w_grid.row_offset != old_row_offset) { 2328 *flush = true; 2329 } 2330 2331 // Mark for redraw in case flush will happen, otherwise redraw now. 2332 if (*flush && (opts->statusline || opts->winbar)) { 2333 wp->w_redr_status = true; 2334 } else if (opts->statusline || opts->winbar) { 2335 win_check_ns_hl(wp); 2336 if (opts->winbar) { 2337 win_redr_winbar(wp); 2338 } 2339 if (opts->statusline) { 2340 win_redr_status(wp); 2341 } 2342 win_check_ns_hl(NULL); 2343 } 2344 } 2345 2346 /// EXPERIMENTAL: this API may change in the future. 2347 /// 2348 /// Instruct Nvim to redraw various components. 2349 /// 2350 /// @see |:redraw| 2351 /// 2352 /// @param opts Optional parameters. 2353 /// - win: Target a specific |window-ID| as described below. 2354 /// - buf: Target a specific buffer number as described below. 2355 /// - flush: Update the screen with pending updates. 2356 /// - valid: When present mark `win`, `buf`, or all windows for 2357 /// redraw. When `true`, only redraw changed lines (useful for 2358 /// decoration providers). When `false`, forcefully redraw. 2359 /// - range: Redraw a range in `buf`, the buffer in `win` or the 2360 /// current buffer (useful for decoration providers). Expects a 2361 /// tuple `[first, last]` with the first and last line number 2362 /// of the range, 0-based end-exclusive |api-indexing|. 2363 /// - cursor: Immediately update cursor position on the screen in 2364 /// `win` or the current window. 2365 /// - statuscolumn: Redraw the 'statuscolumn' in `buf`, `win` or 2366 /// all windows. 2367 /// - statusline: Redraw the 'statusline' in `buf`, `win` or all 2368 /// windows. 2369 /// - winbar: Redraw the 'winbar' in `buf`, `win` or all windows. 2370 /// - tabline: Redraw the 'tabline'. 2371 void nvim__redraw(Dict(redraw) *opts, Error *err) 2372 FUNC_API_SINCE(12) 2373 { 2374 win_T *win = NULL; 2375 buf_T *buf = NULL; 2376 2377 if (HAS_KEY(opts, redraw, win)) { 2378 win = find_window_by_handle(opts->win, err); 2379 if (ERROR_SET(err)) { 2380 return; 2381 } 2382 } 2383 2384 if (HAS_KEY(opts, redraw, buf)) { 2385 VALIDATE(win == NULL, "%s", "cannot use both 'buf' and 'win'", { 2386 return; 2387 }); 2388 buf = find_buffer_by_handle(opts->buf, err); 2389 if (ERROR_SET(err)) { 2390 return; 2391 } 2392 } 2393 2394 unsigned count = (win != NULL) + (buf != NULL); 2395 VALIDATE(xpopcount(opts->is_set__redraw_) > count, "%s", "at least one action required", { 2396 return; 2397 }); 2398 2399 if (HAS_KEY(opts, redraw, valid)) { 2400 // UPD_VALID redraw type does not actually do anything on it's own. Setting 2401 // it here without scrolling or changing buffer text seems pointless but 2402 // the expectation is that this may be called by decoration providers whose 2403 // "on_win" callback may set "w_redr_top/bot". 2404 int type = opts->valid ? UPD_VALID : UPD_NOT_VALID; 2405 if (win != NULL) { 2406 redraw_later(win, type); 2407 } else if (buf != NULL) { 2408 redraw_buf_later(buf, type); 2409 } else { 2410 redraw_all_later(type); 2411 } 2412 } 2413 2414 if (HAS_KEY(opts, redraw, range)) { 2415 VALIDATE(kv_size(opts->range) == 2 2416 && kv_A(opts->range, 0).type == kObjectTypeInteger 2417 && kv_A(opts->range, 1).type == kObjectTypeInteger 2418 && kv_A(opts->range, 0).data.integer >= 0 2419 && kv_A(opts->range, 1).data.integer >= -1, 2420 "%s", "Invalid 'range': Expected 2-tuple of Integers", { 2421 return; 2422 }); 2423 int64_t begin_raw = kv_A(opts->range, 0).data.integer; 2424 int64_t end_raw = kv_A(opts->range, 1).data.integer; 2425 2426 buf_T *rbuf = win ? win->w_buffer : (buf ? buf : curbuf); 2427 linenr_T line_count = rbuf->b_ml.ml_line_count; 2428 2429 int begin = (int)MIN(begin_raw, line_count); 2430 int end; 2431 if (end_raw == -1) { 2432 end = line_count; 2433 } else { 2434 end = (int)MIN(MAX(begin, end_raw), line_count); 2435 } 2436 2437 if (begin < end) { 2438 redraw_buf_range_later(rbuf, 1 + begin, end); 2439 } 2440 } 2441 2442 // Redraw later types require update_screen() so call implicitly unless set to false. 2443 if (HAS_KEY(opts, redraw, valid) || HAS_KEY(opts, redraw, range)) { 2444 opts->flush = HAS_KEY(opts, redraw, flush) ? opts->flush : true; 2445 } 2446 2447 // When explicitly set to false and only "redraw later" types are present, 2448 // don't call ui_flush() either. 2449 bool flush_ui = opts->flush; 2450 if (opts->tabline) { 2451 // Flush later in case tabline was just hidden or shown for the first time. 2452 if (redraw_tabline && firstwin->w_lines_valid == 0) { 2453 opts->flush = true; 2454 } else { 2455 draw_tabline(); 2456 } 2457 flush_ui = true; 2458 } 2459 2460 bool save_lz = p_lz; 2461 int save_rd = RedrawingDisabled; 2462 RedrawingDisabled = 0; 2463 p_lz = false; 2464 if (opts->statuscolumn || opts->statusline || opts->winbar) { 2465 if (win == NULL) { 2466 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { 2467 if (buf == NULL || wp->w_buffer == buf) { 2468 redraw_status(wp, opts, &opts->flush); 2469 } 2470 } 2471 } else { 2472 redraw_status(win, opts, &opts->flush); 2473 } 2474 flush_ui = true; 2475 } 2476 2477 win_T *cwin = win ? win : curwin; 2478 // Allow moving cursor to recently opened window and make sure it is drawn #28868. 2479 if (opts->cursor && (!cwin->w_grid.target || !cwin->w_grid.target->valid)) { 2480 opts->flush = true; 2481 } 2482 2483 // Redraw pending screen updates when explicitly requested or when determined 2484 // that it is necessary to properly draw other requested components. 2485 if (opts->flush && !cmdpreview) { 2486 validate_cursor(curwin); 2487 update_topline(curwin); 2488 update_screen(); 2489 } 2490 2491 if (opts->cursor) { 2492 setcursor_mayforce(cwin, true); 2493 flush_ui = true; 2494 } 2495 2496 if (flush_ui) { 2497 ui_flush(); 2498 } 2499 2500 RedrawingDisabled = save_rd; 2501 p_lz = save_lz; 2502 }