refactor(option): pass varp to set_string_option

This commit is contained in:
Lewis Russell
2023-07-13 16:46:05 +01:00
parent af3c667ac1
commit 038ac39b8e
2 changed files with 9 additions and 14 deletions

View File

@@ -3828,7 +3828,7 @@ const char *set_option_value(const char *const name, const OptVal value, int opt
break; break;
} }
case kOptValTypeString: { case kOptValTypeString: {
errmsg = set_string_option(opt_idx, v.data.string.data, opt_flags, &value_checked, errbuf, errmsg = set_string_option(opt_idx, varp, v.data.string.data, opt_flags, &value_checked, errbuf,
sizeof(errbuf)); sizeof(errbuf));
break; break;
} }

View File

@@ -429,19 +429,14 @@ void set_string_option_direct_in_buf(buf_T *buf, const char *name, int opt_idx,
/// #OPT_GLOBAL. /// #OPT_GLOBAL.
/// ///
/// @return NULL on success, an untranslated error message on error. /// @return NULL on success, an untranslated error message on error.
const char *set_string_option(const int opt_idx, const char *value, const int opt_flags, const char *set_string_option(const int opt_idx, void *varp_arg, const char *value,
bool *value_checked, char *const errbuf, const size_t errbuflen) const int opt_flags, bool *value_checked, char *const errbuf,
const size_t errbuflen)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_WARN_UNUSED_RESULT
{ {
vimoption_T *opt = get_option(opt_idx); vimoption_T *opt = get_option(opt_idx);
if (value == NULL) { void *varp = (char **)varp_arg;
value = "";
}
char **varp = (char **)get_varp_scope(opt, ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
? ((opt->indir & PV_BOTH) ? OPT_GLOBAL : OPT_LOCAL)
: opt_flags));
char *origval_l = NULL; char *origval_l = NULL;
char *origval_g = NULL; char *origval_g = NULL;
@@ -450,11 +445,11 @@ const char *set_string_option(const int opt_idx, const char *value, const int op
// reset, use the global value here. // reset, use the global value here.
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
&& ((int)opt->indir & PV_BOTH)) { && ((int)opt->indir & PV_BOTH)) {
varp = (char **)opt->var; varp = opt->var;
} }
// The old value is kept until we are sure that the new value is valid. // The old value is kept until we are sure that the new value is valid.
char *const oldval = *varp; char *oldval = *(char **)varp;
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL); origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
@@ -476,7 +471,7 @@ const char *set_string_option(const int opt_idx, const char *value, const int op
origval = oldval; origval = oldval;
} }
*varp = xstrdup(value); *(char **)varp = xstrdup(value != NULL ? value : empty_option);
char *const saved_origval = (origval != NULL) ? xstrdup(origval) : NULL; char *const saved_origval = (origval != NULL) ? xstrdup(origval) : NULL;
char *const saved_oldval_l = (origval_l != NULL) ? xstrdup(origval_l) : 0; char *const saved_oldval_l = (origval_l != NULL) ? xstrdup(origval_l) : 0;
@@ -484,7 +479,7 @@ const char *set_string_option(const int opt_idx, const char *value, const int op
// newval (and varp) may become invalid if the buffer is closed by // newval (and varp) may become invalid if the buffer is closed by
// autocommands. // autocommands.
char *const saved_newval = xstrdup(*varp); char *const saved_newval = xstrdup(*(char **)varp);
const int secure_saved = secure; const int secure_saved = secure;