refactor(option): further align set_string_option with do_set_option_string

This commit is contained in:
Lewis Russell
2023-07-13 12:08:43 +01:00
parent 9edd0f077e
commit ec0ca51886
2 changed files with 48 additions and 46 deletions

View File

@@ -1100,6 +1100,8 @@ static void do_set_option_string(int opt_idx, int opt_flags, char **argp, int ne
set_op_T op_arg, uint32_t flags, void *varp_arg, char *errbuf,
size_t errbuflen, bool *value_checked, const char **errmsg)
{
vimoption_T *opt = get_option(opt_idx);
set_op_T op = op_arg;
void *varp = varp_arg;
char *origval_l = NULL;
@@ -1109,20 +1111,20 @@ static void do_set_option_string(int opt_idx, int opt_flags, char **argp, int ne
// with a local value the local value will be
// reset, use the global value here.
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
&& ((int)options[opt_idx].indir & PV_BOTH)) {
varp = options[opt_idx].var;
&& ((int)opt->indir & PV_BOTH)) {
varp = opt->var;
}
// The old value is kept until we are sure that the new value is valid.
char *oldval = *(char **)varp;
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
origval_l = *(char **)get_varp_scope(&(options[opt_idx]), OPT_LOCAL);
origval_g = *(char **)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
origval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
// A global-local string option might have an empty option as value to
// indicate that the global value should be used.
if (((int)options[opt_idx].indir & PV_BOTH) && origval_l == empty_option) {
if (((int)opt->indir & PV_BOTH) && origval_l == empty_option) {
origval_l = origval_g;
}
}
@@ -1130,8 +1132,8 @@ static void do_set_option_string(int opt_idx, int opt_flags, char **argp, int ne
char *origval;
// When setting the local value of a global option, the old value may be
// the global value.
if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) {
origval = *(char **)get_varp(&options[opt_idx]);
if (((int)opt->indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) {
origval = *(char **)get_varp(opt);
} else {
origval = oldval;
}
@@ -1140,51 +1142,46 @@ static void do_set_option_string(int opt_idx, int opt_flags, char **argp, int ne
char *newval = stropt_get_newval(nextchar, opt_idx, argp, varp, origval, &op, flags);
// Set the new value.
*(char **)(varp) = newval;
if (newval == NULL) {
*(char **)(varp) = empty_option;
}
*(char **)(varp) = newval != NULL ? newval : empty_option;
// origval may be freed by did_set_string_option(), make a copy.
char *saved_origval = (origval != NULL) ? xstrdup(origval) : NULL;
char *saved_origval_l = (origval_l != NULL) ? xstrdup(origval_l) : NULL;
char *saved_origval_g = (origval_g != NULL) ? xstrdup(origval_g) : NULL;
char *const saved_origval = (origval != NULL) ? xstrdup(origval) : NULL;
char *const saved_origval_l = (origval_l != NULL) ? xstrdup(origval_l) : NULL;
char *const saved_origval_g = (origval_g != NULL) ? xstrdup(origval_g) : NULL;
// newval (and varp) may become invalid if the buffer is closed by
// autocommands.
char *saved_newval = (newval != NULL) ? xstrdup(newval) : NULL;
char *const saved_newval = (newval != NULL) ? xstrdup(newval) : NULL;
{
uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags);
const int secure_saved = secure;
uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags);
const int secure_saved = secure;
// When an option is set in the sandbox, from a modeline or in secure
// mode, then deal with side effects in secure mode. Also when the
// value was set with the P_INSECURE flag and is not completely
// replaced.
if ((opt_flags & OPT_MODELINE)
|| sandbox != 0
|| (op != OP_NONE && (*p & P_INSECURE))) {
secure = 1;
}
// Handle side effects, and set the global value for ":set" on local
// options. Note: when setting 'syntax' or 'filetype' autocommands may
// be triggered that can cause havoc.
*errmsg = did_set_string_option(curbuf, curwin, opt_idx, (char **)varp, oldval,
errbuf, errbuflen,
opt_flags, value_checked);
secure = secure_saved;
// When an option is set in the sandbox, from a modeline or in secure
// mode, then deal with side effects in secure mode. Also when the
// value was set with the P_INSECURE flag and is not completely
// replaced.
if ((opt_flags & OPT_MODELINE)
|| sandbox != 0
|| (op != OP_NONE && (*p & P_INSECURE))) {
secure = 1;
}
// Handle side effects, and set the global value for ":set" on local
// options. Note: when setting 'syntax' or 'filetype' autocommands may
// be triggered that can cause havoc.
*errmsg = did_set_string_option(curbuf, curwin, opt_idx, (char **)varp, oldval,
errbuf, errbuflen,
opt_flags, value_checked);
secure = secure_saved;
if (*errmsg == NULL) {
if (!starting) {
trigger_optionset_string(opt_idx, opt_flags, saved_origval, saved_origval_l,
saved_origval_g, saved_newval);
}
if (options[opt_idx].flags & P_UI_OPTION) {
ui_call_option_set(cstr_as_string(options[opt_idx].fullname),
ui_call_option_set(cstr_as_string(opt->fullname),
CSTR_AS_OBJ(saved_newval));
}
}

View File

@@ -439,20 +439,25 @@ const char *set_string_option(const int opt_idx, const char *const value, const
= (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_g = NULL;
// The old value is kept until we are sure that the new value is valid.
char *const oldval = *varp;
char *oldval_l = NULL;
char *oldval_g = NULL;
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
oldval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
oldval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
origval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
}
*varp = xstrdup(value);
char *const saved_oldval = xstrdup(oldval);
char *const saved_oldval_l = (oldval_l != NULL) ? xstrdup(oldval_l) : 0;
char *const saved_oldval_g = (oldval_g != NULL) ? xstrdup(oldval_g) : 0;
char *const saved_oldval_l = (origval_l != NULL) ? xstrdup(origval_l) : 0;
char *const saved_oldval_g = (origval_g != NULL) ? xstrdup(origval_g) : 0;
// newval (and varp) may become invalid if the buffer is closed by
// autocommands.
char *const saved_newval = xstrdup(*varp);
const char *const errmsg = did_set_string_option(curbuf, curwin, opt_idx, varp, oldval, errbuf,
@@ -460,12 +465,12 @@ const char *set_string_option(const int opt_idx, const char *const value, const
// call autocommand after handling side effects
if (errmsg == NULL) {
if (!starting) {
trigger_optionset_string(opt_idx, opt_flags, saved_oldval, saved_oldval_l, saved_oldval_g,
saved_newval);
trigger_optionset_string(opt_idx, opt_flags, saved_oldval, saved_oldval_l,
saved_oldval_g, saved_newval);
}
if (opt->flags & P_UI_OPTION) {
ui_call_option_set(cstr_as_string(opt->fullname),
STRING_OBJ(cstr_as_string(saved_newval)));
CSTR_AS_OBJ(saved_newval));
}
}
xfree(saved_oldval);