diff options
Diffstat (limited to 'src/nvim/eval')
-rw-r--r-- | src/nvim/eval/buffer.c | 6 | ||||
-rw-r--r-- | src/nvim/eval/decode.c | 3 | ||||
-rw-r--r-- | src/nvim/eval/decode.h | 2 | ||||
-rw-r--r-- | src/nvim/eval/deprecated.c | 158 | ||||
-rw-r--r-- | src/nvim/eval/deprecated.h | 8 | ||||
-rw-r--r-- | src/nvim/eval/executor.c | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 397 | ||||
-rw-r--r-- | src/nvim/eval/typval.c | 38 | ||||
-rw-r--r-- | src/nvim/eval/typval_defs.h | 7 | ||||
-rw-r--r-- | src/nvim/eval/userfunc.c | 793 | ||||
-rw-r--r-- | src/nvim/eval/vars.c | 35 | ||||
-rw-r--r-- | src/nvim/eval/window.c | 2 |
12 files changed, 807 insertions, 643 deletions
diff --git a/src/nvim/eval/buffer.c b/src/nvim/eval/buffer.c index 73bfd6db2a..41ed17598b 100644 --- a/src/nvim/eval/buffer.c +++ b/src/nvim/eval/buffer.c @@ -3,6 +3,7 @@ #include <stdbool.h> #include <string.h> +#include "klib/kvec.h" #include "nvim/ascii_defs.h" #include "nvim/autocmd.h" #include "nvim/autocmd_defs.h" @@ -66,12 +67,11 @@ buf_T *find_buffer(typval_T *avar) /// If there is a window for "curbuf", make it the current window. static void find_win_for_curbuf(void) { - wininfo_T *wip; - // The b_wininfo list should have the windows that recently contained the // buffer, going over this is faster than going over all the windows. // Do check the buffer is still there. - FOR_ALL_BUF_WININFO(curbuf, wip) { + for (size_t i = 0; i < kv_size(curbuf->b_wininfo); i++) { + WinInfo *wip = kv_A(curbuf->b_wininfo, i); if (wip->wi_win != NULL && wip->wi_win->w_buffer == curbuf) { curwin = wip->wi_win; break; diff --git a/src/nvim/eval/decode.c b/src/nvim/eval/decode.c index afc2efddf6..cfcd415219 100644 --- a/src/nvim/eval/decode.c +++ b/src/nvim/eval/decode.c @@ -6,6 +6,8 @@ #include <string.h> #include "klib/kvec.h" +#include "mpack/conv.h" +#include "mpack/mpack_core.h" #include "mpack/object.h" #include "nvim/ascii_defs.h" #include "nvim/charset.h" @@ -21,7 +23,6 @@ #include "nvim/mbyte.h" #include "nvim/memory.h" #include "nvim/message.h" -#include "nvim/types_defs.h" #include "nvim/vim_defs.h" /// Helper structure for container_struct diff --git a/src/nvim/eval/decode.h b/src/nvim/eval/decode.h index 485cc65561..af5fd3979c 100644 --- a/src/nvim/eval/decode.h +++ b/src/nvim/eval/decode.h @@ -2,7 +2,7 @@ #include <stddef.h> // IWYU pragma: keep -#include "mpack/object.h" +#include "mpack/object.h" // IWYU pragma: keep #include "nvim/eval/typval_defs.h" // IWYU pragma: keep #include "nvim/types_defs.h" // IWYU pragma: keep diff --git a/src/nvim/eval/deprecated.c b/src/nvim/eval/deprecated.c new file mode 100644 index 0000000000..0fc16b605d --- /dev/null +++ b/src/nvim/eval/deprecated.c @@ -0,0 +1,158 @@ +#include <stdbool.h> // for true + +#include "nvim/channel.h" +#include "nvim/errors.h" +#include "nvim/eval.h" +#include "nvim/eval/deprecated.h" +#include "nvim/eval/funcs.h" +#include "nvim/eval/typval.h" +#include "nvim/eval/typval_defs.h" +#include "nvim/ex_cmds.h" +#include "nvim/gettext_defs.h" // for _ +#include "nvim/globals.h" +#include "nvim/macros_defs.h" // for S_LEN +#include "nvim/message.h" // for semsg +#include "nvim/types_defs.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "eval/deprecated.c.generated.h" // IWYU pragma: keep +#endif + +/// "rpcstart()" function (DEPRECATED) +void f_rpcstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = 0; + + if (check_secure()) { + return; + } + + if (argvars[0].v_type != VAR_STRING + || (argvars[1].v_type != VAR_LIST && argvars[1].v_type != VAR_UNKNOWN)) { + // Wrong argument types + emsg(_(e_invarg)); + return; + } + + list_T *args = NULL; + int argsl = 0; + if (argvars[1].v_type == VAR_LIST) { + args = argvars[1].vval.v_list; + argsl = tv_list_len(args); + // Assert that all list items are strings + int i = 0; + TV_LIST_ITER_CONST(args, arg, { + if (TV_LIST_ITEM_TV(arg)->v_type != VAR_STRING) { + semsg(_("E5010: List item %d of the second argument is not a string"), + i); + return; + } + i++; + }); + } + + if (argvars[0].vval.v_string == NULL || argvars[0].vval.v_string[0] == NUL) { + emsg(_(e_api_spawn_failed)); + return; + } + + // Allocate extra memory for the argument vector and the NULL pointer + int argvl = argsl + 2; + char **argv = xmalloc(sizeof(char *) * (size_t)argvl); + + // Copy program name + argv[0] = xstrdup(argvars[0].vval.v_string); + + int i = 1; + // Copy arguments to the vector + if (argsl > 0) { + TV_LIST_ITER_CONST(args, arg, { + argv[i++] = xstrdup(tv_get_string(TV_LIST_ITEM_TV(arg))); + }); + } + + // The last item of argv must be NULL + argv[i] = NULL; + + Channel *chan = channel_job_start(argv, NULL, CALLBACK_READER_INIT, + CALLBACK_READER_INIT, CALLBACK_NONE, + false, true, false, false, + kChannelStdinPipe, NULL, 0, 0, NULL, + &rettv->vval.v_number); + if (chan) { + channel_create_event(chan, NULL); + } +} + +/// "rpcstop()" function +void f_rpcstop(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = 0; + + if (check_secure()) { + return; + } + + if (argvars[0].v_type != VAR_NUMBER) { + // Wrong argument types + emsg(_(e_invarg)); + return; + } + + // if called with a job, stop it, else closes the channel + uint64_t id = (uint64_t)argvars[0].vval.v_number; + if (find_job(id, false)) { + f_jobstop(argvars, rettv, fptr); + } else { + const char *error; + rettv->vval.v_number = + channel_close((uint64_t)argvars[0].vval.v_number, kChannelPartRpc, &error); + if (!rettv->vval.v_number) { + emsg(error); + } + } +} + +/// "last_buffer_nr()" function. +void f_last_buffer_nr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + int n = 0; + + FOR_ALL_BUFFERS(buf) { + if (n < buf->b_fnum) { + n = buf->b_fnum; + } + } + + rettv->vval.v_number = n; +} + +/// "termopen(cmd[, cwd])" function +void f_termopen(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + if (check_secure()) { + return; + } + + bool must_free = false; + + if (argvars[1].v_type == VAR_UNKNOWN) { + must_free = true; + argvars[1].v_type = VAR_DICT; + argvars[1].vval.v_dict = tv_dict_alloc(); + } + + if (argvars[1].v_type != VAR_DICT) { + // Wrong argument types + semsg(_(e_invarg2), "expected dictionary"); + return; + } + + tv_dict_add_bool(argvars[1].vval.v_dict, S_LEN("term"), true); + f_jobstart(argvars, rettv, fptr); + if (must_free) { + tv_dict_free(argvars[1].vval.v_dict); + } +} diff --git a/src/nvim/eval/deprecated.h b/src/nvim/eval/deprecated.h new file mode 100644 index 0000000000..e2e3ee436e --- /dev/null +++ b/src/nvim/eval/deprecated.h @@ -0,0 +1,8 @@ +#pragma once + +#include "nvim/eval/typval_defs.h" // IWYU pragma: keep +#include "nvim/types_defs.h" // IWYU pragma: keep + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "eval/deprecated.h.generated.h" +#endif diff --git a/src/nvim/eval/executor.c b/src/nvim/eval/executor.c index 5b92f217d1..691fd405e9 100644 --- a/src/nvim/eval/executor.c +++ b/src/nvim/eval/executor.c @@ -8,7 +8,6 @@ #include "nvim/eval/typval_defs.h" #include "nvim/garray.h" #include "nvim/gettext_defs.h" -#include "nvim/globals.h" #include "nvim/message.h" #include "nvim/strings.h" #include "nvim/types_defs.h" diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 9e9e36d3c5..8db50a53b3 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -13,6 +13,8 @@ #include <uv.h> #include "auto/config.h" +#include "klib/kvec.h" +#include "mpack/mpack_core.h" #include "mpack/object.h" #include "nvim/api/private/converter.h" #include "nvim/api/private/defs.h" @@ -89,6 +91,7 @@ #include "nvim/msgpack_rpc/channel.h" #include "nvim/msgpack_rpc/channel_defs.h" #include "nvim/msgpack_rpc/packer.h" +#include "nvim/msgpack_rpc/packer_defs.h" #include "nvim/msgpack_rpc/server.h" #include "nvim/normal.h" #include "nvim/normal_defs.h" @@ -545,8 +548,7 @@ static void f_byte2line(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) /// "call(func, arglist [, dict])" function static void f_call(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { - if (argvars[1].v_type != VAR_LIST) { - emsg(_(e_listreq)); + if (tv_check_for_list_arg(argvars, 1) == FAIL) { return; } if (argvars[1].vval.v_list == NULL) { @@ -572,22 +574,32 @@ static void f_call(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (func == NULL || *func == NUL) { return; // type error, empty name or null function } + char *tofree = NULL; + if (argvars[0].v_type == VAR_STRING) { + char *p = func; + tofree = trans_function_name(&p, false, TFN_INT|TFN_QUIET, NULL, NULL); + if (tofree == NULL) { + emsg_funcname(e_unknown_function_str, func); + return; + } + func = tofree; + } dict_T *selfdict = NULL; if (argvars[2].v_type != VAR_UNKNOWN) { if (tv_check_for_dict_arg(argvars, 2) == FAIL) { - if (owned) { - func_unref(func); - } - return; + goto done; } selfdict = argvars[2].vval.v_dict; } func_call(func, &argvars[1], partial, selfdict, rettv); + +done: if (owned) { func_unref(func); } + xfree(tofree); } /// "changenr()" function @@ -2402,14 +2414,15 @@ static void f_getchangelist(typval_T *argvars, typval_T *rettv, EvalFuncData fpt if (buf == curwin->w_buffer) { changelistindex = curwin->w_changelistidx; } else { - wininfo_T *wip; + changelistindex = buf->b_changelistlen; - FOR_ALL_BUF_WININFO(buf, wip) { + for (size_t i = 0; i < kv_size(buf->b_wininfo); i++) { + WinInfo *wip = kv_A(buf->b_wininfo, i); if (wip->wi_win == curwin) { + changelistindex = wip->wi_changelistidx; break; } } - changelistindex = wip != NULL ? wip->wi_changelistidx : buf->b_changelistlen; } tv_list_append_number(rettv->vval.v_list, (varnumber_T)changelistindex); @@ -3541,6 +3554,7 @@ static void f_inputlist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } + msg_ext_set_kind("list_cmd"); msg_start(); msg_row = Rows - 1; // for when 'cmdheight' > 1 lines_left = Rows; // avoid more prompt @@ -3553,10 +3567,10 @@ static void f_inputlist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) }); // Ask for choice. - bool mouse_used; - int selected = prompt_for_number(&mouse_used); + bool mouse_used = false; + int selected = prompt_for_input(NULL, 0, false, &mouse_used); if (mouse_used) { - selected -= lines_left; + selected = tv_list_len(argvars[0].vval.v_list) - (cmdline_row - mouse_row); } rettv->vval.v_number = selected; @@ -3839,8 +3853,8 @@ static const char *required_env_vars[] = { NULL }; -static dict_T *create_environment(const dictitem_T *job_env, const bool clear_env, const bool pty, - const char * const pty_term_name) +dict_T *create_environment(const dictitem_T *job_env, const bool clear_env, const bool pty, + const char * const pty_term_name) { dict_T *env = tv_dict_alloc(); @@ -3932,7 +3946,7 @@ static dict_T *create_environment(const dictitem_T *job_env, const bool clear_en } /// "jobstart()" function -static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -3941,9 +3955,9 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } + const char *cmd; bool executable = true; - char **argv = tv_to_argv(&argvars[0], NULL, &executable); - dict_T *env = NULL; + char **argv = tv_to_argv(&argvars[0], &cmd, &executable); if (!argv) { rettv->vval.v_number = executable ? 0 : -1; return; // Did error message in tv_to_argv. @@ -3960,6 +3974,7 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) bool detach = false; bool rpc = false; bool pty = false; + bool term = false; bool clear_env = false; bool overlapped = false; ChannelStdinMode stdin_mode = kChannelStdinPipe; @@ -3973,7 +3988,8 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) detach = tv_dict_get_number(job_opts, "detach") != 0; rpc = tv_dict_get_number(job_opts, "rpc") != 0; - pty = tv_dict_get_number(job_opts, "pty") != 0; + term = tv_dict_get_number(job_opts, "term") != 0; + pty = term || tv_dict_get_number(job_opts, "pty") != 0; clear_env = tv_dict_get_number(job_opts, "clear_env") != 0; overlapped = tv_dict_get_number(job_opts, "overlapped") != 0; @@ -3988,6 +4004,14 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } } + dictitem_T *const job_term = tv_dict_find(job_opts, S_LEN("term")); + if (job_term && VAR_BOOL != job_term->di_tv.v_type) { + // Restrict "term" field to boolean, in case we want to allow buffer numbers in the future. + semsg(_(e_invarg2), "'term' must be Boolean"); + shell_free_argv(argv); + return; + } + if (pty && rpc) { semsg(_(e_invarg2), "job cannot have both 'pty' and 'rpc' options set"); shell_free_argv(argv); @@ -4031,29 +4055,92 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) uint16_t height = 0; char *term_name = NULL; - if (pty) { - width = (uint16_t)tv_dict_get_number(job_opts, "width"); - height = (uint16_t)tv_dict_get_number(job_opts, "height"); - // Legacy method, before env option existed, to specify $TERM. No longer - // documented, but still usable to avoid breaking scripts. - term_name = tv_dict_get_string(job_opts, "TERM", false); - if (!term_name) { - term_name = "ansi"; + if (term) { + if (text_locked()) { + text_locked_msg(); + shell_free_argv(argv); + return; } + if (curbuf->b_changed) { + emsg(_("jobstart(...,{term=true}) requires unmodified buffer")); + shell_free_argv(argv); + return; + } + assert(!rpc); + term_name = "xterm-256color"; + cwd = cwd ? cwd : "."; + overlapped = false; + detach = false; + stdin_mode = kChannelStdinPipe; + width = (uint16_t)MAX(0, curwin->w_width_inner - win_col_off(curwin)); + height = (uint16_t)curwin->w_height_inner; } - env = create_environment(job_env, clear_env, pty, term_name); + if (pty) { + width = width ? width : (uint16_t)tv_dict_get_number(job_opts, "width"); + height = height ? height : (uint16_t)tv_dict_get_number(job_opts, "height"); + // Deprecated TERM field is from before `env` option existed. + term_name = term_name ? term_name : tv_dict_get_string(job_opts, "TERM", false); + term_name = term_name ? term_name : "ansi"; + } + dict_T *env = create_environment(job_env, clear_env, pty, term_name); Channel *chan = channel_job_start(argv, NULL, on_stdout, on_stderr, on_exit, pty, rpc, overlapped, detach, stdin_mode, cwd, width, height, env, &rettv->vval.v_number); - if (chan) { + if (!chan) { + return; + } else if (!term) { channel_create_event(chan, NULL); + } else { + if (rettv->vval.v_number <= 0) { + return; + } + + int pid = chan->stream.pty.proc.pid; + + // "./…" => "/home/foo/…" + vim_FullName(cwd, NameBuff, sizeof(NameBuff), false); + // "/home/foo/…" => "~/…" + size_t len = home_replace(NULL, NameBuff, IObuff, sizeof(IObuff), true); + // Trim slash. + if (len != 1 && (IObuff[len - 1] == '\\' || IObuff[len - 1] == '/')) { + IObuff[len - 1] = NUL; + } + + if (len == 1 && IObuff[0] == '/') { + // Avoid ambiguity in the URI when CWD is root directory. + IObuff[1] = '.'; + IObuff[2] = NUL; + } + + // Terminal URI: "term://$CWD//$PID:$CMD" + snprintf(NameBuff, sizeof(NameBuff), "term://%s//%d:%s", IObuff, pid, cmd); + // Buffer has no terminal associated yet; unset 'swapfile' to ensure no swapfile is created. + curbuf->b_p_swf = false; + + apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, false, curbuf); + setfname(curbuf, NameBuff, NULL, true); + apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); + + Error err = ERROR_INIT; + // Set (deprecated) buffer-local vars (prefer 'channel' buffer-local option). + dict_set_var(curbuf->b_vars, cstr_as_string("terminal_job_id"), + INTEGER_OBJ((Integer)chan->id), false, false, NULL, &err); + api_clear_error(&err); + dict_set_var(curbuf->b_vars, cstr_as_string("terminal_job_pid"), + INTEGER_OBJ(pid), false, false, NULL, &err); + api_clear_error(&err); + + channel_incref(chan); + channel_terminal_open(curbuf, chan); + channel_create_event(chan, NULL); + channel_decref(chan); } } /// "jobstop()" function -static void f_jobstop(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +void f_jobstop(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -4100,8 +4187,6 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } - ui_busy_start(); - ui_flush(); list_T *args = argvars[0].vval.v_list; Channel **jobs = xcalloc((size_t)tv_list_len(args), sizeof(*jobs)); MultiQueue *waiting_jobs = multiqueue_new_parent(loop_on_put, &main_loop); @@ -4138,6 +4223,13 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) before = os_hrtime(); } + // Only mark the UI as busy when jobwait() blocks + const bool busy = remaining != 0; + if (busy) { + ui_busy_start(); + ui_flush(); + } + for (i = 0; i < tv_list_len(args); i++) { if (remaining == 0) { break; // Timeout. @@ -4179,7 +4271,9 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) multiqueue_free(waiting_jobs); xfree(jobs); - ui_busy_stop(); + if (busy) { + ui_busy_stop(); + } tv_list_ref(rv); rettv->v_type = VAR_LIST; rettv->vval.v_list = rv; @@ -4240,20 +4334,6 @@ static void f_keytrans(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) xfree(escaped); } -/// "last_buffer_nr()" function. -static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) -{ - int n = 0; - - FOR_ALL_BUFFERS(buf) { - if (n < buf->b_fnum) { - n = buf->b_fnum; - } - } - - rettv->vval.v_number = n; -} - /// "len()" function static void f_len(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { @@ -6348,103 +6428,6 @@ end: api_clear_error(&err); } -/// "rpcstart()" function (DEPRECATED) -static void f_rpcstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) -{ - rettv->v_type = VAR_NUMBER; - rettv->vval.v_number = 0; - - if (check_secure()) { - return; - } - - if (argvars[0].v_type != VAR_STRING - || (argvars[1].v_type != VAR_LIST && argvars[1].v_type != VAR_UNKNOWN)) { - // Wrong argument types - emsg(_(e_invarg)); - return; - } - - list_T *args = NULL; - int argsl = 0; - if (argvars[1].v_type == VAR_LIST) { - args = argvars[1].vval.v_list; - argsl = tv_list_len(args); - // Assert that all list items are strings - int i = 0; - TV_LIST_ITER_CONST(args, arg, { - if (TV_LIST_ITEM_TV(arg)->v_type != VAR_STRING) { - semsg(_("E5010: List item %d of the second argument is not a string"), - i); - return; - } - i++; - }); - } - - if (argvars[0].vval.v_string == NULL || argvars[0].vval.v_string[0] == NUL) { - emsg(_(e_api_spawn_failed)); - return; - } - - // Allocate extra memory for the argument vector and the NULL pointer - int argvl = argsl + 2; - char **argv = xmalloc(sizeof(char *) * (size_t)argvl); - - // Copy program name - argv[0] = xstrdup(argvars[0].vval.v_string); - - int i = 1; - // Copy arguments to the vector - if (argsl > 0) { - TV_LIST_ITER_CONST(args, arg, { - argv[i++] = xstrdup(tv_get_string(TV_LIST_ITEM_TV(arg))); - }); - } - - // The last item of argv must be NULL - argv[i] = NULL; - - Channel *chan = channel_job_start(argv, NULL, CALLBACK_READER_INIT, - CALLBACK_READER_INIT, CALLBACK_NONE, - false, true, false, false, - kChannelStdinPipe, NULL, 0, 0, NULL, - &rettv->vval.v_number); - if (chan) { - channel_create_event(chan, NULL); - } -} - -/// "rpcstop()" function -static void f_rpcstop(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) -{ - rettv->v_type = VAR_NUMBER; - rettv->vval.v_number = 0; - - if (check_secure()) { - return; - } - - if (argvars[0].v_type != VAR_NUMBER) { - // Wrong argument types - emsg(_(e_invarg)); - return; - } - - // if called with a job, stop it, else closes the channel - uint64_t id = (uint64_t)argvars[0].vval.v_number; - if (find_job(id, false)) { - f_jobstop(argvars, rettv, fptr); - } else { - const char *error; - rettv->vval.v_number = - channel_close((uint64_t)argvars[0].vval.v_number, kChannelPartRpc, &error); - if (!rettv->vval.v_number) { - emsg(error); - } - } -} - static void screenchar_adjust(ScreenGrid **grid, int *row, int *col) { // TODO(bfredl): this is a hack for legacy tests which use screenchar() @@ -8132,134 +8115,6 @@ static void f_taglist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) (char *)tag_pattern, (char *)fname); } -/// "termopen(cmd[, cwd])" function -static void f_termopen(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) -{ - if (check_secure()) { - return; - } - if (text_locked()) { - text_locked_msg(); - return; - } - if (curbuf->b_changed) { - emsg(_("Can only call this function in an unmodified buffer")); - return; - } - - const char *cmd; - bool executable = true; - char **argv = tv_to_argv(&argvars[0], &cmd, &executable); - if (!argv) { - rettv->vval.v_number = executable ? 0 : -1; - return; // Did error message in tv_to_argv. - } - - if (argvars[1].v_type != VAR_DICT && argvars[1].v_type != VAR_UNKNOWN) { - // Wrong argument type - semsg(_(e_invarg2), "expected dictionary"); - shell_free_argv(argv); - return; - } - - CallbackReader on_stdout = CALLBACK_READER_INIT; - CallbackReader on_stderr = CALLBACK_READER_INIT; - Callback on_exit = CALLBACK_NONE; - dict_T *job_opts = NULL; - const char *cwd = "."; - dict_T *env = NULL; - const bool pty = true; - bool clear_env = false; - dictitem_T *job_env = NULL; - - if (argvars[1].v_type == VAR_DICT) { - job_opts = argvars[1].vval.v_dict; - - const char *const new_cwd = tv_dict_get_string(job_opts, "cwd", false); - if (new_cwd && *new_cwd != NUL) { - cwd = new_cwd; - // The new cwd must be a directory. - if (!os_isdir(cwd)) { - semsg(_(e_invarg2), "expected valid directory"); - shell_free_argv(argv); - return; - } - } - - job_env = tv_dict_find(job_opts, S_LEN("env")); - if (job_env && job_env->di_tv.v_type != VAR_DICT) { - semsg(_(e_invarg2), "env"); - shell_free_argv(argv); - return; - } - - clear_env = tv_dict_get_number(job_opts, "clear_env") != 0; - - if (!common_job_callbacks(job_opts, &on_stdout, &on_stderr, &on_exit)) { - shell_free_argv(argv); - return; - } - } - - env = create_environment(job_env, clear_env, pty, "xterm-256color"); - - const bool rpc = false; - const bool overlapped = false; - const bool detach = false; - ChannelStdinMode stdin_mode = kChannelStdinPipe; - uint16_t term_width = (uint16_t)MAX(0, curwin->w_width_inner - win_col_off(curwin)); - Channel *chan = channel_job_start(argv, NULL, on_stdout, on_stderr, on_exit, - pty, rpc, overlapped, detach, stdin_mode, - cwd, term_width, (uint16_t)curwin->w_height_inner, - env, &rettv->vval.v_number); - if (rettv->vval.v_number <= 0) { - return; - } - - int pid = chan->stream.pty.proc.pid; - - // "./…" => "/home/foo/…" - vim_FullName(cwd, NameBuff, sizeof(NameBuff), false); - // "/home/foo/…" => "~/…" - size_t len = home_replace(NULL, NameBuff, IObuff, sizeof(IObuff), true); - // Trim slash. - if (len != 1 && (IObuff[len - 1] == '\\' || IObuff[len - 1] == '/')) { - IObuff[len - 1] = NUL; - } - - if (len == 1 && IObuff[0] == '/') { - // Avoid ambiguity in the URI when CWD is root directory. - IObuff[1] = '.'; - IObuff[2] = NUL; - } - - // Terminal URI: "term://$CWD//$PID:$CMD" - snprintf(NameBuff, sizeof(NameBuff), "term://%s//%d:%s", - IObuff, pid, cmd); - // at this point the buffer has no terminal instance associated yet, so unset - // the 'swapfile' option to ensure no swap file will be created - curbuf->b_p_swf = false; - - apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, false, curbuf); - setfname(curbuf, NameBuff, NULL, true); - apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); - - // Save the job id and pid in b:terminal_job_{id,pid} - Error err = ERROR_INIT; - // deprecated: use 'channel' buffer option - dict_set_var(curbuf->b_vars, cstr_as_string("terminal_job_id"), - INTEGER_OBJ((Integer)chan->id), false, false, NULL, &err); - api_clear_error(&err); - dict_set_var(curbuf->b_vars, cstr_as_string("terminal_job_pid"), - INTEGER_OBJ(pid), false, false, NULL, &err); - api_clear_error(&err); - - channel_incref(chan); - channel_terminal_open(curbuf, chan); - channel_create_event(chan, NULL); - channel_decref(chan); -} - /// "timer_info([timer])" function static void f_timer_info(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index e7b6a0feee..f9cf245e50 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -9,6 +9,7 @@ #include "nvim/ascii_defs.h" #include "nvim/assert_defs.h" +#include "nvim/buffer_defs.h" #include "nvim/charset.h" #include "nvim/errors.h" #include "nvim/eval.h" @@ -32,6 +33,7 @@ #include "nvim/mbyte.h" #include "nvim/mbyte_defs.h" #include "nvim/memory.h" +#include "nvim/memory_defs.h" #include "nvim/message.h" #include "nvim/os/input.h" #include "nvim/pos_defs.h" @@ -2232,6 +2234,18 @@ dictitem_T *tv_dict_find(const dict_T *const d, const char *const key, const ptr return TV_DICT_HI2DI(hi); } +/// Check if a key is present in a dictionary. +/// +/// @param[in] d Dictionary to check. +/// @param[in] key Dictionary key. +/// +/// @return whether the key is present in the dictionary. +bool tv_dict_has_key(const dict_T *const d, const char *const key) + FUNC_ATTR_NONNULL_ARG(2) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return tv_dict_find(d, key, -1) != NULL; +} + /// Get a typval item from a dictionary and copy it into "rettv". /// /// @param[in] d Dictionary to check. @@ -2631,6 +2645,30 @@ int tv_dict_add_allocated_str(dict_T *const d, const char *const key, const size return OK; } +/// Add a function entry to dictionary. +/// +/// @param[out] d Dictionary to add entry to. +/// @param[in] key Key to add. +/// @param[in] key_len Key length. +/// @param[in] fp Function to add. +/// +/// @return OK in case of success, FAIL when key already exists. +int tv_dict_add_func(dict_T *const d, const char *const key, const size_t key_len, + ufunc_T *const fp) + FUNC_ATTR_NONNULL_ARG(1, 2, 4) +{ + dictitem_T *const item = tv_dict_item_alloc_len(key, key_len); + + item->di_tv.v_type = VAR_FUNC; + item->di_tv.vval.v_string = xmemdupz(fp->uf_name, fp->uf_namelen); + if (tv_dict_add(d, item) == FAIL) { + tv_dict_item_free(item); + return FAIL; + } + func_ref(item->di_tv.vval.v_string); + return OK; +} + //{{{2 Operations on the whole dict /// Clear all the keys of a Dictionary. "d" remains a valid empty Dictionary. diff --git a/src/nvim/eval/typval_defs.h b/src/nvim/eval/typval_defs.h index 1ec7631c4b..90b0d4e4a9 100644 --- a/src/nvim/eval/typval_defs.h +++ b/src/nvim/eval/typval_defs.h @@ -357,9 +357,10 @@ struct ufunc { funccall_T *uf_scoped; ///< l: local variables for closure char *uf_name_exp; ///< if "uf_name[]" starts with SNR the name with ///< "<SNR>" as a string, otherwise NULL - char uf_name[]; ///< Name of function (actual size equals name); - ///< can start with <SNR>123_ - ///< (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) + size_t uf_namelen; ///< Length of uf_name (excluding the NUL) + char uf_name[]; ///< Name of function (actual size equals name); + ///< can start with <SNR>123_ + ///< (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) }; struct partial_S { diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 0050a77fe3..84ee27493c 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -78,7 +78,6 @@ static funccall_T *current_funccal = NULL; // item in it is still being used. static funccall_T *previous_funccal = NULL; -static const char *e_unknown_function_str = N_("E117: Unknown function: %s"); static const char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it"); static const char *e_funcdict = N_("E717: Dictionary entry already exists"); static const char *e_funcref = N_("E718: Funcref required"); @@ -105,6 +104,48 @@ hashtab_T *func_tbl_get(void) return &func_hashtab; } +/// Get one function argument. +/// Return a pointer to after the type. +/// When something is wrong return "arg". +static char *one_function_arg(char *arg, garray_T *newargs, bool skip) +{ + char *p = arg; + + while (ASCII_ISALNUM(*p) || *p == '_') { + p++; + } + if (arg == p || isdigit((uint8_t)(*arg)) + || (p - arg == 9 && strncmp(arg, "firstline", 9) == 0) + || (p - arg == 8 && strncmp(arg, "lastline", 8) == 0)) { + if (!skip) { + semsg(_("E125: Illegal argument: %s"), arg); + } + return arg; + } + + if (newargs != NULL) { + ga_grow(newargs, 1); + uint8_t c = (uint8_t)(*p); + *p = NUL; + char *arg_copy = xstrdup(arg); + + // Check for duplicate argument name. + for (int i = 0; i < newargs->ga_len; i++) { + if (strcmp(((char **)(newargs->ga_data))[i], arg_copy) == 0) { + semsg(_("E853: Duplicate argument name: %s"), arg_copy); + xfree(arg_copy); + return arg; + } + } + ((char **)(newargs->ga_data))[newargs->ga_len] = arg_copy; + newargs->ga_len++; + + *p = (char)c; + } + + return p; +} + /// Get function arguments. static int get_function_args(char **argp, char endchar, garray_T *newargs, int *varargs, garray_T *default_args, bool skip) @@ -135,36 +176,11 @@ static int get_function_args(char **argp, char endchar, garray_T *newargs, int * mustend = true; } else { arg = p; - while (ASCII_ISALNUM(*p) || *p == '_') { - p++; - } - if (arg == p || isdigit((uint8_t)(*arg)) - || (p - arg == 9 && strncmp(arg, "firstline", 9) == 0) - || (p - arg == 8 && strncmp(arg, "lastline", 8) == 0)) { - if (!skip) { - semsg(_("E125: Illegal argument: %s"), arg); - } + p = one_function_arg(p, newargs, skip); + if (p == arg) { break; } - if (newargs != NULL) { - ga_grow(newargs, 1); - uint8_t c = (uint8_t)(*p); - *p = NUL; - arg = xstrdup(arg); - - // Check for duplicate argument name. - for (int i = 0; i < newargs->ga_len; i++) { - if (strcmp(((char **)(newargs->ga_data))[i], arg) == 0) { - semsg(_("E853: Duplicate argument name: %s"), arg); - xfree(arg); - goto err_ret; - } - } - ((char **)(newargs->ga_data))[newargs->ga_len] = arg; - newargs->ga_len++; - *p = (char)c; - } if (*skipwhite(p) == '=' && default_args != NULL) { typval_T rettv; @@ -248,25 +264,47 @@ static void register_closure(ufunc_T *fp) [current_funccal->fc_ufuncs.ga_len++] = fp; } +static char lambda_name[8 + NUMBUFLEN]; +static size_t lambda_namelen = 0; + /// @return a name for a lambda. Returned in static memory. char *get_lambda_name(void) { - static char name[30]; static int lambda_no = 0; - snprintf(name, sizeof(name), "<lambda>%d", ++lambda_no); - return name; + int n = snprintf(lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no); + if (n < 1) { + lambda_namelen = 0; + } else if (n >= (int)sizeof(lambda_name)) { + lambda_namelen = sizeof(lambda_name) - 1; + } else { + lambda_namelen = (size_t)n; + } + + return lambda_name; } -static void set_ufunc_name(ufunc_T *fp, char *name) +/// Get the length of the last lambda name. +size_t get_lambda_name_len(void) { + return lambda_namelen; +} + +/// Allocate a "ufunc_T" for a function called "name". +static ufunc_T *alloc_ufunc(const char *name, size_t namelen) +{ + size_t len = offsetof(ufunc_T, uf_name) + namelen + 1; + ufunc_T *fp = xcalloc(1, len); STRCPY(fp->uf_name, name); + fp->uf_namelen = namelen; if ((uint8_t)name[0] == K_SPECIAL) { - fp->uf_name_exp = xmalloc(strlen(name) + 3); - STRCPY(fp->uf_name_exp, "<SNR>"); - strcat(fp->uf_name_exp, fp->uf_name + 3); + len = namelen + 3; + fp->uf_name_exp = xmalloc(len); + snprintf(fp->uf_name_exp, len, "<SNR>%s", fp->uf_name + 3); } + + return fp; } /// Parse a lambda expression and get a Funcref from "*arg". @@ -334,8 +372,9 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg) garray_T newlines; char *name = get_lambda_name(); + size_t namelen = get_lambda_name_len(); - fp = xcalloc(1, offsetof(ufunc_T, uf_name) + strlen(name) + 1); + fp = alloc_ufunc(name, namelen); pt = xcalloc(1, sizeof(partial_T)); ga_init(&newlines, (int)sizeof(char *), 1); @@ -353,7 +392,6 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg) } fp->uf_refcount = 1; - set_ufunc_name(fp, name); hash_add(&func_hashtab, UF2HIKEY(fp)); fp->uf_args = newargs; ga_init(&fp->uf_def_args, (int)sizeof(char *), 1); @@ -393,7 +431,10 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg) errret: ga_clear_strings(&newargs); - xfree(fp); + if (fp != NULL) { + xfree(fp->uf_name_exp); + xfree(fp); + } xfree(pt); if (evalarg != NULL && evalarg->eval_tofree == NULL) { evalarg->eval_tofree = tofree; @@ -611,33 +652,36 @@ static char *fname_trans_sid(const char *const name, char *const fname_buf, char int *const error) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - const int llen = eval_fname_script(name); - if (llen == 0) { + const char *script_name = name + eval_fname_script(name); + if (script_name == name) { return (char *)name; // no prefix } fname_buf[0] = (char)K_SPECIAL; fname_buf[1] = (char)KS_EXTRA; fname_buf[2] = KE_SNR; - int i = 3; - if (eval_fname_sid(name)) { // "<SID>" or "s:" + size_t fname_buflen = 3; + if (!eval_fname_sid(name)) { // "<SID>" or "s:" + fname_buf[fname_buflen] = NUL; + } else { if (current_sctx.sc_sid <= 0) { *error = FCERR_SCRIPT; } else { - snprintf(fname_buf + i, (size_t)(FLEN_FIXED + 1 - i), "%" PRId64 "_", - (int64_t)current_sctx.sc_sid); - i = (int)strlen(fname_buf); + fname_buflen += (size_t)snprintf(fname_buf + fname_buflen, + FLEN_FIXED + 1 - fname_buflen, + "%" PRIdSCID "_", + current_sctx.sc_sid); } } + size_t fnamelen = fname_buflen + strlen(script_name); char *fname; - if ((size_t)i + strlen(name + llen) < FLEN_FIXED) { - STRCPY(fname_buf + i, name + llen); + if (fnamelen < FLEN_FIXED) { + STRCPY(fname_buf + fname_buflen, script_name); fname = fname_buf; } else { - fname = xmalloc((size_t)i + strlen(name + llen) + 1); + fname = xmalloc(fnamelen + 1); *tofree = fname; - memmove(fname, fname_buf, (size_t)i); - STRCPY(fname + i, name + llen); + snprintf(fname, fnamelen + 1, "%s%s", fname_buf, script_name); } return fname; } @@ -695,20 +739,20 @@ ufunc_T *find_func(const char *name) /// Copy the function name of "fp" to buffer "buf". /// "buf" must be able to hold the function name plus three bytes. /// Takes care of script-local function names. -static void cat_func_name(char *buf, size_t buflen, ufunc_T *fp) +static int cat_func_name(char *buf, size_t bufsize, ufunc_T *fp) { int len = -1; - size_t uflen = strlen(fp->uf_name); + size_t uflen = fp->uf_namelen; assert(uflen > 0); if ((uint8_t)fp->uf_name[0] == K_SPECIAL && uflen > 3) { - len = snprintf(buf, buflen, "<SNR>%s", fp->uf_name + 3); + len = snprintf(buf, bufsize, "<SNR>%s", fp->uf_name + 3); } else { - len = snprintf(buf, buflen, "%s", fp->uf_name); + len = snprintf(buf, bufsize, "%s", fp->uf_name); } - (void)len; // Avoid unused warning on release builds assert(len > 0); + return (len >= (int)bufsize) ? (int)bufsize - 1 : len; } /// Add a number variable "name" to dict "dp" with value "nr". @@ -875,7 +919,6 @@ static void func_clear_items(ufunc_T *fp) ga_clear_strings(&(fp->uf_args)); ga_clear_strings(&(fp->uf_def_args)); ga_clear_strings(&(fp->uf_lines)); - XFREE_CLEAR(fp->uf_name_exp); if (fp->uf_flags & FC_LUAREF) { api_free_luaref(fp->uf_luaref); @@ -914,6 +957,8 @@ static void func_free(ufunc_T *fp) if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0) { func_remove(fp); } + + XFREE_CLEAR(fp->uf_name_exp); xfree(fp); } @@ -967,6 +1012,7 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rett bool islambda = false; char numbuf[NUMBUFLEN]; char *name; + size_t namelen; typval_T *tv_to_free[MAX_FUNC_ARGS]; int tv_to_free_len = 0; proftime_T wait_start; @@ -1092,23 +1138,25 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rett break; } } + + namelen = strlen(name); } else { if ((fp->uf_flags & FC_NOARGS) != 0) { // Bail out if no a: arguments used (in lambda). break; } // "..." argument a:1, a:2, etc. - snprintf(numbuf, sizeof(numbuf), "%d", ai + 1); + namelen = (size_t)snprintf(numbuf, sizeof(numbuf), "%d", ai + 1); name = numbuf; } - if (fixvar_idx < FIXVAR_CNT && strlen(name) <= VAR_SHORT_LEN) { + if (fixvar_idx < FIXVAR_CNT && namelen <= VAR_SHORT_LEN) { v = (dictitem_T *)&fc->fc_fixvar[fixvar_idx++]; v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; + STRCPY(v->di_key, name); } else { - v = xmalloc(sizeof(dictitem_T) + strlen(name)); - v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC; + v = tv_dict_item_alloc_len(name, namelen); + v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX; } - STRCPY(v->di_key, name); // Note: the values are copied directly to avoid alloc/free. // "argvars" must have VAR_FIXED for v_lock. @@ -2085,7 +2133,7 @@ char *trans_function_name(char **pp, bool skip, int flags, funcdict_T *fdp, part len = (int)(end - lv.ll_name); } - size_t sid_buf_len = 0; + size_t sid_buflen = 0; char sid_buf[20]; // Copy the function name to allocated memory. @@ -2095,15 +2143,16 @@ char *trans_function_name(char **pp, bool skip, int flags, funcdict_T *fdp, part lead = 0; // do nothing } else if (lead > 0) { lead = 3; - if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name)) || eval_fname_sid(*pp)) { + if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name)) + || eval_fname_sid(*pp)) { // It's "s:" or "<SID>". if (current_sctx.sc_sid <= 0) { emsg(_(e_usingsid)); goto theend; } - sid_buf_len = - (size_t)snprintf(sid_buf, sizeof(sid_buf), "%" PRIdSCID "_", current_sctx.sc_sid); - lead += (int)sid_buf_len; + sid_buflen = (size_t)snprintf(sid_buf, sizeof(sid_buf), "%" PRIdSCID "_", + current_sctx.sc_sid); + lead += (int)sid_buflen; } } else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, (int)lv.ll_name_len)) { semsg(_("E128: Function name must start with a capital or \"s:\": %s"), @@ -2125,8 +2174,8 @@ char *trans_function_name(char **pp, bool skip, int flags, funcdict_T *fdp, part name[0] = (char)K_SPECIAL; name[1] = (char)KS_EXTRA; name[2] = KE_SNR; - if (sid_buf_len > 0) { // If it's "<SID>" - memcpy(name + 3, sid_buf, sid_buf_len); + if (sid_buflen > 0) { // If it's "<SID>" + memcpy(name + 3, sid_buf, sid_buflen); } } memmove(name + lead, lv.ll_name, (size_t)len); @@ -2162,12 +2211,12 @@ char *get_scriptlocal_funcname(char *funcname) char sid_buf[25]; // Expand s: and <SID> prefix into <SNR>nr_<name> - snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRId64 "_", - (int64_t)current_sctx.sc_sid); + size_t sid_buflen = (size_t)snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRIdSCID "_", + current_sctx.sc_sid); const int off = *funcname == 's' ? 2 : 5; - char *newname = xmalloc(strlen(sid_buf) + strlen(funcname + off) + 1); - STRCPY(newname, sid_buf); - strcat(newname, funcname + off); + size_t newnamesize = sid_buflen + strlen(funcname + off) + 1; + char *newname = xmalloc(newnamesize); + snprintf(newname, newnamesize, "%s%s", sid_buf, funcname + off); return newname; } @@ -2193,8 +2242,6 @@ char *save_function_name(char **name, bool skip, int flags, funcdict_T *fudi) return saved; } -#define MAX_FUNC_NESTING 50 - /// List functions. /// /// @param regmatch When NULL, all of them. @@ -2225,12 +2272,291 @@ static void list_functions(regmatch_T *regmatch) } } +#define MAX_FUNC_NESTING 50 + +/// Read the body of a function, put every line in "newlines". +/// This stops at "endfunction". +/// "newlines" must already have been initialized. +static int get_function_body(exarg_T *eap, garray_T *newlines, char *line_arg_in, + char **line_to_free, bool show_block) +{ + bool saved_wait_return = need_wait_return; + char *line_arg = line_arg_in; + int indent = 2; + int nesting = 0; + char *skip_until = NULL; + int ret = FAIL; + bool is_heredoc = false; + char *heredoc_trimmed = NULL; + size_t heredoc_trimmedlen = 0; + bool do_concat = true; + + while (true) { + if (KeyTyped) { + msg_scroll = true; + saved_wait_return = false; + } + need_wait_return = false; + + char *theline; + char *p; + char *arg; + + if (line_arg != NULL) { + // Use eap->arg, split up in parts by line breaks. + theline = line_arg; + p = vim_strchr(theline, '\n'); + if (p == NULL) { + line_arg += strlen(line_arg); + } else { + *p = NUL; + line_arg = p + 1; + } + } else { + xfree(*line_to_free); + if (eap->ea_getline == NULL) { + theline = getcmdline(':', 0, indent, do_concat); + } else { + theline = eap->ea_getline(':', eap->cookie, indent, do_concat); + } + *line_to_free = theline; + } + if (KeyTyped) { + lines_left = Rows - 1; + } + if (theline == NULL) { + if (skip_until != NULL) { + semsg(_(e_missing_heredoc_end_marker_str), skip_until); + } else { + emsg(_("E126: Missing :endfunction")); + } + goto theend; + } + if (show_block) { + assert(indent >= 0); + ui_ext_cmdline_block_append((size_t)indent, theline); + } + + // Detect line continuation: SOURCING_LNUM increased more than one. + linenr_T sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie); + if (SOURCING_LNUM < sourcing_lnum_off) { + sourcing_lnum_off -= SOURCING_LNUM; + } else { + sourcing_lnum_off = 0; + } + + if (skip_until != NULL) { + // Don't check for ":endfunc" between + // * ":append" and "." + // * ":python <<EOF" and "EOF" + // * ":let {var-name} =<< [trim] {marker}" and "{marker}" + if (heredoc_trimmed == NULL + || (is_heredoc && skipwhite(theline) == theline) + || strncmp(theline, heredoc_trimmed, heredoc_trimmedlen) == 0) { + if (heredoc_trimmed == NULL) { + p = theline; + } else if (is_heredoc) { + p = skipwhite(theline) == theline ? theline : theline + heredoc_trimmedlen; + } else { + p = theline + heredoc_trimmedlen; + } + if (strcmp(p, skip_until) == 0) { + XFREE_CLEAR(skip_until); + XFREE_CLEAR(heredoc_trimmed); + heredoc_trimmedlen = 0; + do_concat = true; + is_heredoc = false; + } + } + } else { + // skip ':' and blanks + for (p = theline; ascii_iswhite(*p) || *p == ':'; p++) {} + + // Check for "endfunction". + if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) { + if (*p == '!') { + p++; + } + char *nextcmd = NULL; + if (*p == '|') { + nextcmd = p + 1; + } else if (line_arg != NULL && *skipwhite(line_arg) != NUL) { + nextcmd = line_arg; + } else if (*p != NUL && *p != '"' && p_verbose > 0) { + swmsg(true, _("W22: Text found after :endfunction: %s"), p); + } + if (nextcmd != NULL) { + // Another command follows. If the line came from "eap" we + // can simply point into it, otherwise we need to change + // "eap->cmdlinep". + eap->nextcmd = nextcmd; + if (*line_to_free != NULL) { + xfree(*eap->cmdlinep); + *eap->cmdlinep = *line_to_free; + *line_to_free = NULL; + } + } + break; + } + + // Increase indent inside "if", "while", "for" and "try", decrease + // at "end". + if (indent > 2 && strncmp(p, "end", 3) == 0) { + indent -= 2; + } else if (strncmp(p, "if", 2) == 0 + || strncmp(p, "wh", 2) == 0 + || strncmp(p, "for", 3) == 0 + || strncmp(p, "try", 3) == 0) { + indent += 2; + } + + // Check for defining a function inside this function. + if (checkforcmd(&p, "function", 2)) { + if (*p == '!') { + p = skipwhite(p + 1); + } + p += eval_fname_script(p); + xfree(trans_function_name(&p, true, 0, NULL, NULL)); + if (*skipwhite(p) == '(') { + if (nesting == MAX_FUNC_NESTING - 1) { + emsg(_(e_function_nesting_too_deep)); + } else { + nesting++; + indent += 2; + } + } + } + + // Check for ":append", ":change", ":insert". + p = skip_range(p, NULL); + if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) + || (p[0] == 'c' + && (!ASCII_ISALPHA(p[1]) + || (p[1] == 'h' && (!ASCII_ISALPHA(p[2]) + || (p[2] == 'a' + && (strncmp(&p[3], "nge", 3) != 0 + || !ASCII_ISALPHA(p[6]))))))) + || (p[0] == 'i' + && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' + && (!ASCII_ISALPHA(p[2]) + || (p[2] == 's')))))) { + skip_until = xmemdupz(".", 1); + } + + // heredoc: Check for ":python <<EOF", ":lua <<EOF", etc. + arg = skipwhite(skiptowhite(p)); + if (arg[0] == '<' && arg[1] == '<' + && ((p[0] == 'p' && p[1] == 'y' + && (!ASCII_ISALNUM(p[2]) || p[2] == 't' + || ((p[2] == '3' || p[2] == 'x') + && !ASCII_ISALPHA(p[3])))) + || (p[0] == 'p' && p[1] == 'e' + && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) + || (p[0] == 't' && p[1] == 'c' + && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) + || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a' + && !ASCII_ISALPHA(p[3])) + || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' + && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) + || (p[0] == 'm' && p[1] == 'z' + && (!ASCII_ISALPHA(p[2]) || p[2] == 's')))) { + // ":python <<" continues until a dot, like ":append" + p = skipwhite(arg + 2); + if (strncmp(p, "trim", 4) == 0 + && (p[4] == NUL || ascii_iswhite(p[4]))) { + // Ignore leading white space. + p = skipwhite(p + 4); + heredoc_trimmedlen = (size_t)(skipwhite(theline) - theline); + heredoc_trimmed = xmemdupz(theline, heredoc_trimmedlen); + } + if (*p == NUL) { + skip_until = xmemdupz(".", 1); + } else { + skip_until = xmemdupz(p, (size_t)(skiptowhite(p) - p)); + } + do_concat = false; + is_heredoc = true; + } + + if (!is_heredoc) { + // Check for ":let v =<< [trim] EOF" + // and ":let [a, b] =<< [trim] EOF" + arg = p; + if (checkforcmd(&arg, "let", 2)) { + int var_count = 0; + int semicolon = 0; + arg = (char *)skip_var_list(arg, &var_count, &semicolon, true); + if (arg != NULL) { + arg = skipwhite(arg); + } + if (arg != NULL && strncmp(arg, "=<<", 3) == 0) { + p = skipwhite(arg + 3); + bool has_trim = false; + while (true) { + if (strncmp(p, "trim", 4) == 0 + && (p[4] == NUL || ascii_iswhite(p[4]))) { + // Ignore leading white space. + p = skipwhite(p + 4); + has_trim = true; + continue; + } + if (strncmp(p, "eval", 4) == 0 + && (p[4] == NUL || ascii_iswhite(p[4]))) { + // Ignore leading white space. + p = skipwhite(p + 4); + continue; + } + break; + } + if (has_trim) { + heredoc_trimmedlen = (size_t)(skipwhite(theline) - theline); + heredoc_trimmed = xmemdupz(theline, heredoc_trimmedlen); + } + skip_until = xmemdupz(p, (size_t)(skiptowhite(p) - p)); + do_concat = false; + is_heredoc = true; + } + } + } + } + + // Add the line to the function. + ga_grow(newlines, 1 + (int)sourcing_lnum_off); + + // Copy the line to newly allocated memory. get_one_sourceline() + // allocates 250 bytes per line, this saves 80% on average. The cost + // is an extra alloc/free. + p = xstrdup(theline); + ((char **)(newlines->ga_data))[newlines->ga_len++] = p; + + // Add NULL lines for continuation lines, so that the line count is + // equal to the index in the growarray. + while (sourcing_lnum_off-- > 0) { + ((char **)(newlines->ga_data))[newlines->ga_len++] = NULL; + } + + // Check for end of eap->arg. + if (line_arg != NULL && *line_arg == NUL) { + line_arg = NULL; + } + } + + // Return OK when no error was detected. + if (!did_emsg) { + ret = OK; + } + +theend: + xfree(skip_until); + xfree(heredoc_trimmed); + need_wait_return |= saved_wait_return; + return ret; +} + /// ":function" void ex_function(exarg_T *eap) { - char *theline; char *line_to_free = NULL; - bool saved_wait_return = need_wait_return; char *arg; char *line_arg = NULL; garray_T newargs; @@ -2238,16 +2564,13 @@ void ex_function(exarg_T *eap) garray_T newlines; int varargs = false; int flags = 0; - ufunc_T *fp; + ufunc_T *fp = NULL; + bool free_fp = false; bool overwrite = false; funcdict_T fudi; static int func_nr = 0; // number for nameless function hashtab_T *ht; - bool is_heredoc = false; - char *skip_until = NULL; - char *heredoc_trimmed = NULL; bool show_block = false; - bool do_concat = true; // ":function" without argument: list functions. if (ends_excmd(*eap->arg)) { @@ -2494,251 +2817,14 @@ void ex_function(exarg_T *eap) // Save the starting line number. linenr_T sourcing_lnum_top = SOURCING_LNUM; - int indent = 2; - int nesting = 0; - while (true) { - if (KeyTyped) { - msg_scroll = true; - saved_wait_return = false; - } - need_wait_return = false; - - if (line_arg != NULL) { - // Use eap->arg, split up in parts by line breaks. - theline = line_arg; - p = vim_strchr(theline, '\n'); - if (p == NULL) { - line_arg += strlen(line_arg); - } else { - *p = NUL; - line_arg = p + 1; - } - } else { - xfree(line_to_free); - if (eap->ea_getline == NULL) { - theline = getcmdline(':', 0, indent, do_concat); - } else { - theline = eap->ea_getline(':', eap->cookie, indent, do_concat); - } - line_to_free = theline; - } - if (KeyTyped) { - lines_left = Rows - 1; - } - if (theline == NULL) { - if (skip_until != NULL) { - semsg(_(e_missing_heredoc_end_marker_str), skip_until); - } else { - emsg(_("E126: Missing :endfunction")); - } - goto erret; - } - if (show_block) { - assert(indent >= 0); - ui_ext_cmdline_block_append((size_t)indent, theline); - } - - // Detect line continuation: SOURCING_LNUM increased more than one. - linenr_T sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie); - if (SOURCING_LNUM < sourcing_lnum_off) { - sourcing_lnum_off -= SOURCING_LNUM; - } else { - sourcing_lnum_off = 0; - } - - if (skip_until != NULL) { - // Don't check for ":endfunc" between - // * ":append" and "." - // * ":python <<EOF" and "EOF" - // * ":let {var-name} =<< [trim] {marker}" and "{marker}" - if (heredoc_trimmed == NULL - || (is_heredoc && skipwhite(theline) == theline) - || strncmp(theline, heredoc_trimmed, - strlen(heredoc_trimmed)) == 0) { - if (heredoc_trimmed == NULL) { - p = theline; - } else if (is_heredoc) { - p = skipwhite(theline) == theline - ? theline : theline + strlen(heredoc_trimmed); - } else { - p = theline + strlen(heredoc_trimmed); - } - if (strcmp(p, skip_until) == 0) { - XFREE_CLEAR(skip_until); - XFREE_CLEAR(heredoc_trimmed); - do_concat = true; - is_heredoc = false; - } - } - } else { - // skip ':' and blanks - for (p = theline; ascii_iswhite(*p) || *p == ':'; p++) {} - - // Check for "endfunction". - if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) { - if (*p == '!') { - p++; - } - char *nextcmd = NULL; - if (*p == '|') { - nextcmd = p + 1; - } else if (line_arg != NULL && *skipwhite(line_arg) != NUL) { - nextcmd = line_arg; - } else if (*p != NUL && *p != '"' && p_verbose > 0) { - swmsg(true, _("W22: Text found after :endfunction: %s"), p); - } - if (nextcmd != NULL) { - // Another command follows. If the line came from "eap" we - // can simply point into it, otherwise we need to change - // "eap->cmdlinep". - eap->nextcmd = nextcmd; - if (line_to_free != NULL) { - xfree(*eap->cmdlinep); - *eap->cmdlinep = line_to_free; - line_to_free = NULL; - } - } - break; - } - - // Increase indent inside "if", "while", "for" and "try", decrease - // at "end". - if (indent > 2 && strncmp(p, "end", 3) == 0) { - indent -= 2; - } else if (strncmp(p, "if", 2) == 0 - || strncmp(p, "wh", 2) == 0 - || strncmp(p, "for", 3) == 0 - || strncmp(p, "try", 3) == 0) { - indent += 2; - } - - // Check for defining a function inside this function. - if (checkforcmd(&p, "function", 2)) { - if (*p == '!') { - p = skipwhite(p + 1); - } - p += eval_fname_script(p); - xfree(trans_function_name(&p, true, 0, NULL, NULL)); - if (*skipwhite(p) == '(') { - if (nesting == MAX_FUNC_NESTING - 1) { - emsg(_(e_function_nesting_too_deep)); - } else { - nesting++; - indent += 2; - } - } - } - - // Check for ":append", ":change", ":insert". - p = skip_range(p, NULL); - if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) - || (p[0] == 'c' - && (!ASCII_ISALPHA(p[1]) - || (p[1] == 'h' && (!ASCII_ISALPHA(p[2]) - || (p[2] == 'a' - && (strncmp(&p[3], "nge", 3) != 0 - || !ASCII_ISALPHA(p[6]))))))) - || (p[0] == 'i' - && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' - && (!ASCII_ISALPHA(p[2]) - || (p[2] == 's')))))) { - skip_until = xstrdup("."); - } - - // heredoc: Check for ":python <<EOF", ":lua <<EOF", etc. - arg = skipwhite(skiptowhite(p)); - if (arg[0] == '<' && arg[1] == '<' - && ((p[0] == 'p' && p[1] == 'y' - && (!ASCII_ISALNUM(p[2]) || p[2] == 't' - || ((p[2] == '3' || p[2] == 'x') - && !ASCII_ISALPHA(p[3])))) - || (p[0] == 'p' && p[1] == 'e' - && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) - || (p[0] == 't' && p[1] == 'c' - && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) - || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a' - && !ASCII_ISALPHA(p[3])) - || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' - && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) - || (p[0] == 'm' && p[1] == 'z' - && (!ASCII_ISALPHA(p[2]) || p[2] == 's')))) { - // ":python <<" continues until a dot, like ":append" - p = skipwhite(arg + 2); - if (strncmp(p, "trim", 4) == 0) { - // Ignore leading white space. - p = skipwhite(p + 4); - heredoc_trimmed = xmemdupz(theline, (size_t)(skipwhite(theline) - theline)); - } - if (*p == NUL) { - skip_until = xstrdup("."); - } else { - skip_until = xmemdupz(p, (size_t)(skiptowhite(p) - p)); - } - do_concat = false; - is_heredoc = true; - } - - // Check for ":let v =<< [trim] EOF" - // and ":let [a, b] =<< [trim] EOF" - arg = p; - if (checkforcmd(&arg, "let", 2)) { - int var_count = 0; - int semicolon = 0; - arg = (char *)skip_var_list(arg, &var_count, &semicolon, true); - if (arg != NULL) { - arg = skipwhite(arg); - } - if (arg != NULL && strncmp(arg, "=<<", 3) == 0) { - p = skipwhite(arg + 3); - while (true) { - if (strncmp(p, "trim", 4) == 0) { - // Ignore leading white space. - p = skipwhite(p + 4); - heredoc_trimmed = xmemdupz(theline, (size_t)(skipwhite(theline) - theline)); - continue; - } - if (strncmp(p, "eval", 4) == 0) { - // Ignore leading white space. - p = skipwhite(p + 4); - continue; - } - break; - } - skip_until = xmemdupz(p, (size_t)(skiptowhite(p) - p)); - do_concat = false; - is_heredoc = true; - } - } - } - - // Add the line to the function. - ga_grow(&newlines, 1 + (int)sourcing_lnum_off); - - // Copy the line to newly allocated memory. get_one_sourceline() - // allocates 250 bytes per line, this saves 80% on average. The cost - // is an extra alloc/free. - p = xstrdup(theline); - ((char **)(newlines.ga_data))[newlines.ga_len++] = p; - - // Add NULL lines for continuation lines, so that the line count is - // equal to the index in the growarray. - while (sourcing_lnum_off-- > 0) { - ((char **)(newlines.ga_data))[newlines.ga_len++] = NULL; - } - - // Check for end of eap->arg. - if (line_arg != NULL && *line_arg == NUL) { - line_arg = NULL; - } - } - - // Don't define the function when skipping commands or when an error was - // detected. - if (eap->skip || did_emsg) { + // Do not define the function when getting the body fails and when skipping. + if (get_function_body(eap, &newlines, line_arg, &line_to_free, show_block) == FAIL + || eap->skip) { goto erret; } // If there are no errors, add the function + size_t namelen = 0; if (fudi.fd_dict == NULL) { dictitem_T *v = find_var(name, strlen(name), &ht, false); if (v != NULL && v->di_tv.v_type == VAR_FUNC) { @@ -2754,11 +2840,11 @@ void ex_function(exarg_T *eap) && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)) { emsg_funcname(e_funcexts, name); - goto erret; + goto errret_keep; } if (fp->uf_calls > 0) { emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"), name); - goto erret; + goto errret_keep; } if (fp->uf_refcount > 1) { // This function is referenced somewhere, don't redefine it but @@ -2779,7 +2865,7 @@ void ex_function(exarg_T *eap) } } } else { - char numbuf[20]; + char numbuf[NUMBUFLEN]; fp = NULL; if (fudi.fd_newkey == NULL && !eap->forceit) { @@ -2799,8 +2885,8 @@ void ex_function(exarg_T *eap) // Give the function a sequential number. Can only be used with a // Funcref! xfree(name); - snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr); - name = xstrdup(numbuf); + namelen = (size_t)snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr); + name = xmemdupz(numbuf, namelen); } if (fp == NULL) { @@ -2824,7 +2910,10 @@ void ex_function(exarg_T *eap) } } - fp = xcalloc(1, offsetof(ufunc_T, uf_name) + strlen(name) + 1); + if (namelen == 0) { + namelen = strlen(name); + } + fp = alloc_ufunc(name, namelen); if (fudi.fd_dict != NULL) { if (fudi.fd_di == NULL) { @@ -2832,7 +2921,7 @@ void ex_function(exarg_T *eap) fudi.fd_di = tv_dict_item_alloc(fudi.fd_newkey); if (tv_dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) { xfree(fudi.fd_di); - xfree(fp); + XFREE_CLEAR(fp); goto erret; } } else { @@ -2840,19 +2929,18 @@ void ex_function(exarg_T *eap) tv_clear(&fudi.fd_di->di_tv); } fudi.fd_di->di_tv.v_type = VAR_FUNC; - fudi.fd_di->di_tv.vval.v_string = xstrdup(name); + fudi.fd_di->di_tv.vval.v_string = xmemdupz(name, namelen); // behave like "dict" was used flags |= FC_DICT; } // insert the new function in the function list - set_ufunc_name(fp, name); if (overwrite) { hashitem_T *hi = hash_find(&func_hashtab, name); hi->hi_key = UF2HIKEY(fp); } else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL) { - xfree(fp); + free_fp = true; goto erret; } fp->uf_refcount = 1; @@ -2881,18 +2969,27 @@ void ex_function(exarg_T *eap) goto ret_free; erret: + if (fp != NULL) { + // these were set to "newargs" and "default_args", which are cleared below + ga_init(&fp->uf_args, (int)sizeof(char *), 1); + ga_init(&fp->uf_def_args, (int)sizeof(char *), 1); + } +errret_2: + if (fp != NULL) { + XFREE_CLEAR(fp->uf_name_exp); + } + if (free_fp) { + XFREE_CLEAR(fp); + } +errret_keep: ga_clear_strings(&newargs); ga_clear_strings(&default_args); -errret_2: ga_clear_strings(&newlines); ret_free: - xfree(skip_until); - xfree(heredoc_trimmed); xfree(line_to_free); xfree(fudi.fd_newkey); xfree(name); did_emsg |= saved_did_emsg; - need_wait_return |= saved_wait_return; if (show_block) { ui_ext_cmdline_block_leave(); } @@ -2979,15 +3076,16 @@ char *get_user_func_name(expand_T *xp, int idx) return ""; // don't show dict and lambda functions } - if (strlen(fp->uf_name) + 4 >= IOSIZE) { + if (fp->uf_namelen + 4 >= IOSIZE) { return fp->uf_name; // Prevent overflow. } - cat_func_name(IObuff, IOSIZE, fp); + int len = cat_func_name(IObuff, IOSIZE, fp); if (xp->xp_context != EXPAND_USER_FUNC) { - xstrlcat(IObuff, "(", IOSIZE); + xstrlcpy(IObuff + len, "(", IOSIZE - (size_t)len); if (!fp->uf_varargs && GA_EMPTY(&fp->uf_args)) { - xstrlcat(IObuff, ")", IOSIZE); + len++; + xstrlcpy(IObuff + len, ")", IOSIZE - (size_t)len); } } return IObuff; @@ -3589,21 +3687,26 @@ char *get_return_cmd(void *rettv) { char *s = NULL; char *tofree = NULL; + size_t slen = 0; if (rettv != NULL) { tofree = s = encode_tv2echo((typval_T *)rettv, NULL); } if (s == NULL) { s = ""; + } else { + slen = strlen(s); } xstrlcpy(IObuff, ":return ", IOSIZE); xstrlcpy(IObuff + 8, s, IOSIZE - 8); - if (strlen(s) + 8 >= IOSIZE) { + size_t IObufflen = 8 + slen; + if (IObufflen >= IOSIZE) { STRCPY(IObuff + IOSIZE - 4, "..."); + IObufflen = IOSIZE - 1; } xfree(tofree); - return xstrdup(IObuff); + return xstrnsave(IObuff, IObufflen); } /// Get next function line. @@ -4046,7 +4149,8 @@ bool set_ref_in_func(char *name, ufunc_T *fp_in, int copyID) char *register_luafunc(LuaRef ref) { char *name = get_lambda_name(); - ufunc_T *fp = xcalloc(1, offsetof(ufunc_T, uf_name) + strlen(name) + 1); + size_t namelen = get_lambda_name_len(); + ufunc_T *fp = alloc_ufunc(name, namelen); fp->uf_refcount = 1; fp->uf_varargs = true; @@ -4055,7 +4159,6 @@ char *register_luafunc(LuaRef ref) fp->uf_script_ctx = current_sctx; fp->uf_luaref = ref; - STRCPY(fp->uf_name, name); hash_add(&func_hashtab, UF2HIKEY(fp)); // coverity[leaked_storage] diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index a8358aab51..c64477741d 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -40,7 +40,6 @@ #include "nvim/ops.h" #include "nvim/option.h" #include "nvim/option_defs.h" -#include "nvim/option_vars.h" #include "nvim/os/os.h" #include "nvim/search.h" #include "nvim/strings.h" @@ -811,9 +810,9 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, // Find the end of the name. char *arg_end = NULL; OptIndex opt_idx; - int scope; + int opt_flags; - char *const p = (char *)find_option_var_end((const char **)&arg, &opt_idx, &scope); + char *const p = (char *)find_option_var_end((const char **)&arg, &opt_idx, &opt_flags); if (p == NULL || (endchars != NULL && vim_strchr(endchars, (uint8_t)(*skipwhite(p))) == NULL)) { emsg(_(e_letunexp)); @@ -825,7 +824,7 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, bool is_tty_opt = is_tty_option(arg); bool hidden = is_option_hidden(opt_idx); - OptVal curval = is_tty_opt ? get_tty_option(arg) : get_option_value(opt_idx, scope); + OptVal curval = is_tty_opt ? get_tty_option(arg) : get_option_value(opt_idx, opt_flags); OptVal newval = NIL_OPTVAL; if (curval.type == kOptValTypeNil) { @@ -845,11 +844,10 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, goto theend; } - // Don't assume current and new values are of the same type in order to future-proof the code for - // when an option can have multiple types. - const bool is_num = ((curval.type == kOptValTypeNumber || curval.type == kOptValTypeBoolean) - && (newval.type == kOptValTypeNumber || newval.type == kOptValTypeBoolean)); - const bool is_string = curval.type == kOptValTypeString && newval.type == kOptValTypeString; + // Current value and new value must have the same type. + assert(curval.type == newval.type); + const bool is_num = curval.type == kOptValTypeNumber || curval.type == kOptValTypeBoolean; + const bool is_string = curval.type == kOptValTypeString; if (op != NULL && *op != '=') { if (!hidden && is_num) { // number or bool @@ -874,15 +872,19 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, } else { newval = BOOLEAN_OPTVAL(TRISTATE_FROM_INT(new_n)); } - } else if (!hidden && is_string - && curval.data.string.data != NULL && newval.data.string.data != NULL) { // string - OptVal newval_old = newval; - newval = CSTR_AS_OPTVAL(concat_str(curval.data.string.data, newval.data.string.data)); - optval_free(newval_old); + } else if (!hidden && is_string) { // string + const char *curval_data = curval.data.string.data; + const char *newval_data = newval.data.string.data; + + if (curval_data != NULL && newval_data != NULL) { + OptVal newval_old = newval; + newval = CSTR_AS_OPTVAL(concat_str(curval_data, newval_data)); + optval_free(newval_old); + } } } - const char *err = set_option_value_handle_tty(arg, opt_idx, newval, scope); + const char *err = set_option_value_handle_tty(arg, opt_idx, newval, opt_flags); arg_end = p; if (err != NULL) { emsg(_(err)); @@ -1409,6 +1411,7 @@ static void list_one_var(dictitem_T *v, const char *prefix, int *first) static void list_one_var_a(const char *prefix, const char *name, const ptrdiff_t name_len, const VarType type, const char *string, int *first) { + msg_ext_set_kind("list_cmd"); // don't use msg() to avoid overwriting "v:statusmsg" msg_start(); msg_puts(prefix); @@ -1902,8 +1905,6 @@ static void getwinvar(typval_T *argvars, typval_T *rettv, int off) /// /// @return Typval converted to OptVal. Must be freed by caller. /// Returns NIL_OPTVAL for invalid option name. -/// -/// TODO(famiu): Refactor this to support multitype options. static OptVal tv_to_optval(typval_T *tv, OptIndex opt_idx, const char *option, bool *error) { OptVal value = NIL_OPTVAL; diff --git a/src/nvim/eval/window.c b/src/nvim/eval/window.c index 86495f1cb6..2e2758d3bc 100644 --- a/src/nvim/eval/window.c +++ b/src/nvim/eval/window.c @@ -31,7 +31,6 @@ #include "nvim/types_defs.h" #include "nvim/vim_defs.h" #include "nvim/window.h" -#include "nvim/winfloat.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "eval/window.c.generated.h" @@ -326,6 +325,7 @@ static dict_T *get_win_info(win_T *wp, int16_t tpnr, int16_t winnr) tv_dict_add_nr(dict, S_LEN("winrow"), wp->w_winrow + 1); tv_dict_add_nr(dict, S_LEN("topline"), wp->w_topline); tv_dict_add_nr(dict, S_LEN("botline"), wp->w_botline - 1); + tv_dict_add_nr(dict, S_LEN("leftcol"), wp->w_leftcol); tv_dict_add_nr(dict, S_LEN("winbar"), wp->w_winbar_height); tv_dict_add_nr(dict, S_LEN("width"), wp->w_width_inner); tv_dict_add_nr(dict, S_LEN("bufnr"), wp->w_buffer->b_fnum); |