aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/api/deprecated.c4
-rw-r--r--src/nvim/api/options.c34
-rw-r--r--src/nvim/eval.c4
-rw-r--r--src/nvim/eval/vars.c6
-rw-r--r--src/nvim/generators/gen_options.lua1
-rw-r--r--src/nvim/option.c191
-rw-r--r--src/nvim/optionstr.c8
-rw-r--r--src/nvim/statusline.c17
8 files changed, 133 insertions, 132 deletions
diff --git a/src/nvim/api/deprecated.c b/src/nvim/api/deprecated.c
index 34bf029483..371361c5a1 100644
--- a/src/nvim/api/deprecated.c
+++ b/src/nvim/api/deprecated.c
@@ -669,8 +669,8 @@ static void set_option_to(uint64_t channel_id, void *to, OptReqScope req_scope,
return;
});
- int opt_idx = findoption(name.data);
- VALIDATE_S(opt_idx >= 0, "option name", name.data, {
+ OptIndex opt_idx = findoption(name.data);
+ VALIDATE_S(opt_idx != kOptInvalid, "option name", name.data, {
return;
});
diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c
index 0d18c7871c..54a2fbf385 100644
--- a/src/nvim/api/options.c
+++ b/src/nvim/api/options.c
@@ -23,9 +23,9 @@
# include "api/options.c.generated.h"
#endif
-static int validate_option_value_args(Dict(option) *opts, char *name, int *opt_idx, int *scope,
- OptReqScope *req_scope, void **from, char **filetype,
- Error *err)
+static int validate_option_value_args(Dict(option) *opts, char *name, OptIndex *opt_idxp,
+ 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)) {
@@ -79,8 +79,8 @@ static int validate_option_value_args(Dict(option) *opts, char *name, int *opt_i
return FAIL;
});
- *opt_idx = findoption(name);
- int flags = get_option_attrs(*opt_idx);
+ *opt_idxp = findoption(name);
+ int flags = get_option_attrs(*opt_idxp);
if (flags == 0) {
// hidden or unknown option
api_set_error(err, kErrorTypeValidation, "Unknown option '%s'", name);
@@ -153,7 +153,7 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err)
Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
FUNC_API_SINCE(9)
{
- int opt_idx = 0;
+ OptIndex opt_idx = 0;
int scope = 0;
OptReqScope req_scope = kOptReqGlobal;
void *from = NULL;
@@ -219,7 +219,7 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
Error *err)
FUNC_API_SINCE(9)
{
- int opt_idx = 0;
+ OptIndex opt_idx = 0;
int scope = 0;
OptReqScope req_scope = kOptReqGlobal;
void *to = NULL;
@@ -306,7 +306,7 @@ Dictionary nvim_get_all_options_info(Error *err)
Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err)
FUNC_API_SINCE(11)
{
- int opt_idx = 0;
+ OptIndex opt_idx = 0;
int scope = 0;
OptReqScope req_scope = kOptReqGlobal;
void *from = NULL;
@@ -392,9 +392,9 @@ static void restore_option_context(void *const ctx, OptReqScope req_scope)
/// @return Option attributes.
/// 0 for hidden or unknown option.
/// See SOPT_* in option_defs.h for other flags.
-int get_option_attrs(int opt_idx)
+int get_option_attrs(OptIndex opt_idx)
{
- if (opt_idx < 0) {
+ if (opt_idx == kOptInvalid) {
return 0;
}
@@ -425,9 +425,9 @@ int get_option_attrs(int opt_idx)
/// @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(int opt_idx, OptReqScope req_scope)
+static bool option_has_scope(OptIndex opt_idx, OptReqScope req_scope)
{
- if (opt_idx < 0) {
+ if (opt_idx == kOptInvalid) {
return false;
}
@@ -463,9 +463,9 @@ static bool option_has_scope(int opt_idx, OptReqScope req_scope)
/// @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(int opt_idx, OptReqScope req_scope, void *from, Error *err)
+OptVal get_option_value_strict(OptIndex opt_idx, OptReqScope req_scope, void *from, Error *err)
{
- if (opt_idx < 0 || !option_has_scope(opt_idx, req_scope)) {
+ if (opt_idx == kOptInvalid || !option_has_scope(opt_idx, req_scope)) {
return NIL_OPTVAL;
}
@@ -500,8 +500,8 @@ OptVal get_option_value_strict(int opt_idx, OptReqScope req_scope, void *from, E
/// @param[out] err Error message, if any.
///
/// @return Option value. Must be freed by caller.
-OptVal get_option_value_for(int opt_idx, int scope, const OptReqScope req_scope, void *const from,
- Error *err)
+OptVal get_option_value_for(OptIndex opt_idx, int scope, const OptReqScope req_scope,
+ void *const from, Error *err)
{
switchwin_T switchwin;
aco_save_T aco;
@@ -531,7 +531,7 @@ OptVal get_option_value_for(int opt_idx, int scope, const OptReqScope req_scope,
/// @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 *name, int opt_idx, OptVal value, const int opt_flags,
+void set_option_value_for(const char *name, OptIndex opt_idx, OptVal value, const int opt_flags,
const OptReqScope req_scope, void *const from, Error *err)
FUNC_ATTR_NONNULL_ARG(1)
{
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 1c30075991..1fdaa95076 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -3788,9 +3788,9 @@ int eval_option(const char **const arg, typval_T *const rettv, const bool evalua
*option_end = NUL;
bool is_tty_opt = is_tty_option(*arg);
- int opt_idx = is_tty_opt ? -1 : findoption(*arg);
+ OptIndex opt_idx = is_tty_opt ? kOptInvalid : findoption(*arg);
- if (opt_idx < 0 && !is_tty_opt) {
+ if (opt_idx == kOptInvalid && !is_tty_opt) {
// Only give error if result is going to be used.
if (rettv != NULL) {
semsg(_("E113: Unknown option: %s"), *arg);
diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c
index 9b7a61f969..73c8ae1191 100644
--- a/src/nvim/eval/vars.c
+++ b/src/nvim/eval/vars.c
@@ -774,7 +774,7 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const,
*p = NUL;
bool is_tty_opt = is_tty_option(arg);
- int opt_idx = is_tty_opt ? -1 : findoption(arg);
+ OptIndex opt_idx = is_tty_opt ? kOptInvalid : findoption(arg);
uint32_t opt_p_flags = get_option_flags(opt_idx);
bool hidden = is_option_hidden(opt_idx);
OptVal curval = is_tty_opt ? get_tty_option(arg) : get_option_value(opt_idx, scope);
@@ -1940,8 +1940,8 @@ typval_T optval_as_tv(OptVal value)
/// Set option "varname" to the value of "varp" for the current buffer/window.
static void set_option_from_tv(const char *varname, typval_T *varp)
{
- int opt_idx = findoption(varname);
- if (opt_idx < 0) {
+ OptIndex opt_idx = findoption(varname);
+ if (opt_idx == kOptInvalid) {
semsg(_(e_unknown_option2), varname);
return;
}
diff --git a/src/nvim/generators/gen_options.lua b/src/nvim/generators/gen_options.lua
index 2b17add7ba..7c5fc08129 100644
--- a/src/nvim/generators/gen_options.lua
+++ b/src/nvim/generators/gen_options.lua
@@ -241,6 +241,7 @@ end
opt_fd = assert(io.open(options_enum_file, 'w'))
w('typedef enum {')
+w(' kOptInvalid = -1,')
for i, o in ipairs(options.options) do
w((' kOpt%s = %u,'):format(title_case(o.full_name), i - 1))
end
diff --git a/src/nvim/option.c b/src/nvim/option.c
index 71d9a2800c..cf469d6e5e 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -157,12 +157,12 @@ typedef enum {
#endif
static int p_bin_dep_opts[] = {
- kOptTextwidth, kOptWrapmargin, kOptModeline, kOptExpandtab, -1
+ kOptTextwidth, kOptWrapmargin, kOptModeline, kOptExpandtab, kOptInvalid
};
static int p_paste_dep_opts[] = {
- kOptAutoindent, kOptExpandtab, kOptRuler, kOptShowmatch, kOptSmarttab,
- kOptSofttabstop, kOptTextwidth, kOptWrapmargin, kOptRevins, kOptVarsofttabstop, -1
+ kOptAutoindent, kOptExpandtab, kOptRuler, kOptShowmatch, kOptSmarttab, kOptSofttabstop,
+ kOptTextwidth, kOptWrapmargin, kOptRevins, kOptVarsofttabstop, kOptInvalid
};
void set_init_tablocal(void)
@@ -199,7 +199,7 @@ static void set_init_default_backupskip(void)
static char *(names[3]) = { "TMPDIR", "TEMP", "TMP" };
#endif
garray_T ga;
- int opt_idx = kOptBackupskip;
+ OptIndex opt_idx = kOptBackupskip;
ga_init(&ga, 1, 100);
for (size_t n = 0; n < ARRAY_SIZE(names); n++) {
@@ -269,8 +269,8 @@ static void set_init_default_cdpath(void)
}
}
buf[j] = NUL;
- int opt_idx = kOptCdpath;
- if (opt_idx >= 0) {
+ OptIndex opt_idx = kOptCdpath;
+ if (opt_idx != kOptInvalid) {
options[opt_idx].def_val = buf;
options[opt_idx].flags |= P_DEF_ALLOCED;
} else {
@@ -288,7 +288,7 @@ static void set_init_default_cdpath(void)
/// default.
static void set_init_expand_env(void)
{
- for (int opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
+ for (OptIndex opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
vimoption_T *opt = &options[opt_idx];
if (opt->flags & P_NO_DEF_EXP) {
continue;
@@ -420,7 +420,7 @@ void set_init_1(bool clean_arg)
/// This does not take care of side effects!
///
/// @param opt_flags OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
-static void set_option_default(const int opt_idx, int opt_flags)
+static void set_option_default(const OptIndex opt_idx, int opt_flags)
{
int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
@@ -507,10 +507,10 @@ static void set_options_default(int opt_flags)
/// @param opt_idx Option index in options[] table.
/// @param val The value of the option.
/// @param allocated If true, do not copy default as it was already allocated.
-static void set_string_default(int opt_idx, char *val, bool allocated)
+static void set_string_default(OptIndex opt_idx, char *val, bool allocated)
FUNC_ATTR_NONNULL_ALL
{
- if (opt_idx < 0) {
+ if (opt_idx == kOptInvalid) {
return;
}
@@ -556,9 +556,9 @@ static char *find_dup_item(char *origval, const char *newval, uint32_t flags)
/// Set the Vi-default value of a number option.
/// Used for 'lines' and 'columns'.
-void set_number_default(int opt_idx, OptInt val)
+void set_number_default(OptIndex opt_idx, OptInt val)
{
- if (opt_idx >= 0) {
+ if (opt_idx != kOptInvalid) {
options[opt_idx].def_val = (void *)(intptr_t)val;
}
}
@@ -744,7 +744,7 @@ void ex_set(exarg_T *eap)
}
/// Get the default value for a string option.
-static char *stropt_get_default_val(int opt_idx, uint64_t flags)
+static char *stropt_get_default_val(OptIndex opt_idx, uint64_t flags)
{
char *newval = options[opt_idx].def_val;
// expand environment variables and ~ since the default value was
@@ -813,7 +813,7 @@ static char *stropt_copy_value(char *origval, char **argp, set_op_T op,
}
/// Expand environment variables and ~ in string option value 'newval'.
-static char *stropt_expand_envvar(int opt_idx, char *origval, char *newval, set_op_T op)
+static char *stropt_expand_envvar(OptIndex opt_idx, char *origval, char *newval, set_op_T op)
{
char *s = option_expand(opt_idx, newval);
if (s == NULL) {
@@ -913,8 +913,8 @@ static void stropt_remove_dupflags(char *newval, uint32_t flags)
/// set {opt}<
/// set {opt}={val}
/// set {opt}:{val}
-static char *stropt_get_newval(int nextchar, int opt_idx, char **argp, void *varp, char *origval,
- set_op_T *op_arg, uint32_t flags)
+static char *stropt_get_newval(int nextchar, OptIndex opt_idx, char **argp, void *varp,
+ char *origval, set_op_T *op_arg, uint32_t flags)
{
char *arg = *argp;
set_op_T op = *op_arg;
@@ -1020,15 +1020,15 @@ static set_prefix_T get_option_prefix(char **argp)
/// @param[out] keyp
/// @param[out] len Length of option name
/// @return FAIL if an error is detected, OK otherwise
-static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp)
+static int parse_option_name(char *arg, int *keyp, int *lenp, OptIndex *opt_idxp)
{
// find end of name
int key = 0;
int len;
- int opt_idx;
+ OptIndex opt_idx;
if (*arg == '<') {
- opt_idx = -1;
+ opt_idx = kOptInvalid;
// look out for <t_>;>
if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4]) {
len = 5;
@@ -1045,7 +1045,7 @@ static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp)
opt_idx = findoption_len(arg + 1, (size_t)(len - 1));
}
len++;
- if (opt_idx == -1) {
+ if (opt_idx == kOptInvalid) {
key = find_key_option(arg + 1, true);
}
} else {
@@ -1059,7 +1059,7 @@ static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp)
}
}
opt_idx = findoption_len(arg, (size_t)len);
- if (opt_idx == -1) {
+ if (opt_idx == kOptInvalid) {
key = find_key_option(arg, false);
}
}
@@ -1071,7 +1071,7 @@ static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp)
return OK;
}
-static int validate_opt_idx(win_T *win, int opt_idx, int opt_flags, uint32_t flags,
+static int validate_opt_idx(win_T *win, OptIndex opt_idx, int opt_flags, uint32_t flags,
set_prefix_T prefix, const char **errmsg)
{
// Only bools can have a prefix of 'inv' or 'no'
@@ -1083,12 +1083,12 @@ static int validate_opt_idx(win_T *win, int opt_idx, int opt_flags, uint32_t fla
// Skip all options that are not window-local (used when showing
// an already loaded buffer in a window).
if ((opt_flags & OPT_WINONLY)
- && (opt_idx < 0 || options[opt_idx].var != VAR_WIN)) {
+ && (opt_idx == kOptInvalid || options[opt_idx].var != VAR_WIN)) {
return FAIL;
}
// Skip all options that are window-local (used for :vimgrep).
- if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
+ if ((opt_flags & OPT_NOWIN) && opt_idx != kOptInvalid
&& options[opt_idx].var == VAR_WIN) {
return FAIL;
}
@@ -1107,7 +1107,7 @@ static int validate_opt_idx(win_T *win, int opt_idx, int opt_flags, uint32_t fla
// 'foldmethod' becomes "marker" instead of "diff" and that
// "wrap" gets set.
if (win->w_p_diff
- && opt_idx >= 0 // shut up coverity warning
+ && opt_idx != kOptInvalid // shut up coverity warning
&& (options[opt_idx].indir == PV_FDM
|| options[opt_idx].indir == PV_WRAP)) {
return FAIL;
@@ -1124,7 +1124,7 @@ static int validate_opt_idx(win_T *win, int opt_idx, int opt_flags, uint32_t fla
}
/// Get new option value from argp. Allocated OptVal must be freed by caller.
-static OptVal get_option_newval(int opt_idx, int opt_flags, set_prefix_T prefix, char **argp,
+static OptVal get_option_newval(OptIndex opt_idx, int opt_flags, set_prefix_T prefix, char **argp,
int nextchar, set_op_T op, uint32_t flags, void *varp, char *errbuf,
const size_t errbuflen, const char **errmsg)
FUNC_ATTR_WARN_UNUSED_RESULT
@@ -1264,7 +1264,7 @@ static void do_one_set_option(int opt_flags, char **argp, bool *did_show, char *
// find end of name
int key = 0;
int len;
- int opt_idx;
+ OptIndex opt_idx;
if (parse_option_name(arg, &key, &len, &opt_idx) == FAIL) {
*errmsg = e_invarg;
return;
@@ -1285,7 +1285,7 @@ static void do_one_set_option(int opt_flags, char **argp, bool *did_show, char *
uint8_t nextchar = (uint8_t)arg[len]; // next non-white char after option name
- if (opt_idx == -1 && key == 0) { // found a mismatch: skip
+ if (opt_idx == kOptInvalid && key == 0) { // found a mismatch: skip
*errmsg = e_unknown_option;
return;
}
@@ -1293,7 +1293,7 @@ static void do_one_set_option(int opt_flags, char **argp, bool *did_show, char *
uint32_t flags; // flags for current option
void *varp = NULL; // pointer to variable for current option
- if (opt_idx >= 0) {
+ if (opt_idx != kOptInvalid) {
if (options[opt_idx].var == NULL) { // hidden option: skip
// Only give an error message when requesting the value of
// a hidden option, ignore setting it.
@@ -1346,7 +1346,7 @@ static void do_one_set_option(int opt_flags, char **argp, bool *did_show, char *
gotocmdline(true); // cursor at status line
*did_show = true; // remember that we did a line
}
- if (opt_idx >= 0) {
+ if (opt_idx != kOptInvalid) {
showoneopt(&options[opt_idx], opt_flags);
if (p_verbose > 0) {
// Mention where the option was last set.
@@ -1614,7 +1614,7 @@ char *find_shada_parameter(int type)
/// These string options cannot be indirect!
/// If "val" is NULL expand the current value of the option.
/// Return pointer to NameBuff, or NULL when not expanded.
-static char *option_expand(int opt_idx, char *val)
+static char *option_expand(OptIndex opt_idx, char *val)
{
// if option doesn't need expansion nothing to do
if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL) {
@@ -1689,7 +1689,7 @@ static void didset_options2(void)
/// Check for string options that are NULL (normally only termcap options).
void check_options(void)
{
- for (int opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++) {
+ for (OptIndex opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++) {
if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL) {
check_string_option((char **)get_varp(&(options[opt_idx])));
}
@@ -1703,9 +1703,9 @@ void check_options(void)
/// @param opt_flags Option flags.
///
/// @return True if option was set from a modeline or in secure mode, false if it wasn't.
-int was_set_insecurely(win_T *const wp, int opt_idx, int opt_flags)
+int was_set_insecurely(win_T *const wp, OptIndex opt_idx, int opt_flags)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
uint32_t *flagp = insecure_flag(wp, opt_idx, opt_flags);
return (*flagp & P_INSECURE) != 0;
@@ -1715,7 +1715,7 @@ int was_set_insecurely(win_T *const wp, int opt_idx, int opt_flags)
/// "opt_idx". For some local options a local flags field is used.
/// NOTE: Caller must make sure that "wp" is set to the window from which
/// the option is used.
-uint32_t *insecure_flag(win_T *const wp, int opt_idx, int opt_flags)
+uint32_t *insecure_flag(win_T *const wp, OptIndex opt_idx, int opt_flags)
{
if (opt_flags & OPT_LOCAL) {
assert(wp != NULL);
@@ -1819,15 +1819,15 @@ bool parse_winhl_opt(win_T *wp)
}
/// Get the script context of global option at index opt_idx.
-sctx_T *get_option_sctx(int opt_idx)
+sctx_T *get_option_sctx(OptIndex opt_idx)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
return &options[opt_idx].last_set.script_ctx;
}
/// Set the script_ctx for an option, taking care of setting the buffer- or
/// window-local value.
-void set_option_sctx(int opt_idx, int opt_flags, sctx_T script_ctx)
+void set_option_sctx(OptIndex opt_idx, int opt_flags, sctx_T script_ctx)
{
int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
int indir = (int)options[opt_idx].indir;
@@ -1861,7 +1861,7 @@ void set_option_sctx(int opt_idx, int opt_flags, sctx_T script_ctx)
}
/// Apply the OptionSet autocommand.
-static void apply_optionset_autocmd(int opt_idx, int opt_flags, OptVal oldval, OptVal oldval_g,
+static void apply_optionset_autocmd(OptIndex opt_idx, int opt_flags, OptVal oldval, OptVal oldval_g,
OptVal oldval_l, OptVal newval, const char *errmsg)
{
// Don't do this while starting up, failure or recursively.
@@ -3013,8 +3013,8 @@ void check_redraw(uint32_t flags)
/// @param[in] arg Option to find index for.
/// @param[in] len Length of the option.
///
-/// @return Index of the option or -1 if option was not found.
-int findoption_len(const char *const arg, const size_t len)
+/// @return Index of the option or kOptInvalid if option was not found.
+OptIndex findoption_len(const char *const arg, const size_t len)
{
const char *s;
static int quick_tab[27] = { 0, 0 }; // quick access table
@@ -3038,10 +3038,10 @@ int findoption_len(const char *const arg, const size_t len)
// Check for name starting with an illegal character.
if (len == 0 || arg[0] < 'a' || arg[0] > 'z') {
- return -1;
+ return kOptInvalid;
}
- int opt_idx;
+ OptIndex opt_idx;
const bool is_term_opt = (len > 2 && arg[0] == 't' && arg[1] == '_');
if (is_term_opt) {
opt_idx = quick_tab[26];
@@ -3069,7 +3069,7 @@ int findoption_len(const char *const arg, const size_t len)
}
}
if (s == NULL) {
- opt_idx = -1;
+ opt_idx = kOptInvalid;
} else {
// Nvim: handle option aliases.
if (strncmp(options[opt_idx].fullname, "viminfo", 7) == 0) {
@@ -3143,8 +3143,8 @@ bool set_tty_option(const char *name, char *value)
///
/// @param[in] arg Option name.
///
-/// @return Option index or -1 if option was not found.
-int findoption(const char *const arg)
+/// @return Option index or kOptInvalid if option was not found.
+OptIndex findoption(const char *const arg)
FUNC_ATTR_NONNULL_ALL
{
return findoption_len(arg, strlen(arg));
@@ -3204,9 +3204,9 @@ bool optval_equal(OptVal o1, OptVal o2)
/// Match type of OptVal with the type of the target option. Returns true if the types match and
/// false otherwise.
-static bool optval_match_type(OptVal o, int opt_idx)
+static bool optval_match_type(OptVal o, OptIndex opt_idx)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
uint32_t flags = options[opt_idx].flags;
switch (o.type) {
@@ -3226,7 +3226,7 @@ static bool optval_match_type(OptVal o, int opt_idx)
///
/// @param opt_idx Option index in options[] table.
/// @param[out] varp Pointer to option variable.
-OptVal optval_from_varp(int opt_idx, void *varp)
+OptVal optval_from_varp(OptIndex opt_idx, void *varp)
{
// Special case: 'modified' is b_changed, but we also want to consider it set when 'ff' or 'fenc'
// changed.
@@ -3266,7 +3266,7 @@ OptVal optval_from_varp(int opt_idx, void *varp)
/// @param[out] varp Pointer to option variable.
/// @param[in] value New option value.
/// @param free_oldval Free old value.
-static void set_option_varp(int opt_idx, void *varp, OptVal value, bool free_oldval)
+static void set_option_varp(OptIndex opt_idx, void *varp, OptVal value, bool free_oldval)
FUNC_ATTR_NONNULL_ARG(2)
{
assert(optval_match_type(value, opt_idx));
@@ -3362,7 +3362,7 @@ OptVal object_as_optval(Object o, bool *error)
/// @param[in] varp Pointer to option variable.
///
/// @return [allocated] Option value equal to the unset value for the option.
-static OptVal optval_unset_local(int opt_idx, void *varp)
+static OptVal optval_unset_local(OptIndex opt_idx, void *varp)
{
vimoption_T *opt = &options[opt_idx];
// For global-local options, use the unset value of the local value.
@@ -3391,7 +3391,7 @@ static OptVal optval_unset_local(int opt_idx, void *varp)
/// For options with a singular type, it returns the name of the type. For options with multiple
/// possible types, it returns a slash separated list of types. For example, if an option can be a
/// number, boolean or string, the function returns "Number/Boolean/String"
-static char *option_get_valid_types(int opt_idx)
+static char *option_get_valid_types(OptIndex opt_idx)
{
uint32_t flags = options[opt_idx].flags;
uint32_t type_count = 0;
@@ -3435,9 +3435,9 @@ static char *option_get_valid_types(int opt_idx)
/// @param opt_idx Option index in options[] table.
///
/// @return True if option is hidden, false otherwise. Returns false if option name is invalid.
-bool is_option_hidden(int opt_idx)
+bool is_option_hidden(OptIndex opt_idx)
{
- return opt_idx < 0 ? false : get_varp(&options[opt_idx]) == NULL;
+ return opt_idx == kOptInvalid ? false : get_varp(&options[opt_idx]) == NULL;
}
/// Get option flags.
@@ -3445,9 +3445,9 @@ bool is_option_hidden(int opt_idx)
/// @param opt_idx Option index in options[] table.
///
/// @return Option flags. Returns 0 for invalid option name.
-uint32_t get_option_flags(int opt_idx)
+uint32_t get_option_flags(OptIndex opt_idx)
{
- return opt_idx < 0 ? 0 : options[opt_idx].flags;
+ return opt_idx == kOptInvalid ? 0 : options[opt_idx].flags;
}
/// Gets the value for an option.
@@ -3456,9 +3456,9 @@ uint32_t get_option_flags(int opt_idx)
/// @param[in] scope Option scope (can be OPT_LOCAL, OPT_GLOBAL or a combination).
///
/// @return [allocated] Option value. Returns NIL_OPTVAL for invalid option index.
-OptVal get_option_value(int opt_idx, int scope)
+OptVal get_option_value(OptIndex opt_idx, int scope)
{
- if (opt_idx < 0) { // option not in the options[] table.
+ if (opt_idx == kOptInvalid) { // option not in the options[] table.
return NIL_OPTVAL;
}
@@ -3469,8 +3469,9 @@ OptVal get_option_value(int opt_idx, int scope)
}
/// Return information for option at 'opt_idx'
-vimoption_T *get_option(int opt_idx)
+vimoption_T *get_option(OptIndex opt_idx)
{
+ assert(opt_idx != kOptInvalid);
return &options[opt_idx];
}
@@ -3496,7 +3497,7 @@ static bool is_option_local_value_unset(vimoption_T *opt, buf_T *buf, win_T *win
/// Handle side-effects of setting an option.
///
-/// @param opt_idx Index in options[] table. Must be >= 0.
+/// @param opt_idx Index in options[] table. Must not be kOptInvalid.
/// @param[in] varp Option variable pointer, cannot be NULL.
/// @param old_value Old option value.
/// @param new_value New option value.
@@ -3507,7 +3508,7 @@ static bool is_option_local_value_unset(vimoption_T *opt, buf_T *buf, win_T *win
/// @param errbuflen Length of error buffer.
///
/// @return NULL on success, an untranslated error message on error.
-static const char *did_set_option(int opt_idx, void *varp, OptVal old_value, OptVal new_value,
+static const char *did_set_option(OptIndex opt_idx, void *varp, OptVal old_value, OptVal new_value,
int opt_flags, bool *value_checked, bool value_replaced,
char *errbuf, size_t errbuflen)
{
@@ -3654,7 +3655,7 @@ static const char *did_set_option(int opt_idx, void *varp, OptVal old_value, Opt
/// Set the value of an option using an OptVal.
///
-/// @param opt_idx Index in options[] table. Must be >= 0.
+/// @param opt_idx Index in options[] table. Must not be kOptInvalid.
/// @param[in] varp Option variable pointer, cannot be NULL.
/// @param value New option value. Might get freed.
/// @param opt_flags Option flags.
@@ -3663,10 +3664,10 @@ static const char *did_set_option(int opt_idx, void *varp, OptVal old_value, Opt
/// @param errbuflen Length of error buffer.
///
/// @return NULL on success, an untranslated error message on error.
-static const char *set_option(const int opt_idx, void *varp, OptVal value, int opt_flags,
+static const char *set_option(const OptIndex opt_idx, void *varp, OptVal value, int opt_flags,
const bool value_replaced, char *errbuf, size_t errbuflen)
{
- assert(opt_idx >= 0 && varp != NULL);
+ assert(opt_idx != kOptInvalid && varp != NULL);
const char *errmsg = NULL;
bool value_checked = false;
@@ -3796,14 +3797,14 @@ err:
/// Set the value of an option.
///
-/// @param opt_idx Index in options[] table. Must be >= 0.
+/// @param opt_idx Index in options[] table. Must not be kOptInvalid.
/// @param[in] value Option value. If NIL_OPTVAL, the option value is cleared.
/// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both).
///
/// @return NULL on success, an untranslated error message on error.
-const char *set_option_value(const int opt_idx, const OptVal value, int opt_flags)
+const char *set_option_value(const OptIndex opt_idx, const OptVal value, int opt_flags)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
static char errbuf[IOSIZE];
uint32_t flags = options[opt_idx].flags;
@@ -3832,13 +3833,13 @@ const char *set_option_value(const int opt_idx, const OptVal value, int opt_flag
/// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both).
///
/// @return NULL on success, an untranslated error message on error.
-const char *set_option_value_handle_tty(const char *name, int opt_idx, const OptVal value,
+const char *set_option_value_handle_tty(const char *name, OptIndex opt_idx, const OptVal value,
int opt_flags)
FUNC_ATTR_NONNULL_ARG(1)
{
static char errbuf[IOSIZE];
- if (opt_idx < 0) {
+ if (opt_idx == kOptInvalid) {
if (is_tty_option(name)) {
return NULL; // Fail silently; many old vimrcs set t_xx options.
}
@@ -3855,7 +3856,7 @@ const char *set_option_value_handle_tty(const char *name, int opt_idx, const Opt
/// @param opt_idx Option index in options[] table.
/// @param value Option value. If NIL_OPTVAL, the option value is cleared.
/// @param opt_flags OPT_LOCAL or 0 (both)
-void set_option_value_give_err(const int opt_idx, OptVal value, int opt_flags)
+void set_option_value_give_err(const OptIndex opt_idx, OptVal value, int opt_flags)
{
const char *errmsg = set_option_value(opt_idx, value, opt_flags);
@@ -4003,7 +4004,7 @@ static int optval_default(vimoption_T *p, const void *varp)
/// Send update to UIs with values of UI relevant options
void ui_refresh_options(void)
{
- for (int opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
+ for (OptIndex opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
uint32_t flags = options[opt_idx].flags;
if (!(flags & P_UI_OPTION)) {
continue;
@@ -4382,7 +4383,7 @@ void *get_varp_scope(vimoption_T *p, int scope)
/// Get pointer to option variable at 'opt_idx', depending on local or global
/// scope.
-void *get_option_varp_scope_from(int opt_idx, int scope, buf_T *buf, win_T *win)
+void *get_option_varp_scope_from(OptIndex opt_idx, int scope, buf_T *buf, win_T *win)
{
return get_varp_scope_from(&(options[opt_idx]), scope, buf, win);
}
@@ -4849,19 +4850,19 @@ void didset_window_options(win_T *wp, bool valid_cursor)
}
/// Index into the options table for a buffer-local option enum.
-static int buf_opt_idx[BV_COUNT];
+static OptIndex buf_opt_idx[BV_COUNT];
#define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].last_set
/// Initialize buf_opt_idx[] if not done already.
static void init_buf_opt_idx(void)
{
- static int did_init_buf_opt_idx = false;
+ static bool did_init_buf_opt_idx = false;
if (did_init_buf_opt_idx) {
return;
}
did_init_buf_opt_idx = true;
- for (int i = 0; options[i].fullname != NULL; i++) {
+ for (OptIndex i = 0; options[i].fullname != NULL; i++) {
if (options[i].indir & PV_BUF) {
buf_opt_idx[options[i].indir & PV_MASK] = i;
}
@@ -5167,7 +5168,7 @@ void set_imsearch_global(buf_T *buf)
p_imsearch = buf->b_p_imsearch;
}
-static int expand_option_idx = -1;
+static int expand_option_idx = kOptInvalid;
static int expand_option_start_col = 0;
static char expand_option_name[5] = { 't', '_', NUL, NUL, NUL };
static int expand_option_flags = 0;
@@ -5217,7 +5218,7 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags)
char nextchar;
uint32_t flags = 0;
- int opt_idx = 0;
+ OptIndex opt_idx = 0;
int is_term_option = false;
if (*arg == '<') {
@@ -5258,7 +5259,7 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags)
}
nextchar = *p;
opt_idx = findoption_len(arg, (size_t)(p - arg));
- if (opt_idx == -1 || options[opt_idx].var == NULL) {
+ if (opt_idx == kOptInvalid || options[opt_idx].var == NULL) {
xp->xp_context = EXPAND_NOTHING;
return;
}
@@ -5291,7 +5292,7 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags)
// Below are for handling expanding a specific option's value after the '=' or ':'
if (is_term_option) {
- expand_option_idx = -1;
+ expand_option_idx = kOptInvalid;
} else {
expand_option_idx = opt_idx;
}
@@ -5315,8 +5316,7 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags)
if (expand_option_subtract) {
xp->xp_context = EXPAND_SETTING_SUBTRACT;
return;
- } else if (expand_option_idx >= 0
- && options[expand_option_idx].opt_expand_cb != NULL) {
+ } else if (expand_option_idx != kOptInvalid && options[expand_option_idx].opt_expand_cb != NULL) {
xp->xp_context = EXPAND_STRING_SETTING;
} else if (*xp->xp_pattern == NUL) {
xp->xp_context = EXPAND_OLD_SETTING;
@@ -5542,7 +5542,7 @@ static char *escape_option_str_cmdline(char *var)
// The reverse is found at stropt_copy_value().
for (var = buf; *var != NUL; MB_PTR_ADV(var)) {
if (var[0] == '\\' && var[1] == '\\'
- && expand_option_idx >= 0
+ && expand_option_idx != kOptInvalid
&& (options[expand_option_idx].flags & P_EXPAND)
&& vim_isfilec((uint8_t)var[2])
&& (var[2] != '\\' || (var == buf && var[4] != '\\'))) {
@@ -5561,12 +5561,12 @@ int ExpandOldSetting(int *numMatches, char ***matches)
*numMatches = 0;
*matches = xmalloc(sizeof(char *));
- // For a terminal key code expand_option_idx is < 0.
- if (expand_option_idx < 0) {
+ // For a terminal key code expand_option_idx is kOptInvalid.
+ if (expand_option_idx == kOptInvalid) {
expand_option_idx = findoption(expand_option_name);
}
- if (expand_option_idx >= 0) {
+ if (expand_option_idx != kOptInvalid) {
// Put string of option value in NameBuff.
option_value2string(&options[expand_option_idx], expand_option_flags);
var = NameBuff;
@@ -5584,8 +5584,7 @@ int ExpandOldSetting(int *numMatches, char ***matches)
/// Expansion handler for :set=/:set+= when the option has a custom expansion handler.
int ExpandStringSetting(expand_T *xp, regmatch_T *regmatch, int *numMatches, char ***matches)
{
- if (expand_option_idx < 0
- || options[expand_option_idx].opt_expand_cb == NULL) {
+ if (expand_option_idx == kOptInvalid || options[expand_option_idx].opt_expand_cb == NULL) {
// Not supposed to reach this. This function is only for options with
// custom expansion callbacks.
return FAIL;
@@ -5617,7 +5616,7 @@ int ExpandStringSetting(expand_T *xp, regmatch_T *regmatch, int *numMatches, cha
/// Expansion handler for :set-=
int ExpandSettingSubtract(expand_T *xp, regmatch_T *regmatch, int *numMatches, char ***matches)
{
- if (expand_option_idx < 0) {
+ if (expand_option_idx == kOptInvalid) {
// term option
return ExpandOldSetting(numMatches, matches);
}
@@ -5802,18 +5801,18 @@ void vimrc_found(char *fname, char *envname)
/// @param[in] name Option name.
///
/// @return True if option was set.
-bool option_was_set(int opt_idx)
+bool option_was_set(OptIndex opt_idx)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
return options[opt_idx].flags & P_WAS_SET;
}
/// Reset the flag indicating option "name" was set.
///
/// @param[in] name Option name.
-void reset_option_was_set(int opt_idx)
+void reset_option_was_set(OptIndex opt_idx)
{
- assert(opt_idx >= 0);
+ assert(opt_idx != kOptInvalid);
options[opt_idx].flags &= ~P_WAS_SET;
}
@@ -5916,7 +5915,7 @@ int option_set_callback_func(char *optval, Callback *optcb)
static void didset_options_sctx(int opt_flags, int *buf)
{
for (int i = 0;; i++) {
- if (buf[i] == -1) {
+ if (buf[i] == kOptInvalid) {
break;
}
@@ -6137,7 +6136,7 @@ dict_T *get_winbuf_options(const int bufopt)
{
dict_T *const d = tv_dict_alloc();
- for (int opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
+ for (OptIndex opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
struct vimoption *opt = &options[opt_idx];
if ((bufopt && (opt->indir & PV_BUF))
@@ -6181,8 +6180,8 @@ int get_sidescrolloff_value(win_T *wp)
Dictionary get_vimoption(String name, int scope, buf_T *buf, win_T *win, Error *err)
{
- int opt_idx = findoption_len(name.data, name.size);
- VALIDATE_S(opt_idx >= 0, "option (not found)", name.data, {
+ OptIndex opt_idx = findoption_len(name.data, name.size);
+ VALIDATE_S(opt_idx != kOptInvalid, "option (not found)", name.data, {
return (Dictionary)ARRAY_DICT_INIT;
});
diff --git a/src/nvim/optionstr.c b/src/nvim/optionstr.c
index adb120fc21..002cc68a3b 100644
--- a/src/nvim/optionstr.c
+++ b/src/nvim/optionstr.c
@@ -283,7 +283,7 @@ static void set_string_option_global(vimoption_T *opt, char **varp)
/// Set a string option to a new value (without checking the effect).
/// The string is copied into allocated memory.
-/// if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
+/// 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".
@@ -291,7 +291,7 @@ static void set_string_option_global(vimoption_T *opt, char **varp)
/// @param opt_flags OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL.
///
/// TODO(famiu): Remove this and its win/buf variants.
-void set_string_option_direct(int opt_idx, const char *val, int opt_flags, int set_sid)
+void set_string_option_direct(OptIndex opt_idx, const char *val, int opt_flags, int set_sid)
{
vimoption_T *opt = get_option(opt_idx);
@@ -339,7 +339,7 @@ void set_string_option_direct(int opt_idx, const char *val, int opt_flags, int s
/// 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, int opt_idx, const char *val, int opt_flags,
+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;
@@ -355,7 +355,7 @@ void set_string_option_direct_in_win(win_T *wp, int opt_idx, const char *val, in
/// 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, int opt_idx, const char *val, int opt_flags,
+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;
diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c
index 87409bb5db..c7d9a65b86 100644
--- a/src/nvim/statusline.c
+++ b/src/nvim/statusline.c
@@ -296,7 +296,7 @@ static void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
char buf[MAXPATHL];
char transbuf[MAXPATHL];
char *stl;
- int opt_idx = -1;
+ OptIndex opt_idx = kOptInvalid;
int opt_scope = 0;
stl_hlrec_t *hltab;
StlClickRecord *tabtab;
@@ -922,9 +922,9 @@ int build_statuscol_str(win_T *wp, linenr_T lnum, linenr_T relnum, statuscol_T *
/// @param stcp Status column attributes (can be NULL)
///
/// @return The final width of the statusline
-int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, int opt_idx, int opt_scope,
- int fillchar, int maxwidth, stl_hlrec_t **hltab, StlClickRecord **tabtab,
- statuscol_T *stcp)
+int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, OptIndex opt_idx,
+ int opt_scope, int fillchar, int maxwidth, stl_hlrec_t **hltab,
+ StlClickRecord **tabtab, statuscol_T *stcp)
{
static size_t stl_items_len = 20; // Initial value, grows as needed.
static stl_item_t *stl_items = NULL;
@@ -958,8 +958,9 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, int opt_idx
}
// If "fmt" was set insecurely it needs to be evaluated in the sandbox.
- // "opt_idx" will be -1 when caller is nvim_eval_statusline().
- const int use_sandbox = (opt_idx != -1) ? was_set_insecurely(wp, opt_idx, opt_scope) : false;
+ // "opt_idx" will be kOptInvalid when caller is nvim_eval_statusline().
+ const int use_sandbox = (opt_idx != kOptInvalid) ? was_set_insecurely(wp, opt_idx, opt_scope)
+ : false;
// When the format starts with "%!" then evaluate it as an expression and
// use the result as the actual format string.
@@ -1544,7 +1545,7 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, int opt_idx
break;
case STL_SHOWCMD:
- if (p_sc && (opt_idx == -1 || findoption(p_sloc) == opt_idx)) {
+ if (p_sc && (opt_idx == kOptInvalid || findoption(p_sloc) == opt_idx)) {
str = showcmd_buf;
}
break;
@@ -2176,7 +2177,7 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, int opt_idx
// TODO(Bram): find out why using called_emsg_before makes tests fail, does it
// matter?
// if (called_emsg > called_emsg_before)
- if (opt_idx != -1 && did_emsg > did_emsg_before) {
+ if (opt_idx != kOptInvalid && did_emsg > did_emsg_before) {
set_string_option_direct(opt_idx, "", OPT_FREE | opt_scope, SID_ERROR);
}