diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:40:31 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:40:31 +0000 |
commit | 339e2d15cc26fe86988ea06468d912a46c8d6f29 (patch) | |
tree | a6167fc8fcfc6ae2dc102f57b2473858eac34063 /src/nvim/api/options.c | |
parent | 067dc73729267c0262438a6fdd66e586f8496946 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.tar.gz rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.tar.bz2 rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.zip |
Merge remote-tracking branch 'upstream/master' into fix_repeatcmdline
Diffstat (limited to 'src/nvim/api/options.c')
-rw-r--r-- | src/nvim/api/options.c | 717 |
1 files changed, 370 insertions, 347 deletions
diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index bfcb99754f..c012a69c7b 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -1,80 +1,135 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - -#include <inttypes.h> -#include <limits.h> +#include <assert.h> #include <stdbool.h> #include <string.h> +#include "nvim/api/keysets_defs.h" #include "nvim/api/options.h" #include "nvim/api/private/defs.h" +#include "nvim/api/private/dispatch.h" #include "nvim/api/private/helpers.h" +#include "nvim/api/private/validate.h" #include "nvim/autocmd.h" -#include "nvim/buffer_defs.h" +#include "nvim/buffer.h" #include "nvim/eval/window.h" +#include "nvim/func_attr.h" #include "nvim/globals.h" +#include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/option.h" -#include "nvim/vim.h" +#include "nvim/option_vars.h" +#include "nvim/vim_defs.h" #include "nvim/window.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/options.c.generated.h" #endif -static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_type, void **from, +static int validate_option_value_args(Dict(option) *opts, char *name, int *scope, + OptReqScope *req_scope, void **from, char **filetype, Error *err) { - if (opts->scope.type == kObjectTypeString) { - if (!strcmp(opts->scope.data.string.data, "local")) { +#define HAS_KEY_X(d, v) HAS_KEY(d, option, v) + if (HAS_KEY_X(opts, scope)) { + if (!strcmp(opts->scope.data, "local")) { *scope = OPT_LOCAL; - } else if (!strcmp(opts->scope.data.string.data, "global")) { + } else if (!strcmp(opts->scope.data, "global")) { *scope = OPT_GLOBAL; } else { - api_set_error(err, kErrorTypeValidation, "invalid scope: must be 'local' or 'global'"); - return FAIL; + VALIDATE_EXP(false, "scope", "'local' or 'global'", NULL, { + return FAIL; + }); } - } else if (HAS_KEY(opts->scope)) { - api_set_error(err, kErrorTypeValidation, "invalid value for key: scope"); - return FAIL; } - *opt_type = SREQ_GLOBAL; + *req_scope = kOptReqGlobal; + + if (filetype != NULL && HAS_KEY_X(opts, filetype)) { + *filetype = opts->filetype.data; + } - if (opts->win.type == kObjectTypeInteger) { - *opt_type = SREQ_WIN; - *from = find_window_by_handle((int)opts->win.data.integer, err); + if (HAS_KEY_X(opts, win)) { + *req_scope = kOptReqWin; + *from = find_window_by_handle(opts->win, err); if (ERROR_SET(err)) { return FAIL; } - } else if (HAS_KEY(opts->win)) { - api_set_error(err, kErrorTypeValidation, "invalid value for key: win"); - return FAIL; } - if (opts->buf.type == kObjectTypeInteger) { + if (HAS_KEY_X(opts, buf)) { *scope = OPT_LOCAL; - *opt_type = SREQ_BUF; - *from = find_buffer_by_handle((int)opts->buf.data.integer, err); + *req_scope = kOptReqBuf; + *from = find_buffer_by_handle(opts->buf, err); if (ERROR_SET(err)) { return FAIL; } - } else if (HAS_KEY(opts->buf)) { - api_set_error(err, kErrorTypeValidation, "invalid value for key: buf"); - return FAIL; } - if (HAS_KEY(opts->scope) && HAS_KEY(opts->buf)) { - api_set_error(err, kErrorTypeValidation, "scope and buf cannot be used together"); + VALIDATE((!HAS_KEY_X(opts, filetype) + || !(HAS_KEY_X(opts, buf) || HAS_KEY_X(opts, scope) || HAS_KEY_X(opts, win))), + "%s", "cannot use 'filetype' with 'scope', 'buf' or 'win'", { return FAIL; - } + }); + + VALIDATE((!HAS_KEY_X(opts, scope) || !HAS_KEY_X(opts, buf)), "%s", + "cannot use both 'scope' and 'buf'", { + return FAIL; + }); - if (HAS_KEY(opts->win) && HAS_KEY(opts->buf)) { - api_set_error(err, kErrorTypeValidation, "buf and win cannot be used together"); + VALIDATE((!HAS_KEY_X(opts, win) || !HAS_KEY_X(opts, buf)), + "%s", "cannot use both 'buf' and 'win'", { return FAIL; + }); + + int flags = get_option_attrs(name); + if (flags == 0) { + // hidden or unknown option + api_set_error(err, kErrorTypeValidation, "Unknown option '%s'", name); + } else if (*req_scope == kOptReqBuf || *req_scope == kOptReqWin) { + // if 'buf' or 'win' is passed, make sure the option supports it + int req_flags = *req_scope == kOptReqBuf ? SOPT_BUF : SOPT_WIN; + if (!(flags & req_flags)) { + char *tgt = *req_scope & kOptReqBuf ? "buf" : "win"; + char *global = flags & SOPT_GLOBAL ? "global " : ""; + char *req = flags & SOPT_BUF ? "buffer-local " + : flags & SOPT_WIN ? "window-local " : ""; + + api_set_error(err, kErrorTypeValidation, "'%s' cannot be passed for %s%soption '%s'", + tgt, global, req, name); + } } return OK; +#undef HAS_KEY_X +} + +/// Create a dummy buffer and run the FileType autocmd on it. +static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err) +{ + if (filetype == NULL) { + return NULL; + } + + // Allocate a buffer without putting it in the buffer list. + buf_T *ftbuf = buflist_new(NULL, NULL, 1, BLN_DUMMY); + if (ftbuf == NULL) { + api_set_error(err, kErrorTypeException, "Could not create internal buffer"); + return NULL; + } + + // Set curwin/curbuf to buf and save a few things. + aucmd_prepbuf(aco, ftbuf); + + TRY_WRAP(err, { + set_option_value("bufhidden", STATIC_CSTR_AS_OPTVAL("hide"), OPT_LOCAL); + set_option_value("buftype", STATIC_CSTR_AS_OPTVAL("nofile"), OPT_LOCAL); + set_option_value("swapfile", BOOLEAN_OPTVAL(false), OPT_LOCAL); + set_option_value("modeline", BOOLEAN_OPTVAL(false), OPT_LOCAL); // 'nomodeline' + + ftbuf->b_p_ft = xstrdup(filetype); + do_filetype_autocmd(ftbuf, false); + }); + + return ftbuf; } /// Gets the value of an option. The behavior of this function matches that of @@ -89,53 +144,61 @@ static int validate_option_value_args(Dict(option) *opts, int *scope, int *opt_t /// - win: |window-ID|. Used for getting window local options. /// - buf: Buffer number. Used for getting buffer local options. /// Implies {scope} is "local". +/// - filetype: |filetype|. Used to get the default option for a +/// specific filetype. Cannot be used with any other option. +/// Note: this will trigger |ftplugin| and all |FileType| +/// autocommands for the corresponding filetype. /// @param[out] err Error details, if any /// @return Option value Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) FUNC_API_SINCE(9) { Object rv = OBJECT_INIT; + OptVal value = NIL_OPTVAL; int scope = 0; - int opt_type = SREQ_GLOBAL; + OptReqScope req_scope = kOptReqGlobal; void *from = NULL; - if (!validate_option_value_args(opts, &scope, &opt_type, &from, err)) { - return rv; + char *filetype = NULL; + + if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, &filetype, err)) { + goto err; } - long numval = 0; - char *stringval = NULL; - getoption_T result = access_option_value_for(name.data, &numval, &stringval, scope, opt_type, - from, true, err); + aco_save_T aco; + + buf_T *ftbuf = do_ft_buf(filetype, &aco, err); if (ERROR_SET(err)) { - return rv; + goto err; } - switch (result) { - case gov_string: - rv = STRING_OBJ(cstr_as_string(stringval)); - break; - case gov_number: - rv = INTEGER_OBJ(numval); - break; - case gov_bool: - switch (numval) { - case 0: - case 1: - rv = BOOLEAN_OBJ(numval); - break; - default: - // Boolean options that return something other than 0 or 1 should return nil. Currently this - // only applies to 'autoread' which uses -1 as a local value to indicate "unset" - rv = NIL; - break; - } - break; - default: - api_set_error(err, kErrorTypeValidation, "unknown option '%s'", name.data); - return rv; + if (ftbuf != NULL) { + assert(!from); + from = ftbuf; + } + + bool hidden; + value = get_option_value_for(name.data, NULL, scope, &hidden, req_scope, from, err); + + if (ftbuf != NULL) { + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); + + assert(curbuf != ftbuf); // safety check + wipe_buffer(ftbuf, false); + } + + if (ERROR_SET(err)) { + goto err; } + VALIDATE_S(!hidden && value.type != kOptValTypeNil, "option", name.data, { + goto err; + }); + + return optval_as_object(value); +err: + optval_free(value); return rv; } @@ -153,13 +216,14 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) /// - win: |window-ID|. Used for setting window local option. /// - buf: Buffer number. Used for setting buffer local option. /// @param[out] err Error details, if any -void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error *err) +void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(option) *opts, + Error *err) FUNC_API_SINCE(9) { int scope = 0; - int opt_type = SREQ_GLOBAL; + OptReqScope req_scope = kOptReqGlobal; void *to = NULL; - if (!validate_option_value_args(opts, &scope, &opt_type, &to, err)) { + if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &to, NULL, err)) { return; } @@ -169,41 +233,35 @@ void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error // - option is global or local to window (global-local) // // Then force scope to local since we don't want to change the global option - if (opt_type == SREQ_WIN && scope == 0) { - int flags = get_option_value_strict(name.data, NULL, NULL, opt_type, to); + if (req_scope == kOptReqWin && scope == 0) { + int flags = get_option_attrs(name.data); if (flags & SOPT_GLOBAL) { scope = OPT_LOCAL; } } - long numval = 0; - char *stringval = NULL; + bool error = false; + OptVal optval = object_as_optval(value, &error); - switch (value.type) { - case kObjectTypeInteger: - numval = value.data.integer; - break; - case kObjectTypeBoolean: - numval = value.data.boolean ? 1 : 0; - break; - case kObjectTypeString: - stringval = value.data.string.data; - break; - case kObjectTypeNil: - scope |= OPT_CLEAR; - break; - default: - api_set_error(err, kErrorTypeValidation, "invalid value for option"); + // Handle invalid option value type. + // Don't use `name` in the error message here, because `name` can be any String. + // No need to check if value type actually matches the types for the option, as set_option_value() + // already handles that. + VALIDATE_EXP(!error, "value", "valid option type", api_typename(value.type), { return; - } + }); - access_option_value_for(name.data, &numval, &stringval, scope, opt_type, to, false, err); + WITH_SCRIPT_CONTEXT(channel_id, { + set_option_value_for(name.data, optval, scope, req_scope, to, err); + }); } /// Gets the option information for all options. /// /// The dictionary has the full option names as keys and option metadata -/// dictionaries as detailed at |nvim_get_option_info()|. +/// dictionaries as detailed at |nvim_get_option_info2()|. +/// +/// @see |nvim_get_commands()| /// /// @return dictionary of all options Dictionary nvim_get_all_options_info(Error *err) @@ -212,7 +270,7 @@ Dictionary nvim_get_all_options_info(Error *err) return get_all_vimoptions(); } -/// Gets the option information for one option +/// Gets the option information for one option from arbitrary buffer or window /// /// Resulting dictionary has keys: /// - name: Name of the option (like 'filetype') @@ -231,324 +289,289 @@ Dictionary nvim_get_all_options_info(Error *err) /// - commalist: List of comma separated values /// - flaglist: List of single char flags /// +/// When {scope} is not provided, the last set information applies to the local +/// value in the current buffer or window if it is available, otherwise the +/// global value information is returned. This behavior can be disabled by +/// explicitly specifying {scope} in the {opts} table. /// -/// @param name Option name +/// @param name Option name +/// @param opts Optional parameters +/// - scope: One of "global" or "local". Analogous to +/// |:setglobal| and |:setlocal|, respectively. +/// - win: |window-ID|. Used for getting window local options. +/// - buf: Buffer number. Used for getting buffer local options. +/// Implies {scope} is "local". /// @param[out] err Error details, if any /// @return Option Information -Dictionary nvim_get_option_info(String name, Error *err) - FUNC_API_SINCE(7) +Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err) + FUNC_API_SINCE(11) { - return get_vimoption(name, err); -} -/// Sets the global value of an option. -/// -/// @param channel_id -/// @param name Option name -/// @param value New option value -/// @param[out] err Error details, if any -void nvim_set_option(uint64_t channel_id, String name, Object value, Error *err) - FUNC_API_SINCE(1) -{ - set_option_to(channel_id, NULL, SREQ_GLOBAL, name, value, err); -} + int scope = 0; + OptReqScope req_scope = kOptReqGlobal; + void *from = NULL; + if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, NULL, err)) { + return (Dictionary)ARRAY_DICT_INIT; + } -/// Gets the global value of an option. -/// -/// @param name Option name -/// @param[out] err Error details, if any -/// @return Option value (global) -Object nvim_get_option(String name, Arena *arena, Error *err) - FUNC_API_SINCE(1) -{ - return get_option_from(NULL, SREQ_GLOBAL, name, err); + buf_T *buf = (req_scope == kOptReqBuf) ? (buf_T *)from : curbuf; + win_T *win = (req_scope == kOptReqWin) ? (win_T *)from : curwin; + + return get_vimoption(name, scope, buf, win, err); } -/// Gets a buffer option value +/// Switch current context to get/set option value for window/buffer. +/// +/// @param[out] ctx Current context. switchwin_T for window and aco_save_T for buffer. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param[in] from Target buffer/window. +/// @param[out] err Error message, if any. /// -/// @param buffer Buffer handle, or 0 for current buffer -/// @param name Option name -/// @param[out] err Error details, if any -/// @return Option value -Object nvim_buf_get_option(Buffer buffer, String name, Arena *arena, Error *err) - FUNC_API_SINCE(1) +/// @return true if context was switched, false otherwise. +static bool switch_option_context(void *const ctx, OptReqScope req_scope, void *const from, + Error *err) { - buf_T *buf = find_buffer_by_handle(buffer, err); + switch (req_scope) { + case kOptReqWin: { + win_T *const win = (win_T *)from; + switchwin_T *const switchwin = (switchwin_T *)ctx; + + if (win == curwin) { + return false; + } - if (!buf) { - return (Object)OBJECT_INIT; + if (switch_win_noblock(switchwin, win, win_find_tabpage(win), true) + == FAIL) { + restore_win_noblock(switchwin, true); + + if (try_end(err)) { + return false; + } + api_set_error(err, kErrorTypeException, "Problem while switching windows"); + return false; + } + return true; } + case kOptReqBuf: { + buf_T *const buf = (buf_T *)from; + aco_save_T *const aco = (aco_save_T *)ctx; - return get_option_from(buf, SREQ_BUF, name, err); + if (buf == curbuf) { + return false; + } + aucmd_prepbuf(aco, buf); + return true; + } + case kOptReqGlobal: + return false; + } + UNREACHABLE; } -/// Sets a buffer option value. Passing `nil` as value deletes the option (only -/// works if there's a global fallback) -/// -/// @param channel_id -/// @param buffer Buffer handle, or 0 for current buffer -/// @param name Option name -/// @param value Option value -/// @param[out] err Error details, if any -void nvim_buf_set_option(uint64_t channel_id, Buffer buffer, String name, Object value, Error *err) - FUNC_API_SINCE(1) +/// Restore context after getting/setting option for window/buffer. See switch_option_context() for +/// params. +static void restore_option_context(void *const ctx, OptReqScope req_scope) { - buf_T *buf = find_buffer_by_handle(buffer, err); - - if (!buf) { - return; + switch (req_scope) { + case kOptReqWin: + restore_win_noblock((switchwin_T *)ctx, true); + break; + case kOptReqBuf: + aucmd_restbuf((aco_save_T *)ctx); + break; + case kOptReqGlobal: + break; } - - set_option_to(channel_id, buf, SREQ_BUF, name, value, err); } -/// Gets a window option value +/// Get attributes for an option. /// -/// @param window Window handle, or 0 for current window -/// @param name Option name -/// @param[out] err Error details, if any -/// @return Option value -Object nvim_win_get_option(Window window, String name, Arena *arena, Error *err) - FUNC_API_SINCE(1) +/// @param name Option name. +/// +/// @return Option attributes. +/// 0 for hidden or unknown option. +/// See SOPT_* in option_defs.h for other flags. +int get_option_attrs(char *name) { - win_T *win = find_window_by_handle(window, err); + int opt_idx = findoption(name); - if (!win) { - return (Object)OBJECT_INIT; + if (opt_idx < 0) { + return 0; } - return get_option_from(win, SREQ_WIN, name, err); -} + vimoption_T *opt = get_option(opt_idx); -/// Sets a window option value. Passing `nil` as value deletes the option (only -/// works if there's a global fallback) -/// -/// @param channel_id -/// @param window Window handle, or 0 for current window -/// @param name Option name -/// @param value Option value -/// @param[out] err Error details, if any -void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object value, Error *err) - FUNC_API_SINCE(1) -{ - win_T *win = find_window_by_handle(window, err); + if (is_tty_option(opt->fullname)) { + return SOPT_STRING | SOPT_GLOBAL; + } - if (!win) { - return; + // Hidden option + if (opt->var == NULL) { + return 0; } - set_option_to(channel_id, win, SREQ_WIN, name, value, err); -} + int attrs = 0; -/// Gets the value of a global or local (buffer, window) option. -/// -/// @param from If `type` is `SREQ_WIN` or `SREQ_BUF`, this must be a pointer -/// to the window or buffer. -/// @param type One of `SREQ_GLOBAL`, `SREQ_WIN` or `SREQ_BUF` -/// @param name The option name -/// @param[out] err Details of an error that may have occurred -/// @return the option value -static Object get_option_from(void *from, int type, String name, Error *err) -{ - Object rv = OBJECT_INIT; + if (opt->flags & P_BOOL) { + attrs |= SOPT_BOOL; + } else if (opt->flags & P_NUM) { + attrs |= SOPT_NUM; + } else if (opt->flags & P_STRING) { + attrs |= SOPT_STRING; + } - if (name.size == 0) { - api_set_error(err, kErrorTypeValidation, "Empty option name"); - return rv; - } - - // Return values - int64_t numval; - char *stringval = NULL; - int flags = get_option_value_strict(name.data, &numval, &stringval, type, from); - - if (!flags) { - api_set_error(err, kErrorTypeValidation, "Invalid option name: '%s'", - name.data); - return rv; - } - - if (flags & SOPT_BOOL) { - rv.type = kObjectTypeBoolean; - rv.data.boolean = numval ? true : false; - } else if (flags & SOPT_NUM) { - rv.type = kObjectTypeInteger; - rv.data.integer = numval; - } else if (flags & SOPT_STRING) { - if (stringval) { - rv.type = kObjectTypeString; - rv.data.string.data = stringval; - rv.data.string.size = strlen(stringval); - } else { - api_set_error(err, kErrorTypeException, - "Failed to get value for option '%s'", - name.data); - } - } else { - api_set_error(err, - kErrorTypeException, - "Unknown type for option '%s'", - name.data); + if (opt->indir == PV_NONE || (opt->indir & PV_BOTH)) { + attrs |= SOPT_GLOBAL; + } + if (opt->indir & PV_WIN) { + attrs |= SOPT_WIN; + } else if (opt->indir & PV_BUF) { + attrs |= SOPT_BUF; } - return rv; + return attrs; } -/// Sets the value of a global or local (buffer, window) option. +/// Check if option has a value in the requested scope. /// -/// @param to If `type` is `SREQ_WIN` or `SREQ_BUF`, this must be a pointer -/// to the window or buffer. -/// @param type One of `SREQ_GLOBAL`, `SREQ_WIN` or `SREQ_BUF` -/// @param name The option name -/// @param[out] err Details of an error that may have occurred -void set_option_to(uint64_t channel_id, void *to, int type, String name, Object value, Error *err) +/// @param name Option name. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// +/// @return true if option has a value in the requested scope, false otherwise. +static bool option_has_scope(char *name, OptReqScope req_scope) { - if (name.size == 0) { - api_set_error(err, kErrorTypeValidation, "Empty option name"); - return; + int opt_idx = findoption(name); + + if (opt_idx < 0) { + return false; } - int flags = get_option_value_strict(name.data, NULL, NULL, type, to); + vimoption_T *opt = get_option(opt_idx); - if (flags == 0) { - api_set_error(err, kErrorTypeValidation, "Invalid option name '%s'", - name.data); - return; + // Hidden option. + if (opt->var == NULL) { + return false; + } + // TTY option. + if (is_tty_option(opt->fullname)) { + return req_scope == kOptReqGlobal; } - if (value.type == kObjectTypeNil) { - if (type == SREQ_GLOBAL) { - api_set_error(err, kErrorTypeException, "Cannot unset option '%s'", - name.data); - return; - } else if (!(flags & SOPT_GLOBAL)) { - api_set_error(err, - kErrorTypeException, - "Cannot unset option '%s' " - "because it doesn't have a global value", - name.data); - return; - } else { - unset_global_local_option(name.data, to); - return; - } + switch (req_scope) { + case kOptReqGlobal: + return opt->var != VAR_WIN; + case kOptReqBuf: + return opt->indir & PV_BUF; + case kOptReqWin: + return opt->indir & PV_WIN; } + UNREACHABLE; +} - long numval = 0; - char *stringval = NULL; +/// Get the option value in the requested scope. +/// +/// @param name Option name. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param[in] from Pointer to buffer or window for local option value. +/// @param[out] err Error message, if any. +/// +/// @return Option value in the requested scope. Returns a Nil option value if option is not found, +/// hidden or if it isn't present in the requested scope. (i.e. has no global, window-local or +/// buffer-local value depending on opt_scope). +OptVal get_option_value_strict(char *name, OptReqScope req_scope, void *from, Error *err) +{ + OptVal retv = NIL_OPTVAL; - if (flags & SOPT_BOOL) { - if (value.type != kObjectTypeBoolean) { - api_set_error(err, - kErrorTypeValidation, - "Option '%s' requires a Boolean value", - name.data); - return; - } + if (!option_has_scope(name, req_scope)) { + return retv; + } + if (get_tty_option(name, &retv.data.string.data)) { + retv.type = kOptValTypeString; + return retv; + } - numval = value.data.boolean; - } else if (flags & SOPT_NUM) { - if (value.type != kObjectTypeInteger) { - api_set_error(err, kErrorTypeValidation, - "Option '%s' requires an integer value", - name.data); - return; - } + int opt_idx = findoption(name); + assert(opt_idx != 0); // option_has_scope() already verifies if option name is valid. - if (value.data.integer > INT_MAX || value.data.integer < INT_MIN) { - api_set_error(err, kErrorTypeValidation, - "Value for option '%s' is out of range", - name.data); - return; - } + vimoption_T *opt = get_option(opt_idx); + switchwin_T switchwin; + aco_save_T aco; + void *ctx = req_scope == kOptReqWin ? (void *)&switchwin + : (req_scope == kOptReqBuf ? (void *)&aco : NULL); + bool switched = switch_option_context(ctx, req_scope, from, err); + if (ERROR_SET(err)) { + return retv; + } - numval = (int)value.data.integer; - } else { - if (value.type != kObjectTypeString) { - api_set_error(err, kErrorTypeValidation, - "Option '%s' requires a string value", - name.data); - return; - } + char *varp = get_varp_scope(opt, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL); + retv = optval_from_varp(opt_idx, varp); - stringval = value.data.string.data; + if (switched) { + restore_option_context(ctx, req_scope); } - // For global-win-local options -> setlocal - // For win-local options -> setglobal and setlocal (opt_flags == 0) - const int opt_flags = (type == SREQ_WIN && !(flags & SOPT_GLOBAL)) ? 0 : - (type == SREQ_GLOBAL) ? OPT_GLOBAL : OPT_LOCAL; - - WITH_SCRIPT_CONTEXT(channel_id, { - access_option_value_for(name.data, &numval, &stringval, opt_flags, type, to, false, err); - }); + return retv; } -static getoption_T access_option_value(char *key, long *numval, char **stringval, int opt_flags, - bool get, Error *err) +/// Get option value for buffer / window. +/// +/// @param[in] name Option name. +/// @param[out] flagsp Set to the option flags (P_xxxx) (if not NULL). +/// @param[in] scope Option scope (can be OPT_LOCAL, OPT_GLOBAL or a combination). +/// @param[out] hidden Whether option is hidden. +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param[in] from Target buffer/window. +/// @param[out] err Error message, if any. +/// +/// @return Option value. Must be freed by caller. +OptVal get_option_value_for(const char *const name, uint32_t *flagsp, int scope, bool *hidden, + const OptReqScope req_scope, void *const from, Error *err) { - if (get) { - return get_option_value(key, numval, stringval, NULL, opt_flags); - } else { - char *errmsg; - if ((errmsg = set_option_value(key, *numval, *stringval, opt_flags))) { - if (try_end(err)) { - return 0; - } + switchwin_T switchwin; + aco_save_T aco; + void *ctx = req_scope == kOptReqWin ? (void *)&switchwin + : (req_scope == kOptReqBuf ? (void *)&aco : NULL); - api_set_error(err, kErrorTypeException, "%s", errmsg); - } - return 0; + bool switched = switch_option_context(ctx, req_scope, from, err); + if (ERROR_SET(err)) { + return NIL_OPTVAL; + } + + OptVal retv = get_option_value(name, flagsp, scope, hidden); + + if (switched) { + restore_option_context(ctx, req_scope); } + + return retv; } -static getoption_T access_option_value_for(char *key, long *numval, char **stringval, int opt_flags, - int opt_type, void *from, bool get, Error *err) +/// Set option value for buffer / window. +/// +/// @param[in] name Option name. +/// @param[in] value Option value. +/// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both). +/// @param req_scope Requested option scope. See OptReqScope in option.h. +/// @param[in] from Target buffer/window. +/// @param[out] err Error message, if any. +void set_option_value_for(const char *const name, OptVal value, const int opt_flags, + const OptReqScope req_scope, void *const from, Error *err) { - bool need_switch = false; switchwin_T switchwin; aco_save_T aco; - getoption_T result = 0; - - try_start(); - switch (opt_type) { - case SREQ_WIN: - need_switch = (win_T *)from != curwin; - if (need_switch) { - if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true) - == FAIL) { - restore_win_noblock(&switchwin, true); - if (try_end(err)) { - return result; - } - api_set_error(err, kErrorTypeException, "Problem while switching windows"); - return result; - } - } - result = access_option_value(key, numval, stringval, opt_flags, get, err); - if (need_switch) { - restore_win_noblock(&switchwin, true); - } - break; - case SREQ_BUF: - need_switch = (buf_T *)from != curbuf; - if (need_switch) { - aucmd_prepbuf(&aco, (buf_T *)from); - } - result = access_option_value(key, numval, stringval, opt_flags, get, err); - if (need_switch) { - aucmd_restbuf(&aco); - } - break; - case SREQ_GLOBAL: - result = access_option_value(key, numval, stringval, opt_flags, get, err); - break; - } + void *ctx = req_scope == kOptReqWin ? (void *)&switchwin + : (req_scope == kOptReqBuf ? (void *)&aco : NULL); + bool switched = switch_option_context(ctx, req_scope, from, err); if (ERROR_SET(err)) { - return result; + return; } - try_end(err); + const char *const errmsg = set_option_value(name, value, opt_flags); + if (errmsg) { + api_set_error(err, kErrorTypeException, "%s", errmsg); + } - return result; + if (switched) { + restore_option_context(ctx, req_scope); + } } |