refactor(options): remove set_string_option_direct()

Problem: `set_string_option_direct()` contains a separate codepath specifically for setting string options. Not only is that unnecessary code duplication, but it's also limited to only string options.

Solution: Replace `set_string_option_direct()` with `set_option_direct()` which calls `set_option()` under the hood. This reduces code duplication and allows directly setting an option of any type.
This commit is contained in:
Famiu Haque
2024-02-03 12:57:03 +06:00
parent 5aa8c02a9d
commit 2214f9c19d
15 changed files with 93 additions and 135 deletions

View File

@@ -266,113 +266,6 @@ void check_string_option(char **pp)
}
}
/// Set global value for string option when it's a local option.
///
/// @param opt option
/// @param varp pointer to option variable
static void set_string_option_global(vimoption_T *opt, char **varp)
{
char **p;
// the global value is always allocated
if (opt->var == VAR_WIN) {
p = (char **)GLOBAL_WO(varp);
} else {
p = (char **)opt->var;
}
if (opt->indir != PV_NONE && p != varp) {
char *s = xstrdup(*varp);
free_string_option(*p);
*p = s;
}
}
/// Set a string option to a new value (without checking the effect).
/// The string is copied into allocated memory.
/// if ("opt_idx" == kOptInvalid) "name" is used, otherwise "opt_idx" is used.
/// When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
/// "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
/// "set_sid".
///
/// @param opt_flags Option flags.
///
/// TODO(famiu): Remove this and its win/buf variants.
void set_string_option_direct(OptIndex opt_idx, const char *val, int opt_flags, scid_T set_sid)
{
vimoption_T *opt = get_option(opt_idx);
if (opt->var == NULL) { // can't set hidden option
return;
}
assert(opt->var != &p_shada);
bool both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
char *s = xstrdup(val);
char **varp = (char **)get_varp_scope(opt, both ? OPT_LOCAL : opt_flags);
if (opt->flags & P_ALLOCED) {
free_string_option(*varp);
}
*varp = s;
// For buffer/window local option may also set the global value.
if (both) {
set_string_option_global(opt, varp);
}
opt->flags |= P_ALLOCED;
// When setting both values of a global option with a local value,
// make the local value empty, so that the global value is used.
if ((opt->indir & PV_BOTH) && both) {
free_string_option(*varp);
*varp = empty_string_option;
}
if (set_sid != SID_NONE) {
sctx_T script_ctx;
if (set_sid == 0) {
script_ctx = current_sctx;
} else {
script_ctx.sc_sid = set_sid;
script_ctx.sc_seq = 0;
script_ctx.sc_lnum = 0;
}
set_option_sctx(opt_idx, opt_flags, script_ctx);
}
}
/// Like set_string_option_direct(), but for a window-local option in "wp".
/// Blocks autocommands to avoid the old curwin becoming invalid.
void set_string_option_direct_in_win(win_T *wp, OptIndex opt_idx, const char *val, int opt_flags,
int set_sid)
{
win_T *save_curwin = curwin;
block_autocmds();
curwin = wp;
curbuf = curwin->w_buffer;
set_string_option_direct(opt_idx, val, opt_flags, set_sid);
curwin = save_curwin;
curbuf = curwin->w_buffer;
unblock_autocmds();
}
/// Like set_string_option_direct(), but for a buffer-local option in "buf".
/// Blocks autocommands to avoid the old curwin becoming invalid.
void set_string_option_direct_in_buf(buf_T *buf, OptIndex opt_idx, const char *val, int opt_flags,
int set_sid)
{
buf_T *save_curbuf = curbuf;
block_autocmds();
curbuf = buf;
set_string_option_direct(opt_idx, val, opt_flags, set_sid);
curbuf = save_curbuf;
unblock_autocmds();
}
/// Return true if "val" is a valid 'filetype' name.
/// Also used for 'syntax' and 'keymap'.
static bool valid_filetype(const char *val)