robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <assert.h> 2#include <float.h> 3#include <inttypes.h> 4#include <limits.h> 5#include <math.h> 6#include <signal.h> 7#include <stdarg.h> 8#include <stddef.h> 9#include <stdio.h> 10#include <stdlib.h> 11#include <string.h> 12#include <time.h> 13#include <uv.h> 14 15#include "auto/config.h" 16#include "klib/kvec.h" 17#include "mpack/mpack_core.h" 18#include "mpack/object.h" 19#include "nvim/api/private/converter.h" 20#include "nvim/api/private/defs.h" 21#include "nvim/api/private/dispatch.h" 22#include "nvim/api/private/helpers.h" 23#include "nvim/api/vim.h" 24#include "nvim/ascii_defs.h" 25#include "nvim/assert_defs.h" 26#include "nvim/autocmd.h" 27#include "nvim/autocmd_defs.h" 28#include "nvim/buffer.h" 29#include "nvim/buffer_defs.h" 30#include "nvim/channel.h" 31#include "nvim/channel_defs.h" 32#include "nvim/charset.h" 33#include "nvim/cmdexpand.h" 34#include "nvim/cmdexpand_defs.h" 35#include "nvim/context.h" 36#include "nvim/cursor.h" 37#include "nvim/dialog.h" 38#include "nvim/errors.h" 39#include "nvim/eval/buffer.h" 40#include "nvim/eval/decode.h" 41#include "nvim/eval/encode.h" 42#include "nvim/eval/executor.h" 43#include "nvim/eval/funcs.h" 44#include "nvim/eval/typval.h" 45#include "nvim/eval/typval_defs.h" 46#include "nvim/eval/userfunc.h" 47#include "nvim/eval/vars.h" 48#include "nvim/eval/window.h" 49#include "nvim/event/defs.h" 50#include "nvim/event/loop.h" 51#include "nvim/event/multiqueue.h" 52#include "nvim/event/proc.h" 53#include "nvim/event/time.h" 54#include "nvim/ex_cmds.h" 55#include "nvim/ex_cmds_defs.h" 56#include "nvim/ex_docmd.h" 57#include "nvim/ex_eval.h" 58#include "nvim/ex_getln.h" 59#include "nvim/garray.h" 60#include "nvim/garray_defs.h" 61#include "nvim/gettext_defs.h" 62#include "nvim/globals.h" 63#include "nvim/grid.h" 64#include "nvim/grid_defs.h" 65#include "nvim/highlight_defs.h" 66#include "nvim/highlight_group.h" 67#include "nvim/indent.h" 68#include "nvim/indent_c.h" 69#include "nvim/input.h" 70#include "nvim/input_defs.h" 71#include "nvim/insert.h" 72#include "nvim/insexpand.h" 73#include "nvim/keycodes.h" 74#include "nvim/lua/executor.h" 75#include "nvim/macros_defs.h" 76#include "nvim/main.h" 77#include "nvim/mark.h" 78#include "nvim/mark_defs.h" 79#include "nvim/math.h" 80#include "nvim/mbyte.h" 81#include "nvim/mbyte_defs.h" 82#include "nvim/memline.h" 83#include "nvim/memory.h" 84#include "nvim/memory_defs.h" 85#include "nvim/menu.h" 86#include "nvim/menu_defs.h" 87#include "nvim/message.h" 88#include "nvim/move.h" 89#include "nvim/msgpack_rpc/channel.h" 90#include "nvim/msgpack_rpc/channel_defs.h" 91#include "nvim/msgpack_rpc/packer.h" 92#include "nvim/msgpack_rpc/packer_defs.h" 93#include "nvim/msgpack_rpc/server.h" 94#include "nvim/normal.h" 95#include "nvim/normal_defs.h" 96#include "nvim/ops.h" 97#include "nvim/option.h" 98#include "nvim/option_defs.h" 99#include "nvim/option_vars.h" 100#include "nvim/optionstr.h" 101#include "nvim/os/dl.h" 102#include "nvim/os/fs.h" 103#include "nvim/os/os.h" 104#include "nvim/os/os_defs.h" 105#include "nvim/os/pty_proc.h" 106#include "nvim/os/shell.h" 107#include "nvim/os/stdpaths_defs.h" 108#include "nvim/os/time.h" 109#include "nvim/path.h" 110#include "nvim/plines.h" 111#include "nvim/popupmenu.h" 112#include "nvim/pos_defs.h" 113#include "nvim/profile.h" 114#include "nvim/regexp.h" 115#include "nvim/regexp_defs.h" 116#include "nvim/register.h" 117#include "nvim/runtime.h" 118#include "nvim/runtime_defs.h" 119#include "nvim/search.h" 120#include "nvim/sha256.h" 121#include "nvim/spell.h" 122#include "nvim/spellsuggest.h" 123#include "nvim/state.h" 124#include "nvim/state_defs.h" 125#include "nvim/strings.h" 126#include "nvim/syntax.h" 127#include "nvim/tag.h" 128#include "nvim/terminal.h" 129#include "nvim/types_defs.h" 130#include "nvim/ui.h" 131#include "nvim/ui_compositor.h" 132#include "nvim/version.h" 133#include "nvim/vim_defs.h" 134#include "nvim/window.h" 135 136/// Describe data to return from find_some_match() 137typedef enum { 138kSomeMatch ,///< Data for match(). 139kSomeMatchEnd ,///< Data for matchend(). 140kSomeMatchList ,///< Data for matchlist(). 141kSomeMatchStr ,///< Data for matchstr(). 142kSomeMatchStrPos ,///< Data for matchstrpos(). 143}SomeMatchType ; 144 145#include "eval/funcs.c.generated.h" 146 147#ifdef _MSC_VER 148// This prevents MSVC from replacing the functions with intrinsics, 149// and causing errors when trying to get their addresses in funcs.generated.h 150# pragma function(ceil) 151# pragma function(floor) 152#endif 153 154PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES 155PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH 156#include "funcs.generated.h" 157 158PRAGMA_DIAG_POP 159PRAGMA_DIAG_POP 160 161static const char e_invalid_submatch_number_nr [] 162= N_ ("E935: Invalid submatch number: %d" ); 163static const char e_string_list_or_blob_required [] 164= N_ ("E1098: String, List or Blob required" ); 165static const char e_missing_function_argument [] 166= N_ ("E1132: Missing function argument" ); 167 168/// Dummy va_list for passing to vim_snprintf 169/// 170/// Used because: 171/// - passing a NULL pointer doesn't work when va_list isn't a pointer 172/// - locally in the function results in a "used before set" warning 173/// - using va_start() to initialize it gives "function with fixed args" error 174static va_list dummy_ap ; 175 176/// Function given to ExpandGeneric() to obtain the list of internal 177/// or user defined function names. 178char * get_function_name (expand_T * xp ,int idx ) 179{ 180static int intidx = -1 ; 181 182if (idx == 0 ) { 183intidx = -1 ; 184 } 185if (intidx < 0 ) { 186char * name = get_user_func_name (xp ,idx ); 187if (name != NULL ) { 188if (* name != NUL && * name != '<' 189&& strncmp ("g:" ,xp -> xp_pattern ,2 )== 0 ) { 190return cat_prefix_varname ('g' ,name ); 191 } 192return name ; 193 } 194 } 195 196const char * const key = functions [++ intidx ].name ; 197if (!key ) { 198return NULL ; 199 } 200const size_t key_len = strlen (key ); 201memcpy (IObuff ,key ,key_len ); 202IObuff [key_len ]= '(' ; 203if (functions [intidx ].max_argc == 0 ) { 204IObuff [key_len + 1 ]= ')' ; 205IObuff [key_len + 2 ]= NUL ; 206 }else { 207IObuff [key_len + 1 ]= NUL ; 208 } 209return IObuff ; 210} 211 212/// Function given to ExpandGeneric() to obtain the list of internal or 213/// user defined variable or function names. 214char * get_expr_name (expand_T * xp ,int idx ) 215{ 216static int intidx = -1 ; 217 218if (idx == 0 ) { 219intidx = -1 ; 220 } 221if (intidx < 0 ) { 222char * name = get_function_name (xp ,idx ); 223if (name != NULL ) { 224return name ; 225 } 226 } 227return get_user_var_name (xp ,++ intidx ); 228} 229 230/// Gets a builtin (aka "vimfn", "eval") function from the generated hash table. 231/// 232/// @param[in] name Name of the function. 233/// 234/// @return pointer to the function definition or NULL if not found. 235const EvalFuncDef * find_internal_func (const char * const name ) 236FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL 237{ 238size_t len = strlen (name ); 239int index = find_internal_func_hash (name ,len ); 240return index >=0 ?& functions [index ] :NULL ; 241} 242 243/// Gets the Lua name of a Lua-implemented "vimfn" function, or NULL if not found. 244const char * find_internal_func_lua (const char * const name ) 245FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL 246{ 247const EvalFuncDef * const fdef = find_internal_func (name ); 248if (fdef && fdef -> func == & lua_wrapper ) { 249return fdef -> data .func_lua ; 250 } 251return NULL ; 252} 253 254/// Check the argument count to use for internal function "fdef". 255/// @return -1 for failure, 0 if no method base accepted, 1 if method base is 256/// first argument, 2 if method base is second argument, etc. 257int check_internal_func (const EvalFuncDef * const fdef ,const int argcount ) 258FUNC_ATTR_NONNULL_ALL 259{ 260int res ; 261 262if (argcount < fdef -> min_argc ) { 263res = FCERR_TOOFEW ; 264 }else if (argcount > fdef -> max_argc ) { 265res = FCERR_TOOMANY ; 266 }else { 267return fdef -> base_arg ; 268 } 269 270const char * const name = fdef -> name ; 271if (res == FCERR_TOOMANY ) { 272semsg (_ (e_toomanyarg ),name ); 273 }else { 274semsg (_ (e_toofewarg ),name ); 275 } 276return -1 ; 277} 278 279int call_internal_func (const char * const fname ,const int argcount ,typval_T * const argvars , 280typval_T * const rettv ) 281FUNC_ATTR_NONNULL_ALL 282{ 283const EvalFuncDef * const fdef = find_internal_func (fname ); 284if (fdef == NULL ) { 285return FCERR_UNKNOWN ; 286 }else if (argcount < fdef -> min_argc ) { 287return FCERR_TOOFEW ; 288 }else if (argcount > fdef -> max_argc ) { 289return FCERR_TOOMANY ; 290 } 291argvars [argcount ].v_type = VAR_UNKNOWN ; 292fdef -> func (argvars ,rettv ,fdef -> data ); 293return FCERR_NONE ; 294} 295 296/// Invoke a method for base->method(). 297int call_internal_method (const char * const fname ,const int argcount ,typval_T * const argvars , 298typval_T * const rettv ,typval_T * const basetv ) 299FUNC_ATTR_NONNULL_ALL 300{ 301const EvalFuncDef * const fdef = find_internal_func (fname ); 302if (fdef == NULL ) { 303return FCERR_UNKNOWN ; 304 }else if (fdef -> base_arg == BASE_NONE ) { 305return FCERR_NOTMETHOD ; 306 }else if (argcount + 1 < fdef -> min_argc ) { 307return FCERR_TOOFEW ; 308 }else if (argcount + 1 > fdef -> max_argc ) { 309return FCERR_TOOMANY ; 310 } 311 312typval_T argv [MAX_FUNC_ARGS + 1 ]; 313const ptrdiff_t base_index = fdef -> base_arg == BASE_LAST ?argcount :fdef -> base_arg - 1 ; 314if (argcount < base_index ) { 315return FCERR_TOOFEW ; 316 } 317memcpy (argv ,argvars , (size_t )base_index * sizeof (typval_T )); 318argv [base_index ]= * basetv ; 319memcpy (argv + base_index + 1 ,argvars + base_index , 320 (size_t )(argcount - base_index )* sizeof (typval_T )); 321argv [argcount + 1 ].v_type = VAR_UNKNOWN ; 322 323fdef -> func (argv ,rettv ,fdef -> data ); 324return FCERR_NONE ; 325} 326 327/// @return true for a non-zero Number and a non-empty String. 328static bool non_zero_arg (typval_T * argvars ) 329{ 330return ((argvars [0 ].v_type == VAR_NUMBER 331&& argvars [0 ].vval .v_number != 0 ) 332|| (argvars [0 ].v_type == VAR_BOOL 333&& argvars [0 ].vval .v_bool == kBoolVarTrue ) 334|| (argvars [0 ].v_type == VAR_STRING 335&& argvars [0 ].vval .v_string != NULL 336&& * argvars [0 ].vval .v_string != NUL )); 337} 338 339/// Apply a floating point C function on a typval with one float_T (`func_float` in eval.lua). 340/// 341/// Some versions of glibc on i386 have an optimization that makes it harder to 342/// call math functions indirectly from inside an inlined function, causing 343/// compile-time errors. Avoid `inline` in that case. #3072 344static void float_op_wrapper (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 345{ 346float_T f ; 347 348rettv -> v_type = VAR_FLOAT ; 349if (tv_get_float_chk (argvars ,& f )) { 350rettv -> vval .v_float = fptr .func_float (f ); 351 }else { 352rettv -> vval .v_float = 0.0 ; 353 } 354} 355 356/// Invokes an API (nvim_) function from Vimscript. 357/// 358/// Converts `argvars` to API Objects, calls the API handler, and converts the result back. 359/// Used by `gen_eval.lua` for `eval=true` API functions. 360static void api_wrapper (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 361{ 362if (check_secure ()) { 363return ; 364 } 365 366MsgpackRpcRequestHandler handler = * fptr .func_api ; 367 368MAXSIZE_TEMP_ARRAY (args ,MAX_FUNC_ARGS ); 369Arena arena = ARENA_EMPTY ; 370 371for (typval_T * tv = argvars ;tv -> v_type != VAR_UNKNOWN ;tv ++ ) { 372ADD_C (args ,vim_to_object (tv ,& arena , false)); 373 } 374 375Error err = ERROR_INIT ; 376Object result = handler .fn (VIML_INTERNAL_CALL ,args ,& arena ,& err ); 377 378if (ERROR_SET (& err )) { 379semsg_multiline ("emsg" ,e_api_error ,err .msg ); 380 gotoend ; 381 } 382 383object_to_vim_take_luaref (& result ,rettv , true,& err ); 384 385end : 386if (handler .ret_alloc ) { 387api_free_object (result ); 388 } 389arena_mem_free (arena_finish (& arena )); 390api_clear_error (& err ); 391} 392 393/// Invokes a Lua-implemented vimfn/"f_xx" function from Vimscript (`func_lua` in eval.lua). 394/// 395/// - Converts argvars to API Objects, calls the Lua function, converts the result back. 396/// - NOT used when called from Lua; `nlua_call()` calls the function directly. 397static void lua_wrapper (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 398{ 399MAXSIZE_TEMP_ARRAY (args ,MAX_FUNC_ARGS ); 400Arena arena = ARENA_EMPTY ; 401 402for (typval_T * tv = argvars ;tv -> v_type != VAR_UNKNOWN ;tv ++ ) { 403ADD_C (args ,vim_to_object (tv ,& arena , false)); 404 } 405 406char buf [256 ]; 407snprintf (buf ,sizeof (buf ),"return require('vim._core.vimfn').%s(...)" ,fptr .func_lua ); 408 409Error err = ERROR_INIT ; 410Object result = nlua_exec (cstr_as_string (buf ),NULL ,args ,kRetObject ,& arena ,& err ); 411 412if (ERROR_SET (& err )) { 413semsg_multiline ("emsg" ,e_api_error ,err .msg ); 414 gotoend ; 415 } 416 417object_to_vim_take_luaref (& result ,rettv , true,& err ); 418 419end : 420arena_mem_free (arena_finish (& arena )); 421api_clear_error (& err ); 422} 423 424/// "abs(expr)" function 425static void f_abs (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 426{ 427if (argvars [0 ].v_type == VAR_FLOAT ) { 428float_op_wrapper (argvars ,rettv , (EvalFuncData ){ .func_float = & fabs }); 429 }else { 430bool error = false; 431 432varnumber_T n = tv_get_number_chk (& argvars [0 ],& error ); 433if (error ) { 434rettv -> vval .v_number = -1 ; 435 }else if (n > 0 ) { 436rettv -> vval .v_number = n ; 437 }else { 438rettv -> vval .v_number = - n ; 439 } 440 } 441} 442 443/// "and(expr, expr)" function 444static void f_and (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 445{ 446rettv -> vval .v_number = tv_get_number_chk (& argvars [0 ],NULL ) 447& tv_get_number_chk (& argvars [1 ],NULL ); 448} 449 450/// "api_info()" function 451static void f_api_info (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 452{ 453object_to_vim (api_metadata (),rettv ,NULL ); 454} 455 456/// "atan2()" function 457static void f_atan2 (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 458{ 459float_T fx ; 460float_T fy ; 461 462rettv -> v_type = VAR_FLOAT ; 463if (tv_get_float_chk (argvars ,& fx )&& tv_get_float_chk (& argvars [1 ],& fy )) { 464rettv -> vval .v_float = atan2 (fx ,fy ); 465 }else { 466rettv -> vval .v_float = 0.0 ; 467 } 468} 469 470/// Get buffer by number or pattern. 471buf_T * tv_get_buf (typval_T * tv ,int curtab_only ) 472{ 473if (tv -> v_type == VAR_NUMBER ) { 474return buflist_findnr ((int )tv -> vval .v_number ); 475 } 476if (tv -> v_type != VAR_STRING ) { 477return NULL ; 478 } 479 480char * name = tv -> vval .v_string ; 481 482if (name == NULL || * name == NUL ) { 483return curbuf ; 484 } 485if (name [0 ]== '$' && name [1 ]== NUL ) { 486return lastbuf ; 487 } 488 489// Ignore 'magic' and 'cpoptions' here to make scripts portable 490int save_magic = p_magic ; 491p_magic = true; 492char * save_cpo = p_cpo ; 493p_cpo = empty_string_option ; 494 495buf_T * buf = buflist_findnr (buflist_findpat (name ,name + strlen (name ), 496 true, false,curtab_only )); 497 498p_magic = save_magic ; 499p_cpo = save_cpo ; 500 501// If not found, try expanding the name, like done for bufexists(). 502if (buf == NULL ) { 503buf = find_buffer (tv ); 504 } 505 506return buf ; 507} 508 509/// Like tv_get_buf() but give an error message if the type is wrong. 510buf_T * tv_get_buf_from_arg (typval_T * const tv )FUNC_ATTR_NONNULL_ALL 511{ 512if (!tv_check_str_or_nr (tv )) { 513return NULL ; 514 } 515emsg_off ++ ; 516buf_T * const buf = tv_get_buf (tv , false); 517emsg_off -- ; 518return buf ; 519} 520 521/// Get the buffer from "arg" and give an error and return NULL if it is not 522/// valid. 523buf_T * get_buf_arg (typval_T * arg ) 524{ 525emsg_off ++ ; 526buf_T * buf = tv_get_buf (arg , false); 527emsg_off -- ; 528if (buf == NULL ) { 529semsg (_ ("E158: Invalid buffer name: %s" ),tv_get_string (arg )); 530 } 531return buf ; 532} 533 534/// "byte2line(byte)" function 535static void f_byte2line (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 536{ 537int boff = (int )tv_get_number (& argvars [0 ])- 1 ; 538if (boff < 0 ) { 539rettv -> vval .v_number = -1 ; 540 }else { 541rettv -> vval .v_number = (varnumber_T )ml_find_line_or_offset (curbuf ,0 , 542& boff , false); 543 } 544} 545 546/// "call(func, arglist [, dict])" function 547static void f_call (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 548{ 549if (tv_check_for_list_arg (argvars ,1 )== FAIL ) { 550return ; 551 } 552if (argvars [1 ].vval .v_list == NULL ) { 553return ; 554 } 555 556bool owned = false; 557char * func ; 558partial_T * partial = NULL ; 559if (argvars [0 ].v_type == VAR_FUNC ) { 560func = argvars [0 ].vval .v_string ; 561 }else if (argvars [0 ].v_type == VAR_PARTIAL ) { 562partial = argvars [0 ].vval .v_partial ; 563func = partial_name (partial ); 564 }else if (nlua_is_table_from_lua (& argvars [0 ])) { 565// TODO(tjdevries): UnifiedCallback 566func = nlua_register_table_as_callable (& argvars [0 ]); 567owned = true; 568 }else { 569func = (char * )tv_get_string (& argvars [0 ]); 570 } 571 572if (func == NULL || * func == NUL ) { 573return ;// type error, empty name or null function 574 } 575char * tofree = NULL ; 576if (argvars [0 ].v_type == VAR_STRING ) { 577char * p = func ; 578tofree = trans_function_name (& p , false,TFN_INT |TFN_QUIET ,NULL ,NULL ); 579if (tofree == NULL ) { 580emsg_funcname (e_unknown_function_str ,func ); 581return ; 582 } 583func = tofree ; 584 } 585 586dict_T * selfdict = NULL ; 587if (argvars [2 ].v_type != VAR_UNKNOWN ) { 588if (tv_check_for_dict_arg (argvars ,2 )== FAIL ) { 589 gotodone ; 590 } 591selfdict = argvars [2 ].vval .v_dict ; 592 } 593 594func_call (func ,& argvars [1 ],partial ,selfdict ,rettv ); 595 596done : 597if (owned ) { 598func_unref (func ); 599 } 600xfree (tofree ); 601} 602 603/// "changenr()" function 604static void f_changenr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 605{ 606rettv -> vval .v_number = curbuf -> b_u_seq_cur ; 607} 608 609/// "chanclose(id[, stream])" function 610static void f_chanclose (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 611{ 612rettv -> v_type = VAR_NUMBER ; 613rettv -> vval .v_number = 0 ; 614 615if (check_secure ()) { 616return ; 617 } 618 619if (argvars [0 ].v_type != VAR_NUMBER || (argvars [1 ].v_type != VAR_STRING 620&& argvars [1 ].v_type != VAR_UNKNOWN )) { 621emsg (_ (e_invarg )); 622return ; 623 } 624 625ChannelPart part = kChannelPartAll ; 626if (argvars [1 ].v_type == VAR_STRING ) { 627const char * stream = tv_get_string (& argvars [1 ]); 628if (!strcmp (stream ,"stdin" )) { 629part = kChannelPartStdin ; 630 }else if (!strcmp (stream ,"stdout" )) { 631part = kChannelPartStdout ; 632 }else if (!strcmp (stream ,"stderr" )) { 633part = kChannelPartStderr ; 634 }else if (!strcmp (stream ,"rpc" )) { 635part = kChannelPartRpc ; 636 }else { 637semsg (_ ("Invalid channel stream \"%s\"" ),stream ); 638return ; 639 } 640 } 641const char * error ; 642rettv -> vval .v_number = channel_close ((uint64_t )argvars [0 ].vval .v_number ,part ,& error ); 643if (!rettv -> vval .v_number ) { 644emsg (error ); 645 } 646} 647 648/// "chansend(id, data)" function 649static void f_chansend (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 650{ 651rettv -> v_type = VAR_NUMBER ; 652rettv -> vval .v_number = 0 ; 653 654if (check_secure ()) { 655return ; 656 } 657 658if (argvars [0 ].v_type != VAR_NUMBER || argvars [1 ].v_type == VAR_UNKNOWN ) { 659// First argument is the channel id and second is the data to write 660emsg (_ (e_invarg )); 661return ; 662 } 663 664ptrdiff_t input_len = 0 ; 665char * input = NULL ; 666uint64_t id = (uint64_t )argvars [0 ].vval .v_number ; 667#ifdef UNIX 668bool crlf = false; 669#else 670Channel * chan = find_channel (id ); 671bool crlf = (chan != NULL && chan -> term ) ? true : false; 672#endif 673 674if (argvars [1 ].v_type == VAR_BLOB ) { 675const blob_T * const b = argvars [1 ].vval .v_blob ; 676input_len = tv_blob_len (b ); 677if (input_len > 0 ) { 678input = xmemdup (b -> bv_ga .ga_data , (size_t )input_len ); 679 } 680 }else { 681input = save_tv_as_string (& argvars [1 ],& input_len , false,crlf ); 682 } 683 684if (!input ) { 685// Either the error has been handled by save_tv_as_string(), 686// or there is no input to send. 687return ; 688 } 689const char * error = NULL ; 690rettv -> vval .v_number = (varnumber_T )channel_send (id ,input , (size_t )input_len , true,& error ); 691if (error ) { 692emsg (error ); 693 } 694} 695 696/// "char2nr(string)" function 697static void f_char2nr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 698{ 699if (argvars [1 ].v_type != VAR_UNKNOWN ) { 700if (!tv_check_num (& argvars [1 ])) { 701return ; 702 } 703 } 704 705rettv -> vval .v_number = utf_ptr2char (tv_get_string (& argvars [0 ])); 706} 707 708/// Get the current cursor column and store it in 'rettv'. 709/// 710/// @return the character index of the column if 'charcol' is true, 711/// otherwise the byte index of the column. 712static void get_col (typval_T * argvars ,typval_T * rettv ,bool charcol ) 713{ 714if (tv_check_for_string_or_list_arg (argvars ,0 )== FAIL 715|| tv_check_for_opt_number_arg (argvars ,1 )== FAIL ) { 716return ; 717 } 718 719win_T * wp = curwin ; 720 721if (argvars [1 ].v_type != VAR_UNKNOWN ) { 722// use the window specified in the second argument 723tabpage_T * tp ; 724wp = win_id2wp_tp ((int )tv_get_number (& argvars [1 ]),& tp ); 725if (wp == NULL || tp == NULL ) { 726return ; 727 } 728check_cursor (wp ); 729 } 730 731buf_T * bp = wp -> w_buffer ; 732colnr_T col = 0 ; 733int fnum = bp -> b_fnum ; 734pos_T * fp = var2fpos (& argvars [0 ], false,& fnum ,charcol ,wp ); 735if (fp != NULL && fnum == bp -> b_fnum ) { 736if (fp -> col == MAXCOL ) { 737// '> can be MAXCOL, get the length of the line then 738if (fp -> lnum <=bp -> b_ml .ml_line_count ) { 739col = ml_get_buf_len (bp ,fp -> lnum )+ 1 ; 740 }else { 741col = MAXCOL ; 742 } 743 }else { 744col = fp -> col + 1 ; 745// col(".") when the cursor is on the NUL at the end of the line 746// because of "coladd" can be seen as an extra column. 747if (virtual_active (wp )&& fp == & wp -> w_cursor ) { 748char * p = ml_get_buf (bp ,wp -> w_cursor .lnum )+ wp -> w_cursor .col ; 749if (wp -> w_cursor .coladd >= 750 (colnr_T )win_chartabsize (wp ,p , 751wp -> w_virtcol - wp -> w_cursor .coladd )) { 752int l ; 753if (* p != NUL && p [(l = utfc_ptr2len (p ))]== NUL ) { 754col += l ; 755 } 756 } 757 } 758 } 759 } 760rettv -> vval .v_number = col ; 761} 762 763/// "charcol()" function 764static void f_charcol (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 765{ 766get_col (argvars ,rettv , true); 767} 768 769win_T * get_optional_window (typval_T * argvars ,int idx ) 770{ 771if (argvars [idx ].v_type == VAR_UNKNOWN ) { 772return curwin ; 773 } 774 775win_T * win = find_win_by_nr_or_id (& argvars [idx ]); 776if (win == NULL ) { 777emsg (_ (e_invalwindow )); 778return NULL ; 779 } 780return win ; 781} 782 783/// "col(string)" function 784static void f_col (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 785{ 786get_col (argvars ,rettv , false); 787} 788 789/// "confirm(message, buttons[, default [, type]])" function 790static void f_confirm (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 791{ 792char buf [NUMBUFLEN ]; 793char buf2 [NUMBUFLEN ]; 794const char * buttons = NULL ; 795int def = 1 ; 796int type = VIM_GENERIC ; 797bool error = false; 798 799const char * message = tv_get_string_chk (& argvars [0 ]); 800if (message == NULL ) { 801error = true; 802 } 803if (argvars [1 ].v_type != VAR_UNKNOWN ) { 804buttons = tv_get_string_buf_chk (& argvars [1 ],buf ); 805if (buttons == NULL ) { 806error = true; 807 } 808if (argvars [2 ].v_type != VAR_UNKNOWN ) { 809def = (int )tv_get_number_chk (& argvars [2 ],& error ); 810if (argvars [3 ].v_type != VAR_UNKNOWN ) { 811const char * typestr = tv_get_string_buf_chk (& argvars [3 ],buf2 ); 812if (typestr == NULL ) { 813error = true; 814 }else { 815switch (TOUPPER_ASC (* typestr )) { 816case 'E' : 817type = VIM_ERROR ;break ; 818case 'Q' : 819type = VIM_QUESTION ;break ; 820case 'I' : 821type = VIM_INFO ;break ; 822case 'W' : 823type = VIM_WARNING ;break ; 824case 'G' : 825type = VIM_GENERIC ;break ; 826 } 827 } 828 } 829 } 830 } 831 832if (buttons == NULL || * buttons == NUL ) { 833buttons = _ ("&Ok" ); 834 } 835 836if (!error ) { 837rettv -> vval .v_number = do_dialog (type ,NULL ,message ,buttons ,def ,NULL , false); 838 } 839} 840 841/// "copy()" function 842static void f_copy (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 843{ 844var_item_copy (NULL ,& argvars [0 ],rettv , false,0 ); 845} 846 847/// Set the cursor position. 848/// If "charcol" is true, then use the column number as a character offset. 849/// Otherwise use the column number as a byte offset. 850static void set_cursorpos (typval_T * argvars ,typval_T * rettv ,bool charcol ) 851{ 852linenr_T lnum ; 853colnr_T col ; 854colnr_T coladd = 0 ; 855bool set_curswant = true; 856 857rettv -> vval .v_number = -1 ; 858if (argvars [0 ].v_type == VAR_LIST ) { 859pos_T pos ; 860colnr_T curswant = -1 ; 861 862if (list2fpos (argvars ,& pos ,NULL ,& curswant ,charcol )== FAIL ) { 863emsg (_ (e_invarg )); 864return ; 865 } 866 867lnum = pos .lnum ; 868col = pos .col ; 869coladd = pos .coladd ; 870if (curswant >=0 ) { 871curwin -> w_curswant = curswant - 1 ; 872set_curswant = false; 873 } 874 }else if ((argvars [0 ].v_type == VAR_NUMBER || argvars [0 ].v_type == VAR_STRING ) 875&& (argvars [1 ].v_type == VAR_NUMBER || argvars [1 ].v_type == VAR_STRING )) { 876lnum = tv_get_lnum (argvars ); 877if (lnum < 0 ) { 878semsg (_ (e_invarg2 ),tv_get_string (& argvars [0 ])); 879 }else if (lnum == 0 ) { 880lnum = curwin -> w_cursor .lnum ; 881 } 882col = (colnr_T )tv_get_number_chk (& argvars [1 ],NULL ); 883if (charcol ) { 884col = buf_charidx_to_byteidx (curbuf ,lnum , (int )col )+ 1 ; 885 } 886if (argvars [2 ].v_type != VAR_UNKNOWN ) { 887coladd = (colnr_T )tv_get_number_chk (& argvars [2 ],NULL ); 888 } 889 }else { 890emsg (_ (e_invarg )); 891return ; 892 } 893if (lnum < 0 || col < 0 || coladd < 0 ) { 894return ;// type error; errmsg already given 895 } 896if (lnum > 0 ) { 897curwin -> w_cursor .lnum = lnum ; 898 } 899if (col != MAXCOL && -- col < 0 ) { 900col = 0 ; 901 } 902curwin -> w_cursor .col = col ; 903curwin -> w_cursor .coladd = coladd ; 904 905// Make sure the cursor is in a valid position. 906check_cursor (curwin ); 907// Correct cursor for multi-byte character. 908mb_adjust_cursor (); 909 910curwin -> w_set_curswant = set_curswant ; 911rettv -> vval .v_number = 0 ; 912} 913 914/// "cursor(lnum, col)" function, or 915/// "cursor(list)" 916/// 917/// Moves the cursor to the specified line and column. 918/// 919/// @return 0 when the position could be set, -1 otherwise. 920static void f_cursor (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 921{ 922set_cursorpos (argvars ,rettv , false); 923} 924 925/// "debugbreak()" function 926static void f_debugbreak (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 927{ 928rettv -> vval .v_number = FAIL ; 929int pid = (int )tv_get_number (& argvars [0 ]); 930if (pid == 0 ) { 931emsg (_ (e_invarg )); 932return ; 933 } 934 935#ifdef MSWIN 936HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS ,0 ,pid ); 937if (hProcess == NULL ) { 938return ; 939 } 940 941DebugBreakProcess (hProcess ); 942CloseHandle (hProcess ); 943rettv -> vval .v_number = OK ; 944#else 945uv_kill (pid ,SIGINT ); 946#endif 947} 948 949/// "deepcopy()" function 950static void f_deepcopy (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 951{ 952if (tv_check_for_opt_bool_arg (argvars ,1 )== FAIL ) { 953return ; 954 } 955 956varnumber_T noref = 0 ; 957if (argvars [1 ].v_type != VAR_UNKNOWN ) { 958noref = tv_get_bool_chk (& argvars [1 ],NULL ); 959 } 960 961var_item_copy (NULL ,& argvars [0 ],rettv , true, (noref == 0 ?get_copyID () :0 )); 962} 963 964/// dictwatcheradd(dict, key, funcref) function 965static void f_dictwatcheradd (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 966{ 967if (check_secure ()) { 968return ; 969 } 970 971if (argvars [0 ].v_type != VAR_DICT ) { 972semsg (_ (e_invarg2 ),"dict" ); 973return ; 974 }else if (argvars [0 ].vval .v_dict == NULL ) { 975const char * const arg_errmsg = _ ("dictwatcheradd() argument" ); 976const size_t arg_errmsg_len = strlen (arg_errmsg ); 977semsg (_ (e_cannot_change_readonly_variable_str ), (int )arg_errmsg_len ,arg_errmsg ); 978return ; 979 } 980 981if (argvars [1 ].v_type != VAR_STRING && argvars [1 ].v_type != VAR_NUMBER ) { 982semsg (_ (e_invarg2 ),"key" ); 983return ; 984 } 985 986const char * const key_pattern = tv_get_string_chk (argvars + 1 ); 987if (key_pattern == NULL ) { 988return ; 989 } 990const size_t key_pattern_len = strlen (key_pattern ); 991 992Callback callback ; 993if (!callback_from_typval (& callback ,& argvars [2 ])) { 994semsg (_ (e_invarg2 ),"funcref" ); 995return ; 996 } 997 998tv_dict_watcher_add (argvars [0 ].vval .v_dict ,key_pattern ,key_pattern_len , 999callback ); 1000} 1001 1002/// dictwatcherdel(dict, key, funcref) function 1003static void f_dictwatcherdel (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1004{ 1005if (check_secure ()) { 1006return ; 1007 } 1008 1009if (argvars [0 ].v_type != VAR_DICT ) { 1010semsg (_ (e_invarg2 ),"dict" ); 1011return ; 1012 } 1013 1014if (argvars [2 ].v_type != VAR_FUNC && argvars [2 ].v_type != VAR_STRING ) { 1015semsg (_ (e_invarg2 ),"funcref" ); 1016return ; 1017 } 1018 1019const char * const key_pattern = tv_get_string_chk (argvars + 1 ); 1020if (key_pattern == NULL ) { 1021return ; 1022 } 1023 1024Callback callback ; 1025if (!callback_from_typval (& callback ,& argvars [2 ])) { 1026return ; 1027 } 1028 1029if (!tv_dict_watcher_remove (argvars [0 ].vval .v_dict ,key_pattern , 1030strlen (key_pattern ),callback )) { 1031emsg ("Couldn't find a watcher matching key and callback" ); 1032 } 1033 1034callback_free (& callback ); 1035} 1036 1037/// "did_filetype()" function 1038static void f_did_filetype (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1039{ 1040rettv -> vval .v_number = curbuf -> b_did_filetype ; 1041} 1042 1043/// "empty({expr})" function 1044static void f_empty (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1045{ 1046bool n = true; 1047 1048switch (argvars [0 ].v_type ) { 1049case VAR_STRING : 1050case VAR_FUNC : 1051n = argvars [0 ].vval .v_string == NULL 1052|| * argvars [0 ].vval .v_string == NUL ; 1053break ; 1054case VAR_PARTIAL : 1055n = argvars [0 ].vval .v_partial == NULL ; 1056break ; 1057case VAR_NUMBER : 1058n = argvars [0 ].vval .v_number == 0 ; 1059break ; 1060case VAR_FLOAT : 1061n = argvars [0 ].vval .v_float == 0.0 ; 1062break ; 1063case VAR_LIST : 1064n = (tv_list_len (argvars [0 ].vval .v_list )== 0 ); 1065break ; 1066case VAR_DICT : 1067n = (tv_dict_len (argvars [0 ].vval .v_dict )== 0 ); 1068break ; 1069case VAR_BOOL : 1070switch (argvars [0 ].vval .v_bool ) { 1071case kBoolVarTrue : 1072n = false; 1073break ; 1074case kBoolVarFalse : 1075n = true; 1076break ; 1077 } 1078break ; 1079case VAR_SPECIAL : 1080n = argvars [0 ].vval .v_special == kSpecialVarNull ; 1081break ; 1082case VAR_BLOB : 1083n = (tv_blob_len (argvars [0 ].vval .v_blob )== 0 ); 1084break ; 1085case VAR_UNKNOWN : 1086internal_error ("f_empty(UNKNOWN)" ); 1087break ; 1088 } 1089 1090rettv -> vval .v_number = n ; 1091} 1092 1093/// "escape({string}, {chars})" function 1094static void f_escape (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1095{ 1096char buf [NUMBUFLEN ]; 1097 1098rettv -> vval .v_string = vim_strsave_escaped (tv_get_string (& argvars [0 ]), 1099tv_get_string_buf (& argvars [1 ],buf )); 1100rettv -> v_type = VAR_STRING ; 1101} 1102 1103/// "getenv()" function 1104static void f_getenv (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1105{ 1106char * p = vim_getenv (tv_get_string (& argvars [0 ])); 1107 1108if (p == NULL ) { 1109rettv -> v_type = VAR_SPECIAL ; 1110rettv -> vval .v_special = kSpecialVarNull ; 1111return ; 1112 } 1113rettv -> vval .v_string = p ; 1114rettv -> v_type = VAR_STRING ; 1115} 1116 1117/// "eval()" function 1118static void f_eval (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1119{ 1120const char * s = tv_get_string_chk (& argvars [0 ]); 1121if (s != NULL ) { 1122s = skipwhite (s ); 1123 } 1124 1125const char * const expr_start = s ; 1126if (s == NULL || eval1 ((char ** )& s ,rettv ,& EVALARG_EVALUATE )== FAIL ) { 1127if (expr_start != NULL && !aborting ()) { 1128semsg (_ (e_invexpr2 ),expr_start ); 1129 } 1130need_clr_eos = false; 1131rettv -> v_type = VAR_NUMBER ; 1132rettv -> vval .v_number = 0 ; 1133 }else if (* s != NUL ) { 1134semsg (_ (e_trailing_arg ),s ); 1135 } 1136} 1137 1138/// "eventhandler()" function 1139static void f_eventhandler (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1140{ 1141rettv -> vval .v_number = vgetc_busy ; 1142} 1143 1144typedef struct { 1145const list_T * const l ; 1146const listitem_T * li ; 1147}GetListLineCookie ; 1148 1149char * get_list_line (int c ,void * cookie ,int indent ,bool do_concat ) 1150{ 1151GetListLineCookie * const p = (GetListLineCookie * )cookie ; 1152 1153const listitem_T * const item = p -> li ; 1154if (item == NULL ) { 1155return NULL ; 1156 } 1157char buf [NUMBUFLEN ]; 1158const char * const s = tv_get_string_buf_chk (TV_LIST_ITEM_TV (item ),buf ); 1159p -> li = TV_LIST_ITEM_NEXT (p -> l ,item ); 1160return s == NULL ?NULL :xstrdup (s ); 1161} 1162 1163void execute_common (typval_T * argvars ,typval_T * rettv ,int arg_off ) 1164{ 1165const int save_msg_silent = msg_silent ; 1166const int save_emsg_silent = emsg_silent ; 1167const bool save_emsg_noredir = emsg_noredir ; 1168const bool save_redir_off = redir_off ; 1169garray_T * const save_capture_ga = capture_ga ; 1170const int save_msg_col = msg_col ; 1171bool echo_output = false; 1172 1173if (check_secure ()) { 1174return ; 1175 } 1176 1177if (argvars [arg_off + 1 ].v_type != VAR_UNKNOWN ) { 1178char buf [NUMBUFLEN ]; 1179const char * const s = tv_get_string_buf_chk (& argvars [arg_off + 1 ],buf ); 1180 1181if (s == NULL ) { 1182return ; 1183 } 1184if (* s == NUL ) { 1185echo_output = true; 1186 } 1187if (strncmp (s ,"silent" ,6 )== 0 ) { 1188msg_silent ++ ; 1189 } 1190if (strcmp (s ,"silent!" )== 0 ) { 1191emsg_silent = true; 1192emsg_noredir = true; 1193 } 1194 }else { 1195msg_silent ++ ; 1196 } 1197 1198garray_T capture_local ; 1199ga_init (& capture_local , (int )sizeof (char ),80 ); 1200capture_ga = & capture_local ; 1201redir_off = false; 1202if (!echo_output ) { 1203msg_col = 0 ;// prevent leading spaces 1204 } 1205 1206if (argvars [arg_off ].v_type != VAR_LIST ) { 1207do_cmdline_cmd (tv_get_string (& argvars [arg_off ])); 1208 }else if (argvars [arg_off ].vval .v_list != NULL ) { 1209list_T * const list = argvars [arg_off ].vval .v_list ; 1210tv_list_ref (list ); 1211GetListLineCookie cookie = { 1212 .l = list , 1213 .li = tv_list_first (list ), 1214 }; 1215do_cmdline (NULL ,get_list_line , (void * )& cookie , 1216DOCMD_NOWAIT |DOCMD_VERBOSE |DOCMD_REPEAT |DOCMD_KEYTYPED ); 1217tv_list_unref (list ); 1218 } 1219msg_silent = save_msg_silent ; 1220emsg_silent = save_emsg_silent ; 1221emsg_noredir = save_emsg_noredir ; 1222redir_off = save_redir_off ; 1223// "silent reg" or "silent echo x" leaves msg_col somewhere in the line. 1224if (echo_output ) { 1225// When not working silently: put it in column zero. A following 1226// "echon" will overwrite the message, unavoidably. 1227msg_col = 0 ; 1228 }else { 1229// When working silently: Put it back where it was, since nothing 1230// should have been written. 1231msg_col = save_msg_col ; 1232 } 1233 1234ga_append (capture_ga ,NUL ); 1235rettv -> v_type = VAR_STRING ; 1236rettv -> vval .v_string = capture_ga -> ga_data ; 1237 1238capture_ga = save_capture_ga ; 1239} 1240 1241/// "execute(command)" function 1242static void f_execute (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1243{ 1244execute_common (argvars ,rettv ,0 ); 1245} 1246 1247/// "exists()" function 1248static void f_exists (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1249{ 1250int n = false; 1251 1252const char * p = tv_get_string (& argvars [0 ]); 1253if (* p == '$' ) {// Environment variable. 1254// First try "normal" environment variables (fast). 1255if (os_env_exists (p + 1 , false)) { 1256n = true; 1257 }else { 1258// Try expanding things like $VIM and ${HOME}. 1259char * const exp = expand_env_save ((char * )p ); 1260if (exp != NULL && * exp != '$' ) { 1261n = true; 1262 } 1263xfree (exp ); 1264 } 1265 }else if (* p == '&' || * p == '+' ) {// Option. 1266n = (eval_option (& p ,NULL , true)== OK ); 1267if (* skipwhite (p )!= NUL ) { 1268n = false;// Trailing garbage. 1269 } 1270 }else if (* p == '*' ) {// Internal or user defined function. 1271n = strnequal (p ,"*v:lua." ,7 ) ?nlua_func_exists (p + 7 ) :function_exists (p + 1 , false); 1272 }else if (* p == ':' ) { 1273n = cmd_exists (p + 1 ); 1274 }else if (* p == '#' ) { 1275if (p [1 ]== '#' ) { 1276n = autocmd_supported (p + 2 ); 1277 }else { 1278n = au_exists (p + 1 ); 1279 } 1280 }else {// Internal variable. 1281n = var_exists (p ); 1282 } 1283 1284rettv -> vval .v_number = n ; 1285} 1286 1287/// "expand()" function 1288static void f_expand (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1289{ 1290int options = WILD_SILENT |WILD_USE_NL |WILD_LIST_NOTFOUND |WILD_USE_SHELLSLASH ; 1291bool error = false; 1292 1293rettv -> v_type = VAR_STRING ; 1294if (argvars [1 ].v_type != VAR_UNKNOWN 1295&& argvars [2 ].v_type != VAR_UNKNOWN 1296&& tv_get_bool_chk (& argvars [2 ],& error ) 1297&& !error ) { 1298tv_list_set_ret (rettv ,NULL ); 1299 } 1300 1301const char * s = tv_get_string (& argvars [0 ]); 1302if (* s == '%' || * s == '#' || * s == '<' ) { 1303if (p_verbose == 0 ) { 1304emsg_off ++ ; 1305 } 1306size_t len ; 1307const char * errormsg = NULL ; 1308char * result = eval_vars ((char * )s ,s ,& len ,NULL ,& errormsg ,NULL , false); 1309if (p_verbose == 0 ) { 1310emsg_off -- ; 1311 }else if (errormsg != NULL ) { 1312emsg (errormsg ); 1313 } 1314if (rettv -> v_type == VAR_LIST ) { 1315tv_list_alloc_ret (rettv , (result != NULL )); 1316if (result != NULL ) { 1317tv_list_append_string (rettv -> vval .v_list ,result ,-1 ); 1318 } 1319XFREE_CLEAR (result ); 1320 }else { 1321rettv -> vval .v_string = result ; 1322 } 1323 }else { 1324// When the optional second argument is non-zero, don't remove matches 1325// for 'wildignore' and don't put matches for 'suffixes' at the end. 1326if (argvars [1 ].v_type != VAR_UNKNOWN 1327&& tv_get_bool_chk (& argvars [1 ],& error )) { 1328options |=WILD_KEEP_ALL ; 1329 } 1330if (!error ) { 1331expand_T xpc ; 1332ExpandInit (& xpc ); 1333xpc .xp_context = EXPAND_FILES ; 1334if (p_wic ) { 1335options += WILD_ICASE ; 1336 } 1337if (rettv -> v_type == VAR_STRING ) { 1338rettv -> vval .v_string = ExpandOne (& xpc , (char * )s ,NULL ,options ,WILD_ALL ); 1339 }else { 1340ExpandOne (& xpc , (char * )s ,NULL ,options ,WILD_ALL_KEEP ); 1341tv_list_alloc_ret (rettv ,xpc .xp_numfiles ); 1342for (int i = 0 ;i < xpc .xp_numfiles ;i ++ ) { 1343tv_list_append_string (rettv -> vval .v_list ,xpc .xp_files [i ],-1 ); 1344 } 1345ExpandCleanup (& xpc ); 1346 } 1347 }else { 1348rettv -> vval .v_string = NULL ; 1349 } 1350 } 1351} 1352 1353/// "menu_get(path [, modes])" function 1354static void f_menu_get (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1355{ 1356tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1357int modes = MENU_ALL_MODES ; 1358if (argvars [1 ].v_type == VAR_STRING ) { 1359const char * const strmodes = tv_get_string (& argvars [1 ]); 1360modes = get_menu_cmd_modes (strmodes , false,NULL ,NULL ); 1361 } 1362menu_get ((char * )tv_get_string (& argvars [0 ]),modes ,rettv -> vval .v_list ); 1363} 1364 1365/// "expandcmd()" function 1366/// Expand all the special characters in a command string. 1367static void f_expandcmd (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1368{ 1369const char * errormsg = NULL ; 1370bool emsgoff = true; 1371 1372if (argvars [1 ].v_type == VAR_DICT 1373&& tv_dict_get_bool (argvars [1 ].vval .v_dict ,"errmsg" ,kBoolVarFalse )) { 1374emsgoff = false; 1375 } 1376 1377rettv -> v_type = VAR_STRING ; 1378char * cmdstr = xstrdup (tv_get_string (& argvars [0 ])); 1379 1380exarg_T eap = { 1381 .cmd = cmdstr , 1382 .arg = cmdstr , 1383 .usefilter = false, 1384 .nextcmd = NULL , 1385 .cmdidx = CMD_USER , 1386 }; 1387eap .argt |=EX_NOSPC ; 1388 1389if (emsgoff ) { 1390emsg_off ++ ; 1391 } 1392if (expand_filename (& eap ,& cmdstr ,& errormsg )== FAIL ) { 1393if (!emsgoff && errormsg != NULL && * errormsg != NUL ) { 1394emsg (errormsg ); 1395 } 1396 } 1397if (emsgoff ) { 1398emsg_off -- ; 1399 } 1400 1401rettv -> vval .v_string = cmdstr ; 1402} 1403 1404/// "flatten()" and "flattennew()" functions 1405static void flatten_common (typval_T * argvars ,typval_T * rettv ,bool make_copy ) 1406{ 1407bool error = false; 1408 1409if (argvars [0 ].v_type != VAR_LIST ) { 1410semsg (_ (e_listarg ),"flatten()" ); 1411return ; 1412 } 1413 1414int maxdepth ; 1415if (argvars [1 ].v_type == VAR_UNKNOWN ) { 1416maxdepth = 999999 ; 1417 }else { 1418maxdepth = (int )tv_get_number_chk (& argvars [1 ],& error ); 1419if (error ) { 1420return ; 1421 } 1422if (maxdepth < 0 ) { 1423emsg (_ ("E900: maxdepth must be non-negative number" )); 1424return ; 1425 } 1426 } 1427 1428list_T * list = argvars [0 ].vval .v_list ; 1429rettv -> v_type = VAR_LIST ; 1430rettv -> vval .v_list = list ; 1431if (list == NULL ) { 1432return ; 1433 } 1434 1435if (make_copy ) { 1436list = tv_list_copy (NULL ,list , false,get_copyID ()); 1437rettv -> vval .v_list = list ; 1438if (list == NULL ) { 1439return ; 1440 } 1441 }else { 1442if (value_check_lock (tv_list_locked (list ),N_ ("flatten() argument" ),TV_TRANSLATE )) { 1443return ; 1444 } 1445tv_list_ref (list ); 1446 } 1447 1448tv_list_flatten (list ,NULL ,tv_list_len (list ),maxdepth ); 1449} 1450 1451/// "flatten(list[, {maxdepth}])" function 1452static void f_flatten (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1453{ 1454flatten_common (argvars ,rettv , false); 1455} 1456 1457/// "flattennew(list[, {maxdepth}])" function 1458static void f_flattennew (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1459{ 1460flatten_common (argvars ,rettv , true); 1461} 1462 1463/// "feedkeys()" function 1464static void f_feedkeys (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1465{ 1466// This is not allowed in the sandbox. If the commands would still be 1467// executed in the sandbox it would be OK, but it probably happens later, 1468// when "sandbox" is no longer set. 1469if (check_secure ()) { 1470return ; 1471 } 1472 1473const char * const keys = tv_get_string (& argvars [0 ]); 1474char nbuf [NUMBUFLEN ]; 1475const char * flags = NULL ; 1476if (argvars [1 ].v_type != VAR_UNKNOWN ) { 1477flags = tv_get_string_buf (& argvars [1 ],nbuf ); 1478 } 1479 1480nvim_feedkeys (cstr_as_string (keys ), 1481cstr_as_string (flags ), true); 1482} 1483 1484/// "float2nr({float})" function 1485static void f_float2nr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1486{ 1487float_T f ; 1488 1489if (!tv_get_float_chk (argvars ,& f )) { 1490return ; 1491 } 1492 1493if (f <= (float_T )(- VARNUMBER_MAX )+ DBL_EPSILON ) { 1494rettv -> vval .v_number = - VARNUMBER_MAX ; 1495 }else if (f >= (float_T )VARNUMBER_MAX - DBL_EPSILON ) { 1496rettv -> vval .v_number = VARNUMBER_MAX ; 1497 }else { 1498rettv -> vval .v_number = (varnumber_T )f ; 1499 } 1500} 1501 1502/// "fmod()" function 1503static void f_fmod (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1504{ 1505float_T fx ; 1506float_T fy ; 1507 1508rettv -> v_type = VAR_FLOAT ; 1509if (tv_get_float_chk (argvars ,& fx )&& tv_get_float_chk (& argvars [1 ],& fy )) { 1510rettv -> vval .v_float = fmod (fx ,fy ); 1511 }else { 1512rettv -> vval .v_float = 0.0 ; 1513 } 1514} 1515 1516/// "fnameescape({string})" function 1517static void f_fnameescape (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1518{ 1519rettv -> vval .v_string = vim_strsave_fnameescape (tv_get_string (& argvars [0 ]),VSE_NONE ); 1520rettv -> v_type = VAR_STRING ; 1521} 1522 1523/// "foreground()" function 1524static void f_foreground (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1525{ 1526} 1527 1528/// "function()" function 1529/// "funcref()" function 1530static void common_function (typval_T * argvars ,typval_T * rettv ,bool is_funcref ) 1531{ 1532char * s ; 1533char * name ; 1534bool use_string = false; 1535partial_T * arg_pt = NULL ; 1536char * trans_name = NULL ; 1537 1538if (argvars [0 ].v_type == VAR_FUNC ) { 1539// function(MyFunc, [arg], dict) 1540s = argvars [0 ].vval .v_string ; 1541 }else if (argvars [0 ].v_type == VAR_PARTIAL 1542&& argvars [0 ].vval .v_partial != NULL ) { 1543// function(dict.MyFunc, [arg]) 1544arg_pt = argvars [0 ].vval .v_partial ; 1545s = partial_name (arg_pt ); 1546// TODO(bfredl): do the entire nlua_is_table_from_lua dance 1547 }else { 1548// function('MyFunc', [arg], dict) 1549s = (char * )tv_get_string (& argvars [0 ]); 1550use_string = true; 1551 } 1552 1553if ((use_string && vim_strchr (s ,AUTOLOAD_CHAR )== NULL )|| is_funcref ) { 1554name = s ; 1555trans_name = save_function_name (& name , false, 1556TFN_INT |TFN_QUIET |TFN_NO_AUTOLOAD |TFN_NO_DEREF ,NULL ); 1557if (* name != NUL ) { 1558s = NULL ; 1559 } 1560 } 1561if (s == NULL || * s == NUL || (use_string && ascii_isdigit (* s )) 1562|| (is_funcref && trans_name == NULL )) { 1563semsg (_ (e_invarg2 ), (use_string ?tv_get_string (& argvars [0 ]) :s )); 1564// Don't check an autoload name for existence here. 1565 }else if (trans_name != NULL 1566&& (is_funcref 1567 ?find_func (trans_name )== NULL 1568 : !translated_function_exists (trans_name ))) { 1569semsg (_ ("E700: Unknown function: %s" ),s ); 1570 }else { 1571int dict_idx = 0 ; 1572int arg_idx = 0 ; 1573list_T * list = NULL ; 1574if (strncmp (s ,"s:" ,2 )== 0 || strncmp (s ,"<SID>" ,5 )== 0 ) { 1575// Expand s: and <SID> into <SNR>nr_, so that the function can 1576// also be called from another script. Using trans_function_name() 1577// would also work, but some plugins depend on the name being 1578// printable text. 1579name = get_scriptlocal_funcname (s ); 1580 }else { 1581name = xstrdup (s ); 1582 } 1583 1584if (argvars [1 ].v_type != VAR_UNKNOWN ) { 1585if (argvars [2 ].v_type != VAR_UNKNOWN ) { 1586// function(name, [args], dict) 1587arg_idx = 1 ; 1588dict_idx = 2 ; 1589 }else if (argvars [1 ].v_type == VAR_DICT ) { 1590// function(name, dict) 1591dict_idx = 1 ; 1592 }else { 1593// function(name, [args]) 1594arg_idx = 1 ; 1595 } 1596if (dict_idx > 0 ) { 1597if (tv_check_for_dict_arg (argvars ,dict_idx )== FAIL ) { 1598xfree (name ); 1599 gototheend ; 1600 } 1601if (argvars [dict_idx ].vval .v_dict == NULL ) { 1602dict_idx = 0 ; 1603 } 1604 } 1605if (arg_idx > 0 ) { 1606if (argvars [arg_idx ].v_type != VAR_LIST ) { 1607emsg (_ ("E923: Second argument of function() must be " 1608"a list or a dict" )); 1609xfree (name ); 1610 gototheend ; 1611 } 1612list = argvars [arg_idx ].vval .v_list ; 1613if (tv_list_len (list )== 0 ) { 1614arg_idx = 0 ; 1615 }else if (tv_list_len (list )> MAX_FUNC_ARGS ) { 1616emsg_funcname (e_toomanyarg ,s ); 1617xfree (name ); 1618 gototheend ; 1619 } 1620 } 1621 } 1622if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref ) { 1623partial_T * const pt = xcalloc (1 ,sizeof (* pt )); 1624 1625// result is a VAR_PARTIAL 1626if (arg_idx > 0 || (arg_pt != NULL && arg_pt -> pt_argc > 0 )) { 1627const int arg_len = (arg_pt == NULL ?0 :arg_pt -> pt_argc ); 1628const int lv_len = tv_list_len (list ); 1629 1630pt -> pt_argc = arg_len + lv_len ; 1631pt -> pt_argv = xmalloc (sizeof (pt -> pt_argv [0 ])* (size_t )pt -> pt_argc ); 1632int i = 0 ; 1633for (;i < arg_len ;i ++ ) { 1634tv_copy (& arg_pt -> pt_argv [i ],& pt -> pt_argv [i ]); 1635 } 1636if (lv_len > 0 ) { 1637TV_LIST_ITER (list ,li , { 1638tv_copy (TV_LIST_ITEM_TV (li ),& pt -> pt_argv [i ++ ]); 1639 }); 1640 } 1641 } 1642 1643// For "function(dict.func, [], dict)" and "func" is a partial 1644// use "dict". That is backwards compatible. 1645if (dict_idx > 0 ) { 1646// The dict is bound explicitly, pt_auto is false 1647pt -> pt_dict = argvars [dict_idx ].vval .v_dict ; 1648 (pt -> pt_dict -> dv_refcount )++ ; 1649 }else if (arg_pt != NULL ) { 1650// If the dict was bound automatically the result is also 1651// bound automatically. 1652pt -> pt_dict = arg_pt -> pt_dict ; 1653pt -> pt_auto = arg_pt -> pt_auto ; 1654if (pt -> pt_dict != NULL ) { 1655 (pt -> pt_dict -> dv_refcount )++ ; 1656 } 1657 } 1658 1659pt -> pt_refcount = 1 ; 1660if (arg_pt != NULL && arg_pt -> pt_func != NULL ) { 1661pt -> pt_func = arg_pt -> pt_func ; 1662func_ptr_ref (pt -> pt_func ); 1663xfree (name ); 1664 }else if (is_funcref ) { 1665pt -> pt_func = find_func (trans_name ); 1666func_ptr_ref (pt -> pt_func ); 1667xfree (name ); 1668 }else { 1669pt -> pt_name = name ; 1670func_ref (name ); 1671 } 1672 1673rettv -> v_type = VAR_PARTIAL ; 1674rettv -> vval .v_partial = pt ; 1675 }else { 1676// result is a VAR_FUNC 1677rettv -> v_type = VAR_FUNC ; 1678rettv -> vval .v_string = name ; 1679func_ref (name ); 1680 } 1681 } 1682theend : 1683xfree (trans_name ); 1684} 1685 1686static void f_funcref (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1687{ 1688common_function (argvars ,rettv , true); 1689} 1690 1691static void f_function (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1692{ 1693common_function (argvars ,rettv , false); 1694} 1695 1696/// "garbagecollect()" function 1697static void f_garbagecollect (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1698{ 1699// This is postponed until we are back at the toplevel, because we may be 1700// using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". 1701want_garbage_collect = true; 1702 1703if (argvars [0 ].v_type != VAR_UNKNOWN && tv_get_bool (& argvars [0 ])== 1 ) { 1704garbage_collect_at_exit = true; 1705 } 1706} 1707 1708/// "get()" function 1709static void f_get (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1710{ 1711typval_T * tv = NULL ; 1712bool what_is_dict = false; 1713 1714if (argvars [0 ].v_type == VAR_BLOB ) { 1715bool error = false; 1716int idx = (int )tv_get_number_chk (& argvars [1 ],& error ); 1717 1718if (!error ) { 1719rettv -> v_type = VAR_NUMBER ; 1720if (idx < 0 ) { 1721idx = tv_blob_len (argvars [0 ].vval .v_blob )+ idx ; 1722 } 1723if (idx < 0 || idx >=tv_blob_len (argvars [0 ].vval .v_blob )) { 1724rettv -> vval .v_number = -1 ; 1725 }else { 1726rettv -> vval .v_number = tv_blob_get (argvars [0 ].vval .v_blob ,idx ); 1727tv = rettv ; 1728 } 1729 } 1730 }else if (argvars [0 ].v_type == VAR_LIST ) { 1731list_T * l = argvars [0 ].vval .v_list ; 1732if (l != NULL ) { 1733bool error = false; 1734 1735listitem_T * li = tv_list_find (l , (int )tv_get_number_chk (& argvars [1 ],& error )); 1736if (!error && li != NULL ) { 1737tv = TV_LIST_ITEM_TV (li ); 1738 } 1739 } 1740 }else if (argvars [0 ].v_type == VAR_DICT ) { 1741dict_T * d = argvars [0 ].vval .v_dict ; 1742if (d != NULL ) { 1743dictitem_T * di = tv_dict_find (d ,tv_get_string (& argvars [1 ]),-1 ); 1744if (di != NULL ) { 1745tv = & di -> di_tv ; 1746 } 1747 } 1748 }else if (tv_is_func (argvars [0 ])) { 1749partial_T * pt ; 1750partial_T fref_pt ; 1751 1752if (argvars [0 ].v_type == VAR_PARTIAL ) { 1753pt = argvars [0 ].vval .v_partial ; 1754 }else { 1755CLEAR_FIELD (fref_pt ); 1756fref_pt .pt_name = argvars [0 ].vval .v_string ; 1757pt = & fref_pt ; 1758 } 1759 1760if (pt != NULL ) { 1761const char * const what = tv_get_string (& argvars [1 ]); 1762 1763if (strcmp (what ,"func" )== 0 || strcmp (what ,"name" )== 0 ) { 1764const char * name = partial_name (pt ); 1765rettv -> v_type = (* what == 'f' ?VAR_FUNC :VAR_STRING ); 1766assert (name != NULL ); 1767if (rettv -> v_type == VAR_FUNC ) { 1768func_ref ((char * )name ); 1769 } 1770if (* what == 'n' && pt -> pt_name == NULL && pt -> pt_func != NULL ) { 1771// use <SNR> instead of the byte code 1772name = printable_func_name (pt -> pt_func ); 1773 } 1774rettv -> vval .v_string = xstrdup (name ); 1775 }else if (strcmp (what ,"dict" )== 0 ) { 1776what_is_dict = true; 1777if (pt -> pt_dict != NULL ) { 1778tv_dict_set_ret (rettv ,pt -> pt_dict ); 1779 } 1780 }else if (strcmp (what ,"args" )== 0 ) { 1781rettv -> v_type = VAR_LIST ; 1782tv_list_alloc_ret (rettv ,pt -> pt_argc ); 1783for (int i = 0 ;i < pt -> pt_argc ;i ++ ) { 1784tv_list_append_tv (rettv -> vval .v_list ,& pt -> pt_argv [i ]); 1785 } 1786 }else if (strcmp (what ,"arity" )== 0 ) { 1787int required = 0 ; 1788int optional = 0 ; 1789bool varargs = false; 1790const char * name = partial_name (pt ); 1791 1792get_func_arity (name ,& required ,& optional ,& varargs ); 1793 1794rettv -> v_type = VAR_DICT ; 1795tv_dict_alloc_ret (rettv ); 1796dict_T * dict = rettv -> vval .v_dict ; 1797 1798// Take into account the arguments of the partial, if any. 1799// Note that it is possible to supply more arguments than the function 1800// accepts. 1801if (pt -> pt_argc >=required + optional ) { 1802required = optional = 0 ; 1803 }else if (pt -> pt_argc > required ) { 1804optional -= pt -> pt_argc - required ; 1805required = 0 ; 1806 }else { 1807required -= pt -> pt_argc ; 1808 } 1809 1810tv_dict_add_nr (dict ,S_LEN ("required" ),required ); 1811tv_dict_add_nr (dict ,S_LEN ("optional" ),optional ); 1812tv_dict_add_bool (dict ,S_LEN ("varargs" ),varargs ); 1813 }else { 1814semsg (_ (e_invarg2 ),what ); 1815 } 1816 1817// When {what} == "dict" and pt->pt_dict == NULL, evaluate the 1818// third argument 1819if (!what_is_dict ) { 1820return ; 1821 } 1822 } 1823 }else { 1824semsg (_ (e_listdictblobarg ),"get()" ); 1825 } 1826 1827if (tv == NULL ) { 1828if (argvars [2 ].v_type != VAR_UNKNOWN ) { 1829tv_copy (& argvars [2 ],rettv ); 1830 } 1831 }else { 1832tv_copy (tv ,rettv ); 1833 } 1834} 1835 1836/// "getchangelist()" function 1837static void f_getchangelist (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1838{ 1839tv_list_alloc_ret (rettv ,2 ); 1840 1841const buf_T * buf ; 1842if (argvars [0 ].v_type == VAR_UNKNOWN ) { 1843buf = curbuf ; 1844 }else { 1845vim_ignored = (int )tv_get_number (& argvars [0 ]);// issue errmsg if type error 1846emsg_off ++ ; 1847buf = tv_get_buf (& argvars [0 ], false); 1848emsg_off -- ; 1849 } 1850if (buf == NULL ) { 1851return ; 1852 } 1853 1854list_T * const l = tv_list_alloc (buf -> b_changelistlen ); 1855tv_list_append_list (rettv -> vval .v_list ,l ); 1856// The current window change list index tracks only the position for the 1857// current buffer. For other buffers use the stored index for the current 1858// window, or, if that's not available, the change list length. 1859int changelistindex ; 1860if (buf == curwin -> w_buffer ) { 1861changelistindex = curwin -> w_changelistidx ; 1862 }else { 1863changelistindex = buf -> b_changelistlen ; 1864 1865for (size_t i = 0 ;i < kv_size (buf -> b_wininfo );i ++ ) { 1866WinInfo * wip = kv_A (buf -> b_wininfo ,i ); 1867if (wip -> wi_win == curwin ) { 1868changelistindex = wip -> wi_changelistidx ; 1869break ; 1870 } 1871 } 1872 } 1873tv_list_append_number (rettv -> vval .v_list , (varnumber_T )changelistindex ); 1874 1875for (int i = 0 ;i < buf -> b_changelistlen ;i ++ ) { 1876if (buf -> b_changelist [i ].mark .lnum == 0 ) { 1877continue ; 1878 } 1879dict_T * const d = tv_dict_alloc (); 1880tv_list_append_dict (l ,d ); 1881tv_dict_add_nr (d ,S_LEN ("lnum" ),buf -> b_changelist [i ].mark .lnum ); 1882tv_dict_add_nr (d ,S_LEN ("col" ),buf -> b_changelist [i ].mark .col ); 1883tv_dict_add_nr (d ,S_LEN ("coladd" ),buf -> b_changelist [i ].mark .coladd ); 1884 } 1885} 1886 1887static void getpos_both (typval_T * argvars ,typval_T * rettv ,bool getcurpos ,bool charcol ) 1888{ 1889pos_T * fp = NULL ; 1890pos_T pos ; 1891win_T * wp = curwin ; 1892int fnum = -1 ; 1893 1894if (getcurpos ) { 1895if (argvars [0 ].v_type != VAR_UNKNOWN ) { 1896wp = find_win_by_nr_or_id (& argvars [0 ]); 1897if (wp != NULL ) { 1898fp = & wp -> w_cursor ; 1899 } 1900 }else { 1901fp = & curwin -> w_cursor ; 1902 } 1903if (fp != NULL && charcol ) { 1904pos = * fp ; 1905pos .col = buf_byteidx_to_charidx (wp -> w_buffer ,pos .lnum ,pos .col ); 1906fp = & pos ; 1907 } 1908 }else { 1909fp = var2fpos (& argvars [0 ], true,& fnum ,charcol ,curwin ); 1910 } 1911 1912list_T * const l = tv_list_alloc_ret (rettv ,4 + getcurpos ); 1913tv_list_append_number (l , (fnum != -1 ) ? (varnumber_T )fnum : (varnumber_T )0 ); 1914tv_list_append_number (l , ((fp != NULL ) ? (varnumber_T )fp -> lnum : (varnumber_T )0 )); 1915tv_list_append_number (l , ((fp != NULL ) 1916 ? (varnumber_T )(fp -> col == MAXCOL ?MAXCOL :fp -> col + 1 ) 1917 : (varnumber_T )0 )); 1918tv_list_append_number (l , (fp != NULL ) ? (varnumber_T )fp -> coladd : (varnumber_T )0 ); 1919if (getcurpos ) { 1920const bool save_set_curswant = curwin -> w_set_curswant ; 1921const colnr_T save_curswant = curwin -> w_curswant ; 1922const colnr_T save_virtcol = curwin -> w_virtcol ; 1923 1924if (wp == curwin ) { 1925update_curswant (); 1926 } 1927tv_list_append_number (l , (wp == NULL ) ?0 : ((wp -> w_curswant == MAXCOL ) 1928 ? (varnumber_T )MAXCOL 1929 : (varnumber_T )wp -> w_curswant + 1 )); 1930 1931// Do not change "curswant", as it is unexpected that a get 1932// function has a side effect. 1933if (wp == curwin && save_set_curswant ) { 1934curwin -> w_set_curswant = save_set_curswant ; 1935curwin -> w_curswant = save_curswant ; 1936curwin -> w_virtcol = save_virtcol ; 1937curwin -> w_valid &= ~VALID_VIRTCOL ; 1938 } 1939 } 1940} 1941 1942/// "getcharpos()" function 1943static void f_getcharpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1944{ 1945getpos_both (argvars ,rettv , false, true); 1946} 1947 1948/// "getcharsearch()" function 1949static void f_getcharsearch (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1950{ 1951tv_dict_alloc_ret (rettv ); 1952 1953dict_T * dict = rettv -> vval .v_dict ; 1954 1955tv_dict_add_str (dict ,S_LEN ("char" ),last_csearch ()); 1956tv_dict_add_nr (dict ,S_LEN ("forward" ),last_csearch_forward ()); 1957tv_dict_add_nr (dict ,S_LEN ("until" ),last_csearch_until ()); 1958} 1959 1960/// "getfontname()" function 1961static void f_getfontname (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1962{ 1963rettv -> v_type = VAR_STRING ; 1964rettv -> vval .v_string = NULL ; 1965} 1966 1967/// "getjumplist()" function 1968static void f_getjumplist (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1969{ 1970tv_list_alloc_ret (rettv ,kListLenMayKnow ); 1971win_T * const wp = find_tabwin (& argvars [0 ],& argvars [1 ]); 1972if (wp == NULL ) { 1973return ; 1974 } 1975 1976cleanup_jumplist (wp , true); 1977 1978list_T * const l = tv_list_alloc (wp -> w_jumplistlen ); 1979tv_list_append_list (rettv -> vval .v_list ,l ); 1980tv_list_append_number (rettv -> vval .v_list ,wp -> w_jumplistidx ); 1981 1982for (int i = 0 ;i < wp -> w_jumplistlen ;i ++ ) { 1983if (wp -> w_jumplist [i ].fmark .mark .lnum == 0 ) { 1984continue ; 1985 } 1986dict_T * const d = tv_dict_alloc (); 1987tv_list_append_dict (l ,d ); 1988tv_dict_add_nr (d ,S_LEN ("lnum" ),wp -> w_jumplist [i ].fmark .mark .lnum ); 1989tv_dict_add_nr (d ,S_LEN ("col" ),wp -> w_jumplist [i ].fmark .mark .col ); 1990tv_dict_add_nr (d ,S_LEN ("coladd" ),wp -> w_jumplist [i ].fmark .mark .coladd ); 1991tv_dict_add_nr (d ,S_LEN ("bufnr" ),wp -> w_jumplist [i ].fmark .fnum ); 1992if (wp -> w_jumplist [i ].fname != NULL ) { 1993tv_dict_add_str (d ,S_LEN ("filename" ),wp -> w_jumplist [i ].fname ); 1994 } 1995 } 1996} 1997 1998/// "getmarklist()" function 1999static void f_getmarklist (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2000{ 2001tv_list_alloc_ret (rettv ,kListLenMayKnow ); 2002 2003if (argvars [0 ].v_type == VAR_UNKNOWN ) { 2004get_global_marks (rettv -> vval .v_list ); 2005return ; 2006 } 2007 2008buf_T * buf = tv_get_buf (& argvars [0 ], false); 2009if (buf == NULL ) { 2010return ; 2011 } 2012 2013get_buf_local_marks (buf ,rettv -> vval .v_list ); 2014} 2015 2016/// "getpid()" function 2017static void f_getpid (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2018{ 2019rettv -> vval .v_number = os_get_pid (); 2020} 2021 2022/// "getcurpos(string)" function 2023static void f_getcurpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2024{ 2025getpos_both (argvars ,rettv , true, false); 2026} 2027 2028static void f_getcursorcharpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2029{ 2030getpos_both (argvars ,rettv , true, true); 2031} 2032 2033/// "getpos(string)" function 2034static void f_getpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2035{ 2036getpos_both (argvars ,rettv , false, false); 2037} 2038 2039/// Convert from block_def to string 2040static String block_def2str (struct block_def * bd ) 2041{ 2042size_t size = (size_t )bd -> startspaces + (size_t )bd -> endspaces + (size_t )bd -> textlen ; 2043String ret = { .data = xmalloc (size + 1 ) }; 2044 2045memset (ret .data ,' ' , (size_t )bd -> startspaces ); 2046ret .size += (size_t )bd -> startspaces ; 2047memmove (ret .data + ret .size ,bd -> textstart , (size_t )bd -> textlen ); 2048ret .size += (size_t )bd -> textlen ; 2049memset (ret .data + ret .size ,' ' , (size_t )bd -> endspaces ); 2050ret .size += (size_t )bd -> endspaces ; 2051ret .data [ret .size ]= NUL ; 2052 2053return ret ; 2054} 2055 2056static int getregionpos (typval_T * argvars ,typval_T * rettv ,pos_T * p1 ,pos_T * p2 , 2057bool * const inclusive ,MotionType * region_type ,oparg_T * oap ) 2058FUNC_ATTR_NONNULL_ALL 2059{ 2060tv_list_alloc_ret (rettv ,kListLenMayKnow ); 2061 2062if (tv_check_for_list_arg (argvars ,0 )== FAIL 2063|| tv_check_for_list_arg (argvars ,1 )== FAIL 2064|| tv_check_for_opt_dict_arg (argvars ,2 )== FAIL ) { 2065return FAIL ; 2066 } 2067 2068int fnum1 = -1 ; 2069int fnum2 = -1 ; 2070if (list2fpos (& argvars [0 ],p1 ,& fnum1 ,NULL , false)!= OK 2071|| list2fpos (& argvars [1 ],p2 ,& fnum2 ,NULL , false)!= OK 2072|| fnum1 != fnum2 ) { 2073return FAIL ; 2074 } 2075 2076bool is_select_exclusive ; 2077char * type ; 2078char default_type []= "v" ; 2079if (argvars [2 ].v_type == VAR_DICT ) { 2080is_select_exclusive = tv_dict_get_bool (argvars [2 ].vval .v_dict ,"exclusive" , 2081* p_sel == 'e' ); 2082type = tv_dict_get_string (argvars [2 ].vval .v_dict ,"type" , false); 2083if (type == NULL ) { 2084type = default_type ; 2085 } 2086 }else { 2087is_select_exclusive = * p_sel == 'e' ; 2088type = default_type ; 2089 } 2090 2091int block_width = 0 ; 2092if (type [0 ]== 'v' && type [1 ]== NUL ) { 2093* region_type = kMTCharWise ; 2094 }else if (type [0 ]== 'V' && type [1 ]== NUL ) { 2095* region_type = kMTLineWise ; 2096 }else if (type [0 ]== Ctrl_V ) { 2097char * p = type + 1 ; 2098if (* p != NUL && ((block_width = getdigits_int (& p , false,0 )) <=0 || * p != NUL )) { 2099semsg (_ (e_invargNval ),"type" ,type ); 2100return FAIL ; 2101 } 2102* region_type = kMTBlockWise ; 2103 }else { 2104semsg (_ (e_invargNval ),"type" ,type ); 2105return FAIL ; 2106 } 2107 2108buf_T * findbuf = fnum1 != 0 ?buflist_findnr (fnum1 ) :curbuf ; 2109if (findbuf == NULL || findbuf -> b_ml .ml_mfp == NULL ) { 2110emsg (_ (e_buffer_is_not_loaded )); 2111return FAIL ; 2112 } 2113 2114if (p1 -> lnum < 1 || p1 -> lnum > findbuf -> b_ml .ml_line_count ) { 2115semsg (_ (e_invalid_line_number_nr ),p1 -> lnum ); 2116return FAIL ; 2117 } 2118if (p1 -> col == MAXCOL ) { 2119p1 -> col = ml_get_buf_len (findbuf ,p1 -> lnum )+ 1 ; 2120 }else if (p1 -> col < 1 || p1 -> col > ml_get_buf_len (findbuf ,p1 -> lnum )+ 1 ) { 2121semsg (_ (e_invalid_column_number_nr ),p1 -> col ); 2122return FAIL ; 2123 } 2124 2125if (p2 -> lnum < 1 || p2 -> lnum > findbuf -> b_ml .ml_line_count ) { 2126semsg (_ (e_invalid_line_number_nr ),p2 -> lnum ); 2127return FAIL ; 2128 } 2129if (p2 -> col == MAXCOL ) { 2130p2 -> col = ml_get_buf_len (findbuf ,p2 -> lnum )+ 1 ; 2131 }else if (p2 -> col < 1 || p2 -> col > ml_get_buf_len (findbuf ,p2 -> lnum )+ 1 ) { 2132semsg (_ (e_invalid_column_number_nr ),p2 -> col ); 2133return FAIL ; 2134 } 2135 2136curbuf = findbuf ; 2137curwin -> w_buffer = curbuf ; 2138virtual_op = virtual_active (curwin ); 2139 2140// NOTE: Adjustment is needed. 2141p1 -> col -- ; 2142p2 -> col -- ; 2143 2144getregionpos_prep (p1 ,p2 ,* region_type ,is_select_exclusive ,block_width ,inclusive ,oap ); 2145return OK ; 2146} 2147 2148/// "getregion()" function 2149static void f_getregion (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2150{ 2151buf_T * const save_curbuf = curbuf ; 2152const TriState save_virtual = virtual_op ; 2153 2154pos_T p1 ,p2 ; 2155bool inclusive = true; 2156MotionType region_type = kMTUnknown ; 2157oparg_T oa ; 2158 2159if (getregionpos (argvars ,rettv ,& p1 ,& p2 ,& inclusive ,& region_type ,& oa )== FAIL ) { 2160return ; 2161 } 2162 2163for (linenr_T lnum = p1 .lnum ;lnum <=p2 .lnum ;lnum ++ ) { 2164String akt = STRING_INIT ; 2165 2166if (region_type == kMTBlockWise ) { 2167struct block_def bd ; 2168block_prep (& oa ,& bd ,lnum , false); 2169akt = block_def2str (& bd ); 2170 }else if (region_type == kMTLineWise || (p1 .lnum < lnum && lnum < p2 .lnum )) { 2171akt = cbuf_to_string (ml_get (lnum ), (size_t )ml_get_len (lnum )); 2172 }else { 2173struct block_def bd ; 2174charwise_block_prep (p1 ,p2 ,& bd ,lnum ,inclusive ); 2175akt = block_def2str (& bd ); 2176 } 2177 2178assert (akt .data != NULL ); 2179tv_list_append_allocated_string (rettv -> vval .v_list ,akt .data ); 2180 } 2181 2182// getregionpos() may change curbuf and virtual_op 2183curbuf = save_curbuf ; 2184curwin -> w_buffer = curbuf ; 2185virtual_op = save_virtual ; 2186} 2187 2188static void add_regionpos_range (typval_T * rettv ,pos_T p1 ,pos_T p2 ) 2189{ 2190list_T * l1 = tv_list_alloc (2 ); 2191tv_list_append_list (rettv -> vval .v_list ,l1 ); 2192 2193list_T * l2 = tv_list_alloc (4 ); 2194tv_list_append_list (l1 ,l2 ); 2195 2196list_T * l3 = tv_list_alloc (4 ); 2197tv_list_append_list (l1 ,l3 ); 2198 2199tv_list_append_number (l2 ,curbuf -> b_fnum ); 2200tv_list_append_number (l2 ,p1 .lnum ); 2201tv_list_append_number (l2 ,p1 .col ); 2202tv_list_append_number (l2 ,p1 .coladd ); 2203 2204tv_list_append_number (l3 ,curbuf -> b_fnum ); 2205tv_list_append_number (l3 ,p2 .lnum ); 2206tv_list_append_number (l3 ,p2 .col ); 2207tv_list_append_number (l3 ,p2 .coladd ); 2208} 2209 2210/// "getregionpos()" function 2211static void f_getregionpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2212{ 2213buf_T * const save_curbuf = curbuf ; 2214const TriState save_virtual = virtual_op ; 2215 2216pos_T p1 ,p2 ; 2217bool inclusive = true; 2218MotionType region_type = kMTUnknown ; 2219bool allow_eol = false; 2220bool bounds_only = false; 2221oparg_T oa ; 2222 2223if (getregionpos (argvars ,rettv ,& p1 ,& p2 ,& inclusive ,& region_type ,& oa )== FAIL ) { 2224return ; 2225 } 2226 2227if (argvars [2 ].v_type == VAR_DICT ) { 2228allow_eol = tv_dict_get_bool (argvars [2 ].vval .v_dict ,"eol" , false); 2229bounds_only = tv_dict_get_bool (argvars [2 ].vval .v_dict ,"bounds" , false); 2230 } 2231 2232if (bounds_only ) { 2233// Only the outer bounds of the region are wanted, so the lines in 2234// between do not have to be visited. 2235pos_T start_pos ,end_pos ; 2236 2237getregionpos_line (p1 .lnum ,p1 ,p2 ,inclusive ,region_type ,& oa , 2238allow_eol ,& start_pos ,& end_pos ); 2239if (p2 .lnum != p1 .lnum ) { 2240pos_T unused ; 2241 2242getregionpos_line (p2 .lnum ,p1 ,p2 ,inclusive ,region_type ,& oa , 2243allow_eol ,& unused ,& end_pos ); 2244 } 2245add_regionpos_range (rettv ,start_pos ,end_pos ); 2246 }else { 2247for (linenr_T lnum = p1 .lnum ;lnum <=p2 .lnum ;lnum ++ ) { 2248pos_T ret_p1 ,ret_p2 ; 2249 2250getregionpos_line (lnum ,p1 ,p2 ,inclusive ,region_type ,& oa , 2251allow_eol ,& ret_p1 ,& ret_p2 ); 2252add_regionpos_range (rettv ,ret_p1 ,ret_p2 ); 2253 } 2254 } 2255 2256// getregionpos() may change curbuf and virtual_op 2257curbuf = save_curbuf ; 2258curwin -> w_buffer = curbuf ; 2259virtual_op = save_virtual ; 2260} 2261 2262/// Common between getreg(), getreginfo() and getregtype(): get the register 2263/// name from the first argument. 2264/// Returns zero on error. 2265static int getreg_get_regname (typval_T * argvars ) 2266{ 2267const char * strregname ; 2268 2269if (argvars [0 ].v_type != VAR_UNKNOWN ) { 2270strregname = tv_get_string_chk (& argvars [0 ]); 2271if (strregname == NULL ) {// type error; errmsg already given 2272return 0 ; 2273 } 2274 }else { 2275// Default to v:register 2276strregname = get_vim_var_str (VV_REG ); 2277 } 2278 2279return * strregname == 0 ?'"' : (uint8_t )(* strregname ); 2280} 2281 2282/// "getreg()" function 2283static void f_getreg (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2284{ 2285int arg2 = false; 2286bool return_list = false; 2287 2288int regname = getreg_get_regname (argvars ); 2289if (regname == 0 ) { 2290return ; 2291 } 2292 2293if (argvars [0 ].v_type != VAR_UNKNOWN && argvars [1 ].v_type != VAR_UNKNOWN ) { 2294bool error = false; 2295arg2 = (int )tv_get_bool_chk (& argvars [1 ],& error ); 2296if (!error && argvars [2 ].v_type != VAR_UNKNOWN ) { 2297return_list = (bool )tv_get_bool_chk (& argvars [2 ],& error ); 2298 } 2299if (error ) { 2300return ; 2301 } 2302 } 2303 2304if (return_list ) { 2305rettv -> v_type = VAR_LIST ; 2306rettv -> vval .v_list = 2307get_reg_contents (regname , (arg2 ?kGRegExprSrc :0 ) |kGRegList ); 2308if (rettv -> vval .v_list == NULL ) { 2309rettv -> vval .v_list = tv_list_alloc (0 ); 2310 } 2311tv_list_ref (rettv -> vval .v_list ); 2312 }else { 2313rettv -> v_type = VAR_STRING ; 2314rettv -> vval .v_string = get_reg_contents (regname ,arg2 ?kGRegExprSrc :0 ); 2315 } 2316} 2317 2318/// "getregtype()" function 2319static void f_getregtype (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2320{ 2321// on error return an empty string 2322rettv -> v_type = VAR_STRING ; 2323rettv -> vval .v_string = NULL ; 2324 2325int regname = getreg_get_regname (argvars ); 2326if (regname == 0 ) { 2327return ; 2328 } 2329 2330colnr_T reglen = 0 ; 2331char buf [NUMBUFLEN + 2 ]; 2332MotionType reg_type = get_reg_type (regname ,& reglen ); 2333format_reg_type (reg_type ,reglen ,buf ,ARRAY_SIZE (buf )); 2334 2335rettv -> vval .v_string = xstrdup (buf ); 2336} 2337 2338/// "gettagstack()" function 2339static void f_gettagstack (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2340{ 2341win_T * wp = curwin ;// default is current window 2342 2343tv_dict_alloc_ret (rettv ); 2344 2345if (argvars [0 ].v_type != VAR_UNKNOWN ) { 2346wp = find_win_by_nr_or_id (& argvars [0 ]); 2347if (wp == NULL ) { 2348return ; 2349 } 2350 } 2351 2352get_tagstack (wp ,rettv -> vval .v_dict ); 2353} 2354 2355/// Dummy timer callback. Used by f_wait(). 2356static void dummy_timer_due_cb (TimeWatcher * tw ,void * data ) 2357{ 2358// If the main loop is closing, the condition won't be checked again. 2359// Close the timer to avoid leaking resources. 2360if (main_loop .closing ) { 2361time_watcher_stop (tw ); 2362time_watcher_close (tw ,dummy_timer_close_cb ); 2363 } 2364} 2365 2366/// Dummy timer close callback. Used by f_wait(). 2367static void dummy_timer_close_cb (TimeWatcher * tw ,void * data ) 2368{ 2369xfree (tw ); 2370} 2371 2372/// "wait(timeout, condition[, interval])" function 2373static void f_wait (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2374{ 2375rettv -> v_type = VAR_NUMBER ; 2376rettv -> vval .v_number = -1 ; 2377 2378if (argvars [0 ].v_type != VAR_NUMBER ) { 2379semsg (_ (e_invargval ),"1" ); 2380return ; 2381 } 2382if ((argvars [2 ].v_type != VAR_NUMBER && argvars [2 ].v_type != VAR_UNKNOWN ) 2383|| (argvars [2 ].v_type == VAR_NUMBER && argvars [2 ].vval .v_number <=0 )) { 2384semsg (_ (e_invargval ),"3" ); 2385return ; 2386 } 2387 2388int timeout = (int )argvars [0 ].vval .v_number ; 2389typval_T expr = argvars [1 ]; 2390int interval = argvars [2 ].v_type == VAR_NUMBER 2391 ? (int )argvars [2 ].vval .v_number 2392 :200 ;// Default. 2393TimeWatcher * tw = xmalloc (sizeof (TimeWatcher )); 2394 2395// Start dummy timer. 2396time_watcher_init (& main_loop ,tw ,NULL ); 2397// Don't schedule the due callback, as that'll lead to two different types of events 2398// on each interval, causing the condition to be checked twice. 2399tw -> events = NULL ; 2400time_watcher_start (tw ,dummy_timer_due_cb , (uint64_t )interval , (uint64_t )interval ); 2401 2402typval_T argv = TV_INITIAL_VALUE ; 2403typval_T exprval = TV_INITIAL_VALUE ; 2404bool error = false; 2405const int called_emsg_before = called_emsg ; 2406 2407// Flush screen updates before blocking. 2408ui_flush (); 2409 2410LOOP_PROCESS_EVENTS_UNTIL (& main_loop ,main_loop .events ,timeout , 2411eval_expr_typval (& expr , false,& argv ,0 ,& exprval )!= OK 2412|| tv_get_number_chk (& exprval ,& error ) 2413|| called_emsg > called_emsg_before || error || got_int ); 2414 2415if (called_emsg > called_emsg_before || error ) { 2416rettv -> vval .v_number = -3 ; 2417 }else if (got_int ) { 2418got_int = false; 2419vgetc (); 2420rettv -> vval .v_number = -2 ; 2421 }else if (tv_get_number_chk (& exprval ,& error )) { 2422rettv -> vval .v_number = 0 ; 2423 } 2424 2425// Stop dummy timer 2426time_watcher_stop (tw ); 2427time_watcher_close (tw ,dummy_timer_close_cb ); 2428} 2429 2430/// "gettext()" function 2431static void f_gettext (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2432{ 2433if (tv_check_for_nonempty_string_arg (argvars ,0 )== FAIL ) { 2434return ; 2435 } 2436 2437rettv -> v_type = VAR_STRING ; 2438rettv -> vval .v_string = xstrdup (_ (argvars [0 ].vval .v_string )); 2439} 2440 2441/// "has()" function 2442static void f_has (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 2443{ 2444static const char * const has_list []= { 2445#ifdef __ANDROID__ 2446"android" , 2447#endif 2448#if defined(BSD )&& !defined (__APPLE__ )&& !defined (__GNU__ ) 2449"bsd" , 2450#endif 2451#ifdef __GNU__ 2452"hurd" , 2453#endif 2454#ifdef __linux__ 2455"linux" , 2456#endif 2457#ifdef SUN_SYSTEM 2458"sun" , 2459#endif 2460#ifdef __TERMUX__ 2461"termux" , 2462#endif 2463#ifdef UNIX 2464"unix" , 2465#endif 2466#ifdef MSWIN 2467"win32" , 2468#endif 2469#ifdef _WIN64 2470"win64" , 2471#endif 2472#ifndef CASE_INSENSITIVE_FILENAME 2473"fname_case" , 2474#endif 2475#ifdef HAVE_ACL 2476"acl" , 2477#endif 2478"autochdir" , 2479"arabic" , 2480"autocmd" , 2481"browsefilter" , 2482"byte_offset" , 2483"cindent" , 2484"cmdline_compl" , 2485"cmdline_hist" , 2486"cmdwin" , 2487"comments" , 2488"conceal" , 2489"cursorbind" , 2490"cursorshape" , 2491"dialog_con" , 2492"diff" , 2493"digraphs" , 2494"eval" ,// always present, of course! 2495"ex_extra" , 2496"extra_search" , 2497"file_in_path" , 2498"filterpipe" , 2499"find_in_path" , 2500"float" , 2501"folding" , 2502#ifdef UNIX 2503"fork" , 2504#endif 2505"gettext" , 2506"iconv" , 2507"insert_expand" , 2508"jumplist" , 2509"keymap" , 2510"lambda" , 2511"langmap" , 2512"libcall" , 2513"linebreak" , 2514"lispindent" , 2515"listcmds" , 2516"localmap" , 2517#ifdef __APPLE__ 2518"mac" , 2519"macunix" , 2520"osx" , 2521"osxdarwin" , 2522#endif 2523"menu" , 2524"mksession" , 2525"modify_fname" , 2526"mouse" , 2527"multi_byte" , 2528"multi_lang" , 2529"nanotime" , 2530"num64" , 2531"packages" , 2532"path_extra" , 2533"persistent_undo" , 2534"profile" , 2535"reltime" , 2536"quickfix" , 2537"rightleft" , 2538"scrollbind" , 2539"showcmd" , 2540"cmdline_info" , 2541"shada" , 2542"signs" , 2543"smartindent" , 2544"startuptime" , 2545"statusline" , 2546"spell" , 2547"syntax" , 2548#ifndef UNIX 2549"system" , 2550#endif 2551"tablineat" , 2552"tag_binary" , 2553"termguicolors" , 2554#ifdef HAVE_UNIBILIUM 2555"terminfo" , 2556#endif 2557"termresponse" , 2558"textobjects" , 2559"timers" , 2560"title" , 2561"user-commands" ,// was accidentally included in 5.4 2562"user_commands" , 2563"vartabs" , 2564"vertsplit" , 2565"vimscript-1" , 2566"virtualedit" , 2567"visual" , 2568"visualextra" , 2569"vreplace" , 2570"wildignore" , 2571"wildmenu" , 2572"windows" , 2573"winaltkeys" , 2574"writebackup" , 2575#ifdef HAVE_XATTR 2576"xattr" , 2577#endif 2578"nvim" , 2579 }; 2580 2581bool x = false; 2582bool n = false; 2583const char * const name = tv_get_string (& argvars [0 ]); 2584 2585// Fast-path: check features not in has_list[] first to avoid the full 2586// linear scan for very common queries like has('patch-...'). 2587if (STRNICMP (name ,"patch" ,5 )== 0 ) { 2588x = true; 2589if (name [5 ]== '-' 2590&& strlen (name ) >=11 2591&& (name [6 ] >='1' && name [6 ] <='9' )) { 2592char * end ; 2593 2594// This works for patch-8.1.2, patch-9.0.3, patch-10.0.4, etc. 2595// Not for patch-9.10.5. 2596int major = (int )strtoul (name + 6 ,& end ,10 ); 2597if (* end == '.' && ascii_isdigit (end [1 ]) 2598&& end [2 ]== '.' && ascii_isdigit (end [3 ])) { 2599int minor = atoi (end + 1 ); 2600 2601// Expect "patch-9.9.01234". 2602n = has_vim_patch (atoi (end + 3 ),major * 100 + minor ); 2603 } 2604 }else if (ascii_isdigit (name [5 ])) { 2605n = has_vim_patch (atoi (name + 5 ),0 ); 2606 } 2607 }else if (STRNICMP (name ,"nvim-" ,5 )== 0 ) { 2608x = true; 2609// Expect "nvim-x.y.z" 2610n = has_nvim_version (name + 5 ); 2611 }else if (STRICMP (name ,"vim_starting" )== 0 ) { 2612x = true; 2613n = (starting != 0 ); 2614 }else if (STRICMP (name ,"ttyin" )== 0 ) { 2615x = true; 2616n = stdin_isatty ; 2617 }else if (STRICMP (name ,"ttyout" )== 0 ) { 2618x = true; 2619n = stdout_isatty ; 2620 }else if (STRICMP (name ,"multi_byte_encoding" )== 0 ) { 2621x = true; 2622n = true; 2623 }else if (STRICMP (name ,"gui_running" )== 0 ) { 2624x = true; 2625n = ui_gui_attached (); 2626 }else if (STRICMP (name ,"syntax_items" )== 0 ) { 2627x = true; 2628n = syntax_present (curwin ); 2629 }else if (STRICMP (name ,"wsl" )== 0 ) { 2630x = true; 2631n = has_wsl (); 2632 } 2633 2634// Look up in has_list[] only if not already handled above. 2635if (!x ) { 2636for (size_t i = 0 ;i < ARRAY_SIZE (has_list );i ++ ) { 2637if (STRICMP (name ,has_list [i ])== 0 ) { 2638x = true; 2639n = true; 2640break ; 2641 } 2642 } 2643 } 2644 2645if (!x ) { 2646// XXX: eval_has_provider() may shell out :( 2647const int save_shell_error = (int )get_vim_var_nr (VV_SHELL_ERROR ); 2648 2649if (STRICMP (name ,"clipboard_working" )== 0 ) { 2650n = eval_has_provider ("clipboard" , true); 2651#ifdef UNIX 2652} else if ( STRICMP ( name , "unnamedplus" ) == 0 ) { 2653n = eval_has_provider ( "clipboard" , true); 2654#endif 2655} else if ( STRICMP ( name , "pythonx" ) == 0 ) { 2656n = eval_has_provider ( "python3" , true); 2657} else if ( eval_has_provider ( name , true)) { 2658n = true; 2659} 2660 2661set_vim_var_nr ( VV_SHELL_ERROR , save_shell_error ); 2662} 2663 2664rettv -> vval . v_number = n ; 2665} 2666 2667static bool has_wsl ( void ) 2668{ 2669static TriState has_wsl = kNone ; 2670if ( has_wsl == kNone ) { 2671Error err = ERROR_INIT ; 2672Object o = NLUA_EXEC_STATIC ( "return vim.uv.os_uname()['release']:lower()" 2673":match('microsoft')" , 2674( Array ) ARRAY_DICT_INIT , kRetNilBool , NULL , & err ); 2675assert (! ERROR_SET ( & err )); 2676has_wsl = LUARET_TRUTHY ( o ) ? kTrue : kFalse ; 2677} 2678return has_wsl == kTrue ; 2679} 2680 2681/// "highlightID(name)" function 2682static void f_hlID ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2683{ 2684rettv -> vval . v_number = syn_name2id ( tv_get_string ( & argvars [ 0 ])); 2685} 2686 2687/// "highlight_exists()" function 2688static void f_hlexists ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2689{ 2690rettv -> vval . v_number = highlight_exists ( tv_get_string ( & argvars [ 0 ])); 2691} 2692 2693/// "index()" function 2694static void f_index ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2695{ 2696int idx = 0 ; 2697bool ic = false; 2698 2699rettv -> vval . v_number = -1 ; 2700if ( argvars [ 0 ]. v_type == VAR_BLOB ) { 2701bool error = false; 2702int start = 0 ; 2703 2704if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 2705start = ( int ) tv_get_number_chk ( & argvars [ 2 ], & error ); 2706if ( error ) { 2707return; 2708} 2709} 2710blob_T * const b = argvars [ 0 ]. vval . v_blob ; 2711if ( b == NULL ) { 2712return; 2713} 2714if ( start < 0 ) { 2715start = tv_blob_len ( b ) + start ; 2716if ( start < 0 ) { 2717start = 0 ; 2718} 2719} 2720for ( idx = start ; idx < tv_blob_len ( b ); idx ++ ) { 2721typval_T tv ; 2722tv . v_type = VAR_NUMBER ; 2723tv . vval . v_number = tv_blob_get ( b , idx ); 2724if ( tv_equal ( & tv , & argvars [ 1 ], ic )) { 2725rettv -> vval . v_number = idx ; 2726return; 2727} 2728} 2729return; 2730} else if ( argvars [ 0 ]. v_type != VAR_LIST ) { 2731emsg ( _ ( e_listblobreq )); 2732return; 2733} 2734 2735list_T * const l = argvars [ 0 ]. vval . v_list ; 2736if ( l == NULL ) { 2737return; 2738} 2739 2740listitem_T * item = tv_list_first ( l ); 2741if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 2742bool error = false; 2743 2744// Start at specified item. 2745idx = tv_list_uidx ( l , ( int ) tv_get_number_chk ( & argvars [ 2 ], & error )); 2746if ( error || idx == -1 ) { 2747item = NULL ; 2748} else { 2749item = tv_list_find ( l , idx ); 2750assert ( item != NULL ); 2751} 2752if ( argvars [ 3 ]. v_type != VAR_UNKNOWN ) { 2753ic = !! tv_get_bool_chk ( & argvars [ 3 ], & error ); 2754if ( error ) { 2755item = NULL ; 2756} 2757} 2758} 2759 2760for (; item != NULL ; item = TV_LIST_ITEM_NEXT ( l , item ), idx ++ ) { 2761if ( tv_equal ( TV_LIST_ITEM_TV ( item ), & argvars [ 1 ], ic )) { 2762rettv -> vval . v_number = idx ; 2763break; 2764} 2765} 2766} 2767 2768/// Evaluate "expr" with the v:key and v:val arguments and return the result. 2769/// The expression is expected to return a boolean value. The caller should set 2770/// the VV_KEY and VV_VAL vim variables before calling this function. 2771static varnumber_T indexof_eval_expr ( typval_T * expr ) 2772{ 2773typval_T argv [ 3 ]; 2774argv [ 0 ] = * get_vim_var_tv ( VV_KEY ); 2775argv [ 1 ] = * get_vim_var_tv ( VV_VAL ); 2776typval_T newtv ; 2777newtv . v_type = VAR_UNKNOWN ; 2778 2779if ( eval_expr_typval ( expr , false, argv , 2 , & newtv ) == FAIL ) { 2780return false; 2781} 2782 2783bool error = false; 2784varnumber_T found = tv_get_bool_chk ( & newtv , & error ); 2785tv_clear ( & newtv ); 2786 2787return error ? false : found ; 2788} 2789 2790/// Evaluate "expr" for each byte in the Blob "b" starting with the byte at 2791/// "startidx" and return the index of the byte where "expr" is TRUE. Returns 2792/// -1 if "expr" doesn't evaluate to TRUE for any of the bytes. 2793static varnumber_T indexof_blob ( blob_T * b , varnumber_T startidx , typval_T * expr ) 2794{ 2795if ( b == NULL ) { 2796return -1 ; 2797} 2798 2799if ( startidx < 0 ) { 2800// negative index: index from the last byte 2801startidx = tv_blob_len ( b ) + startidx ; 2802if ( startidx < 0 ) { 2803startidx = 0 ; 2804} 2805} 2806 2807set_vim_var_type ( VV_KEY , VAR_NUMBER ); 2808set_vim_var_type ( VV_VAL , VAR_NUMBER ); 2809 2810const int called_emsg_start = called_emsg ; 2811for ( varnumber_T idx = startidx ; idx < tv_blob_len ( b ); idx ++ ) { 2812set_vim_var_nr ( VV_KEY , idx ); 2813set_vim_var_nr ( VV_VAL , tv_blob_get ( b , ( int ) idx )); 2814 2815if ( indexof_eval_expr ( expr )) { 2816return idx ; 2817} 2818 2819if ( called_emsg != called_emsg_start ) { 2820return -1 ; 2821} 2822} 2823 2824return -1 ; 2825} 2826 2827/// Evaluate "expr" for each item in the List "l" starting with the item at 2828/// "startidx" and return the index of the item where "expr" is TRUE. Returns 2829/// -1 if "expr" doesn't evaluate to TRUE for any of the items. 2830static varnumber_T indexof_list ( list_T * l , varnumber_T startidx , typval_T * expr ) 2831{ 2832if ( l == NULL ) { 2833return -1 ; 2834} 2835 2836listitem_T * item ; 2837varnumber_T idx = 0 ; 2838if ( startidx == 0 ) { 2839item = tv_list_first ( l ); 2840} else { 2841// Start at specified item. 2842idx = tv_list_uidx ( l , ( int ) startidx ); 2843if ( idx == -1 ) { 2844item = NULL ; 2845} else { 2846item = tv_list_find ( l , ( int ) idx ); 2847assert ( item != NULL ); 2848} 2849} 2850 2851set_vim_var_type ( VV_KEY , VAR_NUMBER ); 2852 2853const int called_emsg_start = called_emsg ; 2854for (; item != NULL ; item = TV_LIST_ITEM_NEXT ( l , item ), idx ++ ) { 2855set_vim_var_nr ( VV_KEY , idx ); 2856tv_copy ( TV_LIST_ITEM_TV ( item ), get_vim_var_tv ( VV_VAL )); 2857 2858bool found = indexof_eval_expr ( expr ); 2859tv_clear ( get_vim_var_tv ( VV_VAL )); 2860 2861if ( found ) { 2862return idx ; 2863} 2864 2865if ( called_emsg != called_emsg_start ) { 2866return -1 ; 2867} 2868} 2869 2870return -1 ; 2871} 2872 2873/// "indexof()" function 2874static void f_indexof ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2875{ 2876rettv -> vval . v_number = -1 ; 2877 2878if ( tv_check_for_list_or_blob_arg ( argvars , 0 ) == FAIL 2879|| tv_check_for_string_or_func_arg ( argvars , 1 ) == FAIL 2880|| tv_check_for_opt_dict_arg ( argvars , 2 ) == FAIL ) { 2881return; 2882} 2883 2884if (( argvars [ 1 ]. v_type == VAR_STRING 2885&& ( argvars [ 1 ]. vval . v_string == NULL || * argvars [ 1 ]. vval . v_string == NUL )) 2886|| ( argvars [ 1 ]. v_type == VAR_FUNC && argvars [ 1 ]. vval . v_partial == NULL )) { 2887return; 2888} 2889 2890varnumber_T startidx = 0 ; 2891if ( argvars [ 2 ]. v_type == VAR_DICT ) { 2892startidx = tv_dict_get_number_def ( argvars [ 2 ]. vval . v_dict , "startidx" , 0 ); 2893} 2894 2895typval_T save_val ; 2896typval_T save_key ; 2897prepare_vimvar ( VV_VAL , & save_val ); 2898prepare_vimvar ( VV_KEY , & save_key ); 2899 2900// We reset "did_emsg" to be able to detect whether an error occurred 2901// during evaluation of the expression. 2902const int save_did_emsg = did_emsg ; 2903did_emsg = false; 2904 2905if ( argvars [ 0 ]. v_type == VAR_BLOB ) { 2906rettv -> vval . v_number = indexof_blob ( argvars [ 0 ]. vval . v_blob , startidx , & argvars [ 1 ]); 2907} else { 2908rettv -> vval . v_number = indexof_list ( argvars [ 0 ]. vval . v_list , startidx , & argvars [ 1 ]); 2909} 2910 2911restore_vimvar ( VV_KEY , & save_key ); 2912restore_vimvar ( VV_VAL , & save_val ); 2913did_emsg |= save_did_emsg ; 2914} 2915 2916static bool inputsecret_flag = false; 2917 2918/// "input()" function 2919/// Also handles inputsecret() when inputsecret is set. 2920static void f_input ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2921{ 2922get_user_input ( argvars , rettv , false, inputsecret_flag ); 2923} 2924 2925/// "inputdialog()" function 2926static void f_inputdialog ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2927{ 2928get_user_input ( argvars , rettv , true, inputsecret_flag ); 2929} 2930 2931/// "inputlist()" function 2932static void f_inputlist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2933{ 2934if ( argvars [ 0 ]. v_type != VAR_LIST ) { 2935semsg ( _ ( e_listarg ), "inputlist()" ); 2936return; 2937} 2938 2939msg_ext_set_kind ( "confirm" ); 2940msg_start (); 2941msg_row = Rows - 1 ; // for when 'cmdheight' > 1 2942lines_left = Rows ; // avoid more prompt 2943msg_scroll = true; 2944msg_clr_eos (); 2945 2946list_T * l = argvars [ 0 ]. vval . v_list ; 2947TV_LIST_ITER_CONST ( l , li , { 2948msg_puts ( tv_get_string ( TV_LIST_ITEM_TV ( li ))); 2949if (! ui_has ( kUIMessages ) || TV_LIST_ITEM_NEXT ( l , li ) != NULL ) { 2950msg_putchar ( '\n' ); 2951} 2952}); 2953 2954// Ask for choice. 2955bool mouse_used = false; 2956int selected = prompt_for_input ( NULL , 0 , false, & mouse_used ); 2957if ( mouse_used ) { 2958selected = tv_list_len ( l ) - ( cmdline_row - mouse_row ); 2959} 2960 2961rettv -> vval . v_number = selected ; 2962} 2963 2964static garray_T ga_userinput = { 0 , 0 , sizeof( tasave_T ), 4 , NULL }; 2965 2966/// "inputrestore()" function 2967static void f_inputrestore ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2968{ 2969if (! GA_EMPTY ( & ga_userinput )) { 2970ga_userinput . ga_len -- ; 2971restore_typeahead (( tasave_T * )( ga_userinput . ga_data ) 2972+ ga_userinput . ga_len ); 2973// default return is zero == OK 2974} else if ( p_verbose > 1 ) { 2975verb_msg ( _ ( "called inputrestore() more often than inputsave()" )); 2976rettv -> vval . v_number = 1 ; // Failed 2977} 2978} 2979 2980/// "inputsave()" function 2981static void f_inputsave ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2982{ 2983// Add an entry to the stack of typeahead storage. 2984tasave_T * p = GA_APPEND_VIA_PTR ( tasave_T , & ga_userinput ); 2985save_typeahead ( p ); 2986} 2987 2988/// "inputsecret()" function 2989static void f_inputsecret ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 2990{ 2991cmdline_star ++ ; 2992inputsecret_flag = true; 2993f_input ( argvars , rettv , fptr ); 2994cmdline_star -- ; 2995inputsecret_flag = false; 2996} 2997 2998/// "interrupt()" function 2999static void f_interrupt ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3000{ 3001got_int = true; 3002} 3003 3004/// "invert(expr)" function 3005static void f_invert ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3006{ 3007rettv -> vval . v_number = ~ tv_get_number_chk ( & argvars [ 0 ], NULL ); 3008} 3009 3010/// "islocked()" function 3011static void f_islocked ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3012{ 3013lval_T lv ; 3014 3015rettv -> vval . v_number = -1 ; 3016const char * const end = get_lval (( char * ) tv_get_string ( & argvars [ 0 ]), 3017NULL , 3018& lv , false, false, 3019GLV_NO_AUTOLOAD | GLV_READ_ONLY , 3020FNE_CHECK_START ); 3021if ( end != NULL && lv . ll_name != NULL ) { 3022if ( * end != NUL ) { 3023semsg ( _ ( lv . ll_name_len == 0 ? e_invarg2 : e_trailing_arg ), end ); 3024} else { 3025if ( lv . ll_tv == NULL ) { 3026dictitem_T * di = find_var ( lv . ll_name , lv . ll_name_len , NULL , true); 3027if ( di != NULL ) { 3028// Consider a variable locked when: 3029// 1. the variable itself is locked 3030// 2. the value of the variable is locked. 3031// 3. the List or Dict value is locked. 3032rettv -> vval . v_number = (( di -> di_flags & DI_FLAGS_LOCK ) 3033|| tv_islocked ( & di -> di_tv )); 3034} 3035} else if ( lv . ll_range ) { 3036emsg ( _ ( "E786: Range not allowed" )); 3037} else if ( lv . ll_newkey != NULL ) { 3038semsg ( _ ( e_dictkey ), lv . ll_newkey ); 3039} else if ( lv . ll_list != NULL ) { 3040// List item. 3041rettv -> vval . v_number = tv_islocked ( TV_LIST_ITEM_TV ( lv . ll_li )); 3042} else { 3043// Dictionary item. 3044rettv -> vval . v_number = tv_islocked ( & lv . ll_di -> di_tv ); 3045} 3046} 3047} 3048 3049clear_lval ( & lv ); 3050} 3051 3052/// "isinf()" function 3053static void f_isinf ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3054{ 3055if ( argvars [ 0 ]. v_type == VAR_FLOAT 3056&& xisinf ( argvars [ 0 ]. vval . v_float )) { 3057rettv -> vval . v_number = argvars [ 0 ]. vval . v_float > 0.0 ? 1 : -1 ; 3058} 3059} 3060 3061/// "isnan()" function 3062static void f_isnan ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3063{ 3064rettv -> vval . v_number = argvars [ 0 ]. v_type == VAR_FLOAT 3065&& xisnan ( argvars [ 0 ]. vval . v_float ); 3066} 3067 3068/// "id()" function 3069static void f_id ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3070FUNC_ATTR_NONNULL_ALL 3071{ 3072const int len = vim_vsnprintf_typval ( NULL , 0 , "%p" , dummy_ap , argvars ); 3073rettv -> v_type = VAR_STRING ; 3074rettv -> vval . v_string = xmalloc (( size_t ) len + 1 ); 3075vim_vsnprintf_typval ( rettv -> vval . v_string , ( size_t ) len + 1 , "%p" , dummy_ap , argvars ); 3076} 3077 3078/// "jobpid(id)" function 3079static void f_jobpid ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3080{ 3081rettv -> v_type = VAR_NUMBER ; 3082rettv -> vval . v_number = 0 ; 3083 3084if ( check_secure ()) { 3085return; 3086} 3087 3088if ( argvars [ 0 ]. v_type != VAR_NUMBER ) { 3089emsg ( _ ( e_invarg )); 3090return; 3091} 3092 3093Channel * data = find_job (( uint64_t ) argvars [ 0 ]. vval . v_number , true); 3094if (! data ) { 3095return; 3096} 3097 3098Proc * proc = & data -> stream . proc ; 3099rettv -> vval . v_number = proc -> pid ; 3100} 3101 3102/// "jobresize(job, width, height)" function 3103static void f_jobresize ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3104{ 3105rettv -> v_type = VAR_NUMBER ; 3106rettv -> vval . v_number = 0 ; 3107 3108if ( check_secure ()) { 3109return; 3110} 3111 3112if ( argvars [ 0 ]. v_type != VAR_NUMBER || argvars [ 1 ]. v_type != VAR_NUMBER 3113|| argvars [ 2 ]. v_type != VAR_NUMBER ) { 3114// job id, width, height 3115emsg ( _ ( e_invarg )); 3116return; 3117} 3118 3119Channel * data = find_job (( uint64_t ) argvars [ 0 ]. vval . v_number , true); 3120if (! data ) { 3121return; 3122} 3123 3124if ( data -> stream . proc . type != kProcTypePty ) { 3125emsg ( _ ( e_channotpty )); 3126return; 3127} 3128 3129pty_proc_resize ( & data -> stream . pty , ( uint16_t ) argvars [ 1 ]. vval . v_number , 3130( uint16_t ) argvars [ 2 ]. vval . v_number ); 3131rettv -> vval . v_number = 1 ; 3132} 3133 3134static const char * pty_ignored_env_vars [] = { 3135#ifndef MSWIN 3136"COLUMNS" , 3137"LINES" , 3138"TERMCAP" , 3139"COLORFGBG" , 3140#endif 3141"COLORTERM" , 3142// Nvim-owned env vars. #6764 3143"VIM" , 3144"VIMRUNTIME" , 3145NULL 3146}; 3147 3148/// According to comments in src/win/process.c of libuv, Windows has a few 3149/// "essential" environment variables. 3150static const char * required_env_vars [] = { 3151#ifdef MSWIN 3152"HOMEDRIVE" , 3153"HOMEPATH" , 3154"LOGONSERVER" , 3155"PATH" , 3156"SYSTEMDRIVE" , 3157"SYSTEMROOT" , 3158"TEMP" , 3159"USERDOMAIN" , 3160"USERNAME" , 3161"USERPROFILE" , 3162"WINDIR" , 3163#endif 3164NULL 3165}; 3166 3167/// Builds an environment dict for a child process (job). 3168/// 3169/// @param set_nvim_addr Set the $NVIM env var. 3170dict_T * create_environment (const dictitem_T * job_env , const bool clear_env , const bool pty , 3171const bool set_nvim_addr , const char * const pty_term_name ) 3172{ 3173dict_T * env = tv_dict_alloc (); 3174 3175if (! clear_env ) { 3176uv_env_item_t * envitems ; 3177int envcount ; 3178if ( uv_os_environ ( & envitems , & envcount ) == 0 ) { 3179for ( int i = 0 ; i < envcount ; i ++ ) { 3180const char * name = envitems [ i ]. name ; 3181size_t namelen = strlen ( name ); 3182#ifdef MSWIN 3183// Force uppercase keys on Windows (for dedup, below). #39443 3184if ( namelen < MAXPATHL / MB_MAXBYTES ) { 3185namelen = mb_strup_buf ( name , NameBuff ); 3186name = NameBuff ; 3187} 3188#endif 3189tv_dict_add_str ( env , name , namelen , envitems [ i ]. value ); 3190} 3191uv_os_free_environ ( envitems , envcount ); 3192} 3193 3194if ( pty ) { 3195// These env vars shouldn't propagate to the child process. #6764 3196// Remove them here, then the user may decide to explicitly set them below. 3197for ( size_t i = 0 ; 3198i < ARRAY_SIZE ( pty_ignored_env_vars ) && pty_ignored_env_vars [ i ]; 3199i ++ ) { 3200dictitem_T * dv = tv_dict_find ( env , pty_ignored_env_vars [ i ], -1 ); 3201if ( dv ) { 3202tv_dict_item_remove ( env , dv ); 3203} 3204} 3205// Set COLORTERM to "truecolor" if termguicolors is set 3206if ( p_tgc ) { 3207tv_dict_add_str ( env , S_LEN ( "COLORTERM" ), "truecolor" ); 3208} 3209} 3210} 3211 3212// For a pty, we need a sane $TERM set. We can't rely on nvim's environment, 3213// because the child process is going to be communicating with nvim, not the 3214// parent terminal. Set a sane default, but let the user override it in the 3215// job's environment if they want. 3216if ( pty ) { 3217dictitem_T * dv = tv_dict_find ( env , S_LEN ( "TERM" )); 3218if ( dv ) { 3219tv_dict_item_remove ( env , dv ); 3220} 3221tv_dict_add_str ( env , S_LEN ( "TERM" ), pty_term_name ); 3222} 3223 3224// Set $NVIM (in the child process) to v:servername. #3118 3225if ( set_nvim_addr ) { 3226char * nvim_addr = get_vim_var_str ( VV_SEND_SERVER ); 3227if ( nvim_addr [ 0 ] != NUL ) { 3228dictitem_T * dv = tv_dict_find ( env , S_LEN ( "NVIM" )); 3229if ( dv ) { 3230tv_dict_item_remove ( env , dv ); 3231} 3232tv_dict_add_str ( env , S_LEN ( "NVIM" ), nvim_addr ); 3233} 3234} 3235 3236if ( job_env ) { 3237#ifdef MSWIN 3238TV_DICT_ITER ( job_env -> di_tv . vval . v_dict , var , { 3239// Force uppercase keys on Windows (dedup). 3240const char * name = var -> di_key ; 3241size_t namelen = strlen ( name ); 3242if ( namelen < MAXPATHL / MB_MAXBYTES ) { 3243namelen = mb_strup_buf ( name , NameBuff ); 3244name = NameBuff ; 3245} 3246dictitem_T * dv = tv_dict_find ( env , name , ( ptrdiff_t ) namelen ); 3247if ( dv ) { 3248tv_dict_item_remove ( env , dv ); 3249} 3250tv_dict_add_str ( env , name , namelen , tv_get_string ( & var -> di_tv )); 3251}); 3252#else 3253tv_dict_extend ( env , job_env -> di_tv . vval . v_dict , "force" ); 3254#endif 3255} 3256 3257if ( pty ) { 3258// Now that the custom environment is configured, we need to ensure certain 3259// environment variables are present. 3260for ( size_t i = 0 ; 3261i < ARRAY_SIZE ( required_env_vars ) && required_env_vars [ i ]; 3262i ++ ) { 3263size_t len = strlen ( required_env_vars [ i ]); 3264dictitem_T * dv = tv_dict_find ( env , required_env_vars [ i ], ( ptrdiff_t ) len ); 3265if (! dv ) { 3266char * env_var = os_getenv ( required_env_vars [ i ]); 3267if ( env_var ) { 3268tv_dict_add_allocated_str ( env , required_env_vars [ i ], len , env_var ); 3269} 3270} 3271} 3272} 3273 3274return env ; 3275} 3276 3277/// "jobstart()" function 3278void f_jobstart ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3279{ 3280rettv -> v_type = VAR_NUMBER ; 3281rettv -> vval . v_number = 0 ; 3282 3283if ( check_secure ()) { 3284return; 3285} 3286 3287const char * cmd ; 3288bool executable = true; 3289char ** argv = tv_to_argv ( & argvars [ 0 ], & cmd , & executable ); 3290if (! argv ) { 3291rettv -> vval . v_number = executable ? 0 : -1 ; 3292return; // Did error message in tv_to_argv. 3293} 3294 3295if ( argvars [ 1 ]. v_type != VAR_DICT && argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 3296// Wrong argument types 3297semsg ( _ ( e_invarg2 ), "expected dictionary" ); 3298shell_free_argv ( argv ); 3299return; 3300} 3301 3302dict_T * job_opts = NULL ; 3303bool detach = false; 3304bool rpc = false; 3305bool pty = false; 3306bool term = false; 3307bool clear_env = false; 3308bool overlapped = false; 3309ChannelStdinMode stdin_mode = kChannelStdinPipe ; 3310CallbackReader on_stdout = CALLBACK_READER_INIT ; 3311CallbackReader on_stderr = CALLBACK_READER_INIT ; 3312Callback on_exit = CALLBACK_NONE ; 3313char * cwd = NULL ; 3314dictitem_T * job_env = NULL ; 3315if ( argvars [ 1 ]. v_type == VAR_DICT ) { 3316job_opts = argvars [ 1 ]. vval . v_dict ; 3317 3318detach = tv_dict_get_number ( job_opts , "detach" ) != 0 ; 3319rpc = tv_dict_get_number ( job_opts , "rpc" ) != 0 ; 3320term = tv_dict_get_number ( job_opts , "term" ) != 0 ; 3321pty = term || tv_dict_get_number ( job_opts , "pty" ) != 0 ; 3322clear_env = tv_dict_get_number ( job_opts , "clear_env" ) != 0 ; 3323overlapped = tv_dict_get_number ( job_opts , "overlapped" ) != 0 ; 3324 3325char * s = tv_dict_get_string ( job_opts , "stdin" , false); 3326if ( s ) { 3327if (! strncmp ( s , "null" , NUMBUFLEN )) { 3328stdin_mode = kChannelStdinNull ; 3329} else if (! strncmp ( s , "pipe" , NUMBUFLEN )) { 3330// Nothing to do, default value 3331} else { 3332semsg ( _ ( e_invargNval ), "stdin" , s ); 3333} 3334} 3335 3336dictitem_T * const job_term = tv_dict_find ( job_opts , S_LEN ( "term" )); 3337if ( job_term && VAR_BOOL != job_term -> di_tv . v_type ) { 3338// Restrict "term" field to boolean, in case we want to allow buffer numbers in the future. 3339semsg ( _ ( e_invarg2 ), "'term' must be Boolean" ); 3340shell_free_argv ( argv ); 3341return; 3342} 3343 3344if ( pty && rpc ) { 3345semsg ( _ ( e_invarg2 ), "job cannot have both 'pty' and 'rpc' options set" ); 3346shell_free_argv ( argv ); 3347return; 3348} 3349 3350#ifdef MSWIN 3351if ( pty && overlapped ) { 3352semsg ( _ ( e_invarg2 ), 3353"job cannot have both 'pty' and 'overlapped' options set" ); 3354shell_free_argv ( argv ); 3355return; 3356} 3357#endif 3358 3359char * new_cwd = tv_dict_get_string ( job_opts , "cwd" , false); 3360if ( new_cwd && * new_cwd != NUL ) { 3361cwd = new_cwd ; 3362// The new cwd must be a directory. 3363if (! os_isdir ( cwd )) { 3364semsg ( _ ( e_invarg2 ), "expected valid directory" ); 3365shell_free_argv ( argv ); 3366return; 3367} 3368} 3369 3370job_env = tv_dict_find ( job_opts , S_LEN ( "env" )); 3371if ( job_env && job_env -> di_tv . v_type != VAR_DICT ) { 3372semsg ( _ ( e_invarg2 ), "env" ); 3373shell_free_argv ( argv ); 3374return; 3375} 3376 3377if (! common_job_callbacks ( job_opts , & on_stdout , & on_stderr , & on_exit )) { 3378shell_free_argv ( argv ); 3379return; 3380} 3381} 3382 3383uint16_t width = ( uint16_t ) tv_dict_get_number ( job_opts , "width" ); 3384uint16_t height = ( uint16_t ) tv_dict_get_number ( job_opts , "height" ); 3385char * term_name = NULL ; 3386 3387if ( term ) { 3388if ( text_locked ()) { 3389text_locked_msg (); 3390shell_free_argv ( argv ); 3391return; 3392} 3393if ( curbuf -> b_changed ) { 3394emsg ( _ ( "jobstart(...,{term=true}) requires unmodified buffer" )); 3395shell_free_argv ( argv ); 3396return; 3397} 3398if ( curbuf -> terminal ) { 3399if ( terminal_running ( curbuf -> terminal )) { 3400semsg ( _ ( "Terminal already connected to buffer %d" ), curbuf -> handle ); 3401shell_free_argv ( argv ); 3402return; 3403} 3404buf_close_terminal ( curbuf ); 3405} 3406assert (! rpc ); 3407term_name = "xterm-256color" ; 3408cwd = cwd ? cwd : "." ; 3409overlapped = false; 3410detach = false; 3411stdin_mode = kChannelStdinPipe ; 3412width = width ? width : ( uint16_t ) MAX ( 0 , curwin -> w_view_width - win_col_off ( curwin )); 3413height = height ? height : ( uint16_t ) curwin -> w_view_height ; 3414} 3415 3416if ( pty ) { 3417// Deprecated TERM field is from before `env` option existed. 3418term_name = term_name ? term_name : tv_dict_get_string ( job_opts , "TERM" , false); 3419term_name = term_name ? term_name : "ansi" ; 3420} 3421 3422dict_T * env = create_environment ( job_env , clear_env , pty , true, term_name ); 3423Channel * chan = channel_job_start ( argv , NULL , on_stdout , on_stderr , on_exit , pty , 3424rpc , overlapped , detach , stdin_mode , cwd , 3425width , height , env , & rettv -> vval . v_number ); 3426if (! chan ) { 3427return; 3428} else if (! term ) { 3429channel_create_event ( chan , NULL ); 3430} else { 3431if ( rettv -> vval . v_number <= 0 ) { 3432return; 3433} 3434 3435const int pid = chan -> stream . pty . proc . pid ; 3436buf_T * const buf = curbuf ; 3437 3438// Unset 'swapfile' to ensure no swapfile is created. 3439buf -> b_p_swf = false; 3440// If the buffer isn't loaded, open a memfile here to avoid spurious autocommands 3441// from open_buffer() when updating the terminal buffer later. 3442if ( buf -> b_ml . ml_mfp == NULL && ml_open ( buf ) == FAIL ) { 3443// Internal error in ml_open(): stop the job. 3444proc_stop ( & chan -> stream . proc ); 3445channel_decref ( chan ); 3446return; 3447} 3448 3449channel_incref ( chan ); 3450channel_terminal_alloc ( buf , chan ); 3451 3452apply_autocmds ( EVENT_BUFFILEPRE , NULL , NULL , false, buf ); 3453 3454if ( chan -> term == NULL || terminal_buf ( chan -> term ) == 0 ) { 3455goto term_done ; // Terminal may be destroyed during autocommands. 3456} 3457 3458// "./…" => "/home/foo/…" 3459vim_FullName ( cwd , NameBuff , sizeof( NameBuff ), false); 3460// "/home/foo/…" => "~/…" 3461size_t len = home_replace ( NULL , NameBuff , IObuff , sizeof( IObuff ), true); 3462// Trim slash. 3463if ( len != 1 && ( IObuff [ len - 1 ] == '\\' || IObuff [ len - 1 ] == '/' )) { 3464IObuff [ len - 1 ] = NUL ; 3465} 3466 3467if ( len == 1 && IObuff [ 0 ] == '/' ) { 3468// Avoid ambiguity in the URI when CWD is root directory. 3469IObuff [ 1 ] = '.' ; 3470IObuff [ 2 ] = NUL ; 3471} 3472 3473// Terminal URI: "term://$CWD//$PID:$CMD" 3474snprintf ( NameBuff , sizeof( NameBuff ), "term://%s//%d:%s" , IObuff , pid , cmd ); 3475 3476setfname ( buf , NameBuff , NULL , true); 3477apply_autocmds ( EVENT_BUFFILEPOST , NULL , NULL , false, buf ); 3478 3479if ( chan -> term == NULL || terminal_buf ( chan -> term ) == 0 ) { 3480goto term_done ; // Terminal may be destroyed during autocommands. 3481} 3482 3483Error err = ERROR_INIT ; 3484buf -> b_locked ++ ; 3485// Set (deprecated) buffer-local vars (prefer 'channel' buffer-local option). 3486dict_set_var ( buf -> b_vars , cstr_as_string ( "terminal_job_id" ), 3487INTEGER_OBJ (( Integer ) chan -> id ), false, false, NULL , & err ); 3488api_clear_error ( & err ); 3489dict_set_var ( buf -> b_vars , cstr_as_string ( "terminal_job_pid" ), 3490INTEGER_OBJ ( pid ), false, false, NULL , & err ); 3491api_clear_error ( & err ); 3492buf -> b_locked -- ; 3493 3494if ( chan -> term == NULL || terminal_buf ( chan -> term ) == 0 ) { 3495goto term_done ; // Terminal may be destroyed in dict watchers. 3496} 3497 3498terminal_open ( & chan -> term , buf ); 3499term_done : 3500channel_create_event ( chan , NULL ); 3501channel_decref ( chan ); 3502} 3503} 3504 3505/// "jobstop()" function 3506void f_jobstop ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3507{ 3508rettv -> v_type = VAR_NUMBER ; 3509rettv -> vval . v_number = 0 ; 3510 3511if ( check_secure ()) { 3512return; 3513} 3514 3515if ( argvars [ 0 ]. v_type != VAR_NUMBER ) { 3516// Only argument is the job id 3517emsg ( _ ( e_invarg )); 3518return; 3519} 3520 3521Channel * data = find_job (( uint64_t ) argvars [ 0 ]. vval . v_number , false); 3522if (! data ) { 3523return; 3524} 3525 3526const char * error = NULL ; 3527if ( data -> is_rpc ) { 3528// Ignore return code, but show error later. 3529channel_close ( data -> id , kChannelPartRpc , & error ); 3530} 3531proc_stop ( & data -> stream . proc ); 3532rettv -> vval . v_number = 1 ; 3533if ( error ) { 3534emsg ( error ); 3535} 3536} 3537 3538/// "jobwait(ids[, timeout])" function 3539static void f_jobwait ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3540{ 3541rettv -> v_type = VAR_NUMBER ; 3542rettv -> vval . v_number = 0 ; 3543 3544if ( check_secure ()) { 3545return; 3546} 3547if ( argvars [ 0 ]. v_type != VAR_LIST || ( argvars [ 1 ]. v_type != VAR_NUMBER 3548&& argvars [ 1 ]. v_type != VAR_UNKNOWN )) { 3549emsg ( _ ( e_invarg )); 3550return; 3551} 3552 3553list_T * args = argvars [ 0 ]. vval . v_list ; 3554Channel ** jobs = xcalloc (( size_t ) tv_list_len ( args ), sizeof( * jobs )); 3555MultiQueue * waiting_jobs = multiqueue_new ( loop_on_put , & main_loop ); 3556 3557// Validate, prepare jobs for waiting. 3558int i = 0 ; 3559TV_LIST_ITER_CONST ( args , arg , { 3560Channel * chan = NULL ; 3561if ( TV_LIST_ITEM_TV ( arg ) -> v_type != VAR_NUMBER 3562|| !( chan = find_channel (( uint64_t ) TV_LIST_ITEM_TV ( arg ) -> vval . v_number )) 3563|| chan -> streamtype != kChannelStreamProc ) { 3564jobs [ i ] = NULL ; // Invalid job. 3565} else if ( proc_is_stopped ( & chan -> stream . proc )) { 3566// Job is stopped but not fully destroyed. 3567// Ensure all callbacks on its event queue are executed. #15402 3568proc_wait ( & chan -> stream . proc , -1 , NULL ); 3569jobs [ i ] = NULL ; // Invalid job. 3570} else { 3571jobs [ i ] = chan ; 3572channel_incref ( chan ); 3573if ( chan -> stream . proc . status < 0 ) { 3574// Flush any events in the job's queue before temporarily replacing it. 3575multiqueue_process_events ( chan -> events ); 3576multiqueue_replace_parent ( chan -> events , waiting_jobs ); 3577} 3578} 3579i ++ ; 3580}); 3581 3582int remaining = -1 ; 3583uint64_t before = 0 ; 3584if ( argvars [ 1 ]. v_type == VAR_NUMBER && argvars [ 1 ]. vval . v_number >= 0 ) { 3585remaining = ( int ) argvars [ 1 ]. vval . v_number ; 3586before = os_hrtime (); 3587} 3588 3589// Only mark the UI as busy when jobwait() blocks 3590const bool busy = remaining != 0 ; 3591if ( busy ) { 3592ui_busy_start (); 3593ui_flush (); 3594} 3595 3596for ( i = 0 ; i < tv_list_len ( args ); i ++ ) { 3597if ( remaining == 0 ) { 3598break; // Timeout. 3599} 3600if ( jobs [ i ] == NULL ) { 3601continue; // Invalid job, will assign status=-3 below. 3602} 3603int status = proc_wait ( & jobs [ i ] -> stream . proc , remaining , 3604waiting_jobs ); 3605if ( status < 0 ) { 3606break; // Interrupted (CTRL-C) or timeout, skip remaining jobs. 3607} 3608if ( remaining > 0 ) { 3609uint64_t now = os_hrtime (); 3610remaining = MIN ( 0 , remaining - ( int )(( now - before ) / 1000000 )); 3611before = now ; 3612} 3613} 3614 3615list_T * const rv = tv_list_alloc ( tv_list_len ( args )); 3616 3617// For each job: 3618// * Restore its parent queue if the job is still alive. 3619// * Append its status to the output list, or: 3620// -3 for "invalid job id" 3621// -2 for "interrupted" (user hit CTRL-C) 3622// -1 for jobs that were skipped or timed out 3623for ( i = 0 ; i < tv_list_len ( args ); i ++ ) { 3624if ( jobs [ i ] == NULL ) { 3625tv_list_append_number ( rv , -3 ); 3626continue; 3627} 3628multiqueue_process_events ( jobs [ i ] -> events ); 3629multiqueue_replace_parent ( jobs [ i ] -> events , main_loop . events ); 3630 3631tv_list_append_number ( rv , jobs [ i ] -> stream . proc . status ); 3632channel_decref ( jobs [ i ]); 3633} 3634 3635multiqueue_free ( waiting_jobs ); 3636xfree ( jobs ); 3637if ( busy ) { 3638ui_busy_stop (); 3639} 3640tv_list_ref ( rv ); 3641rettv -> v_type = VAR_LIST ; 3642rettv -> vval . v_list = rv ; 3643} 3644 3645/// json_decode() function 3646static void f_json_decode ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3647{ 3648char numbuf [ NUMBUFLEN ]; 3649const char * s = NULL ; 3650char * tofree = NULL ; 3651size_t len ; 3652if ( argvars [ 0 ]. v_type == VAR_LIST ) { 3653if (! encode_vim_list_to_buf ( argvars [ 0 ]. vval . v_list , & len , & tofree )) { 3654emsg ( _ ( "E474: Failed to convert list to string" )); 3655return; 3656} 3657s = tofree ; 3658if ( s == NULL ) { 3659assert ( len == 0 ); 3660s = "" ; 3661} 3662} else { 3663s = tv_get_string_buf_chk ( & argvars [ 0 ], numbuf ); 3664if ( s ) { 3665len = strlen ( s ); 3666} else { 3667return; 3668} 3669} 3670if ( json_decode_string ( s , len , rettv ) == FAIL ) { 3671semsg ( _ ( "E474: Failed to parse %.*s" ), ( int ) len , s ); 3672rettv -> v_type = VAR_NUMBER ; 3673rettv -> vval . v_number = 0 ; 3674} 3675assert ( rettv -> v_type != VAR_UNKNOWN ); 3676xfree ( tofree ); 3677} 3678 3679/// json_encode() function 3680static void f_json_encode ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3681{ 3682rettv -> v_type = VAR_STRING ; 3683rettv -> vval . v_string = encode_tv2json ( & argvars [ 0 ], NULL ); 3684} 3685 3686/// "keytrans()" function 3687static void f_keytrans ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3688{ 3689rettv -> v_type = VAR_STRING ; 3690if ( tv_check_for_string_arg ( argvars , 0 ) == FAIL 3691|| argvars [ 0 ]. vval . v_string == NULL ) { 3692return; 3693} 3694// Need to escape K_SPECIAL for mb_unescape(). 3695char * escaped = vim_strsave_escape_ks ( argvars [ 0 ]. vval . v_string ); 3696rettv -> vval . v_string = str2special_save ( escaped , true, true); 3697xfree ( escaped ); 3698} 3699 3700/// "len()" function 3701static void f_len ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3702{ 3703switch ( argvars [ 0 ]. v_type ) { 3704case VAR_STRING : 3705case VAR_NUMBER : 3706rettv -> vval . v_number = ( varnumber_T ) strlen ( tv_get_string ( & argvars [ 0 ])); 3707break; 3708case VAR_BLOB : 3709rettv -> vval . v_number = tv_blob_len ( argvars [ 0 ]. vval . v_blob ); 3710break; 3711case VAR_LIST : 3712rettv -> vval . v_number = tv_list_len ( argvars [ 0 ]. vval . v_list ); 3713break; 3714case VAR_DICT : 3715rettv -> vval . v_number = tv_dict_len ( argvars [ 0 ]. vval . v_dict ); 3716break; 3717case VAR_UNKNOWN : 3718case VAR_BOOL : 3719case VAR_SPECIAL : 3720case VAR_FLOAT : 3721case VAR_PARTIAL : 3722case VAR_FUNC : 3723emsg ( _ ( "E701: Invalid type for len()" )); 3724break; 3725} 3726} 3727 3728static void libcall_common ( typval_T * argvars , typval_T * rettv , int out_type ) 3729{ 3730rettv -> v_type = ( VarType ) out_type ; 3731if ( out_type != VAR_NUMBER ) { 3732rettv -> vval . v_string = NULL ; 3733} 3734 3735if ( check_secure ()) { 3736return; 3737} 3738 3739// The first two args (libname and funcname) must be strings 3740if ( argvars [ 0 ]. v_type != VAR_STRING || argvars [ 1 ]. v_type != VAR_STRING ) { 3741return; 3742} 3743 3744const char * libname = argvars [ 0 ]. vval . v_string ; 3745const char * funcname = argvars [ 1 ]. vval . v_string ; 3746 3747VarType in_type = argvars [ 2 ]. v_type ; 3748 3749// input variables 3750char * str_in = ( in_type == VAR_STRING ) ? argvars [ 2 ]. vval . v_string : NULL ; 3751int int_in = ( int ) argvars [ 2 ]. vval . v_number ; 3752 3753// output variables 3754char ** str_out = ( out_type == VAR_STRING ) ? & rettv -> vval . v_string : NULL ; 3755int int_out = 0 ; 3756 3757bool success = os_libcall ( libname , funcname , 3758str_in , int_in , 3759str_out , & int_out ); 3760 3761if (! success ) { 3762semsg ( _ ( e_libcall ), funcname ); 3763return; 3764} 3765 3766if ( out_type == VAR_NUMBER ) { 3767rettv -> vval . v_number = ( varnumber_T ) int_out ; 3768} 3769} 3770 3771/// "libcall()" function 3772static void f_libcall ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3773{ 3774libcall_common ( argvars , rettv , VAR_STRING ); 3775} 3776 3777/// "libcallnr()" function 3778static void f_libcallnr ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3779{ 3780libcall_common ( argvars , rettv , VAR_NUMBER ); 3781} 3782 3783/// "line(string, [winid])" function 3784static void f_line ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3785{ 3786linenr_T lnum = 0 ; 3787pos_T * fp = NULL ; 3788int fnum ; 3789 3790if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 3791// use window specified in the second argument 3792int id = ( int ) tv_get_number ( & argvars [ 1 ]); 3793tabpage_T * tp ; 3794win_T * wp = win_id2wp_tp ( id , & tp ); 3795if ( wp != NULL && tp != NULL ) { 3796// With 'splitkeep' != cursor and in diff mode, prevent that the 3797// window scrolls and keep the topline. 3798if ( * p_spk != 'c' || ( wp -> w_p_diff && curwin -> w_p_diff )) { 3799skip_update_topline = true; 3800} 3801check_cursor ( wp ); 3802fp = var2fpos ( & argvars [ 0 ], true, & fnum , false, wp ); 3803skip_update_topline = false; 3804} 3805} else { 3806// use current window 3807fp = var2fpos ( & argvars [ 0 ], true, & fnum , false, curwin ); 3808} 3809 3810if ( fp != NULL ) { 3811lnum = fp -> lnum ; 3812} 3813rettv -> vval . v_number = lnum ; 3814} 3815 3816/// "line2byte(lnum)" function 3817static void f_line2byte ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3818{ 3819const linenr_T lnum = tv_get_lnum ( argvars ); 3820if ( lnum < 1 || lnum > curbuf -> b_ml . ml_line_count + 1 ) { 3821rettv -> vval . v_number = -1 ; 3822} else { 3823rettv -> vval . v_number = ml_find_line_or_offset ( curbuf , lnum , NULL , false); 3824} 3825if ( rettv -> vval . v_number >= 0 ) { 3826rettv -> vval . v_number ++ ; 3827} 3828} 3829 3830/// "localtime()" function 3831static void f_localtime ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3832{ 3833rettv -> vval . v_number = ( varnumber_T ) time ( NULL ); 3834} 3835 3836/// luaeval() function implementation 3837static void f_luaeval ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 3838FUNC_ATTR_NONNULL_ALL 3839{ 3840const char * const str = tv_get_string_chk ( & argvars [ 0 ]); 3841if ( str == NULL ) { 3842return; 3843} 3844 3845nlua_call_luaeval ( cstr_as_string ( str ), & argvars [ 1 ], rettv ); 3846} 3847 3848static void find_some_match ( typval_T * const argvars , typval_T * const rettv , 3849const SomeMatchType type ) 3850{ 3851char * str = NULL ; 3852int64_t len = 0 ; 3853char * expr = NULL ; 3854regmatch_T regmatch ; 3855int64_t start = 0 ; 3856int64_t nth = 1 ; 3857colnr_T startcol = 0 ; 3858bool match = false; 3859list_T * l = NULL ; 3860int idx = 0 ; 3861char * tofree = NULL ; 3862 3863// Make 'cpoptions' empty, the 'l' flag should not be used here. 3864char * save_cpo = p_cpo ; 3865p_cpo = empty_string_option ; 3866 3867rettv -> vval . v_number = -1 ; 3868switch ( type ) { 3869// matchlist(): return empty list when there are no matches. 3870case kSomeMatchList : 3871tv_list_alloc_ret ( rettv , kListLenMayKnow ); 3872break; 3873// matchstrpos(): return ["", -1, -1, -1] 3874case kSomeMatchStrPos : 3875tv_list_alloc_ret ( rettv , 4 ); 3876tv_list_append_string ( rettv -> vval . v_list , "" , 0 ); 3877tv_list_append_number ( rettv -> vval . v_list , -1 ); 3878tv_list_append_number ( rettv -> vval . v_list , -1 ); 3879tv_list_append_number ( rettv -> vval . v_list , -1 ); 3880break; 3881case kSomeMatchStr : 3882rettv -> v_type = VAR_STRING ; 3883rettv -> vval . v_string = NULL ; 3884break; 3885case kSomeMatch : 3886case kSomeMatchEnd : 3887// Do nothing: zero is default. 3888break; 3889} 3890 3891listitem_T * li = NULL ; 3892if ( argvars [ 0 ]. v_type == VAR_LIST ) { 3893if (( l = argvars [ 0 ]. vval . v_list ) == NULL ) { 3894goto theend ; 3895} 3896li = tv_list_first ( l ); 3897} else { 3898expr = str = ( char * ) tv_get_string ( & argvars [ 0 ]); 3899len = ( int64_t ) strlen ( str ); 3900} 3901 3902char patbuf [ NUMBUFLEN ]; 3903const char * const pat = tv_get_string_buf_chk ( & argvars [ 1 ], patbuf ); 3904if ( pat == NULL ) { 3905goto theend ; 3906} 3907 3908if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 3909bool error = false; 3910 3911start = tv_get_number_chk ( & argvars [ 2 ], & error ); 3912if ( error ) { 3913goto theend ; 3914} 3915if ( l != NULL ) { 3916idx = tv_list_uidx ( l , ( int ) start ); 3917if ( idx == -1 ) { 3918goto theend ; 3919} 3920li = tv_list_find ( l , idx ); 3921} else { 3922if ( start < 0 ) { 3923start = 0 ; 3924} 3925if ( start > len ) { 3926goto theend ; 3927} 3928// When "count" argument is there ignore matches before "start", 3929// otherwise skip part of the string. Differs when pattern is "^" 3930// or "\<". 3931if ( argvars [ 3 ]. v_type != VAR_UNKNOWN ) { 3932startcol = ( colnr_T ) start ; 3933} else { 3934str += start ; 3935len -= start ; 3936} 3937} 3938 3939if ( argvars [ 3 ]. v_type != VAR_UNKNOWN ) { 3940nth = tv_get_number_chk ( & argvars [ 3 ], & error ); 3941} 3942if ( error ) { 3943goto theend ; 3944} 3945} 3946 3947regmatch . regprog = vim_regcomp ( pat , RE_MAGIC + RE_STRING ); 3948if ( regmatch . regprog != NULL ) { 3949regmatch . rm_ic = p_ic ; 3950 3951while (true) { 3952if ( l != NULL ) { 3953if ( li == NULL ) { 3954match = false; 3955break; 3956} 3957xfree ( tofree ); 3958tofree = expr = str = encode_tv2echo ( TV_LIST_ITEM_TV ( li ), NULL ); 3959if ( str == NULL ) { 3960break; 3961} 3962} 3963 3964match = vim_regexec_nl ( & regmatch , str , startcol ); 3965 3966if ( match && -- nth <= 0 ) { 3967break; 3968} 3969if ( l == NULL && ! match ) { 3970break; 3971} 3972 3973// Advance to just after the match. 3974if ( l != NULL ) { 3975li = TV_LIST_ITEM_NEXT ( l , li ); 3976idx ++ ; 3977} else { 3978startcol = ( colnr_T )( regmatch . startp [ 0 ] 3979+ utfc_ptr2len ( regmatch . startp [ 0 ]) - str ); 3980if ( startcol > ( colnr_T ) len || str + startcol <= regmatch . startp [ 0 ]) { 3981match = false; 3982break; 3983} 3984} 3985} 3986 3987if ( match ) { 3988switch ( type ) { 3989case kSomeMatchStrPos : { 3990list_T * const ret_l = rettv -> vval . v_list ; 3991listitem_T * li1 = tv_list_first ( ret_l ); 3992listitem_T * li2 = TV_LIST_ITEM_NEXT ( ret_l , li1 ); 3993listitem_T * li3 = TV_LIST_ITEM_NEXT ( ret_l , li2 ); 3994listitem_T * li4 = TV_LIST_ITEM_NEXT ( ret_l , li3 ); 3995xfree ( TV_LIST_ITEM_TV ( li1 ) -> vval . v_string ); 3996 3997const size_t rd = ( size_t )( regmatch . endp [ 0 ] - regmatch . startp [ 0 ]); 3998TV_LIST_ITEM_TV ( li1 ) -> vval . v_string = xmemdupz ( regmatch . startp [ 0 ], rd ); 3999TV_LIST_ITEM_TV ( li3 ) -> vval . v_number = ( varnumber_T )( regmatch . startp [ 0 ] - expr ); 4000TV_LIST_ITEM_TV ( li4 ) -> vval . v_number = ( varnumber_T )( regmatch . endp [ 0 ] - expr ); 4001if ( l != NULL ) { 4002TV_LIST_ITEM_TV ( li2 ) -> vval . v_number = ( varnumber_T ) idx ; 4003} 4004break; 4005} 4006case kSomeMatchList : 4007// Return list with matched string and submatches. 4008for ( int i = 0 ; i < NSUBEXP ; i ++ ) { 4009if ( regmatch . endp [ i ] == NULL ) { 4010tv_list_append_string ( rettv -> vval . v_list , NULL , 0 ); 4011} else { 4012tv_list_append_string ( rettv -> vval . v_list , regmatch . startp [ i ], 4013( regmatch . endp [ i ] - regmatch . startp [ i ])); 4014} 4015} 4016break; 4017case kSomeMatchStr : 4018// Return matched string. 4019if ( l != NULL ) { 4020tv_copy ( TV_LIST_ITEM_TV ( li ), rettv ); 4021} else { 4022rettv -> vval . v_string = xmemdupz ( regmatch . startp [ 0 ], 4023( size_t )( regmatch . endp [ 0 ] - 4024regmatch . startp [ 0 ])); 4025} 4026break; 4027case kSomeMatch : 4028case kSomeMatchEnd : 4029if ( l != NULL ) { 4030rettv -> vval . v_number = idx ; 4031} else { 4032if ( type == kSomeMatch ) { 4033rettv -> vval . v_number = ( varnumber_T )( regmatch . startp [ 0 ] - str ); 4034} else { 4035rettv -> vval . v_number = ( varnumber_T )( regmatch . endp [ 0 ] - str ); 4036} 4037rettv -> vval . v_number += ( varnumber_T )( str - expr ); 4038} 4039break; 4040} 4041} 4042vim_regfree ( regmatch . regprog ); 4043} 4044 4045theend : 4046if ( type == kSomeMatchStrPos && l == NULL && rettv -> vval . v_list != NULL ) { 4047// matchstrpos() without a list: drop the second item 4048list_T * const ret_l = rettv -> vval . v_list ; 4049tv_list_item_remove ( ret_l , TV_LIST_ITEM_NEXT ( ret_l , tv_list_first ( ret_l ))); 4050} 4051 4052xfree ( tofree ); 4053p_cpo = save_cpo ; 4054} 4055 4056/// Return all the matches in string "str" for pattern "rmp". 4057/// The matches are returned in the List "mlist". 4058/// If "submatches" is true, then submatch information is also returned. 4059/// "matchbuf" is true when called for matchbufline(). 4060static void get_matches_in_str (const char * str , regmatch_T * rmp , list_T * mlist , int idx , 4061bool submatches , bool matchbuf ) 4062{ 4063size_t len = strlen ( str ); 4064int match = 0 ; 4065colnr_T startidx = 0 ; 4066 4067while (true) { 4068match = vim_regexec_nl ( rmp , str , startidx ); 4069if (! match ) { 4070break; 4071} 4072 4073dict_T * d = tv_dict_alloc (); 4074tv_list_append_dict ( mlist , d ); 4075 4076if ( matchbuf ) { 4077tv_dict_add_nr ( d , S_LEN ( "lnum" ), idx ); 4078} else { 4079tv_dict_add_nr ( d , S_LEN ( "idx" ), idx ); 4080} 4081 4082tv_dict_add_nr ( d , S_LEN ( "byteidx" ), 4083( colnr_T )( rmp -> startp [ 0 ] - str )); 4084 4085tv_dict_add_str_len ( d , S_LEN ( "text" ), rmp -> startp [ 0 ], 4086( int )( rmp -> endp [ 0 ] - rmp -> startp [ 0 ])); 4087 4088if ( submatches ) { 4089list_T * sml = tv_list_alloc ( NSUBEXP - 1 ); 4090 4091tv_dict_add_list ( d , S_LEN ( "submatches" ), sml ); 4092 4093// return a list with the submatches 4094for ( int i = 1 ; i < NSUBEXP ; i ++ ) { 4095if ( rmp -> endp [ i ] == NULL ) { 4096tv_list_append_string ( sml , "" , 0 ); 4097} else { 4098tv_list_append_string ( sml , rmp -> startp [ i ], rmp -> endp [ i ] - rmp -> startp [ i ]); 4099} 4100} 4101} 4102startidx = ( colnr_T )( rmp -> endp [ 0 ] - str ); 4103if ( startidx >= ( colnr_T ) len || str + startidx <= rmp -> startp [ 0 ]) { 4104break; 4105} 4106} 4107} 4108 4109/// "matchbufline()" function 4110static void f_matchbufline ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4111{ 4112rettv -> vval . v_number = -1 ; 4113tv_list_alloc_ret ( rettv , kListLenUnknown ); 4114list_T * retlist = rettv -> vval . v_list ; 4115 4116if ( tv_check_for_buffer_arg ( argvars , 0 ) == FAIL 4117|| tv_check_for_string_arg ( argvars , 1 ) == FAIL 4118|| tv_check_for_lnum_arg ( argvars , 2 ) == FAIL 4119|| tv_check_for_lnum_arg ( argvars , 3 ) == FAIL 4120|| tv_check_for_opt_dict_arg ( argvars , 4 ) == FAIL ) { 4121return; 4122} 4123 4124const int prev_did_emsg = did_emsg ; 4125buf_T * buf = tv_get_buf ( & argvars [ 0 ], false); 4126if ( buf == NULL ) { 4127if ( did_emsg == prev_did_emsg ) { 4128semsg ( _ ( e_invalid_buffer_name_str ), tv_get_string ( & argvars [ 0 ])); 4129} 4130return; 4131} 4132if ( buf -> b_ml . ml_mfp == NULL ) { 4133emsg ( _ ( e_buffer_is_not_loaded )); 4134return; 4135} 4136 4137char patbuf [ NUMBUFLEN ]; 4138const char * pat = tv_get_string_buf ( & argvars [ 1 ], patbuf ); 4139 4140const int did_emsg_before = did_emsg ; 4141linenr_T slnum = tv_get_lnum_buf ( & argvars [ 2 ], buf ); 4142if ( did_emsg > did_emsg_before ) { 4143return; 4144} 4145if ( slnum < 1 ) { 4146semsg ( _ ( e_invargval ), "lnum" ); 4147return; 4148} 4149 4150linenr_T elnum = tv_get_lnum_buf ( & argvars [ 3 ], buf ); 4151if ( did_emsg > did_emsg_before ) { 4152return; 4153} 4154if ( elnum < 1 || elnum < slnum ) { 4155semsg ( _ ( e_invargval ), "end_lnum" ); 4156return; 4157} 4158 4159if ( elnum > buf -> b_ml . ml_line_count ) { 4160elnum = buf -> b_ml . ml_line_count ; 4161} 4162 4163bool submatches = false; 4164if ( argvars [ 4 ]. v_type != VAR_UNKNOWN ) { 4165dict_T * d = argvars [ 4 ]. vval . v_dict ; 4166if ( d != NULL ) { 4167dictitem_T * di = tv_dict_find ( d , S_LEN ( "submatches" )); 4168if ( di != NULL ) { 4169if ( di -> di_tv . v_type != VAR_BOOL ) { 4170semsg ( _ ( e_invargval ), "submatches" ); 4171return; 4172} 4173submatches = tv_get_bool ( & di -> di_tv ); 4174} 4175} 4176} 4177 4178// Make 'cpoptions' empty, the 'l' flag should not be used here. 4179char * const save_cpo = p_cpo ; 4180p_cpo = empty_string_option ; 4181 4182regmatch_T regmatch ; 4183regmatch . regprog = vim_regcomp ( pat , RE_MAGIC + RE_STRING ); 4184if ( regmatch . regprog == NULL ) { 4185goto theend ; 4186} 4187regmatch . rm_ic = p_ic ; 4188 4189while ( slnum <= elnum ) { 4190const char * str = ml_get_buf ( buf , slnum ); 4191get_matches_in_str ( str , & regmatch , retlist , slnum , submatches , true); 4192slnum ++ ; 4193} 4194 4195vim_regfree ( regmatch . regprog ); 4196 4197theend : 4198p_cpo = save_cpo ; 4199} 4200 4201/// "match()" function 4202static void f_match ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4203{ 4204find_some_match ( argvars , rettv , kSomeMatch ); 4205} 4206 4207/// "matchend()" function 4208static void f_matchend ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4209{ 4210find_some_match ( argvars , rettv , kSomeMatchEnd ); 4211} 4212 4213/// "matchlist()" function 4214static void f_matchlist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4215{ 4216find_some_match ( argvars , rettv , kSomeMatchList ); 4217} 4218 4219/// "matchstr()" function 4220static void f_matchstr ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4221{ 4222find_some_match ( argvars , rettv , kSomeMatchStr ); 4223} 4224 4225/// "matchstrlist()" function 4226static void f_matchstrlist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4227{ 4228rettv -> vval . v_number = -1 ; 4229tv_list_alloc_ret ( rettv , kListLenUnknown ); 4230list_T * retlist = rettv -> vval . v_list ; 4231 4232if ( tv_check_for_list_arg ( argvars , 0 ) == FAIL 4233|| tv_check_for_string_arg ( argvars , 1 ) == FAIL 4234|| tv_check_for_opt_dict_arg ( argvars , 2 ) == FAIL ) { 4235return; 4236} 4237 4238list_T * l = NULL ; 4239if (( l = argvars [ 0 ]. vval . v_list ) == NULL ) { 4240return; 4241} 4242 4243char patbuf [ NUMBUFLEN ]; 4244const char * pat = tv_get_string_buf_chk ( & argvars [ 1 ], patbuf ); 4245if ( pat == NULL ) { 4246return; 4247} 4248 4249// Make 'cpoptions' empty, the 'l' flag should not be used here. 4250char * const save_cpo = p_cpo ; 4251p_cpo = empty_string_option ; 4252 4253regmatch_T regmatch ; 4254regmatch . regprog = vim_regcomp ( pat , RE_MAGIC + RE_STRING ); 4255if ( regmatch . regprog == NULL ) { 4256goto theend ; 4257} 4258regmatch . rm_ic = p_ic ; 4259 4260bool submatches = false; 4261if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 4262dict_T * d = argvars [ 2 ]. vval . v_dict ; 4263if ( d != NULL ) { 4264dictitem_T * di = tv_dict_find ( d , S_LEN ( "submatches" )); 4265if ( di != NULL ) { 4266if ( di -> di_tv . v_type != VAR_BOOL ) { 4267semsg ( _ ( e_invargval ), "submatches" ); 4268goto cleanup ; 4269} 4270submatches = tv_get_bool ( & di -> di_tv ); 4271} 4272} 4273} 4274 4275int idx = 0 ; 4276TV_LIST_ITER_CONST ( l , li , { 4277const typval_T * const li_tv = TV_LIST_ITEM_TV ( li ); 4278if ( li_tv -> v_type == VAR_STRING && li_tv -> vval . v_string != NULL ) { 4279const char * str = li_tv -> vval . v_string ; 4280get_matches_in_str ( str , & regmatch , retlist , idx , submatches , false); 4281} 4282idx ++ ; 4283}); 4284 4285cleanup : 4286vim_regfree ( regmatch . regprog ); 4287 4288theend : 4289p_cpo = save_cpo ; 4290} 4291 4292/// "matchstrpos()" function 4293static void f_matchstrpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4294{ 4295find_some_match ( argvars , rettv , kSomeMatchStrPos ); 4296} 4297 4298/// Get maximal/minimal number value in a list or dictionary 4299/// 4300/// @param[in] tv List or dictionary to work with. If it contains something 4301/// that is not an integer number (or cannot be coerced to 4302/// it) error is given. 4303/// @param[out] rettv Location where result will be saved. Only assigns 4304/// vval.v_number, type is not touched. Returns zero for 4305/// empty lists/dictionaries. 4306/// @param[in] domax Determines whether maximal or minimal value is desired. 4307static void max_min (const typval_T * const tv , typval_T * const rettv , const bool domax ) 4308FUNC_ATTR_NONNULL_ALL 4309{ 4310bool error = false; 4311 4312rettv -> vval . v_number = 0 ; 4313varnumber_T n = ( domax ? VARNUMBER_MIN : VARNUMBER_MAX ); 4314if ( tv -> v_type == VAR_LIST ) { 4315if ( tv_list_len ( tv -> vval . v_list ) == 0 ) { 4316return; 4317} 4318TV_LIST_ITER_CONST ( tv -> vval . v_list , li , { 4319const varnumber_T i = tv_get_number_chk ( TV_LIST_ITEM_TV ( li ), & error ); 4320if ( error ) { 4321return; // type error; errmsg already given 4322} 4323if ( domax ? i > n : i < n ) { 4324n = i ; 4325} 4326}); 4327} else if ( tv -> v_type == VAR_DICT ) { 4328if ( tv_dict_len ( tv -> vval . v_dict ) == 0 ) { 4329return; 4330} 4331TV_DICT_ITER ( tv -> vval . v_dict , di , { 4332const varnumber_T i = tv_get_number_chk ( & di -> di_tv , & error ); 4333if ( error ) { 4334return; // type error; errmsg already given 4335} 4336if ( domax ? i > n : i < n ) { 4337n = i ; 4338} 4339}); 4340} else { 4341semsg ( _ ( e_listdictarg ), domax ? "max()" : "min()" ); 4342return; 4343} 4344 4345rettv -> vval . v_number = n ; 4346} 4347 4348/// "max()" function 4349static void f_max ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4350{ 4351max_min ( argvars , rettv , true); 4352} 4353 4354/// "min()" function 4355static void f_min ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4356{ 4357max_min ( argvars , rettv , false); 4358} 4359 4360/// "mode()" function 4361static void f_mode ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4362{ 4363char buf [ MODE_MAX_LENGTH ]; 4364 4365get_mode ( buf ); 4366 4367// Clear out the minor mode when the argument is not a non-zero number or 4368// non-empty string. 4369if (! non_zero_arg ( & argvars [ 0 ])) { 4370buf [ 1 ] = NUL ; 4371} 4372 4373rettv -> vval . v_string = xstrdup ( buf ); 4374rettv -> v_type = VAR_STRING ; 4375} 4376 4377static void may_add_state_char ( garray_T * gap , const char * include , uint8_t c ) 4378{ 4379if ( include == NULL || vim_strchr ( include , c ) != NULL ) { 4380ga_append ( gap , c ); 4381} 4382} 4383 4384/// "state()" function 4385static void f_state ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4386{ 4387garray_T ga ; 4388ga_init ( & ga , 1 , 20 ); 4389const char * include = NULL ; 4390 4391if ( argvars [ 0 ]. v_type != VAR_UNKNOWN ) { 4392include = tv_get_string ( & argvars [ 0 ]); 4393} 4394 4395if (!( stuff_empty () && typebuf . tb_len == 0 && ! using_script ())) { 4396may_add_state_char ( & ga , include , 'm' ); 4397} 4398if ( op_pending ()) { 4399may_add_state_char ( & ga , include , 'o' ); 4400} 4401if ( autocmd_busy ) { 4402may_add_state_char ( & ga , include , 'x' ); 4403} 4404if ( ins_compl_active ()) { 4405may_add_state_char ( & ga , include , 'a' ); 4406} 4407if (! get_was_safe_state ()) { 4408may_add_state_char ( & ga , include , 'S' ); 4409} 4410for ( int i = 0 ; i < get_callback_depth () && i < 3 ; i ++ ) { 4411may_add_state_char ( & ga , include , 'c' ); 4412} 4413if ( msg_scrolled > 0 ) { 4414may_add_state_char ( & ga , include , 's' ); 4415} 4416 4417rettv -> v_type = VAR_STRING ; 4418rettv -> vval . v_string = ga . ga_data ; 4419} 4420 4421/// "msgpackdump()" function 4422static void f_msgpackdump ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4423FUNC_ATTR_NONNULL_ALL 4424{ 4425if ( argvars [ 0 ]. v_type != VAR_LIST ) { 4426semsg ( _ ( e_listarg ), "msgpackdump()" ); 4427return; 4428} 4429list_T * const list = argvars [ 0 ]. vval . v_list ; 4430PackerBuffer packer = packer_string_buffer (); 4431const char * const msg = _ ( "msgpackdump() argument, index %i" ); 4432// Assume that translation will not take more then 4 times more space 4433char msgbuf [sizeof( "msgpackdump() argument, index " ) * 4 + NUMBUFLEN ]; 4434int idx = 0 ; 4435TV_LIST_ITER ( list , li , { 4436vim_snprintf ( msgbuf , sizeof( msgbuf ), msg , idx ); 4437idx ++ ; 4438if ( encode_vim_to_msgpack ( & packer , TV_LIST_ITEM_TV ( li ), msgbuf ) == FAIL ) { 4439break; 4440} 4441}); 4442String data = packer_take_string ( & packer ); 4443if ( argvars [ 1 ]. v_type != VAR_UNKNOWN && strequal ( tv_get_string ( & argvars [ 1 ]), "B" )) { 4444blob_T * b = tv_blob_alloc_ret ( rettv ); 4445b -> bv_ga . ga_data = data . data ; 4446b -> bv_ga . ga_len = ( int ) data . size ; 4447b -> bv_ga . ga_maxlen = ( int )( packer . endptr - packer . startptr ); 4448} else { 4449encode_list_write ( tv_list_alloc_ret ( rettv , kListLenMayKnow ), data . data , data . size ); 4450api_free_string ( data ); 4451} 4452} 4453 4454static void emsg_mpack_error ( int status ) 4455{ 4456switch ( status ) { 4457case MPACK_ERROR : 4458semsg ( _ ( e_invarg2 ), "Failed to parse msgpack string" ); 4459break; 4460 4461case MPACK_EOF : 4462semsg ( _ ( e_invarg2 ), "Incomplete msgpack string" ); 4463break; 4464 4465case MPACK_NOMEM : 4466semsg ( _ ( e_invarg2 ), "object was too deep to unpack" ); 4467break; 4468 4469default: 4470break; 4471} 4472} 4473 4474static void msgpackparse_unpack_list (const list_T * const list , list_T * const ret_list ) 4475FUNC_ATTR_NONNULL_ARG ( 2 ) 4476{ 4477if ( tv_list_len ( list ) == 0 ) { 4478return; 4479} 4480if ( TV_LIST_ITEM_TV ( tv_list_first ( list )) -> v_type != VAR_STRING ) { 4481semsg ( _ ( e_invarg2 ), "List item is not a string" ); 4482return; 4483} 4484ListReaderState lrstate = encode_init_lrstate ( list ); 4485char * buf = alloc_block (); 4486size_t buf_size = 0 ; 4487 4488typval_T cur_item = { . v_type = VAR_UNKNOWN }; 4489mpack_parser_t parser ; 4490mpack_parser_init ( & parser , 0 ); 4491parser . data . p = & cur_item ; 4492 4493int status = MPACK_OK ; 4494while (true) { 4495size_t read_bytes ; 4496const int rlret = encode_read_from_list ( & lrstate , buf + buf_size , ARENA_BLOCK_SIZE - buf_size , 4497& read_bytes ); 4498if ( rlret == FAIL ) { 4499semsg ( _ ( e_invarg2 ), "List item is not a string" ); 4500goto end ; 4501} 4502buf_size += read_bytes ; 4503 4504const char * ptr = buf ; 4505while ( buf_size ) { 4506status = mpack_parse_typval ( & parser , & ptr , & buf_size ); 4507if ( status == MPACK_OK ) { 4508tv_list_append_owned_tv ( ret_list , cur_item ); 4509cur_item . v_type = VAR_UNKNOWN ; 4510} else { 4511break; 4512} 4513} 4514 4515if ( rlret == OK ) { 4516break; 4517} 4518 4519if ( status == MPACK_EOF ) { 4520// move remaining data to front of buffer 4521if ( buf_size && ptr > buf ) { 4522memmove ( buf , ptr , buf_size ); 4523} 4524} else if ( status != MPACK_OK ) { 4525break; 4526} 4527} 4528 4529if ( status != MPACK_OK ) { 4530typval_parser_error_free ( & parser ); 4531emsg_mpack_error ( status ); 4532} 4533 4534end : 4535free_block ( buf ); 4536} 4537 4538static void msgpackparse_unpack_blob (const blob_T * const blob , list_T * const ret_list ) 4539FUNC_ATTR_NONNULL_ARG ( 2 ) 4540{ 4541const int len = tv_blob_len ( blob ); 4542if ( len == 0 ) { 4543return; 4544} 4545 4546const char * data = blob -> bv_ga . ga_data ; 4547size_t remaining = ( size_t ) len ; 4548while ( remaining ) { 4549typval_T tv ; 4550int status = unpack_typval ( & data , & remaining , & tv ); 4551if ( status != MPACK_OK ) { 4552emsg_mpack_error ( status ); 4553return; 4554} 4555 4556tv_list_append_owned_tv ( ret_list , tv ); 4557} 4558} 4559 4560/// "msgpackparse" function 4561static void f_msgpackparse ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4562FUNC_ATTR_NONNULL_ALL 4563{ 4564if ( argvars [ 0 ]. v_type != VAR_LIST && argvars [ 0 ]. v_type != VAR_BLOB ) { 4565semsg ( _ ( e_listblobarg ), "msgpackparse()" ); 4566return; 4567} 4568list_T * const ret_list = tv_list_alloc_ret ( rettv , kListLenMayKnow ); 4569if ( argvars [ 0 ]. v_type == VAR_LIST ) { 4570msgpackparse_unpack_list ( argvars [ 0 ]. vval . v_list , ret_list ); 4571} else { 4572msgpackparse_unpack_blob ( argvars [ 0 ]. vval . v_blob , ret_list ); 4573} 4574} 4575 4576/// "nextnonblank()" function 4577static void f_nextnonblank ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4578{ 4579linenr_T lnum ; 4580 4581for ( lnum = tv_get_lnum ( argvars );; lnum ++ ) { 4582if ( lnum < 0 || lnum > curbuf -> b_ml . ml_line_count ) { 4583lnum = 0 ; 4584break; 4585} 4586if ( * skipwhite ( ml_get ( lnum )) != NUL ) { 4587break; 4588} 4589} 4590rettv -> vval . v_number = lnum ; 4591} 4592 4593/// "nr2char()" function 4594static void f_nr2char ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4595{ 4596if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 4597if (! tv_check_num ( & argvars [ 1 ])) { 4598return; 4599} 4600} 4601 4602bool error = false; 4603const varnumber_T num = tv_get_number_chk ( & argvars [ 0 ], & error ); 4604if ( error ) { 4605return; 4606} 4607if ( num < 0 ) { 4608emsg ( _ ( "E5070: Character number must not be less than zero" )); 4609return; 4610} 4611if ( num > INT_MAX ) { 4612semsg ( _ ( "E5071: Character number must not be greater than INT_MAX (%i)" ), 4613INT_MAX ); 4614return; 4615} 4616 4617char buf [ MB_MAXCHAR ]; 4618const int len = utf_char2bytes (( int ) num , buf ); 4619 4620rettv -> v_type = VAR_STRING ; 4621rettv -> vval . v_string = xmemdupz ( buf , ( size_t ) len ); 4622} 4623 4624/// "or(expr, expr)" function 4625static void f_or ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4626{ 4627rettv -> vval . v_number = tv_get_number_chk ( & argvars [ 0 ], NULL ) 4628| tv_get_number_chk ( & argvars [ 1 ], NULL ); 4629} 4630 4631/// "pow()" function 4632static void f_pow ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4633{ 4634float_T fx ; 4635float_T fy ; 4636 4637rettv -> v_type = VAR_FLOAT ; 4638if ( tv_get_float_chk ( argvars , & fx ) && tv_get_float_chk ( & argvars [ 1 ], & fy )) { 4639rettv -> vval . v_float = pow ( fx , fy ); 4640} else { 4641rettv -> vval . v_float = 0.0 ; 4642} 4643} 4644 4645/// "prevnonblank()" function 4646static void f_prevnonblank ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4647{ 4648linenr_T lnum = tv_get_lnum ( argvars ); 4649if ( lnum < 1 || lnum > curbuf -> b_ml . ml_line_count ) { 4650lnum = 0 ; 4651} else { 4652while ( lnum >= 1 && * skipwhite ( ml_get ( lnum )) == NUL ) { 4653lnum -- ; 4654} 4655} 4656rettv -> vval . v_number = lnum ; 4657} 4658 4659/// "printf()" function 4660static void f_printf ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4661{ 4662rettv -> v_type = VAR_STRING ; 4663rettv -> vval . v_string = NULL ; 4664{ 4665int saved_did_emsg = did_emsg ; 4666 4667// Get the required length, allocate the buffer and do it for real. 4668did_emsg = false; 4669char buf [ NUMBUFLEN ]; 4670const char * fmt = tv_get_string_buf ( & argvars [ 0 ], buf ); 4671int len = vim_vsnprintf_typval ( NULL , 0 , fmt , dummy_ap , argvars + 1 ); 4672if (! did_emsg ) { 4673char * s = xmalloc (( size_t ) len + 1 ); 4674rettv -> vval . v_string = s ; 4675vim_vsnprintf_typval ( s , ( size_t ) len + 1 , fmt , dummy_ap , argvars + 1 ); 4676} 4677did_emsg |= saved_did_emsg ; 4678} 4679} 4680 4681/// "pum_getpos()" function 4682static void f_pum_getpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4683{ 4684tv_dict_alloc_ret ( rettv ); 4685pum_set_event_info ( rettv -> vval . v_dict ); 4686} 4687 4688/// "pumvisible()" function 4689static void f_pumvisible ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4690{ 4691if ( pum_visible ()) { 4692rettv -> vval . v_number = 1 ; 4693} 4694} 4695 4696/// "py3eval()" and "pyxeval()" functions (always python3) 4697static void f_py3eval ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4698{ 4699script_host_eval ( "python3" , argvars , rettv ); 4700} 4701 4702static void init_srand ( uint32_t * const x ) 4703FUNC_ATTR_NONNULL_ALL 4704{ 4705union { 4706uint32_t number ; 4707uint8_t bytes [sizeof( uint32_t )]; 4708} buf ; 4709 4710if ( uv_random ( NULL , NULL , buf . bytes , sizeof( buf . bytes ), 0 , NULL ) == 0 ) { 4711* x = buf . number ; 4712return; 4713} 4714 4715// The system's random number generator doesn't work, 4716// fall back to os_hrtime() XOR with process ID 4717* x = ( uint32_t ) os_hrtime (); 4718* x ^= ( uint32_t ) os_get_pid (); 4719} 4720 4721static inline uint32_t splitmix32 ( uint32_t * const x ) 4722FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE 4723{ 4724uint32_t z = ( * x += 0x9e3779b9 ); 4725z = ( z ^ ( z >> 16 )) * 0x85ebca6b ; 4726z = ( z ^ ( z >> 13 )) * 0xc2b2ae35 ; 4727return z ^ ( z >> 16 ); 4728} 4729 4730static inline uint32_t shuffle_xoshiro128starstar ( uint32_t * const x , uint32_t * const y , 4731uint32_t * const z , uint32_t * const w ) 4732FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE 4733{ 4734#define ROTL ( x , k ) (((x) << (k)) | ((x) >> (32 - (k)))) 4735const uint32_t result = ROTL ( * y * 5 , 7 ) * 9 ; 4736const uint32_t t = * y << 9 ; 4737* z ^= * x ; 4738* w ^= * y ; 4739* y ^= * z ; 4740* x ^= * w ; 4741* z ^= t ; 4742* w = ROTL ( * w , 11 ); 4743#undef ROTL 4744return result ; 4745} 4746 4747/// "rand()" function 4748static void f_rand ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4749{ 4750uint32_t result ; 4751 4752if ( argvars [ 0 ]. v_type == VAR_UNKNOWN ) { 4753static uint32_t gx , gy , gz , gw ; 4754static bool initialized = false; 4755 4756// When no argument is given use the global seed list. 4757if (! initialized ) { 4758// Initialize the global seed list. 4759uint32_t x = 0 ; 4760init_srand ( & x ); 4761 4762gx = splitmix32 ( & x ); 4763gy = splitmix32 ( & x ); 4764gz = splitmix32 ( & x ); 4765gw = splitmix32 ( & x ); 4766initialized = true; 4767} 4768 4769result = shuffle_xoshiro128starstar ( & gx , & gy , & gz , & gw ); 4770} else if ( argvars [ 0 ]. v_type == VAR_LIST ) { 4771list_T * const l = argvars [ 0 ]. vval . v_list ; 4772if ( tv_list_len ( l ) != 4 ) { 4773goto theend ; 4774} 4775 4776typval_T * const tvx = TV_LIST_ITEM_TV ( tv_list_find ( l , 0 )); 4777typval_T * const tvy = TV_LIST_ITEM_TV ( tv_list_find ( l , 1 )); 4778typval_T * const tvz = TV_LIST_ITEM_TV ( tv_list_find ( l , 2 )); 4779typval_T * const tvw = TV_LIST_ITEM_TV ( tv_list_find ( l , 3 )); 4780if ( tvx -> v_type != VAR_NUMBER ) { 4781goto theend ; 4782} 4783if ( tvy -> v_type != VAR_NUMBER ) { 4784goto theend ; 4785} 4786if ( tvz -> v_type != VAR_NUMBER ) { 4787goto theend ; 4788} 4789if ( tvw -> v_type != VAR_NUMBER ) { 4790goto theend ; 4791} 4792uint32_t x = ( uint32_t ) tvx -> vval . v_number ; 4793uint32_t y = ( uint32_t ) tvy -> vval . v_number ; 4794uint32_t z = ( uint32_t ) tvz -> vval . v_number ; 4795uint32_t w = ( uint32_t ) tvw -> vval . v_number ; 4796 4797result = shuffle_xoshiro128starstar ( & x , & y , & z , & w ); 4798 4799tvx -> vval . v_number = ( varnumber_T ) x ; 4800tvy -> vval . v_number = ( varnumber_T ) y ; 4801tvz -> vval . v_number = ( varnumber_T ) z ; 4802tvw -> vval . v_number = ( varnumber_T ) w ; 4803} else { 4804goto theend ; 4805} 4806 4807rettv -> v_type = VAR_NUMBER ; 4808rettv -> vval . v_number = ( varnumber_T ) result ; 4809return; 4810 4811theend : 4812semsg ( _ ( e_invarg2 ), tv_get_string ( & argvars [ 0 ])); 4813rettv -> v_type = VAR_NUMBER ; 4814rettv -> vval . v_number = -1 ; 4815} 4816 4817/// "srand()" function 4818static void f_srand ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4819{ 4820uint32_t x = 0 ; 4821 4822tv_list_alloc_ret ( rettv , 4 ); 4823if ( argvars [ 0 ]. v_type == VAR_UNKNOWN ) { 4824init_srand ( & x ); 4825} else { 4826bool error = false; 4827x = ( uint32_t ) tv_get_number_chk ( & argvars [ 0 ], & error ); 4828if ( error ) { 4829return; 4830} 4831} 4832 4833tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) splitmix32 ( & x )); 4834tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) splitmix32 ( & x )); 4835tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) splitmix32 ( & x )); 4836tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) splitmix32 ( & x )); 4837} 4838 4839/// "perleval()" function 4840static void f_perleval ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4841{ 4842script_host_eval ( "perl" , argvars , rettv ); 4843} 4844 4845/// "rubyeval()" function 4846static void f_rubyeval ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4847{ 4848script_host_eval ( "ruby" , argvars , rettv ); 4849} 4850 4851/// "range()" function 4852static void f_range ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4853{ 4854varnumber_T end ; 4855varnumber_T stride = 1 ; 4856bool error = false; 4857 4858varnumber_T start = tv_get_number_chk ( & argvars [ 0 ], & error ); 4859if ( argvars [ 1 ]. v_type == VAR_UNKNOWN ) { 4860end = start - 1 ; 4861start = 0 ; 4862} else { 4863end = tv_get_number_chk ( & argvars [ 1 ], & error ); 4864if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 4865stride = tv_get_number_chk ( & argvars [ 2 ], & error ); 4866} 4867} 4868 4869if ( error ) { 4870return; // Type error; errmsg already given. 4871} 4872if ( stride == 0 ) { 4873emsg ( _ ( "E726: Stride is zero" )); 4874return; 4875} 4876if ( stride > 0 ? end + 1 < start : end - 1 > start ) { 4877emsg ( _ ( "E727: Start past end" )); 4878return; 4879} 4880 4881tv_list_alloc_ret ( rettv , ( end - start ) / stride ); 4882for ( varnumber_T i = start ; stride > 0 ? i <= end : i >= end ; i += stride ) { 4883tv_list_append_number ( rettv -> vval . v_list , i ); 4884} 4885} 4886 4887/// "getreginfo()" function 4888static void f_getreginfo ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4889{ 4890int regname = getreg_get_regname ( argvars ); 4891if ( regname == 0 ) { 4892return; 4893} 4894 4895if ( regname == '@' ) { 4896regname = '"' ; 4897} 4898 4899tv_dict_alloc_ret ( rettv ); 4900dict_T * const dict = rettv -> vval . v_dict ; 4901 4902list_T * const list = get_reg_contents ( regname , kGRegExprSrc | kGRegList ); 4903if ( list == NULL ) { 4904return; 4905} 4906tv_dict_add_list ( dict , S_LEN ( "regcontents" ), list ); 4907 4908char buf [ NUMBUFLEN + 2 ]; 4909size_t buflen ; 4910colnr_T reglen = 0 ; 4911switch ( get_reg_type ( regname , & reglen )) { 4912case kMTLineWise : 4913buf [ 0 ] = 'V' ; 4914buf [ 1 ] = NUL ; 4915buflen = 1 ; 4916break; 4917case kMTCharWise : 4918buf [ 0 ] = 'v' ; 4919buf [ 1 ] = NUL ; 4920buflen = 1 ; 4921break; 4922case kMTBlockWise : 4923buflen = vim_snprintf_safelen ( buf , sizeof( buf ), "%c%d" , Ctrl_V , reglen + 1 ); 4924break; 4925case kMTUnknown : 4926abort (); 4927} 4928tv_dict_add_str_len ( dict , S_LEN ( "regtype" ), buf , ( int ) buflen ); 4929 4930buf [ 0 ] = ( char ) get_register_name ( get_unname_register ()); 4931buf [ 1 ] = NUL ; 4932buflen = buf [ 0 ] == NUL ? 0 : 1 ; 4933if ( regname == '"' ) { 4934tv_dict_add_str_len ( dict , S_LEN ( "points_to" ), buf , ( int ) buflen ); 4935} else { 4936tv_dict_add_bool ( dict , S_LEN ( "isunnamed" ), 4937regname == buf [ 0 ] ? kBoolVarTrue : kBoolVarFalse ); 4938} 4939} 4940 4941static void return_register ( int regname , typval_T * rettv ) 4942{ 4943char buf [ 2 ] = { ( char ) regname , NUL }; 4944 4945rettv -> v_type = VAR_STRING ; 4946rettv -> vval . v_string = xmemdupz ( buf , buf [ 0 ] == NUL ? 0 : 1 ); 4947} 4948 4949/// "reg_executing()" function 4950static void f_reg_executing ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4951{ 4952return_register ( reg_executing , rettv ); 4953} 4954 4955/// "reg_recording()" function 4956static void f_reg_recording ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4957{ 4958return_register ( reg_recording , rettv ); 4959} 4960 4961static void f_reg_recorded ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 4962{ 4963return_register ( reg_recorded , rettv ); 4964} 4965 4966/// list2proftime - convert a List to proftime_T 4967/// 4968/// @param arg The input list, must be of type VAR_LIST and have 4969/// exactly 2 items 4970/// @param[out] tm The proftime_T representation of `arg` 4971/// @return OK In case of success, FAIL in case of error 4972static int list2proftime ( typval_T * arg , proftime_T * tm ) FUNC_ATTR_NONNULL_ALL 4973{ 4974if ( arg -> v_type != VAR_LIST || tv_list_len ( arg -> vval . v_list ) != 2 ) { 4975return FAIL ; 4976} 4977 4978bool error = false; 4979varnumber_T n1 = tv_list_find_nr ( arg -> vval . v_list , 0 , & error ); 4980varnumber_T n2 = tv_list_find_nr ( arg -> vval . v_list , 1 , & error ); 4981if ( error ) { 4982return FAIL ; 4983} 4984 4985// in f_reltime() we split up the 64-bit proftime_T into two 32-bit 4986// values, now we combine them again. 4987union { 4988struct { int32_t low , high ; } split ; 4989proftime_T prof ; 4990} u = { . split . high = ( int32_t ) n1 , . split . low = ( int32_t ) n2 }; 4991 4992* tm = u . prof ; 4993 4994return OK ; 4995} 4996 4997/// f_reltime - return an item that represents a time value 4998/// 4999/// @param[out] rettv Without an argument it returns the current time. With 5000/// one argument it returns the time passed since the argument. 5001/// With two arguments it returns the time passed between 5002/// the two arguments. 5003static void f_reltime ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5004{ 5005proftime_T res ; 5006proftime_T start ; 5007 5008if ( argvars [ 0 ]. v_type == VAR_UNKNOWN ) { 5009// no arguments: get current time. 5010res = profile_start (); 5011} else if ( argvars [ 1 ]. v_type == VAR_UNKNOWN ) { 5012if ( list2proftime ( & argvars [ 0 ], & res ) == FAIL ) { 5013return; 5014} 5015res = profile_end ( res ); 5016} else { 5017// two arguments: compute the difference. 5018if ( list2proftime ( & argvars [ 0 ], & start ) == FAIL 5019|| list2proftime ( & argvars [ 1 ], & res ) == FAIL ) { 5020return; 5021} 5022res = profile_sub ( res , start ); 5023} 5024 5025// we have to store the 64-bit proftime_T inside of a list of int's 5026// (varnumber_T is defined as int). For all our supported platforms, int's 5027// are at least 32-bits wide. So we'll use two 32-bit values to store it. 5028union { 5029struct { int32_t low , high ; } split ; 5030proftime_T prof ; 5031} u = { . prof = res }; 5032 5033// statically assert that the union type conv will provide the correct 5034// results, if varnumber_T or proftime_T change, the union cast will need 5035// to be revised. 5036STATIC_ASSERT (sizeof( u . prof ) == sizeof( u ) && sizeof( u . split ) == sizeof( u ), 5037"type punning will produce incorrect results on this platform" ); 5038 5039tv_list_alloc_ret ( rettv , 2 ); 5040tv_list_append_number ( rettv -> vval . v_list , u . split . high ); 5041tv_list_append_number ( rettv -> vval . v_list , u . split . low ); 5042} 5043 5044/// "reltimestr()" function 5045static void f_reltimestr ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5046FUNC_ATTR_NONNULL_ALL 5047{ 5048proftime_T tm ; 5049 5050rettv -> v_type = VAR_STRING ; 5051rettv -> vval . v_string = NULL ; 5052if ( list2proftime ( & argvars [ 0 ], & tm ) == OK ) { 5053rettv -> vval . v_string = xstrdup ( profile_msg ( tm )); 5054} 5055} 5056 5057/// Repeat the list "l" "n" times and set "rettv" to the new list. 5058static void repeat_list ( list_T * l , varnumber_T n , typval_T * rettv ) 5059{ 5060const int slen = tv_list_len ( l ); 5061 5062tv_list_alloc_ret ( rettv , ( n > 0 ) * n * slen ); 5063if ( l == NULL || n <= 0 || slen == 0 ) { 5064return; 5065} 5066 5067if ( check_repeat_count ( slen , n ) == FAIL ) { 5068return; 5069} 5070 5071while ( n -- > 0 ) { 5072tv_list_extend ( rettv -> vval . v_list , l , NULL ); 5073} 5074} 5075 5076/// Repeat the blob "b" "n" times and set "rettv" to the new blob. 5077static void repeat_blob ( typval_T * blob_tv , varnumber_T n , typval_T * rettv ) 5078{ 5079blob_T * const blob = blob_tv -> vval . v_blob ; 5080 5081tv_blob_alloc_ret ( rettv ); 5082if ( blob == NULL || n <= 0 ) { 5083return; 5084} 5085 5086const int slen = blob -> bv_ga . ga_len ; 5087if ( slen <= 0 ) { 5088return; 5089} 5090if ( check_repeat_count ( slen , n ) == FAIL ) { 5091return; 5092} 5093const int len = slen * ( int ) n ; 5094 5095ga_grow ( & rettv -> vval . v_blob -> bv_ga , len ); 5096 5097rettv -> vval . v_blob -> bv_ga . ga_len = len ; 5098 5099int i ; 5100for ( i = 0 ; i < slen ; i ++ ) { 5101if ( tv_blob_get ( blob , i ) != 0 ) { 5102break; 5103} 5104} 5105 5106if ( i == slen ) { 5107// No need to copy since all bytes are already zero 5108return; 5109} 5110 5111for ( i = 0 ; i < ( int ) n ; i ++ ) { 5112tv_blob_set_range ( rettv -> vval . v_blob , i * slen , ( i + 1 ) * slen - 1 , blob_tv ); 5113} 5114} 5115 5116/// Repeat the string "str" "n" times and set "rettv" to the new string. 5117static void repeat_string ( typval_T * str_tv , varnumber_T n , typval_T * rettv ) 5118{ 5119rettv -> v_type = VAR_STRING ; 5120rettv -> vval . v_string = NULL ; 5121if ( n <= 0 ) { 5122return; 5123} 5124 5125const char * const p = tv_get_string ( str_tv ); 5126 5127const size_t slen = strlen ( p ); 5128if ( slen == 0 || n <= 0 ) { 5129return; 5130} 5131if ( check_repeat_count (( varnumber_T ) slen , n ) == FAIL ) { 5132return; 5133} 5134const size_t len = slen * ( size_t ) n ; 5135 5136char * const r = xmallocz ( len ); 5137 5138memmove ( r , p , slen ); 5139size_t done = slen ; 5140while ( done < len ) { 5141size_t copy_len = done ; 5142if ( copy_len > len - done ) { 5143copy_len = len - done ; 5144} 5145memmove ( r + done , r , copy_len ); 5146done += copy_len ; 5147} 5148 5149rettv -> vval . v_string = r ; 5150} 5151 5152/// "repeat()" function 5153static void f_repeat ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5154{ 5155varnumber_T n = tv_get_number ( & argvars [ 1 ]); 5156if ( argvars [ 0 ]. v_type == VAR_LIST ) { 5157repeat_list ( argvars [ 0 ]. vval . v_list , n , rettv ); 5158} else if ( argvars [ 0 ]. v_type == VAR_BLOB ) { 5159repeat_blob ( & argvars [ 0 ], n , rettv ); 5160} else { 5161repeat_string ( & argvars [ 0 ], n , rettv ); 5162} 5163} 5164 5165/// Implementation of reduce() for list "argvars[0]", using the function "expr" 5166/// starting with the optional initial value argvars[2] and return the result in 5167/// "rettv". 5168static void reduce_list ( typval_T * argvars , typval_T * expr , typval_T * rettv ) 5169{ 5170list_T * const l = argvars [ 0 ]. vval . v_list ; 5171const int called_emsg_start = called_emsg ; 5172 5173typval_T initial ; 5174const listitem_T * li = NULL ; 5175if ( argvars [ 2 ]. v_type == VAR_UNKNOWN ) { 5176if ( tv_list_len ( l ) == 0 ) { 5177semsg ( _ ( e_reduce_of_an_empty_str_with_no_initial_value ), "List" ); 5178return; 5179} 5180const listitem_T * const first = tv_list_first ( l ); 5181initial = * TV_LIST_ITEM_TV ( first ); 5182li = TV_LIST_ITEM_NEXT ( l , first ); 5183} else { 5184initial = argvars [ 2 ]; 5185li = tv_list_first ( l ); 5186} 5187 5188tv_copy ( & initial , rettv ); 5189 5190if ( l == NULL ) { 5191return; 5192} 5193 5194const VarLockStatus prev_locked = tv_list_locked ( l ); 5195 5196tv_list_set_lock ( l , VAR_FIXED ); // disallow the list changing here 5197for (; li != NULL ; li = TV_LIST_ITEM_NEXT ( l , li )) { 5198typval_T argv [ 3 ]; 5199argv [ 0 ] = * rettv ; 5200argv [ 1 ] = * TV_LIST_ITEM_TV ( li ); 5201rettv -> v_type = VAR_UNKNOWN ; 5202 5203const int r = eval_expr_typval ( expr , true, argv , 2 , rettv ); 5204 5205tv_clear ( & argv [ 0 ]); 5206if ( r == FAIL || called_emsg != called_emsg_start ) { 5207break; 5208} 5209} 5210tv_list_set_lock ( l , prev_locked ); 5211} 5212 5213/// Implementation of reduce() for String "argvars[0]" using the function "expr" 5214/// starting with the optional initial value "argvars[2]" and return the result 5215/// in "rettv". 5216static void reduce_string ( typval_T * argvars , typval_T * expr , typval_T * rettv ) 5217{ 5218const char * p = tv_get_string ( & argvars [ 0 ]); 5219int len ; 5220const int called_emsg_start = called_emsg ; 5221 5222if ( argvars [ 2 ]. v_type == VAR_UNKNOWN ) { 5223if ( * p == NUL ) { 5224semsg ( _ ( e_reduce_of_an_empty_str_with_no_initial_value ), "String" ); 5225return; 5226} 5227len = utfc_ptr2len ( p ); 5228* rettv = ( typval_T ){ 5229. v_type = VAR_STRING , 5230. v_lock = VAR_UNLOCKED , 5231. vval . v_string = xmemdupz ( p , ( size_t ) len ), 5232}; 5233p += len ; 5234} else if ( tv_check_for_string_arg ( argvars , 2 ) == FAIL ) { 5235return; 5236} else { 5237tv_copy ( & argvars [ 2 ], rettv ); 5238} 5239 5240for (; * p != NUL ; p += len ) { 5241typval_T argv [ 3 ]; 5242argv [ 0 ] = * rettv ; 5243rettv -> v_type = VAR_UNKNOWN ; 5244len = utfc_ptr2len ( p ); 5245argv [ 1 ] = ( typval_T ){ 5246. v_type = VAR_STRING , 5247. v_lock = VAR_UNLOCKED , 5248. vval . v_string = xmemdupz ( p , ( size_t ) len ), 5249}; 5250 5251const int r = eval_expr_typval ( expr , true, argv , 2 , rettv ); 5252 5253tv_clear ( & argv [ 0 ]); 5254tv_clear ( & argv [ 1 ]); 5255if ( r == FAIL || called_emsg != called_emsg_start ) { 5256break; 5257} 5258} 5259} 5260 5261/// Implementation of reduce() for Blob "argvars[0]" using the function "expr" 5262/// starting with the optional initial value "argvars[2]" and return the result 5263/// in "rettv". 5264static void reduce_blob ( typval_T * argvars , typval_T * expr , typval_T * rettv ) 5265{ 5266const blob_T * const b = argvars [ 0 ]. vval . v_blob ; 5267const int called_emsg_start = called_emsg ; 5268 5269typval_T initial ; 5270int i ; 5271if ( argvars [ 2 ]. v_type == VAR_UNKNOWN ) { 5272if ( tv_blob_len ( b ) == 0 ) { 5273semsg ( _ ( e_reduce_of_an_empty_str_with_no_initial_value ), "Blob" ); 5274return; 5275} 5276initial = ( typval_T ){ 5277. v_type = VAR_NUMBER , 5278. v_lock = VAR_UNLOCKED , 5279. vval . v_number = tv_blob_get ( b , 0 ), 5280}; 5281i = 1 ; 5282} else if ( tv_check_for_number_arg ( argvars , 2 ) == FAIL ) { 5283return; 5284} else { 5285initial = argvars [ 2 ]; 5286i = 0 ; 5287} 5288 5289tv_copy ( & initial , rettv ); 5290for (; i < tv_blob_len ( b ); i ++ ) { 5291typval_T argv [ 3 ]; 5292argv [ 0 ] = * rettv ; 5293argv [ 1 ] = ( typval_T ){ 5294. v_type = VAR_NUMBER , 5295. v_lock = VAR_UNLOCKED , 5296. vval . v_number = tv_blob_get ( b , i ), 5297}; 5298 5299const int r = eval_expr_typval ( expr , true, argv , 2 , rettv ); 5300 5301if ( r == FAIL || called_emsg != called_emsg_start ) { 5302return; 5303} 5304} 5305} 5306 5307/// "reduce(list, { accumulator, element -> value } [, initial])" function 5308/// "reduce(blob, { accumulator, element -> value } [, initial])" function 5309/// "reduce(string, { accumulator, element -> value } [, initial])" function 5310static void f_reduce ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5311{ 5312if ( argvars [ 0 ]. v_type != VAR_STRING 5313&& argvars [ 0 ]. v_type != VAR_LIST 5314&& argvars [ 0 ]. v_type != VAR_BLOB ) { 5315emsg ( _ ( e_string_list_or_blob_required )); 5316return; 5317} 5318 5319const char * func_name ; 5320if ( argvars [ 1 ]. v_type == VAR_FUNC ) { 5321func_name = argvars [ 1 ]. vval . v_string ; 5322} else if ( argvars [ 1 ]. v_type == VAR_PARTIAL ) { 5323func_name = partial_name ( argvars [ 1 ]. vval . v_partial ); 5324} else { 5325func_name = tv_get_string ( & argvars [ 1 ]); 5326} 5327if ( func_name == NULL || * func_name == NUL ) { 5328emsg ( _ ( e_missing_function_argument )); 5329return; 5330} 5331 5332if ( argvars [ 0 ]. v_type == VAR_LIST ) { 5333reduce_list ( argvars , & argvars [ 1 ], rettv ); 5334} else if ( argvars [ 0 ]. v_type == VAR_STRING ) { 5335reduce_string ( argvars , & argvars [ 1 ], rettv ); 5336} else { 5337reduce_blob ( argvars , & argvars [ 1 ], rettv ); 5338} 5339} 5340 5341#define SP_NOMOVE 0x01 ///< don't move cursor 5342#define SP_REPEAT 0x02 ///< repeat to find outer pair 5343#define SP_RETCOUNT 0x04 ///< return matchcount 5344#define SP_SETPCMARK 0x08 ///< set previous context mark 5345#define SP_START 0x10 ///< accept match at start position 5346#define SP_SUBPAT 0x20 ///< return nr of matching sub-pattern 5347#define SP_END 0x40 ///< leave cursor at end of match 5348#define SP_COLUMN 0x80 ///< start at cursor column 5349 5350/// Get flags for a search function. 5351/// Possibly sets "p_ws". 5352/// 5353/// @return BACKWARD, FORWARD or zero (for an error). 5354static int get_search_arg ( typval_T * varp , int * flagsp ) 5355{ 5356int dir = FORWARD ; 5357 5358if ( varp -> v_type == VAR_UNKNOWN ) { 5359return FORWARD ; 5360} 5361 5362char nbuf [ NUMBUFLEN ]; 5363const char * flags = tv_get_string_buf_chk ( varp , nbuf ); 5364if ( flags == NULL ) { 5365return 0 ; // Type error; errmsg already given. 5366} 5367int mask ; 5368while ( * flags != NUL ) { 5369switch ( * flags ) { 5370case 'b' : 5371dir = BACKWARD ; break; 5372case 'w' : 5373p_ws = true; break; 5374case 'W' : 5375p_ws = false; break; 5376default: 5377mask = 0 ; 5378if ( flagsp != NULL ) { 5379switch ( * flags ) { 5380case 'c' : 5381mask = SP_START ; break; 5382case 'e' : 5383mask = SP_END ; break; 5384case 'm' : 5385mask = SP_RETCOUNT ; break; 5386case 'n' : 5387mask = SP_NOMOVE ; break; 5388case 'p' : 5389mask = SP_SUBPAT ; break; 5390case 'r' : 5391mask = SP_REPEAT ; break; 5392case 's' : 5393mask = SP_SETPCMARK ; break; 5394case 'z' : 5395mask = SP_COLUMN ; break; 5396} 5397} 5398if ( mask == 0 ) { 5399semsg ( _ ( e_invarg2 ), flags ); 5400dir = 0 ; 5401} else { 5402* flagsp |= mask ; 5403} 5404} 5405if ( dir == 0 ) { 5406break; 5407} 5408flags ++ ; 5409} 5410return dir ; 5411} 5412 5413/// Shared by search() and searchpos() functions. 5414static int search_cmn ( typval_T * argvars , pos_T * match_pos , int * flagsp ) 5415{ 5416bool save_p_ws = p_ws ; 5417int retval = 0 ; // default: FAIL 5418linenr_T lnum_stop = 0 ; 5419int64_t time_limit = 0 ; 5420int options = SEARCH_KEEP ; 5421bool use_skip = false; 5422 5423const char * const pat = tv_get_string ( & argvars [ 0 ]); 5424int dir = get_search_arg ( & argvars [ 1 ], flagsp ); // May set p_ws. 5425if ( dir == 0 ) { 5426goto theend ; 5427} 5428int flags = * flagsp ; 5429if ( flags & SP_START ) { 5430options |= SEARCH_START ; 5431} 5432if ( flags & SP_END ) { 5433options |= SEARCH_END ; 5434} 5435if ( flags & SP_COLUMN ) { 5436options |= SEARCH_COL ; 5437} 5438 5439// Optional arguments: line number to stop searching, timeout and skip. 5440if ( argvars [ 1 ]. v_type != VAR_UNKNOWN && argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 5441lnum_stop = ( linenr_T ) tv_get_number_chk ( & argvars [ 2 ], NULL ); 5442if ( lnum_stop < 0 ) { 5443goto theend ; 5444} 5445if ( argvars [ 3 ]. v_type != VAR_UNKNOWN ) { 5446time_limit = tv_get_number_chk ( & argvars [ 3 ], NULL ); 5447if ( time_limit < 0 ) { 5448goto theend ; 5449} 5450use_skip = eval_expr_valid_arg ( & argvars [ 4 ]); 5451} 5452} 5453 5454// Set the time limit, if there is one. 5455proftime_T tm = profile_setlimit ( time_limit ); 5456 5457// This function does not accept SP_REPEAT and SP_RETCOUNT flags. 5458// Check to make sure only those flags are set. 5459// Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both 5460// flags cannot be set. Check for that condition also. 5461if ((( flags & ( SP_REPEAT | SP_RETCOUNT )) != 0 ) 5462|| (( flags & SP_NOMOVE ) && ( flags & SP_SETPCMARK ))) { 5463semsg ( _ ( e_invarg2 ), tv_get_string ( & argvars [ 1 ])); 5464goto theend ; 5465} 5466 5467pos_T save_cursor ; 5468pos_T pos = save_cursor = curwin -> w_cursor ; 5469pos_T firstpos = { 0 }; 5470searchit_arg_T sia = { 5471. sa_stop_lnum = lnum_stop , 5472. sa_tm = & tm , 5473}; 5474 5475const size_t patlen = strlen ( pat ); 5476int subpatnum ; 5477 5478// Repeat until {skip} returns false. 5479while (true) { 5480subpatnum = searchit ( curwin , curbuf , & pos , NULL , dir , ( char * ) pat , patlen , 1 , 5481options , RE_SEARCH , p_magic , & sia ); 5482// finding the first match again means there is no match where {skip} 5483// evaluates to zero. 5484if ( firstpos . lnum != 0 && equalpos ( pos , firstpos )) { 5485subpatnum = FAIL ; 5486} 5487 5488if ( subpatnum == FAIL || ! use_skip ) { 5489// didn't find it or no skip argument 5490break; 5491} 5492if ( firstpos . lnum == 0 ) { 5493firstpos = pos ; 5494} 5495 5496// If the skip expression matches, ignore this match. 5497{ 5498const pos_T save_pos = curwin -> w_cursor ; 5499 5500curwin -> w_cursor = pos ; 5501bool err = false; 5502const bool do_skip = eval_expr_to_bool ( & argvars [ 4 ], & err ); 5503curwin -> w_cursor = save_pos ; 5504if ( err ) { 5505// Evaluating {skip} caused an error, break here. 5506subpatnum = FAIL ; 5507break; 5508} 5509if (! do_skip ) { 5510break; 5511} 5512} 5513 5514// clear the start flag to avoid getting stuck here 5515options &= ~ SEARCH_START ; 5516} 5517 5518if ( subpatnum != FAIL ) { 5519if ( flags & SP_SUBPAT ) { 5520retval = subpatnum ; 5521} else { 5522retval = pos . lnum ; 5523} 5524if ( flags & SP_SETPCMARK ) { 5525setpcmark (); 5526} 5527curwin -> w_cursor = pos ; 5528if ( match_pos != NULL ) { 5529// Store the match cursor position 5530match_pos -> lnum = pos . lnum ; 5531match_pos -> col = pos . col + 1 ; 5532} 5533// "/$" will put the cursor after the end of the line, may need to 5534// correct that here 5535check_cursor ( curwin ); 5536} 5537 5538// If 'n' flag is used: restore cursor position. 5539if ( flags & SP_NOMOVE ) { 5540curwin -> w_cursor = save_cursor ; 5541} else { 5542curwin -> w_set_curswant = true; 5543} 5544theend : 5545p_ws = save_p_ws ; 5546 5547return retval ; 5548} 5549 5550/// "rpcnotify()" function 5551static void f_rpcnotify ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5552{ 5553rettv -> v_type = VAR_NUMBER ; 5554rettv -> vval . v_number = 0 ; 5555 5556if ( check_secure ()) { 5557return; 5558} 5559 5560if ( argvars [ 0 ]. v_type != VAR_NUMBER || argvars [ 0 ]. vval . v_number < 0 ) { 5561semsg ( _ ( e_invarg2 ), "Channel id must be a positive integer" ); 5562return; 5563} 5564 5565if ( argvars [ 1 ]. v_type != VAR_STRING ) { 5566semsg ( _ ( e_invarg2 ), "Event type must be a string" ); 5567return; 5568} 5569 5570MAXSIZE_TEMP_ARRAY ( args , MAX_FUNC_ARGS ); 5571Arena arena = ARENA_EMPTY ; 5572 5573for ( typval_T * tv = argvars + 2 ; tv -> v_type != VAR_UNKNOWN ; tv ++ ) { 5574ADD_C ( args , vim_to_object ( tv , & arena , true)); 5575} 5576 5577bool ok = rpc_send_event (( uint64_t ) argvars [ 0 ]. vval . v_number , 5578tv_get_string ( & argvars [ 1 ]), args ); 5579 5580arena_mem_free ( arena_finish ( & arena )); 5581 5582if (! ok ) { 5583semsg ( _ ( e_invarg2 ), "Channel doesn't exist" ); 5584return; 5585} 5586rettv -> vval . v_number = 1 ; 5587} 5588 5589/// "rpcrequest()" function 5590static void f_rpcrequest ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5591{ 5592rettv -> v_type = VAR_NUMBER ; 5593rettv -> vval . v_number = 0 ; 5594const int l_provider_call_nesting = provider_call_nesting ; 5595 5596if ( check_secure ()) { 5597return; 5598} 5599 5600if ( argvars [ 0 ]. v_type != VAR_NUMBER || argvars [ 0 ]. vval . v_number <= 0 ) { 5601semsg ( _ ( e_invarg2 ), "Channel id must be a positive integer" ); 5602return; 5603} 5604 5605if ( argvars [ 1 ]. v_type != VAR_STRING ) { 5606semsg ( _ ( e_invarg2 ), "Method name must be a string" ); 5607return; 5608} 5609 5610MAXSIZE_TEMP_ARRAY ( args , MAX_FUNC_ARGS ); 5611Arena arena = ARENA_EMPTY ; 5612 5613for ( typval_T * tv = argvars + 2 ; tv -> v_type != VAR_UNKNOWN ; tv ++ ) { 5614ADD_C ( args , vim_to_object ( tv , & arena , true)); 5615} 5616 5617sctx_T save_current_sctx ; 5618char * save_autocmd_fname , * save_autocmd_match ; 5619bool save_autocmd_fname_full ; 5620int save_autocmd_bufnr ; 5621funccal_entry_T funccal_entry ; 5622 5623if ( l_provider_call_nesting ) { 5624// If this is called from a provider function, restore the scope 5625// information of the caller. 5626save_current_sctx = current_sctx ; 5627save_autocmd_fname = autocmd_fname ; 5628save_autocmd_match = autocmd_match ; 5629save_autocmd_fname_full = autocmd_fname_full ; 5630save_autocmd_bufnr = autocmd_bufnr ; 5631save_funccal ( & funccal_entry ); 5632 5633current_sctx = provider_caller_scope . script_ctx ; 5634ga_grow ( & exestack , 1 ); 5635(( estack_T * ) exestack . ga_data )[ exestack . ga_len ++ ] = provider_caller_scope . es_entry ; 5636autocmd_fname = provider_caller_scope . autocmd_fname ; 5637autocmd_match = provider_caller_scope . autocmd_match ; 5638autocmd_fname_full = provider_caller_scope . autocmd_fname_full ; 5639autocmd_bufnr = provider_caller_scope . autocmd_bufnr ; 5640set_current_funccal (( funccall_T * )( provider_caller_scope . funccalp )); 5641} 5642 5643Error err = ERROR_INIT ; 5644 5645uint64_t chan_id = ( uint64_t ) argvars [ 0 ]. vval . v_number ; 5646const char * method = tv_get_string ( & argvars [ 1 ]); 5647 5648ArenaMem res_mem = NULL ; 5649Object result = rpc_send_call ( chan_id , method , args , & res_mem , & err ); 5650arena_mem_free ( arena_finish ( & arena )); 5651 5652if ( l_provider_call_nesting ) { 5653current_sctx = save_current_sctx ; 5654exestack . ga_len -- ; 5655autocmd_fname = save_autocmd_fname ; 5656autocmd_match = save_autocmd_match ; 5657autocmd_fname_full = save_autocmd_fname_full ; 5658autocmd_bufnr = save_autocmd_bufnr ; 5659restore_funccal (); 5660} 5661 5662if ( ERROR_SET ( & err )) { 5663const char * name = NULL ; 5664Channel * chan = find_channel ( chan_id ); 5665if ( chan ) { 5666name = get_client_info ( chan , "name" ); 5667} 5668if ( name ) { 5669semsg_multiline ( "rpc_error" , "Invoking '%s' on channel %" PRIu64 " (%s):\n%s" , 5670method , chan_id , name , err . msg ); 5671} else { 5672semsg_multiline ( "rpc_error" , "Invoking '%s' on channel %" PRIu64 ":\n%s" , 5673method , chan_id , err . msg ); 5674} 5675 5676goto end ; 5677} 5678 5679object_to_vim ( result , rettv , & err ); 5680 5681end : 5682arena_mem_free ( res_mem ); 5683api_clear_error ( & err ); 5684} 5685 5686static void screenchar_adjust ( ScreenGrid ** grid , int * row , int * col ) 5687{ 5688// TODO(bfredl): this is a hack for legacy tests which use screenchar() 5689// to check printed messages on the screen (but not floats etc 5690// as these are not legacy features). If the compositor is refactored to 5691// have its own buffer, this should just read from it instead. 5692msg_scroll_flush (); 5693 5694* grid = ui_comp_get_grid_at_coord ( * row , * col ); 5695 5696// Make `row` and `col` relative to the grid 5697* row -= ( * grid ) -> comp_row ; 5698* col -= ( * grid ) -> comp_col ; 5699} 5700 5701/// "screenattr()" function 5702static void f_screenattr ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5703{ 5704int row = ( int ) tv_get_number_chk ( & argvars [ 0 ], NULL ) - 1 ; 5705int col = ( int ) tv_get_number_chk ( & argvars [ 1 ], NULL ) - 1 ; 5706 5707ScreenGrid * grid ; 5708screenchar_adjust ( & grid , & row , & col ); 5709 5710int c ; 5711if ( row < 0 || row >= grid -> rows || col < 0 || col >= grid -> cols ) { 5712c = -1 ; 5713} else { 5714c = grid -> attrs [ grid -> line_offset [ row ] + ( size_t ) col ]; 5715} 5716rettv -> vval . v_number = c ; 5717} 5718 5719/// "screenchar()" function 5720static void f_screenchar ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5721{ 5722int row = ( int ) tv_get_number_chk ( & argvars [ 0 ], NULL ) - 1 ; 5723int col = ( int ) tv_get_number_chk ( & argvars [ 1 ], NULL ) - 1 ; 5724 5725ScreenGrid * grid ; 5726screenchar_adjust ( & grid , & row , & col ); 5727 5728rettv -> vval . v_number = ( row < 0 || row >= grid -> rows || col < 0 || col >= grid -> cols ) 5729? -1 : schar_get_first_codepoint ( grid_getchar ( grid , row , col , NULL )); 5730} 5731 5732/// "screenchars()" function 5733static void f_screenchars ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5734{ 5735int row = ( int ) tv_get_number_chk ( & argvars [ 0 ], NULL ) - 1 ; 5736int col = ( int ) tv_get_number_chk ( & argvars [ 1 ], NULL ) - 1 ; 5737 5738ScreenGrid * grid ; 5739screenchar_adjust ( & grid , & row , & col ); 5740 5741tv_list_alloc_ret ( rettv , kListLenMayKnow ); 5742if ( row < 0 || row >= grid -> rows || col < 0 || col >= grid -> cols ) { 5743return; 5744} 5745 5746char buf [ MAX_SCHAR_SIZE + 1 ]; 5747schar_get ( buf , grid_getchar ( grid , row , col , NULL )); 5748 5749// schar values are already processed chars which are always NUL-terminated. 5750// A single [0] is expected when char is NUL. 5751size_t i = 0 ; 5752do { 5753int c = utf_ptr2char ( buf + i ); 5754tv_list_append_number ( rettv -> vval . v_list , c ); 5755i += ( size_t ) utf_ptr2len ( buf + i ); 5756} while ( buf [ i ] != NUL ); 5757} 5758 5759/// "screencol()" function 5760/// 5761/// First column is 1 to be consistent with virtcol(). 5762static void f_screencol ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5763{ 5764rettv -> vval . v_number = ui_current_col () + 1 ; 5765} 5766 5767/// "screenrow()" function 5768static void f_screenrow ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5769{ 5770rettv -> vval . v_number = ui_current_row () + 1 ; 5771} 5772 5773/// "screenstring()" function 5774static void f_screenstring ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5775{ 5776rettv -> vval . v_string = NULL ; 5777rettv -> v_type = VAR_STRING ; 5778 5779ScreenGrid * grid ; 5780int row = ( int ) tv_get_number_chk ( & argvars [ 0 ], NULL ) - 1 ; 5781int col = ( int ) tv_get_number_chk ( & argvars [ 1 ], NULL ) - 1 ; 5782 5783screenchar_adjust ( & grid , & row , & col ); 5784 5785if ( row < 0 || row >= grid -> rows || col < 0 || col >= grid -> cols ) { 5786return; 5787} 5788 5789char buf [ MAX_SCHAR_SIZE + 1 ]; 5790schar_get ( buf , grid_getchar ( grid , row , col , NULL )); 5791rettv -> vval . v_string = xstrdup ( buf ); 5792} 5793 5794/// "search()" function 5795static void f_search ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5796{ 5797int flags = 0 ; 5798 5799rettv -> vval . v_number = search_cmn ( argvars , NULL , & flags ); 5800} 5801 5802/// "searchdecl()" function 5803static void f_searchdecl ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5804{ 5805int locally = 1 ; 5806int thisblock = 0 ; 5807bool error = false; 5808 5809rettv -> vval . v_number = 1 ; // default: FAIL 5810 5811const char * const name = tv_get_string_chk ( & argvars [ 0 ]); 5812if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 5813locally = ! tv_get_bool_chk ( & argvars [ 1 ], & error ); 5814if (! error && argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 5815thisblock = ( int ) tv_get_bool_chk ( & argvars [ 2 ], & error ); 5816} 5817} 5818if (! error && name != NULL ) { 5819rettv -> vval . v_number = find_decl (( char * ) name , strlen ( name ), locally , 5820thisblock , SEARCH_KEEP ) == FAIL ; 5821} 5822} 5823 5824/// Used by searchpair() and searchpairpos() 5825static int searchpair_cmn ( typval_T * argvars , pos_T * match_pos ) 5826{ 5827bool save_p_ws = p_ws ; 5828int flags = 0 ; 5829int retval = 0 ; // default: FAIL 5830linenr_T lnum_stop = 0 ; 5831int64_t time_limit = 0 ; 5832 5833// Get the three pattern arguments: start, middle, end. Will result in an 5834// error if not a valid argument. 5835char nbuf1 [ NUMBUFLEN ]; 5836char nbuf2 [ NUMBUFLEN ]; 5837const char * spat = tv_get_string_chk ( & argvars [ 0 ]); 5838const char * mpat = tv_get_string_buf_chk ( & argvars [ 1 ], nbuf1 ); 5839const char * epat = tv_get_string_buf_chk ( & argvars [ 2 ], nbuf2 ); 5840if ( spat == NULL || mpat == NULL || epat == NULL ) { 5841goto theend ; // Type error. 5842} 5843 5844// Handle the optional fourth argument: flags. 5845int dir = get_search_arg ( & argvars [ 3 ], & flags ); // may set p_ws. 5846if ( dir == 0 ) { 5847goto theend ; 5848} 5849 5850// Don't accept SP_END or SP_SUBPAT. 5851// Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set. 5852if (( flags & ( SP_END | SP_SUBPAT )) != 0 5853|| (( flags & SP_NOMOVE ) && ( flags & SP_SETPCMARK ))) { 5854semsg ( _ ( e_invarg2 ), tv_get_string ( & argvars [ 3 ])); 5855goto theend ; 5856} 5857 5858// Using 'r' implies 'W', otherwise it doesn't work. 5859if ( flags & SP_REPEAT ) { 5860p_ws = false; 5861} 5862 5863// Optional fifth argument: skip expression. 5864const typval_T * skip ; 5865if ( argvars [ 3 ]. v_type == VAR_UNKNOWN 5866|| argvars [ 4 ]. v_type == VAR_UNKNOWN ) { 5867skip = NULL ; 5868} else { 5869// Type is checked later. 5870skip = & argvars [ 4 ]; 5871 5872if ( argvars [ 5 ]. v_type != VAR_UNKNOWN ) { 5873lnum_stop = ( linenr_T ) tv_get_number_chk ( & argvars [ 5 ], NULL ); 5874if ( lnum_stop < 0 ) { 5875semsg ( _ ( e_invarg2 ), tv_get_string ( & argvars [ 5 ])); 5876goto theend ; 5877} 5878if ( argvars [ 6 ]. v_type != VAR_UNKNOWN ) { 5879time_limit = tv_get_number_chk ( & argvars [ 6 ], NULL ); 5880if ( time_limit < 0 ) { 5881semsg ( _ ( e_invarg2 ), tv_get_string ( & argvars [ 6 ])); 5882goto theend ; 5883} 5884} 5885} 5886} 5887 5888retval = do_searchpair ( spat , mpat , epat , dir , skip , 5889flags , match_pos , lnum_stop , time_limit ); 5890 5891theend : 5892p_ws = save_p_ws ; 5893 5894return retval ; 5895} 5896 5897/// "searchpair()" function 5898static void f_searchpair ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5899{ 5900rettv -> vval . v_number = searchpair_cmn ( argvars , NULL ); 5901} 5902 5903/// "searchpairpos()" function 5904static void f_searchpairpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 5905{ 5906pos_T match_pos ; 5907int lnum = 0 ; 5908int col = 0 ; 5909 5910tv_list_alloc_ret ( rettv , 2 ); 5911 5912if ( searchpair_cmn ( argvars , & match_pos ) > 0 ) { 5913lnum = match_pos . lnum ; 5914col = match_pos . col ; 5915} 5916 5917tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) lnum ); 5918tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) col ); 5919} 5920 5921/// Search for a start/middle/end thing. 5922/// Used by searchpair(), see its documentation for the details. 5923/// 5924/// @param spat start pattern 5925/// @param mpat middle pattern 5926/// @param epat end pattern 5927/// @param dir BACKWARD or FORWARD 5928/// @param skip skip expression 5929/// @param flags SP_SETPCMARK and other SP_ values 5930/// @param lnum_stop stop at this line if not zero 5931/// @param time_limit stop after this many msec 5932/// 5933/// @returns 0 or -1 for no match, 5934int do_searchpair (const char * spat , const char * mpat , const char * epat , int dir , 5935const typval_T * skip , int flags , pos_T * match_pos , linenr_T lnum_stop , 5936int64_t time_limit ) 5937FUNC_ATTR_NONNULL_ARG ( 1 , 2 , 3 ) 5938{ 5939int retval = 0 ; 5940int nest = 1 ; 5941bool use_skip = false; 5942int options = SEARCH_KEEP ; 5943 5944// Make 'cpoptions' empty, the 'l' flag should not be used here. 5945char * save_cpo = p_cpo ; 5946p_cpo = empty_string_option ; 5947 5948// Set the time limit, if there is one. 5949proftime_T tm = profile_setlimit ( time_limit ); 5950 5951// Make two search patterns: start/end (pat2, for in nested pairs) and 5952// start/middle/end (pat3, for the top pair). 5953const size_t spatlen = strlen ( spat ); 5954const size_t epatlen = strlen ( epat ); 5955const size_t pat2size = spatlen + epatlen + 17 ; 5956char * pat2 = xmalloc ( pat2size ); 5957const size_t pat3size = spatlen + strlen ( mpat ) + epatlen + 25 ; 5958char * pat3 = xmalloc ( pat3size ); 5959int pat2len = snprintf ( pat2 , pat2size , "\\m\\(%s\\m\\)\\|\\(%s\\m\\)" , spat , epat ); 5960int pat3len ; 5961if ( * mpat == NUL ) { 5962STRCPY ( pat3 , pat2 ); 5963pat3len = pat2len ; 5964} else { 5965pat3len = snprintf ( pat3 , pat3size , 5966"\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)" , spat , epat , mpat ); 5967} 5968if ( flags & SP_START ) { 5969options |= SEARCH_START ; 5970} 5971 5972if ( skip != NULL ) { 5973use_skip = eval_expr_valid_arg ( skip ); 5974} 5975 5976pos_T save_cursor = curwin -> w_cursor ; 5977pos_T pos = curwin -> w_cursor ; 5978pos_T firstpos ; 5979clearpos ( & firstpos ); 5980pos_T foundpos ; 5981clearpos ( & foundpos ); 5982char * pat = pat3 ; 5983assert ( pat3len >= 0 ); 5984size_t patlen = ( size_t ) pat3len ; 5985while (true) { 5986searchit_arg_T sia = { 5987. sa_stop_lnum = lnum_stop , 5988. sa_tm = & tm , 5989}; 5990 5991int n = searchit ( curwin , curbuf , & pos , NULL , dir , pat , patlen , 1 , 5992options , RE_SEARCH , true, & sia ); 5993if ( n == FAIL || ( firstpos . lnum != 0 && equalpos ( pos , firstpos ))) { 5994// didn't find it or found the first match again: FAIL 5995break; 5996} 5997 5998if ( firstpos . lnum == 0 ) { 5999firstpos = pos ; 6000} 6001if ( equalpos ( pos , foundpos )) { 6002// Found the same position again. Can happen with a pattern that 6003// has "\zs" at the end and searching backwards. Advance one 6004// character and try again. 6005if ( dir == BACKWARD ) { 6006decl ( & pos ); 6007} else { 6008incl ( & pos ); 6009} 6010} 6011foundpos = pos ; 6012 6013// clear the start flag to avoid getting stuck here 6014options &= ~ SEARCH_START ; 6015 6016// If the skip pattern matches, ignore this match. 6017if ( use_skip ) { 6018pos_T save_pos = curwin -> w_cursor ; 6019curwin -> w_cursor = pos ; 6020bool err = false; 6021const bool r = eval_expr_to_bool ( skip , & err ); 6022curwin -> w_cursor = save_pos ; 6023if ( err ) { 6024// Evaluating {skip} caused an error, break here. 6025curwin -> w_cursor = save_cursor ; 6026retval = -1 ; 6027break; 6028} 6029if ( r ) { 6030continue; 6031} 6032} 6033 6034if (( dir == BACKWARD && n == 3 ) || ( dir == FORWARD && n == 2 )) { 6035// Found end when searching backwards or start when searching 6036// forward: nested pair. 6037nest ++ ; 6038pat = pat2 ; // nested, don't search for middle 6039} else { 6040// Found end when searching forward or start when searching 6041// backward: end of (nested) pair; or found middle in outer pair. 6042if ( -- nest == 1 ) { 6043pat = pat3 ; // outer level, search for middle 6044} 6045} 6046 6047if ( nest == 0 ) { 6048// Found the match: return matchcount or line number. 6049if ( flags & SP_RETCOUNT ) { 6050retval ++ ; 6051} else { 6052retval = pos . lnum ; 6053} 6054if ( flags & SP_SETPCMARK ) { 6055setpcmark (); 6056} 6057curwin -> w_cursor = pos ; 6058if (!( flags & SP_REPEAT )) { 6059break; 6060} 6061nest = 1 ; // search for next unmatched 6062} 6063} 6064 6065if ( match_pos != NULL ) { 6066// Store the match cursor position 6067match_pos -> lnum = curwin -> w_cursor . lnum ; 6068match_pos -> col = curwin -> w_cursor . col + 1 ; 6069} 6070 6071// If 'n' flag is used or search failed: restore cursor position. 6072if (( flags & SP_NOMOVE ) || retval == 0 ) { 6073curwin -> w_cursor = save_cursor ; 6074} 6075 6076xfree ( pat2 ); 6077xfree ( pat3 ); 6078if ( p_cpo == empty_string_option ) { 6079p_cpo = save_cpo ; 6080} else { 6081// Darn, evaluating the {skip} expression changed the value. 6082// If it's still empty it was changed and restored, need to restore in 6083// the complicated way. 6084if ( * p_cpo == NUL ) { 6085set_option_value_give_err ( kOptCpoptions , CSTR_AS_OBJ ( save_cpo ), 0 ); 6086} 6087free_string_option ( save_cpo ); 6088} 6089 6090return retval ; 6091} 6092 6093/// "searchpos()" function 6094static void f_searchpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6095{ 6096pos_T match_pos ; 6097int flags = 0 ; 6098 6099const int n = search_cmn ( argvars , & match_pos , & flags ); 6100 6101tv_list_alloc_ret ( rettv , 2 + (!!( flags & SP_SUBPAT ))); 6102 6103const int lnum = ( n > 0 ? match_pos . lnum : 0 ); 6104const int col = ( n > 0 ? match_pos . col : 0 ); 6105 6106tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) lnum ); 6107tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) col ); 6108if ( flags & SP_SUBPAT ) { 6109tv_list_append_number ( rettv -> vval . v_list , ( varnumber_T ) n ); 6110} 6111} 6112 6113/// "serverlist()" function 6114static void f_serverlist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6115{ 6116size_t n ; 6117char ** addrs = server_address_list ( & n ); 6118 6119// Get the (internal) address list as a typval to pass to Lua. 6120typval_T addrs_tv ; 6121tv_list_alloc_ret ( & addrs_tv , ( ptrdiff_t ) n ); 6122for ( size_t i = 0 ; i < n ; i ++ ) { 6123tv_list_append_allocated_string ( addrs_tv . vval . v_list , addrs [ i ]); 6124} 6125xfree ( addrs ); 6126 6127// Lua handles options (e.g. {peer=true}), peer discovery, and returns combined list. 6128typval_T opts = argvars [ 0 ]. v_type != VAR_UNKNOWN ? argvars [ 0 ] : ( typval_T ){ . v_type = VAR_SPECIAL , 6129. vval . v_special = 6130kSpecialVarNull }; 6131typval_T lua_args [] = { opts , addrs_tv , { . v_type = VAR_UNKNOWN } }; 6132nlua_call_typval ( "vim._core.server" , "serverlist" , lua_args , rettv ); 6133tv_clear ( & addrs_tv ); 6134} 6135 6136/// "serverstart()" function 6137static void f_serverstart ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6138{ 6139rettv -> v_type = VAR_STRING ; 6140rettv -> vval . v_string = NULL ; // Address of the new server 6141 6142if ( check_secure ()) { 6143return; 6144} 6145 6146char * address ; 6147// If the user supplied an address, use it, otherwise use a temp. 6148if ( argvars [ 0 ]. v_type != VAR_UNKNOWN ) { 6149if ( argvars [ 0 ]. v_type != VAR_STRING ) { 6150emsg ( _ ( e_invarg )); 6151return; 6152} 6153address = TO_SLASH_SAVE ( tv_get_string ( argvars )); 6154} else { 6155address = server_address_new ( NULL ); 6156} 6157 6158int result = server_start ( address ); 6159xfree ( address ); 6160 6161if ( result != 0 ) { 6162semsg ( "Failed to start server: %s" , 6163result > 0 ? "Unknown system error" : uv_strerror ( result )); 6164return; 6165} 6166 6167// Since it's possible server_start adjusted the given {address} (e.g., 6168// "localhost:" will now have a port), return the final value to the user. 6169size_t n ; 6170char ** addrs = server_address_list ( & n ); 6171rettv -> vval . v_string = addrs [ n - 1 ]; 6172 6173n -- ; 6174for ( size_t i = 0 ; i < n ; i ++ ) { 6175xfree ( addrs [ i ]); 6176} 6177xfree ( addrs ); 6178} 6179 6180/// "serverstop()" function 6181static void f_serverstop ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6182{ 6183if ( check_secure ()) { 6184return; 6185} 6186 6187if ( argvars [ 0 ]. v_type != VAR_STRING ) { 6188emsg ( _ ( e_invarg )); 6189return; 6190} 6191 6192rettv -> v_type = VAR_NUMBER ; 6193rettv -> vval . v_number = 0 ; 6194if ( argvars [ 0 ]. vval . v_string ) { 6195bool rv = server_stop ( argvars [ 0 ]. vval . v_string , false); 6196rettv -> vval . v_number = ( rv ? 1 : 0 ); 6197} 6198} 6199 6200/// Set the cursor or mark position. 6201/// If "charpos" is true, then use the column number as a character offset. 6202/// Otherwise use the column number as a byte offset. 6203static void set_position ( typval_T * argvars , typval_T * rettv , bool charpos ) 6204{ 6205colnr_T curswant = -1 ; 6206 6207rettv -> vval . v_number = -1 ; 6208const char * const name = tv_get_string_chk ( argvars ); 6209if ( name == NULL ) { 6210return; 6211} 6212 6213pos_T pos ; 6214int fnum ; 6215if ( list2fpos ( & argvars [ 1 ], & pos , & fnum , & curswant , charpos ) != OK ) { 6216return; 6217} 6218 6219if ( pos . col != MAXCOL && -- pos . col < 0 ) { 6220pos . col = 0 ; 6221} 6222if ( name [ 0 ] == '.' && name [ 1 ] == NUL ) { 6223// set cursor; "fnum" is ignored 6224curwin -> w_cursor = pos ; 6225if ( curswant >= 0 ) { 6226curwin -> w_curswant = curswant - 1 ; 6227curwin -> w_set_curswant = false; 6228} 6229check_cursor ( curwin ); 6230rettv -> vval . v_number = 0 ; 6231} else if ( name [ 0 ] == '\'' && name [ 1 ] != NUL && name [ 2 ] == NUL ) { 6232// set mark 6233if ( setmark_pos (( uint8_t ) name [ 1 ], & pos , fnum , NULL ) == OK ) { 6234rettv -> vval . v_number = 0 ; 6235} 6236} else { 6237emsg ( _ ( e_invarg )); 6238} 6239} 6240 6241/// "setcharpos()" function 6242static void f_setcharpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6243{ 6244set_position ( argvars , rettv , true); 6245} 6246 6247static void f_setcharsearch ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6248{ 6249if ( tv_check_for_dict_arg ( argvars , 0 ) == FAIL ) { 6250return; 6251} 6252 6253dict_T * d = argvars [ 0 ]. vval . v_dict ; 6254if ( d == NULL ) { 6255return; 6256} 6257 6258char * const csearch = tv_dict_get_string ( d , "char" , false); 6259if ( csearch != NULL ) { 6260int c = utf_ptr2char ( csearch ); 6261set_last_csearch ( c , csearch , utfc_ptr2len ( csearch )); 6262} 6263 6264dictitem_T * di = tv_dict_find ( d , S_LEN ( "forward" )); 6265if ( di != NULL ) { 6266set_csearch_direction ( tv_get_number ( & di -> di_tv ) ? FORWARD : BACKWARD ); 6267} 6268 6269di = tv_dict_find ( d , S_LEN ( "until" )); 6270if ( di != NULL ) { 6271set_csearch_until (!! tv_get_number ( & di -> di_tv )); 6272} 6273} 6274 6275/// "setcursorcharpos" function 6276static void f_setcursorcharpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6277{ 6278set_cursorpos ( argvars , rettv , true); 6279} 6280 6281/// "setenv()" function 6282static void f_setenv ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6283{ 6284char namebuf [ NUMBUFLEN ]; 6285char valbuf [ NUMBUFLEN ]; 6286const char * name = tv_get_string_buf ( & argvars [ 0 ], namebuf ); 6287 6288// setting an environment variable may be dangerous, e.g. you could 6289// setenv GCONV_PATH=/tmp and then have iconv() unexpectedly call 6290// a shell command using some shared library: 6291if ( check_secure ()) { 6292return; 6293} 6294 6295if ( argvars [ 1 ]. v_type == VAR_SPECIAL 6296&& argvars [ 1 ]. vval . v_special == kSpecialVarNull ) { 6297vim_unsetenv_ext ( name ); 6298} else { 6299vim_setenv_ext ( name , tv_get_string_buf ( & argvars [ 1 ], valbuf )); 6300} 6301} 6302 6303/// "setfperm({fname}, {mode})" function 6304static void f_setfperm ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6305{ 6306rettv -> vval . v_number = 0 ; 6307 6308const char * const fname = tv_get_string_chk ( & argvars [ 0 ]); 6309if ( fname == NULL ) { 6310return; 6311} 6312 6313char modebuf [ NUMBUFLEN ]; 6314const char * const mode_str = tv_get_string_buf_chk ( & argvars [ 1 ], modebuf ); 6315if ( mode_str == NULL ) { 6316return; 6317} 6318if ( strlen ( mode_str ) != 9 ) { 6319semsg ( _ ( e_invarg2 ), mode_str ); 6320return; 6321} 6322 6323int mask = 1 ; 6324int mode = 0 ; 6325for ( int i = 8 ; i >= 0 ; i -- ) { 6326if ( mode_str [ i ] != '-' ) { 6327mode |= mask ; 6328} 6329mask = mask << 1 ; 6330} 6331rettv -> vval . v_number = os_setperm ( fname , mode ) == OK ; 6332} 6333 6334/// "setpos()" function 6335static void f_setpos ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6336{ 6337set_position ( argvars , rettv , false); 6338} 6339 6340/// Translate a register type string to the yank type and block length 6341static int get_yank_type ( char ** const pp , MotionType * const yank_type , int * const block_len ) 6342FUNC_ATTR_NONNULL_ALL 6343{ 6344char * stropt = * pp ; 6345switch ( * stropt ) { 6346case 'v' : 6347case 'c' : // character-wise selection 6348* yank_type = kMTCharWise ; 6349break; 6350case 'V' : 6351case 'l' : // line-wise selection 6352* yank_type = kMTLineWise ; 6353break; 6354case 'b' : 6355case Ctrl_V : // block-wise selection 6356* yank_type = kMTBlockWise ; 6357if ( ascii_isdigit ( stropt [ 1 ])) { 6358stropt ++ ; 6359* block_len = getdigits_int ( & stropt , false, 0 ) - 1 ; 6360stropt -- ; 6361} 6362break; 6363default: 6364return FAIL ; 6365} 6366* pp = stropt ; 6367return OK ; 6368} 6369 6370/// "setreg()" function 6371static void f_setreg ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6372{ 6373bool append = false; 6374 6375int block_len = -1 ; 6376MotionType yank_type = kMTUnknown ; 6377 6378rettv -> vval . v_number = 1 ; // FAIL is default. 6379 6380const char * const strregname = tv_get_string_chk ( argvars ); 6381if ( strregname == NULL ) { 6382return; // Type error; errmsg already given. 6383} 6384char regname = * strregname ; 6385if ( regname == 0 || regname == '@' ) { 6386regname = '"' ; 6387} 6388 6389const typval_T * regcontents = NULL ; 6390char pointreg = 0 ; 6391if ( argvars [ 1 ]. v_type == VAR_DICT ) { 6392dict_T * const d = argvars [ 1 ]. vval . v_dict ; 6393 6394if ( tv_dict_len ( d ) == 0 ) { 6395// Empty dict, clear the register (like setreg(0, [])) 6396char * lstval [ 2 ] = { NULL , NULL }; 6397write_reg_contents_lst ( regname , lstval , false, kMTUnknown , -1 ); 6398return; 6399} 6400 6401dictitem_T * const di = tv_dict_find ( d , "regcontents" , -1 ); 6402if ( di != NULL ) { 6403regcontents = & di -> di_tv ; 6404} 6405 6406const char * stropt = tv_dict_get_string ( d , "regtype" , false); 6407if ( stropt != NULL ) { 6408const int ret = get_yank_type (( char ** ) & stropt , & yank_type , & block_len ); 6409 6410if ( ret == FAIL || * ( ++ stropt ) != NUL ) { 6411semsg ( _ ( e_invargval ), "value" ); 6412return; 6413} 6414} 6415 6416if ( regname == '"' ) { 6417stropt = tv_dict_get_string ( d , "points_to" , false); 6418if ( stropt != NULL ) { 6419pointreg = * stropt ; 6420regname = pointreg ; 6421} 6422} else if ( tv_dict_get_bool ( d , "isunnamed" , -1 ) > 0 ) { 6423pointreg = regname ; 6424} 6425} else { 6426regcontents = & argvars [ 1 ]; 6427} 6428 6429bool set_unnamed = false; 6430if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 6431if ( yank_type != kMTUnknown ) { 6432semsg ( _ ( e_toomanyarg ), "setreg" ); 6433return; 6434} 6435 6436const char * stropt = tv_get_string_chk ( & argvars [ 2 ]); 6437if ( stropt == NULL ) { 6438return; // Type error. 6439} 6440for (; * stropt != NUL ; stropt ++ ) { 6441switch ( * stropt ) { 6442case 'a' : 6443case 'A' : // append 6444append = true; 6445break; 6446case 'u' : 6447case '"' : // unnamed register 6448set_unnamed = true; 6449break; 6450default: 6451get_yank_type (( char ** ) & stropt , & yank_type , & block_len ); 6452} 6453} 6454} 6455 6456if ( regcontents != NULL && regcontents -> v_type == VAR_LIST ) { 6457list_T * const ll = regcontents -> vval . v_list ; 6458// If the list is NULL handle like an empty list. 6459const int len = tv_list_len ( ll ); 6460 6461// First half: use for pointers to result lines; second half: use for 6462// pointers to allocated copies. 6463char ** lstval = xmalloc (sizeof( char * ) * ((( size_t ) len + 1 ) * 2 )); 6464const char ** curval = (const char ** ) lstval ; 6465char ** allocval = lstval + len + 2 ; 6466char ** curallocval = allocval ; 6467 6468TV_LIST_ITER_CONST ( ll , li , { 6469char buf [ NUMBUFLEN ]; 6470* curval = tv_get_string_buf_chk ( TV_LIST_ITEM_TV ( li ), buf ); 6471if ( * curval == NULL ) { 6472goto free_lstval ; 6473} 6474if ( * curval == buf ) { 6475// Need to make a copy, 6476// next tv_get_string_buf_chk() will overwrite the string. 6477* curallocval = xstrdup ( * curval ); 6478* curval = * curallocval ; 6479curallocval ++ ; 6480} 6481curval ++ ; 6482}); 6483* curval ++ = NULL ; 6484 6485write_reg_contents_lst ( regname , lstval , append , yank_type , ( colnr_T ) block_len ); 6486 6487free_lstval : 6488while ( curallocval > allocval ) { 6489xfree ( *-- curallocval ); 6490} 6491xfree ( lstval ); 6492} else if ( regcontents != NULL ) { 6493const char * const strval = tv_get_string_chk ( regcontents ); 6494if ( strval == NULL ) { 6495return; 6496} 6497write_reg_contents_ex ( regname , strval , ( ssize_t ) strlen ( strval ), 6498append , yank_type , ( colnr_T ) block_len ); 6499} 6500if ( pointreg != 0 ) { 6501get_yank_register ( pointreg , YREG_YANK ); 6502} 6503rettv -> vval . v_number = 0 ; 6504 6505if ( set_unnamed ) { 6506// Discard the result. We already handle the error case. 6507op_reg_set_previous ( regname ); 6508} 6509} 6510 6511/// "settagstack()" function 6512static void f_settagstack ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6513{ 6514static const char * e_invact2 = N_ ( "E962: Invalid action: '%s'" ); 6515char action = 'r' ; 6516 6517rettv -> vval . v_number = -1 ; 6518 6519// first argument: window number or id 6520win_T * wp = find_win_by_nr_or_id ( & argvars [ 0 ]); 6521if ( wp == NULL ) { 6522return; 6523} 6524 6525// second argument: dict with items to set in the tag stack 6526if ( tv_check_for_dict_arg ( argvars , 1 ) == FAIL ) { 6527return; 6528} 6529dict_T * d = argvars [ 1 ]. vval . v_dict ; 6530if ( d == NULL ) { 6531return; 6532} 6533 6534// third argument: action - 'a' for append and 'r' for replace. 6535// default is to replace the stack. 6536if ( argvars [ 2 ]. v_type == VAR_UNKNOWN ) { 6537// action = 'r'; 6538} else if ( tv_check_for_string_arg ( argvars , 2 ) == FAIL ) { 6539return; 6540} else { 6541const char * actstr ; 6542actstr = tv_get_string_chk ( & argvars [ 2 ]); 6543if ( actstr == NULL ) { 6544return; 6545} 6546if (( * actstr == 'r' || * actstr == 'a' || * actstr == 't' ) 6547&& actstr [ 1 ] == NUL ) { 6548action = * actstr ; 6549} else { 6550semsg ( _ ( e_invact2 ), actstr ); 6551return; 6552} 6553} 6554 6555if ( set_tagstack ( wp , d , action ) == OK ) { 6556rettv -> vval . v_number = 0 ; 6557} 6558} 6559 6560/// "sha256({expr})" function 6561static void f_sha256 ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6562{ 6563rettv -> v_type = VAR_STRING ; 6564rettv -> vval . v_string = NULL ; 6565 6566if ( argvars [ 0 ]. v_type == VAR_BLOB ) { 6567blob_T * blob = argvars [ 0 ]. vval . v_blob ; 6568const uint8_t * p = blob != NULL ? ( uint8_t * ) blob -> bv_ga . ga_data : ( uint8_t * ) "" ; 6569int len = blob != NULL ? blob -> bv_ga . ga_len : 0 ; 6570rettv -> vval . v_string = xstrdup ( sha256_bytes ( p , ( size_t ) len , NULL , 0 )); 6571} else { 6572const char * p = tv_get_string ( & argvars [ 0 ]); 6573const char * hash = sha256_bytes ((const uint8_t * ) p , strlen ( p ), NULL , 0 ); 6574rettv -> vval . v_string = xstrdup ( hash ); 6575} 6576} 6577 6578/// "shellescape({string})" function 6579static void f_shellescape ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6580{ 6581const bool do_special = non_zero_arg ( & argvars [ 1 ]); 6582 6583rettv -> vval . v_string = 6584vim_strsave_shellescape ( tv_get_string ( & argvars [ 0 ]), do_special , do_special ); 6585rettv -> v_type = VAR_STRING ; 6586} 6587 6588/// shiftwidth() function 6589static void f_shiftwidth ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6590{ 6591rettv -> vval . v_number = 0 ; 6592 6593if ( argvars [ 0 ]. v_type != VAR_UNKNOWN ) { 6594colnr_T col = ( colnr_T ) tv_get_number_chk ( argvars , NULL ); 6595if ( col < 0 ) { 6596return; // type error; errmsg already given 6597} 6598rettv -> vval . v_number = get_sw_value_col ( curbuf , col , false); 6599return; 6600} 6601rettv -> vval . v_number = get_sw_value ( curbuf ); 6602} 6603 6604/// "sockconnect()" function 6605static void f_sockconnect ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6606{ 6607if ( argvars [ 0 ]. v_type != VAR_STRING || argvars [ 1 ]. v_type != VAR_STRING ) { 6608emsg ( _ ( e_invarg )); 6609return; 6610} 6611if ( argvars [ 2 ]. v_type != VAR_DICT && argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 6612// Wrong argument types 6613semsg ( _ ( e_invarg2 ), "expected dictionary" ); 6614return; 6615} 6616 6617const char * mode = tv_get_string ( & argvars [ 0 ]); 6618const char * address = TO_SLASH_SAVE ( tv_get_string ( & argvars [ 1 ])); 6619 6620bool tcp ; 6621if ( strcmp ( mode , "tcp" ) == 0 ) { 6622tcp = true; 6623} else if ( strcmp ( mode , "pipe" ) == 0 ) { 6624tcp = false; 6625} else { 6626semsg ( _ ( e_invarg2 ), "invalid mode" ); 6627goto cleanup ; 6628} 6629 6630bool rpc = false; 6631CallbackReader on_data = CALLBACK_READER_INIT ; 6632if ( argvars [ 2 ]. v_type == VAR_DICT ) { 6633dict_T * opts = argvars [ 2 ]. vval . v_dict ; 6634rpc = tv_dict_get_number ( opts , "rpc" ) != 0 ; 6635 6636if (! tv_dict_get_callback ( opts , S_LEN ( "on_data" ), & on_data . cb )) { 6637goto cleanup ; 6638} 6639on_data . buffered = tv_dict_get_number ( opts , "data_buffered" ); 6640if ( on_data . buffered && on_data . cb . type == kCallbackNone ) { 6641on_data . self = opts ; 6642} 6643} 6644 6645const char * error = NULL ; 6646uint64_t id = channel_connect ( tcp , address , rpc , on_data , 50 , & error ); 6647 6648if ( error ) { 6649semsg ( _ ( "connection failed: %s" ), error ); 6650} 6651 6652rettv -> vval . v_number = ( varnumber_T ) id ; 6653rettv -> v_type = VAR_NUMBER ; 6654cleanup : 6655XFREE_CLEAR ( address ); 6656} 6657 6658/// "stdioopen()" function 6659static void f_stdioopen ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6660{ 6661if ( argvars [ 0 ]. v_type != VAR_DICT ) { 6662emsg ( _ ( e_invarg )); 6663return; 6664} 6665 6666CallbackReader on_stdin = CALLBACK_READER_INIT ; 6667dict_T * opts = argvars [ 0 ]. vval . v_dict ; 6668bool rpc = tv_dict_get_number ( opts , "rpc" ) != 0 ; 6669 6670if (! tv_dict_get_callback ( opts , S_LEN ( "on_stdin" ), & on_stdin . cb )) { 6671return; 6672} 6673if (! tv_dict_get_callback ( opts , S_LEN ( "on_print" ), & on_print )) { 6674return; 6675} 6676 6677on_stdin . buffered = tv_dict_get_number ( opts , "stdin_buffered" ); 6678if ( on_stdin . buffered && on_stdin . cb . type == kCallbackNone ) { 6679on_stdin . self = opts ; 6680} 6681 6682const char * error ; 6683uint64_t id = channel_from_stdio ( rpc , on_stdin , & error ); 6684if (! id ) { 6685semsg ( e_stdiochan2 , error ); 6686} 6687 6688rettv -> vval . v_number = ( varnumber_T ) id ; 6689rettv -> v_type = VAR_NUMBER ; 6690} 6691 6692/// "reltimefloat()" function 6693static void f_reltimefloat ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6694FUNC_ATTR_NONNULL_ALL 6695{ 6696proftime_T tm ; 6697 6698rettv -> v_type = VAR_FLOAT ; 6699rettv -> vval . v_float = 0 ; 6700if ( list2proftime ( & argvars [ 0 ], & tm ) == OK ) { 6701rettv -> vval . v_float = ( float_T ) profile_signed ( tm ) / NS_PER_SEC ; 6702} 6703} 6704 6705/// "soundfold({word})" function 6706static void f_soundfold ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6707{ 6708rettv -> v_type = VAR_STRING ; 6709const char * const s = tv_get_string ( & argvars [ 0 ]); 6710rettv -> vval . v_string = eval_soundfold ( s ); 6711} 6712 6713/// "spellbadword()" function 6714static void f_spellbadword ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6715{ 6716const int wo_spell_save = curwin -> w_p_spell ; 6717 6718if (! curwin -> w_p_spell ) { 6719parse_spelllang ( curwin ); 6720curwin -> w_p_spell = true; 6721} 6722 6723if ( * curwin -> w_s -> b_p_spl == NUL ) { 6724emsg ( _ ( e_no_spell )); 6725curwin -> w_p_spell = wo_spell_save ; 6726return; 6727} 6728 6729const char * word = "" ; 6730hlf_T attr = HLF_COUNT ; 6731size_t len = 0 ; 6732if ( argvars [ 0 ]. v_type == VAR_UNKNOWN ) { 6733// Find the start and length of the badly spelled word. 6734len = spell_move_to ( curwin , FORWARD , SMT_ALL , true, & attr ); 6735if ( len != 0 ) { 6736word = get_cursor_pos_ptr (); 6737curwin -> w_set_curswant = true; 6738} 6739} else if ( * curbuf -> b_s . b_p_spl != NUL ) { 6740const char * str = tv_get_string_chk ( & argvars [ 0 ]); 6741int capcol = -1 ; 6742 6743if ( str != NULL ) { 6744// Check the argument for spelling. 6745while ( * str != NUL ) { 6746len = spell_check ( curwin , ( char * ) str , & attr , & capcol , false); 6747if ( attr != HLF_COUNT ) { 6748word = str ; 6749break; 6750} 6751str += len ; 6752capcol -= ( int ) len ; 6753len = 0 ; 6754} 6755} 6756} 6757curwin -> w_p_spell = wo_spell_save ; 6758 6759assert ( len <= INT_MAX ); 6760tv_list_alloc_ret ( rettv , 2 ); 6761tv_list_append_string ( rettv -> vval . v_list , word , ( ssize_t ) len ); 6762switch ( attr ) { 6763case HLF_SPB : 6764tv_list_append_string ( rettv -> vval . v_list , S_LEN ( "bad" )); break; 6765case HLF_SPR : 6766tv_list_append_string ( rettv -> vval . v_list , S_LEN ( "rare" )); break; 6767case HLF_SPL : 6768tv_list_append_string ( rettv -> vval . v_list , S_LEN ( "local" )); break; 6769case HLF_SPC : 6770tv_list_append_string ( rettv -> vval . v_list , S_LEN ( "caps" )); break; 6771default: 6772tv_list_append_string ( rettv -> vval . v_list , NULL , -1 ); break; 6773} 6774} 6775 6776/// "spellsuggest()" function 6777static void f_spellsuggest ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6778{ 6779garray_T ga = GA_EMPTY_INIT_VALUE ; 6780const int wo_spell_save = curwin -> w_p_spell ; 6781 6782if (! curwin -> w_p_spell ) { 6783parse_spelllang ( curwin ); 6784curwin -> w_p_spell = true; 6785} 6786 6787if ( * curwin -> w_s -> b_p_spl == NUL ) { 6788emsg ( _ ( e_no_spell )); 6789curwin -> w_p_spell = wo_spell_save ; 6790return; 6791} 6792 6793int maxcount ; 6794bool need_capital = false; 6795const char * const str = tv_get_string ( & argvars [ 0 ]); 6796if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 6797bool typeerr = false; 6798maxcount = ( int ) tv_get_number_chk ( & argvars [ 1 ], & typeerr ); 6799if ( maxcount <= 0 ) { 6800goto f_spellsuggest_return ; 6801} 6802if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 6803need_capital = tv_get_bool_chk ( & argvars [ 2 ], & typeerr ); 6804if ( typeerr ) { 6805goto f_spellsuggest_return ; 6806} 6807} 6808} else { 6809maxcount = 25 ; 6810} 6811 6812spell_suggest_list ( & ga , ( char * ) str , maxcount , need_capital , false); 6813 6814f_spellsuggest_return : 6815tv_list_alloc_ret ( rettv , ( ptrdiff_t ) ga . ga_len ); 6816for ( int i = 0 ; i < ga . ga_len ; i ++ ) { 6817char * const p = (( char ** ) ga . ga_data )[ i ]; 6818tv_list_append_allocated_string ( rettv -> vval . v_list , p ); 6819} 6820ga_clear ( & ga ); 6821curwin -> w_p_spell = wo_spell_save ; 6822} 6823 6824static void f_split ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6825{ 6826colnr_T col = 0 ; 6827bool keepempty = false; 6828bool typeerr = false; 6829 6830// Make 'cpoptions' empty, the 'l' flag should not be used here. 6831char * save_cpo = p_cpo ; 6832p_cpo = empty_string_option ; 6833 6834const char * str = tv_get_string ( & argvars [ 0 ]); 6835const char * pat = NULL ; 6836char patbuf [ NUMBUFLEN ]; 6837if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 6838pat = tv_get_string_buf_chk ( & argvars [ 1 ], patbuf ); 6839if ( pat == NULL ) { 6840typeerr = true; 6841} 6842if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 6843keepempty = ( bool ) tv_get_bool_chk ( & argvars [ 2 ], & typeerr ); 6844} 6845} 6846if ( pat == NULL || * pat == NUL ) { 6847pat = "[\\x01- ]\\+" ; 6848} 6849 6850tv_list_alloc_ret ( rettv , kListLenMayKnow ); 6851 6852if ( typeerr ) { 6853goto theend ; 6854} 6855 6856regmatch_T regmatch = { 6857. regprog = vim_regcomp ( pat , RE_MAGIC + RE_STRING ), 6858. startp = { NULL }, 6859. endp = { NULL }, 6860. rm_ic = false, 6861}; 6862if ( regmatch . regprog != NULL ) { 6863while ( * str != NUL || keepempty ) { 6864bool match ; 6865if ( * str == NUL ) { 6866match = false; // Empty item at the end. 6867} else { 6868match = vim_regexec_nl ( & regmatch , str , col ); 6869} 6870const char * end ; 6871if ( match ) { 6872end = regmatch . startp [ 0 ]; 6873} else { 6874end = str + strlen ( str ); 6875} 6876if ( keepempty || end > str || ( tv_list_len ( rettv -> vval . v_list ) > 0 6877&& * str != NUL 6878&& match 6879&& end < regmatch . endp [ 0 ])) { 6880tv_list_append_string ( rettv -> vval . v_list , str , end - str ); 6881} 6882if (! match ) { 6883break; 6884} 6885// Advance to just after the match. 6886if ( regmatch . endp [ 0 ] > str ) { 6887col = 0 ; 6888} else { 6889// Don't get stuck at the same match. 6890col = utfc_ptr2len ( regmatch . endp [ 0 ]); 6891} 6892str = regmatch . endp [ 0 ]; 6893} 6894 6895vim_regfree ( regmatch . regprog ); 6896} 6897 6898theend : 6899p_cpo = save_cpo ; 6900} 6901 6902/// "stdpath()" helper for list results 6903static void get_xdg_var_list (const XDGVarType xdg , typval_T * rettv ) 6904FUNC_ATTR_NONNULL_ALL 6905{ 6906list_T * const list = tv_list_alloc ( kListLenShouldKnow ); 6907rettv -> v_type = VAR_LIST ; 6908rettv -> vval . v_list = list ; 6909tv_list_ref ( list ); 6910 6911char * dirs ; 6912if ( xdg == kXDGConfigHome ) { 6913dirs = stdpaths_user_conf (); 6914} else { 6915dirs = stdpaths_get_xdg_var ( xdg ); 6916} 6917if ( dirs == NULL ) { 6918return; 6919} 6920const void * iter = NULL ; 6921const char * appname = get_appname (false); 6922do { 6923size_t dir_len ; 6924const char * dir ; 6925iter = vim_env_iter ( ENV_SEPCHAR , dirs , iter , & dir , & dir_len ); 6926if ( dir != NULL && dir_len > 0 ) { 6927char * dir_with_nvim = xmemdupz ( dir , dir_len ); 6928dir_with_nvim = concat_fnames_realloc ( dir_with_nvim , appname , true); 6929tv_list_append_allocated_string ( list , dir_with_nvim ); 6930} 6931} while ( iter != NULL ); 6932xfree ( dirs ); 6933} 6934 6935/// "stdpath(type)" function 6936static void f_stdpath ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6937{ 6938rettv -> v_type = VAR_STRING ; 6939rettv -> vval . v_string = NULL ; 6940 6941const char * const p = tv_get_string_chk ( & argvars [ 0 ]); 6942if ( p == NULL ) { 6943return; // Type error; errmsg already given. 6944} 6945 6946if ( strequal ( p , "config" )) { 6947rettv -> vval . v_string = get_xdg_home ( kXDGConfigHome ); 6948} else if ( strequal ( p , "data" )) { 6949rettv -> vval . v_string = get_xdg_home ( kXDGDataHome ); 6950} else if ( strequal ( p , "cache" )) { 6951rettv -> vval . v_string = get_xdg_home ( kXDGCacheHome ); 6952} else if ( strequal ( p , "state" )) { 6953rettv -> vval . v_string = get_xdg_home ( kXDGStateHome ); 6954} else if ( strequal ( p , "log" )) { 6955rettv -> vval . v_string = concat_fnames_realloc ( get_xdg_home ( kXDGStateHome ), "logs" , true); 6956} else if ( strequal ( p , "run" )) { 6957rettv -> vval . v_string = stdpaths_get_xdg_var ( kXDGRuntimeDir ); 6958} else if ( strequal ( p , "config_dirs" )) { 6959get_xdg_var_list ( kXDGConfigDirs , rettv ); 6960} else if ( strequal ( p , "data_dirs" )) { 6961get_xdg_var_list ( kXDGDataDirs , rettv ); 6962} else { 6963semsg ( _ ( "E6100: \"%s\" is not a valid stdpath" ), p ); 6964} 6965} 6966 6967/// "str2float()" function 6968static void f_str2float ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6969{ 6970char * p = skipwhite ( tv_get_string ( & argvars [ 0 ])); 6971bool isneg = ( * p == '-' ); 6972 6973if ( * p == '+' || * p == '-' ) { 6974p = skipwhite ( p + 1 ); 6975} 6976string2float ( p , & rettv -> vval . v_float ); 6977if ( isneg ) { 6978rettv -> vval . v_float *= -1 ; 6979} 6980rettv -> v_type = VAR_FLOAT ; 6981} 6982 6983/// "strftime({format}[, {time}])" function 6984static void f_strftime ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 6985{ 6986time_t seconds ; 6987 6988rettv -> v_type = VAR_STRING ; 6989 6990char * p = ( char * ) tv_get_string ( & argvars [ 0 ]); 6991if ( argvars [ 1 ]. v_type == VAR_UNKNOWN ) { 6992seconds = time ( NULL ); 6993} else { 6994seconds = ( time_t ) tv_get_number ( & argvars [ 1 ]); 6995} 6996 6997struct tm curtime ; 6998struct tm * curtime_ptr = os_localtime_r ( & seconds , & curtime ); 6999// MSVC returns NULL for an invalid value of seconds. 7000if ( curtime_ptr == NULL ) { 7001rettv -> vval . v_string = xstrdup ( _ ( "(Invalid)" )); 7002return; 7003} 7004 7005vimconv_T conv ; 7006 7007conv . vc_type = CONV_NONE ; 7008char * enc = enc_locale (); 7009convert_setup ( & conv , p_enc , enc ); 7010if ( conv . vc_type != CONV_NONE ) { 7011p = string_convert ( & conv , p , NULL ); 7012} 7013char result_buf [ 256 ]; 7014if ( p == NULL || strftime ( result_buf , sizeof( result_buf ), p , curtime_ptr ) == 0 ) { 7015result_buf [ 0 ] = NUL ; 7016} 7017 7018if ( conv . vc_type != CONV_NONE ) { 7019xfree ( p ); 7020} 7021convert_setup ( & conv , enc , p_enc ); 7022if ( conv . vc_type != CONV_NONE ) { 7023rettv -> vval . v_string = string_convert ( & conv , result_buf , NULL ); 7024} else { 7025rettv -> vval . v_string = xstrdup ( result_buf ); 7026} 7027 7028// Release conversion descriptors. 7029convert_setup ( & conv , NULL , NULL ); 7030xfree ( enc ); 7031} 7032 7033/// "strptime({format}, {timestring})" function 7034static void f_strptime ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7035{ 7036char fmt_buf [ NUMBUFLEN ]; 7037char str_buf [ NUMBUFLEN ]; 7038 7039struct tm tmval = { 7040. tm_isdst = -1 , 7041}; 7042char * fmt = ( char * ) tv_get_string_buf ( & argvars [ 0 ], fmt_buf ); 7043char * str = ( char * ) tv_get_string_buf ( & argvars [ 1 ], str_buf ); 7044 7045vimconv_T conv = { 7046. vc_type = CONV_NONE , 7047}; 7048char * enc = enc_locale (); 7049convert_setup ( & conv , p_enc , enc ); 7050if ( conv . vc_type != CONV_NONE ) { 7051fmt = string_convert ( & conv , fmt , NULL ); 7052} 7053if ( fmt == NULL 7054|| os_strptime ( str , fmt , & tmval ) == NULL 7055|| ( rettv -> vval . v_number = mktime ( & tmval )) == -1 ) { 7056rettv -> vval . v_number = 0 ; 7057} 7058if ( conv . vc_type != CONV_NONE ) { 7059xfree ( fmt ); 7060} 7061convert_setup ( & conv , NULL , NULL ); 7062xfree ( enc ); 7063} 7064 7065/// "submatch()" function 7066static void f_submatch ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7067{ 7068bool error = false; 7069int no = ( int ) tv_get_number_chk ( & argvars [ 0 ], & error ); 7070if ( error ) { 7071return; 7072} 7073 7074if ( no < 0 || no >= NSUBEXP ) { 7075semsg ( _ ( e_invalid_submatch_number_nr ), no ); 7076return; 7077} 7078int retList = 0 ; 7079 7080if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 7081retList = ( int ) tv_get_bool_chk ( & argvars [ 1 ], & error ); 7082if ( error ) { 7083return; 7084} 7085} 7086 7087if ( retList == 0 ) { 7088rettv -> v_type = VAR_STRING ; 7089rettv -> vval . v_string = reg_submatch ( no ); 7090} else { 7091rettv -> v_type = VAR_LIST ; 7092rettv -> vval . v_list = reg_submatch_list ( no ); 7093} 7094} 7095 7096/// "substitute()" function 7097static void f_substitute ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7098{ 7099char patbuf [ NUMBUFLEN ]; 7100char subbuf [ NUMBUFLEN ]; 7101char flagsbuf [ NUMBUFLEN ]; 7102 7103const char * const str = tv_get_string_chk ( & argvars [ 0 ]); 7104const char * const pat = tv_get_string_buf_chk ( & argvars [ 1 ], patbuf ); 7105const char * sub = NULL ; 7106const char * const flg = tv_get_string_buf_chk ( & argvars [ 3 ], flagsbuf ); 7107 7108typval_T * expr = NULL ; 7109if ( tv_is_func ( argvars [ 2 ])) { 7110expr = & argvars [ 2 ]; 7111} else { 7112sub = tv_get_string_buf_chk ( & argvars [ 2 ], subbuf ); 7113} 7114 7115rettv -> v_type = VAR_STRING ; 7116if ( str == NULL || pat == NULL || ( sub == NULL && expr == NULL ) 7117|| flg == NULL ) { 7118rettv -> vval . v_string = NULL ; 7119} else { 7120rettv -> vval . v_string = do_string_sub (( char * ) str , strlen ( str ), ( char * ) pat , 7121( char * ) sub , expr , ( char * ) flg , NULL ); 7122} 7123} 7124 7125/// "swapfilelist()" function 7126static void f_swapfilelist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7127{ 7128tv_list_alloc_ret ( rettv , kListLenUnknown ); 7129recover_names ( NULL , false, rettv -> vval . v_list ); 7130} 7131 7132/// "swapinfo(swap_filename)" function 7133static void f_swapinfo ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7134{ 7135tv_dict_alloc_ret ( rettv ); 7136swapfile_dict ( tv_get_string ( argvars ), rettv -> vval . v_dict ); 7137} 7138 7139/// "swapname(expr)" function 7140static void f_swapname ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7141{ 7142rettv -> v_type = VAR_STRING ; 7143buf_T * buf = tv_get_buf ( & argvars [ 0 ], false); 7144if ( buf == NULL 7145|| buf -> b_ml . ml_mfp == NULL 7146|| buf -> b_ml . ml_mfp -> mf_fname == NULL ) { 7147rettv -> vval . v_string = NULL ; 7148} else { 7149rettv -> vval . v_string = xstrdup ( buf -> b_ml . ml_mfp -> mf_fname ); 7150} 7151} 7152 7153/// "synID(lnum, col, trans)" function 7154static void f_synID ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7155{ 7156// -1 on type error (both) 7157const linenr_T lnum = tv_get_lnum ( argvars ); 7158const colnr_T col = ( colnr_T ) tv_get_number ( & argvars [ 1 ]) - 1 ; 7159 7160bool transerr = false; 7161const int trans = ( int ) tv_get_bool_chk ( & argvars [ 2 ], & transerr ); 7162 7163int id = 0 ; 7164if (! transerr && lnum >= 1 && lnum <= curbuf -> b_ml . ml_line_count 7165&& col >= 0 && col < ml_get_len ( lnum )) { 7166id = syn_get_id ( curwin , lnum , col , trans , NULL , false); 7167} 7168 7169rettv -> vval . v_number = id ; 7170} 7171 7172/// "synIDattr(id, what [, mode])" function 7173static void f_synIDattr ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7174{ 7175const int id = ( int ) tv_get_number ( & argvars [ 0 ]); 7176const char * const what = tv_get_string ( & argvars [ 1 ]); 7177int modec ; 7178if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 7179char modebuf [ NUMBUFLEN ]; 7180const char * const mode = tv_get_string_buf ( & argvars [ 2 ], modebuf ); 7181modec = TOLOWER_ASC ( mode [ 0 ]); 7182if ( modec != 'c' && modec != 'g' ) { 7183modec = 0 ; // Replace invalid with current. 7184} 7185} else if ( ui_rgb_attached ()) { 7186modec = 'g' ; 7187} else { 7188modec = 'c' ; 7189} 7190 7191const char * p = NULL ; 7192switch ( TOLOWER_ASC ( what [ 0 ])) { 7193case 'b' : 7194if ( TOLOWER_ASC ( what [ 1 ]) == 'g' ) { // bg[#] 7195p = highlight_color ( id , what , modec ); 7196} else if ( TOLOWER_ASC ( what [ 1 ]) == 'l' ) { // blink 7197p = highlight_has_attr ( id , HL_BLINK , modec ); 7198} else { // bold 7199p = highlight_has_attr ( id , HL_BOLD , modec ); 7200} 7201break; 7202case 'c' : // conceal 7203p = highlight_has_attr ( id , HL_CONCEALED , modec ); 7204break; 7205case 'd' : // dim 7206p = highlight_has_attr ( id , HL_DIM , modec ); 7207break; 7208case 'o' : // overline 7209p = highlight_has_attr ( id , HL_OVERLINE , modec ); 7210break; 7211case 'f' : // fg[#] or font 7212p = highlight_color ( id , what , modec ); 7213break; 7214case 'i' : 7215if ( TOLOWER_ASC ( what [ 1 ]) == 'n' ) { // inverse 7216p = highlight_has_attr ( id , HL_INVERSE , modec ); 7217} else { // italic 7218p = highlight_has_attr ( id , HL_ITALIC , modec ); 7219} 7220break; 7221case 'n' : 7222if ( TOLOWER_ASC ( what [ 1 ]) == 'o' ) { // nocombine 7223p = highlight_has_attr ( id , HL_NOCOMBINE , modec ); 7224} else { // name 7225p = get_highlight_name_ext ( NULL , id - 1 , false); 7226} 7227break; 7228case 'r' : // reverse 7229p = highlight_has_attr ( id , HL_INVERSE , modec ); 7230break; 7231case 's' : 7232if ( TOLOWER_ASC ( what [ 1 ]) == 'p' ) { // sp[#] 7233p = highlight_color ( id , what , modec ); 7234} else if ( TOLOWER_ASC ( what [ 1 ]) == 't' 7235&& TOLOWER_ASC ( what [ 2 ]) == 'r' ) { // strikethrough 7236p = highlight_has_attr ( id , HL_STRIKETHROUGH , modec ); 7237} else { // standout 7238p = highlight_has_attr ( id , HL_STANDOUT , modec ); 7239} 7240break; 7241case 'u' : 7242if ( strlen ( what ) >= 9 ) { 7243if ( TOLOWER_ASC ( what [ 5 ]) == 'l' ) { 7244// underline 7245p = highlight_has_attr ( id , HL_UNDERLINE , modec ); 7246} else if ( TOLOWER_ASC ( what [ 5 ]) != 'd' ) { 7247// undercurl 7248p = highlight_has_attr ( id , HL_UNDERCURL , modec ); 7249} else if ( TOLOWER_ASC ( what [ 6 ]) != 'o' ) { 7250// underdashed 7251p = highlight_has_attr ( id , HL_UNDERDASHED , modec ); 7252} else if ( TOLOWER_ASC ( what [ 7 ]) == 'u' ) { 7253// underdouble 7254p = highlight_has_attr ( id , HL_UNDERDOUBLE , modec ); 7255} else { 7256// underdotted 7257p = highlight_has_attr ( id , HL_UNDERDOTTED , modec ); 7258} 7259} else { 7260// ul 7261p = highlight_color ( id , what , modec ); 7262} 7263break; 7264} 7265 7266rettv -> v_type = VAR_STRING ; 7267rettv -> vval . v_string = p == NULL ? NULL : xstrdup ( p ); 7268} 7269 7270/// "synIDtrans(id)" function 7271static void f_synIDtrans ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7272{ 7273int id = ( int ) tv_get_number ( & argvars [ 0 ]); 7274 7275if ( id > 0 ) { 7276id = syn_get_final_id ( id ); 7277} else { 7278id = 0 ; 7279} 7280 7281rettv -> vval . v_number = id ; 7282} 7283 7284/// "synconcealed(lnum, col)" function 7285static void f_synconcealed ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7286{ 7287int syntax_flags = 0 ; 7288int matchid = 0 ; 7289char str [ NUMBUFLEN ]; 7290 7291tv_list_set_ret ( rettv , NULL ); 7292 7293// -1 on type error (both) 7294const linenr_T lnum = tv_get_lnum ( argvars ); 7295const colnr_T col = ( colnr_T ) tv_get_number ( & argvars [ 1 ]) - 1 ; 7296 7297CLEAR_FIELD ( str ); 7298 7299if ( lnum >= 1 && lnum <= curbuf -> b_ml . ml_line_count 7300&& col >= 0 && col <= ml_get_len ( lnum ) && curwin -> w_p_cole > 0 ) { 7301syn_get_id ( curwin , lnum , col , false, NULL , false); 7302syntax_flags = get_syntax_info ( & matchid ); 7303 7304// get the conceal character 7305if (( syntax_flags & HL_CONCEAL ) && curwin -> w_p_cole < 3 ) { 7306schar_T cchar = schar_from_char ( syn_get_sub_char ()); 7307if ( cchar == NUL && curwin -> w_p_cole == 1 ) { 7308cchar = ( curwin -> w_p_lcs_chars . conceal == NUL ) 7309? schar_from_ascii ( ' ' ) : curwin -> w_p_lcs_chars . conceal ; 7310} 7311if ( cchar != NUL ) { 7312schar_get ( str , cchar ); 7313} 7314} 7315} 7316 7317tv_list_alloc_ret ( rettv , 3 ); 7318tv_list_append_number ( rettv -> vval . v_list , ( syntax_flags & HL_CONCEAL ) != 0 ); 7319// -1 to auto-determine strlen 7320tv_list_append_string ( rettv -> vval . v_list , str , -1 ); 7321tv_list_append_number ( rettv -> vval . v_list , matchid ); 7322} 7323 7324/// "synstack(lnum, col)" function 7325static void f_synstack ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7326{ 7327tv_list_set_ret ( rettv , NULL ); 7328 7329// -1 on type error (both) 7330const linenr_T lnum = tv_get_lnum ( argvars ); 7331const colnr_T col = ( colnr_T ) tv_get_number ( & argvars [ 1 ]) - 1 ; 7332 7333if ( lnum >= 1 && lnum <= curbuf -> b_ml . ml_line_count 7334&& col >= 0 && col <= ml_get_len ( lnum )) { 7335tv_list_alloc_ret ( rettv , kListLenMayKnow ); 7336syn_get_id ( curwin , lnum , col , false, NULL , true); 7337 7338int id ; 7339int i = 0 ; 7340while (( id = syn_get_stack_item ( i ++ )) >= 0 ) { 7341tv_list_append_number ( rettv -> vval . v_list , id ); 7342} 7343} 7344} 7345 7346/// "tabpagebuflist()" function 7347static void f_tabpagebuflist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7348{ 7349win_T * wp = NULL ; 7350 7351if ( argvars [ 0 ]. v_type == VAR_UNKNOWN ) { 7352wp = firstwin ; 7353} else { 7354tabpage_T * const tp = find_tabpage (( int ) tv_get_number ( & argvars [ 0 ])); 7355if ( tp != NULL ) { 7356wp = ( tp == curtab ) ? firstwin : tp -> tp_firstwin ; 7357} 7358} 7359if ( wp != NULL ) { 7360tv_list_alloc_ret ( rettv , kListLenMayKnow ); 7361while ( wp != NULL ) { 7362tv_list_append_number ( rettv -> vval . v_list , wp -> w_buffer -> b_fnum ); 7363wp = wp -> w_next ; 7364} 7365} 7366} 7367 7368/// "tagfiles()" function 7369static void f_tagfiles ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7370{ 7371tv_list_alloc_ret ( rettv , kListLenUnknown ); 7372char * fname = xmalloc ( MAXPATHL ); 7373 7374bool first = true; 7375tagname_T tn ; 7376while ( get_tagfname ( & tn , first , fname ) == OK ) { 7377tv_list_append_string ( rettv -> vval . v_list , fname , -1 ); 7378first = false; 7379} 7380 7381tagname_free ( & tn ); 7382xfree ( fname ); 7383} 7384 7385/// "taglist()" function 7386static void f_taglist ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7387{ 7388const char * const tag_pattern = tv_get_string ( & argvars [ 0 ]); 7389 7390rettv -> vval . v_number = false; 7391if ( * tag_pattern == NUL ) { 7392return; 7393} 7394 7395const char * fname = NULL ; 7396if ( argvars [ 1 ]. v_type != VAR_UNKNOWN ) { 7397fname = tv_get_string ( & argvars [ 1 ]); 7398} 7399get_tags ( tv_list_alloc_ret ( rettv , kListLenUnknown ), 7400( char * ) tag_pattern , ( char * ) fname ); 7401} 7402 7403/// "timer_info([timer])" function 7404static void f_timer_info ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7405{ 7406tv_list_alloc_ret ( rettv , kListLenUnknown ); 7407 7408if ( tv_check_for_opt_number_arg ( argvars , 0 ) == FAIL ) { 7409return; 7410} 7411 7412if ( argvars [ 0 ]. v_type != VAR_UNKNOWN ) { 7413timer_T * timer = find_timer_by_nr ( tv_get_number ( & argvars [ 0 ])); 7414if ( timer != NULL && (! timer -> stopped || timer -> refcount > 1 )) { 7415add_timer_info ( rettv , timer ); 7416} 7417} else { 7418add_timer_info_all ( rettv ); 7419} 7420} 7421 7422/// "timer_pause(timer, paused)" function 7423static void f_timer_pause ( typval_T * argvars , typval_T * unused , EvalFuncData fptr ) 7424{ 7425if ( argvars [ 0 ]. v_type != VAR_NUMBER ) { 7426emsg ( _ ( e_number_exp )); 7427return; 7428} 7429 7430bool paused = ( bool ) tv_get_bool ( & argvars [ 1 ]); 7431timer_T * timer = find_timer_by_nr ( tv_get_number ( & argvars [ 0 ])); 7432if ( timer != NULL ) { 7433if (! timer -> paused && paused ) { 7434time_watcher_stop ( & timer -> tw ); 7435} else if ( timer -> paused && ! paused ) { 7436time_watcher_start ( & timer -> tw , timer_due_cb , ( uint64_t ) timer -> timeout , 7437( uint64_t ) timer -> timeout ); 7438} 7439timer -> paused = paused ; 7440} 7441} 7442 7443/// "timer_start(timeout, callback, opts)" function 7444static void f_timer_start ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7445{ 7446int repeat = 1 ; 7447 7448rettv -> vval . v_number = -1 ; 7449if ( check_secure ()) { 7450return; 7451} 7452 7453if ( argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 7454if ( tv_check_for_nonnull_dict_arg ( argvars , 2 ) == FAIL ) { 7455return; 7456} 7457dict_T * dict = argvars [ 2 ]. vval . v_dict ; 7458dictitem_T * const di = tv_dict_find ( dict , S_LEN ( "repeat" )); 7459if ( di != NULL ) { 7460repeat = ( int ) tv_get_number ( & di -> di_tv ); 7461if ( repeat == 0 ) { 7462repeat = 1 ; 7463} 7464} 7465} 7466 7467Callback callback ; 7468if (! callback_from_typval ( & callback , & argvars [ 1 ])) { 7469return; 7470} 7471rettv -> vval . v_number = ( varnumber_T ) timer_start ( tv_get_number ( & argvars [ 0 ]), repeat , & callback ); 7472} 7473 7474/// "timer_stop(timerid)" function 7475static void f_timer_stop ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7476{ 7477if ( tv_check_for_number_arg ( argvars , 0 ) == FAIL ) { 7478return; 7479} 7480 7481timer_T * timer = find_timer_by_nr ( tv_get_number ( & argvars [ 0 ])); 7482if ( timer == NULL ) { 7483return; 7484} 7485 7486timer_stop ( timer ); 7487} 7488 7489static void f_timer_stopall ( typval_T * argvars , typval_T * unused , EvalFuncData fptr ) 7490{ 7491timer_stop_all (); 7492} 7493 7494/// "type(expr)" function 7495static void f_type ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7496{ 7497int n = -1 ; 7498 7499switch ( argvars [ 0 ]. v_type ) { 7500case VAR_NUMBER : 7501n = VAR_TYPE_NUMBER ; break; 7502case VAR_STRING : 7503n = VAR_TYPE_STRING ; break; 7504case VAR_PARTIAL : 7505case VAR_FUNC : 7506n = VAR_TYPE_FUNC ; break; 7507case VAR_LIST : 7508n = VAR_TYPE_LIST ; break; 7509case VAR_DICT : 7510n = VAR_TYPE_DICT ; break; 7511case VAR_FLOAT : 7512n = VAR_TYPE_FLOAT ; break; 7513case VAR_BOOL : 7514n = VAR_TYPE_BOOL ; break; 7515case VAR_SPECIAL : 7516n = VAR_TYPE_SPECIAL ; break; 7517case VAR_BLOB : 7518n = VAR_TYPE_BLOB ; break; 7519case VAR_UNKNOWN : 7520internal_error ( "f_type(UNKNOWN)" ); 7521break; 7522} 7523rettv -> vval . v_number = n ; 7524} 7525 7526/// "virtcol({expr}, [, {list} [, {winid}]])" function 7527static void f_virtcol ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7528{ 7529colnr_T vcol_start = 0 ; 7530colnr_T vcol_end = 0 ; 7531win_T * wp = curwin ; 7532 7533if ( argvars [ 1 ]. v_type != VAR_UNKNOWN && argvars [ 2 ]. v_type != VAR_UNKNOWN ) { 7534// use the window specified in the third argument 7535tabpage_T * tp ; 7536wp = win_id2wp_tp (( int ) tv_get_number ( & argvars [ 2 ]), & tp ); 7537if ( wp == NULL || tp == NULL ) { 7538goto theend ; 7539} 7540check_cursor ( wp ); 7541} 7542 7543buf_T * bp = wp -> w_buffer ; 7544int fnum = bp -> b_fnum ; 7545pos_T * fp = var2fpos ( & argvars [ 0 ], false, & fnum , false, wp ); 7546if ( fp != NULL && fp -> lnum <= bp -> b_ml . ml_line_count 7547&& fnum == bp -> b_fnum ) { 7548// Limit the column to a valid value, getvvcol() doesn't check. 7549if ( fp -> col < 0 ) { 7550fp -> col = 0 ; 7551} else { 7552const colnr_T len = ml_get_buf_len ( bp , fp -> lnum ); 7553if ( fp -> col > len ) { 7554fp -> col = len ; 7555} 7556} 7557getvvcol ( wp , fp , & vcol_start , NULL , & vcol_end , 0 ); 7558vcol_start ++ ; 7559vcol_end ++ ; 7560} 7561 7562theend : 7563if ( argvars [ 1 ]. v_type != VAR_UNKNOWN && tv_get_bool ( & argvars [ 1 ])) { 7564tv_list_alloc_ret ( rettv , 2 ); 7565tv_list_append_number ( rettv -> vval . v_list , vcol_start ); 7566tv_list_append_number ( rettv -> vval . v_list , vcol_end ); 7567} else { 7568rettv -> vval . v_number = vcol_end ; 7569} 7570} 7571 7572/// "visualmode()" function 7573static void f_visualmode ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7574{ 7575char str [ 2 ]; 7576 7577rettv -> v_type = VAR_STRING ; 7578str [ 0 ] = ( char ) curbuf -> b_visual_mode_eval ; 7579str [ 1 ] = NUL ; 7580rettv -> vval . v_string = xstrdup ( str ); 7581 7582// A non-zero number or non-empty string argument: reset mode. 7583if ( non_zero_arg ( & argvars [ 0 ])) { 7584curbuf -> b_visual_mode_eval = NUL ; 7585} 7586} 7587 7588/// "wildmenumode()" function 7589static void f_wildmenumode ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7590{ 7591if ( wild_menu_showing || (( State & MODE_CMDLINE ) && cmdline_pum_active ())) { 7592rettv -> vval . v_number = 1 ; 7593} 7594} 7595 7596/// "windowsversion()" function 7597static void f_windowsversion ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7598{ 7599rettv -> v_type = VAR_STRING ; 7600rettv -> vval . v_string = xstrdup ( windowsVersion ); 7601} 7602 7603/// "wordcount()" function 7604static void f_wordcount ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7605{ 7606tv_dict_alloc_ret ( rettv ); 7607cursor_pos_info ( rettv -> vval . v_dict ); 7608} 7609 7610/// "xor(expr, expr)" function 7611static void f_xor ( typval_T * argvars , typval_T * rettv , EvalFuncData fptr ) 7612{ 7613rettv -> vval . v_number = tv_get_number_chk ( & argvars [ 0 ], NULL ) 7614^ tv_get_number_chk ( & argvars [ 1 ], NULL ); 7615}