mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 11:58:17 +00:00
refactor(option): further align set_string_option with do_set_option_string
This commit is contained in:
@@ -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,
|
set_op_T op_arg, uint32_t flags, void *varp_arg, char *errbuf,
|
||||||
size_t errbuflen, bool *value_checked, const char **errmsg)
|
size_t errbuflen, bool *value_checked, const char **errmsg)
|
||||||
{
|
{
|
||||||
|
vimoption_T *opt = get_option(opt_idx);
|
||||||
|
|
||||||
set_op_T op = op_arg;
|
set_op_T op = op_arg;
|
||||||
void *varp = varp_arg;
|
void *varp = varp_arg;
|
||||||
char *origval_l = NULL;
|
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
|
// with a local value the local value will be
|
||||||
// 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)options[opt_idx].indir & PV_BOTH)) {
|
&& ((int)opt->indir & PV_BOTH)) {
|
||||||
varp = options[opt_idx].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 *oldval = *(char **)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(&(options[opt_idx]), OPT_LOCAL);
|
origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
|
||||||
origval_g = *(char **)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
|
origval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
|
||||||
|
|
||||||
// A global-local string option might have an empty option as value to
|
// A global-local string option might have an empty option as value to
|
||||||
// indicate that the global value should be used.
|
// 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;
|
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;
|
char *origval;
|
||||||
// When setting the local value of a global option, the old value may be
|
// When setting the local value of a global option, the old value may be
|
||||||
// the global value.
|
// the global value.
|
||||||
if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) {
|
if (((int)opt->indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) {
|
||||||
origval = *(char **)get_varp(&options[opt_idx]);
|
origval = *(char **)get_varp(opt);
|
||||||
} else {
|
} else {
|
||||||
origval = oldval;
|
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);
|
char *newval = stropt_get_newval(nextchar, opt_idx, argp, varp, origval, &op, flags);
|
||||||
|
|
||||||
// Set the new value.
|
// Set the new value.
|
||||||
*(char **)(varp) = newval;
|
*(char **)(varp) = newval != NULL ? newval : empty_option;
|
||||||
if (newval == NULL) {
|
|
||||||
*(char **)(varp) = empty_option;
|
|
||||||
}
|
|
||||||
|
|
||||||
// origval may be freed by did_set_string_option(), make a copy.
|
// origval may be freed by did_set_string_option(), make a copy.
|
||||||
char *saved_origval = (origval != NULL) ? xstrdup(origval) : NULL;
|
char *const saved_origval = (origval != NULL) ? xstrdup(origval) : NULL;
|
||||||
char *saved_origval_l = (origval_l != NULL) ? xstrdup(origval_l) : NULL;
|
char *const 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_g = (origval_g != NULL) ? xstrdup(origval_g) : NULL;
|
||||||
|
|
||||||
// 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 *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);
|
||||||
uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags);
|
const int secure_saved = secure;
|
||||||
const int secure_saved = secure;
|
|
||||||
|
|
||||||
// When an option is set in the sandbox, from a modeline or in 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
|
// mode, then deal with side effects in secure mode. Also when the
|
||||||
// value was set with the P_INSECURE flag and is not completely
|
// value was set with the P_INSECURE flag and is not completely
|
||||||
// replaced.
|
// replaced.
|
||||||
if ((opt_flags & OPT_MODELINE)
|
if ((opt_flags & OPT_MODELINE)
|
||||||
|| sandbox != 0
|
|| sandbox != 0
|
||||||
|| (op != OP_NONE && (*p & P_INSECURE))) {
|
|| (op != OP_NONE && (*p & P_INSECURE))) {
|
||||||
secure = 1;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 (*errmsg == NULL) {
|
||||||
if (!starting) {
|
if (!starting) {
|
||||||
trigger_optionset_string(opt_idx, opt_flags, saved_origval, saved_origval_l,
|
trigger_optionset_string(opt_idx, opt_flags, saved_origval, saved_origval_l,
|
||||||
saved_origval_g, saved_newval);
|
saved_origval_g, saved_newval);
|
||||||
}
|
}
|
||||||
if (options[opt_idx].flags & P_UI_OPTION) {
|
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));
|
CSTR_AS_OBJ(saved_newval));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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
|
= (char **)get_varp_scope(opt, ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
|
||||||
? ((opt->indir & PV_BOTH) ? OPT_GLOBAL : OPT_LOCAL)
|
? ((opt->indir & PV_BOTH) ? OPT_GLOBAL : OPT_LOCAL)
|
||||||
: opt_flags));
|
: 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 *const oldval = *varp;
|
||||||
char *oldval_l = NULL;
|
|
||||||
char *oldval_g = NULL;
|
|
||||||
|
|
||||||
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
|
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) {
|
||||||
oldval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
|
origval_l = *(char **)get_varp_scope(opt, OPT_LOCAL);
|
||||||
oldval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
|
origval_g = *(char **)get_varp_scope(opt, OPT_GLOBAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
*varp = xstrdup(value);
|
*varp = xstrdup(value);
|
||||||
|
|
||||||
char *const saved_oldval = xstrdup(oldval);
|
char *const saved_oldval = xstrdup(oldval);
|
||||||
char *const saved_oldval_l = (oldval_l != NULL) ? xstrdup(oldval_l) : 0;
|
char *const saved_oldval_l = (origval_l != NULL) ? xstrdup(origval_l) : 0;
|
||||||
char *const saved_oldval_g = (oldval_g != NULL) ? xstrdup(oldval_g) : 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);
|
char *const saved_newval = xstrdup(*varp);
|
||||||
|
|
||||||
const char *const errmsg = did_set_string_option(curbuf, curwin, opt_idx, varp, oldval, errbuf,
|
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
|
// call autocommand after handling side effects
|
||||||
if (errmsg == NULL) {
|
if (errmsg == NULL) {
|
||||||
if (!starting) {
|
if (!starting) {
|
||||||
trigger_optionset_string(opt_idx, opt_flags, saved_oldval, saved_oldval_l, saved_oldval_g,
|
trigger_optionset_string(opt_idx, opt_flags, saved_oldval, saved_oldval_l,
|
||||||
saved_newval);
|
saved_oldval_g, saved_newval);
|
||||||
}
|
}
|
||||||
if (opt->flags & P_UI_OPTION) {
|
if (opt->flags & P_UI_OPTION) {
|
||||||
ui_call_option_set(cstr_as_string(opt->fullname),
|
ui_call_option_set(cstr_as_string(opt->fullname),
|
||||||
STRING_OBJ(cstr_as_string(saved_newval)));
|
CSTR_AS_OBJ(saved_newval));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xfree(saved_oldval);
|
xfree(saved_oldval);
|
||||||
|
Reference in New Issue
Block a user