mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
refactor(options): get_option_value_strict()
and SREQ_*
`SREQ_*` values are now actual typedef'd enums. `get_option_value_strict()` has also been refactored and split into two functions, `get_option_attrs()` for getting the option attributes, and `get_option_value_strict()` for getting the actual value. Moreover, it now returns an `OptVal`. Other miscellaneous refactors have also been made.
This commit is contained in:
committed by
Lewis Russell
parent
f4b896198f
commit
6c87d3e0fb
@ -536,7 +536,7 @@ void nvim_set_option(uint64_t channel_id, String name, Object value, Error *err)
|
||||
FUNC_API_SINCE(1)
|
||||
FUNC_API_DEPRECATED_SINCE(11)
|
||||
{
|
||||
set_option_to(channel_id, NULL, SREQ_GLOBAL, name, value, err);
|
||||
set_option_to(channel_id, NULL, kOptReqGlobal, name, value, err);
|
||||
}
|
||||
|
||||
/// Gets the global value of an option.
|
||||
@ -549,7 +549,7 @@ Object nvim_get_option(String name, Arena *arena, Error *err)
|
||||
FUNC_API_SINCE(1)
|
||||
FUNC_API_DEPRECATED_SINCE(11)
|
||||
{
|
||||
return get_option_from(NULL, SREQ_GLOBAL, name, err);
|
||||
return get_option_from(NULL, kOptReqGlobal, name, err);
|
||||
}
|
||||
|
||||
/// Gets a buffer option value
|
||||
@ -569,7 +569,7 @@ Object nvim_buf_get_option(Buffer buffer, String name, Arena *arena, Error *err)
|
||||
return (Object)OBJECT_INIT;
|
||||
}
|
||||
|
||||
return get_option_from(buf, SREQ_BUF, name, err);
|
||||
return get_option_from(buf, kOptReqBuf, name, err);
|
||||
}
|
||||
|
||||
/// Sets a buffer option value. Passing `nil` as value deletes the option (only
|
||||
@ -591,7 +591,7 @@ void nvim_buf_set_option(uint64_t channel_id, Buffer buffer, String name, Object
|
||||
return;
|
||||
}
|
||||
|
||||
set_option_to(channel_id, buf, SREQ_BUF, name, value, err);
|
||||
set_option_to(channel_id, buf, kOptReqBuf, name, value, err);
|
||||
}
|
||||
|
||||
/// Gets a window option value
|
||||
@ -611,7 +611,7 @@ Object nvim_win_get_option(Window window, String name, Arena *arena, Error *err)
|
||||
return (Object)OBJECT_INIT;
|
||||
}
|
||||
|
||||
return get_option_from(win, SREQ_WIN, name, err);
|
||||
return get_option_from(win, kOptReqWin, name, err);
|
||||
}
|
||||
|
||||
/// Sets a window option value. Passing `nil` as value deletes the option (only
|
||||
@ -633,76 +633,56 @@ void nvim_win_set_option(uint64_t channel_id, Window window, String name, Object
|
||||
return;
|
||||
}
|
||||
|
||||
set_option_to(channel_id, win, SREQ_WIN, name, value, err);
|
||||
set_option_to(channel_id, win, kOptReqWin, name, value, err);
|
||||
}
|
||||
|
||||
/// 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)
|
||||
/// @param[in] from Pointer to buffer or window for local option value.
|
||||
/// @param req_scope Requested option scope. See OptReqScope in option.h.
|
||||
/// @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, OptReqScope req_scope, String name, Error *err)
|
||||
{
|
||||
Object rv = OBJECT_INIT;
|
||||
|
||||
VALIDATE_S(name.size > 0, "option name", "<empty>", {
|
||||
return rv;
|
||||
return (Object)OBJECT_INIT;
|
||||
});
|
||||
|
||||
// Return values
|
||||
int64_t numval;
|
||||
char *stringval = NULL;
|
||||
|
||||
int flags = get_option_value_strict(name.data, &numval, &stringval, type, from);
|
||||
VALIDATE_S(flags != 0, "option name", 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) {
|
||||
api_set_error(err, kErrorTypeException, "Failed to get option '%s'", name.data);
|
||||
return rv;
|
||||
}
|
||||
rv.type = kObjectTypeString;
|
||||
rv.data.string.data = stringval;
|
||||
rv.data.string.size = strlen(stringval);
|
||||
} else {
|
||||
api_set_error(err, kErrorTypeException, "Unknown type for option '%s'", name.data);
|
||||
OptVal value = get_option_value_strict(name.data, req_scope, from, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return (Object)OBJECT_INIT;
|
||||
}
|
||||
|
||||
return rv;
|
||||
VALIDATE_S(value.type != kOptValTypeNil, "option name", name.data, {
|
||||
return (Object)OBJECT_INIT;
|
||||
});
|
||||
|
||||
return optval_as_object(value);
|
||||
}
|
||||
|
||||
/// Sets the value of a global or local (buffer, window) option.
|
||||
///
|
||||
/// @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
|
||||
static void set_option_to(uint64_t channel_id, void *to, int type, String name, Object value,
|
||||
Error *err)
|
||||
/// @param[in] to Pointer to buffer or window for local option value.
|
||||
/// @param req_scope Requested option scope. See OptReqScope in option.h.
|
||||
/// @param name The option name.
|
||||
/// @param value New option value.
|
||||
/// @param[out] err Details of an error that may have occurred.
|
||||
static void set_option_to(uint64_t channel_id, void *to, OptReqScope req_scope, String name,
|
||||
Object value, Error *err)
|
||||
{
|
||||
VALIDATE_S(name.size > 0, "option name", "<empty>", {
|
||||
return;
|
||||
});
|
||||
|
||||
int flags = get_option_value_strict(name.data, NULL, NULL, type, to);
|
||||
int flags = get_option_attrs(name.data);
|
||||
VALIDATE_S(flags != 0, "option name", name.data, {
|
||||
return;
|
||||
});
|
||||
|
||||
if (value.type == kObjectTypeNil) {
|
||||
if (type == SREQ_GLOBAL) {
|
||||
if (req_scope == kOptReqGlobal) {
|
||||
api_set_error(err, kErrorTypeException, "Cannot unset option '%s'", name.data);
|
||||
return;
|
||||
} else if (!(flags & SOPT_GLOBAL)) {
|
||||
@ -716,35 +696,23 @@ static void set_option_to(uint64_t channel_id, void *to, int type, String name,
|
||||
}
|
||||
}
|
||||
|
||||
OptVal optval;
|
||||
bool error = false;
|
||||
OptVal optval = object_as_optval(value, &error);
|
||||
|
||||
if (flags & SOPT_BOOL) {
|
||||
VALIDATE(value.type == kObjectTypeBoolean, "Option '%s' value must be Boolean", name.data, {
|
||||
return;
|
||||
});
|
||||
optval = BOOLEAN_OPTVAL(value.data.boolean);
|
||||
} else if (flags & SOPT_NUM) {
|
||||
VALIDATE(value.type == kObjectTypeInteger, "Option '%s' value must be Integer", name.data, {
|
||||
return;
|
||||
});
|
||||
VALIDATE((value.data.integer <= INT_MAX && value.data.integer >= INT_MIN),
|
||||
"Option '%s' value is out of range", name.data, {
|
||||
return;
|
||||
});
|
||||
optval = NUMBER_OPTVAL((OptInt)value.data.integer);
|
||||
} else {
|
||||
VALIDATE(value.type == kObjectTypeString, "Option '%s' value must be String", name.data, {
|
||||
return;
|
||||
});
|
||||
optval = STRING_OPTVAL(value.data.string);
|
||||
}
|
||||
// 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;
|
||||
});
|
||||
|
||||
// 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;
|
||||
const int opt_flags = (req_scope == kOptReqWin && !(flags & SOPT_GLOBAL)) ? 0 :
|
||||
(req_scope == kOptReqGlobal) ? OPT_GLOBAL : OPT_LOCAL;
|
||||
|
||||
WITH_SCRIPT_CONTEXT(channel_id, {
|
||||
set_option_value_for(name.data, optval, opt_flags, type, to, err);
|
||||
set_option_value_for(name.data, optval, opt_flags, req_scope, to, err);
|
||||
});
|
||||
}
|
||||
|
@ -26,8 +26,9 @@
|
||||
# include "api/options.c.generated.h"
|
||||
#endif
|
||||
|
||||
static int validate_option_value_args(Dict(option) *opts, char *name, int *scope, int *opt_type,
|
||||
void **from, char **filetype, Error *err)
|
||||
static int validate_option_value_args(Dict(option) *opts, char *name, int *scope,
|
||||
OptReqScope *req_scope, void **from, char **filetype,
|
||||
Error *err)
|
||||
{
|
||||
#define HAS_KEY_X(d, v) HAS_KEY(d, option, v)
|
||||
if (HAS_KEY_X(opts, scope)) {
|
||||
@ -42,14 +43,14 @@ static int validate_option_value_args(Dict(option) *opts, char *name, int *scope
|
||||
}
|
||||
}
|
||||
|
||||
*opt_type = SREQ_GLOBAL;
|
||||
*req_scope = kOptReqGlobal;
|
||||
|
||||
if (filetype != NULL && HAS_KEY_X(opts, filetype)) {
|
||||
*filetype = opts->filetype.data;
|
||||
}
|
||||
|
||||
if (HAS_KEY_X(opts, win)) {
|
||||
*opt_type = SREQ_WIN;
|
||||
*req_scope = kOptReqWin;
|
||||
*from = find_window_by_handle(opts->win, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return FAIL;
|
||||
@ -58,7 +59,7 @@ static int validate_option_value_args(Dict(option) *opts, char *name, int *scope
|
||||
|
||||
if (HAS_KEY_X(opts, buf)) {
|
||||
*scope = OPT_LOCAL;
|
||||
*opt_type = SREQ_BUF;
|
||||
*req_scope = kOptReqBuf;
|
||||
*from = find_buffer_by_handle(opts->buf, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return FAIL;
|
||||
@ -81,15 +82,15 @@ static int validate_option_value_args(Dict(option) *opts, char *name, int *scope
|
||||
return FAIL;
|
||||
});
|
||||
|
||||
int flags = get_option_value_strict(name, NULL, NULL, 0, NULL);
|
||||
int flags = get_option_attrs(name);
|
||||
if (flags == 0) {
|
||||
// hidden or unknown option
|
||||
api_set_error(err, kErrorTypeValidation, "Unknown option '%s'", name);
|
||||
} else if (*opt_type & (SREQ_BUF | SREQ_WIN)) {
|
||||
} else if (*req_scope == kOptReqBuf || *req_scope == kOptReqWin) {
|
||||
// if 'buf' or 'win' is passed, make sure the option supports it
|
||||
int req_flags = *opt_type & SREQ_BUF ? SOPT_BUF : SOPT_WIN;
|
||||
int req_flags = *req_scope == kOptReqBuf ? SOPT_BUF : SOPT_WIN;
|
||||
if (!(flags & req_flags)) {
|
||||
char *tgt = *opt_type & SREQ_BUF ? "buf" : "win";
|
||||
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 " : "";
|
||||
@ -158,11 +159,11 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
||||
OptVal value = NIL_OPTVAL;
|
||||
|
||||
int scope = 0;
|
||||
int opt_type = SREQ_GLOBAL;
|
||||
OptReqScope req_scope = kOptReqGlobal;
|
||||
void *from = NULL;
|
||||
char *filetype = NULL;
|
||||
|
||||
if (!validate_option_value_args(opts, name.data, &scope, &opt_type, &from, &filetype, err)) {
|
||||
if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, &filetype, err)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
@ -179,7 +180,7 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
||||
}
|
||||
|
||||
bool hidden;
|
||||
value = get_option_value_for(name.data, NULL, scope, &hidden, opt_type, from, err);
|
||||
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
|
||||
@ -222,9 +223,9 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
|
||||
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, name.data, &scope, &opt_type, &to, NULL, err)) {
|
||||
if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &to, NULL, err)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -234,8 +235,8 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
|
||||
// - 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;
|
||||
}
|
||||
@ -245,15 +246,15 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
|
||||
OptVal optval = object_as_optval(value, &error);
|
||||
|
||||
// Handle invalid option value type.
|
||||
if (error) {
|
||||
// Don't use `name` in the error message here, because `name` can be any String.
|
||||
VALIDATE_EXP(false, "value", "Integer/Boolean/String", api_typename(value.type), {
|
||||
return;
|
||||
});
|
||||
}
|
||||
// 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;
|
||||
});
|
||||
|
||||
WITH_SCRIPT_CONTEXT(channel_id, {
|
||||
set_option_value_for(name.data, optval, scope, opt_type, to, err);
|
||||
set_option_value_for(name.data, optval, scope, req_scope, to, err);
|
||||
});
|
||||
}
|
||||
|
||||
@ -308,30 +309,31 @@ Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err)
|
||||
FUNC_API_SINCE(11)
|
||||
{
|
||||
int scope = 0;
|
||||
int opt_type = SREQ_GLOBAL;
|
||||
OptReqScope req_scope = kOptReqGlobal;
|
||||
void *from = NULL;
|
||||
if (!validate_option_value_args(opts, name.data, &scope, &opt_type, &from, NULL, err)) {
|
||||
if (!validate_option_value_args(opts, name.data, &scope, &req_scope, &from, NULL, err)) {
|
||||
return (Dictionary)ARRAY_DICT_INIT;
|
||||
}
|
||||
|
||||
buf_T *buf = (opt_type == SREQ_BUF) ? (buf_T *)from : curbuf;
|
||||
win_T *win = (opt_type == SREQ_WIN) ? (win_T *)from : curwin;
|
||||
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);
|
||||
}
|
||||
|
||||
/// 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[in] opt_type Option type. See SREQ_* in option_defs.h.
|
||||
/// @param[in] from Target buffer/window.
|
||||
/// @param[out] err Error message, if any.
|
||||
/// @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.
|
||||
///
|
||||
/// @return true if context was switched, false otherwise.
|
||||
static bool switch_option_context(void *const ctx, int opt_type, void *const from, Error *err)
|
||||
static bool switch_option_context(void *const ctx, OptReqScope req_scope, void *const from,
|
||||
Error *err)
|
||||
{
|
||||
switch (opt_type) {
|
||||
case SREQ_WIN: {
|
||||
switch (req_scope) {
|
||||
case kOptReqWin: {
|
||||
win_T *const win = (win_T *)from;
|
||||
switchwin_T *const switchwin = (switchwin_T *)ctx;
|
||||
|
||||
@ -351,7 +353,7 @@ static bool switch_option_context(void *const ctx, int opt_type, void *const fro
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case SREQ_BUF: {
|
||||
case kOptReqBuf: {
|
||||
buf_T *const buf = (buf_T *)from;
|
||||
aco_save_T *const aco = (aco_save_T *)ctx;
|
||||
|
||||
@ -361,51 +363,177 @@ static bool switch_option_context(void *const ctx, int opt_type, void *const fro
|
||||
aucmd_prepbuf(aco, buf);
|
||||
return true;
|
||||
}
|
||||
case SREQ_GLOBAL:
|
||||
case kOptReqGlobal:
|
||||
return false;
|
||||
default:
|
||||
abort(); // This should never happen.
|
||||
}
|
||||
UNREACHABLE;
|
||||
}
|
||||
|
||||
/// Restore context after getting/setting option for window/buffer. See switch_option_context() for
|
||||
/// params.
|
||||
static void restore_option_context(void *const ctx, const int opt_type)
|
||||
static void restore_option_context(void *const ctx, OptReqScope req_scope)
|
||||
{
|
||||
switch (opt_type) {
|
||||
case SREQ_WIN:
|
||||
switch (req_scope) {
|
||||
case kOptReqWin:
|
||||
restore_win_noblock((switchwin_T *)ctx, true);
|
||||
break;
|
||||
case SREQ_BUF:
|
||||
case kOptReqBuf:
|
||||
aucmd_restbuf((aco_save_T *)ctx);
|
||||
break;
|
||||
case SREQ_GLOBAL:
|
||||
case kOptReqGlobal:
|
||||
break;
|
||||
default:
|
||||
abort(); // This should never happen.
|
||||
}
|
||||
}
|
||||
|
||||
/// Get attributes for an option.
|
||||
///
|
||||
/// @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)
|
||||
{
|
||||
int opt_idx = findoption(name);
|
||||
|
||||
if (opt_idx < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
vimoption_T *opt = get_option(opt_idx);
|
||||
|
||||
if (is_tty_option(opt->fullname)) {
|
||||
return SOPT_STRING | SOPT_GLOBAL;
|
||||
}
|
||||
|
||||
// Hidden option
|
||||
if (opt->var == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int attrs = 0;
|
||||
|
||||
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 (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 attrs;
|
||||
}
|
||||
|
||||
/// Check if option has a value in the requested scope.
|
||||
///
|
||||
/// @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)
|
||||
{
|
||||
int opt_idx = findoption(name);
|
||||
|
||||
if (opt_idx < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vimoption_T *opt = get_option(opt_idx);
|
||||
|
||||
// Hidden option.
|
||||
if (opt->var == NULL) {
|
||||
return false;
|
||||
}
|
||||
// TTY option.
|
||||
if (is_tty_option(opt->fullname)) {
|
||||
return req_scope == kOptReqGlobal;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// 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 (!option_has_scope(name, req_scope)) {
|
||||
return retv;
|
||||
}
|
||||
if (get_tty_option(name, &retv.data.string.data)) {
|
||||
retv.type = kOptValTypeString;
|
||||
return retv;
|
||||
}
|
||||
|
||||
int opt_idx = findoption(name);
|
||||
assert(opt_idx != 0); // option_has_scope() already verifies if option name is valid.
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
char *varp = get_varp_scope(opt, req_scope == kOptReqGlobal ? OPT_GLOBAL : OPT_LOCAL);
|
||||
retv = optval_from_varp(opt_idx, varp);
|
||||
|
||||
if (switched) {
|
||||
restore_option_context(ctx, req_scope);
|
||||
}
|
||||
|
||||
return retv;
|
||||
}
|
||||
|
||||
/// 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[in] opt_type Option type. See SREQ_* in option_defs.h.
|
||||
/// @param[in] from Target buffer/window.
|
||||
/// @param[out] err Error message, if any.
|
||||
/// @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 int opt_type, void *const from, Error *err)
|
||||
const OptReqScope req_scope, void *const from, Error *err)
|
||||
{
|
||||
switchwin_T switchwin;
|
||||
aco_save_T aco;
|
||||
void *ctx = opt_type == SREQ_WIN ? (void *)&switchwin
|
||||
: (opt_type == SREQ_BUF ? (void *)&aco : NULL);
|
||||
void *ctx = req_scope == kOptReqWin ? (void *)&switchwin
|
||||
: (req_scope == kOptReqBuf ? (void *)&aco : NULL);
|
||||
|
||||
bool switched = switch_option_context(ctx, opt_type, from, err);
|
||||
bool switched = switch_option_context(ctx, req_scope, from, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return NIL_OPTVAL;
|
||||
}
|
||||
@ -413,7 +541,7 @@ OptVal get_option_value_for(const char *const name, uint32_t *flagsp, int scope,
|
||||
OptVal retv = get_option_value(name, flagsp, scope, hidden);
|
||||
|
||||
if (switched) {
|
||||
restore_option_context(ctx, opt_type);
|
||||
restore_option_context(ctx, req_scope);
|
||||
}
|
||||
|
||||
return retv;
|
||||
@ -421,21 +549,21 @@ OptVal get_option_value_for(const char *const name, uint32_t *flagsp, int scope,
|
||||
|
||||
/// 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[in] opt_type Option type. See SREQ_* in option_defs.h.
|
||||
/// @param[in] from Target buffer/window.
|
||||
/// @param[out] err Error message, if any.
|
||||
/// @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 int opt_type, void *const from, Error *err)
|
||||
const OptReqScope req_scope, void *const from, Error *err)
|
||||
{
|
||||
switchwin_T switchwin;
|
||||
aco_save_T aco;
|
||||
void *ctx = opt_type == SREQ_WIN ? (void *)&switchwin
|
||||
: (opt_type == SREQ_BUF ? (void *)&aco : NULL);
|
||||
void *ctx = req_scope == kOptReqWin ? (void *)&switchwin
|
||||
: (req_scope == kOptReqBuf ? (void *)&aco : NULL);
|
||||
|
||||
bool switched = switch_option_context(ctx, opt_type, from, err);
|
||||
bool switched = switch_option_context(ctx, req_scope, from, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return;
|
||||
}
|
||||
@ -446,6 +574,6 @@ void set_option_value_for(const char *const name, OptVal value, const int opt_fl
|
||||
}
|
||||
|
||||
if (switched) {
|
||||
restore_option_context(ctx, opt_type);
|
||||
restore_option_context(ctx, req_scope);
|
||||
}
|
||||
}
|
||||
|
@ -1143,7 +1143,7 @@ static OptVal get_option_newval(int opt_idx, int opt_flags, set_prefix_T prefix,
|
||||
assert(varp != NULL);
|
||||
|
||||
char *arg = *argp;
|
||||
OptVal oldval = optval_from_varp_flags(varp, flags);
|
||||
OptVal oldval = optval_from_varp(opt_idx, varp);
|
||||
OptVal newval = NIL_OPTVAL;
|
||||
|
||||
switch (oldval.type) {
|
||||
@ -3232,8 +3232,24 @@ static bool optval_match_type(OptVal o, int opt_idx)
|
||||
}
|
||||
|
||||
/// Create OptVal from var pointer.
|
||||
static OptVal optval_from_varp(OptValType type, void *varp)
|
||||
///
|
||||
/// @param opt_idx Option index in options[] table.
|
||||
/// @param[out] varp Pointer to option variable.
|
||||
OptVal optval_from_varp(int opt_idx, void *varp)
|
||||
{
|
||||
uint32_t flags = options[opt_idx].flags;
|
||||
|
||||
OptValType type = kOptValTypeNil;
|
||||
if (flags & P_BOOL) {
|
||||
type = kOptValTypeBoolean;
|
||||
} else if (flags & P_NUM) {
|
||||
type = kOptValTypeNumber;
|
||||
} else if (flags & P_STRING) {
|
||||
type = kOptValTypeString;
|
||||
} else {
|
||||
abort();
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case kOptValTypeNil:
|
||||
return NIL_OPTVAL;
|
||||
@ -3247,24 +3263,6 @@ static OptVal optval_from_varp(OptValType type, void *varp)
|
||||
UNREACHABLE;
|
||||
}
|
||||
|
||||
/// Get option value from var pointer and option flags.
|
||||
static OptVal optval_from_varp_flags(void *varp, const uint32_t flags)
|
||||
{
|
||||
OptValType type = kOptValTypeNil;
|
||||
|
||||
if (flags & P_BOOL) {
|
||||
type = kOptValTypeBoolean;
|
||||
} else if (flags & P_NUM) {
|
||||
type = kOptValTypeNumber;
|
||||
} else if (flags & P_STRING) {
|
||||
type = kOptValTypeString;
|
||||
} else {
|
||||
abort();
|
||||
}
|
||||
|
||||
return optval_from_varp(type, varp);
|
||||
}
|
||||
|
||||
/// Set option var pointer value from Optval.
|
||||
static void set_option_varp(void *varp, OptVal value)
|
||||
{
|
||||
@ -3489,128 +3487,6 @@ OptVal get_option_value(const char *name, uint32_t *flagsp, int scope, bool *hid
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the option attributes and its value. Unlike the above function it
|
||||
// will return either global value or local value of the option depending on
|
||||
// what was requested, but it will never return global value if it was
|
||||
// requested to return local one and vice versa. Neither it will return
|
||||
// buffer-local value if it was requested to return window-local one.
|
||||
//
|
||||
// Pretends that option is absent if it is not present in the requested scope
|
||||
// (i.e. has no global, window-local or buffer-local value depending on
|
||||
// opt_type).
|
||||
//
|
||||
// Returned flags:
|
||||
// 0 hidden or unknown option, also option that does not have requested
|
||||
// type (see SREQ_* in option_defs.h)
|
||||
// see SOPT_* in option_defs.h for other flags
|
||||
//
|
||||
// Possible opt_type values: see SREQ_* in option_defs.h
|
||||
int get_option_value_strict(char *name, int64_t *numval, char **stringval, int opt_type, void *from)
|
||||
{
|
||||
if (get_tty_option(name, stringval)) {
|
||||
return SOPT_STRING | SOPT_GLOBAL;
|
||||
}
|
||||
|
||||
int rv = 0;
|
||||
int opt_idx = findoption(name);
|
||||
if (opt_idx < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
vimoption_T *p = &options[opt_idx];
|
||||
|
||||
// Hidden option
|
||||
if (p->var == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (p->flags & P_BOOL) {
|
||||
rv |= SOPT_BOOL;
|
||||
} else if (p->flags & P_NUM) {
|
||||
rv |= SOPT_NUM;
|
||||
} else if (p->flags & P_STRING) {
|
||||
rv |= SOPT_STRING;
|
||||
}
|
||||
|
||||
if (p->indir == PV_NONE) {
|
||||
if (opt_type == SREQ_GLOBAL) {
|
||||
rv |= SOPT_GLOBAL;
|
||||
} else {
|
||||
return 0; // Did not request global-only option
|
||||
}
|
||||
} else {
|
||||
if (p->indir & PV_BOTH) {
|
||||
rv |= SOPT_GLOBAL;
|
||||
}
|
||||
|
||||
if (p->indir & PV_WIN) {
|
||||
if (opt_type == SREQ_BUF) {
|
||||
return 0; // Requested buffer-local, not window-local option
|
||||
}
|
||||
rv |= SOPT_WIN;
|
||||
} else if (p->indir & PV_BUF) {
|
||||
if (opt_type == SREQ_WIN) {
|
||||
return 0; // Requested window-local, not buffer-local option
|
||||
}
|
||||
rv |= SOPT_BUF;
|
||||
}
|
||||
}
|
||||
|
||||
if (stringval == NULL) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
void *varp = NULL;
|
||||
|
||||
if (opt_type == SREQ_GLOBAL) {
|
||||
if (p->var == VAR_WIN) {
|
||||
return 0;
|
||||
}
|
||||
varp = p->var;
|
||||
} else {
|
||||
if (opt_type == SREQ_BUF) {
|
||||
// Special case: 'modified' is b_changed, but we also want to
|
||||
// consider it set when 'ff' or 'fenc' changed.
|
||||
if (p->indir == PV_MOD) {
|
||||
*numval = bufIsChanged((buf_T *)from);
|
||||
varp = NULL;
|
||||
} else {
|
||||
buf_T *save_curbuf = curbuf;
|
||||
|
||||
// only getting a pointer, no need to use aucmd_prepbuf()
|
||||
curbuf = (buf_T *)from;
|
||||
curwin->w_buffer = curbuf;
|
||||
varp = get_varp_scope(p, OPT_LOCAL);
|
||||
curbuf = save_curbuf;
|
||||
curwin->w_buffer = curbuf;
|
||||
}
|
||||
} else if (opt_type == SREQ_WIN) {
|
||||
win_T *save_curwin = curwin;
|
||||
curwin = (win_T *)from;
|
||||
curbuf = curwin->w_buffer;
|
||||
varp = get_varp_scope(p, OPT_LOCAL);
|
||||
curwin = save_curwin;
|
||||
curbuf = curwin->w_buffer;
|
||||
}
|
||||
|
||||
if (varp == p->var) {
|
||||
return (rv | SOPT_UNSET);
|
||||
}
|
||||
}
|
||||
|
||||
if (varp != NULL) {
|
||||
if (p->flags & P_STRING) {
|
||||
*stringval = *(char **)(varp);
|
||||
} else if (p->flags & P_NUM) {
|
||||
*numval = *(OptInt *)varp;
|
||||
} else {
|
||||
*numval = *(int *)varp;
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/// Return information for option at 'opt_idx'
|
||||
vimoption_T *get_option(int opt_idx)
|
||||
{
|
||||
@ -3649,7 +3525,7 @@ static const char *set_option(const int opt_idx, void *varp, OptVal value, int o
|
||||
return e_secure;
|
||||
}
|
||||
|
||||
OptVal old_value = optval_from_varp(value.type, varp);
|
||||
OptVal old_value = optval_from_varp(opt_idx, varp);
|
||||
OptVal old_global_value = NIL_OPTVAL;
|
||||
OptVal old_local_value = NIL_OPTVAL;
|
||||
|
||||
@ -3659,10 +3535,8 @@ static const char *set_option(const int opt_idx, void *varp, OptVal value, int o
|
||||
// TODO(famiu): This needs to be changed to use the current type of the old value instead of
|
||||
// value.type, when multi-type options are added.
|
||||
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
|
||||
old_local_value =
|
||||
optval_from_varp(value.type, get_varp_scope(opt, OPT_LOCAL));
|
||||
old_global_value =
|
||||
optval_from_varp(value.type, get_varp_scope(opt, OPT_GLOBAL));
|
||||
old_local_value = optval_from_varp(opt_idx, get_varp_scope(opt, OPT_LOCAL));
|
||||
old_global_value = optval_from_varp(opt_idx, get_varp_scope(opt, OPT_GLOBAL));
|
||||
}
|
||||
|
||||
if (value.type == kOptValTypeNumber) {
|
||||
@ -3729,7 +3603,7 @@ static const char *set_option(const int opt_idx, void *varp, OptVal value, int o
|
||||
errmsg);
|
||||
|
||||
if (opt->flags & P_UI_OPTION) {
|
||||
OptVal value_copy = optval_copy(optval_from_varp(value.type, varp));
|
||||
OptVal value_copy = optval_copy(optval_from_varp(opt_idx, varp));
|
||||
ui_call_option_set(cstr_as_string(opt->fullname),
|
||||
optval_as_object(value_copy));
|
||||
}
|
||||
|
@ -91,15 +91,14 @@ enum {
|
||||
SOPT_GLOBAL = 0x08, ///< Option has global value
|
||||
SOPT_WIN = 0x10, ///< Option has window-local value
|
||||
SOPT_BUF = 0x20, ///< Option has buffer-local value
|
||||
SOPT_UNSET = 0x40, ///< Option does not have local value set
|
||||
};
|
||||
|
||||
/// Option types for various functions in option.c
|
||||
enum {
|
||||
SREQ_GLOBAL = 0, ///< Request global option value
|
||||
SREQ_WIN = 1, ///< Request window-local option value
|
||||
SREQ_BUF = 2, ///< Request buffer-local option value
|
||||
};
|
||||
/// Requested option scopes for various functions in option.c
|
||||
typedef enum {
|
||||
kOptReqGlobal = 0, ///< Request global option value
|
||||
kOptReqWin = 1, ///< Request window-local option value
|
||||
kOptReqBuf = 2, ///< Request buffer-local option value
|
||||
} OptReqScope;
|
||||
|
||||
// OptVal helper macros.
|
||||
#define NIL_OPTVAL ((OptVal) { .type = kOptValTypeNil })
|
||||
|
@ -1493,7 +1493,7 @@ describe('API', function()
|
||||
pcall_err(nvim, 'set_option_value', 'scrolloff', 1, {scope = 'bogus'}))
|
||||
eq("Invalid 'scope': expected String, got Integer",
|
||||
pcall_err(nvim, 'get_option_value', 'scrolloff', {scope = 42}))
|
||||
eq("Invalid 'value': expected Integer/Boolean/String, got Array",
|
||||
eq("Invalid 'value': expected valid option type, got Array",
|
||||
pcall_err(nvim, 'set_option_value', 'scrolloff', {}, {}))
|
||||
eq("Invalid value for option 'scrolloff': expected Number, got Boolean true",
|
||||
pcall_err(nvim, 'set_option_value', 'scrolloff', true, {}))
|
||||
|
Reference in New Issue
Block a user